{"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":8015523,"sourceType":"competition"}],"dockerImageVersionId":30673,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"\n> \n\n> ****> NEW MODEL","metadata":{}},{"cell_type":"markdown","source":"<a id=\"top\"></a>\n# Table of Contents\n\n* ANALISIS EXPLORATORIO \n* VISUALIZACION DE DATOS \n*MODELO BASICO","metadata":{}},{"cell_type":"markdown","source":"<a id=\"0\"></a>\n# Install & Import dependencies","metadata":{"_kg_hide-input":false}},{"cell_type":"markdown","source":"<a id=\"1\"></a>\n# Dataset Overview","metadata":{}},{"cell_type":"markdown","source":"- `[train/test]/*/*/images`: A batch of images all taken near the same location. Some of training datasets may also contain a folder named images_full with additional images. The published test folder comprises a subset of the church scene from train and is provided solely for example purposes. The training data usually has a sequential capture ordering and significant image-to-image content overlap while the test set has limited image-to-image overlap and the image ordering is randomized.\n\n- `train/*/*/smf`: A 3-D reconstruction for this batch of images, which can be opened with colmap, the 3-D structure-from-motion library bundled with this competition.\n\n- `train/*/*/LICENSE.txt`: The license for this dataset.\n\n- `train/train_labels.csv`: A list of images in these datasets, with ground truths.","metadata":{}},{"cell_type":"markdown","source":"### 1️⃣ Lets inspect `train/train_labels.csv`","metadata":{}},{"cell_type":"markdown","source":"- `dataset`: The unique identifier for the dataset.\n- `scene`: The unique identifier for the scene.\n- `image_path`: The image filename, including the path.\n- `rotation_matrix`: The first target column. A 3x3 matrix, flattened into a vector in row-major convention, with values separated by `;`.\n- `translation_vector`: The second target column. A 3-D dimensional vector, with values separated by ;.","metadata":{}},{"cell_type":"code","source":"import pandas as pd","metadata":{"execution":{"iopub.status.busy":"2024-03-27T03:40:16.131684Z","iopub.execute_input":"2024-03-27T03:40:16.132332Z","iopub.status.idle":"2024-03-27T03:40:16.136479Z","shell.execute_reply.started":"2024-03-27T03:40:16.132296Z","shell.execute_reply":"2024-03-27T03:40:16.135516Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_labels = pd.read_csv(\"/kaggle/input/image-matching-challenge-2024/train/train_labels.csv\")\ntrain_labels","metadata":{"execution":{"iopub.status.busy":"2024-03-27T03:40:11.600803Z","iopub.execute_input":"2024-03-27T03:40:11.601170Z","iopub.status.idle":"2024-03-27T03:40:11.638856Z","shell.execute_reply.started":"2024-03-27T03:40:11.601141Z","shell.execute_reply":"2024-03-27T03:40:11.637972Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### 2️⃣ What is the relationship between datasets and scenes?","metadata":{}},{"cell_type":"code","source":"train_labels.groupby(\"dataset\")[\"scene\"].nunique()","metadata":{"execution":{"iopub.status.busy":"2024-03-27T03:40:22.977238Z","iopub.execute_input":"2024-03-27T03:40:22.978047Z","iopub.status.idle":"2024-03-27T03:40:22.994336Z","shell.execute_reply.started":"2024-03-27T03:40:22.978018Z","shell.execute_reply":"2024-03-27T03:40:22.993475Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"If we take a look at the datasets and scenes, we can observe that **there is only one scene per dataset** so we can disregard that for training","metadata":{}},{"cell_type":"markdown","source":"### 3️⃣ What is the distribution of the datasets?","metadata":{}},{"cell_type":"code","source":"!pip install px\n\n","metadata":{"execution":{"iopub.status.busy":"2024-03-27T03:40:29.345401Z","iopub.execute_input":"2024-03-27T03:40:29.346215Z","iopub.status.idle":"2024-03-27T03:40:42.642723Z","shell.execute_reply.started":"2024-03-27T03:40:29.346182Z","shell.execute_reply":"2024-03-27T03:40:42.641417Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pkg_resources\n\ntry:\n    pkg_resources.get_distribution('px')\nexcept pkg_resources.DistributionNotFound:\n    print(\"El módulo 'px' no está instalado.\")\n     \n","metadata":{"execution":{"iopub.status.busy":"2024-03-27T03:40:44.664560Z","iopub.execute_input":"2024-03-27T03:40:44.664951Z","iopub.status.idle":"2024-03-27T03:40:44.951405Z","shell.execute_reply.started":"2024-03-27T03:40:44.664919Z","shell.execute_reply":"2024-03-27T03:40:44.950496Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### 4️⃣ Let's take a closer look at the categories in the train dataset","metadata":{}},{"cell_type":"code","source":"train_categories = pd.read_csv(\"/kaggle/input/image-matching-challenge-2024/train/categories.csv\")\n\n# From comma separated list of categories for each dataset\n# To one dataset & category per row\ntrain_categories[\"category\"] = train_categories[\"categories\"].str.split(\";\")\ntrain_categories = train_categories.explode(\"category\")","metadata":{"execution":{"iopub.status.busy":"2024-03-27T03:40:56.022109Z","iopub.execute_input":"2024-03-27T03:40:56.022486Z","iopub.status.idle":"2024-03-27T03:40:56.047941Z","shell.execute_reply.started":"2024-03-27T03:40:56.022446Z","shell.execute_reply":"2024-03-27T03:40:56.047236Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### 5️⃣ How are the categories distributed?","metadata":{}},{"cell_type":"code","source":"!pip install pandas plotly-express\n\nimport pandas as pd\nimport plotly.express as px\n\n","metadata":{"execution":{"iopub.status.busy":"2024-03-27T03:41:03.313849Z","iopub.execute_input":"2024-03-27T03:41:03.314266Z","iopub.status.idle":"2024-03-27T03:41:16.288065Z","shell.execute_reply.started":"2024-03-27T03:41:03.314231Z","shell.execute_reply":"2024-03-27T03:41:16.287085Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### 6️⃣ What is the relationship between a scene  and a category? \n\n❗ Remember that scenes and datasets  are related one-to-one!","metadata":{}},{"cell_type":"markdown","source":"<a id=\"2\"></a>\n# Exploring each dataset","metadata":{}},{"cell_type":"code","source":"def explore(split: str, dataset: str, plot_image_limit: int = 12) -> None:\n    path = Path(\"/kaggle/input/image-matching-challenge-2024\") / split / dataset\n    images_path = path / \"images\"\n    smf_path = path / \"smf\"    \n\n    images = [cv2.cvtColor(cv2.imread(str(p)), cv2.COLOR_BGR2RGB) for p in list(images_path.glob(\"*\"))[:plot_image_limit]]\n    mediapy.show_images(images, height=300, columns=3)\n    \n    if split != \"test\":\n        rec_gt = pycolmap.Reconstruction(smf_path)\n\n        fig = viz_3d.init_figure()\n        viz_3d.plot_reconstruction(fig, rec_gt, cameras=False, color='rgba(227,168,30,0.5)', name=\"Ground Truth\", cs=5)\n        fig.show()","metadata":{"_kg_hide-input":true,"_kg_hide-output":true,"execution":{"iopub.status.busy":"2024-03-27T03:41:19.494135Z","iopub.execute_input":"2024-03-27T03:41:19.494503Z","iopub.status.idle":"2024-03-27T03:41:19.501698Z","shell.execute_reply.started":"2024-03-27T03:41:19.494475Z","shell.execute_reply":"2024-03-27T03:41:19.500788Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"/kaggle/input/image-matching-challenge-2024/train/church/smf\")","metadata":{"execution":{"iopub.status.busy":"2024-03-27T03:41:27.260337Z","iopub.execute_input":"2024-03-27T03:41:27.260706Z","iopub.status.idle":"2024-03-27T03:41:27.265729Z","shell.execute_reply.started":"2024-03-27T03:41:27.260676Z","shell.execute_reply":"2024-03-27T03:41:27.264771Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a id=\"church\"></a>\n## 1️⃣ Church","metadata":{}},{"cell_type":"markdown","source":"The *Church of the Most Sacred Heart of Our Lord* is a Roman Catholic church located in  Jiřího z Poděbrad Square in Prague's Vinohrady district.\n\n- This was one of three new buildings constructed in 1929 in Prague, inspired by the 1000th anniversary of the death of St. Wenceslas.\n- It is considered one of the most significant Czech religious constructions of the 20th century.}\n- During World War II, the six bells from the tower were melted down for arms production, and in 1992, two copies were returned. Since 2010, the church has been ranked among national cultural monuments. [Click here for more information](https://en.wikipedia.org/wiki/Church_of_the_Most_Sacred_Heart_of_Our_Lord).\n\n[Explore the location on Google Maps!](https://www.google.com/maps/place/Kostel+Nejsv%C4%9Bt%C4%9Bj%C5%A1%C3%ADho+Srdce+P%C3%A1n%C4%9B/@50.0780029,14.4477159,17z/data=!3m1!4b1!4m6!3m5!1s0x470b949cd90326df:0xdf5dfb58f652dbac!8m2!3d50.0779995!4d14.4502908!16s%2Fm%2F0t524gl?authuser=0&entry=ttu)","metadata":{}},{"cell_type":"code","source":"!pip install pycolmap\nimport pycolmap\n\n","metadata":{"execution":{"iopub.status.busy":"2024-03-27T03:41:34.992636Z","iopub.execute_input":"2024-03-27T03:41:34.993314Z","iopub.status.idle":"2024-03-27T03:41:47.861754Z","shell.execute_reply.started":"2024-03-27T03:41:34.993281Z","shell.execute_reply":"2024-03-27T03:41:47.860599Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a id=\"dioscuri\"></a>\n## 2️⃣ Dioscuri","metadata":{}},{"cell_type":"markdown","source":"The *Temple of the Dioscuri* is a beautiful temple located in Agrigento, Italy.\n❗ This temple was also featured in the [2023 edition of the IMC competition](https://www.kaggle.com/competitions/image-matching-challenge-2023).\n\n- It was built in the middle of the 5th century BCE. The preserved four columns prove that they were made in the Doric order. The building had six columns on both sides; of the other two, thirteen.\n- Dioscuri were twins of divine origin who were worshipped in ancient Greece and Rome. According to Greek mythology, they took part in the Argonaut’s expedition, and after their death, Zeus (Jupiter) placed them in the sky as a constellation of Twins.\n- The temple is located in the so-called The Valley of the Temples in the central part of Sicily, in Agrigento (Roman Agrigentum, Greek Akragas).\n\n[Explore the location on Google Maps!](https://www.google.com/maps/place/Temple+of+the+Dioscuri/@37.2913186,13.5815322,15z/data=!4m6!3m5!1s0x13108230e21d2e2f:0x9f9aa044be1dff05!8m2!3d37.2913186!4d13.5815322!16s%2Fg%2F1234zsmq?hl=en&entry=ttu)","metadata":{}},{"cell_type":"code","source":"import matplotlib.pyplot as plt  # For basic 2D/3D plotting\nimport seaborn as sns ","metadata":{"execution":{"iopub.status.busy":"2024-03-27T03:41:47.863588Z","iopub.execute_input":"2024-03-27T03:41:47.863895Z","iopub.status.idle":"2024-03-27T03:41:48.780684Z","shell.execute_reply.started":"2024-03-27T03:41:47.863865Z","shell.execute_reply":"2024-03-27T03:41:48.779889Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a id=\"lizard\"></a>\n## 3️⃣ Lizard","metadata":{}},{"cell_type":"markdown","source":"This lizard appears to be in the *Rangherka & Herold Park*, situated in the heart of Vršovice district in Prague, Czech Republic.  \n- The park is named after the Italian businessman Rangheri, who planted mulberry orchards here and founded Prague’s silk industry.\n- The Chateau in the park was also named after him. Nowadays there is a retirement home and a ceremonial hall.\n- This cute lizard is wandering around the Herold orchards (Sluneční hodiny). \n\n[Explore the location on Google Maps!](https://www.google.com/maps/place/Slune%C4%8Dn%C3%AD+hodiny/@50.069777,14.4531037,19.42z/data=!4m15!1m8!3m7!1s0x470b937e6803b4ed:0xb9e4c6c93639025c!2sHerold+orchards!8m2!3d50.0702835!4d14.4530186!10e5!16s%2Fg%2F122kj785!3m5!1s0x470b9300e8f5e8cd:0x82fd9a75524f3758!8m2!3d50.0697772!4d14.4531745!16s%2Fg%2F11lgky0py0?authuser=0&entry=ttu)","metadata":{}},{"cell_type":"code","source":"!pip install explore\n!pip install mediapy\nimport mediapy\nimport cv2\n","metadata":{"execution":{"iopub.status.busy":"2024-03-27T03:41:54.958532Z","iopub.execute_input":"2024-03-27T03:41:54.959217Z","iopub.status.idle":"2024-03-27T03:42:20.216539Z","shell.execute_reply.started":"2024-03-27T03:41:54.959184Z","shell.execute_reply":"2024-03-27T03:42:20.215513Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a id=\"temple\"></a>\n## 4️⃣ Temple","metadata":{}},{"cell_type":"code","source":"import tensorflow as tf\nfrom tensorflow.keras import models, layers\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\n","metadata":{"execution":{"iopub.status.busy":"2024-03-27T03:42:41.694377Z","iopub.execute_input":"2024-03-27T03:42:41.695052Z","iopub.status.idle":"2024-03-27T03:42:41.699394Z","shell.execute_reply.started":"2024-03-27T03:42:41.695019Z","shell.execute_reply":"2024-03-27T03:42:41.698502Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Carga del archivo de etiquetas\ntrain_labels = pd.read_csv(\"/kaggle/input/image-matching-challenge-2024/train/train_labels.csv\")\n\n# Extracción de rutas de imágenes y etiquetas\nimage_paths = train_labels[\"image_name\"].tolist()\nlabels = train_labels[\"dataset\"].tolist()\n\n# Generador de datos de entrenamiento\ntrain_datagen = ImageDataGenerator(rescale=1./255)\ntrain_generator = train_datagen.flow_from_directory(\"/kaggle/input/image-matching-challenge-2024\"),\ntarget_size=(224, 224),\nbatch_size=32,\nclass_mode=\"categorical\"\n\n                   \n                                               \n","metadata":{"execution":{"iopub.status.busy":"2024-03-27T03:42:48.776583Z","iopub.execute_input":"2024-03-27T03:42:48.777504Z","iopub.status.idle":"2024-03-27T03:42:49.605343Z","shell.execute_reply.started":"2024-03-27T03:42:48.777448Z","shell.execute_reply":"2024-03-27T03:42:49.604594Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Generador de datos de validación\nval_datagen = ImageDataGenerator(rescale=1./255)\nval_generator = val_datagen.flow_from_directory(\"/kaggle/input/image-matching-challenge-2024/train/pond\",\ntarget_size=(224, 224),\nbatch_size=32,\nclass_mode='categorical')\n                            ","metadata":{"execution":{"iopub.status.busy":"2024-03-27T03:42:56.010729Z","iopub.execute_input":"2024-03-27T03:42:56.011071Z","iopub.status.idle":"2024-03-27T03:42:56.057906Z","shell.execute_reply.started":"2024-03-27T03:42:56.011046Z","shell.execute_reply":"2024-03-27T03:42:56.056991Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_generator = train_datagen.flow_from_directory(\"/kaggle/input/image-matching-challenge-2024/train/pond\")","metadata":{"execution":{"iopub.status.busy":"2024-03-27T03:43:03.583330Z","iopub.execute_input":"2024-03-27T03:43:03.583734Z","iopub.status.idle":"2024-03-27T03:43:03.626750Z","shell.execute_reply.started":"2024-03-27T03:43:03.583702Z","shell.execute_reply":"2024-03-27T03:43:03.625872Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Modelo ResNet50 pre-entrenado\nbase_model = tf.keras.applications.ResNet50(\n    include_top=False,\n    weights=\"imagenet\",\n    input_shape=(224, 224, 3)\n)","metadata":{"execution":{"iopub.status.busy":"2024-03-27T03:43:08.886091Z","iopub.execute_input":"2024-03-27T03:43:08.886801Z","iopub.status.idle":"2024-03-27T03:43:11.484021Z","shell.execute_reply.started":"2024-03-27T03:43:08.886770Z","shell.execute_reply":"2024-03-27T03:43:11.483236Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Añadir capas para la clasificación específica del problema\nmodel = models.Sequential([\n    base_model,\n    layers.GlobalAveragePooling2D(),\n    layers.Dense(128, activation=\"relu\"),\n    layers.Dense(len(train_labels[\"dataset\"].unique()), activation=\"softmax\")\n])\n#Compilación del modelo\nmodel.compile(optimizer=\"adam\", loss=\"categorical_crossentropy\", metrics=[\"accuracy\"])","metadata":{"execution":{"iopub.status.busy":"2024-03-27T03:43:21.176895Z","iopub.execute_input":"2024-03-27T03:43:21.177260Z","iopub.status.idle":"2024-03-27T03:43:21.193041Z","shell.execute_reply.started":"2024-03-27T03:43:21.177229Z","shell.execute_reply":"2024-03-27T03:43:21.192108Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred = model.predict(val_generator)","metadata":{"execution":{"iopub.status.busy":"2024-03-27T03:43:27.094616Z","iopub.execute_input":"2024-03-27T03:43:27.095336Z","iopub.status.idle":"2024-03-27T03:44:31.224644Z","shell.execute_reply.started":"2024-03-27T03:43:27.095307Z","shell.execute_reply":"2024-03-27T03:44:31.223702Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred","metadata":{"execution":{"iopub.status.busy":"2024-03-27T03:44:42.378893Z","iopub.execute_input":"2024-03-27T03:44:42.380020Z","iopub.status.idle":"2024-03-27T03:44:42.387428Z","shell.execute_reply.started":"2024-03-27T03:44:42.379983Z","shell.execute_reply":"2024-03-27T03:44:42.386522Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission_df = pd.DataFrame(y_pred)\n","metadata":{"execution":{"iopub.status.busy":"2024-03-27T03:45:44.042728Z","iopub.execute_input":"2024-03-27T03:45:44.043090Z","iopub.status.idle":"2024-03-27T03:45:44.047899Z","shell.execute_reply.started":"2024-03-27T03:45:44.043061Z","shell.execute_reply":"2024-03-27T03:45:44.046850Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission_data = pd.DataFrame(y_pred)\n","metadata":{"execution":{"iopub.status.busy":"2024-03-27T03:45:46.812177Z","iopub.execute_input":"2024-03-27T03:45:46.812561Z","iopub.status.idle":"2024-03-27T03:45:46.817081Z","shell.execute_reply.started":"2024-03-27T03:45:46.812531Z","shell.execute_reply":"2024-03-27T03:45:46.816069Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission = submission_data","metadata":{"execution":{"iopub.status.busy":"2024-03-27T03:45:49.728989Z","iopub.execute_input":"2024-03-27T03:45:49.729354Z","iopub.status.idle":"2024-03-27T03:45:49.733426Z","shell.execute_reply.started":"2024-03-27T03:45:49.729324Z","shell.execute_reply":"2024-03-27T03:45:49.732392Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nsubmission.to_csv(\"submission.csv\", index=False)\nsubmission.head()","metadata":{"execution":{"iopub.status.busy":"2024-03-27T03:45:58.379093Z","iopub.execute_input":"2024-03-27T03:45:58.379854Z","iopub.status.idle":"2024-03-27T03:45:58.403752Z","shell.execute_reply.started":"2024-03-27T03:45:58.379821Z","shell.execute_reply":"2024-03-27T03:45:58.402914Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a id=\"pond\"></a>\n## 5️⃣ Pond","metadata":{}},{"cell_type":"markdown","source":"<a id=\"cup\"></a>\n## 6️⃣ Glass Cup","metadata":{}},{"cell_type":"markdown","source":"Not much to say here, this appears to be the upper half of a green-ish transparent glass cup.","metadata":{}},{"cell_type":"markdown","source":"<a id=\"cylinder\"></a>\n## 7️⃣ Glass Cylinder","metadata":{}},{"cell_type":"markdown","source":"Again not much here. It looks like a white-ish transparent glass cylinder, similar to a test tube like the ones that could be found in a chemical testing laboratory.","metadata":{}},{"cell_type":"markdown","source":"# Work in progress!","metadata":{}}]}