{"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":"markdown","source":"# <a name=\"0\">State Farm Distracted Driver Detection</a>\n## A Computer Vision Problem.\n### The competition page from [this](https://www.kaggle.com/competitions/state-farm-distracted-driver-detection) link.","metadata":{}},{"cell_type":"markdown","source":"### Table of Contents of the notebook:\n\n1. <a href=\"#1\">**Libraries**</a>\n2. <a href=\"#2\">**Display Excel File**</a>\n3. <a href=\"#3\">**Split train data to train & validation**</a>\n4. <a href=\"#4\">**Display some Images**</a>\n5. <a href='#5'>**Dense Layer**</a>\n6. <a href='#6'>**CNN model**</a>\n7. <a href=\"#7\">**Data Augentation**</a>\n8. <a href=\"#8\">**Frozen model**</a>\n9. <a href=\"#9\">**Fine Tuning**</a>\n10. <a href=\"#10\">**Testing**</a>\n11. <a href=\"#11\">**Group names**</a>","metadata":{}},{"cell_type":"markdown","source":"# 1. <a name=\"1\">**Libraries**</a>","metadata":{}},{"cell_type":"code","source":"!pip install --upgrade  tensorflow==2.8.0","metadata":{"execution":{"iopub.status.busy":"2023-04-28T05:00:39.221790Z","iopub.execute_input":"2023-04-28T05:00:39.222396Z","iopub.status.idle":"2023-04-28T05:01:46.283055Z","shell.execute_reply.started":"2023-04-28T05:00:39.222363Z","shell.execute_reply":"2023-04-28T05:01:46.281794Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install split-folders","metadata":{"id":"zbUkzYxrRLOR","outputId":"c7ecc6da-528e-435a-f1c7-005e6e2464ab","execution":{"iopub.status.busy":"2023-04-28T05:01:46.285833Z","iopub.execute_input":"2023-04-28T05:01:46.287412Z","iopub.status.idle":"2023-04-28T05:01:57.692064Z","shell.execute_reply.started":"2023-04-28T05:01:46.287358Z","shell.execute_reply":"2023-04-28T05:01:57.690841Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nfrom tqdm import tqdm\nimport datetime\n\nfrom getpass import getpass\nimport os\nimport cv2, glob\n\nimport splitfolders\n\nfrom sklearn.model_selection import train_test_split\n\nimport tensorflow as tf\nimport tensorflow.keras as keras\n\nfrom tensorflow.keras.layers import Conv2D, MaxPooling2D, Flatten, Dense,Dropout\nfrom tensorflow.keras.utils import plot_model\n\nfrom tensorflow.keras.applications import VGG16, VGG19\nfrom tensorflow.keras.applications.resnet import ResNet50\nfrom tensorflow.keras.utils import to_categorical\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom tensorflow.keras.applications.imagenet_utils import preprocess_input\nfrom tensorflow.keras.callbacks import EarlyStopping\n\n\nfrom tensorflow.keras import models\nfrom tensorflow.keras import layers\nfrom tensorflow.keras import optimizers\n\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom tensorflow.keras.applications.imagenet_utils import preprocess_input\n\nfrom tensorflow.keras.applications.imagenet_utils import decode_predictions\n\nfrom tensorflow.keras.models import load_model","metadata":{"id":"15ao7Qtwgvxa","execution":{"iopub.status.busy":"2023-04-28T05:01:57.693643Z","iopub.execute_input":"2023-04-28T05:01:57.693950Z","iopub.status.idle":"2023-04-28T05:02:01.970624Z","shell.execute_reply.started":"2023-04-28T05:01:57.693918Z","shell.execute_reply":"2023-04-28T05:02:01.969411Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"keras.__version__","metadata":{"execution":{"iopub.status.busy":"2023-04-28T05:02:01.973742Z","iopub.execute_input":"2023-04-28T05:02:01.974626Z","iopub.status.idle":"2023-04-28T05:02:01.981853Z","shell.execute_reply.started":"2023-04-28T05:02:01.974585Z","shell.execute_reply":"2023-04-28T05:02:01.980768Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 2. <a name=\"2\">**Display Excel File**</a>","metadata":{}},{"cell_type":"code","source":"df = pd.read_csv(\"/kaggle/input/state-farm-distracted-driver-detection/driver_imgs_list.csv\")\ndf.head()","metadata":{"execution":{"iopub.status.busy":"2023-04-28T05:02:01.984368Z","iopub.execute_input":"2023-04-28T05:02:01.985963Z","iopub.status.idle":"2023-04-28T05:02:02.046520Z","shell.execute_reply.started":"2023-04-28T05:02:01.985846Z","shell.execute_reply":"2023-04-28T05:02:02.045355Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.shape","metadata":{"execution":{"iopub.status.busy":"2023-04-28T05:02:02.048175Z","iopub.execute_input":"2023-04-28T05:02:02.048551Z","iopub.status.idle":"2023-04-28T05:02:02.055855Z","shell.execute_reply.started":"2023-04-28T05:02:02.048504Z","shell.execute_reply":"2023-04-28T05:02:02.054853Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.info()","metadata":{"execution":{"iopub.status.busy":"2023-04-28T05:02:02.057160Z","iopub.execute_input":"2023-04-28T05:02:02.057946Z","iopub.status.idle":"2023-04-28T05:02:02.086389Z","shell.execute_reply.started":"2023-04-28T05:02:02.057911Z","shell.execute_reply":"2023-04-28T05:02:02.084398Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### 3. <a name=\"3\">**Split train data to train & validation**</a>","metadata":{}},{"cell_type":"code","source":"data_dir = '/kaggle/input/state-farm-distracted-driver-detection/imgs/train'\nsplitfolders.ratio(data_dir, output=\"dataa\",\n    seed=1337, ratio=(.8, .2), group_prefix=None, move=False)","metadata":{"id":"p07ckzq7MqD_","outputId":"c0bbb58f-d31b-4756-e971-41db2f540243","execution":{"iopub.status.busy":"2023-04-28T05:02:02.089029Z","iopub.execute_input":"2023-04-28T05:02:02.089662Z","iopub.status.idle":"2023-04-28T05:04:33.133191Z","shell.execute_reply.started":"2023-04-28T05:02:02.089623Z","shell.execute_reply":"2023-04-28T05:04:33.132059Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 4. <a name=\"4\">**Display some Images**</a>","metadata":{}},{"cell_type":"code","source":"state = ['safe driving', 'texting - right', 'talking on the phone - right', 'texting - left', 'talking on the phone - left',\n         'operating the radio', 'drinking', 'reaching behind', 'hair and makeup', 'talking to passenger', 'UNKNOWN']\n\ndef Display(path, Class=None):\n    img = cv2.imread(path)\n    img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)\n    if Class == None:\n        plt.figure()\n        plt.title(state[10])\n        plt.imshow(img)\n        plt.axis(\"off\")\n        # print(img.shape)\n    else:\n        plt.subplot(2, 5, Class+1)\n        plt.title(state[Class])\n        plt.imshow(img)\n        plt.axis(\"off\")\n\nplt.figure(figsize=(20, 5))\nDisplay(\"/kaggle/input/state-farm-distracted-driver-detection/imgs/train/c0/img_100026.jpg\", 0)\nDisplay(\"/kaggle/input/state-farm-distracted-driver-detection/imgs/train/c1/img_100021.jpg\", 1)\nDisplay(\"/kaggle/input/state-farm-distracted-driver-detection/imgs/train/c2/img_100029.jpg\", 2)\nDisplay(\"/kaggle/input/state-farm-distracted-driver-detection/imgs/train/c3/img_100006.jpg\", 3)\nDisplay(\"/kaggle/input/state-farm-distracted-driver-detection/imgs/train/c4/img_100225.jpg\", 4)\nDisplay(\"/kaggle/input/state-farm-distracted-driver-detection/imgs/train/c5/img_10000.jpg\", 5)\nDisplay(\"/kaggle/input/state-farm-distracted-driver-detection/imgs/train/c6/img_100036.jpg\", 6)\nDisplay(\"/kaggle/input/state-farm-distracted-driver-detection/imgs/train/c7/img_100057.jpg\", 7)\nDisplay(\"/kaggle/input/state-farm-distracted-driver-detection/imgs/train/c8/img_100015.jpg\", 8)\nDisplay(\"/kaggle/input/state-farm-distracted-driver-detection/imgs/train/c9/img_100090.jpg\", 9)","metadata":{"execution":{"iopub.status.busy":"2023-04-28T05:04:33.134895Z","iopub.execute_input":"2023-04-28T05:04:33.135292Z","iopub.status.idle":"2023-04-28T05:04:34.484322Z","shell.execute_reply.started":"2023-04-28T05:04:33.135253Z","shell.execute_reply":"2023-04-28T05:04:34.482987Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 5. <a name=\"5\">**Dense Layer**</a>","metadata":{}},{"cell_type":"code","source":"dense_train_gen = ImageDataGenerator(rescale=1./255)\ndense_train = dense_train_gen.flow_from_directory('/kaggle/working/dataa/train', batch_size=128, target_size= (256, 256))\n\ndense_val_gen = ImageDataGenerator(rescale=1./255)\ndense_val = dense_val_gen.flow_from_directory('/kaggle/working/dataa/val', batch_size=128, target_size= (256, 256))","metadata":{"execution":{"iopub.status.busy":"2023-04-28T05:04:34.488208Z","iopub.execute_input":"2023-04-28T05:04:34.490100Z","iopub.status.idle":"2023-04-28T05:04:35.052929Z","shell.execute_reply.started":"2023-04-28T05:04:34.490045Z","shell.execute_reply":"2023-04-28T05:04:35.051802Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_dense = models.Sequential()\nmodel_dense.add(Flatten(input_shape=(256, 256, 3)))\nmodel_dense.add(Dense(512, activation='relu'))\nmodel_dense.add(Dense(256, activation='relu'))\nmodel_dense.add(Dense(128, activation='relu'))\nmodel_dense.add(Dense(10, activation='softmax'))\nmodel_dense.compile(optimizer='adam',\n                loss='categorical_crossentropy',\n                metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2023-04-28T05:04:35.054421Z","iopub.execute_input":"2023-04-28T05:04:35.054910Z","iopub.status.idle":"2023-04-28T05:04:37.474863Z","shell.execute_reply.started":"2023-04-28T05:04:35.054861Z","shell.execute_reply":"2023-04-28T05:04:37.473874Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_dense.summary()","metadata":{"execution":{"iopub.status.busy":"2023-04-28T05:04:37.476339Z","iopub.execute_input":"2023-04-28T05:04:37.476711Z","iopub.status.idle":"2023-04-28T05:04:37.500566Z","shell.execute_reply.started":"2023-04-28T05:04:37.476673Z","shell.execute_reply":"2023-04-28T05:04:37.499789Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history_dense = model_dense.fit(dense_train ,epochs=10,validation_data=dense_val)","metadata":{"execution":{"iopub.status.busy":"2023-04-28T05:04:37.501512Z","iopub.execute_input":"2023-04-28T05:04:37.501843Z","iopub.status.idle":"2023-04-28T05:27:55.278214Z","shell.execute_reply.started":"2023-04-28T05:04:37.501809Z","shell.execute_reply":"2023-04-28T05:27:55.277088Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(history_dense.history['accuracy'], 'bo')\nplt.plot(history_dense.history['val_accuracy'], 'b')\nplt.title('Dense Model Accuracy')\nplt.ylabel('accuracy')\nplt.xlabel('epoch')\nplt.legend(['train', 'val'], loc='upper left')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-04-28T05:27:55.281459Z","iopub.execute_input":"2023-04-28T05:27:55.281792Z","iopub.status.idle":"2023-04-28T05:27:55.506297Z","shell.execute_reply.started":"2023-04-28T05:27:55.281760Z","shell.execute_reply":"2023-04-28T05:27:55.505065Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(history_dense.history['loss'], 'bo')\nplt.plot(history_dense.history['val_loss'], 'b')\nplt.title('Dense Model Loss')\nplt.ylabel('loss')\nplt.xlabel('epoch')\nplt.legend(['train', 'val'], loc='upper right')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-04-28T05:27:55.507861Z","iopub.execute_input":"2023-04-28T05:27:55.508184Z","iopub.status.idle":"2023-04-28T05:27:55.717174Z","shell.execute_reply.started":"2023-04-28T05:27:55.508151Z","shell.execute_reply":"2023-04-28T05:27:55.716060Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_dense.save('/kaggle/working/dense_model.h5')","metadata":{"execution":{"iopub.status.busy":"2023-04-28T05:27:55.718554Z","iopub.execute_input":"2023-04-28T05:27:55.718936Z","iopub.status.idle":"2023-04-28T05:27:59.663789Z","shell.execute_reply.started":"2023-04-28T05:27:55.718898Z","shell.execute_reply":"2023-04-28T05:27:59.662050Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 6. <a name=\"6\">**CNN model**</a>","metadata":{}},{"cell_type":"code","source":"train_gen = ImageDataGenerator(rescale=1./255)\ntrain = train_gen.flow_from_directory('/kaggle/working/dataa/train', batch_size=64, target_size= (256, 256))\n\nval_gen = ImageDataGenerator(rescale=1./255)\nval = val_gen.flow_from_directory('/kaggle/working/dataa/val', batch_size=64, target_size= (256, 256))","metadata":{"execution":{"iopub.status.busy":"2023-04-28T05:27:59.665871Z","iopub.execute_input":"2023-04-28T05:27:59.668441Z","iopub.status.idle":"2023-04-28T05:28:00.875663Z","shell.execute_reply.started":"2023-04-28T05:27:59.668402Z","shell.execute_reply":"2023-04-28T05:28:00.874644Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_CNN = models.Sequential()\nmodel_CNN.add(Conv2D(32, (3, 3), activation='relu', input_shape=(256, 256, 3)))\nmodel_CNN.add(MaxPooling2D((2, 2)))\n\n\nmodel_CNN.add(Conv2D(64, (3, 3), activation='relu'))\nmodel_CNN.add(MaxPooling2D((2, 2)))\n\nmodel_CNN.add(Conv2D(128, (3, 3), activation='relu'))\nmodel_CNN.add(MaxPooling2D((2, 2)))\n\nmodel_CNN.add(Flatten())\nmodel_CNN.add(Dense(128, activation='relu'))\n\n\n\nmodel_CNN.add(Dense(10, activation='softmax'))\n\n\nmodel_CNN.summary()","metadata":{"execution":{"iopub.status.busy":"2023-04-28T05:28:00.880096Z","iopub.execute_input":"2023-04-28T05:28:00.882328Z","iopub.status.idle":"2023-04-28T05:28:01.030709Z","shell.execute_reply.started":"2023-04-28T05:28:00.882289Z","shell.execute_reply":"2023-04-28T05:28:01.030013Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_CNN.compile(optimizer='adam',\n                loss='categorical_crossentropy',\n                metrics=['acc'])","metadata":{"execution":{"iopub.status.busy":"2023-04-28T05:28:01.031659Z","iopub.execute_input":"2023-04-28T05:28:01.031978Z","iopub.status.idle":"2023-04-28T05:28:01.059140Z","shell.execute_reply.started":"2023-04-28T05:28:01.031948Z","shell.execute_reply":"2023-04-28T05:28:01.058149Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from IPython.testing import test\nhistory_CNN=model_CNN.fit(train ,epochs=20, batch_size=64,validation_data=val)","metadata":{"execution":{"iopub.status.busy":"2023-04-28T05:28:01.060143Z","iopub.execute_input":"2023-04-28T05:28:01.060477Z","iopub.status.idle":"2023-04-28T06:17:21.473108Z","shell.execute_reply.started":"2023-04-28T05:28:01.060447Z","shell.execute_reply":"2023-04-28T06:17:21.472070Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"acc = history_CNN.history['acc']\nval_acc = history_CNN.history['val_acc']\n\nepochs = range(len(acc))\n\nplt.plot(epochs, acc, 'bo', label='Training accuracy')\nplt.plot(epochs, val_acc, 'b', label='Validation accuracy')\nplt.title('Training and validation accuracy')\nplt.legend()","metadata":{"execution":{"iopub.status.busy":"2023-04-28T06:17:21.474681Z","iopub.execute_input":"2023-04-28T06:17:21.475365Z","iopub.status.idle":"2023-04-28T06:17:21.731444Z","shell.execute_reply.started":"2023-04-28T06:17:21.475324Z","shell.execute_reply":"2023-04-28T06:17:21.730503Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"loss = history_CNN.history['loss']\nval_loss = history_CNN.history['val_loss']\n\nplt.plot(epochs, loss, 'bo', label='Training loss')\nplt.plot(epochs, val_loss, 'b', label='Validation loss')\nplt.title('Training and validation loss')\nplt.legend()\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-04-28T06:17:21.732840Z","iopub.execute_input":"2023-04-28T06:17:21.733301Z","iopub.status.idle":"2023-04-28T06:17:21.946255Z","shell.execute_reply.started":"2023-04-28T06:17:21.733263Z","shell.execute_reply":"2023-04-28T06:17:21.945182Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_CNN.save('/kaggle/working/CNN_model.h5')","metadata":{"execution":{"iopub.status.busy":"2023-04-28T06:17:21.947843Z","iopub.execute_input":"2023-04-28T06:17:21.948504Z","iopub.status.idle":"2023-04-28T06:17:22.375602Z","shell.execute_reply.started":"2023-04-28T06:17:21.948463Z","shell.execute_reply":"2023-04-28T06:17:22.374555Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 7. <a name=\"7\">**Data Augmentation**</a>","metadata":{}},{"cell_type":"markdown","source":"## Preprocessing","metadata":{}},{"cell_type":"code","source":"batch_size = 32\ndatagen = ImageDataGenerator(rescale = 1/255,\n                            zoom_range=0.05,\n                            width_shift_range=0.05,\n                            height_shift_range=0.05,\n                            shear_range=0.05, \n                            fill_mode=\"nearest\")\n\n\ntrain = datagen.flow_from_directory('/kaggle/working/dataa/train', batch_size=batch_size, target_size = (256, 256))\n\nval_gen = ImageDataGenerator(rescale = 1/255)\nval = val_gen.flow_from_directory('/kaggle/working/dataa/val', batch_size=batch_size, target_size = (256, 256))","metadata":{"id":"FZqI26O6VG_A","outputId":"54c1bf27-88ce-431e-cf45-4bdf25220a2f","execution":{"iopub.status.busy":"2023-04-28T06:17:22.377245Z","iopub.execute_input":"2023-04-28T06:17:22.377643Z","iopub.status.idle":"2023-04-28T06:17:22.932977Z","shell.execute_reply.started":"2023-04-28T06:17:22.377605Z","shell.execute_reply":"2023-04-28T06:17:22.932018Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for data_batch, labels_batch in train:\n    print('data batch shape:', data_batch.shape)\n    print('labels batch shape:', labels_batch.shape)\n    break","metadata":{"execution":{"iopub.status.busy":"2023-04-28T06:17:22.934475Z","iopub.execute_input":"2023-04-28T06:17:22.934818Z","iopub.status.idle":"2023-04-28T06:17:23.438411Z","shell.execute_reply.started":"2023-04-28T06:17:22.934789Z","shell.execute_reply":"2023-04-28T06:17:23.437080Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Baseline Model","metadata":{}},{"cell_type":"code","source":"from keras import models\nfrom keras import layers\nfrom tensorflow.keras.layers import Dropout\n\nmodel_DA = models.Sequential()\nmodel_DA.add(layers.Conv2D(32,(3,3), activation='relu', name='Layer_1', input_shape=(256,256, 3)))\nmodel_DA.add(layers.MaxPooling2D((2, 2)))\nmodel_DA.add(layers.Conv2D(128,(3,3), activation='relu', name='Layer_2'))\nmodel_DA.add(layers.MaxPooling2D((2, 2)))\nmodel_DA.add(layers.Conv2D(128,(3,3), activation='relu', name='Layer_3'))\nmodel_DA.add(layers.MaxPooling2D((2, 2)))\nmodel_DA.add(layers.Conv2D(64,(3,3), activation='relu', name='Layer_4'))\nmodel_DA.add(layers.MaxPooling2D((2, 2)))\nmodel_DA.add(layers.Flatten())\nmodel_DA.add(layers.Dense(512, activation='relu'))\nmodel_DA.add(layers.Dropout(0.4))\nmodel_DA.add(layers.Dense(10, activation='softmax'))","metadata":{"execution":{"iopub.status.busy":"2023-04-28T06:17:23.440223Z","iopub.execute_input":"2023-04-28T06:17:23.440629Z","iopub.status.idle":"2023-04-28T06:17:23.520699Z","shell.execute_reply.started":"2023-04-28T06:17:23.440591Z","shell.execute_reply":"2023-04-28T06:17:23.519685Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_DA.summary()","metadata":{"execution":{"iopub.status.busy":"2023-04-28T06:17:23.521956Z","iopub.execute_input":"2023-04-28T06:17:23.522324Z","iopub.status.idle":"2023-04-28T06:17:23.578981Z","shell.execute_reply.started":"2023-04-28T06:17:23.522288Z","shell.execute_reply":"2023-04-28T06:17:23.578164Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_DA.compile(optimizer='rmsprop',\n                loss='categorical_crossentropy',\n                metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2023-04-28T06:17:23.589971Z","iopub.execute_input":"2023-04-28T06:17:23.590727Z","iopub.status.idle":"2023-04-28T06:17:23.605102Z","shell.execute_reply.started":"2023-04-28T06:17:23.590689Z","shell.execute_reply":"2023-04-28T06:17:23.604050Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history_DA = model_DA.fit(\n      train,\n      epochs=20,\n      steps_per_epoch = 150,\n      validation_data=val,\n      verbose=2,\n      validation_steps=50)","metadata":{"execution":{"iopub.status.busy":"2023-04-28T06:17:23.606721Z","iopub.execute_input":"2023-04-28T06:17:23.607420Z","iopub.status.idle":"2023-04-28T06:57:27.878267Z","shell.execute_reply.started":"2023-04-28T06:17:23.607383Z","shell.execute_reply":"2023-04-28T06:57:27.877144Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"acc = history_DA.history['accuracy']\nval_acc = history_DA.history['val_accuracy']\n\nepochs = range(len(acc))\n\nplt.plot(epochs, acc, 'bo', label='Training accuracy')\nplt.plot(epochs, val_acc, 'b', label='Validation accuracy')\nplt.title('Training and validation accuracy')\nplt.legend()","metadata":{"execution":{"iopub.status.busy":"2023-04-28T06:57:27.879949Z","iopub.execute_input":"2023-04-28T06:57:27.880279Z","iopub.status.idle":"2023-04-28T06:57:28.145209Z","shell.execute_reply.started":"2023-04-28T06:57:27.880249Z","shell.execute_reply":"2023-04-28T06:57:28.144156Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"loss = history_DA.history['loss']\nval_loss = history_DA.history['val_loss']\n\nplt.plot(epochs, loss, 'bo', label='Training loss')\nplt.plot(epochs, val_loss, 'b', label='Validation loss')\nplt.title('Training and validation loss')\nplt.legend()\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-04-28T06:57:28.146883Z","iopub.execute_input":"2023-04-28T06:57:28.147284Z","iopub.status.idle":"2023-04-28T06:57:28.371086Z","shell.execute_reply.started":"2023-04-28T06:57:28.147247Z","shell.execute_reply":"2023-04-28T06:57:28.370048Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_DA.save(\"/kaggle/working/Data_Augmentation_model.h5\")","metadata":{"execution":{"iopub.status.busy":"2023-04-28T06:57:28.372532Z","iopub.execute_input":"2023-04-28T06:57:28.373196Z","iopub.status.idle":"2023-04-28T06:57:28.500922Z","shell.execute_reply.started":"2023-04-28T06:57:28.373156Z","shell.execute_reply":"2023-04-28T06:57:28.499897Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 8. <a name=\"8\">**Frozen model**</a>","metadata":{}},{"cell_type":"code","source":"batch_size = 32\ndatagen = ImageDataGenerator(preprocessing_function=preprocess_input,\n                            zoom_range=0.05,\n                            width_shift_range=0.05,\n                            height_shift_range=0.05,\n                            shear_range=0.05, \n                            fill_mode=\"nearest\")\n\n\ntrain = datagen.flow_from_directory('/kaggle/working/dataa/train', batch_size=batch_size, target_size = (256, 256))\n\nval_gen = ImageDataGenerator(preprocessing_function = preprocess_input)\nval = val_gen.flow_from_directory('/kaggle/working/dataa/val', batch_size=batch_size, target_size = (256, 256))","metadata":{"execution":{"iopub.status.busy":"2023-04-28T06:57:28.502216Z","iopub.execute_input":"2023-04-28T06:57:28.502582Z","iopub.status.idle":"2023-04-28T06:57:29.064195Z","shell.execute_reply.started":"2023-04-28T06:57:28.502544Z","shell.execute_reply":"2023-04-28T06:57:29.063168Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Build NN","metadata":{}},{"cell_type":"code","source":"conv_base = VGG16(weights='imagenet',\n                  include_top=False,\n                  input_shape=(256, 256, 3))\n\nconv_base.summary()","metadata":{"id":"yMk8-J64dF21","outputId":"6bf5861a-c19d-4535-d86d-f2a3b5f5a082","execution":{"iopub.status.busy":"2023-04-28T06:57:29.065585Z","iopub.execute_input":"2023-04-28T06:57:29.065973Z","iopub.status.idle":"2023-04-28T06:57:29.663298Z","shell.execute_reply.started":"2023-04-28T06:57:29.065933Z","shell.execute_reply":"2023-04-28T06:57:29.662440Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_DL = models.Sequential()\nmodel_DL.add(conv_base)\nmodel_DL.add(layers.Flatten())\nmodel_DL.add(layers.Dense(512, activation='relu'))\nmodel_DL.add(layers.Dropout(0.35))\nmodel_DL.add(layers.Dense(128, activation='relu'))\nmodel_DL.add(layers.Dropout(0.35))\nmodel_DL.add(layers.Dense(32, activation='relu'))\nmodel_DL.add(layers.Dense(10, activation='softmax'))\n\nmodel_DL.summary()","metadata":{"id":"H2oOTq62doB9","outputId":"dd4b1a12-6ae8-48b4-a807-91c4c7db8822","execution":{"iopub.status.busy":"2023-04-28T06:57:29.664447Z","iopub.execute_input":"2023-04-28T06:57:29.665161Z","iopub.status.idle":"2023-04-28T06:57:29.804427Z","shell.execute_reply.started":"2023-04-28T06:57:29.665100Z","shell.execute_reply":"2023-04-28T06:57:29.803614Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('This is the number of trainable weights '\n      'before freezing the conv base:', len(model_DL.trainable_weights))","metadata":{"id":"or6NaXTHe8vZ","outputId":"a263a657-b846-48cc-b23c-c5438212e42a","execution":{"iopub.status.busy":"2023-04-28T06:57:29.805447Z","iopub.execute_input":"2023-04-28T06:57:29.805766Z","iopub.status.idle":"2023-04-28T06:57:29.811291Z","shell.execute_reply.started":"2023-04-28T06:57:29.805732Z","shell.execute_reply":"2023-04-28T06:57:29.810455Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"conv_base.trainable = False","metadata":{"id":"UgovBcnZe8n4","execution":{"iopub.status.busy":"2023-04-28T06:57:29.812637Z","iopub.execute_input":"2023-04-28T06:57:29.813056Z","iopub.status.idle":"2023-04-28T06:57:29.823008Z","shell.execute_reply.started":"2023-04-28T06:57:29.813020Z","shell.execute_reply":"2023-04-28T06:57:29.821787Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('This is the number of trainable weights '\n      'before freezing the conv base:', len(model_DL.trainable_weights))","metadata":{"id":"4N-ljuiee8k_","outputId":"b0ed54f6-44af-42ec-895d-42feb9291f2b","execution":{"iopub.status.busy":"2023-04-28T06:57:29.824059Z","iopub.execute_input":"2023-04-28T06:57:29.824682Z","iopub.status.idle":"2023-04-28T06:57:29.835641Z","shell.execute_reply.started":"2023-04-28T06:57:29.824640Z","shell.execute_reply":"2023-04-28T06:57:29.834593Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_DL.summary()","metadata":{"execution":{"iopub.status.busy":"2023-04-28T06:57:29.836833Z","iopub.execute_input":"2023-04-28T06:57:29.837245Z","iopub.status.idle":"2023-04-28T06:57:29.867853Z","shell.execute_reply.started":"2023-04-28T06:57:29.837210Z","shell.execute_reply":"2023-04-28T06:57:29.866957Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_DL.compile(optimizer = optimizers.Adam(learning_rate=0.0001),\n             loss='categorical_crossentropy',\n              metrics=['accuracy'])\n\nhistory_DL = model_DL.fit(train,\n                    epochs=20,\n                    steps_per_epoch = 150,\n                    validation_data=val,\n                    verbose=1)","metadata":{"id":"p6p_li69fDAc","outputId":"654eabb7-6cc9-4604-81b5-f98376ebf905","execution":{"iopub.status.busy":"2023-04-28T06:57:29.868829Z","iopub.execute_input":"2023-04-28T06:57:29.869204Z","iopub.status.idle":"2023-04-28T07:43:30.597016Z","shell.execute_reply.started":"2023-04-28T06:57:29.869168Z","shell.execute_reply":"2023-04-28T07:43:30.595925Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_acc, train_loss = model_DL.evaluate(train)","metadata":{"execution":{"iopub.status.busy":"2023-04-28T07:43:30.600381Z","iopub.execute_input":"2023-04-28T07:43:30.600680Z","iopub.status.idle":"2023-04-28T07:49:22.185214Z","shell.execute_reply.started":"2023-04-28T07:43:30.600652Z","shell.execute_reply":"2023-04-28T07:49:22.184147Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"val_acc, val_loss = model_DL.evaluate(val)","metadata":{"execution":{"iopub.status.busy":"2023-04-28T07:49:22.187215Z","iopub.execute_input":"2023-04-28T07:49:22.187638Z","iopub.status.idle":"2023-04-28T07:49:59.827570Z","shell.execute_reply.started":"2023-04-28T07:49:22.187596Z","shell.execute_reply":"2023-04-28T07:49:59.826066Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"acc = history_DL.history['accuracy']\nval_acc = history_DL.history['val_accuracy']\n\nepochs = range(len(acc))\n\nplt.plot(epochs, acc, 'bo', label='Training accuracy')\nplt.plot(epochs, val_acc, 'b', label='Validation accuracy')\nplt.title('Training and validation accuracy')\nplt.legend()","metadata":{"id":"jAsFeA6ckW8N","execution":{"iopub.status.busy":"2023-04-28T07:49:59.829069Z","iopub.execute_input":"2023-04-28T07:49:59.829449Z","iopub.status.idle":"2023-04-28T07:50:00.388324Z","shell.execute_reply.started":"2023-04-28T07:49:59.829417Z","shell.execute_reply":"2023-04-28T07:50:00.387196Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"loss = history_DL.history['loss']\nval_loss = history_DL.history['val_loss']\n\nplt.plot(epochs, loss, 'bo', label='Training loss')\nplt.plot(epochs, val_loss, 'b', label='Validation loss')\nplt.title('Training and validation loss')\nplt.legend()\n\nplt.show()","metadata":{"id":"4PnAkL3LkWxC","execution":{"iopub.status.busy":"2023-04-28T07:50:00.389853Z","iopub.execute_input":"2023-04-28T07:50:00.390940Z","iopub.status.idle":"2023-04-28T07:50:00.628218Z","shell.execute_reply.started":"2023-04-28T07:50:00.390896Z","shell.execute_reply":"2023-04-28T07:50:00.627059Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_DL.save('/kaggle/working/driver_detection_Frozen_model.h5')\nprint(\"Saved model to kaggle\")","metadata":{"id":"AdF8iqitfC9P","execution":{"iopub.status.busy":"2023-04-28T07:50:00.629974Z","iopub.execute_input":"2023-04-28T07:50:00.630449Z","iopub.status.idle":"2023-04-28T07:50:01.263578Z","shell.execute_reply.started":"2023-04-28T07:50:00.630407Z","shell.execute_reply":"2023-04-28T07:50:01.262269Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 9. <a name=\"9\">**Fine Tuning**</a>","metadata":{"id":"-bgrbfJmNnj4"}},{"cell_type":"code","source":"conv_base.summary()","metadata":{"execution":{"iopub.status.busy":"2023-04-28T07:50:01.265457Z","iopub.execute_input":"2023-04-28T07:50:01.265850Z","iopub.status.idle":"2023-04-28T07:50:01.308079Z","shell.execute_reply.started":"2023-04-28T07:50:01.265810Z","shell.execute_reply":"2023-04-28T07:50:01.307219Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"conv_base.trainable = True\n\nset_trainable = False\nfor layer in conv_base.layers:\n    if layer.name == 'conv5_block1_1_conv':\n        set_trainable = True\n    if set_trainable:\n        layer.trainable = True\n    else:\n        layer.trainable = False","metadata":{"execution":{"iopub.status.busy":"2023-04-28T07:50:01.309259Z","iopub.execute_input":"2023-04-28T07:50:01.309639Z","iopub.status.idle":"2023-04-28T07:50:01.334866Z","shell.execute_reply.started":"2023-04-28T07:50:01.309601Z","shell.execute_reply":"2023-04-28T07:50:01.333511Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"conv_base.summary()","metadata":{"execution":{"iopub.status.busy":"2023-04-28T07:50:01.336070Z","iopub.execute_input":"2023-04-28T07:50:01.336521Z","iopub.status.idle":"2023-04-28T07:50:01.376865Z","shell.execute_reply.started":"2023-04-28T07:50:01.336476Z","shell.execute_reply":"2023-04-28T07:50:01.376084Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_copy= keras.models.clone_model(model_DL)\nmodel_copy.build((None, 150, 150, 3))                                \nmodel_copy.compile(optimizer = optimizers.Adam(learning_rate=0.0001),\n                   loss='categorical_crossentropy',\n                   metrics=['accuracy'])\nmodel_copy.set_weights(model_DL.get_weights())\n\nmodel_copy.summary()","metadata":{"execution":{"iopub.status.busy":"2023-04-28T07:50:01.378002Z","iopub.execute_input":"2023-04-28T07:50:01.378401Z","iopub.status.idle":"2023-04-28T07:50:01.928287Z","shell.execute_reply.started":"2023-04-28T07:50:01.378362Z","shell.execute_reply":"2023-04-28T07:50:01.927361Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_DL.summary()","metadata":{"execution":{"iopub.status.busy":"2023-04-28T07:50:01.929537Z","iopub.execute_input":"2023-04-28T07:50:01.930070Z","iopub.status.idle":"2023-04-28T07:50:01.965298Z","shell.execute_reply.started":"2023-04-28T07:50:01.930026Z","shell.execute_reply":"2023-04-28T07:50:01.964391Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_copy.compile(optimizer = optimizers.Adam(learning_rate=0.0001),\n             loss='categorical_crossentropy',\n              metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2023-04-28T07:50:01.966428Z","iopub.execute_input":"2023-04-28T07:50:01.966934Z","iopub.status.idle":"2023-04-28T07:50:01.993012Z","shell.execute_reply.started":"2023-04-28T07:50:01.966892Z","shell.execute_reply":"2023-04-28T07:50:01.992039Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history_FT = model_copy.fit(\n      train,\n      steps_per_epoch=100,                 #train.n//train.batch_size,\n      epochs=20,\n      validation_data=val,\n      validation_steps=50,                 #val.n//val.batch_size)\n      verbose=1)","metadata":{"execution":{"iopub.status.busy":"2023-04-28T07:50:01.994224Z","iopub.execute_input":"2023-04-28T07:50:01.994612Z","iopub.status.idle":"2023-04-28T08:16:42.784255Z","shell.execute_reply.started":"2023-04-28T07:50:01.994570Z","shell.execute_reply":"2023-04-28T08:16:42.782987Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_acc_FT, train_loss_FT = model_copy.evaluate(train)","metadata":{"execution":{"iopub.status.busy":"2023-04-28T08:16:42.786541Z","iopub.execute_input":"2023-04-28T08:16:42.787313Z","iopub.status.idle":"2023-04-28T08:22:23.781696Z","shell.execute_reply.started":"2023-04-28T08:16:42.787271Z","shell.execute_reply":"2023-04-28T08:22:23.780676Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"val_acc_FT, val_loss_FT = model_copy.evaluate(val)","metadata":{"execution":{"iopub.status.busy":"2023-04-28T08:22:23.784966Z","iopub.execute_input":"2023-04-28T08:22:23.785279Z","iopub.status.idle":"2023-04-28T08:22:58.976103Z","shell.execute_reply.started":"2023-04-28T08:22:23.785251Z","shell.execute_reply":"2023-04-28T08:22:58.975077Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"acc = history_FT.history['accuracy']\nval_acc = history_FT.history['val_accuracy']\n\nepochs = range(len(acc))\n\nplt.plot(epochs, acc, 'bo', label='Training accuracy')\nplt.plot(epochs, val_acc, 'b', label='Validation accuracy')\nplt.title('Training and validation accuracy')\nplt.legend()","metadata":{"execution":{"iopub.status.busy":"2023-04-28T08:22:58.979229Z","iopub.execute_input":"2023-04-28T08:22:58.979548Z","iopub.status.idle":"2023-04-28T08:22:59.243462Z","shell.execute_reply.started":"2023-04-28T08:22:58.979517Z","shell.execute_reply":"2023-04-28T08:22:59.242435Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"loss = history_FT.history['loss']\nval_loss = history_FT.history['val_loss']\n\nplt.plot(epochs, loss, 'bo', label='Training loss')\nplt.plot(epochs, val_loss, 'b', label='Validation loss')\nplt.title('Training and validation loss')\nplt.legend()\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-04-28T08:22:59.245011Z","iopub.execute_input":"2023-04-28T08:22:59.245648Z","iopub.status.idle":"2023-04-28T08:22:59.464202Z","shell.execute_reply.started":"2023-04-28T08:22:59.245610Z","shell.execute_reply":"2023-04-28T08:22:59.463266Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Smoothing","metadata":{}},{"cell_type":"code","source":"def smooth_curve(points, factor=0.8):\n    smoothed_points = []\n    for point in points:\n        if smoothed_points:\n            previous = smoothed_points[-1]\n            smoothed_points.append(previous * factor + point * (1 - factor))\n        else:\n            smoothed_points.append(point)\n    return smoothed_points\n\nplt.plot(epochs,\n         smooth_curve(acc), 'bo', label='Smoothed training acc')\nplt.plot(epochs,\n         smooth_curve(val_acc), 'b', label='Smoothed validation acc')\nplt.title('Training and validation accuracy')\nplt.legend()\n\nplt.figure()\n\nplt.plot(epochs,\n         smooth_curve(loss), 'bo', label='Smoothed training loss')\nplt.plot(epochs,\n         smooth_curve(val_loss), 'b', label='Smoothed validation loss')\nplt.title('Training and validation loss')\nplt.legend()\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-04-28T08:22:59.465643Z","iopub.execute_input":"2023-04-28T08:22:59.465984Z","iopub.status.idle":"2023-04-28T08:22:59.904065Z","shell.execute_reply.started":"2023-04-28T08:22:59.465950Z","shell.execute_reply":"2023-04-28T08:22:59.903090Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_copy.save('/kaggle/working/driver_detection_Fine_Tuning_model2.h5')\nprint(\"Saved model to kaggle\")","metadata":{"execution":{"iopub.status.busy":"2023-04-28T08:22:59.905346Z","iopub.execute_input":"2023-04-28T08:22:59.905694Z","iopub.status.idle":"2023-04-28T08:23:00.436618Z","shell.execute_reply.started":"2023-04-28T08:22:59.905657Z","shell.execute_reply":"2023-04-28T08:23:00.435351Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# # dense\nacc1=history_dense.history['accuracy'][-1]\nvacc1=history_dense.history['val_accuracy'][-1]\nloss1=history_dense.history['loss'][-2]\nvloss1=history_dense.history['val_loss'][-2]\n\n\n# CNN\nacc2=history_CNN.history['acc'][-1]\nvacc2=history_CNN.history['val_acc'][-1]\nloss2=history_CNN.history['loss'][-2]\nvloss2=history_CNN.history['val_loss'][-2]\n\n# # Data Augmentation\nacc3=history_DA.history['accuracy'][-1]\nvacc3=history_DA.history['val_accuracy'][-1]\nloss3=history_DA.history['loss'][-2]\nvloss3=history_DA.history['val_loss'][-2]\n\n# VGG16\nacc4=history_DL.history['accuracy'][-1]\nvacc4=history_DL.history['val_accuracy'][-1]\nloss4=history_DL.history['loss'][-2]\nvloss4=history_DL.history['val_loss'][-2]\n\n# VGG16\nacc5=history_FT.history['accuracy'][-1]\nvacc5=history_FT.history['val_accuracy'][-1]\nloss5=history_FT.history['loss'][-2]\nvloss5=history_FT.history['val_loss'][-2]","metadata":{"execution":{"iopub.status.busy":"2023-04-28T08:23:00.438357Z","iopub.execute_input":"2023-04-28T08:23:00.438748Z","iopub.status.idle":"2023-04-28T08:23:00.448227Z","shell.execute_reply.started":"2023-04-28T08:23:00.438709Z","shell.execute_reply":"2023-04-28T08:23:00.447129Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"results = pd.DataFrame([[\"Dense model\",acc1*100,vacc1*100,loss1,vloss1],\n                       [\"CNN\",acc2*100,vacc2*100,loss2,vloss2],\n                       [\"CNN_data_augmentation\",acc3*100,vacc3*100,loss3,vloss3],\n                       [\"Transfer Learning model\",acc4*100,vacc4*100,loss4,vloss4],\n                       [\"Transfer Learning model_FT\",acc5*100,vacc5*100,loss5,vloss5]],\n                       columns = [\"Model\",\"Training Accuracy %\",\"Validation Accuracy %\", 'Loss', 'Validation Loss'])\nresults","metadata":{"execution":{"iopub.status.busy":"2023-04-28T08:23:00.449827Z","iopub.execute_input":"2023-04-28T08:23:00.450479Z","iopub.status.idle":"2023-04-28T08:23:00.475553Z","shell.execute_reply.started":"2023-04-28T08:23:00.450444Z","shell.execute_reply":"2023-04-28T08:23:00.474592Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 10. <a name=\"10\">**Testing**</a>","metadata":{}},{"cell_type":"markdown","source":"## Displaying some test images","metadata":{}},{"cell_type":"code","source":"Display(\"/kaggle/input/state-farm-distracted-driver-detection/imgs/test/img_10.jpg\")\nDisplay(\"/kaggle/input/state-farm-distracted-driver-detection/imgs/test/img_100008.jpg\")\nDisplay(\"/kaggle/input/state-farm-distracted-driver-detection/imgs/test/img_100049.jpg\")\nDisplay(\"/kaggle/input/state-farm-distracted-driver-detection/imgs/test/img_100163.jpg\")\nDisplay(\"/kaggle/input/state-farm-distracted-driver-detection/imgs/test/img_100300.jpg\")","metadata":{"execution":{"iopub.status.busy":"2023-04-28T08:23:00.476980Z","iopub.execute_input":"2023-04-28T08:23:00.477417Z","iopub.status.idle":"2023-04-28T08:23:01.944440Z","shell.execute_reply.started":"2023-04-28T08:23:00.477376Z","shell.execute_reply":"2023-04-28T08:23:01.943533Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Load Test Data","metadata":{}},{"cell_type":"code","source":"test_datagen = ImageDataGenerator(preprocessing_function=preprocess_input)\n\ntest = test_datagen.flow_from_directory('/kaggle/input/state-farm-distracted-driver-detection/imgs/.',\n                                                  classes=['test'],\n                                                  target_size=(256, 256),\n                                                  batch_size = 20,\n                                                  class_mode = None,\n                                                  shuffle = False)","metadata":{"execution":{"iopub.status.busy":"2023-04-28T08:23:01.945823Z","iopub.execute_input":"2023-04-28T08:23:01.946450Z","iopub.status.idle":"2023-04-28T08:24:18.439820Z","shell.execute_reply.started":"2023-04-28T08:23:01.946412Z","shell.execute_reply":"2023-04-28T08:24:18.438832Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Prediction","metadata":{}},{"cell_type":"code","source":"# # Load Model\n# model = load_model('/kaggle/working/driver_detection_Fine_Tuning_model.h5')\n# model.summary()  # As a reminder.","metadata":{"execution":{"iopub.status.busy":"2023-04-28T08:24:18.444078Z","iopub.execute_input":"2023-04-28T08:24:18.444955Z","iopub.status.idle":"2023-04-28T08:24:18.451467Z","shell.execute_reply.started":"2023-04-28T08:24:18.444916Z","shell.execute_reply":"2023-04-28T08:24:18.450552Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Predict\nPredict = model_copy.predict(test)","metadata":{"execution":{"iopub.status.busy":"2023-04-28T08:24:18.455888Z","iopub.execute_input":"2023-04-28T08:24:18.458478Z","iopub.status.idle":"2023-04-28T08:43:54.738320Z","shell.execute_reply.started":"2023-04-28T08:24:18.458438Z","shell.execute_reply":"2023-04-28T08:43:54.737188Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\n\nimg_names = []\nfor img_name in os.listdir('/kaggle/input/state-farm-distracted-driver-detection/imgs/test'):\n    img_names.append(img_name)\n\nimg_names = np.sort(img_names)","metadata":{"execution":{"iopub.status.busy":"2023-04-28T08:43:54.739894Z","iopub.execute_input":"2023-04-28T08:43:54.740283Z","iopub.status.idle":"2023-04-28T08:43:54.815371Z","shell.execute_reply.started":"2023-04-28T08:43:54.740243Z","shell.execute_reply":"2023-04-28T08:43:54.814416Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from PIL import Image\n\ntags = { 0: \"safe driving\",\n        1: \"texting - right\",\n        2: \"talking on the phone - right\",\n        3: \"texting - left\",\n        4: \"talking on the phone - left\",\n        5: \"operating the radio\",\n        6: \"drinking\",\n        7: \"reaching behind\",\n        8: \"hair and makeup\",\n        9: \"talking to passenger\" }\n\ndef load_images_and_labels(data_path):\n    x = []\n    i = 0\n    for img_name in img_names:\n\n        img = cv2.imread(data_path + '/' + img_name)\n        if img is not None:\n            img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)\n            img_array = Image.fromarray(img, 'RGB')\n            img_rs = img_array.resize((150, 150))\n            img_rs = np.array(img_rs)\n            x.append(img_rs)\n            i+=1\n        if i ==100:\n            break\n    return x","metadata":{"execution":{"iopub.status.busy":"2023-04-28T08:43:54.817009Z","iopub.execute_input":"2023-04-28T08:43:54.817436Z","iopub.status.idle":"2023-04-28T08:43:54.825176Z","shell.execute_reply.started":"2023-04-28T08:43:54.817399Z","shell.execute_reply":"2023-04-28T08:43:54.824070Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x = load_images_and_labels('/kaggle/input/state-farm-distracted-driver-detection/imgs/test')","metadata":{"execution":{"iopub.status.busy":"2023-04-28T08:43:54.826986Z","iopub.execute_input":"2023-04-28T08:43:54.827705Z","iopub.status.idle":"2023-04-28T08:43:55.853563Z","shell.execute_reply.started":"2023-04-28T08:43:54.827661Z","shell.execute_reply":"2023-04-28T08:43:55.852493Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import random\n\nplt.figure(figsize=(17, 7))\n\nfor i in range(0, 10, 1):\n#     ind = random.randint(0, 8)\n    plt.subplot(2, 5, i+1)\n    plt.imshow(x[i])\n    plt.title(tags[np.argmax(Predict[i])])","metadata":{"execution":{"iopub.status.busy":"2023-04-28T08:43:55.855178Z","iopub.execute_input":"2023-04-28T08:43:55.855563Z","iopub.status.idle":"2023-04-28T08:43:57.260413Z","shell.execute_reply.started":"2023-04-28T08:43:55.855526Z","shell.execute_reply":"2023-04-28T08:43:57.259207Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Predict[0]\nnp.argmax(Predict[0])","metadata":{"execution":{"iopub.status.busy":"2023-04-28T08:43:57.261975Z","iopub.execute_input":"2023-04-28T08:43:57.262338Z","iopub.status.idle":"2023-04-28T08:43:57.269031Z","shell.execute_reply.started":"2023-04-28T08:43:57.262305Z","shell.execute_reply":"2023-04-28T08:43:57.268170Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_sub = pd.read_csv(\"/kaggle/input/state-farm-distracted-driver-detection/sample_submission.csv\")\nsample_sub.head()","metadata":{"execution":{"iopub.status.busy":"2023-04-28T08:43:57.270299Z","iopub.execute_input":"2023-04-28T08:43:57.271246Z","iopub.status.idle":"2023-04-28T08:43:57.454381Z","shell.execute_reply.started":"2023-04-28T08:43:57.271196Z","shell.execute_reply":"2023-04-28T08:43:57.453398Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img = pd.DataFrame(img_names, columns = ['img'])\nimg.head(20)","metadata":{"execution":{"iopub.status.busy":"2023-04-28T08:43:57.455803Z","iopub.execute_input":"2023-04-28T08:43:57.456379Z","iopub.status.idle":"2023-04-28T08:43:57.475294Z","shell.execute_reply.started":"2023-04-28T08:43:57.456341Z","shell.execute_reply":"2023-04-28T08:43:57.474174Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Pred = pd.DataFrame(Predict, columns = ['c0','c1','c2','c3','c4','c5','c6','c7','c8','c9'])\nPred.head()","metadata":{"execution":{"iopub.status.busy":"2023-04-28T08:43:57.477080Z","iopub.execute_input":"2023-04-28T08:43:57.477774Z","iopub.status.idle":"2023-04-28T08:43:57.502081Z","shell.execute_reply.started":"2023-04-28T08:43:57.477737Z","shell.execute_reply":"2023-04-28T08:43:57.500919Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub = pd.concat((img, Pred), axis = 1)\nsub.head()","metadata":{"execution":{"iopub.status.busy":"2023-04-28T08:43:57.503506Z","iopub.execute_input":"2023-04-28T08:43:57.503932Z","iopub.status.idle":"2023-04-28T08:43:57.533025Z","shell.execute_reply.started":"2023-04-28T08:43:57.503896Z","shell.execute_reply":"2023-04-28T08:43:57.532155Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub.columns","metadata":{"execution":{"iopub.status.busy":"2023-04-28T08:43:57.534496Z","iopub.execute_input":"2023-04-28T08:43:57.534846Z","iopub.status.idle":"2023-04-28T08:43:57.541789Z","shell.execute_reply.started":"2023-04-28T08:43:57.534813Z","shell.execute_reply":"2023-04-28T08:43:57.540619Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub[['img', 'c0', 'c1', 'c2', 'c3', 'c4', 'c5', 'c6', 'c7', 'c8', 'c9']].to_csv(r'/kaggle/working/sub256.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2023-04-28T08:43:57.543542Z","iopub.execute_input":"2023-04-28T08:43:57.544366Z","iopub.status.idle":"2023-04-28T08:43:58.306007Z","shell.execute_reply.started":"2023-04-28T08:43:57.544320Z","shell.execute_reply":"2023-04-28T08:43:58.304913Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 11. <a name=\"11\">**Group names**</a>","metadata":{}},{"cell_type":"markdown","source":"## 1. Andrew Abd El-Messih Fakhry\n## 2. Asmaa Mohammed Mansour\n## 3. Farah Yousri Abdel meguid\n## 4. Marwan Sadek Abdo\n## 5. Zyad Samy Ahmed","metadata":{}}]}