{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.7.12","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"},{"sourceId":115285,"sourceType":"modelInstanceVersion","isSourceIdPinned":true,"modelInstanceId":96829,"modelId":121013},{"sourceId":118125,"sourceType":"modelInstanceVersion","isSourceIdPinned":true,"modelInstanceId":99333,"modelId":123500},{"sourceId":118126,"sourceType":"modelInstanceVersion","isSourceIdPinned":true,"modelInstanceId":99334,"modelId":123501}],"dockerImageVersionId":30445,"isInternetEnabled":false,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"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 git+https://github.com/jfilter/split-folders","metadata":{"id":"zbUkzYxrRLOR","outputId":"c7ecc6da-528e-435a-f1c7-005e6e2464ab","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#!pip install --upgrade  tensorflow==2.8.0","metadata":{"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\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, ConvNeXtTiny\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 Input\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":"2024-09-29T16:32:09.957938Z","iopub.execute_input":"2024-09-29T16:32:09.959916Z","iopub.status.idle":"2024-09-29T16:32:26.872004Z","shell.execute_reply.started":"2024-09-29T16:32:09.959858Z","shell.execute_reply":"2024-09-29T16:32:26.870700Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"keras.__version__","metadata":{"execution":{"iopub.status.busy":"2024-09-29T16:32:26.874043Z","iopub.execute_input":"2024-09-29T16:32:26.874742Z","iopub.status.idle":"2024-09-29T16:32:26.882958Z","shell.execute_reply.started":"2024-09-29T16:32:26.874707Z","shell.execute_reply":"2024-09-29T16:32:26.881825Z"},"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":"2024-09-21T17:45:02.970117Z","iopub.execute_input":"2024-09-21T17:45:02.971169Z","iopub.status.idle":"2024-09-21T17:45:03.035345Z","shell.execute_reply.started":"2024-09-21T17:45:02.971125Z","shell.execute_reply":"2024-09-21T17:45:03.034358Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.shape","metadata":{"execution":{"iopub.status.busy":"2024-09-21T17:45:05.187226Z","iopub.execute_input":"2024-09-21T17:45:05.188225Z","iopub.status.idle":"2024-09-21T17:45:05.194366Z","shell.execute_reply.started":"2024-09-21T17:45:05.188162Z","shell.execute_reply":"2024-09-21T17:45:05.193397Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.info()","metadata":{"execution":{"iopub.status.busy":"2024-09-21T17:45:07.016218Z","iopub.execute_input":"2024-09-21T17:45:07.016905Z","iopub.status.idle":"2024-09-21T17:45:07.044989Z","shell.execute_reply.started":"2024-09-21T17:45:07.016865Z","shell.execute_reply":"2024-09-21T17:45:07.043937Z"},"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":"import os\nimport shutil\nfrom sklearn.model_selection import train_test_split\n\nsource_dir = '/kaggle/input/state-farm-distracted-driver-detection/imgs/train' \ntrain_dir = '/kaggle/working/train_images'\ntest_dir = '/kaggle/working/test_images'\n\nos.makedirs(train_dir, exist_ok=True)\nos.makedirs(test_dir, exist_ok=True)\n\nfor class_name in os.listdir(source_dir):\n    class_dir = os.path.join(source_dir, class_name)\n    \n    if os.path.isdir(class_dir):\n        images = os.listdir(class_dir)\n        images = [img for img in images if os.path.isfile(os.path.join(class_dir, img))]\n        \n        train_images, test_images = train_test_split(images, test_size=0.2, random_state=42)\n\n        train_class_dir = os.path.join(train_dir, class_name)\n        test_class_dir = os.path.join(test_dir, class_name)\n        os.makedirs(train_class_dir, exist_ok=True)\n        os.makedirs(test_class_dir, exist_ok=True)\n\n        for img in train_images:\n            src_img_path = os.path.join(class_dir, img)\n            dst_img_path = os.path.join(train_class_dir, img)\n            shutil.copy2(src_img_path, dst_img_path)\n\n        for img in test_images:\n            src_img_path = os.path.join(class_dir, img)\n            dst_img_path = os.path.join(test_class_dir, img)\n            shutil.copy2(src_img_path, dst_img_path)\n\nprint(\"80-20 split completed successfully.\")","metadata":{"execution":{"iopub.status.busy":"2024-09-29T17:17:11.409265Z","iopub.execute_input":"2024-09-29T17:17:11.410184Z","iopub.status.idle":"2024-09-29T17:18:24.516547Z","shell.execute_reply.started":"2024-09-29T17:17:11.410146Z","shell.execute_reply":"2024-09-29T17:18:24.515403Z"},"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":"2024-09-29T17:18:44.796566Z","iopub.execute_input":"2024-09-29T17:18:44.797245Z","iopub.status.idle":"2024-09-29T17:18:45.972209Z","shell.execute_reply.started":"2024-09-29T17:18:44.797201Z","shell.execute_reply":"2024-09-29T17:18:45.971250Z"},"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/train_images', batch_size=128, target_size= (256, 256))\n\ndense_test_gen = ImageDataGenerator(rescale=1./255)\ndense_test = dense_test_gen.flow_from_directory('/kaggle/working/test_images', batch_size=128, target_size= (256, 256))","metadata":{"execution":{"iopub.status.busy":"2024-09-29T17:21:34.821139Z","iopub.execute_input":"2024-09-29T17:21:34.822100Z","iopub.status.idle":"2024-09-29T17:21:35.702647Z","shell.execute_reply.started":"2024-09-29T17:21:34.822057Z","shell.execute_reply":"2024-09-29T17:21:35.701762Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for batch_images, batch_labels in dense_train:\n    print(batch_labels)\n    break\n","metadata":{"execution":{"iopub.status.busy":"2024-09-29T17:22:55.828892Z","iopub.execute_input":"2024-09-29T17:22:55.829659Z","iopub.status.idle":"2024-09-29T17:22:56.501205Z","shell.execute_reply.started":"2024-09-29T17:22:55.829619Z","shell.execute_reply":"2024-09-29T17:22:56.500156Z"},"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":"2024-09-29T17:23:02.422704Z","iopub.execute_input":"2024-09-29T17:23:02.423470Z","iopub.status.idle":"2024-09-29T17:23:02.482345Z","shell.execute_reply.started":"2024-09-29T17:23:02.423433Z","shell.execute_reply":"2024-09-29T17:23:02.481549Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_dense.summary()","metadata":{"execution":{"iopub.status.busy":"2024-09-29T17:23:04.488735Z","iopub.execute_input":"2024-09-29T17:23:04.489911Z","iopub.status.idle":"2024-09-29T17:23:04.511419Z","shell.execute_reply.started":"2024-09-29T17:23:04.489867Z","shell.execute_reply":"2024-09-29T17:23:04.510468Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history_dense = model_dense.fit(dense_train, epochs=5)\n#10 epochs\n#Can make it 3 or 4 epochs itself","metadata":{"execution":{"iopub.status.busy":"2024-09-29T17:23:08.267616Z","iopub.execute_input":"2024-09-29T17:23:08.267988Z","iopub.status.idle":"2024-09-29T17:32:21.735048Z","shell.execute_reply.started":"2024-09-29T17:23:08.267955Z","shell.execute_reply":"2024-09-29T17:32:21.734042Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(history_dense.history['accuracy'], 'bo')\n#plt.plot(history_dense.history['val_accuracy'], 'b')\nplt.title('Dense Model Accuracy')\nplt.ylabel('accuracy')\nplt.xlabel('epoch')\nplt.legend(['train'], loc='upper left')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-09-29T17:33:08.274875Z","iopub.execute_input":"2024-09-29T17:33:08.275197Z","iopub.status.idle":"2024-09-29T17:33:08.510485Z","shell.execute_reply.started":"2024-09-29T17:33:08.275165Z","shell.execute_reply":"2024-09-29T17:33:08.509554Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(history_dense.history['loss'], 'bo')\n#plt.plot(history_dense.history['val_loss'], 'b')\nplt.title('Dense Model Loss')\nplt.ylabel('loss')\nplt.xlabel('epoch')\nplt.legend(['train'], loc='upper right')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-09-29T17:33:08.511792Z","iopub.execute_input":"2024-09-29T17:33:08.512088Z","iopub.status.idle":"2024-09-29T17:33:08.743334Z","shell.execute_reply.started":"2024-09-29T17:33:08.512059Z","shell.execute_reply":"2024-09-29T17:33:08.742333Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"loss, accuracy = model_dense.evaluate(dense_test)","metadata":{"execution":{"iopub.status.busy":"2024-09-29T17:32:39.470776Z","iopub.execute_input":"2024-09-29T17:32:39.471434Z","iopub.status.idle":"2024-09-29T17:33:08.272787Z","shell.execute_reply.started":"2024-09-29T17:32:39.471395Z","shell.execute_reply":"2024-09-29T17:33:08.271533Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\nfrom sklearn.metrics import confusion_matrix\nimport seaborn as sns\nimport matplotlib.pyplot as plt\n\ntrue_labels = dense_test.classes  \n\nclass_indices = dense_test.class_indices\nlabels = list(class_indices.keys())\n\npredictions = model_dense.predict(dense_test)\n\npredicted_labels = np.argmax(predictions, axis=1)\n\ncm = confusion_matrix(true_labels, predicted_labels)\n\nplt.figure(figsize=(10, 10))\nsns.heatmap(cm, annot=True, fmt=\"d\", xticklabels=labels, yticklabels=labels, cmap=\"Blues\")\nplt.xlabel('Predicted Labels')\nplt.ylabel('True Labels')\nplt.title('Confusion Matrix')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-09-29T17:33:47.555525Z","iopub.execute_input":"2024-09-29T17:33:47.556232Z","iopub.status.idle":"2024-09-29T17:34:17.072648Z","shell.execute_reply.started":"2024-09-29T17:33:47.556177Z","shell.execute_reply":"2024-09-29T17:34:17.071549Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"count = 0\nfor i in range(len(predicted_labels)):\n    if predicted_labels[i] == true_labels[i]:\n        count +=1\nacc = count/len(predicted_labels)\nacc","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_dense.save('/kaggle/working/dense_model.h5')","metadata":{"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(data_dir, batch_size=64, target_size= (256, 256))\n\n#val_gen = ImageDataGenerator(rescale=1./255)\n#val = val_gen.flow_from_directory('/kaggle/working/dataa/val', batch_size=64, target_size= (256, 256))","metadata":{"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":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_CNN.compile(optimizer='adam',\n                loss='categorical_crossentropy',\n                metrics=['acc'])","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from IPython.testing import test\nhistory_CNN=model_CNN.fit(train, epochs=5)\n#Can make it 2 epochs itself","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"acc = history_CNN.history['acc']\n#val_acc = history_CNN.history['val_acc']\n\nepochs = range(len(acc))\n\nplt.plot(epochs, acc, 'bo', label='Training accuracy')\n#plt.plot(epochs, val_acc, 'b', label='Validation accuracy')\nplt.title('Training')\nplt.legend()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"loss = history_CNN.history['loss']\n#val_loss = history_CNN.history['val_loss']\n\nplt.plot(epochs, loss, 'bo', label='Training loss')\n#plt.plot(epochs, val_loss, 'b', label='Validation loss')\nplt.title('Training')\nplt.legend()\nplt.show()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_CNN.save('/kaggle/working/CNN_model.h5')","metadata":{"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(data_dir, batch_size=batch_size, target_size = (256, 256))\n\n#val_gen = ImageDataGenerator(rescale = 1/255)\n#val = 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","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":{"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":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_DA.summary()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_DA.compile(optimizer='rmsprop',\n                loss='categorical_crossentropy',\n                metrics=['accuracy'])","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history_DA = model_DA.fit(\n      train,\n      epochs=5,\n      steps_per_epoch = 150,\n      verbose=1)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"acc = history_DA.history['accuracy']\n#val_acc = history_DA.history['val_accuracy']\n\nepochs = range(len(acc))\n\nplt.plot(epochs, acc, 'bo', label='Training accuracy')\n#plt.plot(epochs, val_acc, 'b', label='Validation accuracy')\nplt.title('Training accuracy')\nplt.legend()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"loss = history_DA.history['loss']\n#val_loss = history_DA.history['val_loss']\n\nplt.plot(epochs, loss, 'bo', label='Training loss')\n#plt.plot(epochs, val_loss, 'b', label='Validation loss')\nplt.title('Training loss')\nplt.legend()\n\nplt.show()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_DA.save(\"/kaggle/working/Data_Augmentation_model.h5\")","metadata":{"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(data_dir, batch_size=batch_size, target_size = (256, 256))\n\n\n#val_gen = ImageDataGenerator(preprocessing_function = preprocess_input)\n#val = val_gen.flow_from_directory('/kaggle/working/dataa/val', batch_size=batch_size, target_size = (256, 256))","metadata":{"execution":{"iopub.status.busy":"2024-09-28T02:05:24.572496Z","iopub.execute_input":"2024-09-28T02:05:24.573581Z","iopub.status.idle":"2024-09-28T02:05:56.152126Z","shell.execute_reply.started":"2024-09-28T02:05:24.573527Z","shell.execute_reply":"2024-09-28T02:05:56.151282Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Build NN","metadata":{}},{"cell_type":"code","source":"# Define the input shape\nimage_input = tf.keras.Input(shape=(256, 256, 3))\n#print(image_input)\n\nweights_path = '/kaggle/input/kerasconvnexttiny/keras/default/1/convnext_tiny_notop.h5'  # Update with the correct path\n\nconv_base = ConvNeXtTiny(\n    include_top=False,\n    include_preprocessing=False,\n    weights=weights_path,  # Use the local path to the downloaded weights\n    input_tensor=image_input,\n    pooling=max\n)\n\nmodel = tf.keras.Model(inputs=image_input, outputs=conv_base.output)\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2024-09-28T02:06:29.177428Z","iopub.execute_input":"2024-09-28T02:06:29.178251Z","iopub.status.idle":"2024-09-28T02:06:33.079066Z","shell.execute_reply.started":"2024-09-28T02:06:29.178214Z","shell.execute_reply":"2024-09-28T02:06:33.078006Z"},"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":"2024-09-28T02:06:48.681065Z","iopub.execute_input":"2024-09-28T02:06:48.682086Z","iopub.status.idle":"2024-09-28T02:06:49.781462Z","shell.execute_reply.started":"2024-09-28T02:06:48.682042Z","shell.execute_reply":"2024-09-28T02:06:49.780445Z"},"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":"2024-09-28T02:06:52.990647Z","iopub.execute_input":"2024-09-28T02:06:52.991037Z","iopub.status.idle":"2024-09-28T02:06:53.001932Z","shell.execute_reply.started":"2024-09-28T02:06:52.991001Z","shell.execute_reply":"2024-09-28T02:06:53.000687Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"conv_base.trainable = False","metadata":{"id":"UgovBcnZe8n4","execution":{"iopub.status.busy":"2024-09-28T02:06:54.493764Z","iopub.execute_input":"2024-09-28T02:06:54.494590Z","iopub.status.idle":"2024-09-28T02:06:54.506046Z","shell.execute_reply.started":"2024-09-28T02:06:54.494545Z","shell.execute_reply":"2024-09-28T02:06:54.504303Z"},"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":"2024-09-28T02:06:55.992524Z","iopub.execute_input":"2024-09-28T02:06:55.993604Z","iopub.status.idle":"2024-09-28T02:06:55.999067Z","shell.execute_reply.started":"2024-09-28T02:06:55.993548Z","shell.execute_reply":"2024-09-28T02:06:55.998029Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_DL.summary()","metadata":{"execution":{"iopub.status.busy":"2024-09-28T02:06:59.388480Z","iopub.execute_input":"2024-09-28T02:06:59.389384Z","iopub.status.idle":"2024-09-28T02:06:59.427189Z","shell.execute_reply.started":"2024-09-28T02:06:59.389346Z","shell.execute_reply":"2024-09-28T02:06:59.425495Z"},"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=5,\n                    steps_per_epoch = 150,\n                    verbose=1)","metadata":{"id":"p6p_li69fDAc","outputId":"654eabb7-6cc9-4604-81b5-f98376ebf905","execution":{"iopub.status.busy":"2024-09-28T02:07:09.785080Z","iopub.execute_input":"2024-09-28T02:07:09.785443Z","iopub.status.idle":"2024-09-28T02:16:20.112337Z","shell.execute_reply.started":"2024-09-28T02:07:09.785411Z","shell.execute_reply":"2024-09-28T02:16:20.111433Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#train_acc, train_loss = model_DL.evaluate(train)","metadata":{"execution":{"iopub.status.busy":"2024-09-21T17:59:05.015893Z","iopub.execute_input":"2024-09-21T17:59:05.016824Z","iopub.status.idle":"2024-09-21T18:06:35.557026Z","shell.execute_reply.started":"2024-09-21T17:59:05.016781Z","shell.execute_reply":"2024-09-21T18:06:35.556116Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#val_acc, val_loss = model_DL.evaluate(val)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"acc = history_DL.history['accuracy']\n#val_acc = history_DL.history['val_accuracy']\n\nepochs = range(len(acc))\n\nplt.plot(epochs, acc, 'bo', label='Training accuracy')\n#plt.plot(epochs, val_acc, 'b', label='Validation accuracy')\nplt.title('Training')\nplt.legend()","metadata":{"id":"jAsFeA6ckW8N","execution":{"iopub.status.busy":"2024-09-21T18:10:08.554047Z","iopub.execute_input":"2024-09-21T18:10:08.555068Z","iopub.status.idle":"2024-09-21T18:10:08.747416Z","shell.execute_reply.started":"2024-09-21T18:10:08.555026Z","shell.execute_reply":"2024-09-21T18:10:08.746350Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"loss = history_DL.history['loss']\n#val_loss = history_DL.history['val_loss']\n\nplt.plot(epochs, loss, 'bo', label='Training loss')\n#plt.plot(epochs, val_loss, 'b', label='Validation loss')\nplt.title('Trainingloss')\nplt.legend()\n\nplt.show()","metadata":{"id":"4PnAkL3LkWxC","execution":{"iopub.status.busy":"2024-09-21T18:10:10.885586Z","iopub.execute_input":"2024-09-21T18:10:10.886595Z","iopub.status.idle":"2024-09-21T18:10:11.052922Z","shell.execute_reply.started":"2024-09-21T18:10:10.886551Z","shell.execute_reply":"2024-09-21T18:10:11.051938Z"},"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":"2024-09-28T02:57:03.771833Z","iopub.execute_input":"2024-09-28T02:57:03.772315Z","iopub.status.idle":"2024-09-28T02:57:04.703118Z","shell.execute_reply.started":"2024-09-28T02:57:03.772272Z","shell.execute_reply":"2024-09-28T02:57:04.702003Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(image_input)","metadata":{"execution":{"iopub.status.busy":"2024-09-28T02:19:33.779802Z","iopub.execute_input":"2024-09-28T02:19:33.780671Z","iopub.status.idle":"2024-09-28T02:19:33.786225Z","shell.execute_reply.started":"2024-09-28T02:19:33.780631Z","shell.execute_reply":"2024-09-28T02:19:33.785033Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"weights_path = '/kaggle/input/resnet50/keras/default/1/resnet50_weights_tf_dim_ordering_tf_kernels_notop.h5'\n\nconv_base_one = ResNet50(weights=weights_path,\n                  include_top=False,\n                  input_shape=(256, 256, 3))\n\nmodel = tf.keras.Model(inputs=conv_base_one.input, outputs=conv_base_one.output)\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2024-09-28T02:21:37.204619Z","iopub.execute_input":"2024-09-28T02:21:37.205476Z","iopub.status.idle":"2024-09-28T02:21:40.502127Z","shell.execute_reply.started":"2024-09-28T02:21:37.205440Z","shell.execute_reply":"2024-09-28T02:21:40.501065Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_DV = models.Sequential()\nmodel_DV.add(conv_base)\nmodel_DV.add(layers.Flatten())\nmodel_DV.add(layers.Dense(512, activation='relu'))\nmodel_DV.add(layers.Dropout(0.35))\nmodel_DV.add(layers.Dense(128, activation='relu'))\nmodel_DV.add(layers.Dropout(0.35))\nmodel_DV.add(layers.Dense(32, activation='relu'))\nmodel_DV.add(layers.Dense(10, activation='softmax'))\n\nmodel_DV.summary()","metadata":{"execution":{"iopub.status.busy":"2024-09-28T02:22:05.018171Z","iopub.execute_input":"2024-09-28T02:22:05.018580Z","iopub.status.idle":"2024-09-28T02:22:06.082977Z","shell.execute_reply.started":"2024-09-28T02:22:05.018543Z","shell.execute_reply":"2024-09-28T02:22:06.082004Z"},"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_DV.trainable_weights))","metadata":{"execution":{"iopub.status.busy":"2024-09-28T02:22:09.234206Z","iopub.execute_input":"2024-09-28T02:22:09.234976Z","iopub.status.idle":"2024-09-28T02:22:09.240079Z","shell.execute_reply.started":"2024-09-28T02:22:09.234936Z","shell.execute_reply":"2024-09-28T02:22:09.239052Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"conv_base_one.trainable = False","metadata":{"execution":{"iopub.status.busy":"2024-09-28T02:22:10.381841Z","iopub.execute_input":"2024-09-28T02:22:10.382540Z","iopub.status.idle":"2024-09-28T02:22:10.393442Z","shell.execute_reply.started":"2024-09-28T02:22:10.382489Z","shell.execute_reply":"2024-09-28T02:22:10.392567Z"},"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":{"execution":{"iopub.status.busy":"2024-09-28T02:22:10.730710Z","iopub.execute_input":"2024-09-28T02:22:10.731588Z","iopub.status.idle":"2024-09-28T02:22:10.737111Z","shell.execute_reply.started":"2024-09-28T02:22:10.731539Z","shell.execute_reply":"2024-09-28T02:22:10.736071Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_DV.summary()","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_DV.compile(optimizer = optimizers.Adam(learning_rate=0.0001),\n             loss='categorical_crossentropy',\n              metrics=['accuracy'])\n\nhistory_DV = model_DV.fit(train,\n                    epochs=5,\n                    steps_per_epoch = 150,\n                    verbose=1)","metadata":{"execution":{"iopub.status.busy":"2024-09-28T02:22:13.910098Z","iopub.execute_input":"2024-09-28T02:22:13.910972Z","iopub.status.idle":"2024-09-28T02:30:18.829170Z","shell.execute_reply.started":"2024-09-28T02:22:13.910935Z","shell.execute_reply":"2024-09-28T02:30:18.828027Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#train_acc, train_loss = model_DV.evaluate(train)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"acc = history_DV.history['accuracy']\n#val_acc = history_DL.history['val_accuracy']\n\nepochs = range(len(acc))\n\nplt.plot(epochs, acc, 'bo', label='Training accuracy')\n#plt.plot(epochs, val_acc, 'b', label='Validation accuracy')\nplt.title('Training')\nplt.legend()","metadata":{"execution":{"iopub.status.busy":"2024-09-28T02:30:33.402926Z","iopub.execute_input":"2024-09-28T02:30:33.403322Z","iopub.status.idle":"2024-09-28T02:30:33.663621Z","shell.execute_reply.started":"2024-09-28T02:30:33.403285Z","shell.execute_reply":"2024-09-28T02:30:33.662575Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"loss = history_DV.history['loss']\n#val_loss = history_DL.history['val_loss']\n\nplt.plot(epochs, loss, 'bo', label='Training loss')\n#plt.plot(epochs, val_loss, 'b', label='Validation loss')\nplt.title('Trainingloss')\nplt.legend()\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-09-28T02:30:36.338243Z","iopub.execute_input":"2024-09-28T02:30:36.339297Z","iopub.status.idle":"2024-09-28T02:30:36.592383Z","shell.execute_reply.started":"2024-09-28T02:30:36.339255Z","shell.execute_reply":"2024-09-28T02:30:36.591226Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_DV.save('/kaggle/working/driver_detection_ResNet50_model.h5')\nprint(\"Saved model to kaggle\")","metadata":{"execution":{"iopub.status.busy":"2024-09-28T03:21:20.798431Z","iopub.execute_input":"2024-09-28T03:21:20.799415Z","iopub.status.idle":"2024-09-28T03:21:21.730226Z","shell.execute_reply.started":"2024-09-28T03:21:20.799355Z","shell.execute_reply":"2024-09-28T03:21:21.729184Z"},"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":"2024-09-28T02:31:41.032902Z","iopub.execute_input":"2024-09-28T02:31:41.033880Z","iopub.status.idle":"2024-09-28T02:31:41.465006Z","shell.execute_reply.started":"2024-09-28T02:31:41.033838Z","shell.execute_reply":"2024-09-28T02:31:41.463889Z"},"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":"2024-09-28T02:32:31.470920Z","iopub.execute_input":"2024-09-28T02:32:31.471810Z","iopub.status.idle":"2024-09-28T02:32:31.489889Z","shell.execute_reply.started":"2024-09-28T02:32:31.471772Z","shell.execute_reply":"2024-09-28T02:32:31.488939Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"conv_base.summary()","metadata":{"execution":{"iopub.status.busy":"2024-09-28T02:32:35.655858Z","iopub.execute_input":"2024-09-28T02:32:35.656890Z","iopub.status.idle":"2024-09-28T02:32:36.092475Z","shell.execute_reply.started":"2024-09-28T02:32:35.656846Z","shell.execute_reply":"2024-09-28T02:32:36.091419Z"},"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":"2024-09-28T02:32:59.624574Z","iopub.execute_input":"2024-09-28T02:32:59.625875Z","iopub.status.idle":"2024-09-28T02:32:59.734927Z","shell.execute_reply.started":"2024-09-28T02:32:59.625810Z","shell.execute_reply":"2024-09-28T02:32:59.733388Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_DL.summary()","metadata":{"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":{"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":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_acc_FT, train_loss_FT = model_copy.evaluate(train)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#val_acc_FT, val_loss_FT = model_copy.evaluate(val)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"acc = history_FT.history['accuracy']\n#val_acc = history_FT.history['val_accuracy']\n\nepochs = range(len(acc))\n\nplt.plot(epochs, acc, 'bo', label='Training accuracy')\n#plt.plot(epochs, val_acc, 'b', label='Validation accuracy')\nplt.title('Training')\nplt.legend()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"loss = history_FT.history['loss']\n#val_loss = history_FT.history['val_loss']\n\nplt.plot(epochs, loss, 'bo', label='Training loss')\n#plt.plot(epochs, val_loss, 'b', label='Validation loss')\nplt.title('Training')\nplt.legend()\n\nplt.show()","metadata":{"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')\n#plt.plot(epochs,\n         #smooth_curve(val_acc), 'b', label='Smoothed validation acc')\nplt.title('Training')\nplt.legend()\n\nplt.figure()\n\nplt.plot(epochs,\n         smooth_curve(loss), 'bo', label='Smoothed training loss')\n#plt.plot(epochs,\n         #smooth_curve(val_loss), 'b', label='Smoothed validation loss')\nplt.title('Training')\nplt.legend()\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-09-28T02:49:46.510675Z","iopub.execute_input":"2024-09-28T02:49:46.511659Z","iopub.status.idle":"2024-09-28T02:49:46.979406Z","shell.execute_reply.started":"2024-09-28T02:49:46.511619Z","shell.execute_reply":"2024-09-28T02:49:46.978338Z"},"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":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"'''# dense\nacc1=history_dense.history['accuracy'][-1]\n#vacc1=history_dense.history['val_accuracy'][-1]\nloss1=history_dense.history['loss'][-2]\n#vloss1=history_dense.history['val_loss'][-2]\n\n\n# CNN\nacc2=history_CNN.history['acc'][-1]\n#vacc2=history_CNN.history['val_acc'][-1]\nloss2=history_CNN.history['loss'][-2]\n#vloss2=history_CNN.history['val_loss'][-2]\n\n# Data Augmentation\nacc3=history_DA.history['accuracy'][-1]\n#vacc3=history_DA.history['val_accuracy'][-1]\nloss3=history_DA.history['loss'][-2]\n#vloss3=history_DA.history['val_loss'][-2]'''\n\n#ConvNeXtTiny\nacc4=history_DL.history['accuracy'][-1]\n#vacc4=history_DL.history['val_accuracy'][-1]\nloss4=history_DL.history['loss'][-2]\n#vloss4=history_DL.history['val_loss'][-2]\n\n#ResNet50\nacc5=history_DV.history['accuracy'][-1]\n#vacc5=history_DV.history['val_accuracy'][-1]\nloss5=history_DV.history['loss'][-2]\n#vloss5=history_DV.history['val_loss'][-2]","metadata":{"execution":{"iopub.status.busy":"2024-09-28T03:21:38.469753Z","iopub.execute_input":"2024-09-28T03:21:38.470540Z","iopub.status.idle":"2024-09-28T03:21:38.477079Z","shell.execute_reply.started":"2024-09-28T03:21:38.470478Z","shell.execute_reply":"2024-09-28T03:21:38.475842Z"},"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'])'''\n\nresults = pd.DataFrame([[\"Transfer Learning model_DL\",acc4*100,loss4],\n                       [\"Transfer Learning model_DV\",acc5*100,loss5]],\n                       columns = [\"Model\",\"Training Accuracy %\", 'Loss'])\nresults","metadata":{"execution":{"iopub.status.busy":"2024-09-28T03:21:40.839186Z","iopub.execute_input":"2024-09-28T03:21:40.840229Z","iopub.status.idle":"2024-09-28T03:21:40.853174Z","shell.execute_reply.started":"2024-09-28T03:21:40.840187Z","shell.execute_reply":"2024-09-28T03:21:40.852098Z"},"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":"2024-09-28T02:54:57.533823Z","iopub.execute_input":"2024-09-28T02:54:57.534587Z","iopub.status.idle":"2024-09-28T02:54:58.687607Z","shell.execute_reply.started":"2024-09-28T02:54:57.534549Z","shell.execute_reply":"2024-09-28T02:54:58.686621Z"},"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":"2024-09-28T02:55:10.380057Z","iopub.execute_input":"2024-09-28T02:55:10.380744Z","iopub.status.idle":"2024-09-28T02:57:03.769824Z","shell.execute_reply.started":"2024-09-28T02:55:10.380706Z","shell.execute_reply":"2024-09-28T02:57:03.768814Z"},"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":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Predict\nPredict = model_DL.predict(test)","metadata":{"execution":{"iopub.status.busy":"2024-09-28T03:22:27.051706Z","iopub.execute_input":"2024-09-28T03:22:27.052672Z","iopub.status.idle":"2024-09-28T03:40:35.841904Z","shell.execute_reply.started":"2024-09-28T03:22:27.052630Z","shell.execute_reply":"2024-09-28T03:40:35.840957Z"},"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":"2024-09-28T03:41:12.998204Z","iopub.execute_input":"2024-09-28T03:41:12.998721Z","iopub.status.idle":"2024-09-28T03:41:13.092226Z","shell.execute_reply.started":"2024-09-28T03:41:12.998683Z","shell.execute_reply":"2024-09-28T03:41:13.091371Z"},"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":"2024-09-28T03:41:17.232554Z","iopub.execute_input":"2024-09-28T03:41:17.233355Z","iopub.status.idle":"2024-09-28T03:41:17.241919Z","shell.execute_reply.started":"2024-09-28T03:41:17.233318Z","shell.execute_reply":"2024-09-28T03:41:17.240882Z"},"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":"2024-09-28T03:41:21.672722Z","iopub.execute_input":"2024-09-28T03:41:21.673618Z","iopub.status.idle":"2024-09-28T03:41:22.714866Z","shell.execute_reply.started":"2024-09-28T03:41:21.673578Z","shell.execute_reply":"2024-09-28T03:41:22.713952Z"},"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":"2024-09-28T03:41:26.321412Z","iopub.execute_input":"2024-09-28T03:41:26.322304Z","iopub.status.idle":"2024-09-28T03:41:27.788095Z","shell.execute_reply.started":"2024-09-28T03:41:26.322267Z","shell.execute_reply":"2024-09-28T03:41:27.787038Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Predict[0]\nnp.argmax(Predict[0])","metadata":{"execution":{"iopub.status.busy":"2024-09-28T03:41:33.719185Z","iopub.execute_input":"2024-09-28T03:41:33.719885Z","iopub.status.idle":"2024-09-28T03:41:33.726559Z","shell.execute_reply.started":"2024-09-28T03:41:33.719844Z","shell.execute_reply":"2024-09-28T03:41:33.725530Z"},"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":"2024-09-28T03:41:38.697903Z","iopub.execute_input":"2024-09-28T03:41:38.698299Z","iopub.status.idle":"2024-09-28T03:41:38.894356Z","shell.execute_reply.started":"2024-09-28T03:41:38.698263Z","shell.execute_reply":"2024-09-28T03:41:38.893345Z"},"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":"2024-09-28T03:41:40.904199Z","iopub.execute_input":"2024-09-28T03:41:40.904895Z","iopub.status.idle":"2024-09-28T03:41:40.923686Z","shell.execute_reply.started":"2024-09-28T03:41:40.904856Z","shell.execute_reply":"2024-09-28T03:41:40.922643Z"},"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":"2024-09-28T03:41:44.440033Z","iopub.execute_input":"2024-09-28T03:41:44.440677Z","iopub.status.idle":"2024-09-28T03:41:44.457889Z","shell.execute_reply.started":"2024-09-28T03:41:44.440639Z","shell.execute_reply":"2024-09-28T03:41:44.456756Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub = pd.concat((img, Pred), axis = 1)\nsub.head()","metadata":{"execution":{"iopub.status.busy":"2024-09-28T03:41:47.388036Z","iopub.execute_input":"2024-09-28T03:41:47.388976Z","iopub.status.idle":"2024-09-28T03:41:47.409076Z","shell.execute_reply.started":"2024-09-28T03:41:47.388935Z","shell.execute_reply":"2024-09-28T03:41:47.408105Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub.columns","metadata":{"execution":{"iopub.status.busy":"2024-09-28T03:41:49.607762Z","iopub.execute_input":"2024-09-28T03:41:49.608680Z","iopub.status.idle":"2024-09-28T03:41:49.615245Z","shell.execute_reply.started":"2024-09-28T03:41:49.608639Z","shell.execute_reply":"2024-09-28T03:41:49.614244Z"},"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":"2024-09-28T03:41:52.853374Z","iopub.execute_input":"2024-09-28T03:41:52.854403Z","iopub.status.idle":"2024-09-28T03:41:53.982869Z","shell.execute_reply.started":"2024-09-28T03:41:52.854362Z","shell.execute_reply":"2024-09-28T03:41:53.981918Z"},"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":{}}]}