{"metadata":{"kaggle":{"accelerator":"gpu","dataSources":[{"sourceId":5048,"databundleVersionId":868335,"sourceType":"competition"},{"sourceId":8357922,"sourceType":"datasetVersion","datasetId":4966732}],"dockerImageVersionId":30684,"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.status.busy":"2024-05-13T21:55:04.752404Z","iopub.execute_input":"2024-05-13T21:55:04.75282Z","iopub.status.idle":"2024-05-13T21:55:37.477311Z","shell.execute_reply.started":"2024-05-13T21:55:04.752786Z","shell.execute_reply":"2024-05-13T21:55:37.476044Z"},"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-13T21:56:04.814549Z","iopub.execute_input":"2024-05-13T21:56:04.814915Z","iopub.status.idle":"2024-05-13T21:56:04.81972Z","shell.execute_reply.started":"2024-05-13T21:56:04.814886Z","shell.execute_reply":"2024-05-13T21:56:04.81872Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Training","metadata":{}},{"cell_type":"code","source":"model = YOLO('yolov8n-cls.pt')","metadata":{"execution":{"iopub.status.busy":"2024-05-13T21:56:07.942169Z","iopub.execute_input":"2024-05-13T21:56:07.942838Z","iopub.status.idle":"2024-05-13T21:56:11.10924Z","shell.execute_reply.started":"2024-05-13T21:56:07.942803Z","shell.execute_reply":"2024-05-13T21:56:11.108349Z"},"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.status.busy":"2024-05-13T21:56:22.824521Z","iopub.execute_input":"2024-05-13T21:56:22.825032Z","iopub.status.idle":"2024-05-13T21:56:37.209377Z","shell.execute_reply.started":"2024-05-13T21:56:22.825002Z","shell.execute_reply":"2024-05-13T21:56:37.207987Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"results = model.train(data = '/kaggle/input/rapidreceipts-dataset/Grocery Dataset', epochs = 10)","metadata":{"execution":{"iopub.status.busy":"2024-05-13T21:57:29.313198Z","iopub.execute_input":"2024-05-13T21:57:29.313903Z"},"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.49795Z","iopub.status.idle":"2024-04-11T18:33:24.532142Z","shell.execute_reply":"2024-04-11T18:33:24.5311Z","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.56658Z","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.63821Z"},"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":[]}]}