{"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":"none","dataSources":[{"sourceId":5048,"databundleVersionId":868335,"sourceType":"competition"}],"dockerImageVersionId":30684,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"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-11-11T22:42:31.159642Z","iopub.execute_input":"2025-11-11T22:42:31.159938Z","iopub.status.idle":"2025-11-11T22:42:57.169127Z","shell.execute_reply.started":"2025-11-11T22:42:31.159912Z","shell.execute_reply":"2025-11-11T22:42:57.167765Z"},"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-11-11T22:42:57.170970Z","iopub.execute_input":"2025-11-11T22:42:57.171370Z","iopub.status.idle":"2025-11-11T22:42:57.177479Z","shell.execute_reply.started":"2025-11-11T22:42:57.171334Z","shell.execute_reply":"2025-11-11T22:42:57.176551Z"},"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-11-11T22:42:57.178652Z","iopub.execute_input":"2025-11-11T22:42:57.178984Z","iopub.status.idle":"2025-11-11T22:42:57.212645Z","shell.execute_reply.started":"2025-11-11T22:42:57.178957Z","shell.execute_reply":"2025-11-11T22:42:57.211765Z"},"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-11-11T22:42:57.213881Z","iopub.execute_input":"2025-11-11T22:42:57.214525Z","iopub.status.idle":"2025-11-11T22:43:43.874120Z","shell.execute_reply.started":"2025-11-11T22:42:57.214494Z","shell.execute_reply":"2025-11-11T22:43:43.873438Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"results = model.train(data = '/kaggle/working/output', epochs = 12)","metadata":{"execution":{"iopub.status.busy":"2025-11-11T22:43:43.875392Z","iopub.execute_input":"2025-11-11T22:43:43.875657Z","iopub.status.idle":"2025-11-11T23:03:42.349848Z","shell.execute_reply.started":"2025-11-11T22:43:43.875634Z","shell.execute_reply":"2025-11-11T23:03:42.348730Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Predicting","metadata":{}},{"cell_type":"code","source":"model.val()","metadata":{"execution":{"iopub.status.busy":"2025-11-11T23:03:42.351704Z","iopub.execute_input":"2025-11-11T23:03:42.352465Z","iopub.status.idle":"2025-11-11T23:03:58.022406Z","shell.execute_reply.started":"2025-11-11T23:03:42.352429Z","shell.execute_reply":"2025-11-11T23:03:58.021430Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df = pd.read_csv(\"/kaggle/working/runs/classify/train/results.csv\")\ndf.head(5)","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"Image(\"/kaggle/working/runs/classify/train/results.png\")","metadata":{"execution":{"iopub.status.busy":"2025-11-11T23:03:58.049462Z","iopub.execute_input":"2025-11-11T23:03:58.049707Z","iopub.status.idle":"2025-11-11T23:03:58.058413Z","execution_failed":"2025-11-11T23:44:01.958Z"},"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-11-12T02:42:40.096910Z","iopub.execute_input":"2025-11-12T02:42:40.097465Z","iopub.status.idle":"2025-11-12T02:42:40.320079Z","shell.execute_reply.started":"2025-11-12T02:42:40.097424Z","shell.execute_reply":"2025-11-12T02:42:40.318876Z"},"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-11-11T23:03:58.074684Z","iopub.execute_input":"2025-11-11T23:03:58.074977Z","iopub.status.idle":"2025-11-11T23:03:58.786698Z","execution_failed":"2025-11-11T23:44:02.099Z"},"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-11-11T23:03:58.790098Z","iopub.execute_input":"2025-11-11T23:03:58.790364Z","iopub.status.idle":"2025-11-11T23:03:58.794864Z","execution_failed":"2025-11-11T23:44:02.110Z"},"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-11-11T23:03:58.795736Z","iopub.execute_input":"2025-11-11T23:03:58.795944Z","iopub.status.idle":"2025-11-11T23:04:04.127063Z","execution_failed":"2025-11-11T23:44:02.120Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"!zip -r /kaggle/working/runs.zip /kaggle/working/runs","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-11T23:04:04.128696Z","iopub.execute_input":"2025-11-11T23:04:04.129140Z","iopub.status.idle":"2025-11-11T23:04:06.394906Z","execution_failed":"2025-11-11T23:44:02.130Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}