{"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":"# 📓 Please Upvote the notebook 📓","metadata":{}},{"cell_type":"code","source":"import os\nimport pandas\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport cv2\n\nfrom fastai.vision.all import *","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install fastai timm nbdev -q --upgrade","metadata":{"execution":{"iopub.status.busy":"2021-09-12T16:56:05.262689Z","iopub.execute_input":"2021-09-12T16:56:05.262998Z","iopub.status.idle":"2021-09-12T16:56:19.925188Z","shell.execute_reply.started":"2021-09-12T16:56:05.262938Z","shell.execute_reply":"2021-09-12T16:56:19.924181Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import timm","metadata":{"execution":{"iopub.status.busy":"2021-09-12T16:56:19.926605Z","iopub.execute_input":"2021-09-12T16:56:19.926871Z","iopub.status.idle":"2021-09-12T16:56:20.714063Z","shell.execute_reply.started":"2021-09-12T16:56:19.926836Z","shell.execute_reply":"2021-09-12T16:56:20.71298Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"path = '/kaggle/input/landmark-recognition-2021/'","metadata":{"execution":{"iopub.status.busy":"2021-09-12T16:56:20.716245Z","iopub.execute_input":"2021-09-12T16:56:20.71648Z","iopub.status.idle":"2021-09-12T16:56:20.723813Z","shell.execute_reply.started":"2021-09-12T16:56:20.716449Z","shell.execute_reply":"2021-09-12T16:56:20.722473Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data = pd.read_csv(path+'train.csv')\nsamp_subm = pd.read_csv(path+'sample_submission.csv')","metadata":{"execution":{"iopub.status.busy":"2021-09-12T16:56:20.725091Z","iopub.execute_input":"2021-09-12T16:56:20.725315Z","iopub.status.idle":"2021-09-12T16:56:22.229901Z","shell.execute_reply.started":"2021-09-12T16:56:20.72529Z","shell.execute_reply":"2021-09-12T16:56:22.229029Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data.head(5)\n","metadata":{"execution":{"iopub.status.busy":"2021-09-12T16:56:22.231236Z","iopub.execute_input":"2021-09-12T16:56:22.231453Z","iopub.status.idle":"2021-09-12T16:56:22.252716Z","shell.execute_reply.started":"2021-09-12T16:56:22.231428Z","shell.execute_reply":"2021-09-12T16:56:22.251895Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(len(train_data))","metadata":{"execution":{"iopub.status.busy":"2021-09-12T16:56:22.25377Z","iopub.execute_input":"2021-09-12T16:56:22.254013Z","iopub.status.idle":"2021-09-12T16:56:22.259444Z","shell.execute_reply.started":"2021-09-12T16:56:22.253984Z","shell.execute_reply":"2021-09-12T16:56:22.258554Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"samp_subm.head(5)","metadata":{"execution":{"iopub.status.busy":"2021-09-12T16:56:22.260825Z","iopub.execute_input":"2021-09-12T16:56:22.261172Z","iopub.status.idle":"2021-09-12T16:56:22.276118Z","shell.execute_reply.started":"2021-09-12T16:56:22.261139Z","shell.execute_reply":"2021-09-12T16:56:22.275218Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### Now we will get the path to all our images using a fast.ai function","metadata":{}},{"cell_type":"code","source":"train_img_files = get_image_files(f'{path}train') #fastai function that helps us grab all the image files (recursively) in one folder.\nlen(train_img_files)","metadata":{"execution":{"iopub.status.busy":"2021-09-12T16:56:22.277726Z","iopub.execute_input":"2021-09-12T16:56:22.277966Z","iopub.status.idle":"2021-09-12T17:01:57.460255Z","shell.execute_reply.started":"2021-09-12T16:56:22.277924Z","shell.execute_reply":"2021-09-12T17:01:57.459582Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_img_files[0] #now we have the path of each image","metadata":{"execution":{"iopub.status.busy":"2021-09-12T17:01:57.461801Z","iopub.execute_input":"2021-09-12T17:01:57.46233Z","iopub.status.idle":"2021-09-12T17:01:57.468857Z","shell.execute_reply.started":"2021-09-12T17:01:57.462289Z","shell.execute_reply":"2021-09-12T17:01:57.467928Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_img_files[0].name","metadata":{"execution":{"iopub.status.busy":"2021-09-12T17:01:57.471347Z","iopub.execute_input":"2021-09-12T17:01:57.471591Z","iopub.status.idle":"2021-09-12T17:01:57.493664Z","shell.execute_reply.started":"2021-09-12T17:01:57.471563Z","shell.execute_reply":"2021-09-12T17:01:57.483365Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### Now let us see the image and the shape for few","metadata":{}},{"cell_type":"code","source":"for i in range(5):\n    img = PILImage.create(train_img_files[i])\n    img.show()\n    print(img.shape)","metadata":{"execution":{"iopub.status.busy":"2021-09-12T17:01:57.495185Z","iopub.execute_input":"2021-09-12T17:01:57.495527Z","iopub.status.idle":"2021-09-12T17:01:58.570742Z","shell.execute_reply.started":"2021-09-12T17:01:57.495485Z","shell.execute_reply":"2021-09-12T17:01:58.569724Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### So we can see that the shape for the images are all diffrent","metadata":{}},{"cell_type":"code","source":"bs = 8 #batch size","metadata":{"execution":{"iopub.status.busy":"2021-09-12T17:01:58.57222Z","iopub.execute_input":"2021-09-12T17:01:58.573075Z","iopub.status.idle":"2021-09-12T17:01:58.577517Z","shell.execute_reply.started":"2021-09-12T17:01:58.57303Z","shell.execute_reply":"2021-09-12T17:01:58.57637Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### We would pass in the train_data later so for that we would collect X and Y so we need to make functions for it","metadata":{}},{"cell_type":"code","source":"get_x = lambda x: path/'train'/f'{x[0][0]}'/f'{x[0][1]}'/f'{x[0][2]}'/f'{x[0]}.jpg'","metadata":{"execution":{"iopub.status.busy":"2021-09-12T17:01:58.579492Z","iopub.execute_input":"2021-09-12T17:01:58.579803Z","iopub.status.idle":"2021-09-12T17:01:58.591427Z","shell.execute_reply.started":"2021-09-12T17:01:58.579764Z","shell.execute_reply":"2021-09-12T17:01:58.590309Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"get_y = lambda x: x[1]","metadata":{"execution":{"iopub.status.busy":"2021-09-12T17:01:58.593086Z","iopub.execute_input":"2021-09-12T17:01:58.593489Z","iopub.status.idle":"2021-09-12T17:01:58.604279Z","shell.execute_reply.started":"2021-09-12T17:01:58.593447Z","shell.execute_reply":"2021-09-12T17:01:58.603405Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"item_tfms = Resize(size = 460)\nbatch_tfms = [*aug_transforms(size = 224, min_scale = 0.35), Normalize.from_stats(*imagenet_stats)]","metadata":{"execution":{"iopub.status.busy":"2021-09-12T17:01:58.605545Z","iopub.execute_input":"2021-09-12T17:01:58.60635Z","iopub.status.idle":"2021-09-12T17:01:58.633503Z","shell.execute_reply.started":"2021-09-12T17:01:58.606301Z","shell.execute_reply":"2021-09-12T17:01:58.632592Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### Creating a Datablock","metadata":{}},{"cell_type":"code","source":"landmark_dataset = DataBlock(blocks = (ImageBlock, CategoryBlock),\n                             get_x = get_x,\n                             splitter = RandomSplitter(),\n                             get_y = get_y,\n                             item_tfms = item_tfms ,\n                             batch_tfms = batch_tfms)","metadata":{"execution":{"iopub.status.busy":"2021-09-12T17:01:58.634655Z","iopub.execute_input":"2021-09-12T17:01:58.635284Z","iopub.status.idle":"2021-09-12T17:01:58.643469Z","shell.execute_reply.started":"2021-09-12T17:01:58.635238Z","shell.execute_reply":"2021-09-12T17:01:58.64255Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dls = landmark_dataset.dataloaders(train_data, bs = bs)","metadata":{"execution":{"iopub.status.busy":"2021-09-12T17:01:58.644773Z","iopub.execute_input":"2021-09-12T17:01:58.645133Z","iopub.status.idle":"2021-09-12T17:02:02.176631Z","shell.execute_reply.started":"2021-09-12T17:01:58.645104Z","shell.execute_reply":"2021-09-12T17:02:02.175539Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dls.show_batch(max_n = 8)","metadata":{"execution":{"iopub.status.busy":"2021-09-12T17:02:02.177621Z","iopub.status.idle":"2021-09-12T17:02:02.178059Z","shell.execute_reply.started":"2021-09-12T17:02:02.177879Z","shell.execute_reply":"2021-09-12T17:02:02.177895Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### Now we will train the model","metadata":{}},{"cell_type":"code","source":"learn = cnn_learner(dls, resnet34, metrics=accuracy, pretrained=False)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.fit_one_cycle(5, 5e-3)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### Now we will select a good learning rate","metadata":{}},{"cell_type":"code","source":"learn.lr_find()","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### Now we will save our model and do prediction in the inference notebook","metadata":{}},{"cell_type":"code","source":"learn.export()\npath = Path()\npath.ls(file_exts='.pkl')","metadata":{},"execution_count":null,"outputs":[]}]}