{"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":"none","dataSources":[{"sourceId":71885,"databundleVersionId":8015523,"sourceType":"competition"},{"sourceId":7959914,"sourceType":"datasetVersion","datasetId":4682399}],"dockerImageVersionId":30673,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"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","execution":{"iopub.status.busy":"2024-03-27T20:26:43.831150Z","iopub.execute_input":"2024-03-27T20:26:43.831629Z","iopub.status.idle":"2024-03-27T20:26:46.239803Z","shell.execute_reply.started":"2024-03-27T20:26:43.831590Z","shell.execute_reply":"2024-03-27T20:26:46.238006Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# packages\n\n# standard\nimport numpy as np\nimport pandas as pd\n\n# plots\nimport matplotlib.pyplot as plt\nimport plotly.express as px\nimport seaborn as sns\nfrom scipy.spatial.transform import Rotation as R","metadata":{"execution":{"iopub.status.busy":"2024-03-27T20:28:16.811385Z","iopub.execute_input":"2024-03-27T20:28:16.811932Z","iopub.status.idle":"2024-03-27T20:28:16.818717Z","shell.execute_reply.started":"2024-03-27T20:28:16.811888Z","shell.execute_reply":"2024-03-27T20:28:16.817542Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# configs\npd.set_option('display.max_columns', None) # we want to display all columns in this notebook\npd.set_option('display.max_colwidth', None)\n\n# aesthetics\ndefault_color_1 = 'darkblue'\ndefault_color_2 = 'darkgreen'\ndefault_color_3 = 'darkred'","metadata":{"execution":{"iopub.status.busy":"2024-03-27T20:28:29.036730Z","iopub.execute_input":"2024-03-27T20:28:29.037590Z","iopub.status.idle":"2024-03-27T20:28:29.044889Z","shell.execute_reply.started":"2024-03-27T20:28:29.037536Z","shell.execute_reply":"2024-03-27T20:28:29.043443Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# load training data table\ndf_train = pd.read_csv(\"/kaggle/input/image-matching-challenge-2024/sample_submission.csv\")\ndf_train.head()","metadata":{"execution":{"iopub.status.busy":"2024-03-27T20:29:03.246025Z","iopub.execute_input":"2024-03-27T20:29:03.246746Z","iopub.status.idle":"2024-03-27T20:29:03.285419Z","shell.execute_reply.started":"2024-03-27T20:29:03.246701Z","shell.execute_reply":"2024-03-27T20:29:03.283962Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# dataset feature - frequencies\ndf_train.dataset.value_counts()","metadata":{"execution":{"iopub.status.busy":"2024-03-27T20:29:45.614647Z","iopub.execute_input":"2024-03-27T20:29:45.615213Z","iopub.status.idle":"2024-03-27T20:29:45.635979Z","shell.execute_reply.started":"2024-03-27T20:29:45.615153Z","shell.execute_reply":"2024-03-27T20:29:45.634559Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# scene feature - frequencies\ndf_train.scene.value_counts()","metadata":{"execution":{"iopub.status.busy":"2024-03-27T20:29:52.431714Z","iopub.execute_input":"2024-03-27T20:29:52.432611Z","iopub.status.idle":"2024-03-27T20:29:52.441914Z","shell.execute_reply.started":"2024-03-27T20:29:52.432563Z","shell.execute_reply":"2024-03-27T20:29:52.440771Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# convert input string to matrix\ndef get_rotation_matrix_from_string(i_matrix_string):\n    # replace semicolon by comma\n    my_string = i_matrix_string.replace(';',',')\n    # split to list\n    my_list = my_string.split(',')\n    # convert each element from string to double\n    my_list = [float(s) for s in my_list]\n    # convert list to array\n    my_array = np.array(my_list)\n    # convert array to 3x3-matrix\n    rot_matrix = np.asmatrix(my_array.reshape(3,3))\n    return rot_matrix\n","metadata":{"execution":{"iopub.status.busy":"2024-03-27T20:30:03.251847Z","iopub.execute_input":"2024-03-27T20:30:03.252433Z","iopub.status.idle":"2024-03-27T20:30:03.260557Z","shell.execute_reply.started":"2024-03-27T20:30:03.252378Z","shell.execute_reply":"2024-03-27T20:30:03.259162Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# convert matrix (in string representation) to rotation vector\ndef get_rotation_vector(i_matrix_string): \n    mat = get_rotation_matrix_from_string(i_matrix_string)\n    rot = R.from_matrix(mat)\n    vec = rot.as_rotvec()\n    return vec","metadata":{"execution":{"iopub.status.busy":"2024-03-27T20:30:10.886747Z","iopub.execute_input":"2024-03-27T20:30:10.887402Z","iopub.status.idle":"2024-03-27T20:30:10.896191Z","shell.execute_reply.started":"2024-03-27T20:30:10.887334Z","shell.execute_reply":"2024-03-27T20:30:10.894506Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# convert matrix (in string representation) to euler angles\ndef get_rotation_angles(i_matrix_string): \n    mat = get_rotation_matrix_from_string(i_matrix_string)\n    rot = R.from_matrix(mat)\n    vec = rot.as_euler('zyx', degrees=True)\n    return vec","metadata":{"execution":{"iopub.status.busy":"2024-03-27T20:30:18.074745Z","iopub.execute_input":"2024-03-27T20:30:18.075240Z","iopub.status.idle":"2024-03-27T20:30:18.082155Z","shell.execute_reply.started":"2024-03-27T20:30:18.075197Z","shell.execute_reply":"2024-03-27T20:30:18.080647Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# example\nmy_example = df_train.rotation_matrix[7]\nprint('Rotation matrix:')\nprint(get_rotation_matrix_from_string(my_example))\nprint()\nprint('Converted to rotation vector:')\nprint(get_rotation_vector(my_example))\nprint()\nprint('Converted to Euler angles:')\nprint(get_rotation_angles(my_example))","metadata":{"execution":{"iopub.status.busy":"2024-03-27T20:30:25.222400Z","iopub.execute_input":"2024-03-27T20:30:25.222883Z","iopub.status.idle":"2024-03-27T20:30:25.240015Z","shell.execute_reply.started":"2024-03-27T20:30:25.222850Z","shell.execute_reply":"2024-03-27T20:30:25.238888Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_rotation_vector_x(i_matrix_string):\n    vec = get_rotation_vector(i_matrix_string)\n    return vec[0]\n\ndef get_rotation_vector_y(i_matrix_string):\n    vec = get_rotation_vector(i_matrix_string)\n    return vec[1]\n\ndef get_rotation_vector_z(i_matrix_string):\n    vec = get_rotation_vector(i_matrix_string)\n    return vec[2]\n\ndef get_rotation_angle_1(i_matrix_string):\n    vec = get_rotation_angles(i_matrix_string)\n    return vec[0]\n\ndef get_rotation_angle_2(i_matrix_string):\n    vec = get_rotation_angles(i_matrix_string)\n    return vec[1]\n\ndef get_rotation_angle_3(i_matrix_string):\n    vec = get_rotation_angles(i_matrix_string)\n    return vec[2]","metadata":{"execution":{"iopub.status.busy":"2024-03-27T20:30:37.166890Z","iopub.execute_input":"2024-03-27T20:30:37.167427Z","iopub.status.idle":"2024-03-27T20:30:37.177732Z","shell.execute_reply.started":"2024-03-27T20:30:37.167386Z","shell.execute_reply":"2024-03-27T20:30:37.175730Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# add rotation vector components and norm to data frame\ndf_train['rot_x'] = df_train.rotation_matrix.apply(get_rotation_vector_x)\ndf_train['rot_y'] = df_train.rotation_matrix.apply(get_rotation_vector_y)\ndf_train['rot_z'] = df_train.rotation_matrix.apply(get_rotation_vector_z)\ndf_train['rot_norm'] = np.sqrt(df_train.rot_x**2 + df_train.rot_y**2 + df_train.rot_z**2)\n\n# add Euler angles (in degrees)\ndf_train['angle_1'] = df_train.rotation_matrix.apply(get_rotation_angle_1)\ndf_train['angle_2'] = df_train.rotation_matrix.apply(get_rotation_angle_2)\ndf_train['angle_3'] = df_train.rotation_matrix.apply(get_rotation_angle_3)","metadata":{"execution":{"iopub.status.busy":"2024-03-27T20:30:45.468706Z","iopub.execute_input":"2024-03-27T20:30:45.469218Z","iopub.status.idle":"2024-03-27T20:30:45.499127Z","shell.execute_reply.started":"2024-03-27T20:30:45.469183Z","shell.execute_reply":"2024-03-27T20:30:45.497227Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# statistics\nrot_features = ['rot_x','rot_y','rot_z','rot_norm',\n                'angle_1', 'angle_2', 'angle_3']\ndf_train[rot_features].describe()","metadata":{"execution":{"iopub.status.busy":"2024-03-27T20:30:53.168567Z","iopub.execute_input":"2024-03-27T20:30:53.169072Z","iopub.status.idle":"2024-03-27T20:30:53.224991Z","shell.execute_reply.started":"2024-03-27T20:30:53.169034Z","shell.execute_reply":"2024-03-27T20:30:53.223575Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# distributions\nfor f in rot_features:\n    plt.figure(figsize=(8,3))\n    df_train[f].plot(kind='hist', bins=50, color=default_color_1)\n    plt.title(f)\n    plt.grid()\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2024-03-27T20:31:01.474910Z","iopub.execute_input":"2024-03-27T20:31:01.475432Z","iopub.status.idle":"2024-03-27T20:31:03.859690Z","shell.execute_reply.started":"2024-03-27T20:31:01.475389Z","shell.execute_reply":"2024-03-27T20:31:03.858488Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# interactive 3d plot using plotly\nfig = px.scatter_3d(df_train, \n                    x='rot_x', y='rot_y', z='rot_z',\n                    color='dataset',\n                    hover_data=['image_path'],\n                    opacity=0.5)\nfig.update_traces(marker_size = 5)\nfig.update_layout(title='Rotation Vectors')\nfig.show()","metadata":{"execution":{"iopub.status.busy":"2024-03-27T20:32:35.562034Z","iopub.execute_input":"2024-03-27T20:32:35.562583Z","iopub.status.idle":"2024-03-27T20:32:38.106821Z","shell.execute_reply.started":"2024-03-27T20:32:35.562547Z","shell.execute_reply":"2024-03-27T20:32:38.105243Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import 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/imc2024-packages-lightglue-rerun-kornia'):\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\n\n\n!pip install --no-index /kaggle/input/imc2024-packages-lightglue-rerun-kornia/* --no-deps\n!mkdir -p /root/.cache/torch/hub/checkpoints\n!cp /kaggle/input/aliked/pytorch/aliked-n16/1/* /root/.cache/torch/hub/checkpoints/\n!cp /kaggle/input/lightglue/pytorch/aliked/1/* /root/.cache/torch/hub/checkpoints/\n!cp /kaggle/input/lightglue/pytorch/aliked/1/aliked_lightglue.pth /root/.cache/torch/hub/checkpoints/aliked_lightglue_v0-1_arxiv-pth","metadata":{"execution":{"iopub.status.busy":"2024-03-27T20:44:06.575842Z","iopub.execute_input":"2024-03-27T20:44:06.577879Z","iopub.status.idle":"2024-03-27T20:44:13.948682Z","shell.execute_reply.started":"2024-03-27T20:44:06.577813Z","shell.execute_reply":"2024-03-27T20:44:13.946904Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"execution":{"iopub.status.busy":"2024-03-27T20:37:19.141174Z","iopub.execute_input":"2024-03-27T20:37:19.141722Z","iopub.status.idle":"2024-03-27T20:37:19.158487Z","shell.execute_reply.started":"2024-03-27T20:37:19.141683Z","shell.execute_reply":"2024-03-27T20:37:19.156786Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}