{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"gpu","dataSources":[{"sourceId":71885,"databundleVersionId":8069805,"sourceType":"competition"},{"sourceId":5689030,"sourceType":"datasetVersion","datasetId":3270810},{"sourceId":5860481,"sourceType":"datasetVersion","datasetId":3369908},{"sourceId":7884485,"sourceType":"datasetVersion","datasetId":4628051},{"sourceId":7884725,"sourceType":"datasetVersion","datasetId":4628331},{"sourceId":168912882,"sourceType":"kernelVersion"}],"dockerImageVersionId":30674,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"Thanks for https://www.kaggle.com/code/asarvazyan/imc-understanding-the-baseline","metadata":{}},{"cell_type":"markdown","source":"# Install Packages & Improve relevent models","metadata":{}},{"cell_type":"code","source":"!git clone --quiet --recursive https://github.com/cvg/Hierarchical-Localization/\n%cd Hierarchical-Localization\n!pip install --progress-bar off --quiet -e .\n!pip install --progress-bar off --quiet --upgrade plotly","metadata":{"execution":{"iopub.status.busy":"2024-04-02T09:11:43.159121Z","iopub.execute_input":"2024-04-02T09:11:43.159558Z","iopub.status.idle":"2024-04-02T09:12:52.603824Z","shell.execute_reply.started":"2024-04-02T09:11:43.159517Z","shell.execute_reply":"2024-04-02T09:12:52.602526Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import 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, visualization, pairs_from_exhaustive, match_dense, pairs_from_retrieval\nfrom hloc.visualization import plot_images, read_image\nfrom hloc.utils import viz_3d\nimport gc","metadata":{"execution":{"iopub.status.busy":"2024-04-02T08:50:04.497271Z","iopub.execute_input":"2024-04-02T08:50:04.497698Z","iopub.status.idle":"2024-04-02T08:50:04.504065Z","shell.execute_reply.started":"2024-04-02T08:50:04.497656Z","shell.execute_reply":"2024-04-02T08:50:04.503156Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Setting Paths about features，matches...","metadata":{}},{"cell_type":"code","source":"images = Path('/kaggle/input/image-matching-challenge-2024/test/church/images')\noutputs = Path('/kaggle/working/demo')\n!rm -rf $outputs\nsfm_pairs = outputs / 'pairs-sfm.txt'\nloc_pairs = outputs / 'pairs-loc.txt'\nsfm_dir = outputs / 'sfm'\nfeatures = outputs / 'keypoints.h5'\nmatches = outputs / 'matches.h5'\nreference_sfm = outputs / \"sfm_loftr\"\n\n# feature_conf = extract_features.confs['superpoint_aachen']\n# matcher_conf = match_features.confs['superpoint+lightglue']\n\nretrieval_conf = extract_features.confs[\"disk\"]\nmatcher_conf = match_dense.confs[\"loftr\"]","metadata":{"execution":{"iopub.status.busy":"2024-04-02T08:50:04.505212Z","iopub.execute_input":"2024-04-02T08:50:04.505498Z","iopub.status.idle":"2024-04-02T08:50:05.498313Z","shell.execute_reply.started":"2024-04-02T08:50:04.505473Z","shell.execute_reply":"2024-04-02T08:50:05.496909Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Plot images","metadata":{}},{"cell_type":"code","source":"references = [str(p.relative_to(images)) for p in (images).iterdir()]\nprint(len(references), \"mapping images\")\nplot_images([read_image(images / r) for r in references], dpi=25)","metadata":{"execution":{"iopub.status.busy":"2024-04-02T08:50:05.500267Z","iopub.execute_input":"2024-04-02T08:50:05.500935Z","iopub.status.idle":"2024-04-02T08:50:13.427009Z","shell.execute_reply.started":"2024-04-02T08:50:05.500904Z","shell.execute_reply":"2024-04-02T08:50:13.426028Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Extract features and matching","metadata":{}},{"cell_type":"code","source":"# extract_features.main(retrieval_conf, images, image_list=references, feature_path=features)\nglobal_descriptors = extract_features.main(retrieval_conf, images, outputs)\npairs_from_exhaustive.main(sfm_pairs, image_list=references)\nmatch_dense.main(matcher_conf, sfm_pairs, images, outputs, features=features, matches=matches);","metadata":{"execution":{"iopub.status.busy":"2024-04-02T08:50:13.429101Z","iopub.execute_input":"2024-04-02T08:50:13.429400Z","iopub.status.idle":"2024-04-02T08:54:55.575576Z","shell.execute_reply.started":"2024-04-02T08:50:13.429373Z","shell.execute_reply":"2024-04-02T08:54:55.574801Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Reconstruction","metadata":{}},{"cell_type":"code","source":"model = reconstruction.main(sfm_dir, images, sfm_pairs, features, matches, image_list=references, mapper_options={\"min_model_size\": 3, # By default colmap does not generate a reconstruction if less than 10 images are registered. Lower it to 3.\n        \"max_num_models\": 2})\ngc.collect()","metadata":{"execution":{"iopub.status.busy":"2024-04-02T08:54:55.576895Z","iopub.execute_input":"2024-04-02T08:54:55.577277Z","iopub.status.idle":"2024-04-02T09:05:45.861178Z","shell.execute_reply.started":"2024-04-02T08:54:55.577231Z","shell.execute_reply":"2024-04-02T09:05:45.860316Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model","metadata":{"execution":{"iopub.status.busy":"2024-04-02T09:05:45.862359Z","iopub.execute_input":"2024-04-02T09:05:45.862666Z","iopub.status.idle":"2024-04-02T09:05:45.868459Z","shell.execute_reply.started":"2024-04-02T09:05:45.862640Z","shell.execute_reply":"2024-04-02T09:05:45.867597Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig = viz_3d.init_figure()\nviz_3d.plot_reconstruction(fig, model, color='rgba(255,0,0,0.5)', name=\"mapping\", points_rgb=True)\nfig.show()","metadata":{"execution":{"iopub.status.busy":"2024-04-02T09:10:04.195842Z","iopub.execute_input":"2024-04-02T09:10:04.196519Z","iopub.status.idle":"2024-04-02T09:10:05.757640Z","shell.execute_reply.started":"2024-04-02T09:10:04.196486Z","shell.execute_reply":"2024-04-02T09:10:05.756740Z"},"trusted":true},"execution_count":null,"outputs":[]}]}