{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"nvidiaTeslaT4","dataSources":[{"sourceId":5048,"databundleVersionId":868335,"sourceType":"competition"}],"dockerImageVersionId":30699,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"%%capture --no-display\n!pip install ultralytics\n!pip install split-folders\n!pip install -U ipywidgets","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-05-23T17:16:34.143332Z","iopub.execute_input":"2024-05-23T17:16:34.143935Z","iopub.status.idle":"2024-05-23T17:17:13.994855Z","shell.execute_reply.started":"2024-05-23T17:16:34.143903Z","shell.execute_reply":"2024-05-23T17:17:13.993522Z"},"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.status.busy":"2024-05-23T17:17:13.997123Z","iopub.execute_input":"2024-05-23T17:17:13.997496Z","iopub.status.idle":"2024-05-23T17:17:18.397335Z","shell.execute_reply.started":"2024-05-23T17:17:13.99746Z","shell.execute_reply":"2024-05-23T17:17:18.396386Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = YOLO('yolov8n-cls.pt')","metadata":{"execution":{"iopub.status.busy":"2024-05-23T17:17:18.39856Z","iopub.execute_input":"2024-05-23T17:17:18.398959Z","iopub.status.idle":"2024-05-23T17:17:21.95227Z","shell.execute_reply.started":"2024-05-23T17:17:18.398932Z","shell.execute_reply":"2024-05-23T17:17:21.951261Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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":"2024-05-23T17:17:21.954968Z","iopub.execute_input":"2024-05-23T17:17:21.95571Z","iopub.status.idle":"2024-05-23T17:20:35.09951Z","shell.execute_reply.started":"2024-05-23T17:17:21.955675Z","shell.execute_reply":"2024-05-23T17:20:35.098595Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"results = model.train(data = '/kaggle/working/output', epochs = 100)","metadata":{"execution":{"iopub.status.busy":"2024-05-23T17:20:35.100615Z","iopub.execute_input":"2024-05-23T17:20:35.100887Z","iopub.status.idle":"2024-05-23T20:13:12.057132Z","shell.execute_reply.started":"2024-05-23T17:20:35.100862Z","shell.execute_reply":"2024-05-23T20:13:12.056043Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.val()","metadata":{"execution":{"iopub.status.busy":"2024-05-23T20:13:12.058936Z","iopub.execute_input":"2024-05-23T20:13:12.059351Z","iopub.status.idle":"2024-05-23T20:13:28.977076Z","shell.execute_reply.started":"2024-05-23T20:13:12.059317Z","shell.execute_reply":"2024-05-23T20:13:28.976021Z"},"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.status.busy":"2024-05-23T20:13:28.978613Z","iopub.execute_input":"2024-05-23T20:13:28.978923Z","iopub.status.idle":"2024-05-23T20:13:29.001642Z","shell.execute_reply.started":"2024-05-23T20:13:28.978894Z","shell.execute_reply":"2024-05-23T20:13:29.000785Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Image(\"/kaggle/working/runs/classify/train/results.png\")","metadata":{"execution":{"iopub.status.busy":"2024-05-23T20:13:29.002618Z","iopub.execute_input":"2024-05-23T20:13:29.002888Z","iopub.status.idle":"2024-05-23T20:13:29.014195Z","shell.execute_reply.started":"2024-05-23T20:13:29.002864Z","shell.execute_reply":"2024-05-23T20:13:29.013206Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Image(\"/kaggle/working/runs/classify/train/confusion_matrix_normalized.png\")","metadata":{"execution":{"iopub.status.busy":"2024-05-23T20:13:29.015236Z","iopub.execute_input":"2024-05-23T20:13:29.01548Z","iopub.status.idle":"2024-05-23T20:13:29.024018Z","shell.execute_reply.started":"2024-05-23T20:13:29.015458Z","shell.execute_reply":"2024-05-23T20:13:29.023008Z"},"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.status.busy":"2024-05-23T20:13:29.029065Z","iopub.execute_input":"2024-05-23T20:13:29.029378Z","iopub.status.idle":"2024-05-23T20:13:33.640294Z","shell.execute_reply.started":"2024-05-23T20:13:29.029356Z","shell.execute_reply":"2024-05-23T20:13:33.639227Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels = {\n0: '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":"2024-05-23T20:13:33.641691Z","iopub.execute_input":"2024-05-23T20:13:33.642178Z","iopub.status.idle":"2024-05-23T20:13:33.647014Z","shell.execute_reply.started":"2024-05-23T20:13:33.64215Z","shell.execute_reply":"2024-05-23T20:13:33.645551Z"},"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.status.busy":"2024-05-23T20:13:33.648028Z","iopub.execute_input":"2024-05-23T20:13:33.648297Z","iopub.status.idle":"2024-05-23T20:13:39.462535Z","shell.execute_reply.started":"2024-05-23T20:13:33.648275Z","shell.execute_reply":"2024-05-23T20:13:39.461543Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.save(\"/kaggle/working/yolov8n-cls-best.pt\")\n","metadata":{"execution":{"iopub.status.busy":"2024-05-23T20:13:39.463793Z","iopub.execute_input":"2024-05-23T20:13:39.464126Z","iopub.status.idle":"2024-05-23T20:13:39.607583Z","shell.execute_reply.started":"2024-05-23T20:13:39.464099Z","shell.execute_reply":"2024-05-23T20:13:39.606813Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_weights = \"/kaggle/working/runs/classify/train/weights/best.pt\"","metadata":{"execution":{"iopub.status.busy":"2024-05-23T20:17:02.214484Z","iopub.execute_input":"2024-05-23T20:17:02.214861Z","iopub.status.idle":"2024-05-23T20:17:02.21923Z","shell.execute_reply.started":"2024-05-23T20:17:02.214829Z","shell.execute_reply":"2024-05-23T20:17:02.218266Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}