{"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":"# **Imports**","metadata":{}},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport os\nfrom PIL import Image\nimport matplotlib.pyplot as plt\n\nimport splitfolders\n\nimport tensorflow as tf\nimport tensorflow.keras as keras\n\nfrom tensorflow.keras import models\nfrom tensorflow.keras import layers\nfrom tensorflow.keras import optimizers\n\nfrom tensorflow.keras.layers import Conv2D, MaxPooling2D, Flatten, Dense, Dropout\nfrom tensorflow.keras.applications import VGG16\nfrom keras.applications.imagenet_utils import preprocess_input\n\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\n\nfrom keras.models import load_model","metadata":{"execution":{"iopub.status.busy":"2023-06-10T15:00:52.328188Z","iopub.execute_input":"2023-06-10T15:00:52.328554Z","iopub.status.idle":"2023-06-10T15:00:52.336786Z","shell.execute_reply.started":"2023-06-10T15:00:52.328525Z","shell.execute_reply":"2023-06-10T15:00:52.335820Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# All the train data (before splitting)\ntrain_data_path = \"/kaggle/input/state-farm-distracted-driver-detection/imgs/train\"","metadata":{"execution":{"iopub.status.busy":"2023-06-10T06:37:35.615118Z","iopub.execute_input":"2023-06-10T06:37:35.615674Z","iopub.status.idle":"2023-06-10T06:37:35.620243Z","shell.execute_reply.started":"2023-06-10T06:37:35.615636Z","shell.execute_reply":"2023-06-10T06:37:35.619214Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **1. Visualization**","metadata":{}},{"cell_type":"code","source":"label_dict = {\n    'c0': 'Safe driving',\n    'c1': 'Texting - right',\n    'c2': 'Talking on the phone - right',\n    'c3': 'Texting - left',\n    'c4': 'Talking on the phone - left',\n    'c5': 'Operating the radio',\n    'c6': 'Drinking',\n    'c7': 'Reaching behind',\n    'c8': 'Hair and makeup',\n    'c9': 'Talking to passenger'\n}","metadata":{"execution":{"iopub.status.busy":"2023-06-10T07:02:59.015908Z","iopub.execute_input":"2023-06-10T07:02:59.016306Z","iopub.status.idle":"2023-06-10T07:02:59.022688Z","shell.execute_reply.started":"2023-06-10T07:02:59.016273Z","shell.execute_reply":"2023-06-10T07:02:59.021411Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Plot one image from each class\nfig, axs = plt.subplots(2, 5, figsize = (30, 15))\n\nfor i, cls in enumerate(label_dict.keys()):\n    # Get the first image from the class folder\n    img_path = os.path.join(train_data_path, cls, os.listdir(os.path.join(train_data_path, cls))[0])\n    img = Image.open(img_path)\n    # Plot the image\n    axs[i//5, i%5].imshow(img)\n    axs[i//5, i%5].axis('off')\n    axs[i//5, i%5].set_title(f'{cls}: {label_dict[cls]}', fontsize = 25, pad = 20)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-06-10T07:06:11.555228Z","iopub.execute_input":"2023-06-10T07:06:11.555920Z","iopub.status.idle":"2023-06-10T07:06:13.555346Z","shell.execute_reply.started":"2023-06-10T07:06:11.555889Z","shell.execute_reply":"2023-06-10T07:06:13.554285Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **2. Data Splitting**","metadata":{}},{"cell_type":"code","source":"splitfolders.ratio(train_data_path, output = \"data\",seed = 1337,\n                   ratio = (0.8, 0.2), group_prefix = None, move = False)","metadata":{"execution":{"iopub.status.busy":"2023-06-10T07:22:44.127188Z","iopub.execute_input":"2023-06-10T07:22:44.127579Z","iopub.status.idle":"2023-06-10T07:26:16.648826Z","shell.execute_reply.started":"2023-06-10T07:22:44.127547Z","shell.execute_reply":"2023-06-10T07:26:16.647853Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = \"/kaggle/working/data/train\"\nvalidation = \"/kaggle/working/data/val\"","metadata":{"execution":{"iopub.status.busy":"2023-06-10T07:29:00.715237Z","iopub.execute_input":"2023-06-10T07:29:00.715669Z","iopub.status.idle":"2023-06-10T07:29:00.720138Z","shell.execute_reply.started":"2023-06-10T07:29:00.715636Z","shell.execute_reply":"2023-06-10T07:29:00.719227Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **3. Baseline Dense Layers Model**","metadata":{}},{"cell_type":"markdown","source":"* ### **ETL Pipeline**","metadata":{}},{"cell_type":"code","source":"batch_size = 128\ntarget_size = (256, 256)\n\ntrain_datagen = ImageDataGenerator(rescale = 1./255)\nval_datagen = ImageDataGenerator(rescale = 1./255)\n\ntrain_generator = train_datagen.flow_from_directory(\n        train,\n        batch_size = batch_size,\n        target_size = target_size,\n)\n\nvalidation_generator = val_datagen.flow_from_directory(\n        validation,\n        batch_size = batch_size,\n        target_size = target_size\n)","metadata":{"execution":{"iopub.status.busy":"2023-06-10T07:54:56.844300Z","iopub.execute_input":"2023-06-10T07:54:56.844796Z","iopub.status.idle":"2023-06-10T07:54:57.454917Z","shell.execute_reply.started":"2023-06-10T07:54:56.844766Z","shell.execute_reply":"2023-06-10T07:54:57.453931Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"* ### **Dense Layers Model** ","metadata":{}},{"cell_type":"code","source":"dense_model = models.Sequential()\n\ndense_model.add(Flatten(input_shape=(256, 256, 3)))\ndense_model.add(layers.Dense(512, activation='relu', name='Layer_1'))\ndense_model.add(layers.Dense(256, activation='relu', name='Layer_2'))\ndense_model.add(layers.Dense(128, activation='relu', name='Layer_3'))\n\ndense_model.add(layers.Dense(10, activation='softmax'))","metadata":{"execution":{"iopub.status.busy":"2023-06-10T08:11:17.414281Z","iopub.execute_input":"2023-06-10T08:11:17.414885Z","iopub.status.idle":"2023-06-10T08:11:17.536629Z","shell.execute_reply.started":"2023-06-10T08:11:17.414841Z","shell.execute_reply":"2023-06-10T08:11:17.535713Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dense_model.summary()","metadata":{"execution":{"iopub.status.busy":"2023-06-10T08:11:25.495367Z","iopub.execute_input":"2023-06-10T08:11:25.495747Z","iopub.status.idle":"2023-06-10T08:11:25.518366Z","shell.execute_reply.started":"2023-06-10T08:11:25.495719Z","shell.execute_reply":"2023-06-10T08:11:25.517608Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dense_model.compile(\n                optimizer='adam',\n                loss='categorical_crossentropy',\n                metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2023-06-10T08:12:28.153680Z","iopub.execute_input":"2023-06-10T08:12:28.154061Z","iopub.status.idle":"2023-06-10T08:12:28.175922Z","shell.execute_reply.started":"2023-06-10T08:12:28.154031Z","shell.execute_reply":"2023-06-10T08:12:28.175023Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dense_history = dense_model.fit(\n                              train_generator,\n                              epochs=5,\n                              validation_data=validation_generator,\n                            )","metadata":{"execution":{"iopub.status.busy":"2023-06-10T08:34:32.564992Z","iopub.execute_input":"2023-06-10T08:34:32.566051Z","iopub.status.idle":"2023-06-10T08:43:40.292792Z","shell.execute_reply.started":"2023-06-10T08:34:32.566014Z","shell.execute_reply":"2023-06-10T08:43:40.291732Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### **Let's plot the loss and accuracy of the model over the training and validation data:**","metadata":{}},{"cell_type":"code","source":"acc = dense_history.history['accuracy']\nval_acc = dense_history.history['val_accuracy']\nloss = dense_history.history['loss']\nval_loss = dense_history.history['val_loss']\n\nepochs = range(len(acc))\n\nplt.plot(epochs, acc, 'bo', label='Training acc')\nplt.plot(epochs, val_acc, 'b', label='Validation acc')\nplt.title(\"Dense's Training and validation accuracy\")\nplt.legend()\n\nplt.figure()\n\nplt.plot(epochs, loss, 'bo', label='Training loss')\nplt.plot(epochs, val_loss, 'b', label='Validation loss')\nplt.title(\"Dense's Training and validation loss\")\nplt.legend()\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-06-10T08:43:54.199711Z","iopub.execute_input":"2023-06-10T08:43:54.200092Z","iopub.status.idle":"2023-06-10T08:43:54.835125Z","shell.execute_reply.started":"2023-06-10T08:43:54.200061Z","shell.execute_reply":"2023-06-10T08:43:54.834131Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dense_model.save('/kaggle/working/models/dense_model.h5')","metadata":{"execution":{"iopub.status.busy":"2023-06-10T08:44:15.175064Z","iopub.execute_input":"2023-06-10T08:44:15.175450Z","iopub.status.idle":"2023-06-10T08:44:17.701827Z","shell.execute_reply.started":"2023-06-10T08:44:15.175420Z","shell.execute_reply":"2023-06-10T08:44:17.700087Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **4. Baseline CNN Model**","metadata":{}},{"cell_type":"markdown","source":"* ### **ETL Pipeline**","metadata":{}},{"cell_type":"code","source":"batch_size = 128\ntarget_size = (256, 256)\n\ntrain_datagen = ImageDataGenerator(rescale = 1./255)\nval_datagen = ImageDataGenerator(rescale = 1./255)\n\ntrain_generator = train_datagen.flow_from_directory(\n        train,\n        batch_size = batch_size,\n        target_size = target_size,\n)\n\nvalidation_generator = val_datagen.flow_from_directory(\n        validation,\n        batch_size = batch_size,\n        target_size = target_size\n)","metadata":{"execution":{"iopub.status.busy":"2023-06-10T08:46:02.795797Z","iopub.execute_input":"2023-06-10T08:46:02.796229Z","iopub.status.idle":"2023-06-10T08:46:03.411200Z","shell.execute_reply.started":"2023-06-10T08:46:02.796191Z","shell.execute_reply":"2023-06-10T08:46:03.410125Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"* ### **CNN Model**","metadata":{}},{"cell_type":"code","source":"cnn_model = models.Sequential()\n\ncnn_model.add(layers.Conv2D(32, (3, 3), activation='relu', input_shape=(256, 256, 3)))\ncnn_model.add(layers.MaxPooling2D((2, 2)))\n\ncnn_model.add(layers.Conv2D(64, (3, 3), activation='relu'))\ncnn_model.add(layers.MaxPooling2D((2, 2)))\n\ncnn_model.add(layers.Conv2D(64, (3, 3), activation='relu'))\ncnn_model.add(layers.MaxPooling2D((2, 2)))\n\ncnn_model.add(layers.Flatten())\n\ncnn_model.add(layers.Dense(256, activation='relu'))\ncnn_model.add(layers.Dense(10, activation='softmax'))","metadata":{"execution":{"iopub.status.busy":"2023-06-10T08:46:29.925254Z","iopub.execute_input":"2023-06-10T08:46:29.925658Z","iopub.status.idle":"2023-06-10T08:46:30.011314Z","shell.execute_reply.started":"2023-06-10T08:46:29.925624Z","shell.execute_reply":"2023-06-10T08:46:30.010326Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cnn_model.summary()","metadata":{"execution":{"iopub.status.busy":"2023-06-10T08:46:31.224137Z","iopub.execute_input":"2023-06-10T08:46:31.224542Z","iopub.status.idle":"2023-06-10T08:46:31.256132Z","shell.execute_reply.started":"2023-06-10T08:46:31.224511Z","shell.execute_reply":"2023-06-10T08:46:31.255383Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cnn_model.compile(\n                optimizer='adam',\n                loss='categorical_crossentropy',\n                metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2023-06-10T08:46:39.162982Z","iopub.execute_input":"2023-06-10T08:46:39.163370Z","iopub.status.idle":"2023-06-10T08:46:39.177720Z","shell.execute_reply.started":"2023-06-10T08:46:39.163339Z","shell.execute_reply":"2023-06-10T08:46:39.176577Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cnn_history = cnn_model.fit(\n                              train_generator,\n                              epochs=5,\n                              validation_data=validation_generator,\n                            )","metadata":{"execution":{"iopub.status.busy":"2023-06-10T08:46:46.588044Z","iopub.execute_input":"2023-06-10T08:46:46.589008Z","iopub.status.idle":"2023-06-10T08:57:13.052762Z","shell.execute_reply.started":"2023-06-10T08:46:46.588963Z","shell.execute_reply":"2023-06-10T08:57:13.051664Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### **Let's plot the loss and accuracy of the model over the training and validation data:**","metadata":{}},{"cell_type":"code","source":"acc = cnn_history.history['accuracy']\nval_acc = cnn_history.history['val_accuracy']\nloss = cnn_history.history['loss']\nval_loss = cnn_history.history['val_loss']\n\nepochs = range(len(acc))\n\nplt.plot(epochs, acc, 'bo', label='Training acc')\nplt.plot(epochs, val_acc, 'b', label='Validation acc')\nplt.title(\"CNN's Training and validation accuracy\")\nplt.legend()\n\nplt.figure()\n\nplt.plot(epochs, loss, 'bo', label='Training loss')\nplt.plot(epochs, val_loss, 'b', label='Validation loss')\nplt.title(\"CNN's Training and validation loss\")\nplt.legend()\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-06-10T08:57:24.029979Z","iopub.execute_input":"2023-06-10T08:57:24.030396Z","iopub.status.idle":"2023-06-10T08:57:24.639005Z","shell.execute_reply.started":"2023-06-10T08:57:24.030354Z","shell.execute_reply":"2023-06-10T08:57:24.638069Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cnn_model.save('/kaggle/working/models/cnn_model.h5')","metadata":{"execution":{"iopub.status.busy":"2023-06-10T08:57:54.464126Z","iopub.execute_input":"2023-06-10T08:57:54.464540Z","iopub.status.idle":"2023-06-10T08:57:54.886319Z","shell.execute_reply.started":"2023-06-10T08:57:54.464508Z","shell.execute_reply":"2023-06-10T08:57:54.885285Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **5. Data Augmentation**","metadata":{}},{"cell_type":"markdown","source":"* ### **ETL Pipeline**","metadata":{}},{"cell_type":"code","source":"batch_size = 128\ntarget_size = (256, 256)\n\ntrain_datagen = ImageDataGenerator(rescale = 1./255,\n                                   rotation_range = 20,\n                                   width_shift_range = 0.15,\n                                   height_shift_range = 0.15,\n                                   shear_range = 0.1,\n                                   zoom_range = 0.2,\n                                   fill_mode ='nearest',\n )\n\n\nval_datagen = ImageDataGenerator(rescale = 1./255)\n\ntrain_generator = train_datagen.flow_from_directory(\n        train,\n        batch_size = batch_size,\n        target_size = target_size,\n)\n\nvalidation_generator = val_datagen.flow_from_directory(\n        validation,\n        batch_size = batch_size,\n        target_size = target_size\n)","metadata":{"execution":{"iopub.status.busy":"2023-06-10T08:58:42.725561Z","iopub.execute_input":"2023-06-10T08:58:42.725964Z","iopub.status.idle":"2023-06-10T08:58:43.395484Z","shell.execute_reply.started":"2023-06-10T08:58:42.725929Z","shell.execute_reply":"2023-06-10T08:58:43.394488Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"* ### **CNN Model**","metadata":{}},{"cell_type":"code","source":"cnn_aug_model = models.Sequential()\n\ncnn_aug_model.add(layers.Conv2D(32, (3, 3), activation='relu', input_shape=(256, 256, 3)))\ncnn_aug_model.add(layers.MaxPooling2D((2, 2)))\n\ncnn_aug_model.add(layers.Conv2D(64, (3, 3), activation='relu'))\ncnn_aug_model.add(layers.MaxPooling2D((2, 2)))\n\ncnn_aug_model.add(layers.Conv2D(64, (3, 3), activation='relu'))\ncnn_aug_model.add(layers.MaxPooling2D((2, 2)))\n\ncnn_aug_model.add(layers.Conv2D(128, (3, 3), activation='relu'))\ncnn_aug_model.add(layers.MaxPooling2D((2, 2)))\n\ncnn_aug_model.add(layers.Flatten())\n\ncnn_aug_model.add(layers.Dense(256, activation='relu'))\ncnn_aug_model.add(layers.Dropout(0.5))\ncnn_aug_model.add(layers.Dense(10, activation='softmax'))","metadata":{"execution":{"iopub.status.busy":"2023-06-10T09:39:26.639616Z","iopub.execute_input":"2023-06-10T09:39:26.639987Z","iopub.status.idle":"2023-06-10T09:39:26.735991Z","shell.execute_reply.started":"2023-06-10T09:39:26.639957Z","shell.execute_reply":"2023-06-10T09:39:26.735061Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cnn_aug_model.summary()","metadata":{"execution":{"iopub.status.busy":"2023-06-10T09:39:27.804871Z","iopub.execute_input":"2023-06-10T09:39:27.805255Z","iopub.status.idle":"2023-06-10T09:39:27.840058Z","shell.execute_reply.started":"2023-06-10T09:39:27.805224Z","shell.execute_reply":"2023-06-10T09:39:27.839330Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cnn_aug_model.compile(\n                optimizer='adam',\n                loss='categorical_crossentropy',\n                metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2023-06-10T09:39:31.879098Z","iopub.execute_input":"2023-06-10T09:39:31.880069Z","iopub.status.idle":"2023-06-10T09:39:31.893909Z","shell.execute_reply.started":"2023-06-10T09:39:31.880035Z","shell.execute_reply":"2023-06-10T09:39:31.892839Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cnn_aug_history = cnn_aug_model.fit(\n                              train_generator,\n                              epochs=10,\n                              validation_data=validation_generator,\n                            )","metadata":{"execution":{"iopub.status.busy":"2023-06-10T09:39:32.594996Z","iopub.execute_input":"2023-06-10T09:39:32.595362Z","iopub.status.idle":"2023-06-10T10:32:57.051866Z","shell.execute_reply.started":"2023-06-10T09:39:32.595333Z","shell.execute_reply":"2023-06-10T10:32:57.050807Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### **Let's plot the loss and accuracy of the model over the training and validation data:**","metadata":{}},{"cell_type":"code","source":"acc = cnn_aug_history.history['accuracy']\nval_acc = cnn_aug_history.history['val_accuracy']\nloss = cnn_aug_history.history['loss']\nval_loss = cnn_aug_history.history['val_loss']\n\nepochs = range(len(acc))\n\nplt.plot(epochs, acc, 'bo', label='Training acc')\nplt.plot(epochs, val_acc, 'b', label='Validation acc')\nplt.title(\"CNN's Training and validation accuracy\")\nplt.legend()\n\nplt.figure()\n\nplt.plot(epochs, loss, 'bo', label='Training loss')\nplt.plot(epochs, val_loss, 'b', label='Validation loss')\nplt.title(\"CNN's Training and validation loss\")\nplt.legend()\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-06-10T10:34:39.715262Z","iopub.execute_input":"2023-06-10T10:34:39.715652Z","iopub.status.idle":"2023-06-10T10:34:40.323277Z","shell.execute_reply.started":"2023-06-10T10:34:39.715620Z","shell.execute_reply":"2023-06-10T10:34:40.322167Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cnn_aug_model.save('/kaggle/working/models/cnn_aug_model.h5')","metadata":{"execution":{"iopub.status.busy":"2023-06-10T10:34:58.459692Z","iopub.execute_input":"2023-06-10T10:34:58.460051Z","iopub.status.idle":"2023-06-10T10:34:58.637102Z","shell.execute_reply.started":"2023-06-10T10:34:58.460022Z","shell.execute_reply":"2023-06-10T10:34:58.636110Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **6. Transfer Learning**","metadata":{}},{"cell_type":"markdown","source":"* ### **Scenario 1: ConvNet as fixed feature extractor**","metadata":{}},{"cell_type":"markdown","source":"  *  ### **ETL Pipeline**","metadata":{}},{"cell_type":"code","source":"batch_size = 128\ntarget_size = (256, 256)\n\ntrain_datagen = ImageDataGenerator(\n                                   preprocessing_function = preprocess_input\n )\n\n\nval_datagen = ImageDataGenerator(preprocessing_function = preprocess_input)\n\ntrain_generator = train_datagen.flow_from_directory(\n        train,\n        batch_size = batch_size,\n        target_size = target_size,\n)\n\nvalidation_generator = val_datagen.flow_from_directory(\n        validation,\n        batch_size = batch_size,\n        target_size = target_size\n)","metadata":{"execution":{"iopub.status.busy":"2023-06-10T14:15:25.163638Z","iopub.execute_input":"2023-06-10T14:15:25.164018Z","iopub.status.idle":"2023-06-10T14:15:25.755911Z","shell.execute_reply.started":"2023-06-10T14:15:25.163989Z","shell.execute_reply":"2023-06-10T14:15:25.754971Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"* ### **Pre-trained Model (VGG16)**","metadata":{}},{"cell_type":"code","source":"conv_base = VGG16(weights='imagenet',\n                  include_top=False,\n                  input_shape=(256, 256,3))","metadata":{"execution":{"iopub.status.busy":"2023-06-10T14:15:29.322183Z","iopub.execute_input":"2023-06-10T14:15:29.322538Z","iopub.status.idle":"2023-06-10T14:15:29.653803Z","shell.execute_reply.started":"2023-06-10T14:15:29.322510Z","shell.execute_reply":"2023-06-10T14:15:29.652800Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = models.Sequential()\nmodel.add(conv_base)\nmodel.add(layers.Flatten())\nmodel.add(layers.Dense(512, activation='relu'))\nmodel.add(layers.Dropout(0.5))\nmodel.add(layers.Dense(128, activation='relu'))\nmodel.add(layers.Dropout(0.5))\nmodel.add(layers.Dense(10, activation='softmax'))","metadata":{"execution":{"iopub.status.busy":"2023-06-10T14:15:29.902182Z","iopub.execute_input":"2023-06-10T14:15:29.904442Z","iopub.status.idle":"2023-06-10T14:15:30.011473Z","shell.execute_reply.started":"2023-06-10T14:15:29.904400Z","shell.execute_reply":"2023-06-10T14:15:30.010428Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2023-06-10T14:15:31.467888Z","iopub.execute_input":"2023-06-10T14:15:31.468980Z","iopub.status.idle":"2023-06-10T14:15:31.501700Z","shell.execute_reply.started":"2023-06-10T14:15:31.468938Z","shell.execute_reply":"2023-06-10T14:15:31.500925Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"conv_base.trainable = False","metadata":{"execution":{"iopub.status.busy":"2023-06-10T14:15:34.087788Z","iopub.execute_input":"2023-06-10T14:15:34.088136Z","iopub.status.idle":"2023-06-10T14:15:34.093044Z","shell.execute_reply.started":"2023-06-10T14:15:34.088107Z","shell.execute_reply":"2023-06-10T14:15:34.092133Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2023-06-10T14:15:34.387290Z","iopub.execute_input":"2023-06-10T14:15:34.387584Z","iopub.status.idle":"2023-06-10T14:15:34.414746Z","shell.execute_reply.started":"2023-06-10T14:15:34.387559Z","shell.execute_reply":"2023-06-10T14:15:34.414048Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(loss='categorical_crossentropy',\n              optimizer = optimizers.Adam(learning_rate=0.0001),\n              metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2023-06-10T14:15:37.433107Z","iopub.execute_input":"2023-06-10T14:15:37.433694Z","iopub.status.idle":"2023-06-10T14:15:37.454970Z","shell.execute_reply.started":"2023-06-10T14:15:37.433645Z","shell.execute_reply":"2023-06-10T14:15:37.453952Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model.fit(\n      train_generator,\n      epochs=10,\n      validation_data=validation_generator)","metadata":{"execution":{"iopub.status.busy":"2023-06-10T14:15:40.032230Z","iopub.execute_input":"2023-06-10T14:15:40.033368Z","iopub.status.idle":"2023-06-10T14:39:07.428778Z","shell.execute_reply.started":"2023-06-10T14:15:40.033324Z","shell.execute_reply":"2023-06-10T14:39:07.427667Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### **Let's plot the loss and accuracy of the model over the training and validation data:**","metadata":{}},{"cell_type":"code","source":"acc = history.history['accuracy']\nval_acc = history.history['val_accuracy']\nloss = history.history['loss']\nval_loss = history.history['val_loss']\n\nepochs = range(len(acc))\n\nplt.plot(epochs, acc, 'bo', label='Training acc')\nplt.plot(epochs, val_acc, 'b', label='Validation acc')\nplt.title('Training and validation accuracy')\nplt.legend()\n\nplt.figure()\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-06-10T14:39:38.735188Z","iopub.execute_input":"2023-06-10T14:39:38.736319Z","iopub.status.idle":"2023-06-10T14:39:39.300356Z","shell.execute_reply.started":"2023-06-10T14:39:38.736275Z","shell.execute_reply":"2023-06-10T14:39:39.299383Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.save('/kaggle/working/models/fixed_model.h5')","metadata":{"execution":{"iopub.status.busy":"2023-06-10T14:40:00.282865Z","iopub.execute_input":"2023-06-10T14:40:00.283256Z","iopub.status.idle":"2023-06-10T14:40:00.816020Z","shell.execute_reply.started":"2023-06-10T14:40:00.283224Z","shell.execute_reply":"2023-06-10T14:40:00.815033Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"* ### **Scenario 2: Fine-Tuning the ConvNet**","metadata":{}},{"cell_type":"code","source":"conv_base.summary()","metadata":{"execution":{"iopub.status.busy":"2023-06-10T13:32:13.868605Z","iopub.execute_input":"2023-06-10T13:32:13.868988Z","iopub.status.idle":"2023-06-10T13:32:13.912859Z","shell.execute_reply.started":"2023-06-10T13:32:13.868958Z","shell.execute_reply":"2023-06-10T13:32:13.912064Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fine_tuned_model = models.Sequential()\nfine_tuned_model.add(conv_base)\nfine_tuned_model.add(layers.Flatten())\nfine_tuned_model.add(layers.Dense(256, activation='relu'))\nfine_tuned_model.add(layers.Dropout(0.5))\nfine_tuned_model.add(layers.Dense(10, activation='softmax'))","metadata":{"execution":{"iopub.status.busy":"2023-06-10T13:32:52.497663Z","iopub.execute_input":"2023-06-10T13:32:52.498030Z","iopub.status.idle":"2023-06-10T13:32:52.596303Z","shell.execute_reply.started":"2023-06-10T13:32:52.498001Z","shell.execute_reply":"2023-06-10T13:32:52.595320Z"},"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 == 'block5_conv3':\n        set_trainable = True\n    if set_trainable:\n        layer.trainable = True\n    else:\n        layer.trainable = False","metadata":{"execution":{"iopub.status.busy":"2023-06-10T13:32:54.999062Z","iopub.execute_input":"2023-06-10T13:32:54.999452Z","iopub.status.idle":"2023-06-10T13:32:55.009606Z","shell.execute_reply.started":"2023-06-10T13:32:54.999423Z","shell.execute_reply":"2023-06-10T13:32:55.008237Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fine_tuned_model.compile(\n              loss='categorical_crossentropy',\n              optimizer=\"adam\",\n              metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2023-06-10T13:33:06.928113Z","iopub.execute_input":"2023-06-10T13:33:06.929069Z","iopub.status.idle":"2023-06-10T13:33:06.943334Z","shell.execute_reply.started":"2023-06-10T13:33:06.929036Z","shell.execute_reply":"2023-06-10T13:33:06.942211Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fine_tuned_history = fine_tuned_model.fit(\n      train_generator,\n      epochs=5,\n      validation_data=validation_generator)","metadata":{"execution":{"iopub.status.busy":"2023-06-10T13:42:39.465193Z","iopub.execute_input":"2023-06-10T13:42:39.465591Z","iopub.status.idle":"2023-06-10T14:14:03.392209Z","shell.execute_reply.started":"2023-06-10T13:42:39.465542Z","shell.execute_reply":"2023-06-10T14:14:03.391083Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### **Let's plot the loss and accuracy of the model over the training and validation data:**","metadata":{}},{"cell_type":"code","source":"acc = fine_tuned_history.history['accuracy']\nval_acc = fine_tuned_history.history['val_accuracy']\nloss = fine_tuned_history.history['loss']\nval_loss = fine_tuned_history.history['val_loss']\n\nepochs = range(len(acc))\n\nplt.plot(epochs, acc, 'bo', label='Training acc')\nplt.plot(epochs, val_acc, 'b', label='Validation acc')\nplt.title('Training and validation accuracy')\nplt.legend()\n\nplt.figure()\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-06-10T14:14:03.394262Z","iopub.execute_input":"2023-06-10T14:14:03.394620Z","iopub.status.idle":"2023-06-10T14:14:04.018924Z","shell.execute_reply.started":"2023-06-10T14:14:03.394594Z","shell.execute_reply":"2023-06-10T14:14:04.017799Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fine_tuned_model.save('/kaggle/working/models/fineTuned_model.h5')","metadata":{"execution":{"iopub.status.busy":"2023-06-10T14:14:04.020349Z","iopub.execute_input":"2023-06-10T14:14:04.022431Z","iopub.status.idle":"2023-06-10T14:14:04.488124Z","shell.execute_reply.started":"2023-06-10T14:14:04.022392Z","shell.execute_reply":"2023-06-10T14:14:04.487059Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **7. Evaluation** ","metadata":{}},{"cell_type":"markdown","source":"* #### **Pre-trained VGG16 Model, Fixed**","metadata":{}},{"cell_type":"code","source":"# I will choose this model for testing\nval_loss, val_acc = model.evaluate(validation_generator)","metadata":{"execution":{"iopub.status.busy":"2023-06-10T14:47:09.073178Z","iopub.execute_input":"2023-06-10T14:47:09.073546Z","iopub.status.idle":"2023-06-10T14:47:50.606536Z","shell.execute_reply.started":"2023-06-10T14:47:09.073517Z","shell.execute_reply":"2023-06-10T14:47:50.605516Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **8. Testing** ","metadata":{}},{"cell_type":"code","source":"test = '/kaggle/input/state-farm-distracted-driver-detection/imgs/.'\ntest_datagen = ImageDataGenerator(preprocessing_function=preprocess_input)\n\ntest_generator = test_datagen.flow_from_directory(\n                                                test,\n                                                target_size =target_size,\n                                                batch_size = batch_size,\n                                                classes = ['test'],\n                                                shuffle = False\n)","metadata":{"execution":{"iopub.status.busy":"2023-06-10T14:58:18.032671Z","iopub.execute_input":"2023-06-10T14:58:18.033047Z","iopub.status.idle":"2023-06-10T14:58:44.309364Z","shell.execute_reply.started":"2023-06-10T14:58:18.033017Z","shell.execute_reply":"2023-06-10T14:58:44.308437Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Load the choosed model (Pre-trained VGG16 Model, Fixed)\nfinal_model = load_model('/kaggle/working/models/fixed_model.h5')","metadata":{"execution":{"iopub.status.busy":"2023-06-10T15:00:56.597494Z","iopub.execute_input":"2023-06-10T15:00:56.597860Z","iopub.status.idle":"2023-06-10T15:00:57.879679Z","shell.execute_reply.started":"2023-06-10T15:00:56.597829Z","shell.execute_reply":"2023-06-10T15:00:57.878606Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"prediction = final_model.predict(test_generator)","metadata":{"execution":{"iopub.status.busy":"2023-06-10T15:02:11.723972Z","iopub.execute_input":"2023-06-10T15:02:11.724358Z","iopub.status.idle":"2023-06-10T15:18:54.023558Z","shell.execute_reply.started":"2023-06-10T15:02:11.724326Z","shell.execute_reply":"2023-06-10T15:18:54.022527Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"prediction.shape","metadata":{"execution":{"iopub.status.busy":"2023-06-10T15:20:30.056965Z","iopub.execute_input":"2023-06-10T15:20:30.057393Z","iopub.status.idle":"2023-06-10T15:20:30.064540Z","shell.execute_reply.started":"2023-06-10T15:20:30.057361Z","shell.execute_reply":"2023-06-10T15:20:30.063426Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"prediction[0]","metadata":{"execution":{"iopub.status.busy":"2023-06-10T15:21:28.712634Z","iopub.execute_input":"2023-06-10T15:21:28.713034Z","iopub.status.idle":"2023-06-10T15:21:28.720003Z","shell.execute_reply.started":"2023-06-10T15:21:28.713001Z","shell.execute_reply":"2023-06-10T15:21:28.719014Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"imgs_names = sorted(os.listdir(\"/kaggle/input/state-farm-distracted-driver-detection/imgs/test\"))","metadata":{"execution":{"iopub.status.busy":"2023-06-10T15:25:24.212886Z","iopub.execute_input":"2023-06-10T15:25:24.213298Z","iopub.status.idle":"2023-06-10T15:25:24.288829Z","shell.execute_reply.started":"2023-06-10T15:25:24.213266Z","shell.execute_reply":"2023-06-10T15:25:24.287652Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"imgs_names[0]","metadata":{"execution":{"iopub.status.busy":"2023-06-10T15:25:28.602533Z","iopub.execute_input":"2023-06-10T15:25:28.602901Z","iopub.status.idle":"2023-06-10T15:25:28.613286Z","shell.execute_reply.started":"2023-06-10T15:25:28.602872Z","shell.execute_reply":"2023-06-10T15:25:28.612250Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img_df = pd.DataFrame(imgs_names, columns = ['img'])\nimg_df.head(5)","metadata":{"execution":{"iopub.status.busy":"2023-06-10T15:25:59.587732Z","iopub.execute_input":"2023-06-10T15:25:59.588435Z","iopub.status.idle":"2023-06-10T15:25:59.609795Z","shell.execute_reply.started":"2023-06-10T15:25:59.588402Z","shell.execute_reply":"2023-06-10T15:25:59.608670Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"prediction_df = pd.DataFrame(prediction, columns = ['c0','c1','c2','c3','c4','c5','c6','c7','c8','c9'])\nprediction_df.head(5)","metadata":{"execution":{"iopub.status.busy":"2023-06-10T15:28:10.263359Z","iopub.execute_input":"2023-06-10T15:28:10.263747Z","iopub.status.idle":"2023-06-10T15:28:10.294405Z","shell.execute_reply.started":"2023-06-10T15:28:10.263717Z","shell.execute_reply":"2023-06-10T15:28:10.293214Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission_df = pd.concat((img_df, prediction_df), axis = 1)\nsubmission_df.head(5)","metadata":{"execution":{"iopub.status.busy":"2023-06-10T15:29:46.688315Z","iopub.execute_input":"2023-06-10T15:29:46.689267Z","iopub.status.idle":"2023-06-10T15:29:46.715432Z","shell.execute_reply.started":"2023-06-10T15:29:46.689235Z","shell.execute_reply":"2023-06-10T15:29:46.714219Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission_df.to_csv('/kaggle/working/submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2023-06-10T15:31:43.003508Z","iopub.execute_input":"2023-06-10T15:31:43.003922Z","iopub.status.idle":"2023-06-10T15:31:44.273514Z","shell.execute_reply.started":"2023-06-10T15:31:43.003891Z","shell.execute_reply":"2023-06-10T15:31:44.272512Z"},"trusted":true},"execution_count":null,"outputs":[]}]}