{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"nvidiaTeslaT4","dataSources":[{"sourceId":49349,"databundleVersionId":5447706,"sourceType":"competition"},{"sourceId":7290652,"sourceType":"datasetVersion","datasetId":4228338},{"sourceId":7295563,"sourceType":"datasetVersion","datasetId":4231604}],"dockerImageVersionId":30558,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"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,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nprint(os.getcwd())","metadata":{"execution":{"iopub.status.busy":"2023-12-27T14:03:46.771136Z","iopub.status.idle":"2023-12-27T14:03:46.771457Z","shell.execute_reply.started":"2023-12-27T14:03:46.771299Z","shell.execute_reply":"2023-12-27T14:03:46.771313Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torch\ntorch.cuda.is_available()","metadata":{"execution":{"iopub.status.busy":"2023-12-27T14:03:46.773007Z","iopub.status.idle":"2023-12-27T14:03:46.773334Z","shell.execute_reply.started":"2023-12-27T14:03:46.773170Z","shell.execute_reply":"2023-12-27T14:03:46.773185Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nos._exit(00)","metadata":{"execution":{"iopub.status.busy":"2023-12-27T14:03:46.775292Z","iopub.status.idle":"2023-12-27T14:03:46.775663Z","shell.execute_reply.started":"2023-12-27T14:03:46.775477Z","shell.execute_reply":"2023-12-27T14:03:46.775493Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n!git clone --branch kaggle https://github.com/RaphaelHaddad/3D_reconstruction_Tsinghua_2023.git\n\n%cd \"./3D_reconstruction_Tsinghua_2023/\"\n%mv * ../\n%cd ..\n%rm -r \"3D_reconstruction_Tsinghua_2023\"\n!pip install -r requirements.txt\n!pip install flash_attn\n\nfrom pipeline import main\n\nmain()","metadata":{"execution":{"iopub.status.busy":"2023-12-28T16:57:04.800854Z","iopub.execute_input":"2023-12-28T16:57:04.801478Z","iopub.status.idle":"2023-12-28T17:00:48.693585Z","shell.execute_reply.started":"2023-12-28T16:57:04.801445Z","shell.execute_reply":"2023-12-28T17:00:48.692096Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!rm -r ./colmap\n!rm -r ./build\n# !git clone https://github.com/colmap/colmap.git\n# !cd colmap\n# !git checkout dev\n# !mkdir build\n# !cd build\n# !cmake .. -D CUDA_ENABLED=ON","metadata":{"execution":{"iopub.status.busy":"2023-12-28T11:59:14.164782Z","iopub.execute_input":"2023-12-28T11:59:14.165545Z","iopub.status.idle":"2023-12-28T11:59:16.061850Z","shell.execute_reply.started":"2023-12-28T11:59:14.165509Z","shell.execute_reply":"2023-12-28T11:59:16.060563Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pycolmap\nimport os\nfrom pathlib import Path\nfrom kp_imc23.config.paths import DataPaths\nimport torch\n\npaths = DataPaths(data_dir=Path(\"../input/image-matching-challenge-2023/\"),\n        output_dir=Path(\"./output\"),\n        dataset=\"heritage\",\n        scene=\"cyprus\",\n        mode=\"train\")\n\ndatabase_path = \"output/heritage/cyprus/database-2.db\"\noutput_path = \"output/heritage/cyprus/colmap_rec\"\nimg_dir = Path(\"output/heritage/cyprus/images_rotated\")\n\nos.makedirs(output_path, exist_ok=True)\n\ndbpath = Path(\"../input/database/database3.db\")\ndef find_gpu_index():\n    try:\n        cuda_available = torch.cuda.is_available()\n        if cuda_available:\n            # Get the index of the first available GPU\n            cuda_device = torch.cuda.current_device()\n            print(cuda_device)\n            cuda_index = torch.cuda.get_device_properties(cuda_device).index\n            return cuda_index\n        else:\n            return None\n    except Exception as e:\n        print(f\"Error finding GPU index: {e}\")\n        return None\n    \nopts = pycolmap.SiftExtractionOptions()\nopts.max_num_features=50\n# opts.gpu_index='0'\npycolmap.extract_features(database_path=database_path, image_path=paths.input_dir_images,sift_options=opts)\npycolmap.match_exhaustive(database_path=database_path)\n\nmapper_options = pycolmap.IncrementalMapperOptions()\nmapper_options.min_model_size = 2\n# mapper_options.feature_quality = pycolmap.FeatureQuality.HIGH\nmapper_options.min_num_matches = 1\nmapper_options.ba_local_max_num_iterations = 200\nmapper_options.ba_global_max_num_iterations = 200\n\nmaps = pycolmap.incremental_mapping(database_path=database_path, image_path=paths.input_dir_images,output_path=output_path, options=mapper_options)\n\nprint(paths.database_path)","metadata":{"execution":{"iopub.status.busy":"2023-12-28T12:02:44.884539Z","iopub.execute_input":"2023-12-28T12:02:44.885750Z","iopub.status.idle":"2023-12-28T12:09:15.399997Z","shell.execute_reply.started":"2023-12-28T12:02:44.885708Z","shell.execute_reply":"2023-12-28T12:09:15.398820Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"maps","metadata":{"execution":{"iopub.status.busy":"2023-12-28T12:11:03.638555Z","iopub.execute_input":"2023-12-28T12:11:03.639283Z","iopub.status.idle":"2023-12-28T12:11:03.648504Z","shell.execute_reply.started":"2023-12-28T12:11:03.639246Z","shell.execute_reply":"2023-12-28T12:11:03.647438Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from kp_imc23.config.paths import DataPaths\nimport os\nfrom kp_imc23.matching.save_matches_keypoints import keypoints_to_out_match_unique_kpts, register_keypoints, register_matches, import_into_colmap, create_submission\nfrom kp_imc23.matching.colmap import COLMAP_mapping, COLMAP_result_analysis\n\noutput_path = \"output/heritage/cyprus/colmap_rec\"\npaths = DataPaths(data_dir=Path(\"../input/image-matching-challenge-2023/\"),\n        output_dir=Path(\"./output\"),\n        dataset=\"heritage\",\n        scene=\"cyprus\",\n        mode=\"train\")\n\n# maps = COLMAP_mapping(output_path, database_path, paths.input_dir_images)\n\n# Results analysis\nout_results = COLMAP_result_analysis(maps, \"heritage\", \"cyprus\")\nimage_list = os.listdir(paths.input_dir_images)\nout_results\n\n# Create submission\ncreate_submission(out_results, image_list, \"./submission.csv\",\"heritage\",\"cyprus\")","metadata":{"execution":{"iopub.status.busy":"2023-12-28T12:16:59.680045Z","iopub.execute_input":"2023-12-28T12:16:59.681056Z","iopub.status.idle":"2023-12-28T12:16:59.691506Z","shell.execute_reply.started":"2023-12-28T12:16:59.681007Z","shell.execute_reply":"2023-12-28T12:16:59.690546Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pip show pycolmap","metadata":{"execution":{"iopub.status.busy":"2023-12-28T07:59:10.250595Z","iopub.execute_input":"2023-12-28T07:59:10.251550Z","iopub.status.idle":"2023-12-28T07:59:21.728865Z","shell.execute_reply.started":"2023-12-28T07:59:10.251508Z","shell.execute_reply":"2023-12-28T07:59:21.727409Z"},"trusted":true},"execution_count":null,"outputs":[]}]}