{"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":"# # This Python 3 environment comes with many helpful analytics libraries installed\n# # It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# # For example, here's several helpful packages to load\n\n# import numpy as np # linear algebra\n# import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# # Input data files are available in the read-only \"../input/\" directory\n# # For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\n# import os\n# for dirname, _, filenames in os.walk('/kaggle/input'):\n#     for filename in filenames:\n#         print(os.path.join(dirname, filename))\n\n# # You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# # You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-03-15T21:25:36.084591Z","iopub.execute_input":"2022-03-15T21:25:36.08492Z","iopub.status.idle":"2022-03-15T21:25:36.092319Z","shell.execute_reply.started":"2022-03-15T21:25:36.084889Z","shell.execute_reply":"2022-03-15T21:25:36.09103Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Configuration for using TPU with Fastai (Based on Pytorch)\n###### faster,saves time as compared to GPU(as assignment deadline is short so instead of gpu using tpu) and support large memory so can use greater image target size)","metadata":{}},{"cell_type":"code","source":"%%capture\n!pip install -Uqq fastcore --upgrade\n!pip install -Uqq fastai --upgrade\n!pip install -Uqq cloud-tpu-client==0.10 https://storage.googleapis.com/tpu-pytorch/wheels/torch_xla-1.7-cp37-cp37m-linux_x86_64.whl\n!pip install -Uqq git+https://github.com/butchland/fastai_xla_extensions.git","metadata":{"execution":{"iopub.status.busy":"2022-03-15T21:25:36.094374Z","iopub.execute_input":"2022-03-15T21:25:36.095093Z","iopub.status.idle":"2022-03-15T21:25:37.020326Z","shell.execute_reply.started":"2022-03-15T21:25:36.09505Z","shell.execute_reply":"2022-03-15T21:25:37.018886Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%capture\nimport fastai_xla_extensions.core","metadata":{"execution":{"iopub.status.busy":"2022-03-15T21:25:37.023134Z","iopub.execute_input":"2022-03-15T21:25:37.023661Z","iopub.status.idle":"2022-03-15T21:25:37.038151Z","shell.execute_reply.started":"2022-03-15T21:25:37.023577Z","shell.execute_reply":"2022-03-15T21:25:37.037019Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from fastai.vision.data import ImageDataLoaders\nfrom fastai.vision.all import *\nfrom fastai.imports import *\nimport gc \nimport pandas as pd\nimport numpy as np\n%matplotlib inline","metadata":{"execution":{"iopub.status.busy":"2022-03-15T21:25:37.04149Z","iopub.execute_input":"2022-03-15T21:25:37.042422Z","iopub.status.idle":"2022-03-15T21:25:37.059306Z","shell.execute_reply.started":"2022-03-15T21:25:37.042357Z","shell.execute_reply":"2022-03-15T21:25:37.058462Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Dataset Preparation","metadata":{}},{"cell_type":"code","source":"PATH = \"../input/state-farm-distracted-driver-detection/\"\nTRAINING_PATH = \"driver_imgs_list.csv\"\nCUSTOM_DATASET_PATH = \"../input/state-farm-distracted-driver-detection/sample_submission.csv\"","metadata":{"execution":{"iopub.status.busy":"2022-03-15T21:25:37.061196Z","iopub.execute_input":"2022-03-15T21:25:37.062342Z","iopub.status.idle":"2022-03-15T21:25:37.074092Z","shell.execute_reply.started":"2022-03-15T21:25:37.062285Z","shell.execute_reply":"2022-03-15T21:25:37.07329Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df = pd.read_csv(PATH+TRAINING_PATH)\ntrain_df[\"img\"]=\"input/state-farm-distracted-driver-detection/imgs/train/\"+train_df[\"classname\"]+\"/\"+train_df[\"img\"]","metadata":{"execution":{"iopub.status.busy":"2022-03-15T21:25:37.076179Z","iopub.execute_input":"2022-03-15T21:25:37.076958Z","iopub.status.idle":"2022-03-15T21:25:37.188772Z","shell.execute_reply.started":"2022-03-15T21:25:37.076905Z","shell.execute_reply":"2022-03-15T21:25:37.187356Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-03-15T21:25:37.19088Z","iopub.execute_input":"2022-03-15T21:25:37.19124Z","iopub.status.idle":"2022-03-15T21:25:37.216135Z","shell.execute_reply.started":"2022-03-15T21:25:37.191181Z","shell.execute_reply":"2022-03-15T21:25:37.214371Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.tail()","metadata":{"execution":{"iopub.status.busy":"2022-03-15T21:25:37.218511Z","iopub.execute_input":"2022-03-15T21:25:37.219007Z","iopub.status.idle":"2022-03-15T21:25:37.236785Z","shell.execute_reply.started":"2022-03-15T21:25:37.218941Z","shell.execute_reply":"2022-03-15T21:25:37.235561Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df=train_df.drop(columns=['subject'])\ntrain_df=train_df[['img','classname']]\ntrain_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-03-15T21:25:37.241369Z","iopub.execute_input":"2022-03-15T21:25:37.242365Z","iopub.status.idle":"2022-03-15T21:25:37.278047Z","shell.execute_reply.started":"2022-03-15T21:25:37.242309Z","shell.execute_reply":"2022-03-15T21:25:37.276636Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Data Loaders for fast ai setting image size to 450 X 450","metadata":{}},{"cell_type":"code","source":"data = ImageDataLoaders.from_df(train_df,path=\"../\",\n                                 item_tfms=Resize(450),\n                                 valid_pct=0.1,\n                                 splitter=RandomSplitter(seed=42))\n#for gpu cudnn\n#seed=42\n# , device=torch.device('cuda'))\n# num_workers=0, device=torch.device('cuda')\n","metadata":{"execution":{"iopub.status.busy":"2022-03-15T21:25:37.27992Z","iopub.execute_input":"2022-03-15T21:25:37.280916Z","iopub.status.idle":"2022-03-15T21:25:38.267823Z","shell.execute_reply.started":"2022-03-15T21:25:37.280861Z","shell.execute_reply":"2022-03-15T21:25:38.264647Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Dataset Sample","metadata":{}},{"cell_type":"code","source":"data.show_batch(max_n=18, nrows=3)\n","metadata":{"execution":{"iopub.status.busy":"2022-03-15T21:25:38.269417Z","iopub.status.idle":"2022-03-15T21:25:38.270008Z","shell.execute_reply.started":"2022-03-15T21:25:38.269703Z","shell.execute_reply":"2022-03-15T21:25:38.269731Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Downloading Pretrained Models -: resnet50 (pretrained on imagenet)   ","metadata":{}},{"cell_type":"code","source":"learn = cnn_learner(data, resnet50, metrics=accuracy, pretrained=True)","metadata":{"execution":{"iopub.status.busy":"2022-03-15T21:25:38.274194Z","iopub.status.idle":"2022-03-15T21:25:38.27507Z","shell.execute_reply.started":"2022-03-15T21:25:38.274747Z","shell.execute_reply":"2022-03-15T21:25:38.274784Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Moving Model & Data to TPU & Finetuning","metadata":{}},{"cell_type":"code","source":"learn.to_xla()\nlearn.fine_tune(2)","metadata":{"execution":{"iopub.status.busy":"2022-03-15T21:25:38.276391Z","iopub.status.idle":"2022-03-15T21:25:38.277709Z","shell.execute_reply.started":"2022-03-15T21:25:38.277423Z","shell.execute_reply":"2022-03-15T21:25:38.277455Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.recorder.plot_loss()","metadata":{"execution":{"iopub.status.busy":"2022-03-15T21:25:38.279076Z","iopub.status.idle":"2022-03-15T21:25:38.279566Z","shell.execute_reply.started":"2022-03-15T21:25:38.27934Z","shell.execute_reply":"2022-03-15T21:25:38.279368Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Result Analysis","metadata":{}},{"cell_type":"markdown","source":"## Confussion Metric","metadata":{}},{"cell_type":"code","source":"interp = ClassificationInterpretation.from_learner(learn)\ninterp.plot_confusion_matrix(figsize=(15,15), dpi=60)","metadata":{"execution":{"iopub.status.busy":"2022-03-15T21:25:38.281512Z","iopub.status.idle":"2022-03-15T21:25:38.282553Z","shell.execute_reply.started":"2022-03-15T21:25:38.282234Z","shell.execute_reply":"2022-03-15T21:25:38.282268Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Most Confused Classes","metadata":{}},{"cell_type":"code","source":"interp.most_confused(min_val=1)","metadata":{"execution":{"iopub.status.busy":"2022-03-15T21:25:38.283959Z","iopub.status.idle":"2022-03-15T21:25:38.284412Z","shell.execute_reply.started":"2022-03-15T21:25:38.284166Z","shell.execute_reply":"2022-03-15T21:25:38.28419Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"interp.plot_top_losses(6, nrows=2)","metadata":{"execution":{"iopub.status.busy":"2022-03-15T21:25:38.286854Z","iopub.status.idle":"2022-03-15T21:25:38.287353Z","shell.execute_reply.started":"2022-03-15T21:25:38.287092Z","shell.execute_reply":"2022-03-15T21:25:38.287117Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Finding Optimal Learning Rate","metadata":{}},{"cell_type":"code","source":"learn.lr_find()","metadata":{"execution":{"iopub.status.busy":"2022-03-15T21:25:38.289153Z","iopub.status.idle":"2022-03-15T21:25:38.289622Z","shell.execute_reply.started":"2022-03-15T21:25:38.289412Z","shell.execute_reply":"2022-03-15T21:25:38.289435Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.unfreeze()","metadata":{"execution":{"iopub.status.busy":"2022-03-15T21:25:38.292178Z","iopub.status.idle":"2022-03-15T21:25:38.293457Z","shell.execute_reply.started":"2022-03-15T21:25:38.292463Z","shell.execute_reply":"2022-03-15T21:25:38.292492Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.fit_one_cycle(2, lr_max=slice(1.2e-7, 1.5e-7))","metadata":{"execution":{"iopub.status.busy":"2022-03-15T21:25:38.295922Z","iopub.status.idle":"2022-03-15T21:25:38.296823Z","shell.execute_reply.started":"2022-03-15T21:25:38.296565Z","shell.execute_reply":"2022-03-15T21:25:38.296593Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Loading Test Dataset","metadata":{}},{"cell_type":"code","source":"test_dl = data.test_dl(get_image_files('../input/state-farm-distracted-driver-detection/imgs/test'),ordered=True)","metadata":{"execution":{"iopub.status.busy":"2022-03-15T21:25:54.776518Z","iopub.execute_input":"2022-03-15T21:25:54.776839Z","iopub.status.idle":"2022-03-15T21:26:22.322082Z","shell.execute_reply.started":"2022-03-15T21:25:54.776808Z","shell.execute_reply":"2022-03-15T21:26:22.320902Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os \nk=os.listdir(\"../input/state-farm-distracted-driver-detection/imgs/test\")","metadata":{"execution":{"iopub.status.busy":"2022-03-15T21:26:31.778171Z","iopub.execute_input":"2022-03-15T21:26:31.77852Z","iopub.status.idle":"2022-03-15T21:26:31.841743Z","shell.execute_reply.started":"2022-03-15T21:26:31.778491Z","shell.execute_reply":"2022-03-15T21:26:31.840686Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Predicting Results","metadata":{}},{"cell_type":"code","source":"preds=learn.get_preds(dl=test_dl)","metadata":{"execution":{"iopub.status.busy":"2022-03-15T21:26:33.13517Z","iopub.execute_input":"2022-03-15T21:26:33.135787Z","iopub.status.idle":"2022-03-15T21:53:56.610085Z","shell.execute_reply.started":"2022-03-15T21:26:33.135732Z","shell.execute_reply":"2022-03-15T21:53:56.608521Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## converting Tensors to numpy array","metadata":{}},{"cell_type":"code","source":"l=[]\nfor i in preds[0]:\n    l.append(i.numpy())","metadata":{"execution":{"iopub.status.busy":"2022-03-15T21:57:05.385831Z","iopub.execute_input":"2022-03-15T21:57:05.38619Z","iopub.status.idle":"2022-03-15T21:57:05.604972Z","shell.execute_reply.started":"2022-03-15T21:57:05.386156Z","shell.execute_reply":"2022-03-15T21:57:05.603414Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ans=pd.DataFrame(l)","metadata":{"execution":{"iopub.status.busy":"2022-03-15T21:57:29.552727Z","iopub.execute_input":"2022-03-15T21:57:29.553273Z","iopub.status.idle":"2022-03-15T21:57:30.181988Z","shell.execute_reply.started":"2022-03-15T21:57:29.553231Z","shell.execute_reply":"2022-03-15T21:57:30.180728Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ans['img']=k","metadata":{"execution":{"iopub.status.busy":"2022-03-15T21:57:42.77858Z","iopub.execute_input":"2022-03-15T21:57:42.779022Z","iopub.status.idle":"2022-03-15T21:57:42.800673Z","shell.execute_reply.started":"2022-03-15T21:57:42.778971Z","shell.execute_reply":"2022-03-15T21:57:42.799465Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Creating CSV of required format","metadata":{}},{"cell_type":"code","source":"ans=ans[['img',0,1,2,3,4,5,6,7,8,9]]","metadata":{"execution":{"iopub.status.busy":"2022-03-15T21:59:50.983773Z","iopub.execute_input":"2022-03-15T21:59:50.984323Z","iopub.status.idle":"2022-03-15T21:59:50.996831Z","shell.execute_reply.started":"2022-03-15T21:59:50.984272Z","shell.execute_reply":"2022-03-15T21:59:50.995513Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ans","metadata":{"execution":{"iopub.status.busy":"2022-03-15T22:04:49.274715Z","iopub.execute_input":"2022-03-15T22:04:49.275283Z","iopub.status.idle":"2022-03-15T22:04:49.308149Z","shell.execute_reply.started":"2022-03-15T22:04:49.27524Z","shell.execute_reply":"2022-03-15T22:04:49.307129Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ans=ans.rename(columns={0:'c0',1:'c1',2:'c2',3:'c3',4:'c4',5:'c5',6:'c6',7:'c7',8:'c8',9:'c9'})","metadata":{"execution":{"iopub.status.busy":"2022-03-15T22:02:47.351148Z","iopub.execute_input":"2022-03-15T22:02:47.352552Z","iopub.status.idle":"2022-03-15T22:02:47.363627Z","shell.execute_reply.started":"2022-03-15T22:02:47.352496Z","shell.execute_reply":"2022-03-15T22:02:47.3623Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ans.head()","metadata":{"execution":{"iopub.status.busy":"2022-03-15T22:06:16.210352Z","iopub.execute_input":"2022-03-15T22:06:16.211268Z","iopub.status.idle":"2022-03-15T22:06:16.237613Z","shell.execute_reply.started":"2022-03-15T22:06:16.211219Z","shell.execute_reply":"2022-03-15T22:06:16.235853Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ans.to_csv(\"submission.csv\",index=False)","metadata":{"execution":{"iopub.status.busy":"2022-03-15T22:07:50.406309Z","iopub.execute_input":"2022-03-15T22:07:50.406832Z","iopub.status.idle":"2022-03-15T22:07:51.967577Z","shell.execute_reply.started":"2022-03-15T22:07:50.406795Z","shell.execute_reply":"2022-03-15T22:07:51.96673Z"},"trusted":true},"execution_count":null,"outputs":[]}]}