{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\n# import os\n# for dirname, _, filenames in os.walk('/kaggle/input'):\n#     for filename in filenames:\n#         print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-03-18T06:55:04.497500Z","iopub.execute_input":"2023-03-18T06:55:04.497913Z","iopub.status.idle":"2023-03-18T06:55:04.530765Z","shell.execute_reply.started":"2023-03-18T06:55:04.497883Z","shell.execute_reply":"2023-03-18T06:55:04.529805Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport cv2\nimport os\nfrom tqdm import tqdm\nfrom glob import glob\nfrom keras.utils import np_utils\nfrom sklearn.model_selection import train_test_split\nimport matplotlib.pyplot as plt\nimport matplotlib.image as mpimg\nfrom tensorflow.keras import layers\nfrom tensorflow.keras import models\nfrom keras.preprocessing.image import ImageDataGenerator\nfrom keras.models import Sequential, Model\nfrom keras.layers import Conv2D, MaxPooling2D, Flatten, Dense, Dropout, BatchNormalization, GlobalAveragePooling2D","metadata":{"execution":{"iopub.status.busy":"2023-03-18T06:55:04.532880Z","iopub.execute_input":"2023-03-18T06:55:04.533631Z","iopub.status.idle":"2023-03-18T06:55:14.523406Z","shell.execute_reply.started":"2023-03-18T06:55:04.533590Z","shell.execute_reply":"2023-03-18T06:55:14.522232Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.read_csv('../input/state-farm-distracted-driver-detection/driver_imgs_list.csv')\ndf.head(5)","metadata":{"execution":{"iopub.status.busy":"2023-03-18T06:55:14.525494Z","iopub.execute_input":"2023-03-18T06:55:14.526370Z","iopub.status.idle":"2023-03-18T06:55:14.574457Z","shell.execute_reply.started":"2023-03-18T06:55:14.526329Z","shell.execute_reply":"2023-03-18T06:55:14.573049Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Groupby subjects\nby_drivers = df.groupby('subject') \n#Group unique drivers\nunique_drivers = by_drivers.groups.keys() # drivers id\nprint('There are : ',len(unique_drivers), ' unique drivers')\nprint('There is a mean of ',round(df.groupby('subject').count()['classname'].mean()), ' images by driver.')","metadata":{"execution":{"iopub.status.busy":"2023-03-18T06:55:14.576743Z","iopub.execute_input":"2023-03-18T06:55:14.577014Z","iopub.status.idle":"2023-03-18T06:55:14.602728Z","shell.execute_reply.started":"2023-03-18T06:55:14.576988Z","shell.execute_reply":"2023-03-18T06:55:14.601598Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install split-folders","metadata":{"execution":{"iopub.status.busy":"2023-03-18T06:55:14.604248Z","iopub.execute_input":"2023-03-18T06:55:14.605194Z","iopub.status.idle":"2023-03-18T06:55:26.008704Z","shell.execute_reply.started":"2023-03-18T06:55:14.605151Z","shell.execute_reply":"2023-03-18T06:55:26.007487Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import splitfolders\ndata_dir = '/kaggle/input/state-farm-distracted-driver-detection/imgs/train'\nsplitfolders.ratio(data_dir, output=\"output\",\n    seed=1337, ratio=(.8, .2), group_prefix=None, move=False)","metadata":{"execution":{"iopub.status.busy":"2023-03-18T06:55:26.011855Z","iopub.execute_input":"2023-03-18T06:55:26.012683Z","iopub.status.idle":"2023-03-18T06:58:07.847981Z","shell.execute_reply.started":"2023-03-18T06:55:26.012633Z","shell.execute_reply":"2023-03-18T06:58:07.846751Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.preprocessing.image import ImageDataGenerator\n\n\n\n# Set the batch size and image size for the data generator\nbatch_size = 32\nimg_height = 224\nimg_width = 224\n\n# Create an instance of the ImageDataGenerator class with no augmentation\ndatagen_train = ImageDataGenerator(rescale=1./255)\n\n# Create a separate instance of the ImageDataGenerator class for the validation data\ndatagen_val = ImageDataGenerator(rescale=1./255)\n\n# Create separate generators for the training and validation sets\ntrain_generator = datagen_train.flow_from_directory(\n    '/kaggle/working/output/train',\n    target_size=(img_height, img_width),\n    batch_size=batch_size,\n    class_mode='categorical',\n    subset='training'\n)\n\nval_generator = datagen_val.flow_from_directory(\n    '/kaggle/working/output/val',\n    target_size=(img_height, img_width),\n    batch_size=batch_size,\n    class_mode='categorical',\n  \n)","metadata":{"execution":{"iopub.status.busy":"2023-03-18T06:58:07.849560Z","iopub.execute_input":"2023-03-18T06:58:07.850012Z","iopub.status.idle":"2023-03-18T06:58:08.413123Z","shell.execute_reply.started":"2023-03-18T06:58:07.849972Z","shell.execute_reply":"2023-03-18T06:58:08.412134Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"activity_map = {'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'}\n\n\nplt.figure(figsize = (12, 20))\nimage_count = 1\nBASE_URL = '../input/state-farm-distracted-driver-detection/imgs/train/'\nfor directory in os.listdir(BASE_URL):\n    if directory[0] != '.':\n        for i, file in enumerate(os.listdir(BASE_URL + directory)):\n            if i == 1:\n                break\n            else:\n                fig = plt.subplot(5, 2, image_count)\n                image_count += 1\n                image = mpimg.imread(BASE_URL + directory + '/' + file)\n                plt.imshow(image)\n                plt.title(activity_map[directory])","metadata":{"execution":{"iopub.status.busy":"2023-03-18T06:58:08.414728Z","iopub.execute_input":"2023-03-18T06:58:08.415122Z","iopub.status.idle":"2023-03-18T06:58:10.863709Z","shell.execute_reply.started":"2023-03-18T06:58:08.415067Z","shell.execute_reply":"2023-03-18T06:58:10.862251Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def plot_train_history(history):\n    \"\"\"\n    Plot the validation accuracy and validation loss over epochs\n    \"\"\"\n    # Summarize history for accuracy\n    plt.plot(history.history['accuracy'])\n    plt.plot(history.history['val_accuracy'])\n    plt.title('Model accuracy')\n    plt.ylabel('accuracy')\n    plt.xlabel('epoch')\n    plt.legend(['train', 'test'], loc='upper left')\n    plt.show()\n\n    # Summarize history for loss\n    plt.plot(history.history['loss'])\n    plt.plot(history.history['val_loss'])\n    plt.title('Model loss')\n    plt.ylabel('loss')\n    plt.xlabel('epoch')\n    plt.legend(['train', 'test'], loc='upper left')\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2023-03-18T06:58:10.868421Z","iopub.execute_input":"2023-03-18T06:58:10.875292Z","iopub.status.idle":"2023-03-18T06:58:10.884032Z","shell.execute_reply.started":"2023-03-18T06:58:10.875257Z","shell.execute_reply":"2023-03-18T06:58:10.882671Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"##  fully connected layers with the following architecture: \n256-128","metadata":{}},{"cell_type":"code","source":"model = models.Sequential([\n    layers.Flatten(input_shape=(224, 224, 3)),\n    layers.Dense(256, activation='relu'),\n    layers.Dense(128, activation='relu'),\n    layers.Dense(10, activation='softmax')\n])\nloss = 'categorical_crossentropy'\noptimizer = 'adam'\nmetrices = ['accuracy']\n\nmodel.compile(optimizer, loss, metrices)\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2023-03-18T06:58:10.885867Z","iopub.execute_input":"2023-03-18T06:58:10.886708Z","iopub.status.idle":"2023-03-18T06:58:14.577999Z","shell.execute_reply.started":"2023-03-18T06:58:10.886654Z","shell.execute_reply":"2023-03-18T06:58:14.577173Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"n_epochs = 10\nbatch_size = 32\nhistory1 = model.fit(train_generator,\n                   validation_data=val_generator,\n                   epochs=n_epochs, batch_size=batch_size, verbose=1)","metadata":{"execution":{"iopub.status.busy":"2023-03-18T06:58:14.579219Z","iopub.execute_input":"2023-03-18T06:58:14.579684Z","iopub.status.idle":"2023-03-18T07:18:43.271947Z","shell.execute_reply.started":"2023-03-18T06:58:14.579653Z","shell.execute_reply":"2023-03-18T07:18:43.270789Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_train_history(history1)","metadata":{"execution":{"iopub.status.busy":"2023-03-18T07:18:43.273871Z","iopub.execute_input":"2023-03-18T07:18:43.274329Z","iopub.status.idle":"2023-03-18T07:18:43.713116Z","shell.execute_reply.started":"2023-03-18T07:18:43.274287Z","shell.execute_reply":"2023-03-18T07:18:43.711987Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Conv2D","metadata":{}},{"cell_type":"code","source":"model = models.Sequential([\n    layers.Conv2D(32, (3,3), activation='relu', input_shape=(224,224, 3)),\n    layers.MaxPooling2D((2,2)),\n    layers.Conv2D(64, (3,3), activation='relu'),\n    layers.MaxPooling2D((2,2)),\n    layers.Conv2D(64, (3,3), activation='relu'),\n    layers.Flatten(),\n    layers.Dense(256, activation='relu'),\n    layers.Dense(128, activation='relu'),\n    layers.Dense(64, activation='relu'),\n    layers.Dense(10, activation='softmax'),\n])","metadata":{"execution":{"iopub.status.busy":"2023-03-18T07:18:43.714624Z","iopub.execute_input":"2023-03-18T07:18:43.717475Z","iopub.status.idle":"2023-03-18T07:18:43.830997Z","shell.execute_reply.started":"2023-03-18T07:18:43.717444Z","shell.execute_reply":"2023-03-18T07:18:43.829790Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"loss = 'categorical_crossentropy'\noptimizer = 'adam'\nmetrices = ['accuracy']\n\nmodel.compile(optimizer, loss, metrices)\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2023-03-18T07:18:43.832707Z","iopub.execute_input":"2023-03-18T07:18:43.833069Z","iopub.status.idle":"2023-03-18T07:18:43.871057Z","shell.execute_reply.started":"2023-03-18T07:18:43.833034Z","shell.execute_reply":"2023-03-18T07:18:43.870290Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"n_epochs = 10\nbatch_size = 32\nhistory = model.fit(train_generator,\n                   validation_data=val_generator,\n                   epochs=n_epochs, batch_size=batch_size, verbose=1)","metadata":{"execution":{"iopub.status.busy":"2023-03-18T07:18:43.872116Z","iopub.execute_input":"2023-03-18T07:18:43.872483Z","iopub.status.idle":"2023-03-18T07:40:38.542716Z","shell.execute_reply.started":"2023-03-18T07:18:43.872444Z","shell.execute_reply":"2023-03-18T07:40:38.541677Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_train_history(history)","metadata":{"execution":{"iopub.status.busy":"2023-03-18T07:40:38.544399Z","iopub.execute_input":"2023-03-18T07:40:38.544786Z","iopub.status.idle":"2023-03-18T07:40:38.980607Z","shell.execute_reply.started":"2023-03-18T07:40:38.544743Z","shell.execute_reply":"2023-03-18T07:40:38.979225Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Data Augmentation","metadata":{}},{"cell_type":"code","source":"# Create an instance of the ImageDataGenerator class\ndata_dir = '/kaggle/input/state-farm-distracted-driver-detection/imgs/train'\ndatagen_train = ImageDataGenerator(   \n    \n    rescale=1./255, # rescale pixel values between 0 and 1\n    shear_range=0.2, # apply random shear transformations\n    zoom_range=0.2, # apply random zoom transformations\n    horizontal_flip=True )\n\n# Create a separate instance of the ImageDataGenerator class for the validation data\ndatagen_val = ImageDataGenerator(rescale=1./255)\n\n# Create separate generators for the training and validation sets\ntrain_generator = datagen_train.flow_from_directory(\n    '/kaggle/working/output/train',\n    target_size=(img_height, img_width),\n    batch_size=batch_size,\n    class_mode='categorical',\n    subset='training'\n)\n\nval_generator = datagen_val.flow_from_directory(\n    '/kaggle/working/output/val',\n    target_size=(img_height, img_width),\n    batch_size=batch_size,\n    class_mode='categorical',\n  \n)","metadata":{"execution":{"iopub.status.busy":"2023-03-18T07:40:38.982326Z","iopub.execute_input":"2023-03-18T07:40:38.983067Z","iopub.status.idle":"2023-03-18T07:40:39.537123Z","shell.execute_reply.started":"2023-03-18T07:40:38.983022Z","shell.execute_reply":"2023-03-18T07:40:39.535948Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = models.Sequential([\n    layers.Conv2D(32, (3,3), activation='relu', input_shape=(224,224, 3)),\n    layers.MaxPooling2D((2,2)),\n    layers.Conv2D(64, (3,3), activation='relu'),\n    layers.MaxPooling2D((2,2)),\n    layers.Conv2D(64, (3,3), activation='relu'),\n    layers.Flatten(),\n    layers.Dense(256, activation='relu'),\n    layers.Dense(128, activation='relu'),\n    layers.Dense(64, activation='relu'),\n    layers.Dense(10, activation='softmax'),\n])","metadata":{"execution":{"iopub.status.busy":"2023-03-18T07:40:39.538691Z","iopub.execute_input":"2023-03-18T07:40:39.539034Z","iopub.status.idle":"2023-03-18T07:40:39.630533Z","shell.execute_reply.started":"2023-03-18T07:40:39.538997Z","shell.execute_reply":"2023-03-18T07:40:39.629521Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"loss = 'categorical_crossentropy'\noptimizer = 'adam'\nmetrices = ['accuracy']\n\nmodel.compile(optimizer, loss, metrices)\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2023-03-18T07:40:39.631854Z","iopub.execute_input":"2023-03-18T07:40:39.632220Z","iopub.status.idle":"2023-03-18T07:40:39.672435Z","shell.execute_reply.started":"2023-03-18T07:40:39.632180Z","shell.execute_reply":"2023-03-18T07:40:39.671705Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"n_epochs = 10\nbatch_size = 32\nhistory_v2 = model.fit(train_generator,\n                         steps_per_epoch=561,\n                         epochs = n_epochs, \n                         verbose = 1,\n                         validation_data = val_generator,\n                         validation_steps =  140)","metadata":{"execution":{"iopub.status.busy":"2023-03-18T07:40:39.673464Z","iopub.execute_input":"2023-03-18T07:40:39.674008Z","iopub.status.idle":"2023-03-18T08:32:02.029122Z","shell.execute_reply.started":"2023-03-18T07:40:39.673969Z","shell.execute_reply":"2023-03-18T08:32:02.027957Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_train_history(history_v2)","metadata":{"execution":{"iopub.status.busy":"2023-03-18T08:32:02.031201Z","iopub.execute_input":"2023-03-18T08:32:02.031654Z","iopub.status.idle":"2023-03-18T08:32:02.465037Z","shell.execute_reply.started":"2023-03-18T08:32:02.031611Z","shell.execute_reply":"2023-03-18T08:32:02.464028Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.applications import VGG19\nimport tensorflow as tf ","metadata":{"execution":{"iopub.status.busy":"2023-03-18T08:32:02.466464Z","iopub.execute_input":"2023-03-18T08:32:02.467633Z","iopub.status.idle":"2023-03-18T08:32:02.473393Z","shell.execute_reply.started":"2023-03-18T08:32:02.467593Z","shell.execute_reply":"2023-03-18T08:32:02.472113Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pretrained_model3 = VGG19(weights= 'imagenet', include_top=False, input_shape= (224, 224,3))\npretrained_model3.summary()\nlast_layer=pretrained_model3.get_layer('block5_pool')\nlast_output = last_layer.output\n\nx=tf.keras.layers.Flatten()(last_output)\nx=tf.keras.layers.Dense(2048,activation='relu')(x)\nx=tf.keras.layers.Dense(1024,activation='relu')(x)\nx=tf.keras.layers.Dropout(0.2)(x)\nx=tf.keras.layers.Dense(256,activation='relu')(x)\nx=tf.keras.layers.Dropout(0.2)(x)\nx=tf.keras.layers.Dense(10,activation='softmax')(x)\n\n\n\nmodel3=tf.keras.Model(pretrained_model3.input,x)\n    \nmodel3.compile(optimizer = tf.keras.optimizers.SGD(lr=0.001),\n              loss='categorical_crossentropy',\n               metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2023-03-18T08:32:02.475011Z","iopub.execute_input":"2023-03-18T08:32:02.475404Z","iopub.status.idle":"2023-03-18T08:32:03.615010Z","shell.execute_reply.started":"2023-03-18T08:32:02.475366Z","shell.execute_reply":"2023-03-18T08:32:03.614046Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history_v3 = model3.fit(train_generator,\n                         steps_per_epoch=561,\n                         epochs = n_epochs, \n                         verbose = 1,\n                         validation_data = val_generator,\n                         validation_steps =  140)","metadata":{"execution":{"iopub.status.busy":"2023-03-18T08:32:03.620000Z","iopub.execute_input":"2023-03-18T08:32:03.620954Z","iopub.status.idle":"2023-03-18T09:40:58.298191Z","shell.execute_reply.started":"2023-03-18T08:32:03.620889Z","shell.execute_reply":"2023-03-18T09:40:58.297151Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_train_history(history_v3)","metadata":{"execution":{"iopub.status.busy":"2023-03-18T09:40:58.302994Z","iopub.execute_input":"2023-03-18T09:40:58.305501Z","iopub.status.idle":"2023-03-18T09:40:58.752454Z","shell.execute_reply.started":"2023-03-18T09:40:58.305458Z","shell.execute_reply":"2023-03-18T09:40:58.751461Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Team:\nMohamed Mostafa Badr\n\nMohamed Ali Elfeky\n\nMohamed Essam Abdelmonem \n\nFatima Mahmoud \n\nOla Abddallah","metadata":{}},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}