{"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":"markdown","source":"## Data loader for IMC 2023","metadata":{}},{"cell_type":"markdown","source":"### This notebook gives a pytorch dataloader for Image Matching Challenge 2023","metadata":{}},{"cell_type":"markdown","source":"#### Imports","metadata":{}},{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nfrom pathlib import Path\nimport wandb\nimport os\nfrom tqdm.autonotebook import tqdm\nimport torch\nimport torch.nn as nn\nfrom torch.utils.data import Dataset, DataLoader\nfrom torchvision import transforms\nfrom PIL import Image\n","metadata":{"execution":{"iopub.status.busy":"2023-05-20T14:17:54.640582Z","iopub.execute_input":"2023-05-20T14:17:54.641373Z","iopub.status.idle":"2023-05-20T14:17:54.647938Z","shell.execute_reply.started":"2023-05-20T14:17:54.641334Z","shell.execute_reply":"2023-05-20T14:17:54.646818Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### Use pandas to read the train_labels.csv file","metadata":{}},{"cell_type":"code","source":"main_path=Path('/kaggle/input/image-matching-challenge-2023')\ntrain_path= main_path / 'train'\ntrain_labels_path= train_path / 'train_labels.csv'\ntrain_labels=pd.read_csv(train_labels_path)\ntrain_labels.head(4)","metadata":{"execution":{"iopub.status.busy":"2023-05-20T14:17:03.266241Z","iopub.execute_input":"2023-05-20T14:17:03.267100Z","iopub.status.idle":"2023-05-20T14:17:03.285068Z","shell.execute_reply.started":"2023-05-20T14:17:03.267060Z","shell.execute_reply":"2023-05-20T14:17:03.283914Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### Do a 70:30 train test split of the train_labels dataframe","metadata":{}},{"cell_type":"code","source":"df_train_labels=train_labels.sample(frac=0.7, random_state=33)\ndf_test_labels=train_labels.drop(df_train_labels.index)","metadata":{"execution":{"iopub.status.busy":"2023-05-20T14:17:05.888052Z","iopub.execute_input":"2023-05-20T14:17:05.888459Z","iopub.status.idle":"2023-05-20T14:17:05.896281Z","shell.execute_reply.started":"2023-05-20T14:17:05.888426Z","shell.execute_reply":"2023-05-20T14:17:05.895159Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train_labels.head(5)","metadata":{"execution":{"iopub.status.busy":"2023-05-20T14:17:08.816034Z","iopub.execute_input":"2023-05-20T14:17:08.817018Z","iopub.status.idle":"2023-05-20T14:17:08.829743Z","shell.execute_reply.started":"2023-05-20T14:17:08.816983Z","shell.execute_reply":"2023-05-20T14:17:08.828374Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_test_labels.head(5)","metadata":{"execution":{"iopub.status.busy":"2023-05-20T14:17:11.686094Z","iopub.execute_input":"2023-05-20T14:17:11.686505Z","iopub.status.idle":"2023-05-20T14:17:11.701092Z","shell.execute_reply.started":"2023-05-20T14:17:11.686470Z","shell.execute_reply":"2023-05-20T14:17:11.699901Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### create a image transform","metadata":{}},{"cell_type":"code","source":"image_transform=transforms.Compose([transforms.Resize(size=(224,224)), transforms.ToTensor()])","metadata":{"execution":{"iopub.status.busy":"2023-05-20T14:17:26.447929Z","iopub.execute_input":"2023-05-20T14:17:26.449192Z","iopub.status.idle":"2023-05-20T14:17:26.455696Z","shell.execute_reply.started":"2023-05-20T14:17:26.449148Z","shell.execute_reply":"2023-05-20T14:17:26.454575Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### Create a image dataset class","metadata":{}},{"cell_type":"code","source":"class ImageDataset(Dataset):\n    def __init__(self, \n                 df_labels : pd.DataFrame,\n                 image_transform : transforms = None\n                ):\n        super().__init__()\n        self.image_paths=df_labels['image_path'].tolist()\n        self.rotation_matrices=df_labels['rotation_matrix'].tolist()\n        self.translation_vectors=df_labels['translation_vector'].tolist()\n        self.image_transform=image_transform\n        \n    def __getitem__(self,index):\n        actual_image_path='/kaggle/input/image-matching-challenge-2023/train/'+self.image_paths[index]\n        image = Image.open(actual_image_path)\n        \n        if self.image_transform is not None:\n            image = self.image_transform(image)\n            \n        rotation_matrix=self.rotation_matrices[index].split(';')\n        rotation_matrix=np.array(rotation_matrix, dtype=float)\n        rotation_matrix=torch.from_numpy(rotation_matrix).type(torch.float32)\n        \n        translation_vector=self.translation_vectors[index].split(';')\n        translation_vector=np.array(translation_vector, dtype=float)\n        translation_vector=torch.from_numpy(translation_vector).type(torch.float32)\n        \n        \n        return image, rotation_matrix, translation_vector\n        \n    def __len__(self):\n        return len(self.image_paths)","metadata":{"execution":{"iopub.status.busy":"2023-05-20T14:17:28.870889Z","iopub.execute_input":"2023-05-20T14:17:28.871263Z","iopub.status.idle":"2023-05-20T14:17:28.882013Z","shell.execute_reply.started":"2023-05-20T14:17:28.871233Z","shell.execute_reply":"2023-05-20T14:17:28.880740Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dataset=ImageDataset(df_train_labels, image_transform)\ntest_dataset=ImageDataset(df_test_labels, image_transform)","metadata":{"execution":{"iopub.status.busy":"2023-05-20T14:17:32.365135Z","iopub.execute_input":"2023-05-20T14:17:32.366144Z","iopub.status.idle":"2023-05-20T14:17:32.373149Z","shell.execute_reply.started":"2023-05-20T14:17:32.366092Z","shell.execute_reply":"2023-05-20T14:17:32.371770Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### Verify the train and test datasets","metadata":{}},{"cell_type":"code","source":"len(train_dataset)","metadata":{"execution":{"iopub.status.busy":"2023-05-20T14:17:35.283167Z","iopub.execute_input":"2023-05-20T14:17:35.283570Z","iopub.status.idle":"2023-05-20T14:17:35.290669Z","shell.execute_reply.started":"2023-05-20T14:17:35.283537Z","shell.execute_reply":"2023-05-20T14:17:35.289501Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(test_dataset)","metadata":{"execution":{"iopub.status.busy":"2023-05-20T14:17:36.990938Z","iopub.execute_input":"2023-05-20T14:17:36.991348Z","iopub.status.idle":"2023-05-20T14:17:36.998434Z","shell.execute_reply.started":"2023-05-20T14:17:36.991315Z","shell.execute_reply":"2023-05-20T14:17:36.997096Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_img,sample_rotmat,sample_transvect=train_dataset[0]\nprint(sample_img.shape)\nprint(sample_rotmat)\nprint(sample_transvect)","metadata":{"execution":{"iopub.status.busy":"2023-05-20T14:18:44.332923Z","iopub.execute_input":"2023-05-20T14:18:44.333363Z","iopub.status.idle":"2023-05-20T14:18:44.414319Z","shell.execute_reply.started":"2023-05-20T14:18:44.333331Z","shell.execute_reply":"2023-05-20T14:18:44.413090Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_img,sample_rotmat,sample_transvect=test_dataset[0]\nprint(sample_img.shape)\nprint(sample_rotmat)\nprint(sample_transvect)","metadata":{"execution":{"iopub.status.busy":"2023-05-20T14:19:16.980777Z","iopub.execute_input":"2023-05-20T14:19:16.981245Z","iopub.status.idle":"2023-05-20T14:19:17.067102Z","shell.execute_reply.started":"2023-05-20T14:19:16.981210Z","shell.execute_reply":"2023-05-20T14:19:17.066185Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### Create train and test dataloaders with batch_size=8","metadata":{}},{"cell_type":"code","source":"train_dataloader=DataLoader(dataset=train_dataset, batch_size=8, shuffle= True)\ntest_dataloader=DataLoader(dataset=test_dataset, batch_size=8, shuffle= False)","metadata":{"execution":{"iopub.status.busy":"2023-05-20T14:20:36.453710Z","iopub.execute_input":"2023-05-20T14:20:36.454171Z","iopub.status.idle":"2023-05-20T14:20:36.460397Z","shell.execute_reply.started":"2023-05-20T14:20:36.454136Z","shell.execute_reply":"2023-05-20T14:20:36.459175Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### Verify the dataloaders","metadata":{}},{"cell_type":"code","source":"sample_img,sample_rotmat,sample_transvect =next(iter(train_dataloader))","metadata":{"execution":{"iopub.status.busy":"2023-05-20T14:22:31.126262Z","iopub.execute_input":"2023-05-20T14:22:31.126713Z","iopub.status.idle":"2023-05-20T14:22:33.162983Z","shell.execute_reply.started":"2023-05-20T14:22:31.126677Z","shell.execute_reply":"2023-05-20T14:22:33.161683Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(sample_img.shape)\nprint(sample_rotmat)\nprint(sample_transvect)","metadata":{"execution":{"iopub.status.busy":"2023-05-20T14:22:52.081250Z","iopub.execute_input":"2023-05-20T14:22:52.081654Z","iopub.status.idle":"2023-05-20T14:22:52.093071Z","shell.execute_reply.started":"2023-05-20T14:22:52.081622Z","shell.execute_reply":"2023-05-20T14:22:52.091956Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_img,sample_rotmat,sample_transvect =next(iter(train_dataloader))","metadata":{"execution":{"iopub.status.busy":"2023-05-20T14:23:22.873621Z","iopub.execute_input":"2023-05-20T14:23:22.875101Z","iopub.status.idle":"2023-05-20T14:23:24.836452Z","shell.execute_reply.started":"2023-05-20T14:23:22.875050Z","shell.execute_reply":"2023-05-20T14:23:24.835505Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(sample_img.shape)\nprint(sample_rotmat)\nprint(sample_transvect)","metadata":{"execution":{"iopub.status.busy":"2023-05-20T14:23:39.204716Z","iopub.execute_input":"2023-05-20T14:23:39.206073Z","iopub.status.idle":"2023-05-20T14:23:39.215836Z","shell.execute_reply.started":"2023-05-20T14:23:39.206022Z","shell.execute_reply":"2023-05-20T14:23:39.214702Z"},"trusted":true},"execution_count":null,"outputs":[]}]}