{"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":30746,"isInternetEnabled":false,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# State Farm Distracted Driver Detection\n","metadata":{}},{"cell_type":"markdown","source":"## Objective\n\n\n\nIn this Project, we are given 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). Our goal is to predict the likelihood of what the driver is doing in each picture.\n\nThe 10 classes to predict are:\n\n- c0: safe driving\n- c1: texting - right\n- c2: talking on the phone - right\n- c3: texting - left\n- c4: talking on the phone - left\n- c5: operating the radio\n- c6: drinking\n- c7: reaching behind\n- c8: hair and makeup\n- c9: talking to passenger","metadata":{}},{"cell_type":"markdown","source":"## File descriptions\n\n- `imgs` - Folder of all (train/test) images\n- `sample_submission.csv` - a sample submission file in the correct format\n- `driver_imgs_list.csv` - a list of training images, their subject (driver) id, and class id\nIn our case, we are going to use only the images from the `train` folder present inside the `imgs` folder.","metadata":{}},{"cell_type":"markdown","source":"## Outline of the models used:\nIn our project, we have trained the dataset using the following models and compared their performance:-\n\n- **Simple Dense Model**\n- **CNN Model**\n- **VGG16 Model**\n- **RestNet50 Model**\n- **Yolo v8 Model**\n\n\nEach Model is trained till `15Epochs`. Moreover, comparison between the behaviour of Train and Validation Accuracies and Losses on each epoch for each of our Model has been made.","metadata":{}},{"cell_type":"markdown","source":"## Spliting Approach \n\nApart from that, we have used two method for creating directories for training set, validation set and test set. One, with our standard `os` module and other using simpler method by taking the benefit of `splitfolders` module","metadata":{}},{"cell_type":"markdown","source":"*Below lines were used to install `ultralytics`, `split-folders` Module. We will be using them for YoLo Model and for spliting our datasets respectively*","metadata":{}},{"cell_type":"code","source":"!pip install ultralytics\n!pip install split-folders\n# !pip install -U ipywidgets\n# !pip install tensorflow-cpu","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-30T08:09:52.173198Z","iopub.execute_input":"2025-04-30T08:09:52.173568Z"}},"outputs":[{"name":"stdout","text":"\u001b[33mWARNING: Retrying (Retry(total=4, connect=None, read=None, redirect=None, status=None)) after connection broken by 'NewConnectionError('<pip._vendor.urllib3.connection.HTTPSConnection object at 0x7c48a394fcd0>: Failed to establish a new connection: [Errno -3] Temporary failure in name resolution')': /simple/ultralytics/\u001b[0m\u001b[33m\n\u001b[0m\u001b[33mWARNING: Retrying (Retry(total=3, connect=None, read=None, redirect=None, status=None)) after connection broken by 'NewConnectionError('<pip._vendor.urllib3.connection.HTTPSConnection object at 0x7c48a394ffd0>: Failed to establish a new connection: [Errno -3] Temporary failure in name resolution')': /simple/ultralytics/\u001b[0m\u001b[33m\n\u001b[0m\u001b[33mWARNING: Retrying (Retry(total=2, connect=None, read=None, redirect=None, status=None)) after connection broken by 'NewConnectionError('<pip._vendor.urllib3.connection.HTTPSConnection object at 0x7c48a397c2b0>: Failed to establish a new connection: [Errno -3] Temporary failure in name resolution')': /simple/ultralytics/\u001b[0m\u001b[33m\n\u001b[0m\u001b[33mWARNING: Retrying (Retry(total=1, connect=None, read=None, redirect=None, status=None)) after connection broken by 'NewConnectionError('<pip._vendor.urllib3.connection.HTTPSConnection object at 0x7c48a397c460>: Failed to establish a new connection: [Errno -3] Temporary failure in name resolution')': /simple/ultralytics/\u001b[0m\u001b[33m\n\u001b[0m\u001b[33mWARNING: Retrying (Retry(total=0, connect=None, read=None, redirect=None, status=None)) after connection broken by 'NewConnectionError('<pip._vendor.urllib3.connection.HTTPSConnection object at 0x7c48a397c610>: Failed to establish a new connection: [Errno -3] Temporary failure in name resolution')': /simple/ultralytics/\u001b[0m\u001b[33m\n\u001b[0m\u001b[31mERROR: Could not find a version that satisfies the requirement ultralytics (from versions: none)\u001b[0m\u001b[31m\n\u001b[0m\u001b[31mERROR: No matching distribution found for ultralytics\u001b[0m\u001b[31m\n\u001b[0m","output_type":"stream"}],"execution_count":null},{"cell_type":"markdown","source":"#### *Importing all the libraries that we will be requiring during our project*","metadata":{}},{"cell_type":"code","source":"import csv\nimport os\nfrom glob import glob\nfrom shutil import copyfile\nfrom random import shuffle, seed\nimport splitfolders\nimport tensorflow as tf\nfrom tensorflow.keras import Model\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom tensorflow.keras.optimizers import Adam\nfrom tensorflow.keras.callbacks import EarlyStopping\nfrom tensorflow.keras.layers import Conv2D, MaxPooling2D, Dense, Flatten, BatchNormalization, Dropout\nfrom tensorflow.keras.applications.resnet50 import ResNet50\nfrom tensorflow.keras.applications.vgg16 import VGG16\nfrom ultralytics import YOLO\nimport pandas as pd\nimport numpy as np\nfrom IPython.display import display, Image\nfrom sklearn.metrics import accuracy_score\nimport matplotlib.pyplot as plt\n%matplotlib inline","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-09T16:45:55.165751Z","iopub.execute_input":"2025-04-09T16:45:55.16653Z","iopub.status.idle":"2025-04-09T16:46:04.090902Z","shell.execute_reply.started":"2025-04-09T16:45:55.166494Z","shell.execute_reply":"2025-04-09T16:46:04.089973Z"}},"outputs":[{"name":"stdout","text":"Creating new Ultralytics Settings v0.0.6 file ✅ \nView Ultralytics Settings with 'yolo settings' or at '/root/.config/Ultralytics/settings.json'\nUpdate Settings with 'yolo settings key=value', i.e. 'yolo settings runs_dir=path/to/dir'. For help see https://docs.ultralytics.com/quickstart/#ultralytics-settings.\n","output_type":"stream"}],"execution_count":2},{"cell_type":"markdown","source":"## Let us load our data and explore it a bit\n","metadata":{}},{"cell_type":"markdown","source":"As per the dataset below, we have `22424` 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":{}},{"cell_type":"code","source":"data = {}\n\nwith open('/kaggle/input/state-farm-distracted-driver-detection/driver_imgs_list.csv') as file:\n    read_file = csv.reader(file)\n    read_file = list(read_file)\n    \n    for row in read_file[1:]:\n        key = row[1]\n        if key in data:\n            data[key].append(row[2])\n        else:\n            data[key] = [row[2]]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-09T16:46:27.010189Z","iopub.execute_input":"2025-04-09T16:46:27.010799Z","iopub.status.idle":"2025-04-09T16:46:27.044082Z","shell.execute_reply.started":"2025-04-09T16:46:27.010771Z","shell.execute_reply":"2025-04-09T16:46:27.043446Z"}},"outputs":[],"execution_count":3},{"cell_type":"code","source":"data['c0'][:5]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-09T16:46:41.148889Z","iopub.execute_input":"2025-04-09T16:46:41.149628Z","iopub.status.idle":"2025-04-09T16:46:41.15576Z","shell.execute_reply.started":"2025-04-09T16:46:41.149598Z","shell.execute_reply":"2025-04-09T16:46:41.154884Z"}},"outputs":[{"execution_count":4,"output_type":"execute_result","data":{"text/plain":"['img_44733.jpg',\n 'img_72999.jpg',\n 'img_25094.jpg',\n 'img_69092.jpg',\n 'img_92629.jpg']"},"metadata":{}}],"execution_count":4},{"cell_type":"markdown","source":"We have 10 classes in total (`c0`, `c1`, `c2`, `c3`, `c4`, `c5`, `c6`, `c7`, `c8`, `c9`)","metadata":{}},{"cell_type":"code","source":"classes_list = list(data.keys())\nclasses_list","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-09T16:46:47.224895Z","iopub.execute_input":"2025-04-09T16:46:47.225693Z","iopub.status.idle":"2025-04-09T16:46:47.231215Z","shell.execute_reply.started":"2025-04-09T16:46:47.225663Z","shell.execute_reply":"2025-04-09T16:46:47.2303Z"}},"outputs":[{"execution_count":5,"output_type":"execute_result","data":{"text/plain":"['c0', 'c1', 'c2', 'c3', 'c4', 'c5', 'c6', 'c7', 'c8', 'c9']"},"metadata":{}}],"execution_count":5},{"cell_type":"code","source":"dataset_folder = '/kaggle/input/state-farm-distracted-driver-detection/imgs/'\n\ntrain_dir = os.path.join(dataset_folder, 'train/')\ntest_dir = os.path.join(dataset_folder, 'test/')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-09T16:46:50.367634Z","iopub.execute_input":"2025-04-09T16:46:50.368274Z","iopub.status.idle":"2025-04-09T16:46:50.372522Z","shell.execute_reply.started":"2025-04-09T16:46:50.368242Z","shell.execute_reply":"2025-04-09T16:46:50.371584Z"}},"outputs":[],"execution_count":6},{"cell_type":"code","source":"print('Number of images in the training dataset : ', str(len(glob(train_dir+'*/*'))))\nprint('Number of images in the testing dataset : ', str(len(glob(test_dir+'*'))))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-09T16:46:54.910335Z","iopub.execute_input":"2025-04-09T16:46:54.911256Z","iopub.status.idle":"2025-04-09T16:46:56.452556Z","shell.execute_reply.started":"2025-04-09T16:46:54.911207Z","shell.execute_reply":"2025-04-09T16:46:56.451709Z"}},"outputs":[{"name":"stdout","text":"Number of images in the training dataset :  22424\nNumber of images in the testing dataset :  79726\n","output_type":"stream"}],"execution_count":7},{"cell_type":"markdown","source":"## Reducing our dataset\n\nFor the sake of simplicity and to reduce the overall time taken to train all our model,we will reduce our dataset by 1/5th.\n\n#### *Note: In case you are testing only one or two model. I would recommend to use whole dataset and use more layers in your models*","metadata":{}},{"cell_type":"code","source":"dataset_small_folder_path = '/kaggle/working/state-farm-distracted-driver-detection/smallset'\nsubfolders = classes_list\n\nif os.path.exists(dataset_small_folder_path):\n    for root, dirs, files in os.walk(dataset_small_folder_path, topdown = False):\n        for name in files:\n            file_path = os.path.join(root, name)\n            os.remove(file_path)\n        for name in dirs:\n            dir_path = os.path.join(root, name)\n            os.rmdir(dir_path)\n    os.rmdir(path)\n    \nfor folder in subfolders:\n            subfolder_path = os.path.join(dataset_small_folder_path, folder)\n            os.makedirs(subfolder_path)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-09T16:47:00.515111Z","iopub.execute_input":"2025-04-09T16:47:00.515882Z","iopub.status.idle":"2025-04-09T16:47:00.522598Z","shell.execute_reply.started":"2025-04-09T16:47:00.51585Z","shell.execute_reply":"2025-04-09T16:47:00.521807Z"}},"outputs":[],"execution_count":8},{"cell_type":"code","source":"for clas, images in data.items():\n    length = len(images)\n    seed(42)\n    shuffle(images)\n    for image in images[:int(length*0.2)]:\n        source = os.path.join(dataset_folder, 'train/', clas, image)\n        #print(source)                     \n        destination = os.path.join(dataset_small_folder_path, clas, image)\n        copyfile(source, destination)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-09T16:47:09.418706Z","iopub.execute_input":"2025-04-09T16:47:09.419282Z","iopub.status.idle":"2025-04-09T16:47:46.229938Z","shell.execute_reply.started":"2025-04-09T16:47:09.419253Z","shell.execute_reply":"2025-04-09T16:47:46.228919Z"}},"outputs":[],"execution_count":9},{"cell_type":"code","source":"print(dataset_small_folder_path)\nfor subfolder in subfolders:\n    subfolder_path = os.path.join(dataset_small_folder_path, subfolder)\n    print(\"Number of images for each class: \", subfolder, \"->\", len(os.listdir(subfolder_path)))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-09T16:48:15.203207Z","iopub.execute_input":"2025-04-09T16:48:15.203995Z","iopub.status.idle":"2025-04-09T16:48:15.212665Z","shell.execute_reply.started":"2025-04-09T16:48:15.203967Z","shell.execute_reply":"2025-04-09T16:48:15.21178Z"}},"outputs":[{"name":"stdout","text":"/kaggle/working/state-farm-distracted-driver-detection/smallset\nNumber of images for each class:  c0 -> 497\nNumber of images for each class:  c1 -> 453\nNumber of images for each class:  c2 -> 463\nNumber of images for each class:  c3 -> 469\nNumber of images for each class:  c4 -> 465\nNumber of images for each class:  c5 -> 462\nNumber of images for each class:  c6 -> 465\nNumber of images for each class:  c7 -> 400\nNumber of images for each class:  c8 -> 382\nNumber of images for each class:  c9 -> 425\n","output_type":"stream"}],"execution_count":11},{"cell_type":"code","source":"small_dataset = {}\nfor subfolder in os.listdir(dataset_small_folder_path):\n    small_dataset[subfolder] = os.listdir(os.path.join(dataset_small_folder_path, subfolder))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-09T16:48:19.653183Z","iopub.execute_input":"2025-04-09T16:48:19.653885Z","iopub.status.idle":"2025-04-09T16:48:19.661732Z","shell.execute_reply.started":"2025-04-09T16:48:19.653854Z","shell.execute_reply":"2025-04-09T16:48:19.660801Z"}},"outputs":[],"execution_count":12},{"cell_type":"markdown","source":"## Spliting Approach1 - For creating directories for training set, validation set and test set, using `os` and `shutil` modules","metadata":{}},{"cell_type":"markdown","source":"#### Writing helper function for creating directories for training set, validation set and test set\n- `remove_directory` :- Removes the folder, subfolder and files within it\n- `create_directories` :- Creates the folder and classes subfolders in each folder in the following order\n    - `Folder`:- train or val or test\n    - `Subfolders`:- All labeled classes *(('c0', 'c1', 'c2', 'c3', 'c4', 'c5', 'c6', 'c7', 'c8', 'c9'))*\n    \n**Note for the shake of simplicity and for reducing the total run time, we will reduce our dataset by the margin of ten.**","metadata":{}},{"cell_type":"code","source":"def remove_directory(path):\n    for root, dirs, files in os.walk(path, topdown = False):\n        for name in files:\n            file_path = os.path.join(root, name)\n            os.remove(file_path)\n        for name in dirs:\n            dir_path = os.path.join(root, name)\n            os.rmdir(dir_path)\n    os.rmdir(path)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-09T16:48:25.554643Z","iopub.execute_input":"2025-04-09T16:48:25.55518Z","iopub.status.idle":"2025-04-09T16:48:25.560066Z","shell.execute_reply.started":"2025-04-09T16:48:25.55515Z","shell.execute_reply":"2025-04-09T16:48:25.559218Z"}},"outputs":[],"execution_count":13},{"cell_type":"code","source":"def create_directories(paths, subfolders):\n    for path in paths:\n        if os.path.exists(path):\n            remove_directory(path)\n        \n        for folder in subfolders:\n            subfolder_path = os.path.join(path, folder)\n            os.makedirs(subfolder_path)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-09T16:48:31.214232Z","iopub.execute_input":"2025-04-09T16:48:31.21491Z","iopub.status.idle":"2025-04-09T16:48:31.219208Z","shell.execute_reply.started":"2025-04-09T16:48:31.214882Z","shell.execute_reply":"2025-04-09T16:48:31.218024Z"}},"outputs":[],"execution_count":14},{"cell_type":"markdown","source":"#### We will store the path for the cleaned dataset containing train, val and test datasets in `paths` list and classes in subfolder list ","metadata":{}},{"cell_type":"code","source":"paths = ['/kaggle/working/state-farm-distracted-driver-detection/cleaned_dataset/train',\n         '/kaggle/working/state-farm-distracted-driver-detection/cleaned_dataset/val',\n        '/kaggle/working/state-farm-distracted-driver-detection/cleaned_dataset/test']","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-09T16:48:36.028864Z","iopub.execute_input":"2025-04-09T16:48:36.029234Z","iopub.status.idle":"2025-04-09T16:48:36.033563Z","shell.execute_reply.started":"2025-04-09T16:48:36.029204Z","shell.execute_reply":"2025-04-09T16:48:36.032616Z"}},"outputs":[],"execution_count":15},{"cell_type":"code","source":"subfolders = classes_list","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-09T16:48:43.830088Z","iopub.execute_input":"2025-04-09T16:48:43.830968Z","iopub.status.idle":"2025-04-09T16:48:43.834526Z","shell.execute_reply.started":"2025-04-09T16:48:43.830925Z","shell.execute_reply":"2025-04-09T16:48:43.833773Z"}},"outputs":[],"execution_count":16},{"cell_type":"markdown","source":"### Creating Train, Val, Test folders along with sub-directories (all Classes)","metadata":{}},{"cell_type":"code","source":"create_directories(paths, subfolders)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-09T16:48:49.714394Z","iopub.execute_input":"2025-04-09T16:48:49.715045Z","iopub.status.idle":"2025-04-09T16:48:49.72066Z","shell.execute_reply.started":"2025-04-09T16:48:49.715013Z","shell.execute_reply":"2025-04-09T16:48:49.719694Z"}},"outputs":[],"execution_count":17},{"cell_type":"markdown","source":"### Copying the image files to the cleaned dataset we have created using the above helper functions\n\nOur dataset is splited in the following order using the `copyfile` submodule from the `shutil` module:\n- Train -> 80% \n- Test, Val -> 10% each","metadata":{}},{"cell_type":"code","source":"split_size = [0.8, 0.1]\n\n\nfor clas, images in small_dataset.items():\n    # print(len(images))\n    train_size = int(split_size[0]*len(images))\n    # print(\"Train size: \", train_size)\n    \n    test_size = int(split_size[1]*len(images))\n    #print(\"Test size: \", test_size)\n    \n    train_images = images[:train_size]\n    # print(\"Train Images Length\", len(train_images))\n    \n    val_images = images[train_size: train_size + test_size]\n    # print(\"Val Images Length\", len(val_images))\n    \n    test_images = images[train_size + test_size:]\n    # print(\"Test Images Length\", len(test_images))\n    \n    \n    \n    for image in train_images:\n        source = os.path.join(train_dir, clas, image)\n        # print(os.path.exists(source))\n        dest = os.path.join(paths[0], clas, image)\n        copyfile(source, dest)\n    \n    for image in val_images:\n        source = os.path.join(train_dir, clas, image)\n        dest = os.path.join(paths[1], clas, image)\n        copyfile(source, dest)\n    \n    for image in test_images:\n        source = os.path.join(train_dir, clas, image)\n        dest = os.path.join(paths[2], clas, image)\n        copyfile(source, dest)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-09T16:48:54.624937Z","iopub.execute_input":"2025-04-09T16:48:54.625251Z","iopub.status.idle":"2025-04-09T16:48:59.720252Z","shell.execute_reply.started":"2025-04-09T16:48:54.625225Z","shell.execute_reply":"2025-04-09T16:48:59.719426Z"}},"outputs":[],"execution_count":18},{"cell_type":"markdown","source":"## Spliting Approach2 -  For creating directories for training set, validation set and test set, using `splitfolders` module","metadata":{}},{"cell_type":"markdown","source":"We will use a better approach for creating the cleaned dataset using splitfolders module.\n\nLet us first delete the cleaned dataset created using the above function `create_directories()` and `copyfile` submodule","metadata":{}},{"cell_type":"code","source":"remove_directory('/kaggle/working/state-farm-distracted-driver-detection/cleaned_dataset')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-09T16:49:18.554282Z","iopub.execute_input":"2025-04-09T16:49:18.555067Z","iopub.status.idle":"2025-04-09T16:49:18.699636Z","shell.execute_reply.started":"2025-04-09T16:49:18.555022Z","shell.execute_reply":"2025-04-09T16:49:18.699088Z"}},"outputs":[],"execution_count":19},{"cell_type":"markdown","source":"### Creating the cleaned dataset now using splitfolder module.\n\nThe original dataset is splited in the following order and test, val, train subdirectories have been created automatically :\n- Train -> 90%\n- Test, Val -> 10% each","metadata":{}},{"cell_type":"code","source":"images_dir = '/kaggle/working/state-farm-distracted-driver-detection/smallset'\noutput_folder = '/kaggle/working/state-farm-distracted-driver-detection/cleaned_dataset' \n\nsplit_ratio = (0.8, 0.1, 0.1)\n\n# Note: the module will create val, train, test sub directories by itself in the background.\n# No need for us to create those.\nsplitfolders.ratio(images_dir, output= output_folder, seed = 42, ratio= split_ratio)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-09T16:49:23.879631Z","iopub.execute_input":"2025-04-09T16:49:23.880237Z","iopub.status.idle":"2025-04-09T16:49:24.620445Z","shell.execute_reply.started":"2025-04-09T16:49:23.880209Z","shell.execute_reply":"2025-04-09T16:49:24.619518Z"}},"outputs":[{"name":"stderr","text":"Copying files: 4481 files [00:00, 6125.51 files/s]\n","output_type":"stream"}],"execution_count":20},{"cell_type":"markdown","source":"#### Done ! Just needed one line of code.","metadata":{}},{"cell_type":"markdown","source":"#### Let us keep all our directory paths together at one place.\nFrom now on, these Directory paths will be used for our training, validation and testing purpose","metadata":{}},{"cell_type":"code","source":"parent_dir = '/kaggle/working/state-farm-distracted-driver-detection/cleaned_dataset'\ntrain_dir = '/kaggle/working/state-farm-distracted-driver-detection/cleaned_dataset/train'\nval_dir = '/kaggle/working/state-farm-distracted-driver-detection/cleaned_dataset/val'\ntest_dir = '/kaggle/working/state-farm-distracted-driver-detection/cleaned_dataset/test'","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-09T16:50:09.957755Z","iopub.execute_input":"2025-04-09T16:50:09.958499Z","iopub.status.idle":"2025-04-09T16:50:09.962688Z","shell.execute_reply.started":"2025-04-09T16:50:09.958458Z","shell.execute_reply":"2025-04-09T16:50:09.961727Z"}},"outputs":[],"execution_count":21},{"cell_type":"markdown","source":"## Creating Function using Tensorflow's `ImageDataGenerator` Module for our image preprocessing\n\n`imagedatageneration` function will be created that will return three generated batches of tensor image-data namely:-\n- train_generator (generated from our train folder)\n- val_generator (generated from our val folder)\n- test_generator (generated from our test folder)\n\n*Note: We will leverage the Data Augmentation facility of ImageDataGenerator module in training our model*","metadata":{}},{"cell_type":"code","source":"def imagedatageneration(train_dir, val_dir, test_dir, target_size = (256, 256), batch_size = 64):\n    \n    \n    ## It can be seen that the augmentation is applied only on the training set.\n    ## We have skipped it for val because it is not recommended. But we can try and experiment with it later\n    \n    train_datagen = ImageDataGenerator(rescale = 1.0 / 255,\n                                       rotation_range = 30,\n                                       width_shift_range = 0.1,\n                                       height_shift_range = 0.1,\n                                       zoom_range = 0.1,\n                                       shear_range = 0.1,\n                                       fill_mode = \"nearest\"\n                                      )\n    train_generator = train_datagen.flow_from_directory(\n                                                            train_dir,\n                                                            target_size = target_size,\n                                                            class_mode = 'categorical',\n                                                            shuffle = True,\n                                                            batch_size = batch_size\n                                                        )\n    \n    \n    val_datagen = ImageDataGenerator(rescale = 1.0 / 255)\n    \n    val_generator = val_datagen.flow_from_directory(\n                                                        val_dir,\n                                                        target_size = target_size,\n                                                        class_mode = 'categorical',\n                                                        shuffle = True,\n                                                        batch_size = batch_size\n                                                    )\n    \n    test_datagen = ImageDataGenerator(rescale = 1.0/255)\n    test_generator = test_datagen.flow_from_directory(\n                                                        test_dir,\n                                                        target_size = target_size,\n                                                        class_mode = 'categorical',\n                                                        shuffle = False,\n                                                        batch_size = 1\n                                                      )\n    \n    return train_generator, val_generator, test_generator","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-09T16:50:16.475835Z","iopub.execute_input":"2025-04-09T16:50:16.476356Z","iopub.status.idle":"2025-04-09T16:50:16.486167Z","shell.execute_reply.started":"2025-04-09T16:50:16.476309Z","shell.execute_reply":"2025-04-09T16:50:16.485554Z"}},"outputs":[],"execution_count":22},{"cell_type":"markdown","source":"#### *`es` object is created using the EarlyStopping Class which will stop training when a monitored metric has stopped improving. We are monitoring validation accuracy in this case*","metadata":{}},{"cell_type":"code","source":"es = EarlyStopping(monitor = \"val_acc\",\n                    min_delta = 0.0001,\n                    verbose=1,\n                    patience = 5,\n                    restore_best_weights = True,\n                    baseline = None)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-09T16:50:25.070955Z","iopub.execute_input":"2025-04-09T16:50:25.071593Z","iopub.status.idle":"2025-04-09T16:50:25.075452Z","shell.execute_reply.started":"2025-04-09T16:50:25.071562Z","shell.execute_reply":"2025-04-09T16:50:25.074698Z"}},"outputs":[],"execution_count":23},{"cell_type":"markdown","source":"#### `train_val_plot` function will compare the behaviour of Train and Validation Accuracies and Losses on each epoch for each of our Model","metadata":{}},{"cell_type":"code","source":"def train_val_plot(model, model_name):\n    train_loss, train_acc, val_loss, val_acc = model.history['loss'], model.history['acc'], model.history['val_loss'], model.history['val_acc']\n    \n    plt.plot(train_acc)\n    plt.plot(val_acc)\n    plt.title('{} Model Accuracy'.format(model_name))\n    plt.ylabel('Accuracy')\n    plt.xlabel('Epoch')\n    plt.legend(['Train', 'Validation'], loc = 'upper left')\n    plt.show()\n    \n    \n    plt.plot(train_loss)\n    plt.plot(val_loss)\n    plt.title('{} Model Loss'.format(model_name))\n    plt.ylabel('Loss')\n    plt.xlabel('Epoch')\n    plt.legend(['Train', 'Validation'], loc = 'upper left')\n    plt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-09T16:50:40.551772Z","iopub.execute_input":"2025-04-09T16:50:40.552426Z","iopub.status.idle":"2025-04-09T16:50:40.557949Z","shell.execute_reply.started":"2025-04-09T16:50:40.55238Z","shell.execute_reply":"2025-04-09T16:50:40.557028Z"}},"outputs":[],"execution_count":24},{"cell_type":"markdown","source":"## First Model -> Dense Model","metadata":{}},{"cell_type":"markdown","source":"Loading our dataset using `imagedatageneration` function which will return three tuples with generated batches of tensor image-data namely:-\n\n- `train_generator` (generated from our train folder)\n- `val_generator` (generated from our val folder)\n- `test_generator` (generated from our test folder)","metadata":{}},{"cell_type":"code","source":"train_generator, val_generator, test_generator = imagedatageneration(train_dir, val_dir, test_dir)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-09T16:50:44.735146Z","iopub.execute_input":"2025-04-09T16:50:44.735865Z","iopub.status.idle":"2025-04-09T16:50:44.844036Z","shell.execute_reply.started":"2025-04-09T16:50:44.735835Z","shell.execute_reply":"2025-04-09T16:50:44.843452Z"}},"outputs":[{"name":"stdout","text":"Found 3582 images belonging to 10 classes.\nFound 444 images belonging to 10 classes.\nFound 455 images belonging to 10 classes.\n","output_type":"stream"}],"execution_count":25},{"cell_type":"markdown","source":"### Our `Model1` consist of three Dense Hidden Layers.\n\n*Note: This is a very basic model and we can experimnent with adding layers and finetuning our model later*","metadata":{}},{"cell_type":"code","source":"model1 = tf.keras.models.Sequential([\n    Flatten(input_shape = (256, 256, 3)),\n    Dense(16, activation = 'relu'),\n    Dense(32, activation = 'relu'),\n    BatchNormalization(),\n    Dense(64, activation = 'relu'),\n    Dense(128, activation = 'relu'),\n    BatchNormalization(),\n    Dense(256, activation = 'relu'),\n    Dense(512, activation = 'relu'),\n    BatchNormalization(),\n    Dense(1024, activation = 'relu'),\n    Dense(10, activation = 'softmax')\n])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-09T16:50:53.01076Z","iopub.execute_input":"2025-04-09T16:50:53.011538Z","iopub.status.idle":"2025-04-09T16:50:53.214492Z","shell.execute_reply.started":"2025-04-09T16:50:53.011508Z","shell.execute_reply":"2025-04-09T16:50:53.213587Z"}},"outputs":[],"execution_count":26},{"cell_type":"markdown","source":"The Model is compiled using `Adam Optimizer` with the `learning rate` of `0.001`. As it is a multi-label classification problem, we have used `categorical_crossentropy` as our loss function","metadata":{}},{"cell_type":"code","source":"model1.compile(optimizer = Adam(learning_rate = 0.001), loss = 'categorical_crossentropy', metrics = ['acc'])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-09T16:50:57.334862Z","iopub.execute_input":"2025-04-09T16:50:57.335526Z","iopub.status.idle":"2025-04-09T16:50:57.353558Z","shell.execute_reply.started":"2025-04-09T16:50:57.335494Z","shell.execute_reply":"2025-04-09T16:50:57.352648Z"}},"outputs":[],"execution_count":27},{"cell_type":"code","source":"model1.summary()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-09T16:51:01.704773Z","iopub.execute_input":"2025-04-09T16:51:01.705486Z","iopub.status.idle":"2025-04-09T16:51:01.739206Z","shell.execute_reply.started":"2025-04-09T16:51:01.705454Z","shell.execute_reply":"2025-04-09T16:51:01.738414Z"}},"outputs":[{"name":"stdout","text":"Model: \"sequential\"\n_________________________________________________________________\n Layer (type)                Output Shape              Param #   \n=================================================================\n flatten (Flatten)           (None, 196608)            0         \n                                                                 \n dense (Dense)               (None, 16)                3145744   \n                                                                 \n dense_1 (Dense)             (None, 32)                544       \n                                                                 \n batch_normalization (Batch  (None, 32)                128       \n Normalization)                                                  \n                                                                 \n dense_2 (Dense)             (None, 64)                2112      \n                                                                 \n dense_3 (Dense)             (None, 128)               8320      \n                                                                 \n batch_normalization_1 (Bat  (None, 128)               512       \n chNormalization)                                                \n                                                                 \n dense_4 (Dense)             (None, 256)               33024     \n                                                                 \n dense_5 (Dense)             (None, 512)               131584    \n                                                                 \n batch_normalization_2 (Bat  (None, 512)               2048      \n chNormalization)                                                \n                                                                 \n dense_6 (Dense)             (None, 1024)              525312    \n                                                                 \n dense_7 (Dense)             (None, 10)                10250     \n                                                                 \n=================================================================\nTotal params: 3859578 (14.72 MB)\nTrainable params: 3858234 (14.72 MB)\nNon-trainable params: 1344 (5.25 KB)\n_________________________________________________________________\n","output_type":"stream"}],"execution_count":28},{"cell_type":"markdown","source":"The model will run for `15 epochs`. And it will stop before it if the metrics stopped improving as per the EarlyStopping object we have defined previously.","metadata":{}},{"cell_type":"code","source":"model1_history = model1.fit(train_generator,\n                    epochs = 15,\n                    verbose = 1,\n                    validation_data = val_generator,\n                    callbacks = [es])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-09T16:51:29.784927Z","iopub.execute_input":"2025-04-09T16:51:29.785737Z"}},"outputs":[{"name":"stdout","text":"Epoch 1/15\n56/56 [==============================] - 60s 1s/step - loss: 2.7020 - acc: 0.1170 - val_loss: 2.4001 - val_acc: 0.1036\nEpoch 2/15\n56/56 [==============================] - 58s 1s/step - loss: 2.5266 - acc: 0.1298 - val_loss: 2.4971 - val_acc: 0.0946\nEpoch 3/15\n56/56 [==============================] - 57s 1s/step - loss: 2.4669 - acc: 0.1401 - val_loss: 2.3542 - val_acc: 0.1059\nEpoch 4/15\n56/56 [==============================] - 57s 1s/step - loss: 2.3937 - acc: 0.1519 - val_loss: 2.5541 - val_acc: 0.1036\nEpoch 5/15\n56/56 [==============================] - 57s 1s/step - loss: 2.3546 - acc: 0.1549 - val_loss: 2.3676 - val_acc: 0.1014\nEpoch 6/15\n56/56 [==============================] - 58s 1s/step - loss: 2.3047 - acc: 0.1630 - val_loss: 2.3465 - val_acc: 0.1036\nEpoch 7/15\n56/56 [==============================] - 57s 1s/step - loss: 2.2584 - acc: 0.1753 - val_loss: 2.2856 - val_acc: 0.1396\nEpoch 8/15\n56/56 [==============================] - 58s 1s/step - loss: 2.2425 - acc: 0.1709 - val_loss: 5.4987 - val_acc: 0.0901\nEpoch 9/15\n56/56 [==============================] - ETA: 0s - loss: 2.2012 - acc: 0.1857","output_type":"stream"}],"execution_count":null},{"cell_type":"code","source":"model1_accuracy = model1.evaluate(test_generator)\n\nprint(\"Accuracy based on simple Dense Model :- {:.2f}%\".format(model1_accuracy[1]*100))","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model1_history.history.keys()","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"Comparison of Train and Validation Accuracies and Losses on each epoch for our Model","metadata":{}},{"cell_type":"code","source":"model1_history","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_val_plot(model1_history, 'Simple Dense')","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Second Model -> CNN Model","metadata":{}},{"cell_type":"markdown","source":"Loading our dataset using `imagedatageneration` function which will return three tuples with generated batches of tensor image-data namely:-\n\n- `train_generator` (generated from our train folder)\n- `val_generator` (generated from our val folder)\n- `test_generator` (generated from our test folder)","metadata":{}},{"cell_type":"code","source":"train_generator, val_generator, test_generator = imagedatageneration(train_dir, val_dir, test_dir)\n","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"Our `Model2` consist of `6 Conv2D Layers`, with `5 Dense Layers` at the end.\nWe have applied `Batch normalization` after each Conv2D layer to keep a transformation that maintains the mean output close to 0 and the output standard deviation close to 1.","metadata":{}},{"cell_type":"code","source":"model2 = tf.keras.models.Sequential([\n    Conv2D(16, (3, 3), activation = 'relu', input_shape = (256, 256, 3)),\n    MaxPooling2D(2, 2),\n    Conv2D(32, (3, 3), activation = 'relu'),\n    MaxPooling2D(2, 2),\n    Conv2D(64, (3, 3), activation = 'relu'),\n    MaxPooling2D(2, 2),\n    Flatten(),\n    Dense(512, activation = 'relu'),\n    Dense(1024, activation = 'relu'),\n    BatchNormalization(),\n    Dense(10, activation = 'softmax')\n])","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"The Model is compiled using `Adam Optimizer` with the `learning rate of 0.001`. As it is a multi-label classification problem, we have used `categorical_crossentropy` as our loss function","metadata":{}},{"cell_type":"code","source":"model2.compile(optimizer = Adam(learning_rate = 0.001), loss = 'categorical_crossentropy', metrics = ['acc'])\nmodel2.summary()","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"The model will run for `15 epochs`. And it will stop before it if the metrics stopped improving as per the `EarlyStopping` object we have defined previously.","metadata":{}},{"cell_type":"code","source":"model2_history = model2.fit(train_generator,\n            epochs = 15,\n            verbose = 1,\n            validation_data = val_generator,\n            callbacks = [es])","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"accuracy = model2.evaluate(test_generator)\n\nprint(\"Accuracy based on our CNN Model :- {:.2f}%\".format(accuracy[1]*100))","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"Comparison of Train and Validation Accuracies and Losses on each epoch for our Model","metadata":{}},{"cell_type":"code","source":"train_val_plot(model2_history, 'CNN')","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Third Model -> VGG16","metadata":{}},{"cell_type":"markdown","source":"Loading our dataset using `imagedatageneration` function which will return three tuples with generated batches of tensor image-data namely:-\n\n- `train_generator` (generated from our train folder)\n- `val_generator` (generated from our val folder)\n- `test_generator` (generated from our test folder)","metadata":{}},{"cell_type":"code","source":"train_generator, val_generator, test_generator = imagedatageneration(train_dir, val_dir, test_dir)\n","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"Our `Model3` consist of pretrained model of VGG16 with 5 Dense Layers at the end.\n\nWe have applied `Batch normalization` after Dense layer to keep a transformation that maintains the mean output close to 0 and the output standard deviation close to 1. We have set all the layers apart from last 5 layers of our VGG16 models as `Non-trainable` during the training.","metadata":{}},{"cell_type":"code","source":"pretrained_model = VGG16(weights = 'imagenet', include_top = False, input_shape = (256, 256, 3))\npretrained_model.summary()","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"for layer in pretrained_model.layers[:-5]:\n    layer.trainable = False","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"last_layer = pretrained_model.get_layer('block4_pool')\nlast_output = last_layer.output\n\nx = Flatten()(last_output)\nx = Dropout(0.2)(x)\nx = Dense(128, activation = 'relu')(x)\nx = BatchNormalization()(x)\nx = Dense(256, activation = 'relu')(x)\nx = BatchNormalization()(x)\nx = Dense(10, activation = 'softmax')(x)\n\n\nmodel3 = Model(pretrained_model.input, x)","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"The Model is compiled using `Adam Optimizer` with the `learning rate of 0.001`. As it is a multi-label classification problem, we have used `categorical_crossentropy` as our loss function","metadata":{}},{"cell_type":"code","source":"model3.compile(optimizer = Adam(learning_rate = 0.001), loss = 'categorical_crossentropy', metrics = ['acc'])\nmodel3.summary()","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"The model will run for `15 epochs`. And it will stop before it if the metrics stopped improving as per the `EarlyStopping` object we have defined previously.","metadata":{}},{"cell_type":"code","source":"model3_history =  model3.fit(train_generator,\n            epochs = 15,\n            verbose = 1,\n            validation_data = val_generator,\n            callbacks = [es])","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"accuracy = model3.evaluate(test_generator)\n\nprint(\"Accuracy based on our VGG16 Model :- {:.2f}%\".format(accuracy[1]*100))","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"Comparison of Train and Validation Accuracies and Losses on each epoch for our Model","metadata":{}},{"cell_type":"code","source":"train_val_plot(model3_history, 'VGG16')\n","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Fourth Model -> ResNet50","metadata":{}},{"cell_type":"markdown","source":"Loading our dataset using `imagedatageneration` function which will return three tuples with generated batches of tensor image-data namely:-\n\n- `train_generator` (generated from our train folder)\n- `val_generator` (generated from our val folder)\n- `test_generator` (generated from our test folder)","metadata":{}},{"cell_type":"code","source":"train_generator, val_generator, test_generator = imagedatageneration(train_dir, val_dir, test_dir)\n","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"Our `Model4` consist of pretrained model of ResNet50 with 5 Dense Layers at the end.\n\nWe have applied `Batch normalization` after Dense layer to keep a transformation that maintains the mean output close to 0 and the output standard deviation close to 1. We have set all the layers apart from last 3 layers of our Resnet50 models as `Non-trainable` during the training.","metadata":{}},{"cell_type":"code","source":"pretrained_model = ResNet50(weights = 'imagenet', include_top = False, input_shape = (256, 256, 3))\npretrained_model.summary()","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"for layer in pretrained_model.layers[:-3]:\n    layer.trainable = False\n","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"last_layer = pretrained_model.get_layer('conv5_block2_out')\nlast_output = last_layer.output\n\nx = Flatten()(last_output)\nx = Dropout(0.2)(x)\nx = Dense(128, activation = 'relu')(x)\nx = BatchNormalization()(x)\nx = Dense(256, activation = 'relu')(x)\nx = BatchNormalization()(x)\nx = Dense(10, activation = 'softmax')(x)\n\n\nmodel4 = Model(pretrained_model.input, x)","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"The Model is compiled using `Adam Optimizer` with the `learning rate of 0.001`. As it is a multi-label classification problem, we have used `categorical_crossentropy` as our loss function","metadata":{}},{"cell_type":"code","source":"model4.compile(optimizer = Adam(learning_rate = 0.001), loss = 'categorical_crossentropy', metrics = ['acc'])\nmodel4.summary()","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"The model will run for `15 epochs`. ","metadata":{}},{"cell_type":"code","source":"model4_history = model4.fit(train_generator,\n            epochs = 15,\n            verbose = 1,\n            validation_data = val_generator)","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"accuracy = model4.evaluate(test_generator)\n\nprint(\"Accuracy based on our ResNet50 Model :- {:.2f}%\".format(accuracy[1]*100))","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_val_plot(model4_history, 'ResNet50')","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Fifth Model -> Yolo v8","metadata":{}},{"cell_type":"markdown","source":"### Creating the object of out Yolo model","metadata":{}},{"cell_type":"code","source":"model5 = YOLO('yolov8n-cls.pt')","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"#### Below we have trained our `YOLO` model with `20 epochs` on the training dataset we have created","metadata":{}},{"cell_type":"code","source":"# Import wandb and log in\nimport wandb\nfrom kaggle_secrets import UserSecretsClient\nuser_secrets = UserSecretsClient()\nmy_secret = user_secrets.get_secret(\"wandb_api_key\") \n\n\n\n# Log in to wandb with API key\nwandb.login(key=my_secret)","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"results = model5.train(data = parent_dir, epochs = 15)","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"#### Validating our model performance on the validation dataset","metadata":{}},{"cell_type":"code","source":"model5.val()","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df= pd.read_csv('/kaggle/working/runs/classify/train/results.csv')\ndf.head()","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"#### Comparison of Train and Validation Losses for our Model","metadata":{}},{"cell_type":"code","source":"Image(\"/kaggle/working/runs/classify/train/results.png\")","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"Image(\"/kaggle/working/runs/classify/train/confusion_matrix_normalized.png\")","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"### Predicting our model performance on the test dataset we have created.\n\nBelow we compare the test images one by one and compare the result with their original class.\n\nOverall accuracy came out `99.44%` on our test dataset.","metadata":{}},{"cell_type":"code","source":"classes = ['c0', 'c1', 'c2', 'c3', 'c4', 'c5', 'c6', 'c7', 'c8', 'c9']\ntest_images_path = test_dir\nmodel_weights = \"/kaggle/working/runs/classify/train/weights/best.pt\"\n\npredicted_list = []\n\nfor clas in classes:\n    image_dir = os.path.join(test_images_path, clas)\n    # print(image_dir)\n    images_list = os.listdir(image_dir)\n    # print(images_list)\n    # Class label in the form of 0 to 9\n    class_label = int(clas[-1])\n    # print(class_label)\n    for image in images_list:\n        path = os.path.join(image_dir, image)\n        # print(path)\n        y_actual = class_label\n        y_predicted = model5.predict(path, model = model_weights)[0].probs.top1\n        predicted_list.append([path, y_actual, y_predicted])","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(\"length of the Predicted List : \", len(predicted_list))","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df = pd.DataFrame(predicted_list, columns = ['Image_path', 'Y_actual', 'Y_predicted'])","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(\"Accuracy based on our VOLO V8 Model :- {:.2f}%\".format(accuracy_score(df['Y_actual'], df['Y_predicted'])*100))","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Conclusion","metadata":{}},{"cell_type":"markdown","source":"In our project, we have used two method for creating directories for training set, validation set and test set. One, with our standard `os` module and other using simpler method by taking the benefit of `splitfolders` module.\n\nWe have trained the dataset using the below models. Each Model is trained till 20Epochs. Moreover, comparison between the behaviour of Train and Validation Accuracies and Losses on each epoch for each of our Model has been made. (For the Previous Running version)\n\n- **Simple Dense Model** - Test Acc. (**42.42%%**)\n- **CNN Model** - Test Acc. (**82.20%**)\n- **VGG16 Model** - Test Acc. (**97.58%**)\n- **RestNet50 Model** - Test Acc. (**77.80%**)\n- **Yolo v8 Model** - Test Acc. (**98.46%**)","metadata":{}},{"cell_type":"markdown","source":"### Test Accuracy Perfomance Order\n\nModel1 (Simple Dense) -> Model3 (ResNet50) -> Model2 (CNN) -> Model3 (VGG16) -> Model5 (YOLOv8)\n\n**Note**: This is just the start of the analysis. These models can be improved further with the addition/removal of some of the layers, fine tuning hyperparameters, changing the size of the splited datasets, epochs, data augmentation and so on.","metadata":{}},{"cell_type":"markdown","source":"## References\n\n1. [State Farm Distracted Driver Detection](https://www.kaggle.com/c/state-farm-distracted-driver-detection/data)\n2. [System for Distraction Detection and Monitoring](https://www.kaggle.com/code/azzaali/system-for-distraction-detection-and-monitoring/notebook)\n3. [Distracted Driver Detection-project](https://www.kaggle.com/code/mob2dr/distracted-driver-detection-project)\n4. [Train Yolo v8 on Custom Dataset](https://www.kaggle.com/code/karakoza22/train-yolo-v8-on-custom-dataset/notebook)\n5. [VGG16 200 X 280](https://www.kaggle.com/code/agrawalmayank/vgg16-200-x-280)\n6. [Ultralytics YOLO Docs](https://docs.ultralytics.com/)\n7. [Weights & Biases Tutorial (beginner)](https://www.kaggle.com/code/samuelcortinhas/weights-biases-tutorial-beginner)","metadata":{}},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}