{"metadata":{"kernelspec":{"name":"python3","display_name":"Python 3","language":"python"},"language_info":{"name":"python","version":"3.10.14","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":5048,"databundleVersionId":868335,"sourceType":"competition"}],"dockerImageVersionId":30786,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"id":"d935f64e","cell_type":"markdown","source":"# <a name=\"0\">State Farm Distracted Driver Detection</a>","metadata":{}},{"id":"0b6baeed","cell_type":"markdown","source":"   <a id='top'></a>\n\n<div class=\"list-group\" id=\"list-tab\" role=\"tablist\"></div>\n\n\n\n## <div style=\"background-color:#015958;font-family:sans-serif;color:#FFF9ED;font-size:150%;text-align:center;border-radius:9px 9px; padding: 15px; border-style: solid; border-color: black\">Table of Contents</div>   \n\n    \n\n- Introduction\n\n    \n\n- Import Modules\n\n\n\n- Load Dataset\n\n    \n\n- Exploratory Data Analysis\n\n        \n\n- Split the Dataset\n\n\n\n- Create Generators for the Dataset\n\n\n\n- Plot Samples of Our Dataset\n\n\n\n- Model Structure\n\n\n\n- Fit the model\n\n\n\n- Evaluate the model\n\n\n\n\n\n    \n\n \n","metadata":{}},{"id":"67ed6aa0","cell_type":"markdown","source":"## <div style=\"background-color:#015958;font-family:sans-serif;color:#FFF9ED;font-size:150%;text-align:center;border-radius:9px 9px; padding: 15px; border-style: solid; border-color: black\">Introduction</div>   ","metadata":{}},{"id":"7f5654b2","cell_type":"markdown","source":"# Objective\n\nGiven a dataset of 2D dashboard camera images, an algorithm needs to be developed to classify each driver's behaviour and determine if they are driving attentively, wearing their seatbelt, or taking a selfie with their friends in the backseat etc..? This can then be used to automatically detect drivers engaging in distracted behaviours from dashboard cameras.\n\n\n\n# Dataset\n\nThe provided data set has driver images, each taken in a car with a driver doing something in the car (texting, eating, talking on the phone, makeup, reaching behind, etc). This dataset is obtained from  [Kaggle](https://www.kaggle.com/competitions/state-farm-distracted-driver-detection/overview).\n\n\n\n\n\nThe 10 classes to predict are:\n\n\n\n- c0: safe driving\n\n- c1: texting - right\n\n- c2: talking on the phone - right\n\n- c3: texting - left\n\n- c4: talking on the phone - left\n\n- c5: operating the radio\n\n- c6: drinking\n\n- c7: reaching behind\n\n- c8: hair and makeup\n\n- c9: talking to passenger","metadata":{}},{"id":"52c4ad46","cell_type":"markdown","source":"## File descriptions\n\n\n\n- `imgs` - Folder of all (train/test) images\n\n- `sample_submission.csv` - a sample submission file in the correct format\n\n- `driver_imgs_list.csv` - a list of training images, their subject (driver) id, and class id\n\nIn our case, we are going to use only the images from the `train` folder present inside the `imgs` folder.","metadata":{}},{"id":"bcd90c92","cell_type":"markdown","source":"## The Model\n\nIn our project, we have trained the dataset using:\n\n\n\n- **Yolo v8 Model**","metadata":{}},{"id":"081cea4d","cell_type":"markdown","source":"## Spliting Approach \n\n\n\nWe have used method for creating directories for training set, validation set and test set, with using simpler method by taking the benefit of `splitfolders` module","metadata":{}},{"id":"f6ae5c69","cell_type":"markdown","source":"## <div style=\"background-color:#015958;font-family:sans-serif;color:#FFF9ED;font-size:150%;text-align:center;border-radius:9px 9px; padding: 15px; border-style: solid; border-color: black\">Import Modules</div>   \n\n\n\n### Importing all the libraries that we will be requiring during our project","metadata":{}},{"id":"a272ec14-94bf-4373-96c1-c356271bf607","cell_type":"code","source":"!pip install split-folders","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-01T18:33:22.332888Z","iopub.execute_input":"2024-11-01T18:33:22.333878Z","iopub.status.idle":"2024-11-01T18:33:34.835894Z","shell.execute_reply.started":"2024-11-01T18:33:22.333834Z","shell.execute_reply":"2024-11-01T18:33:34.834802Z"}},"outputs":[],"execution_count":null},{"id":"c17c7be8-d946-4ff7-b0fa-0e737018bd2f","cell_type":"code","source":"!pip install ultralytics","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-01T18:34:34.398835Z","iopub.execute_input":"2024-11-01T18:34:34.399246Z","iopub.status.idle":"2024-11-01T18:34:47.079583Z","shell.execute_reply.started":"2024-11-01T18:34:34.399209Z","shell.execute_reply":"2024-11-01T18:34:47.078458Z"}},"outputs":[],"execution_count":null},{"id":"aa72465b","cell_type":"code","source":"# import system libs\n\nimport os\n\nimport csv\n\nimport time\n\nimport shutil\n\nimport pathlib\n\nimport datetime\n\nimport itertools\n\nfrom glob import glob\n\nfrom tqdm import tqdm\n\nfrom getpass import getpass\n\nfrom shutil import copyfile\n\nfrom random import shuffle, seed\n\n\n\n\n\n# import data handling tools\n\nimport cv2\n\nimport numpy as np\n\nimport pandas as pd\n\nimport seaborn as sns\n\nsns.set_style('darkgrid')\n\nimport matplotlib.pyplot as plt\n\nfrom plotly.subplots import make_subplots\n\nimport plotly.graph_objects as go\n\nfrom plotly.offline import iplot\n\nfrom IPython.display import display, Image\n\nfrom sklearn.metrics import accuracy_score\n\nfrom sklearn.model_selection import train_test_split\n\nfrom sklearn.metrics import confusion_matrix, classification_report\n\n\n\n%matplotlib inline\n\n\n\n# import Deep learning Libraries\n\nimport tensorflow as tf\n\nfrom tensorflow import keras\n\nfrom tensorflow.keras import models\n\nfrom tensorflow.keras import layers\n\nfrom tensorflow.keras import optimizers\n\nfrom tensorflow.keras import regularizers\n\nfrom tensorflow.keras.models import Sequential\n\nfrom tensorflow.keras.optimizers import Adam, Adamax\n\nfrom tensorflow.keras.metrics import categorical_crossentropy\n\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\n\nfrom tensorflow.keras.layers import Conv2D, MaxPooling2D, Flatten, Dense, Activation, Dropout, BatchNormalization\n\nfrom tensorflow.keras.applications.imagenet_utils import preprocess_input\n\nfrom tensorflow.keras.applications.imagenet_utils import decode_predictions\n\nfrom tensorflow.keras.utils import to_categorical\n\nfrom tensorflow.keras.utils import plot_model\n\nfrom tensorflow.keras.models import load_model\n\nfrom keras.callbacks import EarlyStopping, ReduceLROnPlateau\n\nfrom tensorflow.keras.applications.resnet50 import ResNet50\n\nfrom tensorflow.keras.applications.vgg16 import VGG16\n\nfrom ultralytics import YOLO\n\n\n\n\n\n# Ignore Warnings\n\nimport warnings\n\nwarnings.filterwarnings(\"ignore\")\n\n\n\nprint ('Modules Loaded')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-01T18:34:58.163584Z","iopub.execute_input":"2024-11-01T18:34:58.164109Z","iopub.status.idle":"2024-11-01T18:35:18.045042Z","shell.execute_reply.started":"2024-11-01T18:34:58.164066Z","shell.execute_reply":"2024-11-01T18:35:18.043895Z"}},"outputs":[],"execution_count":null},{"id":"8fc6066b","cell_type":"markdown","source":"## <div style=\"background-color:#015958;font-family:sans-serif;color:#FFF9ED;font-size:150%;text-align:center;border-radius:9px 9px; padding: 15px; border-style: solid; border-color: black\"> Load Dataset </div>  \n\n\n\n## Let us load our data and explore it a bit\n\n\n\nAs per the dataset below, we have `7000` images in the `training dataset`. We are gonna split it in three sets (train, val and test), respectively We will ignore `testing dataset` as we don't have any labels for them.","metadata":{}},{"id":"4e09bc35","cell_type":"code","source":"data = {}\n\n\n\nwith open(\"/kaggle/input/state-farm-distracted-driver-detection/driver_imgs_list.csv\") as file:\n\n    read_file = csv.reader(file)\n\n    read_file = list(read_file)\n\n    \n\n    for row in read_file[1:]:\n\n        key = row[1]\n\n        if key in data:\n\n            data[key].append(row[2])\n\n        else:\n\n            data[key] = [row[2]]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-01T18:35:31.705459Z","iopub.execute_input":"2024-11-01T18:35:31.706164Z","iopub.status.idle":"2024-11-01T18:35:31.749483Z","shell.execute_reply.started":"2024-11-01T18:35:31.706121Z","shell.execute_reply":"2024-11-01T18:35:31.748165Z"}},"outputs":[],"execution_count":null},{"id":"1b463909","cell_type":"code","source":"classes_list = list(data.keys())\n\nclasses_list","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-01T18:35:36.94084Z","iopub.execute_input":"2024-11-01T18:35:36.94128Z","iopub.status.idle":"2024-11-01T18:35:36.949743Z","shell.execute_reply.started":"2024-11-01T18:35:36.941222Z","shell.execute_reply":"2024-11-01T18:35:36.948787Z"}},"outputs":[],"execution_count":null},{"id":"62f4ebe1","cell_type":"markdown","source":"#### We have 10 classes in total (`c0`, `c1`, `c2`, `c3`, `c4`, `c5`, `c6`, `c7`, `c8`, `c9`)","metadata":{}},{"id":"8fd1d6d6","cell_type":"markdown","source":"\n\n### In this part we are going to extract an image of each class we have.","metadata":{}},{"id":"5a318dd2","cell_type":"code","source":"# Generate data paths with labels\n\ndata_dir = \"/kaggle/input/state-farm-distracted-driver-detection/imgs\"\n\nfilepaths = []\n\nlabels = []\n\n\n\nfolds = os.listdir(data_dir)\n\nfor fold in folds:\n\n    foldpath = os.path.join(data_dir, fold)\n\n    filelist = os.listdir(foldpath)\n\n    for file in filelist:\n\n        fpath = os.path.join(foldpath, file)\n\n        filepaths.append(fpath)\n\n        labels.append(fold)\n\n\n\n# Concatenate data paths with labels into one dataframe\n\nFseries = pd.Series(filepaths, name= 'filepaths')\n\nLseries = pd.Series(labels, name='labels')\n\ndf = pd.concat([Fseries, Lseries], axis= 1)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-01T18:35:47.336593Z","iopub.execute_input":"2024-11-01T18:35:47.337021Z","iopub.status.idle":"2024-11-01T18:35:48.445595Z","shell.execute_reply.started":"2024-11-01T18:35:47.336977Z","shell.execute_reply":"2024-11-01T18:35:48.444593Z"}},"outputs":[],"execution_count":null},{"id":"26a3fa1e","cell_type":"code","source":"df.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-01T18:35:53.504419Z","iopub.execute_input":"2024-11-01T18:35:53.505528Z","iopub.status.idle":"2024-11-01T18:35:53.526785Z","shell.execute_reply.started":"2024-11-01T18:35:53.50548Z","shell.execute_reply":"2024-11-01T18:35:53.525517Z"}},"outputs":[],"execution_count":null},{"id":"1b207cf0","cell_type":"code","source":"df.tail(10)","metadata":{"scrolled":true,"trusted":true,"execution":{"iopub.status.busy":"2024-11-01T18:35:56.633049Z","iopub.execute_input":"2024-11-01T18:35:56.633472Z","iopub.status.idle":"2024-11-01T18:35:56.644454Z","shell.execute_reply.started":"2024-11-01T18:35:56.633431Z","shell.execute_reply":"2024-11-01T18:35:56.643227Z"}},"outputs":[],"execution_count":null},{"id":"ba751cee","cell_type":"markdown","source":"## <div style=\"background-color:#015958;font-family:sans-serif;color:#FFF9ED;font-size:150%;text-align:center;border-radius:9px 9px; padding: 15px; border-style: solid; border-color: black\"> Exploratory Data Analysis </div>   ","metadata":{}},{"id":"4aef564e","cell_type":"code","source":"df.info()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-01T18:36:02.14528Z","iopub.execute_input":"2024-11-01T18:36:02.14612Z","iopub.status.idle":"2024-11-01T18:36:02.182526Z","shell.execute_reply.started":"2024-11-01T18:36:02.146074Z","shell.execute_reply":"2024-11-01T18:36:02.181326Z"}},"outputs":[],"execution_count":null},{"id":"5284ebc2","cell_type":"code","source":"print(\"Shape of The Dataset:\", df.shape)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-01T18:36:22.803887Z","iopub.execute_input":"2024-11-01T18:36:22.804841Z","iopub.status.idle":"2024-11-01T18:36:22.810462Z","shell.execute_reply.started":"2024-11-01T18:36:22.804788Z","shell.execute_reply":"2024-11-01T18:36:22.809202Z"}},"outputs":[],"execution_count":null},{"id":"2cc1f9d1","cell_type":"code","source":"print(\"Number of Unique Values:\")\n\nprint(df.nunique())","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-01T18:36:33.936465Z","iopub.execute_input":"2024-11-01T18:36:33.936928Z","iopub.status.idle":"2024-11-01T18:36:33.984386Z","shell.execute_reply.started":"2024-11-01T18:36:33.936886Z","shell.execute_reply":"2024-11-01T18:36:33.983202Z"}},"outputs":[],"execution_count":null},{"id":"2196c23c","cell_type":"code","source":"print(\"Number of Null Values:\")\n\nprint(df.isnull().sum())","metadata":{"scrolled":true},"outputs":[],"execution_count":null},{"id":"db1e1e68","cell_type":"markdown","source":"## Now, we will see the distrubution of data in every class.","metadata":{}},{"id":"465f2e6e","cell_type":"code","source":"def summary_with_graph(dataframe, col_name):\n\n    \n\n    #Subplots\n\n    fig = make_subplots(rows = 1, cols = 2,\n\n                        subplot_titles = ('Countplot', 'Percentages'),\n\n                        specs=[[{\"type\": \"xy\"}, {'type': 'domain'}]])\n\n    #Barchart\n\n    fig.add_trace(go.Bar(y = dataframe[col_name].value_counts().values.tolist(),\n\n                         x = [str(i) for i in dataframe[col_name].value_counts().index],\n\n                         text = dataframe[col_name].value_counts().values.tolist(),\n\n                         textfont = dict(size = 15),\n\n                         name = col_name,\n\n                         textposition = 'auto',\n\n                         showlegend = False,\n\n                         marker = dict(color = colors,\n\n                                     line = dict(color = '#DBE6EC',\n\n                                               width = 1))),\n\n                  row = 1, col = 1)\n\n\n\n    #Piechart\n\n    fig.add_trace(go.Pie(labels = dataframe[col_name].value_counts().keys(),\n\n                         values = dataframe[col_name].value_counts().values,\n\n                         textfont = dict(size = 20),\n\n                         textposition = 'auto',\n\n                         showlegend = False,\n\n                         name = col_name,\n\n                         marker = dict(colors = colors)),\n\n                  row = 1, col = 2)\n\n\n\n    fig.update_layout(title = {'text': col_name,\n\n                             'y': 0.9,\n\n                             'x': 0.5,\n\n                             'xanchor': 'center',\n\n                             'yanchor': 'top'},\n\n                      template = 'plotly_white')\n\n\n\n    iplot(fig)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-01T18:36:55.683924Z","iopub.execute_input":"2024-11-01T18:36:55.684367Z","iopub.status.idle":"2024-11-01T18:36:55.694951Z","shell.execute_reply.started":"2024-11-01T18:36:55.684325Z","shell.execute_reply":"2024-11-01T18:36:55.693831Z"}},"outputs":[],"execution_count":null},{"id":"9442d867","cell_type":"code","source":"colors = ['#0FC2C0', '#0CABA8', '#008F8C', '#015958', '#023535',\n\n              '#0FC2C0', '#0CABA8', '#008F8C', '#015958', '#023535']","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-01T18:37:05.104217Z","iopub.execute_input":"2024-11-01T18:37:05.104643Z","iopub.status.idle":"2024-11-01T18:37:05.109353Z","shell.execute_reply.started":"2024-11-01T18:37:05.104607Z","shell.execute_reply":"2024-11-01T18:37:05.108323Z"}},"outputs":[],"execution_count":null},{"id":"2083f113","cell_type":"code","source":"summary_with_graph(df, 'labels')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-01T18:38:04.785992Z","iopub.execute_input":"2024-11-01T18:38:04.786418Z","iopub.status.idle":"2024-11-01T18:38:04.88007Z","shell.execute_reply.started":"2024-11-01T18:38:04.786377Z","shell.execute_reply":"2024-11-01T18:38:04.879105Z"}},"outputs":[],"execution_count":null},{"id":"1d523606","cell_type":"markdown","source":"## <div style=\"background-color:#015958;font-family:sans-serif;color:#FFF9ED;font-size:150%;text-align:center;border-radius:9px 9px; padding: 15px; border-style: solid; border-color: black\"> Split Dataset into Train & Validation & Test</div>   \n\n\n\n\n\n### We will split the data in three parts.\n\n* Training dataset (80%)\n\n* Validation dataset (10%)\n\n* Testing dataset (10%)","metadata":{}},{"id":"dbc91767","cell_type":"code","source":"import splitfolders  # تأكد من استدعاء المكتبة بشكل صحيح\n\n# استخدام الدالة لتقسيم الملفات\nsplitfolders.ratio(\"/kaggle/input/state-farm-distracted-driver-detection/imgs\", output='Output', seed=7, ratio=(0.8, 0.1, 0.1))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-01T18:40:42.944016Z","iopub.execute_input":"2024-11-01T18:40:42.945031Z","iopub.status.idle":"2024-11-01T18:51:55.78663Z","shell.execute_reply.started":"2024-11-01T18:40:42.944984Z","shell.execute_reply":"2024-11-01T18:51:55.784706Z"}},"outputs":[],"execution_count":null},{"id":"c58cd1a7","cell_type":"code","source":"parent_dir = \"/kaggle/working/Output\"\n\ntrain_dir = \"/kaggle/working/Output/train\"\n\nval_dir = \"/kaggle/working/Output/val\"\n\ntest_dir = \"/kaggle/working/Output/test\"","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-01T19:00:02.858751Z","iopub.execute_input":"2024-11-01T19:00:02.860062Z","iopub.status.idle":"2024-11-01T19:00:02.868027Z","shell.execute_reply.started":"2024-11-01T19:00:02.859991Z","shell.execute_reply":"2024-11-01T19:00:02.866736Z"}},"outputs":[],"execution_count":null},{"id":"16c501c1","cell_type":"markdown","source":"## <div style=\"background-color:#015958;font-family:sans-serif;color:#FFF9ED;font-size:150%;text-align:center;border-radius:9px 9px; padding: 15px; border-style: solid; border-color: black\"> Plot Samples of Our Data </div>  \n\n\n\n\n\n## First, we start by Visualizing the Variable of Interest.\n\n\n\n## Let's view more Images in a Grid Format.","metadata":{}},{"id":"96ec9900","cell_type":"code","source":"state = ['safe driving', 'texting - right', 'talking on the phone - right', 'texting - left', 'talking on the phone - left',\n\n         'operating the radio', 'drinking', 'reaching behind', 'hair and makeup', 'talking to passenger', 'UNKNOWN']\n\n\n\ndef Display(path, Class=None):\n\n    img = cv2.imread(path)\n\n    img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)\n\n    if Class == None:\n\n        plt.figure()\n\n        plt.title(state[10])\n\n        plt.imshow(img)\n\n        plt.axis(\"off\")\n\n        # print(img.shape)\n\n    else:\n\n        plt.subplot(2, 5, Class+1)\n\n        plt.title(state[Class])\n\n        plt.imshow(img)\n\n        plt.axis(\"off\")\n\n\n\nplt.figure(figsize=(20, 5))\n\nDisplay(\"/kaggle/input/state-farm-distracted-driver-detection/imgs/train/c0/img_100026.jpg\", 0)\n\nDisplay(\"/kaggle/input/state-farm-distracted-driver-detection/imgs/train/c1/img_100021.jpg\", 1)\n\nDisplay(\"/kaggle/input/state-farm-distracted-driver-detection/imgs/train/c2/img_100029.jpg\", 2)\n\nDisplay(\"/kaggle/input/state-farm-distracted-driver-detection/imgs/train/c3/img_100006.jpg\", 3)\n\nDisplay(\"/kaggle/input/state-farm-distracted-driver-detection/imgs/train/c4/img_100225.jpg\", 4)\n\nDisplay(\"/kaggle/input/state-farm-distracted-driver-detection/imgs/train/c5/img_10000.jpg\", 5)\n\nDisplay(\"/kaggle/input/state-farm-distracted-driver-detection/imgs/train/c6/img_0.jpg\", 6)\n\nDisplay(\"/kaggle/input/state-farm-distracted-driver-detection/imgs/train/c7/img_100057.jpg\", 7)\n\nDisplay(\"/kaggle/input/state-farm-distracted-driver-detection/imgs/train/c8/img_100015.jpg\", 8)\n\nDisplay(\"/kaggle/input/state-farm-distracted-driver-detection/imgs/train/c9/img_100090.jpg\", 9)","metadata":{"scrolled":true,"trusted":true,"execution":{"iopub.status.busy":"2024-11-01T19:03:40.489525Z","iopub.execute_input":"2024-11-01T19:03:40.490031Z","iopub.status.idle":"2024-11-01T19:03:43.804087Z","shell.execute_reply.started":"2024-11-01T19:03:40.489987Z","shell.execute_reply":"2024-11-01T19:03:43.802483Z"}},"outputs":[],"execution_count":null},{"id":"09028549","cell_type":"markdown","source":"## <div style=\"background-color:#015958;font-family:sans-serif;color:#FFF9ED;font-size:150%;text-align:center;border-radius:9px 9px; padding: 15px; border-style: solid; border-color: black\"> Image Data Generator & Flow from Directory</div>   ","metadata":{}},{"id":"55f2b8a3","cell_type":"markdown","source":"## Creating Function using Tensorflow's `ImageDataGenerator` Module for our image preprocessing\n\n\n\n`imagedatageneration` function will be created that will return three generated batches of tensor image-data namely:\n\n- train_generator (generated from our train folder)\n\n- val_generator (generated from our val folder)\n\n- test_generator (generated from our test folder)","metadata":{}},{"id":"e13ce9a4","cell_type":"code","source":"def imagedatageneration(train_dir, val_dir, test_dir, target_size = (256, 256), batch_size = 64):\n\n    \n\n    \n\n    ## It can be seen that the augmentation is applied only on the training set.\n\n    ## We have skipped it for val because it is not recommended. But we can try and experiment with it later\n\n    \n\n    train_datagen = ImageDataGenerator(rescale = 1.0 / 255,\n\n                                       rotation_range = 30,\n\n                                       width_shift_range = 0.1,\n\n                                       height_shift_range = 0.1,\n\n                                       zoom_range = 0.1,\n\n                                       shear_range = 0.1,\n\n                                       fill_mode = \"nearest\"\n\n                                      )\n\n    train_generator = train_datagen.flow_from_directory(\n\n                                                            train_dir,\n\n                                                            target_size = target_size,\n\n                                                            class_mode = 'categorical',\n\n                                                            shuffle = True,\n\n                                                            batch_size = batch_size\n\n                                                        )\n\n    \n\n    \n\n    val_datagen = ImageDataGenerator(rescale = 1.0 / 255)\n\n    \n\n    val_generator = val_datagen.flow_from_directory(\n\n                                                        val_dir,\n\n                                                        target_size = target_size,\n\n                                                        class_mode = 'categorical',\n\n                                                        shuffle = True,\n\n                                                        batch_size = batch_size\n\n                                                    )\n\n    \n\n    test_datagen = ImageDataGenerator(rescale = 1.0/255)\n\n    test_generator = test_datagen.flow_from_directory(\n\n                                                        test_dir,\n\n                                                        target_size = target_size,\n\n                                                        class_mode = 'categorical',\n\n                                                        shuffle = False,\n\n                                                        batch_size = 1\n\n                                                      )\n\n    \n\n    return train_generator, val_generator, test_generator","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-01T19:04:12.396002Z","iopub.execute_input":"2024-11-01T19:04:12.397201Z","iopub.status.idle":"2024-11-01T19:04:12.406069Z","shell.execute_reply.started":"2024-11-01T19:04:12.397148Z","shell.execute_reply":"2024-11-01T19:04:12.405024Z"}},"outputs":[],"execution_count":null},{"id":"931cde7c","cell_type":"code","source":"train_generator, val_generator, test_generator = imagedatageneration(train_dir, val_dir, test_dir)","metadata":{"scrolled":true,"trusted":true,"execution":{"iopub.status.busy":"2024-11-01T19:04:17.14049Z","iopub.execute_input":"2024-11-01T19:04:17.140928Z","iopub.status.idle":"2024-11-01T19:04:18.619005Z","shell.execute_reply.started":"2024-11-01T19:04:17.140885Z","shell.execute_reply":"2024-11-01T19:04:18.617948Z"}},"outputs":[],"execution_count":null},{"id":"d42da6b6","cell_type":"markdown","source":"## <div style=\"background-color:#015958;font-family:sans-serif;color:#FFF9ED;font-size:150%;text-align:center;border-radius:9px 9px; padding: 15px; border-style: solid; border-color: black\"> Deep Learning Model </div> \n\n\n\n### Creating the Object of Out Yolo Model","metadata":{}},{"id":"72be74a6","cell_type":"code","source":"model = YOLO('yolov8n-cls.pt')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-01T19:04:38.22124Z","iopub.execute_input":"2024-11-01T19:04:38.222026Z","iopub.status.idle":"2024-11-01T19:04:41.263333Z","shell.execute_reply.started":"2024-11-01T19:04:38.221982Z","shell.execute_reply":"2024-11-01T19:04:41.262199Z"}},"outputs":[],"execution_count":null},{"id":"aee01a82","cell_type":"code","source":"results = model.train(data = parent_dir, epochs = 15)","metadata":{"scrolled":true,"trusted":true,"execution":{"iopub.status.busy":"2024-11-01T19:05:20.982533Z","iopub.execute_input":"2024-11-01T19:05:20.982986Z"}},"outputs":[],"execution_count":null},{"id":"27c93465","cell_type":"code","source":"model.val()","metadata":{"scrolled":true},"outputs":[],"execution_count":null},{"id":"a9c169a1","cell_type":"code","source":"df = pd.read_csv(r\"D:\\Personal\\5. Courses\\Depi\\Technical\\Runs\\Classify\\Train\\results.csv\")\n\ndf.head()","metadata":{"scrolled":true},"outputs":[],"execution_count":null},{"id":"631cadc7","cell_type":"markdown","source":"## <div style=\"background-color:#015958;font-family:sans-serif;color:#FFF9ED;font-size:150%;text-align:center;border-radius:9px 9px; padding: 15px; border-style: solid; border-color: black\"> Plot Acc and Loss Curves </div> ","metadata":{}},{"id":"bcac00ef","cell_type":"code","source":"Image(r\"D:\\Personal\\5. Courses\\Depi\\Technical\\Runs\\Classify\\Train\\results.png\")","metadata":{},"outputs":[],"execution_count":null},{"id":"c1a4edec","cell_type":"markdown","source":"## <div style=\"background-color:#015958;font-family:sans-serif;color:#FFF9ED;font-size:150%;text-align:center;border-radius:9px 9px; padding: 15px; border-style: solid; border-color: black\"> Plot Confussion Matrix </div> ","metadata":{}},{"id":"d2eb531e","cell_type":"code","source":"Image(r\"D:\\Personal\\5. Courses\\Depi\\Technical\\Runs\\Classify\\Train\\confusion_matrix_normalized.png\")","metadata":{},"outputs":[],"execution_count":null},{"id":"72c15247","cell_type":"markdown","source":"## <div style=\"background-color:#015958;font-family:sans-serif;color:#FFF9ED;font-size:150%;text-align:center;border-radius:9px 9px; padding: 15px; border-style: solid; border-color: black\"> Prediction </div> ","metadata":{}},{"id":"9b169977","cell_type":"code","source":"classes = ['c0', 'c1', 'c2', 'c3', 'c4', 'c5', 'c6', 'c7', 'c8', 'c9']\n\ntest_images_path = test_dir\n\nmodel_weights = r\"D:\\Personal\\5. Courses\\Depi\\Technical\\Runs\\Classify\\Train\\weights\\best.pt\"\n\n\n\npredicted_list = []\n\n\n\nfor clas in classes:\n\n    image_dir = os.path.join(test_images_path, clas)\n\n    # print(image_dir)\n\n    images_list = os.listdir(image_dir)\n\n    # print(images_list)\n\n    # Class label in the form of 0 to 9\n\n    class_label = int(clas[-1])\n\n    # print(class_label)\n\n    for image in images_list:\n\n        path = os.path.join(image_dir, image)\n\n        # print(path)\n\n        y_actual = class_label\n\n        y_predicted = model.predict(path, model = model_weights)[0].probs.top1\n\n        predicted_list.append([path, y_actual, y_predicted])","metadata":{},"outputs":[],"execution_count":null},{"id":"2de95d19","cell_type":"code","source":"print(\"length of the Predicted List : \", len(predicted_list))","metadata":{},"outputs":[],"execution_count":null},{"id":"7c604e1d","cell_type":"code","source":"df = pd.DataFrame(predicted_list, columns = ['Image_path', 'Y_actual', 'Y_predicted'])\n\ndf.head()","metadata":{},"outputs":[],"execution_count":null},{"id":"a1a11a43","cell_type":"code","source":"print(\"Accuracy based on our VOLO V8 Model :- {:.2f}%\".format(accuracy_score(df['Y_actual'], df['Y_predicted'])*100))","metadata":{},"outputs":[],"execution_count":null},{"id":"05fea57d","cell_type":"markdown","source":"## <div style=\"background-color:#015958;font-family:sans-serif;color:#FFF9ED;font-size:150%;text-align:center;border-radius:9px 9px; padding: 15px; border-style: solid; border-color: black\"> Plot Confussion Matrix </div> ","metadata":{}},{"id":"97c62d0d","cell_type":"code","source":"import seaborn as sns\n\nimport matplotlib.pyplot as plt\n\nfrom sklearn.metrics import confusion_matrix\n\n\n\n# Define your categories based on the labels you have\n\ncategories = {\n\n    0: 'normal driving',\n\n    1: 'texting - right',\n\n    2: 'talking on the phone - right',\n\n    3: 'texting - left',\n\n    4: 'talking on the phone - left',\n\n    5: 'operating the radio',\n\n    6: 'drinking',\n\n    7: 'reaching behind',\n\n    8: 'hair and makeup',\n\n    9: 'talking to passenger'\n\n}\n\n\n\n# Generate the confusion matrix\n\nconfusion_mtx = confusion_matrix(df[\"Y_actual\"], df[\"Y_predicted\"]) \n\n\n\n# Plotting the confusion matrix\n\nf, ax = plt.subplots(figsize=(20, 8), dpi=200)\n\n\n\nsns.heatmap(confusion_mtx, annot=True, \n\n            linewidths=0.1, cmap=\"Blues\", \n\n            linecolor=\"black\", fmt='d', ax=ax, \n\n            cbar=False, xticklabels=categories.values(), \n\n            yticklabels=categories.values())\n\n\n\nplt.xlabel(\"Predicted Label\", fontdict={'color': 'black', 'size': 15})\n\nplt.ylabel(\"Actual Label\", fontdict={'color': 'black', 'size': 15})\n\nplt.title(\"Confusion Matrix\", fontdict={'color': 'black', 'size': 25})\n\n\n\nplt.show()\n","metadata":{},"outputs":[],"execution_count":null},{"id":"5295b38e","cell_type":"markdown","source":"\n\n## <div style=\"background-color:#015958;font-family:sans-serif;color:#FFF9ED;font-size:150%;text-align:center;border-radius:9px 9px; padding: 15px; border-style: solid; border-color: black\"> Predict Images </div> \n","metadata":{}},{"id":"b7f10a81","cell_type":"code","source":"import matplotlib.pyplot as plt\n\nimport matplotlib.image as mpimg\n\n\n\npredicted_list = [\n\n    [r\"D:\\Personal\\5. Courses\\Depi\\Technical\\Dataset\\Test\\c0\\img_17794.jpg\", \"Normal Driving\", \"Normal Driving\"],\n\n    [r\"D:\\Personal\\5. Courses\\Depi\\Technical\\Dataset\\Test\\c1\\img_25412.jpg\", \"Texting - Right\", \"Texting - Right\"],\n\n    [r\"D:\\Personal\\5. Courses\\Depi\\Technical\\Dataset\\Test\\c2\\img_12723.jpg\", \"Talking on the Phone - Right\", \"Talking on the Phone - Right\"],\n\n    [r\"D:\\Personal\\5. Courses\\Depi\\Technical\\Dataset\\Test\\c3\\img_27703.jpg\", \"Texting - Left\", \"Texting - Left\"],\n\n    [r\"D:\\Personal\\5. Courses\\Depi\\Technical\\Dataset\\Test\\c4\\img_29229.jpg\", \"Talking on the Phone - Left\", \"Talking on the Phone - Left\"],\n\n    [r\"D:\\Personal\\5. Courses\\Depi\\Technical\\Dataset\\Test\\c5\\img_17571.jpg\", \"Operating the Radio\", \"Operating the Radio\"],\n\n    [r\"D:\\Personal\\5. Courses\\Depi\\Technical\\Dataset\\Test\\c6\\img_11811.jpg\", \"Drinking\", \"Drinking\"],\n\n    [r\"D:\\Personal\\5. Courses\\Depi\\Technical\\Dataset\\Test\\c7\\img_30938.jpg\", \"Reaching Behind\", \"Reaching Behind\"],\n\n    [r\"D:\\Personal\\5. Courses\\Depi\\Technical\\Dataset\\Test\\c8\\img_13090.jpg\", \"Hair and Makeup\", \"Hair and Makeup\"],\n\n    [r\"D:\\Personal\\5. Courses\\Depi\\Technical\\Dataset\\Test\\c9\\img_13396.jpg\", \"Normal Driving\", \"Talking to Passenger\"]\n\n]\n\n\n\n# Set up the number of images to display\n\nnum_images = len(predicted_list)\n\nrows = (num_images // 5) + (num_images % 5 > 0)  # Calculate rows needed for a 5-column layout\n\ncols = 5  # Number of columns\n\n\n\n# Create a figure\n\nfig, ax = plt.subplots(rows, cols, figsize=(20, rows * 4))  # Adjust height per row\n\nax = ax.flatten()  # Flatten the axes array for easy indexing\n\n\n\n# Loop through predicted_list and plot each image\n\nfor i, (img_path, actual, predicted) in enumerate(predicted_list):\n\n    img = mpimg.imread(img_path)  # Read the image from the path\n\n    ax[i].imshow(img)  # Display the image\n\n    ax[i].set_title(f\"Actual: {actual}\\nPredicted: {predicted}\", fontsize=15)  # Set title with actual and predicted labels\n\n    ax[i].axis('off')  # Hide the axes\n\n\n\n# Hide any unused subplots\n\nfor j in range(i + 1, rows * cols):\n\n    ax[j].axis('off')\n\n\n\nplt.suptitle(\"Image Predictions vs Actual Labels\", fontsize=20)\n\nplt.tight_layout()\n\nplt.show()\n","metadata":{},"outputs":[],"execution_count":null}]}