{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"gpu","dataSources":[{"sourceId":5048,"databundleVersionId":868335,"sourceType":"competition"}],"dockerImageVersionId":30588,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\nimport os\n\n# import fast ai vision library\nfrom fastai.vision.all import *","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-05-27T06:35:13.647888Z","iopub.execute_input":"2024-05-27T06:35:13.648236Z","iopub.status.idle":"2024-05-27T06:35:21.130056Z","shell.execute_reply.started":"2024-05-27T06:35:13.648207Z","shell.execute_reply":"2024-05-27T06:35:21.129125Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"path = '/kaggle/input/state-farm-distracted-driver-detection/'\nimg_list = pd.read_csv(path + 'driver_imgs_list.csv')","metadata":{"execution":{"iopub.status.busy":"2024-05-27T06:35:21.131789Z","iopub.execute_input":"2024-05-27T06:35:21.132087Z","iopub.status.idle":"2024-05-27T06:35:21.171964Z","shell.execute_reply.started":"2024-05-27T06:35:21.132046Z","shell.execute_reply":"2024-05-27T06:35:21.171243Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"valid_subjects = img_list.subject.sort_values().unique()[-4:]","metadata":{"execution":{"iopub.status.busy":"2024-05-27T06:35:21.173173Z","iopub.execute_input":"2024-05-27T06:35:21.173461Z","iopub.status.idle":"2024-05-27T06:35:21.204115Z","shell.execute_reply.started":"2024-05-27T06:35:21.173438Z","shell.execute_reply":"2024-05-27T06:35:21.20344Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img_list['is_valid'] = img_list['subject'].isin(valid_subjects)","metadata":{"execution":{"iopub.status.busy":"2024-05-27T06:35:21.206446Z","iopub.execute_input":"2024-05-27T06:35:21.20679Z","iopub.status.idle":"2024-05-27T06:35:21.214719Z","shell.execute_reply.started":"2024-05-27T06:35:21.206756Z","shell.execute_reply":"2024-05-27T06:35:21.213785Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"valid subjects: \", valid_subjects)\nprint(img_list[img_list['is_valid']==True].subject.count())","metadata":{"execution":{"iopub.status.busy":"2024-05-27T06:35:21.215775Z","iopub.execute_input":"2024-05-27T06:35:21.216117Z","iopub.status.idle":"2024-05-27T06:35:21.229948Z","shell.execute_reply.started":"2024-05-27T06:35:21.216065Z","shell.execute_reply":"2024-05-27T06:35:21.229084Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# create new column with class folder and image name combined\nimg_list['img_path'] = img_list.classname + '/' + img_list.img","metadata":{"execution":{"iopub.status.busy":"2024-05-27T06:35:21.231028Z","iopub.execute_input":"2024-05-27T06:35:21.23131Z","iopub.status.idle":"2024-05-27T06:35:21.24224Z","shell.execute_reply.started":"2024-05-27T06:35:21.231288Z","shell.execute_reply":"2024-05-27T06:35:21.241264Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"valid_names = img_list[img_list['subject'].isin(valid_subjects)].img\nvalid_names = valid_names.to_list()","metadata":{"execution":{"iopub.status.busy":"2024-05-27T06:35:21.243433Z","iopub.execute_input":"2024-05-27T06:35:21.24377Z","iopub.status.idle":"2024-05-27T06:35:21.252727Z","shell.execute_reply.started":"2024-05-27T06:35:21.243719Z","shell.execute_reply":"2024-05-27T06:35:21.252036Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img_list.head()","metadata":{"execution":{"iopub.status.busy":"2024-05-27T06:35:21.253983Z","iopub.execute_input":"2024-05-27T06:35:21.25428Z","iopub.status.idle":"2024-05-27T06:35:21.27015Z","shell.execute_reply.started":"2024-05-27T06:35:21.254246Z","shell.execute_reply":"2024-05-27T06:35:21.269318Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Convert images to 'Float' data type and apply data augmentation transforms\ntfms = [ToTensor(), IntToFloatTensor()] + aug_transforms(do_flip=False)","metadata":{"execution":{"iopub.status.busy":"2024-05-27T06:35:21.271046Z","iopub.execute_input":"2024-05-27T06:35:21.271336Z","iopub.status.idle":"2024-05-27T06:35:21.27745Z","shell.execute_reply.started":"2024-05-27T06:35:21.271314Z","shell.execute_reply":"2024-05-27T06:35:21.276572Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n# Create ImageDataLoaders\ndls = ImageDataLoaders.from_df(img_list, path=path + 'imgs/train/',\n                               fn_col='img_path', valid_col='is_valid',batch_tfms=tfms, get_x=ColReader('classname', pref=path + 'imgs/train/'))\n","metadata":{"execution":{"iopub.status.busy":"2024-05-27T06:35:21.281613Z","iopub.execute_input":"2024-05-27T06:35:21.282106Z","iopub.status.idle":"2024-05-27T06:35:23.814145Z","shell.execute_reply.started":"2024-05-27T06:35:21.282057Z","shell.execute_reply":"2024-05-27T06:35:23.813127Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dls.show_batch()","metadata":{"execution":{"iopub.status.busy":"2024-05-27T06:35:23.815329Z","iopub.execute_input":"2024-05-27T06:35:23.815627Z","iopub.status.idle":"2024-05-27T06:35:26.484951Z","shell.execute_reply.started":"2024-05-27T06:35:23.815602Z","shell.execute_reply":"2024-05-27T06:35:26.484052Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"","metadata":{}},{"cell_type":"code","source":"learn = cnn_learner(dls, models.resnet34, metrics=accuracy, model_dir='/')","metadata":{"execution":{"iopub.status.busy":"2024-05-27T06:35:26.485974Z","iopub.execute_input":"2024-05-27T06:35:26.48631Z","iopub.status.idle":"2024-05-27T06:35:27.665126Z","shell.execute_reply.started":"2024-05-27T06:35:26.486281Z","shell.execute_reply":"2024-05-27T06:35:27.664126Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nlearn.fit_one_cycle(24, lr_max=1e-02)","metadata":{"execution":{"iopub.status.busy":"2024-05-27T06:35:27.666501Z","iopub.execute_input":"2024-05-27T06:35:27.666888Z","iopub.status.idle":"2024-05-27T08:01:34.143264Z","shell.execute_reply.started":"2024-05-27T06:35:27.666853Z","shell.execute_reply":"2024-05-27T08:01:34.142356Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.unfreeze()\nlearn.fit_one_cycle(24, lr_max=(1e-4, 1e-3, 1e-2))","metadata":{"execution":{"iopub.status.busy":"2024-05-27T08:01:34.14463Z","iopub.execute_input":"2024-05-27T08:01:34.144946Z","iopub.status.idle":"2024-05-27T09:57:22.128787Z","shell.execute_reply.started":"2024-05-27T08:01:34.144914Z","shell.execute_reply":"2024-05-27T09:57:22.126397Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.export('/kaggle/working/cnn_model.pth')","metadata":{"execution":{"iopub.status.busy":"2024-05-27T09:57:22.130953Z","iopub.execute_input":"2024-05-27T09:57:22.131771Z","iopub.status.idle":"2024-05-27T09:57:22.76005Z","shell.execute_reply.started":"2024-05-27T09:57:22.131722Z","shell.execute_reply":"2024-05-27T09:57:22.759131Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"loaded_learn = load_learner('/kaggle/working/cnn_model.pth')","metadata":{"execution":{"iopub.status.busy":"2024-05-27T09:57:22.761389Z","iopub.execute_input":"2024-05-27T09:57:22.762189Z","iopub.status.idle":"2024-05-27T09:57:22.902283Z","shell.execute_reply.started":"2024-05-27T09:57:22.762139Z","shell.execute_reply":"2024-05-27T09:57:22.901486Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred","metadata":{"execution":{"iopub.status.busy":"2024-05-27T10:47:12.083777Z","iopub.execute_input":"2024-05-27T10:47:12.084042Z","iopub.status.idle":"2024-05-27T10:47:12.411285Z","shell.execute_reply.started":"2024-05-27T10:47:12.084011Z","shell.execute_reply":"2024-05-27T10:47:12.410176Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n\n# Load and preprocess the single image you want to predict\nimg_path = '/kaggle/input/state-farm-distracted-driver-detection/imgs/test/img_3.jpg'  # Replace with the path to your image\nimg = PILImage.create(img_path)\n\n# Make the prediction\npred, _, prob = loaded_learn.predict(img)\n\n\n","metadata":{"execution":{"iopub.status.busy":"2024-05-27T10:47:21.331038Z","iopub.execute_input":"2024-05-27T10:47:21.331451Z","iopub.status.idle":"2024-05-27T10:47:21.356302Z","shell.execute_reply.started":"2024-05-27T10:47:21.331421Z","shell.execute_reply":"2024-05-27T10:47:21.355172Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred","metadata":{"execution":{"iopub.status.busy":"2024-05-27T10:47:17.141379Z","iopub.execute_input":"2024-05-27T10:47:17.142309Z","iopub.status.idle":"2024-05-27T10:47:17.17025Z","shell.execute_reply.started":"2024-05-27T10:47:17.142273Z","shell.execute_reply":"2024-05-27T10:47:17.168773Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.summary()","metadata":{"execution":{"iopub.status.busy":"2024-05-27T09:57:23.515809Z","iopub.status.idle":"2024-05-27T09:57:23.51614Z","shell.execute_reply.started":"2024-05-27T09:57:23.515958Z","shell.execute_reply":"2024-05-27T09:57:23.515973Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img.show()","metadata":{"execution":{"iopub.status.busy":"2024-05-27T09:57:23.517254Z","iopub.status.idle":"2024-05-27T09:57:23.51762Z","shell.execute_reply.started":"2024-05-27T09:57:23.517438Z","shell.execute_reply":"2024-05-27T09:57:23.517471Z"},"trusted":true},"execution_count":null,"outputs":[]}]}