{"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":"2023-11-26T23:19:47.657294Z","iopub.execute_input":"2023-11-26T23:19:47.657615Z","iopub.status.idle":"2023-11-26T23:19:54.286430Z","shell.execute_reply.started":"2023-11-26T23:19:47.657589Z","shell.execute_reply":"2023-11-26T23:19:54.285356Z"},"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":"2023-11-26T23:07:14.844805Z","iopub.execute_input":"2023-11-26T23:07:14.845167Z","iopub.status.idle":"2023-11-26T23:07:14.880665Z","shell.execute_reply.started":"2023-11-26T23:07:14.845141Z","shell.execute_reply":"2023-11-26T23:07:14.879958Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"valid_subjects = img_list.subject.sort_values().unique()[-4:]","metadata":{"execution":{"iopub.status.busy":"2023-11-26T23:08:21.086287Z","iopub.execute_input":"2023-11-26T23:08:21.087036Z","iopub.status.idle":"2023-11-26T23:08:21.115068Z","shell.execute_reply.started":"2023-11-26T23:08:21.087002Z","shell.execute_reply":"2023-11-26T23:08:21.114129Z"},"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":"2023-11-26T23:12:34.678170Z","iopub.execute_input":"2023-11-26T23:12:34.678891Z","iopub.status.idle":"2023-11-26T23:12:34.684912Z","shell.execute_reply.started":"2023-11-26T23:12:34.678860Z","shell.execute_reply":"2023-11-26T23:12:34.684019Z"},"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":"2023-11-26T23:12:49.739447Z","iopub.execute_input":"2023-11-26T23:12:49.740329Z","iopub.status.idle":"2023-11-26T23:12:49.748777Z","shell.execute_reply.started":"2023-11-26T23:12:49.740299Z","shell.execute_reply":"2023-11-26T23:12:49.747783Z"},"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":"2023-11-26T23:13:06.755372Z","iopub.execute_input":"2023-11-26T23:13:06.756056Z","iopub.status.idle":"2023-11-26T23:13:06.766782Z","shell.execute_reply.started":"2023-11-26T23:13:06.756022Z","shell.execute_reply":"2023-11-26T23:13:06.765859Z"},"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":"2023-11-26T23:13:24.063211Z","iopub.execute_input":"2023-11-26T23:13:24.063942Z","iopub.status.idle":"2023-11-26T23:13:24.070886Z","shell.execute_reply.started":"2023-11-26T23:13:24.063912Z","shell.execute_reply":"2023-11-26T23:13:24.069876Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img_list.head()","metadata":{"execution":{"iopub.status.busy":"2023-11-26T23:25:47.707947Z","iopub.execute_input":"2023-11-26T23:25:47.708308Z","iopub.status.idle":"2023-11-26T23:25:47.719304Z","shell.execute_reply.started":"2023-11-26T23:25:47.708278Z","shell.execute_reply":"2023-11-26T23:25:47.718035Z"},"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":"2023-11-27T00:10:53.593878Z","iopub.execute_input":"2023-11-27T00:10:53.594249Z","iopub.status.idle":"2023-11-27T00:10:53.600375Z","shell.execute_reply.started":"2023-11-27T00:10:53.594220Z","shell.execute_reply":"2023-11-27T00:10:53.599248Z"},"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":"2023-11-27T00:15:55.378764Z","iopub.execute_input":"2023-11-27T00:15:55.379764Z","iopub.status.idle":"2023-11-27T00:15:57.064612Z","shell.execute_reply.started":"2023-11-27T00:15:55.379721Z","shell.execute_reply":"2023-11-27T00:15:57.063770Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dls.show_batch()","metadata":{"execution":{"iopub.status.busy":"2023-11-27T00:16:11.579721Z","iopub.execute_input":"2023-11-27T00:16:11.580588Z","iopub.status.idle":"2023-11-27T00:16:14.097878Z","shell.execute_reply.started":"2023-11-27T00:16:11.580545Z","shell.execute_reply":"2023-11-27T00:16:14.096869Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn = cnn_learner(dls, models.resnet34, metrics=accuracy, model_dir='/')","metadata":{"execution":{"iopub.status.busy":"2023-11-27T00:16:54.645817Z","iopub.execute_input":"2023-11-27T00:16:54.646787Z","iopub.status.idle":"2023-11-27T00:16:55.590151Z","shell.execute_reply.started":"2023-11-27T00:16:54.646738Z","shell.execute_reply":"2023-11-27T00:16:55.589122Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nlearn.fit_one_cycle(6, lr_max=1e-02)","metadata":{"execution":{"iopub.status.busy":"2023-11-27T00:18:11.780733Z","iopub.execute_input":"2023-11-27T00:18:11.781637Z","iopub.status.idle":"2023-11-27T00:39:58.622944Z","shell.execute_reply.started":"2023-11-27T00:18:11.781591Z","shell.execute_reply":"2023-11-27T00:39:58.621926Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.unfreeze()\nlearn.fit_one_cycle(6, lr_max=(1e-4, 1e-3, 1e-2))","metadata":{"execution":{"iopub.status.busy":"2023-11-27T00:45:39.188595Z","iopub.execute_input":"2023-11-27T00:45:39.189289Z","iopub.status.idle":"2023-11-27T01:14:44.186243Z","shell.execute_reply.started":"2023-11-27T00:45:39.189256Z","shell.execute_reply":"2023-11-27T01:14:44.185012Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.export('/kaggle/working/cnn_model.pth')","metadata":{"execution":{"iopub.status.busy":"2023-11-27T01:44:53.651664Z","iopub.execute_input":"2023-11-27T01:44:53.652624Z","iopub.status.idle":"2023-11-27T01:44:53.946846Z","shell.execute_reply.started":"2023-11-27T01:44:53.652592Z","shell.execute_reply":"2023-11-27T01:44:53.945858Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"loaded_learn = load_learner('/kaggle/working/cnn_model.pth')","metadata":{"execution":{"iopub.status.busy":"2023-11-27T01:45:24.595511Z","iopub.execute_input":"2023-11-27T01:45:24.596637Z","iopub.status.idle":"2023-11-27T01:45:24.718604Z","shell.execute_reply.started":"2023-11-27T01:45:24.596588Z","shell.execute_reply":"2023-11-27T01:45:24.717680Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"preds","metadata":{"execution":{"iopub.status.busy":"2023-11-27T01:54:50.440122Z","iopub.status.idle":"2023-11-27T01:54:50.440502Z","shell.execute_reply.started":"2023-11-27T01:54:50.440306Z","shell.execute_reply":"2023-11-27T01:54:50.440323Z"},"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_1.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":"2023-11-27T01:56:39.014447Z","iopub.execute_input":"2023-11-27T01:56:39.015422Z","iopub.status.idle":"2023-11-27T01:56:39.443527Z","shell.execute_reply.started":"2023-11-27T01:56:39.015387Z","shell.execute_reply":"2023-11-27T01:56:39.442791Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred","metadata":{"execution":{"iopub.status.busy":"2023-11-27T01:56:42.983772Z","iopub.execute_input":"2023-11-27T01:56:42.984148Z","iopub.status.idle":"2023-11-27T01:56:42.990032Z","shell.execute_reply.started":"2023-11-27T01:56:42.984117Z","shell.execute_reply":"2023-11-27T01:56:42.988957Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img.show()","metadata":{"execution":{"iopub.status.busy":"2023-11-27T01:56:47.697654Z","iopub.execute_input":"2023-11-27T01:56:47.698027Z","iopub.status.idle":"2023-11-27T01:56:47.977368Z","shell.execute_reply.started":"2023-11-27T01:56:47.697997Z","shell.execute_reply":"2023-11-27T01:56:47.976423Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}