{"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":30840,"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 roboflow\n!pip install ultralytics\n!pip install split-folders","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2025-02-01T23:57:41.107878Z","iopub.execute_input":"2025-02-01T23:57:41.108190Z","iopub.status.idle":"2025-02-01T23:57:54.818523Z","shell.execute_reply.started":"2025-02-01T23:57:41.108164Z","shell.execute_reply":"2025-02-01T23:57:54.817514Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import ultralytics\nfrom ultralytics import YOLO\nfrom IPython.display import Image\nfrom roboflow import Roboflow\nfrom kaggle_secrets import UserSecretsClient\nimport os\nimport splitfolders\n\nultralytics.checks()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-01T23:57:56.093343Z","iopub.execute_input":"2025-02-01T23:57:56.093664Z","iopub.status.idle":"2025-02-01T23:57:56.105898Z","shell.execute_reply.started":"2025-02-01T23:57:56.093638Z","shell.execute_reply":"2025-02-01T23:57:56.105163Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"splitfolders.ratio(\"/kaggle/input/state-farm-distracted-driver-detection/imgs/train\", output=\"dataset\", seed=32, ratio=(0.7, 0.15, 0.15))\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-01T23:58:29.582079Z","iopub.execute_input":"2025-02-01T23:58:29.582361Z","iopub.status.idle":"2025-02-02T00:00:51.093741Z","shell.execute_reply.started":"2025-02-01T23:58:29.582341Z","shell.execute_reply":"2025-02-02T00:00:51.092881Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model = YOLO('yolo11s-cls.pt')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-01T23:57:58.741792Z","iopub.execute_input":"2025-02-01T23:57:58.742122Z","iopub.status.idle":"2025-02-01T23:58:00.076362Z","shell.execute_reply.started":"2025-02-01T23:57:58.742093Z","shell.execute_reply":"2025-02-01T23:58:00.075660Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"results = model.train(data = '/kaggle/working/dataset', epochs = 150, batch=32, imgsz=640,degrees=10, patience=8,seed=42)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-02T00:14:44.675547Z","iopub.execute_input":"2025-02-02T00:14:44.675855Z","iopub.status.idle":"2025-02-02T00:21:00.323540Z","shell.execute_reply.started":"2025-02-02T00:14:44.675832Z","shell.execute_reply":"2025-02-02T00:21:00.322320Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"Image(\"/kaggle/working/runs/classify/train/train_batch0.jpg\", width=600)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-02T00:26:39.734761Z","iopub.execute_input":"2025-02-02T00:26:39.735126Z","iopub.status.idle":"2025-02-02T00:26:39.756196Z","shell.execute_reply.started":"2025-02-02T00:26:39.735095Z","shell.execute_reply":"2025-02-02T00:26:39.755303Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"metrics = model.val()\nprint(\"Validation Metrics:\")\nprint(metrics)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-02T00:22:14.173037Z","iopub.execute_input":"2025-02-02T00:22:14.173348Z","iopub.status.idle":"2025-02-02T00:22:57.729791Z","shell.execute_reply.started":"2025-02-02T00:22:14.173321Z","shell.execute_reply":"2025-02-02T00:22:57.728770Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_img_path = '/kaggle/working/dataset/test/c0/img_100074.jpg'  # adjust path as needed\nresults = model.predict(test_img_path, imgsz=640)\nresult = results[0]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-02T00:30:49.774591Z","iopub.execute_input":"2025-02-02T00:30:49.774901Z","iopub.status.idle":"2025-02-02T00:30:49.838897Z","shell.execute_reply.started":"2025-02-02T00:30:49.774878Z","shell.execute_reply":"2025-02-02T00:30:49.838271Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nimport numpy as np\nfrom PIL import Image\n\nplt.figure(figsize=(12, 4))\nplt.subplot(1, 2, 1)\nimg = Image.open(test_img_path)\nplt.imshow(img)\nplt.title(f'Predicted: {result.names[result.probs.top1]}')\nplt.axis('off')\n\n# Show probability distribution\nplt.subplot(1, 2, 2)\nprobs = result.probs.data.cpu().numpy()\nclass_names = list(result.names.values())\ny_pos = np.arange(len(class_names))\nplt.barh(y_pos, probs)\nplt.yticks(y_pos, class_names)\nplt.xlabel('Probability')\nplt.title('Class Probabilities')\n\nplt.tight_layout()\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-02T00:32:46.762289Z","iopub.execute_input":"2025-02-02T00:32:46.762612Z","iopub.status.idle":"2025-02-02T00:32:47.082637Z","shell.execute_reply.started":"2025-02-02T00:32:46.762588Z","shell.execute_reply":"2025-02-02T00:32:47.081729Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}