{"metadata":{"kaggle":{"accelerator":"gpu","dataSources":[{"sourceType":"competition","sourceId":5048,"databundleVersionId":868335}],"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true},"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"codemirror_mode":{"name":"ipython","version":3},"file_extension":".py","mimetype":"text/x-python","name":"python","nbconvert_exporter":"python","pygments_lexer":"ipython3","version":"3.10.13"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# Imports","metadata":{}},{"cell_type":"code","source":"%%capture --no-display\n!pip install ultralytics\n!pip install split-folders\n!pip install -U ipywidgets","metadata":{"execution":{"iopub.execute_input":"2024-04-11T18:07:19.126300Z","iopub.status.busy":"2024-04-11T18:07:19.125753Z","iopub.status.idle":"2024-04-11T18:07:58.566381Z","shell.execute_reply":"2024-04-11T18:07:58.565312Z","shell.execute_reply.started":"2024-04-11T18:07:19.126269Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from ultralytics import YOLO\nimport matplotlib.pyplot as plt\nimport os\nimport splitfolders\nfrom IPython.display import display, Image\nimport pandas as pd","metadata":{"execution":{"iopub.execute_input":"2024-04-11T20:07:59.751189Z","iopub.status.busy":"2024-04-11T20:07:59.750810Z","iopub.status.idle":"2024-04-11T20:07:59.756246Z","shell.execute_reply":"2024-04-11T20:07:59.755306Z","shell.execute_reply.started":"2024-04-11T20:07:59.751158Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Training","metadata":{}},{"cell_type":"code","source":"model = YOLO('yolov8n-cls.pt')","metadata":{"execution":{"iopub.execute_input":"2024-04-11T18:08:09.092113Z","iopub.status.busy":"2024-04-11T18:08:09.091848Z","iopub.status.idle":"2024-04-11T18:08:10.725066Z","shell.execute_reply":"2024-04-11T18:08:10.724107Z","shell.execute_reply.started":"2024-04-11T18:08:09.092090Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Splitting Data into Train, Validation & Test","metadata":{}},{"cell_type":"code","source":"splitfolders.ratio(\"/kaggle/input/state-farm-distracted-driver-detection/imgs/train\", output=\"output\", seed=1337, ratio=(0.7, 0.15, 0.15))","metadata":{"execution":{"iopub.execute_input":"2024-04-11T18:08:24.856176Z","iopub.status.busy":"2024-04-11T18:08:24.855319Z","iopub.status.idle":"2024-04-11T18:12:49.371050Z","shell.execute_reply":"2024-04-11T18:12:49.370104Z","shell.execute_reply.started":"2024-04-11T18:08:24.856134Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"results = model.train(data = '/kaggle/working/output', epochs = 10)","metadata":{"execution":{"iopub.execute_input":"2024-04-11T18:13:34.015899Z","iopub.status.busy":"2024-04-11T18:13:34.015526Z","iopub.status.idle":"2024-04-11T18:29:46.769717Z","shell.execute_reply":"2024-04-11T18:29:46.768456Z","shell.execute_reply.started":"2024-04-11T18:13:34.015870Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Predicting","metadata":{}},{"cell_type":"code","source":"model.val()","metadata":{"execution":{"iopub.execute_input":"2024-04-11T18:33:08.498714Z","iopub.status.busy":"2024-04-11T18:33:08.497950Z","iopub.status.idle":"2024-04-11T18:33:24.532142Z","shell.execute_reply":"2024-04-11T18:33:24.531100Z","shell.execute_reply.started":"2024-04-11T18:33:08.498681Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.read_csv(\"/kaggle/working/runs/classify/train/results.csv\")\ndf.head()","metadata":{"execution":{"iopub.execute_input":"2024-04-11T18:34:00.566580Z","iopub.status.busy":"2024-04-11T18:34:00.566197Z","iopub.status.idle":"2024-04-11T18:34:00.587731Z","shell.execute_reply":"2024-04-11T18:34:00.586827Z","shell.execute_reply.started":"2024-04-11T18:34:00.566548Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Image(\"/kaggle/working/runs/classify/train/results.png\")","metadata":{"execution":{"iopub.execute_input":"2024-04-11T19:48:20.933971Z","iopub.status.busy":"2024-04-11T19:48:20.933317Z","iopub.status.idle":"2024-04-11T19:48:20.943839Z","shell.execute_reply":"2024-04-11T19:48:20.942993Z","shell.execute_reply.started":"2024-04-11T19:48:20.933936Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Image(\"/kaggle/working/runs/classify/train/confusion_matrix_normalized.png\")","metadata":{"execution":{"iopub.execute_input":"2024-04-11T19:49:07.252186Z","iopub.status.busy":"2024-04-11T19:49:07.251816Z","iopub.status.idle":"2024-04-11T19:49:07.260948Z","shell.execute_reply":"2024-04-11T19:49:07.260047Z","shell.execute_reply.started":"2024-04-11T19:49:07.252157Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"path = \"/kaggle/input/state-farm-distracted-driver-detection/imgs/test/\"\nmodel_weights = \"/kaggle/working/runs/classify/train/weights/best.pt\"\npred = [(path+i,model.predict(path+i, model = model_weights)[0].probs.top1) for i in os.listdir(path)[:45]]","metadata":{"execution":{"iopub.execute_input":"2024-04-11T20:10:41.638247Z","iopub.status.busy":"2024-04-11T20:10:41.637301Z","iopub.status.idle":"2024-04-11T20:10:42.659308Z","shell.execute_reply":"2024-04-11T20:10:42.658377Z","shell.execute_reply.started":"2024-04-11T20:10:41.638210Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels = {\n    0: 'normal driving',\n1: 'texting - right',\n2: 'talking on the phone - right',\n3: 'texting - left',\n4: 'talking on the phone - left',\n5: 'operating the radio',\n6: 'drinking',\n7: 'reaching behind',\n8: 'hair and makeup',\n9: 'talking to passenger'}","metadata":{"execution":{"iopub.execute_input":"2024-04-11T20:10:48.243504Z","iopub.status.busy":"2024-04-11T20:10:48.243134Z","iopub.status.idle":"2024-04-11T20:10:48.248471Z","shell.execute_reply":"2024-04-11T20:10:48.247549Z","shell.execute_reply.started":"2024-04-11T20:10:48.243472Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"rows = 9\ncols = 5\nfig, ax = plt.subplots(rows, cols, figsize=(20, 20))\nfor i, (img, label) in enumerate(pred):\n    row = i // cols\n    col = i % cols\n    ax[row, col].imshow(plt.imread(img))\n    ax[row, col].set_title(labels.get(label))\n    ax[row, col].axis('off')\n\nplt.suptitle(\"Predicted Images\")\nplt.tight_layout()\nplt.show()","metadata":{"execution":{"iopub.execute_input":"2024-04-11T20:12:30.407999Z","iopub.status.busy":"2024-04-11T20:12:30.407586Z","iopub.status.idle":"2024-04-11T20:12:36.176198Z","shell.execute_reply":"2024-04-11T20:12:36.175201Z","shell.execute_reply.started":"2024-04-11T20:12:30.407967Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}