{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"Published on April 12,2023. By Marília Prata, mpwolke.","metadata":{}},{"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","_kg_hide-input":true,"_kg_hide-output":true,"execution":{"iopub.status.busy":"2023-04-12T19:54:06.165262Z","iopub.execute_input":"2023-04-12T19:54:06.166237Z","iopub.status.idle":"2023-04-12T19:54:09.492015Z","shell.execute_reply.started":"2023-04-12T19:54:06.16603Z","shell.execute_reply":"2023-04-12T19:54:09.490354Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#Competition Citation\n\n@misc{image-matching-challenge-2023,\n\n    author = {Ashley Chow, Eduard Trulls, HCL-Jevster, Kwang Moo Yi, lcmrll, \n    old-ufo, Sohier Dane, tanjigou, WastedCode, Weiwei Sun},\n    \n    title = {Image Matching Challenge 2023},\n    \n    publisher = {Kaggle},\n    \n    year = {2023},\n    \n    url = {https://kaggle.com/competitions/image-matching-challenge-2023}\n}","metadata":{}},{"cell_type":"code","source":"import csv\nimport cv2\nimport math\nimport numpy as np\nimport os\nimport pandas as pd\n\nfrom collections import namedtuple\nfrom tqdm.notebook import tqdm as tqdm\n\ninput_dir = \"../input/image-matching-challenge-2023/train\"\noutput_dir = \"/kaggle/working\"","metadata":{"execution":{"iopub.status.busy":"2023-04-12T19:54:16.084061Z","iopub.execute_input":"2023-04-12T19:54:16.084573Z","iopub.status.idle":"2023-04-12T19:54:16.409157Z","shell.execute_reply.started":"2023-04-12T19:54:16.084527Z","shell.execute_reply":"2023-04-12T19:54:16.407708Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#By TMYOK https://www.kaggle.com/code/tmyok1984/imc2022-validation-csv\n\ndf = pd.read_csv(os.path.join(input_dir, \"train_labels.csv\"))\nall_scene = df[\"scene\"].unique()\n\nscene_list = []\nimage_id_list = []\nwidth_list = []\nheight_list = []\nfor scene in all_scene:\n\n    #df = pd.read_csv(os.path.join(input_dir, scene, \"calibration.csv\"))\n    image_ids = df[\"image_path\"].values\n\n    for image_id in tqdm(image_ids, desc=f\"{scene}\", dynamic_ncols=True):\n        img = cv2.imread(os.path.join(input_dir, scene, \"images\", f\"{image_id}.png\"))\n\n        scene_list.append(scene)\n        image_id_list.append(image_id)\n        #width_list.append(img.shape[1])\n        #height_list.append(img.shape[0])\n\ndf = pd.DataFrame({\"scene\":scene_list, \"image_id\":image_id_list})\ndf.to_csv(os.path.join(output_dir, \"validation_image_size.csv\"), index=False)","metadata":{"execution":{"iopub.status.busy":"2023-04-12T19:54:21.425454Z","iopub.execute_input":"2023-04-12T19:54:21.425889Z","iopub.status.idle":"2023-04-12T19:54:21.668539Z","shell.execute_reply.started":"2023-04-12T19:54:21.42585Z","shell.execute_reply":"2023-04-12T19:54:21.667034Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#I gave up and copied Kulyk (Leonid's code) \n\nAfter more than 7 hours struggling to plot some images I gave up and copied Kulyk clear code to show images and the 3D.\n\nhttps://www.kaggle.com/code/leonidkulyk/eda-imc-3d-plots-interactive-vis#-%F0%9F%8F%9B%EF%B8%8F-Image-Matching-Challenge---%F0%9F%93%8A-Exploratory-Data-Analysis","metadata":{}},{"cell_type":"code","source":"!pip install mediapy -q","metadata":{"execution":{"iopub.status.busy":"2023-04-12T19:54:28.156254Z","iopub.execute_input":"2023-04-12T19:54:28.156799Z","iopub.status.idle":"2023-04-12T19:54:43.428351Z","shell.execute_reply.started":"2023-04-12T19:54:28.156754Z","shell.execute_reply":"2023-04-12T19:54:43.426471Z"},"_kg_hide-output":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%cd /kaggle/working/\n!rm -rf /kaggle/working/Hierarchical-Localization\n!git clone --quiet --recursive https://github.com/cvg/Hierarchical-Localization/\n%cd /kaggle/working/Hierarchical-Localization\n!pip install -e .\n\nfrom hloc import extract_features, match_features, reconstruction, visualization, pairs_from_exhaustive\nfrom hloc.visualization import plot_images, read_image\nfrom hloc.utils import viz_3d\n\n%cd /kaggle/working/","metadata":{"execution":{"iopub.status.busy":"2023-04-12T19:54:56.229235Z","iopub.execute_input":"2023-04-12T19:54:56.229712Z","iopub.status.idle":"2023-04-12T19:55:31.937452Z","shell.execute_reply.started":"2023-04-12T19:54:56.229669Z","shell.execute_reply":"2023-04-12T19:55:31.935689Z"},"_kg_hide-output":true,"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#PyColpmap installation\n\nOpen with Colmap\n\ntrain/*/*/sfm A 3D reconstruction for this batch of images, which can be opened with colmap, the 3D structure-from-motion library bundled with this competition.\n\nhttps://www.kaggle.com/competitions/image-matching-challenge-2023/data","metadata":{}},{"cell_type":"code","source":"import os\nimport cv2\nimport pycolmap\n\nimport numpy as np\nimport mediapy as media\nimport pandas as pd\nimport plotly.express as px\nimport plotly.graph_objects as go\n\nfrom glob import glob\nfrom pathlib import Path\nfrom time import time","metadata":{"_kg_hide-output":true,"execution":{"iopub.status.busy":"2023-04-12T19:55:50.707055Z","iopub.execute_input":"2023-04-12T19:55:50.708256Z","iopub.status.idle":"2023-04-12T19:55:52.900639Z","shell.execute_reply.started":"2023-04-12T19:55:50.708184Z","shell.execute_reply":"2023-04-12T19:55:52.899295Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataset = 'phototourism'","metadata":{"execution":{"iopub.status.busy":"2023-04-12T20:11:53.333059Z","iopub.execute_input":"2023-04-12T20:11:53.333579Z","iopub.status.idle":"2023-04-12T20:11:53.341236Z","shell.execute_reply.started":"2023-04-12T20:11:53.333538Z","shell.execute_reply":"2023-04-12T20:11:53.339206Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#Taj Mahal Scene","metadata":{}},{"cell_type":"code","source":"scene = 'taj_mahal'\nsrc = f'/kaggle/input/image-matching-challenge-2023/train/{dataset}/{scene}'","metadata":{"execution":{"iopub.status.busy":"2023-04-12T20:13:38.830564Z","iopub.execute_input":"2023-04-12T20:13:38.831051Z","iopub.status.idle":"2023-04-12T20:13:38.837317Z","shell.execute_reply.started":"2023-04-12T20:13:38.831013Z","shell.execute_reply":"2023-04-12T20:13:38.835745Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#By Leonid Kulyk https://www.kaggle.com/code/leonidkulyk/eda-imc-3d-plots-interactive-vis#-%F0%9F%8F%9B%EF%B8%8F-Image-Matching-Challenge---%F0%9F%93%8A-Exploratory-Data-Analysis\n\nlimit = 12\nimages = [cv2.cvtColor(cv2.imread(im), cv2.COLOR_BGR2RGB) for im in glob(f'{src}/images/*')[:limit]]\nmedia.show_images(images, height=300, columns=3)","metadata":{"execution":{"iopub.status.busy":"2023-04-12T20:13:57.502168Z","iopub.execute_input":"2023-04-12T20:13:57.502637Z","iopub.status.idle":"2023-04-12T20:13:58.652969Z","shell.execute_reply.started":"2023-04-12T20:13:57.502594Z","shell.execute_reply":"2023-04-12T20:13:58.650688Z"},"_kg_hide-input":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#SFM 3D","metadata":{}},{"cell_type":"code","source":"#By Leonid Kulyk https://www.kaggle.com/code/leonidkulyk/eda-imc-3d-plots-interactive-vis#-%F0%9F%8F%9B%EF%B8%8F-Image-Matching-Challenge---%F0%9F%93%8A-Exploratory-Data-Analysis\n\nrec_gt = pycolmap.Reconstruction(f'{src}/sfm')\n\nfig = viz_3d.init_figure()\n# viz_3d.plot_cameras(fig, rec_gt, color='rgba(50,255,50, 0.5)', name=\"Ground Truth\", size=10)\nviz_3d.plot_reconstruction(fig, rec_gt, cameras = False, color='rgba(201,56,110,0.5)', name=\"Ground Truth\", cs=5)\nfig.show()","metadata":{"execution":{"iopub.status.busy":"2023-04-12T20:14:41.247501Z","iopub.execute_input":"2023-04-12T20:14:41.247974Z","iopub.status.idle":"2023-04-12T20:14:53.523598Z","shell.execute_reply.started":"2023-04-12T20:14:41.247936Z","shell.execute_reply":"2023-04-12T20:14:53.521422Z"},"_kg_hide-input":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"a 3D map of a scene from a small set of images and then localize an image downloaded from the Internet. This demo was contributed by Philipp Lindenberger.","metadata":{}},{"cell_type":"code","source":"#https://colab.research.google.com/drive/1MrVs9b8aQYODtOGkoaGNF9Nji3sbCNMQ\n\n!git clone --recursive https://github.com/cvg/Hierarchical-Localization/\nimport os\nos.chdir(\"./Hierarchical-Localization\")\n!python -m pip install -e .\n!curl https://cvg-data.inf.ethz.ch/hloc/netvlad/Pitts30K_struct.mat --create-dirs -o /kaggle/working/Hierarchical-Localization/third_party/netvlad/VGG16-NetVLAD-Pitts30K.mat\n!pip install --upgrade --quiet plotly\n\nimport pandas as pd\nimport numpy as np\nimport tqdm, tqdm.notebook\ntqdm.tqdm = tqdm.notebook.tqdm  # notebook-friendly progress bars\nfrom pathlib import Path\n\nfrom hloc import extract_features, match_features, reconstruction, pairs_from_retrieval\nfrom hloc.utils import viz_3d\nimport plotly.graph_objects as go","metadata":{"execution":{"iopub.status.busy":"2023-04-12T20:39:57.975163Z","iopub.execute_input":"2023-04-12T20:39:57.975694Z","iopub.status.idle":"2023-04-12T20:41:33.38199Z","shell.execute_reply.started":"2023-04-12T20:39:57.975651Z","shell.execute_reply":"2023-04-12T20:41:33.380646Z"},"_kg_hide-output":true,"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#SuperPoint\n\nSuperPoint local features with the SuperGlue matcher, but it's easy to switch to other features like SIFT or R2D2.\n\nhttps://colab.research.google.com/drive/1MrVs9b8aQYODtOGkoaGNF9Nji3sbCNMQ#scrollTo=a72ac394\n\nI tried to Run SfM though it didn't work as I expected.","metadata":{}},{"cell_type":"code","source":"#https://colab.research.google.com/drive/1MrVs9b8aQYODtOGkoaGNF9Nji3sbCNMQ\n\nimages = Path('datasets/sacre_coeur')\noutputs = Path('outputs/demo/')\n!rm -rf $outputs\nsfm_pairs = outputs / 'pairs-sfm.txt'\nloc_pairs = outputs / 'pairs-loc.txt'\nsfm_dir = outputs / 'sfm'\nfeatures = outputs / 'features.h5'\nmatches = outputs / 'matches.h5'\n\nfeature_conf = extract_features.confs['superpoint_aachen']\nmatcher_conf = match_features.confs['superglue']","metadata":{"execution":{"iopub.status.busy":"2023-04-12T20:52:28.682116Z","iopub.execute_input":"2023-04-12T20:52:28.683625Z","iopub.status.idle":"2023-04-12T20:52:29.813663Z","shell.execute_reply.started":"2023-04-12T20:52:28.683566Z","shell.execute_reply":"2023-04-12T20:52:29.811368Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"![](https://psarlin.com/superglue/assets/animations/teaser_animation_v2-all_compressed.gif)https://psarlin.com/superglue/","metadata":{}},{"cell_type":"code","source":"#https://colab.research.google.com/drive/1MrVs9b8aQYODtOGkoaGNF9Nji3sbCNMQ\n\nreferences = [str(p.relative_to(images)) for p in (images / 'mapping/').iterdir()]\nprint(len(references), \"mapping images\")\nplot_images([read_image(images / r) for r in references], dpi=25)","metadata":{"execution":{"iopub.status.busy":"2023-04-12T20:52:51.382215Z","iopub.execute_input":"2023-04-12T20:52:51.382692Z","iopub.status.idle":"2023-04-12T20:52:53.897513Z","shell.execute_reply.started":"2023-04-12T20:52:51.382647Z","shell.execute_reply":"2023-04-12T20:52:53.895934Z"},"_kg_hide-input":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#Structure from Motion (SfM)\n\n\"Structure from Motion (SfM) is the process of estimating the 3-D structure of a scene from a set of 2-D images.\"","metadata":{}},{"cell_type":"markdown","source":"#Run SfM\n\nTakes about 20 minutes for british museum","metadata":{}},{"cell_type":"code","source":"#By Jesper Andersson https://www.kaggle.com/code/jesperandersson/visualize-superglue-sfm-reconstruction\n\nscene = \"british_museum\"\n\nimages = Path(f'/kaggle/input/image-matching-challenge-2023/train/phototourism/{scene}/images')\n\noutputs = Path('outputs/sfm/')\nsfm_pairs = outputs / 'pairs-netvlad.txt' #pairs-netvlad\nsfm_dir = outputs / 'sfm_superpoint+superglue'\n\nretrieval_conf = extract_features.confs['netvlad']\nfeature_conf = extract_features.confs['superpoint_aachen']\nmatcher_conf = match_features.confs['superglue']\n\n\nretrieval_path = extract_features.main(retrieval_conf, images, outputs)\npairs_from_retrieval.main(retrieval_path, sfm_pairs, num_matched=5)\n\nfeature_path = extract_features.main(feature_conf, images, outputs)\nmatch_path = match_features.main(matcher_conf, sfm_pairs, feature_conf['output'], outputs)\n\nmodel = reconstruction.main(sfm_dir, images, sfm_pairs, feature_path, match_path, verbose=False)\n\nfig = viz_3d.init_figure()\n\nviz_3d.plot_reconstruction(fig, model, color='rgba(255,0,0,0.5)', points=True, cameras=False)\nviz_3d.plot_reconstruction(fig, model, color='rgba(0,255,0,0.5)', points=False, cameras=True)\n\nfig.show()","metadata":{"execution":{"iopub.status.busy":"2023-04-12T21:07:09.27461Z","iopub.execute_input":"2023-04-12T21:07:09.275149Z"},"_kg_hide-output":true,"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#Open with Colmap\n\ntrain/*/*/sfm A 3D reconstruction for this batch of images, which can be opened with colmap, the 3D structure-from-motion library bundled with this competition.\n\nhttps://www.kaggle.com/competitions/image-matching-challenge-2023/data","metadata":{}},{"cell_type":"markdown","source":"#Trying to install Colmap\n\nhttps://colmap.github.io/install.html","metadata":{}},{"cell_type":"code","source":"!git clone https://github.com/microsoft/vcpkg\ncd vcpkg\n.\\bootstrap-vcpkg.bat\n.\\vcpkg install colmap[cuda,tests]:x64-windows","metadata":{"execution":{"iopub.status.busy":"2023-04-12T20:28:45.979479Z","iopub.execute_input":"2023-04-12T20:28:45.980052Z","iopub.status.idle":"2023-04-12T20:28:45.991884Z","shell.execute_reply.started":"2023-04-12T20:28:45.980002Z","shell.execute_reply":"2023-04-12T20:28:45.989567Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#Could Not install Colmap as it's shown on their page.\n\nThat's why I copied Leonid Kulyk snippets to make the 3D\n\nhttps://colmap.github.io/install.html","metadata":{}},{"cell_type":"code","source":".\\vcpkg install colmap[cuda-redist]:x64-windows","metadata":{"execution":{"iopub.status.busy":"2023-04-12T20:29:45.032867Z","iopub.execute_input":"2023-04-12T20:29:45.033298Z","iopub.status.idle":"2023-04-12T20:29:45.040941Z","shell.execute_reply.started":"2023-04-12T20:29:45.033253Z","shell.execute_reply":"2023-04-12T20:29:45.039391Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#Acknowledgements:\n\nLeonid Kulyk https://www.kaggle.com/code/leonidkulyk/eda-imc-3d-plots-interactive-vis#-%F0%9F%8F%9B%EF%B8%8F-Image-Matching-Challenge---%F0%9F%93%8A-Exploratory-Data-Analysis\n\nJesper Andersson https://www.kaggle.com/code/jesperandersson/visualize-superglue-sfm-reconstruction\n\nTMYOK https://www.kaggle.com/code/tmyok1984/imc2022-validation-csv\n\nhttps://colab.research.google.com/drive/1MrVs9b8aQYODtOGkoaGNF9Nji3sbCNMQ\n\nhttps://colmap.github.io/install.html","metadata":{}},{"cell_type":"markdown","source":"#SuperGlue\n\n@inproceedings{sarlin20superglue,\n\n  title     = {{SuperGlue}: Learning Feature Matching with Graph Neural Networks},\n  \n  author    = {Paul-Edouard Sarlin and\n  \n               Daniel DeTone and\n               \n               Tomasz Malisiewicz and\n               \n               Andrew Rabinovich},\n               \n  booktitle = {CVPR},\n  \n  year      = {2020},\n  \n}\n© 2020 Paul-Edouard Sarlin","metadata":{}}]}