{"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 numpy as np\nimport pandas as pd\nimport os\nfrom torch.utils.data import Dataset, DataLoader\nfrom sklearn.model_selection import train_test_split\nimport cv2\nimport albumentations as A\nfrom albumentations.pytorch.transforms import ToTensorV2\nimport torch\nimport torch.nn as nn\nimport torch.nn.functional as F\nimport torch.optim as optim\nfrom tqdm import tqdm","metadata":{"_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","execution":{"iopub.execute_input":"2023-05-06T06:54:16.453235Z","iopub.status.busy":"2023-05-06T06:54:16.452023Z","iopub.status.idle":"2023-05-06T06:54:20.943183Z","shell.execute_reply":"2023-05-06T06:54:20.942138Z"},"papermill":{"duration":4.503302,"end_time":"2023-05-06T06:54:20.946050","exception":false,"start_time":"2023-05-06T06:54:16.442748","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_label = pd.read_csv(\"/kaggle/input/image-matching-challenge-2023/train/train_labels.csv\")","metadata":{"execution":{"iopub.execute_input":"2023-05-06T06:54:20.960908Z","iopub.status.busy":"2023-05-06T06:54:20.960112Z","iopub.status.idle":"2023-05-06T06:54:20.980420Z","shell.execute_reply":"2023-05-06T06:54:20.979494Z"},"papermill":{"duration":0.029946,"end_time":"2023-05-06T06:54:20.982736","exception":false,"start_time":"2023-05-06T06:54:20.952790","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_folder = \"/kaggle/input/image-matching-challenge-2023/train/\"","metadata":{"execution":{"iopub.execute_input":"2023-05-06T06:54:20.997475Z","iopub.status.busy":"2023-05-06T06:54:20.996400Z","iopub.status.idle":"2023-05-06T06:54:21.001522Z","shell.execute_reply":"2023-05-06T06:54:21.000566Z"},"papermill":{"duration":0.014601,"end_time":"2023-05-06T06:54:21.003722","exception":false,"start_time":"2023-05-06T06:54:20.989121","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_transform = A.Compose([\n                            A.Resize(224,224),\n                            A.Normalize(mean=(0.485, 0.456, 0.406), std=(0.229, 0.224, 0.225), max_pixel_value=255.0, always_apply=False, p=1.0),\n                            ToTensorV2()\n                            ])\n\ntest_transform = A.Compose([\n                            A.Resize(224,224),\n                            A.Normalize(mean=(0.485, 0.456, 0.406), std=(0.229, 0.224, 0.225), max_pixel_value=255.0, always_apply=False, p=1.0),\n                            ToTensorV2()\n                            ])","metadata":{"execution":{"iopub.execute_input":"2023-05-06T06:54:21.017950Z","iopub.status.busy":"2023-05-06T06:54:21.017658Z","iopub.status.idle":"2023-05-06T06:54:21.024485Z","shell.execute_reply":"2023-05-06T06:54:21.023426Z"},"papermill":{"duration":0.016547,"end_time":"2023-05-06T06:54:21.026764","exception":false,"start_time":"2023-05-06T06:54:21.010217","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class CustomDataset(Dataset):\n    def __init__(self, root, img_path_list, rotation_label_list, translation_label_list, transforms=None):\n        self.root = root\n        self.img_path_list = img_path_list\n        self.rotation_label_list = rotation_label_list\n        self.translation_label_list = translation_label_list\n        self.transforms = transforms\n        \n    def __getitem__(self, index):\n        img_path = self.img_path_list[index]\n        img_path = self.root + img_path\n        image = cv2.imread(img_path)\n        \n        if self.transforms is not None:\n            image = self.transforms(image=image)['image']\n        \n        rotation_label = self.rotation_label_list[index].split(\";\")\n        rotation_label = list(map(float, rotation_label))\n        \n        translation_label = self.translation_label_list[index].split(\";\")\n        translation_label = list(map(float, translation_label))\n        \n        return image, np.array(rotation_label), np.array(translation_label)\n        \n    def __len__(self):\n        return len(self.img_path_list)","metadata":{"execution":{"iopub.execute_input":"2023-05-06T06:54:21.041627Z","iopub.status.busy":"2023-05-06T06:54:21.041336Z","iopub.status.idle":"2023-05-06T06:54:21.049590Z","shell.execute_reply":"2023-05-06T06:54:21.048445Z"},"papermill":{"duration":0.018274,"end_time":"2023-05-06T06:54:21.051737","exception":false,"start_time":"2023-05-06T06:54:21.033463","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"valid_set = train_label.sample(frac=0.3)","metadata":{"execution":{"iopub.execute_input":"2023-05-06T06:54:21.066733Z","iopub.status.busy":"2023-05-06T06:54:21.065853Z","iopub.status.idle":"2023-05-06T06:54:21.073137Z","shell.execute_reply":"2023-05-06T06:54:21.072137Z"},"papermill":{"duration":0.016978,"end_time":"2023-05-06T06:54:21.075330","exception":false,"start_time":"2023-05-06T06:54:21.058352","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_set = train_label[~train_label[\"image_path\"].isin(valid_set[\"image_path\"])]","metadata":{"execution":{"iopub.execute_input":"2023-05-06T06:54:21.090447Z","iopub.status.busy":"2023-05-06T06:54:21.090159Z","iopub.status.idle":"2023-05-06T06:54:21.101543Z","shell.execute_reply":"2023-05-06T06:54:21.100483Z"},"papermill":{"duration":0.020626,"end_time":"2023-05-06T06:54:21.103676","exception":false,"start_time":"2023-05-06T06:54:21.083050","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_label[\"rotation_matrix_split\"] = train_label.apply(lambda x:list(map(float, x[\"rotation_matrix\"].split(\";\"))), axis=1)\ntrain_label[\"translation_vector_split\"] = train_label.apply(lambda x:list(map(float, x[\"translation_vector\"].split(\";\"))), axis=1)","metadata":{"execution":{"iopub.execute_input":"2023-05-06T06:54:21.117527Z","iopub.status.busy":"2023-05-06T06:54:21.116985Z","iopub.status.idle":"2023-05-06T06:54:21.134484Z","shell.execute_reply":"2023-05-06T06:54:21.133550Z"},"papermill":{"duration":0.026679,"end_time":"2023-05-06T06:54:21.136606","exception":false,"start_time":"2023-05-06T06:54:21.109927","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"rotation_value = np.array(train_label[\"rotation_matrix_split\"].tolist())\ntranslation_value = np.array(train_label[\"translation_vector_split\"].tolist())","metadata":{"execution":{"iopub.execute_input":"2023-05-06T06:54:21.151019Z","iopub.status.busy":"2023-05-06T06:54:21.150108Z","iopub.status.idle":"2023-05-06T06:54:21.156243Z","shell.execute_reply":"2023-05-06T06:54:21.155282Z"},"papermill":{"duration":0.015479,"end_time":"2023-05-06T06:54:21.158368","exception":false,"start_time":"2023-05-06T06:54:21.142889","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"np.min(rotation_value), np.max(rotation_value), np.min(translation_value), np.max(translation_value)","metadata":{"execution":{"iopub.execute_input":"2023-05-06T06:54:21.172219Z","iopub.status.busy":"2023-05-06T06:54:21.171853Z","iopub.status.idle":"2023-05-06T06:54:21.179608Z","shell.execute_reply":"2023-05-06T06:54:21.178577Z"},"papermill":{"duration":0.01772,"end_time":"2023-05-06T06:54:21.182245","exception":false,"start_time":"2023-05-06T06:54:21.164525","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"translation_value[:, 0].min(), translation_value[:, 0].max(), translation_value[:, 1].min(), translation_value[:, 1].max(), translation_value[:, 2].min(), translation_value[:, 2].max()","metadata":{"execution":{"iopub.execute_input":"2023-05-06T06:54:21.196503Z","iopub.status.busy":"2023-05-06T06:54:21.195919Z","iopub.status.idle":"2023-05-06T06:54:21.203870Z","shell.execute_reply":"2023-05-06T06:54:21.202812Z"},"papermill":{"duration":0.017206,"end_time":"2023-05-06T06:54:21.205972","exception":false,"start_time":"2023-05-06T06:54:21.188766","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"translation_value[:, 0].mean(), translation_value[:, 1].mean(), translation_value[:, 2].mean()","metadata":{"execution":{"iopub.execute_input":"2023-05-06T06:54:21.220196Z","iopub.status.busy":"2023-05-06T06:54:21.219891Z","iopub.status.idle":"2023-05-06T06:54:21.226591Z","shell.execute_reply":"2023-05-06T06:54:21.225532Z"},"papermill":{"duration":0.016386,"end_time":"2023-05-06T06:54:21.228893","exception":false,"start_time":"2023-05-06T06:54:21.212507","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"translation_value[:, 0].std(), translation_value[:, 1].std(), translation_value[:, 2].std()","metadata":{"execution":{"iopub.execute_input":"2023-05-06T06:54:21.243729Z","iopub.status.busy":"2023-05-06T06:54:21.242960Z","iopub.status.idle":"2023-05-06T06:54:21.250319Z","shell.execute_reply":"2023-05-06T06:54:21.249273Z"},"papermill":{"duration":0.017023,"end_time":"2023-05-06T06:54:21.252536","exception":false,"start_time":"2023-05-06T06:54:21.235513","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataset = CustomDataset(train_folder, train_set[\"image_path\"].tolist(), train_set[\"rotation_matrix\"].tolist(), train_set[\"translation_vector\"].tolist(), transforms=train_transform)","metadata":{"execution":{"iopub.execute_input":"2023-05-06T06:54:21.268492Z","iopub.status.busy":"2023-05-06T06:54:21.267496Z","iopub.status.idle":"2023-05-06T06:54:21.273430Z","shell.execute_reply":"2023-05-06T06:54:21.272512Z"},"papermill":{"duration":0.016047,"end_time":"2023-05-06T06:54:21.275474","exception":false,"start_time":"2023-05-06T06:54:21.259427","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_loader = DataLoader(dataset, batch_size = 4, shuffle=True, num_workers=0)","metadata":{"execution":{"iopub.execute_input":"2023-05-06T06:54:21.290910Z","iopub.status.busy":"2023-05-06T06:54:21.290011Z","iopub.status.idle":"2023-05-06T06:54:21.296615Z","shell.execute_reply":"2023-05-06T06:54:21.295618Z"},"papermill":{"duration":0.016719,"end_time":"2023-05-06T06:54:21.298892","exception":false,"start_time":"2023-05-06T06:54:21.282173","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"val_dataset = CustomDataset(train_folder, valid_set[\"image_path\"].tolist(), valid_set[\"rotation_matrix\"].tolist(), valid_set[\"translation_vector\"].tolist(), transforms=test_transform)","metadata":{"execution":{"iopub.execute_input":"2023-05-06T06:54:21.314537Z","iopub.status.busy":"2023-05-06T06:54:21.313567Z","iopub.status.idle":"2023-05-06T06:54:21.319308Z","shell.execute_reply":"2023-05-06T06:54:21.318428Z"},"papermill":{"duration":0.015528,"end_time":"2023-05-06T06:54:21.321360","exception":false,"start_time":"2023-05-06T06:54:21.305832","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"val_loader = DataLoader(val_dataset, batch_size=4, shuffle=False)","metadata":{"execution":{"iopub.execute_input":"2023-05-06T06:54:21.336495Z","iopub.status.busy":"2023-05-06T06:54:21.335916Z","iopub.status.idle":"2023-05-06T06:54:21.341006Z","shell.execute_reply":"2023-05-06T06:54:21.340029Z"},"papermill":{"duration":0.014784,"end_time":"2023-05-06T06:54:21.343158","exception":false,"start_time":"2023-05-06T06:54:21.328374","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class BaseModel(nn.Module):\n    def __init__(self, dropout=0.2):\n        super(BaseModel, self).__init__()\n        self.layer1 = nn.Sequential(\n            nn.Conv2d(3, 64, kernel_size=3, stride=1, padding=1),\n            nn.BatchNorm2d(64),\n            nn.ReLU(),\n            nn.Conv2d(64, 64, kernel_size=3, stride=1, padding=1), \n            nn.BatchNorm2d(64), \n            nn.ReLU(),\n            nn.Dropout(dropout),\n            nn.MaxPool2d(kernel_size=2))\n        self.layer2 = nn.Sequential(\n            nn.Conv2d(64, 128, kernel_size=3, stride=1, padding=1), \n            nn.BatchNorm2d(128),\n            nn.ReLU(),\n            nn.Conv2d(128, 128, kernel_size=3, stride=1, padding=1),\n            nn.BatchNorm2d(128), \n            nn.ReLU(),\n            nn.Dropout(dropout),\n            nn.MaxPool2d(kernel_size=2))\n        self.layer3 = nn.Sequential(\n            nn.Conv2d(128, 256, kernel_size=3, stride=1, padding=1), \n            nn.BatchNorm2d(256),\n            nn.ReLU(),\n            nn.Conv2d(256, 256, kernel_size=3, stride=1, padding=1),\n            nn.BatchNorm2d(256),\n            nn.ReLU(),\n            nn.Conv2d(256, 256, kernel_size=3, stride=1, padding=1),\n            nn.BatchNorm2d(256), \n            nn.ReLU(), \n            nn.Dropout(dropout),\n            nn.MaxPool2d(kernel_size=2))\n        self.layer4 = nn.Sequential(\n            nn.Conv2d(256, 512, kernel_size=3, stride=1, padding=1), \n            nn.BatchNorm2d(512),\n            nn.ReLU(),\n            nn.Conv2d(512, 512, kernel_size=3, stride=1, padding=1),\n            nn.BatchNorm2d(512),\n            nn.ReLU(),\n            nn.Conv2d(512, 512, kernel_size=3, stride=1, padding=1),\n            nn.BatchNorm2d(512), \n            nn.ReLU(), \n            nn.Dropout(dropout),\n            nn.MaxPool2d(kernel_size=2))\n        self.layer5 = nn.Sequential(\n            nn.Conv2d(512, 512, kernel_size=3, stride=1, padding=1), \n            nn.BatchNorm2d(512),\n            nn.ReLU(),\n            nn.Conv2d(512, 512, kernel_size=3, stride=1, padding=1),\n            nn.BatchNorm2d(512),\n            nn.ReLU(),\n            nn.Conv2d(512, 512, kernel_size=3, stride=1, padding=1),\n            nn.BatchNorm2d(512), \n            nn.ReLU(), \n            nn.Dropout(dropout),\n            nn.MaxPool2d(kernel_size=2), \n            nn.AvgPool2d(kernel_size=7))\n        \n        self.layer6 = nn.Sequential(\n            nn.Linear(512, 512),\n            nn.ReLU(),\n        )\n        self.rotation_out = nn.Linear(512, 9)\n        self.tanh = nn.Tanh()\n        \n        self.translation_out = nn.Linear(512, 3)\n        \n    def forward(self, x):\n        x = self.layer1(x)\n        x = self.layer2(x)\n        x = self.layer3(x)\n        x = self.layer4(x)\n        x = self.layer5(x)\n        \n        x = x.view(-1, 512)\n        x = self.layer6(x)\n        \n        rotation_out = self.rotation_out(x)\n        rotation_out = self.tanh(rotation_out)\n        \n        translation_out = self.translation_out(x)\n        \n        return rotation_out, translation_out","metadata":{"execution":{"iopub.execute_input":"2023-05-06T06:54:21.358468Z","iopub.status.busy":"2023-05-06T06:54:21.358179Z","iopub.status.idle":"2023-05-06T06:54:21.375620Z","shell.execute_reply":"2023-05-06T06:54:21.374490Z"},"papermill":{"duration":0.028041,"end_time":"2023-05-06T06:54:21.377974","exception":false,"start_time":"2023-05-06T06:54:21.349933","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = BaseModel(dropout=0.2)","metadata":{"execution":{"iopub.execute_input":"2023-05-06T06:54:21.393463Z","iopub.status.busy":"2023-05-06T06:54:21.392566Z","iopub.status.idle":"2023-05-06T06:54:21.595425Z","shell.execute_reply":"2023-05-06T06:54:21.594180Z"},"papermill":{"duration":0.213381,"end_time":"2023-05-06T06:54:21.598310","exception":false,"start_time":"2023-05-06T06:54:21.384929","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"l1_distance = nn.L1Loss()","metadata":{"execution":{"iopub.execute_input":"2023-05-06T06:54:21.615364Z","iopub.status.busy":"2023-05-06T06:54:21.614972Z","iopub.status.idle":"2023-05-06T06:54:21.620168Z","shell.execute_reply":"2023-05-06T06:54:21.619039Z"},"papermill":{"duration":0.016649,"end_time":"2023-05-06T06:54:21.622443","exception":false,"start_time":"2023-05-06T06:54:21.605794","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"optimizer = torch.optim.Adam(params = model.parameters(), lr = 0.001)\nscheduler = torch.optim.lr_scheduler.ReduceLROnPlateau(optimizer, mode='max', factor=0.5, patience=2, threshold_mode='abs', min_lr=1e-8, verbose=True)","metadata":{"execution":{"iopub.execute_input":"2023-05-06T06:54:21.637841Z","iopub.status.busy":"2023-05-06T06:54:21.636982Z","iopub.status.idle":"2023-05-06T06:54:21.642866Z","shell.execute_reply":"2023-05-06T06:54:21.641864Z"},"papermill":{"duration":0.015686,"end_time":"2023-05-06T06:54:21.644984","exception":false,"start_time":"2023-05-06T06:54:21.629298","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"device = torch.device(\"cuda\")","metadata":{"execution":{"iopub.execute_input":"2023-05-06T06:54:21.660104Z","iopub.status.busy":"2023-05-06T06:54:21.659233Z","iopub.status.idle":"2023-05-06T06:54:21.664195Z","shell.execute_reply":"2023-05-06T06:54:21.663267Z"},"papermill":{"duration":0.014482,"end_time":"2023-05-06T06:54:21.666225","exception":false,"start_time":"2023-05-06T06:54:21.651743","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"best_loss = 1000000000\nepochs = 10\nbest_model = None","metadata":{"execution":{"iopub.execute_input":"2023-05-06T06:54:21.681526Z","iopub.status.busy":"2023-05-06T06:54:21.680386Z","iopub.status.idle":"2023-05-06T06:54:21.685595Z","shell.execute_reply":"2023-05-06T06:54:21.684697Z"},"papermill":{"duration":0.014912,"end_time":"2023-05-06T06:54:21.687683","exception":false,"start_time":"2023-05-06T06:54:21.672771","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.to(device)","metadata":{"execution":{"iopub.execute_input":"2023-05-06T06:54:21.702985Z","iopub.status.busy":"2023-05-06T06:54:21.702058Z","iopub.status.idle":"2023-05-06T06:54:24.521270Z","shell.execute_reply":"2023-05-06T06:54:24.520148Z"},"papermill":{"duration":2.829105,"end_time":"2023-05-06T06:54:24.523558","exception":false,"start_time":"2023-05-06T06:54:21.694453","status":"completed"},"tags":[],"collapsed":true,"jupyter":{"outputs_hidden":true}},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for epoch in range(1, epochs+1):\n    \n    train_loss = []\n    rot_loss = []\n    trans_loss = []\n    \n    val_loss = []\n    val_rot_loss = []\n    val_trans_loss = []\n    \n    for imgs, rotation_labels, translation_labels in tqdm(train_loader):\n        model.train()\n        optimizer.zero_grad()\n        \n        imgs = imgs.to(device)\n        rotation_labels = rotation_labels.to(device)\n        translation_labels = translation_labels.to(device)\n        \n        rotation_output, translation_output = model(imgs)\n        \n        rotation_loss = l1_distance(rotation_output, rotation_labels)\n        translation_loss = l1_distance(translation_output, translation_labels)\n        loss = rotation_loss + translation_loss\n        loss.backward()\n        \n        optimizer.step()\n        \n        train_loss.append(loss.item())\n        rot_loss.append(rotation_loss.item())\n        trans_loss.append(translation_loss.item())\n\n    for imgs, rotation_labels, translation_labels in tqdm(val_loader):\n        model.eval()\n        \n        imgs = imgs.to(device)\n        rotation_labels = rotation_labels.to(device)\n        translation_labels = translation_labels.to(device)\n        \n        rotation_output, translation_output = model(imgs)\n        rotation_loss = l1_distance(rotation_output, rotation_labels)\n        translation_loss = l1_distance(translation_output, translation_labels)\n        loss = rotation_loss + translation_loss\n        \n        val_loss.append(loss.item())\n        val_rot_loss.append(rotation_loss.item())\n        val_trans_loss.append(translation_loss.item())\n        \n    mtrain_loss = np.mean(train_loss)\n    mval_loss = np.mean(val_loss)\n    mtrain_rot_loss = np.mean(rot_loss)\n    mtrain_trans_loss = np.mean(trans_loss)\n    mval_rot_loss = np.mean(val_rot_loss)\n    mval_trans_loss = np.mean(val_trans_loss)\n    \n    print(f'Epoch [{epoch}], Train Loss : [{mtrain_loss:.5f}] \\\n    Train Rotation Loss : [{mtrain_rot_loss:.5f}] Train Translation Loss : [{mtrain_trans_loss:.5f}] \\\n          Val Loss : [{mval_loss:.5f}] Val Rotation Loss : [{mval_rot_loss:.5f}] Val Translation Loss : [{mval_trans_loss:.5f}]')\n\n    if scheduler is not None:\n        scheduler.step(mval_loss)\n\n    if best_loss < mval_loss:\n        best_loss = mval_loss\n        best_model = model","metadata":{"execution":{"iopub.execute_input":"2023-05-06T06:54:24.538850Z","iopub.status.busy":"2023-05-06T06:54:24.538528Z","iopub.status.idle":"2023-05-06T07:02:20.780484Z","shell.execute_reply":"2023-05-06T07:02:20.779155Z"},"papermill":{"duration":476.252433,"end_time":"2023-05-06T07:02:20.782905","exception":false,"start_time":"2023-05-06T06:54:24.530472","status":"completed"},"tags":[],"collapsed":true,"jupyter":{"outputs_hidden":true}},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission = pd.read_csv(\"/kaggle/input/image-matching-challenge-2023/sample_submission.csv\")","metadata":{"execution":{"iopub.execute_input":"2023-05-06T07:02:20.886780Z","iopub.status.busy":"2023-05-06T07:02:20.886430Z","iopub.status.idle":"2023-05-06T07:02:20.895601Z","shell.execute_reply":"2023-05-06T07:02:20.894612Z"},"papermill":{"duration":0.063511,"end_time":"2023-05-06T07:02:20.898017","exception":false,"start_time":"2023-05-06T07:02:20.834506","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission","metadata":{"execution":{"iopub.execute_input":"2023-05-06T07:02:21.004380Z","iopub.status.busy":"2023-05-06T07:02:21.002808Z","iopub.status.idle":"2023-05-06T07:02:21.018725Z","shell.execute_reply":"2023-05-06T07:02:21.017583Z"},"papermill":{"duration":0.071922,"end_time":"2023-05-06T07:02:21.021563","exception":false,"start_time":"2023-05-06T07:02:20.949641","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"src = '/kaggle/input/image-matching-challenge-2023'","metadata":{"execution":{"iopub.execute_input":"2023-05-06T07:02:21.125135Z","iopub.status.busy":"2023-05-06T07:02:21.124755Z","iopub.status.idle":"2023-05-06T07:02:21.129398Z","shell.execute_reply":"2023-05-06T07:02:21.128349Z"},"papermill":{"duration":0.058833,"end_time":"2023-05-06T07:02:21.131566","exception":false,"start_time":"2023-05-06T07:02:21.072733","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def arr_to_str(a):\n    return ';'.join([str(x) for x in a.reshape(-1)])","metadata":{"execution":{"iopub.execute_input":"2023-05-06T07:02:21.234748Z","iopub.status.busy":"2023-05-06T07:02:21.234400Z","iopub.status.idle":"2023-05-06T07:02:21.239706Z","shell.execute_reply":"2023-05-06T07:02:21.238588Z"},"papermill":{"duration":0.06027,"end_time":"2023-05-06T07:02:21.242283","exception":false,"start_time":"2023-05-06T07:02:21.182013","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_rotation_list = []\ntest_translation_list = []\ntest_img_list = []\ntest_dataset_list = []\ntest_scene_list = []\nfor i in range(len(submission)):\n    test_row = submission.iloc[i]\n    test_img = test_row[\"image_path\"]\n    test_dataset = test_row[\"dataset\"]\n    test_scene = test_row[\"scene\"]\n    img_path = f\"{src}/test/{test_img}\"\n    \n    try:\n        image = cv2.imread(img_path)\n        image = test_transforms(image=image)['image']\n\n        rotation, translation = model(image)\n\n        rotation = arr_to_str(rotation.detach().cpu().numpy())\n        translation = arr_to_str(translation.detach().cpu().numpy())\n    except:\n        rotation = \"1.0;0.0;0.0;0.0;1.0;0.0;0.0;0.0;1.0\"\n        translation = \"0.0;0.0;0.0\"\n    \n    test_rotation_list.append(rotation)\n    test_translation_list.append(translation)\n    test_img_list.append(test_img)\n    test_dataset_list.append(test_dataset)\n    test_scene_list.append(test_scene)\n    \nmy_submission = pd.DataFrame()\nmy_submission[\"image_path\"] = test_img_list\nmy_submission[\"dataset\"] = test_dataset_list\nmy_submission[\"scene\"] = test_scene_list\nmy_submission[\"rotation_matrix\"] = test_rotation_list\nmy_submission[\"translation_vector\"] = test_translation_list\n\nmy_submission.to_csv(\"submission.csv\")","metadata":{"execution":{"iopub.execute_input":"2023-05-06T07:02:21.346710Z","iopub.status.busy":"2023-05-06T07:02:21.346350Z","iopub.status.idle":"2023-05-06T07:02:21.363784Z","shell.execute_reply":"2023-05-06T07:02:21.362870Z"},"papermill":{"duration":0.072505,"end_time":"2023-05-06T07:02:21.366012","exception":false,"start_time":"2023-05-06T07:02:21.293507","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"papermill":{"duration":0.051925,"end_time":"2023-05-06T07:02:21.468593","exception":false,"start_time":"2023-05-06T07:02:21.416668","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]}]}