{"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 torch\nimport torch.nn as nn\n\nfrom torch.utils.data import Dataset, DataLoader\nfrom torchvision.transforms import transforms\n\nimport torchvision.models as models","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-06-28T00:26:54.557569Z","iopub.execute_input":"2021-06-28T00:26:54.557911Z","iopub.status.idle":"2021-06-28T00:26:54.562751Z","shell.execute_reply.started":"2021-06-28T00:26:54.557881Z","shell.execute_reply":"2021-06-28T00:26:54.561834Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\n\ntrain_df = pd.read_csv(\"../input/landmark-recognition-2020/train.csv\")\n\ntrain_df.head()","metadata":{"execution":{"iopub.status.busy":"2021-06-28T00:18:00.819541Z","iopub.execute_input":"2021-06-28T00:18:00.81982Z","iopub.status.idle":"2021-06-28T00:18:02.449169Z","shell.execute_reply.started":"2021-06-28T00:18:00.819794Z","shell.execute_reply":"2021-06-28T00:18:02.448074Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"landmark_id = str(train_df.iloc[0].landmark_id)\nindividual_image = train_df.iloc[0].id + '.jpg'\n\nprint(len(train_df.iloc[0].id))\nprint(individual_image, landmark_id )\n\nfrom PIL import Image\nimport matplotlib.pyplot as plt\n\nimg = Image.open('../input/landmark-recognition-2020/train/'+individual_image[0]+ \"/\" + individual_image[1] + \"/\" + individual_image[2]+\"/\" + individual_image)\nplt.imshow(img)\n","metadata":{"execution":{"iopub.status.busy":"2021-06-28T00:18:02.451402Z","iopub.execute_input":"2021-06-28T00:18:02.451823Z","iopub.status.idle":"2021-06-28T00:18:02.730113Z","shell.execute_reply.started":"2021-06-28T00:18:02.45178Z","shell.execute_reply":"2021-06-28T00:18:02.729114Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mean = (0.485, 0.456, 0.406)\nstd =  (0.229,0.225,0.224)\nIMG_SIZE = 128\ntransformations = transforms.Compose([transforms.Resize((IMG_SIZE,IMG_SIZE),interpolation=Image.NEAREST),\n                                      transforms.ToTensor(),\n                                      transforms.Normalize(mean,std)\n                                     ]\n                                    )    ","metadata":{"execution":{"iopub.status.busy":"2021-06-28T00:18:02.731746Z","iopub.execute_input":"2021-06-28T00:18:02.732062Z","iopub.status.idle":"2021-06-28T00:18:02.737837Z","shell.execute_reply.started":"2021-06-28T00:18:02.732014Z","shell.execute_reply":"2021-06-28T00:18:02.736789Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"base_directory = \"../input/landmark-recognition-2020/\"\n\nclass get_landmarks(Dataset):\n    def __init__(self,training_csv_path=\"../input/landmark-recognition-2020/train.csv\", training_image_path=\"../input/landmark-recognition-2020/train/\", my_transform=transformations):\n        self.training_df = pd.read_csv(training_csv_path)\n        self.image_path = training_image_path\n        self.transform = my_transform\n        \n    def __len__(self):\n        return len(self.training_df)\n    \n    def __getitem__(self,index):\n        \n        individual_image = self.training_df.iloc[index].id + \".jpg\"\n        landmark_id = self.training_df.iloc[index].landmark_id\n        full_path = self.image_path +individual_image[0]+ \"/\" + individual_image[1] + \"/\" + individual_image[2]+ \"/\" + individual_image\n#         print(full_path)\n        \n        img = Image.open(full_path)\n        \n        if self.transform:\n            img = self.transform(img)\n        \n        return img, landmark_id\n        \n        \n        ","metadata":{"execution":{"iopub.status.busy":"2021-06-28T00:18:02.739787Z","iopub.execute_input":"2021-06-28T00:18:02.74013Z","iopub.status.idle":"2021-06-28T00:18:02.752493Z","shell.execute_reply.started":"2021-06-28T00:18:02.7401Z","shell.execute_reply":"2021-06-28T00:18:02.75139Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dataset = get_landmarks()\n\nx, y = train_dataset[0]","metadata":{"execution":{"iopub.status.busy":"2021-06-28T00:18:02.75413Z","iopub.execute_input":"2021-06-28T00:18:02.754567Z","iopub.status.idle":"2021-06-28T00:18:03.962853Z","shell.execute_reply.started":"2021-06-28T00:18:02.754519Z","shell.execute_reply":"2021-06-28T00:18:03.962052Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(y)\nplt.imshow(x.permute(1,2,0))","metadata":{"execution":{"iopub.status.busy":"2021-06-28T00:18:03.963959Z","iopub.execute_input":"2021-06-28T00:18:03.964404Z","iopub.status.idle":"2021-06-28T00:18:04.121455Z","shell.execute_reply.started":"2021-06-28T00:18:03.964362Z","shell.execute_reply":"2021-06-28T00:18:04.120419Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dataloader = DataLoader(dataset=train_dataset,batch_size=64,shuffle=True )","metadata":{"execution":{"iopub.status.busy":"2021-06-28T00:18:04.122804Z","iopub.execute_input":"2021-06-28T00:18:04.123128Z","iopub.status.idle":"2021-06-28T00:18:04.127449Z","shell.execute_reply.started":"2021-06-28T00:18:04.123097Z","shell.execute_reply":"2021-06-28T00:18:04.126456Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = models.resnet18(pretrained=True)","metadata":{"execution":{"iopub.status.busy":"2021-06-28T00:27:48.529913Z","iopub.execute_input":"2021-06-28T00:27:48.530276Z","iopub.status.idle":"2021-06-28T00:27:52.107617Z","shell.execute_reply.started":"2021-06-28T00:27:48.530246Z","shell.execute_reply":"2021-06-28T00:27:52.106366Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model","metadata":{"execution":{"iopub.status.busy":"2021-06-28T00:28:05.885663Z","iopub.execute_input":"2021-06-28T00:28:05.886041Z","iopub.status.idle":"2021-06-28T00:28:05.894408Z","shell.execute_reply.started":"2021-06-28T00:28:05.885996Z","shell.execute_reply":"2021-06-28T00:28:05.893258Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(train_df['landmark_id'].unique())\n\n# what the hell!","metadata":{"execution":{"iopub.status.busy":"2021-06-28T00:38:00.295822Z","iopub.execute_input":"2021-06-28T00:38:00.296215Z","iopub.status.idle":"2021-06-28T00:38:00.317486Z","shell.execute_reply.started":"2021-06-28T00:38:00.296181Z","shell.execute_reply":"2021-06-28T00:38:00.316416Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.fc = nn.Linear(in_features=512, out_features=len(train_df['landmark_id'].unique()), bias=True)","metadata":{"execution":{"iopub.status.busy":"2021-06-28T00:38:35.26193Z","iopub.execute_input":"2021-06-28T00:38:35.262343Z","iopub.status.idle":"2021-06-28T00:38:35.667567Z","shell.execute_reply.started":"2021-06-28T00:38:35.262309Z","shell.execute_reply":"2021-06-28T00:38:35.666548Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')","metadata":{"execution":{"iopub.status.busy":"2021-06-28T00:39:48.683828Z","iopub.execute_input":"2021-06-28T00:39:48.684443Z","iopub.status.idle":"2021-06-28T00:39:48.688441Z","shell.execute_reply.started":"2021-06-28T00:39:48.684407Z","shell.execute_reply":"2021-06-28T00:39:48.687773Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = model.to(device=device)","metadata":{"execution":{"iopub.status.busy":"2021-06-28T00:39:50.194299Z","iopub.execute_input":"2021-06-28T00:39:50.194644Z","iopub.status.idle":"2021-06-28T00:39:50.20431Z","shell.execute_reply.started":"2021-06-28T00:39:50.194614Z","shell.execute_reply":"2021-06-28T00:39:50.203287Z"},"trusted":true},"execution_count":null,"outputs":[]}]}