{"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":"code","source":"import os\nimport cv2\nimport numpy as np\nimport pandas as pd\nimport tensorflow as tf\nfrom tensorflow.keras import Sequential\nfrom tensorflow.keras.layers import Conv2D, MaxPooling2D, Flatten, Dense, Dropout, BatchNormalization\nfrom tensorflow.keras.callbacks import ReduceLROnPlateau, EarlyStopping\nfrom tensorflow.keras.optimizers import Adam\nfrom sklearn.model_selection import train_test_split","metadata":{"execution":{"iopub.status.busy":"2023-04-17T23:03:40.366328Z","iopub.execute_input":"2023-04-17T23:03:40.366968Z","iopub.status.idle":"2023-04-17T23:03:49.092156Z","shell.execute_reply.started":"2023-04-17T23:03:40.366923Z","shell.execute_reply":"2023-04-17T23:03:49.090962Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def preprocess_data(data_dir, labels_path):\n    # Load labels CSV\n    labels_df = pd.read_csv(labels_path)\n\n    # Preprocess images and labels\n    images = []\n    poses = []\n    for _, row in labels_df.iterrows():\n        img_path = os.path.join(data_dir, row['image_path'])\n        img = cv2.imread(img_path)\n\n        if img is None:\n            print(f\"Failed to load image: {img_path}\")\n            continue\n\n        img = cv2.resize(img, (224, 224))  # Resize image\n        img = img / 255.0  # Normalize pixel values\n\n        rotation = np.array(row['rotation_matrix'].split(';'), dtype=float).reshape(3, 3)\n        translation = np.array(row['translation_vector'].split(';'), dtype=float)\n\n        pose = np.hstack((rotation, translation.reshape(-1, 1)))\n        pose = pose.flatten()  # Flatten the pose matrix to match the model output shape\n\n        images.append(img)\n        poses.append(pose)\n\n    images = np.array(images)\n    poses = np.array(poses)\n\n    return images, poses\n","metadata":{"execution":{"iopub.status.busy":"2023-04-17T23:03:49.094325Z","iopub.execute_input":"2023-04-17T23:03:49.095180Z","iopub.status.idle":"2023-04-17T23:03:49.106200Z","shell.execute_reply.started":"2023-04-17T23:03:49.095136Z","shell.execute_reply":"2023-04-17T23:03:49.105043Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_dir = \"/kaggle/input/image-matching-challenge-2023/train\"\nlabels_path = \"/kaggle/input/image-matching-challenge-2023/train/train_labels.csv\"\n\nimages, poses = preprocess_data(data_dir, labels_path)\nX_train, X_val, y_train, y_val = train_test_split(images, poses, test_size=0.2, random_state=42)","metadata":{"execution":{"iopub.status.busy":"2023-04-17T23:03:49.107975Z","iopub.execute_input":"2023-04-17T23:03:49.108645Z","iopub.status.idle":"2023-04-17T23:04:36.375181Z","shell.execute_reply.started":"2023-04-17T23:03:49.108608Z","shell.execute_reply":"2023-04-17T23:04:36.374111Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom tensorflow.keras.layers import Flatten\n\n# Data augmentation\ndata_gen = ImageDataGenerator(rotation_range=20,\n                              width_shift_range=0.2,\n                              height_shift_range=0.2,\n                              shear_range=0.2,\n                              zoom_range=0.2,\n                              horizontal_flip=True,\n                              fill_mode='nearest')\n\ndata_gen.fit(X_train)","metadata":{"execution":{"iopub.status.busy":"2023-04-17T23:04:36.377560Z","iopub.execute_input":"2023-04-17T23:04:36.377990Z","iopub.status.idle":"2023-04-17T23:04:36.506834Z","shell.execute_reply.started":"2023-04-17T23:04:36.377950Z","shell.execute_reply":"2023-04-17T23:04:36.505775Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.applications.resnet50 import ResNet50\nfrom tensorflow.keras.models import Model\nfrom tensorflow.keras.layers import GlobalAveragePooling2D, Dense, Dropout\nfrom tensorflow.keras.optimizers import Adam\nfrom tensorflow.keras.callbacks import ReduceLROnPlateau, EarlyStopping, ModelCheckpoint\nfrom keras.layers import Input\n\ndef create_model():\n    input_img = Input(shape=(224, 224, 3))\n    x = Conv2D(64, (3, 3), activation='relu', padding='same')(input_img)\n    x = Conv2D(64, (3, 3), activation='relu', padding='same')(x)\n    x = MaxPooling2D((2, 2), padding='same')(x)\n    x = Conv2D(128, (3, 3), activation='relu', padding='same')(x)\n    x = Conv2D(128, (3, 3), activation='relu', padding='same')(x)\n    x = MaxPooling2D((2, 2), padding='same')(x)\n    x = Conv2D(256, (3, 3), activation='relu', padding='same')(x)\n    x = Conv2D(256, (3, 3), activation='relu', padding='same')(x)\n    x = MaxPooling2D((2, 2), padding='same')(x)\n    x = Conv2D(512, (3, 3), activation='relu', padding='same')(x)\n    x = Conv2D(512, (3, 3), activation='relu', padding='same')(x)\n    x = MaxPooling2D((2, 2), padding='same')(x)\n    x = Flatten()(x)\n    x = Dense(1024, activation='relu')(x)\n    x = Dense(512, activation='relu')(x)\n    pose_output = Dense(12, activation=None)(x)\n\n    model = Model(inputs=input_img, outputs=pose_output)\n    model.compile(optimizer='adam', loss='mean_squared_error')\n\n    return model","metadata":{"execution":{"iopub.status.busy":"2023-04-17T23:04:41.181330Z","iopub.execute_input":"2023-04-17T23:04:41.182258Z","iopub.status.idle":"2023-04-17T23:04:41.195633Z","shell.execute_reply.started":"2023-04-17T23:04:41.182205Z","shell.execute_reply":"2023-04-17T23:04:41.194641Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"initial_learning_rate = 1e-4\n\nmodel = create_model()\nmodel.compile(optimizer=Adam(learning_rate=initial_learning_rate), loss='mean_squared_error')\n\nhistory = model.fit(data_gen.flow(X_train, y_train, batch_size=1),\n                    validation_data=(X_val, y_val),\n                    epochs=75,\n                    steps_per_epoch=len(X_train) // 1)","metadata":{"execution":{"iopub.status.busy":"2023-04-17T23:09:36.058240Z","iopub.execute_input":"2023-04-17T23:09:36.059321Z","iopub.status.idle":"2023-04-17T23:17:10.773984Z","shell.execute_reply.started":"2023-04-17T23:09:36.059274Z","shell.execute_reply":"2023-04-17T23:17:10.773005Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def preprocess_test_data(data_dir, image_paths):\n    images = []\n    for image_path in image_paths:\n        img_path = os.path.join(data_dir, image_path)\n        img = cv2.imread(img_path)\n\n        if img is None:\n            print(f\"Failed to load image: {img_path}\")\n            continue\n\n        img = cv2.resize(img, (224, 224))  # Resize image\n        img = img / 255.0  # Normalize pixel values\n\n        images.append(img)\n\n    images = np.array(images)\n\n    return images","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Load the sample submission file\nsample_submission_path = \"/kaggle/input/image-matching-challenge-2023/sample_submission.csv\"\nsample_submission_df = pd.read_csv(sample_submission_path)\ntest_image_paths = sample_submission_df['image_path'].values\n\n# Preprocess the test data\ntest_data_dir = \"/kaggle/input/image-matching-challenge-2023/test\"\ntest_images_processed = preprocess_test_data(test_data_dir, test_image_paths)\n\n# Predict the poses using the trained model\npredicted_poses = model.predict(test_images_processed)\n\n# Create a submission file with the predicted poses\nsample_submission_df.iloc[:, 1:] = predicted_poses\nsample_submission_df.to_csv(\"submission.csv\", index=False)","metadata":{"execution":{"iopub.status.busy":"2023-04-17T22:30:54.347306Z","iopub.execute_input":"2023-04-17T22:30:54.348363Z","iopub.status.idle":"2023-04-17T22:30:54.361348Z","shell.execute_reply.started":"2023-04-17T22:30:54.348311Z","shell.execute_reply":"2023-04-17T22:30:54.360338Z"},"trusted":true},"execution_count":null,"outputs":[]}]}