{"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-16T11:06:20.453338Z","iopub.status.idle":"2023-03-16T11:06:20.453781Z","shell.execute_reply.started":"2023-03-16T11:06:20.453528Z","shell.execute_reply":"2023-03-16T11:06:20.453547Z"},"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-16T11:09:48.342715Z","iopub.execute_input":"2023-03-16T11:09:48.343856Z","iopub.status.idle":"2023-03-16T11:09:48.351170Z","shell.execute_reply.started":"2023-03-16T11:09:48.343804Z","shell.execute_reply":"2023-03-16T11:09:48.349856Z"},"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-16T11:06:34.072155Z","iopub.execute_input":"2023-03-16T11:06:34.072952Z","iopub.status.idle":"2023-03-16T11:09:11.388292Z","shell.execute_reply.started":"2023-03-16T11:06:34.072911Z","shell.execute_reply":"2023-03-16T11:09:11.386857Z"},"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/\"\ntest_dir  = \"/kaggle/working/test/\"","metadata":{"execution":{"iopub.status.busy":"2023-03-16T11:09:11.390558Z","iopub.execute_input":"2023-03-16T11:09:11.390970Z","iopub.status.idle":"2023-03-16T11:09:11.396579Z","shell.execute_reply.started":"2023-03-16T11:09:11.390927Z","shell.execute_reply":"2023-03-16T11:09:11.395378Z"},"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-16T11:09:11.399915Z","iopub.execute_input":"2023-03-16T11:09:11.400803Z","iopub.status.idle":"2023-03-16T11:09:11.417483Z","shell.execute_reply.started":"2023-03-16T11:09:11.400767Z","shell.execute_reply":"2023-03-16T11:09:11.416260Z"},"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-16T11:09:11.419411Z","iopub.execute_input":"2023-03-16T11:09:11.419953Z","iopub.status.idle":"2023-03-16T11:09:11.427375Z","shell.execute_reply.started":"2023-03-16T11:09:11.419906Z","shell.execute_reply":"2023-03-16T11:09:11.426165Z"},"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-16T11:09:11.429093Z","iopub.execute_input":"2023-03-16T11:09:11.429441Z","iopub.status.idle":"2023-03-16T11:09:11.651840Z","shell.execute_reply.started":"2023-03-16T11:09:11.429407Z","shell.execute_reply":"2023-03-16T11:09:11.650825Z"},"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-16T11:09:52.969901Z","iopub.execute_input":"2023-03-16T11:09:52.970588Z","iopub.status.idle":"2023-03-16T11:09:55.568105Z","shell.execute_reply.started":"2023-03-16T11:09:52.970551Z","shell.execute_reply":"2023-03-16T11:09:55.566688Z"},"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":"datagen = ImageDataGenerator(rescale= 1 / 255.0)","metadata":{"execution":{"iopub.status.busy":"2023-03-16T11:10:00.064178Z","iopub.execute_input":"2023-03-16T11:10:00.064549Z","iopub.status.idle":"2023-03-16T11:10:00.070340Z","shell.execute_reply.started":"2023-03-16T11:10:00.064514Z","shell.execute_reply":"2023-03-16T11:10:00.069035Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#  Normalized data","metadata":{}},{"cell_type":"code","source":"batch_size = 32\ntrain_batches =datagen.flow_from_directory(directory = train_dir,shuffle = True,batch_size = batch_size)\nval_batches =datagen.flow_from_directory(directory = valid_dir,shuffle = True, batch_size = batch_size)\ntest_batches =datagen.flow_from_directory(directory= test_dir,shuffle = False,batch_size = 1)","metadata":{"execution":{"iopub.status.busy":"2023-03-16T11:10:01.401467Z","iopub.execute_input":"2023-03-16T11:10:01.402698Z","iopub.status.idle":"2023-03-16T11:10:02.052269Z","shell.execute_reply.started":"2023-03-16T11:10:01.402622Z","shell.execute_reply":"2023-03-16T11:10:02.051174Z"},"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-16T11:12:40.922069Z","iopub.execute_input":"2023-03-16T11:12:40.922528Z","iopub.status.idle":"2023-03-16T11:12:40.978487Z","shell.execute_reply.started":"2023-03-16T11:12:40.922475Z","shell.execute_reply":"2023-03-16T11:12:40.977469Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"networkBaseline.summary()","metadata":{"execution":{"iopub.status.busy":"2023-03-16T11:12:41.173715Z","iopub.execute_input":"2023-03-16T11:12:41.174444Z","iopub.status.idle":"2023-03-16T11:12:41.213979Z","shell.execute_reply.started":"2023-03-16T11:12:41.174389Z","shell.execute_reply":"2023-03-16T11:12:41.212308Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_model(networkBaseline)","metadata":{"execution":{"iopub.status.busy":"2023-03-16T11:12:41.968175Z","iopub.execute_input":"2023-03-16T11:12:41.968554Z","iopub.status.idle":"2023-03-16T11:12:42.110924Z","shell.execute_reply.started":"2023-03-16T11:12:41.968522Z","shell.execute_reply":"2023-03-16T11:12:42.109626Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"networkBaseline.compile(optimizer= optimizers.Adam(learning_rate=1e-3),\n                loss='categorical_crossentropy',\n                metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2023-03-16T11:12:42.316256Z","iopub.execute_input":"2023-03-16T11:12:42.317310Z","iopub.status.idle":"2023-03-16T11:12:42.335834Z","shell.execute_reply.started":"2023-03-16T11:12:42.317265Z","shell.execute_reply":"2023-03-16T11:12:42.334833Z"},"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)\nmodelBaseline=networkBaseline.fit(x = train_batches,\n          epochs=15,\n          validation_data = val_batches,\n          callbacks=[stop_criteria]) ","metadata":{"execution":{"iopub.status.busy":"2023-03-16T11:12:43.248441Z","iopub.execute_input":"2023-03-16T11:12:43.249615Z","iopub.status.idle":"2023-03-16T11:32:30.890896Z","shell.execute_reply.started":"2023-03-16T11:12:43.249549Z","shell.execute_reply":"2023-03-16T11:32:30.889901Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Accuracy","metadata":{}},{"cell_type":"code","source":"scoresBaseline= networkBaseline.evaluate(test_batches)\nprint(\"Accuracy: %.2f%%\" % (scoresBaseline[1]*100))","metadata":{"execution":{"iopub.status.busy":"2023-03-16T11:32:30.893322Z","iopub.execute_input":"2023-03-16T11:32:30.893721Z","iopub.status.idle":"2023-03-16T11:32:47.946497Z","shell.execute_reply.started":"2023-03-16T11:32:30.893682Z","shell.execute_reply":"2023-03-16T11:32:47.945543Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Plot model Accuracy","metadata":{}},{"cell_type":"code","source":"plt.plot(modelBaseline.history['accuracy'])  \nplt.plot(modelBaseline.history['val_accuracy']) \nplt.title('Accuracy of Model')\nplt.ylabel('Accuracy')\nplt.ylabel('Epoch')\nplt.legend(['accuracy', 'val_accuracy'])","metadata":{"execution":{"iopub.status.busy":"2023-03-16T11:32:47.948301Z","iopub.execute_input":"2023-03-16T11:32:47.949103Z","iopub.status.idle":"2023-03-16T11:32:48.228946Z","shell.execute_reply.started":"2023-03-16T11:32:47.949062Z","shell.execute_reply":"2023-03-16T11:32:48.227913Z"},"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-16T11:32:48.231852Z","iopub.execute_input":"2023-03-16T11:32:48.232514Z","iopub.status.idle":"2023-03-16T11:32:48.338161Z","shell.execute_reply.started":"2023-03-16T11:32:48.232469Z","shell.execute_reply":"2023-03-16T11:32:48.337172Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"modelCNN.summary()","metadata":{"execution":{"iopub.status.busy":"2023-03-16T11:32:48.339704Z","iopub.execute_input":"2023-03-16T11:32:48.340052Z","iopub.status.idle":"2023-03-16T11:32:48.380357Z","shell.execute_reply.started":"2023-03-16T11:32:48.340014Z","shell.execute_reply":"2023-03-16T11:32:48.379569Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_model(modelCNN)","metadata":{"execution":{"iopub.status.busy":"2023-03-16T11:32:48.381544Z","iopub.execute_input":"2023-03-16T11:32:48.382231Z","iopub.status.idle":"2023-03-16T11:32:48.568010Z","shell.execute_reply.started":"2023-03-16T11:32:48.382190Z","shell.execute_reply":"2023-03-16T11:32:48.566669Z"},"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-16T11:32:48.570185Z","iopub.execute_input":"2023-03-16T11:32:48.570583Z","iopub.status.idle":"2023-03-16T11:32:48.587405Z","shell.execute_reply.started":"2023-03-16T11:32:48.570526Z","shell.execute_reply":"2023-03-16T11:32:48.586423Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"stop_criteria = EarlyStopping(monitor='val_loss', mode='min', verbose=1,patience=3)\nmodelCNNRun=modelCNN.fit(x = train_batches,\n          epochs=15,\n          validation_data = val_batches,\n          callbacks=[stop_criteria]) ","metadata":{"execution":{"iopub.status.busy":"2023-03-16T11:32:48.589057Z","iopub.execute_input":"2023-03-16T11:32:48.589425Z","iopub.status.idle":"2023-03-16T11:51:23.191732Z","shell.execute_reply.started":"2023-03-16T11:32:48.589388Z","shell.execute_reply":"2023-03-16T11:51:23.190747Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"scoresBaseline= modelCNN.evaluate(test_batches)\nprint(\"Accuracy: %.2f%%\" % (scoresBaseline[1]*100))","metadata":{"execution":{"iopub.status.busy":"2023-03-16T11:51:23.193427Z","iopub.execute_input":"2023-03-16T11:51:23.193786Z","iopub.status.idle":"2023-03-16T11:51:43.726142Z","shell.execute_reply.started":"2023-03-16T11:51:23.193747Z","shell.execute_reply":"2023-03-16T11:51:43.724992Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(modelCNNRun.history['accuracy'])  \nplt.plot(modelCNNRun.history['val_accuracy']) \nplt.title('Accuracy of Model')\nplt.ylabel('Accuracy')\nplt.ylabel('Epoch')\nplt.legend(['accuracy', 'val_accuracy'])","metadata":{"execution":{"iopub.status.busy":"2023-03-16T11:51:43.729523Z","iopub.execute_input":"2023-03-16T11:51:43.730122Z","iopub.status.idle":"2023-03-16T11:51:43.976596Z","shell.execute_reply.started":"2023-03-16T11:51:43.730091Z","shell.execute_reply":"2023-03-16T11:51:43.975552Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# (3) Data augmentation","metadata":{}},{"cell_type":"code","source":"datagen_train = ImageDataGenerator(\n    rescale=1./255,\n    #rotation_range=40,\n    width_shift_range=0.1,\n    height_sahift_range=0.1,\n    #shear_range=0.2,\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":{"execution":{"iopub.status.busy":"2023-03-16T11:57:33.936024Z","iopub.execute_input":"2023-03-16T11:57:33.936846Z","iopub.status.idle":"2023-03-16T11:57:33.943291Z","shell.execute_reply.started":"2023-03-16T11:57:33.936808Z","shell.execute_reply":"2023-03-16T11:57:33.942123Z"},"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":{"execution":{"iopub.status.busy":"2023-03-16T11:57:34.346368Z","iopub.execute_input":"2023-03-16T11:57:34.346755Z","iopub.status.idle":"2023-03-16T11:57:34.783271Z","shell.execute_reply.started":"2023-03-16T11:57:34.346722Z","shell.execute_reply":"2023-03-16T11:57:34.782243Z"},"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":{"execution":{"iopub.status.busy":"2023-03-16T11:57:34.785411Z","iopub.execute_input":"2023-03-16T11:57:34.785802Z","iopub.status.idle":"2023-03-16T11:57:34.896214Z","shell.execute_reply.started":"2023-03-16T11:57:34.785763Z","shell.execute_reply":"2023-03-16T11:57:34.895051Z"},"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":{"execution":{"iopub.status.busy":"2023-03-16T11:57:34.897379Z","iopub.execute_input":"2023-03-16T11:57:34.897779Z","iopub.status.idle":"2023-03-16T11:57:35.007873Z","shell.execute_reply.started":"2023-03-16T11:57:34.897741Z","shell.execute_reply":"2023-03-16T11:57:35.006861Z"},"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-16T11:57:35.009711Z","iopub.execute_input":"2023-03-16T11:57:35.009972Z","iopub.status.idle":"2023-03-16T11:57:35.100448Z","shell.execute_reply.started":"2023-03-16T11:57:35.009946Z","shell.execute_reply":"2023-03-16T11:57:35.099481Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"modelCNNWithAug.summary()","metadata":{"execution":{"iopub.status.busy":"2023-03-16T11:57:35.104537Z","iopub.execute_input":"2023-03-16T11:57:35.105076Z","iopub.status.idle":"2023-03-16T11:57:35.136442Z","shell.execute_reply.started":"2023-03-16T11:57:35.105047Z","shell.execute_reply":"2023-03-16T11:57:35.135714Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_model(modelCNNWithAug)","metadata":{"execution":{"iopub.status.busy":"2023-03-16T11:57:35.650228Z","iopub.execute_input":"2023-03-16T11:57:35.651278Z","iopub.status.idle":"2023-03-16T11:57:35.856230Z","shell.execute_reply.started":"2023-03-16T11:57:35.651225Z","shell.execute_reply":"2023-03-16T11:57:35.855041Z"},"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-16T11:57:36.123580Z","iopub.execute_input":"2023-03-16T11:57:36.124339Z","iopub.status.idle":"2023-03-16T11:57:36.139954Z","shell.execute_reply.started":"2023-03-16T11:57:36.124298Z","shell.execute_reply":"2023-03-16T11:57:36.138742Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"stop_criteria = EarlyStopping(monitor='val_loss', mode='min', verbose=1,patience=3)\nmodelCNNRunWithAug=modelCNNWithAug.fit(x = train_generator,\n          epochs=15,\n          validation_data = validation_generator,\n          callbacks=[stop_criteria])","metadata":{"execution":{"iopub.status.busy":"2023-03-16T11:57:36.884715Z","iopub.execute_input":"2023-03-16T11:57:36.885686Z","iopub.status.idle":"2023-03-16T13:16:33.500399Z","shell.execute_reply.started":"2023-03-16T11:57:36.885636Z","shell.execute_reply":"2023-03-16T13:16:33.499394Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"scores = modelCNNWithAug.evaluate(test_batches)\nprint(\"Accuracy: %.2f%%\" % (scores[1]*100))","metadata":{"execution":{"iopub.status.busy":"2023-03-16T13:16:33.504189Z","iopub.execute_input":"2023-03-16T13:16:33.505208Z","iopub.status.idle":"2023-03-16T13:16:54.035745Z","shell.execute_reply.started":"2023-03-16T13:16:33.505165Z","shell.execute_reply":"2023-03-16T13:16:54.034494Z"},"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":{"execution":{"iopub.status.busy":"2023-03-16T13:16:54.037313Z","iopub.execute_input":"2023-03-16T13:16:54.037792Z","iopub.status.idle":"2023-03-16T13:16:54.290569Z","shell.execute_reply.started":"2023-03-16T13:16:54.037752Z","shell.execute_reply":"2023-03-16T13:16:54.289532Z"},"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":{"execution":{"iopub.status.busy":"2023-03-16T13:55:17.874546Z","iopub.execute_input":"2023-03-16T13:55:17.874953Z","iopub.status.idle":"2023-03-16T13:55:17.883924Z","shell.execute_reply.started":"2023-03-16T13:55:17.874920Z","shell.execute_reply":"2023-03-16T13:55:17.882394Z"},"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":{"execution":{"iopub.status.busy":"2023-03-16T13:55:18.236251Z","iopub.execute_input":"2023-03-16T13:55:18.236620Z","iopub.status.idle":"2023-03-16T13:55:18.672504Z","shell.execute_reply.started":"2023-03-16T13:55:18.236572Z","shell.execute_reply":"2023-03-16T13:55:18.671521Z"},"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":{"execution":{"iopub.status.busy":"2023-03-16T13:55:18.674385Z","iopub.execute_input":"2023-03-16T13:55:18.675871Z","iopub.status.idle":"2023-03-16T13:55:18.785018Z","shell.execute_reply.started":"2023-03-16T13:55:18.675827Z","shell.execute_reply":"2023-03-16T13:55:18.784076Z"},"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":{"execution":{"iopub.status.busy":"2023-03-16T13:55:18.988114Z","iopub.execute_input":"2023-03-16T13:55:18.988748Z","iopub.status.idle":"2023-03-16T13:55:19.099506Z","shell.execute_reply.started":"2023-03-16T13:55:18.988706Z","shell.execute_reply":"2023-03-16T13:55:19.098617Z"},"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":{"execution":{"iopub.status.busy":"2023-03-16T13:55:19.489751Z","iopub.execute_input":"2023-03-16T13:55:19.490627Z","iopub.status.idle":"2023-03-16T13:55:19.838689Z","shell.execute_reply.started":"2023-03-16T13:55:19.490553Z","shell.execute_reply":"2023-03-16T13:55:19.837566Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"conv_base.summary()","metadata":{"execution":{"iopub.status.busy":"2023-03-16T13:55:19.964204Z","iopub.execute_input":"2023-03-16T13:55:19.964514Z","iopub.status.idle":"2023-03-16T13:55:20.005782Z","shell.execute_reply.started":"2023-03-16T13:55:19.964483Z","shell.execute_reply":"2023-03-16T13:55:20.005036Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"conv_base.trainable = True","metadata":{"execution":{"iopub.status.busy":"2023-03-16T13:55:20.271440Z","iopub.execute_input":"2023-03-16T13:55:20.271746Z","iopub.status.idle":"2023-03-16T13:55:20.276321Z","shell.execute_reply.started":"2023-03-16T13:55:20.271717Z","shell.execute_reply":"2023-03-16T13:55:20.275289Z"},"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":{"execution":{"iopub.status.busy":"2023-03-16T13:55:22.252470Z","iopub.execute_input":"2023-03-16T13:55:22.253290Z","iopub.status.idle":"2023-03-16T13:55:22.347977Z","shell.execute_reply.started":"2023-03-16T13:55:22.253248Z","shell.execute_reply":"2023-03-16T13:55:22.347020Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2023-03-16T13:55:24.225253Z","iopub.execute_input":"2023-03-16T13:55:24.226018Z","iopub.status.idle":"2023-03-16T13:55:24.247180Z","shell.execute_reply.started":"2023-03-16T13:55:24.225977Z","shell.execute_reply":"2023-03-16T13:55:24.246375Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_model(model)","metadata":{"execution":{"iopub.status.busy":"2023-03-16T13:55:24.957586Z","iopub.execute_input":"2023-03-16T13:55:24.958283Z","iopub.status.idle":"2023-03-16T13:55:25.133681Z","shell.execute_reply.started":"2023-03-16T13:55:24.958247Z","shell.execute_reply":"2023-03-16T13:55:25.132498Z"},"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":{"execution":{"iopub.status.busy":"2023-03-16T13:55:26.242270Z","iopub.execute_input":"2023-03-16T13:55:26.243560Z","iopub.status.idle":"2023-03-16T13:55:26.259635Z","shell.execute_reply.started":"2023-03-16T13:55:26.243508Z","shell.execute_reply":"2023-03-16T13:55:26.258577Z"},"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":{"execution":{"iopub.status.busy":"2023-03-16T13:56:32.352652Z","iopub.execute_input":"2023-03-16T13:56:32.353782Z","iopub.status.idle":"2023-03-16T14:12:58.571274Z","shell.execute_reply.started":"2023-03-16T13:56:32.353733Z","shell.execute_reply":"2023-03-16T14:12:58.570258Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"scores = model.evaluate(test_batches)\nprint(\"Accuracy: %.2f%%\" % (scores[1]*100))","metadata":{"execution":{"iopub.status.busy":"2023-03-16T14:12:58.573420Z","iopub.execute_input":"2023-03-16T14:12:58.573908Z","iopub.status.idle":"2023-03-16T14:13:49.950147Z","shell.execute_reply.started":"2023-03-16T14:12:58.573868Z","shell.execute_reply":"2023-03-16T14:13:49.948128Z"},"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":{"execution":{"iopub.status.busy":"2023-03-16T14:13:49.951828Z","iopub.execute_input":"2023-03-16T14:13:49.952181Z","iopub.status.idle":"2023-03-16T14:13:50.192473Z","shell.execute_reply.started":"2023-03-16T14:13:49.952143Z","shell.execute_reply":"2023-03-16T14:13:50.191536Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}