{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        #print(os.path.join(dirname, filename))\n        os.path.join(dirname, filename)\n\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-03-23T14:51:20.591528Z","iopub.execute_input":"2023-03-23T14:51:20.592097Z","iopub.status.idle":"2023-03-23T14:53:46.12749Z","shell.execute_reply.started":"2023-03-23T14:51:20.592063Z","shell.execute_reply":"2023-03-23T14:53:46.126448Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Team Members:\n### Ehab Nabile Fathy\n### Mahmoud Samir Gooda\n### Al Zharaa Mohamed Shaeen \n### Ahmed Rabie Galal Taha","metadata":{}},{"cell_type":"markdown","source":"# Import imortant liberires","metadata":{}},{"cell_type":"code","source":"import cv2\nimport numpy as np \nimport pandas as pd \nimport os\nimport random\nimport shutil\nimport matplotlib.pyplot as plt\n\n#EarlyStopping\nfrom tensorflow.keras.callbacks import EarlyStopping\n\n#Models\nfrom tensorflow.keras import layers ,models,optimizers\nfrom keras.layers import Dropout, Flatten, Dense\nfrom tensorflow.keras.utils import plot_model\n\n# ImageDataGenerator\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\n\n#Transfer Learning (VGG16)\nfrom tensorflow.keras.applications import VGG16\nfrom tensorflow.keras.applications.imagenet_utils import preprocess_input \n\n#Visualisation\nfrom keras.preprocessing import image","metadata":{"execution":{"iopub.status.busy":"2023-03-23T14:53:54.668972Z","iopub.execute_input":"2023-03-23T14:53:54.669336Z","iopub.status.idle":"2023-03-23T14:54:10.560225Z","shell.execute_reply.started":"2023-03-23T14:53:54.669302Z","shell.execute_reply":"2023-03-23T14:54:10.559Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!cp -r /kaggle/input/state-farm-distracted-driver-detection/imgs/train ./","metadata":{"execution":{"iopub.status.busy":"2023-03-23T14:54:15.684494Z","iopub.execute_input":"2023-03-23T14:54:15.6852Z","iopub.status.idle":"2023-03-23T14:57:58.437374Z","shell.execute_reply.started":"2023-03-23T14:54:15.68516Z","shell.execute_reply":"2023-03-23T14:57:58.436007Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Create Directories","metadata":{}},{"cell_type":"code","source":"train_dir = \"/kaggle/working/train/\"\nvalid_dir =\"/kaggle/working/val/\"\n#test_dir  = \"/kaggle/working/test/\"","metadata":{"execution":{"iopub.status.busy":"2023-03-16T20:46:03.035865Z","iopub.execute_input":"2023-03-16T20:46:03.036194Z","iopub.status.idle":"2023-03-16T20:46:03.041542Z","shell.execute_reply.started":"2023-03-16T20:46:03.036164Z","shell.execute_reply":"2023-03-16T20:46:03.040103Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Define Classes (10 Classes)","metadata":{}},{"cell_type":"code","source":"classes = {   '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\"}\nclasses","metadata":{"execution":{"iopub.status.busy":"2023-03-16T20:46:03.043007Z","iopub.execute_input":"2023-03-16T20:46:03.043647Z","iopub.status.idle":"2023-03-16T20:46:03.061525Z","shell.execute_reply.started":"2023-03-16T20:46:03.04361Z","shell.execute_reply":"2023-03-16T20:46:03.060343Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Arrange Directories","metadata":{}},{"cell_type":"code","source":"for file in os.listdir(train_dir):\n    shutil.move(os.path.join(train_dir,file), os.path.join(train_dir,classes[f'{file}']))","metadata":{"execution":{"iopub.status.busy":"2023-03-16T20:46:03.063151Z","iopub.execute_input":"2023-03-16T20:46:03.063539Z","iopub.status.idle":"2023-03-16T20:46:03.079799Z","shell.execute_reply.started":"2023-03-16T20:46:03.063503Z","shell.execute_reply":"2023-03-16T20:46:03.078565Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for file in os.listdir(train_dir):\n    os.makedirs(valid_dir + '/' + file, exist_ok=True)\n    #os.makedirs(test_dir + '/' + file, exist_ok=True)    \n    train_dir_img = train_dir + file\n    file_len = len([sample for sample in os.listdir(train_dir_img)])\n    print(file_len)\n    \n    for sample in random.sample(os.listdir(train_dir_img) , int(float(0.1) * file_len)):\n        shutil.move(train_dir_img + '/' + sample, valid_dir + file)\n    #for sample in random.sample(os.listdir(train_dir_img) , int(float(0.1) * file_len)):\n        #shutil.move(train_dir_img + '/' + sample, test_dir + file)","metadata":{"execution":{"iopub.status.busy":"2023-03-16T20:46:03.082422Z","iopub.execute_input":"2023-03-16T20:46:03.083169Z","iopub.status.idle":"2023-03-16T20:46:03.208448Z","shell.execute_reply.started":"2023-03-16T20:46:03.08313Z","shell.execute_reply":"2023-03-16T20:46:03.207269Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Plot One Image for Each Class","metadata":{}},{"cell_type":"code","source":"plt.figure(figsize = (15, 20))\nurl = '../input/state-farm-distracted-driver-detection/imgs/train/'\ncount = 1\nfor directory in os.listdir(url):\n    if directory[0] != '.':\n        for i, file in enumerate(os.listdir(url + directory)):\n            if i == 1:\n                break\n            else:\n                fig = plt.subplot(5, 2, count)\n                count += 1\n                image =cv2.imread(url + directory + '/' + file)\n                img = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)\n                plt.imshow(img)\n                plt.title(classes[directory])","metadata":{"execution":{"iopub.status.busy":"2023-03-16T20:46:03.210062Z","iopub.execute_input":"2023-03-16T20:46:03.210422Z","iopub.status.idle":"2023-03-16T20:46:05.655588Z","shell.execute_reply.started":"2023-03-16T20:46:03.210387Z","shell.execute_reply":"2023-03-16T20:46:05.654262Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Rescaling factor 1/255 to rescale the initial values from 0 to 255 to 0 to 1 instead","metadata":{}},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_datagen = ImageDataGenerator(rescale=1./255)\nval_datagen = ImageDataGenerator(rescale=1./255)\n#test_datagen = ImageDataGenerator(rescale=1./255)","metadata":{"execution":{"iopub.status.busy":"2023-03-16T21:03:30.503764Z","iopub.execute_input":"2023-03-16T21:03:30.504127Z","iopub.status.idle":"2023-03-16T21:03:30.509408Z","shell.execute_reply.started":"2023-03-16T21:03:30.504096Z","shell.execute_reply":"2023-03-16T21:03:30.508221Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#  Normalized data","metadata":{}},{"cell_type":"code","source":"train_generator = train_datagen.flow_from_directory(\n        # This is the target directory\n        train_dir,\n        # All images will be resized to 150x150\n        target_size=(256, 256),\n        batch_size=20,\n        # Since we use binary_crossentropy loss, we need binary labels\n        class_mode='categorical')\n\nvalidation_generator = val_datagen.flow_from_directory(\n        valid_dir,\n        target_size=(256, 256),\n        batch_size=20,\n        class_mode='categorical')","metadata":{"execution":{"iopub.status.busy":"2023-03-16T21:03:33.465689Z","iopub.execute_input":"2023-03-16T21:03:33.466633Z","iopub.status.idle":"2023-03-16T21:03:34.136302Z","shell.execute_reply.started":"2023-03-16T21:03:33.466591Z","shell.execute_reply":"2023-03-16T21:03:34.135375Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for data_batch, labels_batch in train_generator:\n    print('data batch shape:', data_batch.shape)\n    print('labels batch shape:', labels_batch.shape)\n    break","metadata":{"execution":{"iopub.status.busy":"2023-03-16T21:03:36.004756Z","iopub.execute_input":"2023-03-16T21:03:36.005705Z","iopub.status.idle":"2023-03-16T21:03:36.112556Z","shell.execute_reply.started":"2023-03-16T21:03:36.005649Z","shell.execute_reply":"2023-03-16T21:03:36.111157Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# (1) Use Baseline Dense layers model","metadata":{}},{"cell_type":"markdown","source":"### Model","metadata":{}},{"cell_type":"code","source":"networkBaseline = models.Sequential()\nnetworkBaseline.add(layers.Flatten(input_shape=(256,256,3)))\nnetworkBaseline.add(layers.Dense(512,activation = 'relu',name = 'input'))\n#networkBaseline.add(layers.BatchNormalization())\nnetworkBaseline.add(layers.Dense(256,activation = 'relu',name = 'HL1'))\n#networkBaseline.add(layers.BatchNormalization())\nnetworkBaseline.add(layers.Dense(128,activation = 'relu',name = 'HL2'))\n#Batch normalization applies a transformation that maintains the mean output 0 \n#and the output standard deviation 1\n#networkBaseline.add(layers.BatchNormalization())\nnetworkBaseline.add(layers.Dense(len(classes),activation = 'softmax',name = 'output'))","metadata":{"execution":{"iopub.status.busy":"2023-03-16T21:03:42.443706Z","iopub.execute_input":"2023-03-16T21:03:42.444089Z","iopub.status.idle":"2023-03-16T21:03:42.500261Z","shell.execute_reply.started":"2023-03-16T21:03:42.444058Z","shell.execute_reply":"2023-03-16T21:03:42.499294Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"networkBaseline.summary()","metadata":{"execution":{"iopub.status.busy":"2023-03-16T21:03:45.173728Z","iopub.execute_input":"2023-03-16T21:03:45.174091Z","iopub.status.idle":"2023-03-16T21:03:45.198323Z","shell.execute_reply.started":"2023-03-16T21:03:45.174059Z","shell.execute_reply":"2023-03-16T21:03:45.197546Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_model(networkBaseline)","metadata":{"execution":{"iopub.status.busy":"2023-03-16T21:03:49.139208Z","iopub.execute_input":"2023-03-16T21:03:49.139895Z","iopub.status.idle":"2023-03-16T21:03:49.310595Z","shell.execute_reply.started":"2023-03-16T21:03:49.139847Z","shell.execute_reply":"2023-03-16T21:03:49.309392Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"networkBaseline.compile(optimizer= optimizers.Adam(learning_rate=.0001),\n                loss='categorical_crossentropy',\n                metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2023-03-16T21:03:57.806731Z","iopub.execute_input":"2023-03-16T21:03:57.807182Z","iopub.status.idle":"2023-03-16T21:03:57.824086Z","shell.execute_reply.started":"2023-03-16T21:03:57.807142Z","shell.execute_reply":"2023-03-16T21:03:57.822963Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### EarlyStopping","metadata":{}},{"cell_type":"code","source":"#stop_criteria = EarlyStopping(monitor='val_loss', mode='min', verbose=1,patience=3)\n#modelBaseline=networkBaseline.fit(x = train_batches,\n         # epochs=15,\n          #validation_data = val_batches,\n          #callbacks=[stop_criteria]) \nhistory = networkBaseline.fit(\n      train_generator,\n      steps_per_epoch=100,\n      epochs=10,\n      validation_data=validation_generator,\n      validation_steps=50)","metadata":{"execution":{"iopub.status.busy":"2023-03-16T21:04:05.570282Z","iopub.execute_input":"2023-03-16T21:04:05.571285Z","iopub.status.idle":"2023-03-16T21:09:38.25571Z","shell.execute_reply.started":"2023-03-16T21:04:05.571249Z","shell.execute_reply":"2023-03-16T21:09:38.254527Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Accuracy","metadata":{}},{"cell_type":"code","source":"scoresBaseline= networkBaseline.evaluate(validation_generator)\nprint(\"Accuracy: %.2f%%\" % (scoresBaseline[1]*100))","metadata":{"execution":{"iopub.status.busy":"2023-03-16T21:12:26.587003Z","iopub.execute_input":"2023-03-16T21:12:26.587789Z","iopub.status.idle":"2023-03-16T21:12:38.609352Z","shell.execute_reply.started":"2023-03-16T21:12:26.587749Z","shell.execute_reply":"2023-03-16T21:12:38.608241Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Plot model Accuracy","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-03-16T21:09:42.316611Z","iopub.execute_input":"2023-03-16T21:09:42.317005Z","iopub.status.idle":"2023-03-16T21:09:42.751236Z","shell.execute_reply.started":"2023-03-16T21:09:42.316973Z","shell.execute_reply":"2023-03-16T21:09:42.750317Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# (2) Baseline CNN model","metadata":{}},{"cell_type":"code","source":"modelCNN = models.Sequential([\n    layers.Conv2D(32, (3, 3), activation='relu', input_shape=(256, 256, 3), name = 'input'),\n    layers.MaxPooling2D((2, 2)),\n    layers.Conv2D(64, (3, 3), activation='relu'),\n    layers.MaxPooling2D((2, 2)),\n    layers.Conv2D(128, (3, 3), activation='relu'),\n    layers.MaxPooling2D((2, 2)),\n    layers.Flatten(),\n    layers.Dense(512, activation='relu'),\n    layers.Dense(256, activation='relu'),\n    layers.Dense(128, activation='relu'),\n    layers.Dense(len(classes), activation='softmax',name = 'output')\n])","metadata":{"execution":{"iopub.status.busy":"2023-03-16T21:13:44.069701Z","iopub.execute_input":"2023-03-16T21:13:44.070542Z","iopub.status.idle":"2023-03-16T21:13:44.183688Z","shell.execute_reply.started":"2023-03-16T21:13:44.070502Z","shell.execute_reply":"2023-03-16T21:13:44.182675Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"modelCNN.summary()","metadata":{"execution":{"iopub.status.busy":"2023-03-16T21:13:47.576061Z","iopub.execute_input":"2023-03-16T21:13:47.577062Z","iopub.status.idle":"2023-03-16T21:13:47.610463Z","shell.execute_reply.started":"2023-03-16T21:13:47.57702Z","shell.execute_reply":"2023-03-16T21:13:47.609607Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_model(modelCNN)","metadata":{"execution":{"iopub.status.busy":"2023-03-16T21:13:48.1011Z","iopub.execute_input":"2023-03-16T21:13:48.101486Z","iopub.status.idle":"2023-03-16T21:13:48.295433Z","shell.execute_reply.started":"2023-03-16T21:13:48.101453Z","shell.execute_reply":"2023-03-16T21:13:48.294212Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"modelCNN.compile(optimizer= optimizers.Adam(learning_rate=1e-3),\n                loss='categorical_crossentropy',\n                metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2023-03-16T21:16:27.9223Z","iopub.execute_input":"2023-03-16T21:16:27.923126Z","iopub.status.idle":"2023-03-16T21:16:27.941948Z","shell.execute_reply.started":"2023-03-16T21:16:27.923083Z","shell.execute_reply":"2023-03-16T21:16:27.940918Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#stop_criteria = EarlyStopping(monitor='val_loss', mode='min', verbose=1,patience=3)\n#modelBaseline=networkBaseline.fit(x = train_batches,\n         # epochs=15,\n          #validation_data = val_batches,\n          #callbacks=[stop_criteria]) \nhistory = modelCNN.fit(\n      train_generator,\n      steps_per_epoch=100,\n      epochs=10,\n      validation_data=validation_generator,\n      validation_steps=50)","metadata":{"execution":{"iopub.status.busy":"2023-03-16T21:17:21.189475Z","iopub.execute_input":"2023-03-16T21:17:21.190428Z","iopub.status.idle":"2023-03-16T21:22:22.050692Z","shell.execute_reply.started":"2023-03-16T21:17:21.190387Z","shell.execute_reply":"2023-03-16T21:22:22.049655Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"scoresBaseline= modelCNN.evaluate(validation_generator)\nprint(\"Accuracy: %.2f%%\" % (scoresBaseline[1]*100))","metadata":{"execution":{"iopub.status.busy":"2023-03-16T21:23:20.404438Z","iopub.execute_input":"2023-03-16T21:23:20.404844Z","iopub.status.idle":"2023-03-16T21:23:41.025363Z","shell.execute_reply.started":"2023-03-16T21:23:20.40481Z","shell.execute_reply":"2023-03-16T21:23:41.024043Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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-03-16T21:23:47.514105Z","iopub.execute_input":"2023-03-16T21:23:47.514837Z","iopub.status.idle":"2023-03-16T21:23:47.94935Z","shell.execute_reply.started":"2023-03-16T21:23:47.514796Z","shell.execute_reply":"2023-03-16T21:23:47.948281Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# (3) Data augmentation","metadata":{}},{"cell_type":"code","source":"datagen_train2 = ImageDataGenerator(\n    rescale=1./255,\n    #rotation_range=40,\n    width_shift_range=0.1,\n    height_shift_range=0.1,\n    #shear_range=0.2,\n    #zoom_range=0.1,\n    horizontal_flip=True,\n    fill_mode=\"nearest\")\n\nvalid_datagen2 = ImageDataGenerator(rescale=1./255)\n#test_datagen = ImageDataGenerator(rescale=1./255)","metadata":{"execution":{"iopub.status.busy":"2023-03-16T21:25:51.782307Z","iopub.execute_input":"2023-03-16T21:25:51.783041Z","iopub.status.idle":"2023-03-16T21:25:51.788972Z","shell.execute_reply.started":"2023-03-16T21:25:51.783002Z","shell.execute_reply":"2023-03-16T21:25:51.787709Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_generator2 = datagen_train2.flow_from_directory(\n        directory = train_dir,\n        target_size=(256, 256),\n        batch_size=20,\n        class_mode='categorical')","metadata":{"execution":{"iopub.status.busy":"2023-03-16T21:29:48.839744Z","iopub.execute_input":"2023-03-16T21:29:48.840137Z","iopub.status.idle":"2023-03-16T21:29:49.286691Z","shell.execute_reply.started":"2023-03-16T21:29:48.840103Z","shell.execute_reply":"2023-03-16T21:29:49.285635Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"validation_generator2 = valid_datagen2.flow_from_directory(\n        directory = valid_dir,\n        target_size=(256, 256),\n        batch_size=20,\n        class_mode='categorical')","metadata":{"execution":{"iopub.status.busy":"2023-03-16T21:29:51.886214Z","iopub.execute_input":"2023-03-16T21:29:51.886623Z","iopub.status.idle":"2023-03-16T21:29:51.99882Z","shell.execute_reply.started":"2023-03-16T21:29:51.886589Z","shell.execute_reply":"2023-03-16T21:29:51.997851Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#test_generator = test_datagen.flow_from_directory(\n      #  directory = test_dir,\n       # target_size=(256, 256),\n        #batch_size=32,\n        #class_mode='categorical')","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"modelCNNWithAug = models.Sequential([\n    layers.Conv2D(32, (3, 3), activation='relu', input_shape=(256, 256, 3), name = 'input'),\n    layers.MaxPooling2D((2, 2)),\n    layers.Conv2D(64, (3, 3), activation='relu'),\n    layers.MaxPooling2D((2, 2)),\n    layers.Conv2D(128, (3, 3), activation='relu'),\n    layers.MaxPooling2D((2, 2)),\n    layers.Flatten(),\n    layers.Dense(512, activation='relu'),\n    layers.Dense(256, activation='relu'),\n    layers.Dense(128, activation='relu'),\n    layers.Dense(len(classes), activation='softmax',name = 'output')\n])","metadata":{"execution":{"iopub.status.busy":"2023-03-16T21:29:58.753539Z","iopub.execute_input":"2023-03-16T21:29:58.753925Z","iopub.status.idle":"2023-03-16T21:29:58.852101Z","shell.execute_reply.started":"2023-03-16T21:29:58.753887Z","shell.execute_reply":"2023-03-16T21:29:58.851024Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"modelCNNWithAug.summary()","metadata":{"execution":{"iopub.status.busy":"2023-03-16T21:29:59.448364Z","iopub.execute_input":"2023-03-16T21:29:59.449102Z","iopub.status.idle":"2023-03-16T21:29:59.482328Z","shell.execute_reply.started":"2023-03-16T21:29:59.449063Z","shell.execute_reply":"2023-03-16T21:29:59.481504Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_model(modelCNNWithAug)","metadata":{"execution":{"iopub.status.busy":"2023-03-16T21:26:38.203114Z","iopub.execute_input":"2023-03-16T21:26:38.204124Z","iopub.status.idle":"2023-03-16T21:26:38.440256Z","shell.execute_reply.started":"2023-03-16T21:26:38.204083Z","shell.execute_reply":"2023-03-16T21:26:38.43902Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"modelCNNWithAug.compile(optimizer= optimizers.Adam(learning_rate=1e-3),\n                loss='categorical_crossentropy',\n                metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2023-03-16T21:30:05.507415Z","iopub.execute_input":"2023-03-16T21:30:05.508571Z","iopub.status.idle":"2023-03-16T21:30:05.521385Z","shell.execute_reply.started":"2023-03-16T21:30:05.508532Z","shell.execute_reply":"2023-03-16T21:30:05.520328Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#stop_criteria = EarlyStopping(monitor='val_loss', mode='min', verbose=1,patience=3)\n#modelBaseline=networkBaseline.fit(x = train_batches,\n         # epochs=15,\n          #validation_data = val_batches,\n          #callbacks=[stop_criteria]) \nhistory = modelCNNWithAug.fit(\n      train_generator2,\n      steps_per_epoch=100,\n      epochs=10,\n      validation_data=validation_generator2,\n      validation_steps=50)","metadata":{"execution":{"iopub.status.busy":"2023-03-16T21:30:11.153139Z","iopub.execute_input":"2023-03-16T21:30:11.154229Z","iopub.status.idle":"2023-03-16T21:41:25.984745Z","shell.execute_reply.started":"2023-03-16T21:30:11.154188Z","shell.execute_reply":"2023-03-16T21:41:25.983617Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"scores = modelCNNWithAug.evaluate(test_batches)\nprint(\"Accuracy: %.2f%%\" % (scores[1]*100))","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(modelCNNRunWithAug.history['accuracy'])  \nplt.plot(modelCNNRunWithAug.history['val_accuracy']) \nplt.title('Accuracy of Model')\nplt.ylabel('Accuracy')\nplt.ylabel('Epoch')\nplt.legend(['accuracy', 'val_accuracy'])","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# (4) Transfer Learning","metadata":{}},{"cell_type":"code","source":"datagen_train = ImageDataGenerator(\n    rescale=1./255,\n    width_shift_range=0.1,\n    zoom_range=0.1,\n    horizontal_flip=True,\n    fill_mode=\"nearest\")\n\nvalid_datagen = ImageDataGenerator(rescale=1./255)\ntest_datagen = ImageDataGenerator(rescale=1./255)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_generator = datagen_train.flow_from_directory(\n        directory = train_dir,\n        target_size=(256, 256),\n        batch_size=32,\n        class_mode='categorical')","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"validation_generator = valid_datagen.flow_from_directory(\n        directory = valid_dir,\n        target_size=(256, 256),\n        batch_size=32,\n        class_mode='categorical')","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_generator = test_datagen.flow_from_directory(\n        directory = test_dir,\n        target_size=(256, 256),\n        batch_size=32,\n        class_mode='categorical')","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"conv_base = VGG16(weights='imagenet',\n                  include_top=False,\n                  input_shape=(256, 256, 3))","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"conv_base.summary()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"conv_base.trainable = True","metadata":{"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(256, activation='relu'))\nmodel.add(layers.Dense(len(classes), activation='softmax',name = 'output'))","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_model(model)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(loss='categorical_crossentropy',\n              optimizer=optimizers.Adam(learning_rate=1e-5),\n              metrics=['accuracy'])","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"stop_criteria = EarlyStopping(monitor='val_loss', mode='min', verbose=1,patience=3)\n       \nhistory = model.fit(\n      train_generator,\n      steps_per_epoch=100,\n      epochs=100,\n      validation_data=validation_generator,\n      validation_steps=50,\n      callbacks=[stop_criteria])","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"scores = model.evaluate(test_batches)\nprint(\"Accuracy: %.2f%%\" % (scores[1]*100))","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(history.history['accuracy'])  \nplt.plot(history.history['val_accuracy']) \nplt.title('Accuracy of Model')\nplt.ylabel('Accuracy')\nplt.ylabel('Epoch')\nplt.legend(['accuracy', 'val_accuracy'])","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}