{"metadata":{"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"},"kaggle":{"accelerator":"nvidiaTeslaT4","dataSources":[{"sourceId":5048,"databundleVersionId":868335,"sourceType":"competition"}],"dockerImageVersionId":30684,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"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.status.busy":"2025-10-07T16:49:07.594565Z","iopub.execute_input":"2025-10-07T16:49:07.595254Z","iopub.status.idle":"2025-10-07T16:49:36.704693Z","shell.execute_reply.started":"2025-10-07T16:49:07.595227Z","shell.execute_reply":"2025-10-07T16:49:36.703662Z"},"trusted":true},"outputs":[],"execution_count":null},{"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.status.busy":"2025-10-07T16:49:45.099108Z","iopub.execute_input":"2025-10-07T16:49:45.099810Z","iopub.status.idle":"2025-10-07T16:49:49.036682Z","shell.execute_reply.started":"2025-10-07T16:49:45.099779Z","shell.execute_reply":"2025-10-07T16:49:49.035735Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Training","metadata":{}},{"cell_type":"code","source":"model = YOLO('yolov8n-cls.pt')","metadata":{"execution":{"iopub.status.busy":"2025-10-07T16:49:53.275286Z","iopub.execute_input":"2025-10-07T16:49:53.276250Z","iopub.status.idle":"2025-10-07T16:49:56.252828Z","shell.execute_reply.started":"2025-10-07T16:49:53.276219Z","shell.execute_reply":"2025-10-07T16:49:56.252125Z"},"trusted":true},"outputs":[],"execution_count":null},{"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.status.busy":"2025-10-07T16:50:01.703339Z","iopub.execute_input":"2025-10-07T16:50:01.704293Z","iopub.status.idle":"2025-10-07T16:51:40.831001Z","shell.execute_reply.started":"2025-10-07T16:50:01.704267Z","shell.execute_reply":"2025-10-07T16:51:40.830122Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"results = model.train(data = '/kaggle/working/output', epochs = 5)","metadata":{"execution":{"iopub.status.busy":"2025-10-07T16:52:05.275176Z","iopub.execute_input":"2025-10-07T16:52:05.275884Z","iopub.status.idle":"2025-10-07T17:00:23.004191Z","shell.execute_reply.started":"2025-10-07T16:52:05.275853Z","shell.execute_reply":"2025-10-07T17:00:23.003370Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Predicting","metadata":{}},{"cell_type":"code","source":"model.val()","metadata":{"execution":{"iopub.status.busy":"2025-10-07T17:01:09.171269Z","iopub.execute_input":"2025-10-07T17:01:09.171805Z","iopub.status.idle":"2025-10-07T17:01:24.734047Z","shell.execute_reply.started":"2025-10-07T17:01:09.171773Z","shell.execute_reply":"2025-10-07T17:01:24.733195Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df = pd.read_csv(\"/kaggle/working/runs/classify/train/results.csv\")\ndf.head()","metadata":{"execution":{"iopub.status.busy":"2025-10-07T17:01:57.191999Z","iopub.execute_input":"2025-10-07T17:01:57.192850Z","iopub.status.idle":"2025-10-07T17:01:57.217274Z","shell.execute_reply.started":"2025-10-07T17:01:57.192805Z","shell.execute_reply":"2025-10-07T17:01:57.216368Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"Image(\"/kaggle/working/runs/classify/train/results.png\")","metadata":{"execution":{"iopub.status.busy":"2025-10-07T17:02:03.604020Z","iopub.execute_input":"2025-10-07T17:02:03.604306Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"Image(\"/kaggle/working/runs/classify/train/confusion_matrix_normalized.png\")","metadata":{"execution":{"iopub.status.busy":"2025-10-07T17:02:17.003193Z","iopub.execute_input":"2025-10-07T17:02:17.003622Z"},"trusted":true},"outputs":[],"execution_count":null},{"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.status.busy":"2025-10-07T17:02:22.788297Z","iopub.execute_input":"2025-10-07T17:02:22.788675Z","iopub.status.idle":"2025-10-07T17:02:24.344557Z","shell.execute_reply.started":"2025-10-07T17:02:22.788648Z","shell.execute_reply":"2025-10-07T17:02:24.343707Z"},"trusted":true},"outputs":[],"execution_count":null},{"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.status.busy":"2025-10-07T17:02:34.458718Z","iopub.execute_input":"2025-10-07T17:02:34.459588Z","iopub.status.idle":"2025-10-07T17:02:34.463722Z","shell.execute_reply.started":"2025-10-07T17:02:34.459560Z","shell.execute_reply":"2025-10-07T17:02:34.462783Z"},"trusted":true},"outputs":[],"execution_count":null},{"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.status.busy":"2025-10-07T17:02:42.235026Z","iopub.execute_input":"2025-10-07T17:02:42.235350Z","iopub.status.idle":"2025-10-07T17:02:47.358828Z","shell.execute_reply.started":"2025-10-07T17:02:42.235325Z","shell.execute_reply":"2025-10-07T17:02:47.356262Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"!zip -r output.zip output\n\nfrom IPython.display import FileLink\nFileLink(r'output.zip')\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-07T17:11:11.996922Z","iopub.execute_input":"2025-10-07T17:11:11.997269Z"}},"outputs":[],"execution_count":null}]}