{"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\n# import os\n# for dirname, _, filenames in os.walk('/kaggle/input'):\n#     for filename in filenames:\n#         pass\n        #print(os.path.join(dirname, filename))\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":"2022-07-10T15:01:28.414339Z","iopub.execute_input":"2022-07-10T15:01:28.414746Z","iopub.status.idle":"2022-07-10T15:01:28.437407Z","shell.execute_reply.started":"2022-07-10T15:01:28.414661Z","shell.execute_reply":"2022-07-10T15:01:28.436507Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Libraries","metadata":{}},{"cell_type":"code","source":"from getpass import getpass\nimport os\nimport glob as gb \nimport shutil\nimport pandas as pd\nimport numpy as np\nimport math\nimport cv2\nimport matplotlib.pyplot as plt\n%matplotlib inline\nfrom mpl_toolkits.axes_grid1 import ImageGrid\nfrom keras import models\nfrom keras import layers\nimport tensorflow.keras as keras\nfrom tensorflow.keras.utils import to_categorical\nfrom tensorflow.keras.utils import plot_model\nfrom sklearn.model_selection import train_test_split\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom tensorflow.keras import callbacks\nfrom tensorflow.keras.applications.resnet50 import ResNet50, preprocess_input","metadata":{"execution":{"iopub.status.busy":"2022-07-10T15:01:29.556874Z","iopub.execute_input":"2022-07-10T15:01:29.557491Z","iopub.status.idle":"2022-07-10T15:01:35.563292Z","shell.execute_reply.started":"2022-07-10T15:01:29.557455Z","shell.execute_reply":"2022-07-10T15:01:35.56234Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Explore the data","metadata":{}},{"cell_type":"code","source":"main_folder=\"/kaggle/input/state-farm-distracted-driver-detection/\"","metadata":{"execution":{"iopub.status.busy":"2022-07-10T15:01:35.566312Z","iopub.execute_input":"2022-07-10T15:01:35.567251Z","iopub.status.idle":"2022-07-10T15:01:35.571595Z","shell.execute_reply.started":"2022-07-10T15:01:35.567213Z","shell.execute_reply":"2022-07-10T15:01:35.570839Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_driver=pd.read_csv(main_folder+\"driver_imgs_list.csv\")\nlen(df_driver)","metadata":{"execution":{"iopub.status.busy":"2022-07-10T15:01:35.572946Z","iopub.execute_input":"2022-07-10T15:01:35.57367Z","iopub.status.idle":"2022-07-10T15:01:35.624642Z","shell.execute_reply.started":"2022-07-10T15:01:35.573624Z","shell.execute_reply":"2022-07-10T15:01:35.623785Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_driver.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-10T15:01:35.628318Z","iopub.execute_input":"2022-07-10T15:01:35.628572Z","iopub.status.idle":"2022-07-10T15:01:35.6477Z","shell.execute_reply.started":"2022-07-10T15:01:35.628543Z","shell.execute_reply":"2022-07-10T15:01:35.646882Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"count=0\nfile_path=main_folder+\"imgs/train\"\ncounts={}\nfor folder in os.listdir(file_path):\n    class_count=0\n    images=gb.glob(pathname=str(file_path+'/'+folder+'/*.*'))\n    for img in images:\n        count+=1\n        class_count+=1\n    num_image={f\"{folder}\":class_count}\n    counts.update(num_image)\n    print(f\"the number of images in class {folder}: {class_count}\")\n    print()\nprint()\nprint(f\"The Total Number of Images: {count}\")","metadata":{"execution":{"iopub.status.busy":"2022-07-10T15:01:35.648766Z","iopub.execute_input":"2022-07-10T15:01:35.649169Z","iopub.status.idle":"2022-07-10T15:01:37.815319Z","shell.execute_reply.started":"2022-07-10T15:01:35.649134Z","shell.execute_reply":"2022-07-10T15:01:37.814338Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(counts)","metadata":{"execution":{"iopub.status.busy":"2022-07-10T15:01:37.816809Z","iopub.execute_input":"2022-07-10T15:01:37.817151Z","iopub.status.idle":"2022-07-10T15:01:37.822431Z","shell.execute_reply.started":"2022-07-10T15:01:37.817116Z","shell.execute_reply":"2022-07-10T15:01:37.821348Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"list(counts.keys())","metadata":{"execution":{"iopub.status.busy":"2022-07-10T15:01:37.823888Z","iopub.execute_input":"2022-07-10T15:01:37.824489Z","iopub.status.idle":"2022-07-10T15:01:37.836777Z","shell.execute_reply.started":"2022-07-10T15:01:37.824454Z","shell.execute_reply":"2022-07-10T15:01:37.835655Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"list(counts.values())","metadata":{"execution":{"iopub.status.busy":"2022-07-10T15:01:37.838836Z","iopub.execute_input":"2022-07-10T15:01:37.839183Z","iopub.status.idle":"2022-07-10T15:01:37.849548Z","shell.execute_reply.started":"2022-07-10T15:01:37.839128Z","shell.execute_reply":"2022-07-10T15:01:37.848587Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig=plt.figure(figsize=(10,5))\nax = fig.add_axes([0,0,1,1])\nax.bar(list(counts.keys()) , list(counts.values()), color = 'maroon',width=0.5)","metadata":{"execution":{"iopub.status.busy":"2022-07-10T15:01:37.851183Z","iopub.execute_input":"2022-07-10T15:01:37.851524Z","iopub.status.idle":"2022-07-10T15:01:38.06159Z","shell.execute_reply.started":"2022-07-10T15:01:37.851488Z","shell.execute_reply":"2022-07-10T15:01:38.060698Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"classes_name = {  '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\"}","metadata":{"execution":{"iopub.status.busy":"2022-07-10T15:01:38.065112Z","iopub.execute_input":"2022-07-10T15:01:38.065374Z","iopub.status.idle":"2022-07-10T15:01:38.070594Z","shell.execute_reply.started":"2022-07-10T15:01:38.065348Z","shell.execute_reply":"2022-07-10T15:01:38.069591Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_driver.info()","metadata":{"execution":{"iopub.status.busy":"2022-07-10T15:01:38.072317Z","iopub.execute_input":"2022-07-10T15:01:38.072807Z","iopub.status.idle":"2022-07-10T15:01:38.10496Z","shell.execute_reply.started":"2022-07-10T15:01:38.072772Z","shell.execute_reply":"2022-07-10T15:01:38.104014Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.utils import shuffle\n\nimgs=[]\nnames=[]\nfor label in list(classes_name.keys()):\n    imgs_name=shuffle(df_driver[df_driver[\"classname\"] == label])[\"img\"][:5]\n    imgs.extend(imgs_name)\n    names.extend(((label+\" \")*5).split())\n\nprint(imgs)\nprint(names)","metadata":{"execution":{"iopub.status.busy":"2022-07-10T15:01:38.106249Z","iopub.execute_input":"2022-07-10T15:01:38.106553Z","iopub.status.idle":"2022-07-10T15:01:38.156942Z","shell.execute_reply.started":"2022-07-10T15:01:38.10652Z","shell.execute_reply":"2022-07-10T15:01:38.156116Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nfig = plt.figure(figsize=(30, 30))\ngrid = ImageGrid(fig, 111,  # similar to subplot(111)\n                 nrows_ncols=(10, 5),  # creates 2x2 grid of axes\n                 axes_pad=0.5, label_mode='all' # pad between axes in inch.\n)\ni=0\nfile_path=main_folder+\"imgs/train/\"\nfor axes in grid:\n    img=cv2.imread(file_path+names[i]+\"/\"+imgs[i])\n    rgb_img=cv2.cvtColor(img, cv2.COLOR_BGR2RGB)\n    axes.imshow(rgb_img)\n    axes.xaxis.set_visible(False)\n    axes.yaxis.set_visible(False)\n    axes.set_title(classes_name.get(names[i]))\n    i+=1\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-10T15:01:38.158532Z","iopub.execute_input":"2022-07-10T15:01:38.158997Z","iopub.status.idle":"2022-07-10T15:01:44.146847Z","shell.execute_reply.started":"2022-07-10T15:01:38.158958Z","shell.execute_reply":"2022-07-10T15:01:44.142901Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Working Without Data Augmentation","metadata":{}},{"cell_type":"markdown","source":"### Preparing Data","metadata":{}},{"cell_type":"code","source":"df_driver.info()","metadata":{"execution":{"iopub.status.busy":"2022-07-10T15:01:44.148009Z","iopub.execute_input":"2022-07-10T15:01:44.149082Z","iopub.status.idle":"2022-07-10T15:01:44.171673Z","shell.execute_reply.started":"2022-07-10T15:01:44.149033Z","shell.execute_reply":"2022-07-10T15:01:44.170455Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_data=df_driver[\"img\"]\ny_data=df_driver[\"classname\"]","metadata":{"execution":{"iopub.status.busy":"2022-07-10T15:01:44.173396Z","iopub.execute_input":"2022-07-10T15:01:44.174019Z","iopub.status.idle":"2022-07-10T15:01:44.179769Z","shell.execute_reply.started":"2022-07-10T15:01:44.17398Z","shell.execute_reply":"2022-07-10T15:01:44.178544Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train,x_test,y_train,y_test=train_test_split(x_data,y_data,test_size = 0.4, random_state = 42,shuffle=True)","metadata":{"execution":{"iopub.status.busy":"2022-07-10T15:01:44.182077Z","iopub.execute_input":"2022-07-10T15:01:44.18304Z","iopub.status.idle":"2022-07-10T15:01:44.196493Z","shell.execute_reply.started":"2022-07-10T15:01:44.182996Z","shell.execute_reply":"2022-07-10T15:01:44.194985Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_valid,xtest,y_valid,ytest=train_test_split(x_test,y_test,test_size = 0.4, random_state = 42,shuffle=True)","metadata":{"execution":{"iopub.status.busy":"2022-07-10T15:01:44.198545Z","iopub.execute_input":"2022-07-10T15:01:44.199241Z","iopub.status.idle":"2022-07-10T15:01:44.207582Z","shell.execute_reply.started":"2022-07-10T15:01:44.199208Z","shell.execute_reply":"2022-07-10T15:01:44.206343Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train.value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-07-10T15:01:44.214081Z","iopub.execute_input":"2022-07-10T15:01:44.214656Z","iopub.status.idle":"2022-07-10T15:01:44.225528Z","shell.execute_reply.started":"2022-07-10T15:01:44.214623Z","shell.execute_reply":"2022-07-10T15:01:44.224541Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_valid.value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-07-10T15:01:44.226891Z","iopub.execute_input":"2022-07-10T15:01:44.227465Z","iopub.status.idle":"2022-07-10T15:01:44.238501Z","shell.execute_reply.started":"2022-07-10T15:01:44.227429Z","shell.execute_reply":"2022-07-10T15:01:44.237205Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ytest.value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-07-10T15:01:44.240438Z","iopub.execute_input":"2022-07-10T15:01:44.2411Z","iopub.status.idle":"2022-07-10T15:01:44.250871Z","shell.execute_reply.started":"2022-07-10T15:01:44.241061Z","shell.execute_reply":"2022-07-10T15:01:44.24974Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train = pd.DataFrame(x_train.reset_index(drop=True))\nx_valid = pd.DataFrame(x_valid.reset_index(drop=True))\nxtest = pd.DataFrame(xtest.reset_index(drop=True))\n\ny_train = pd.DataFrame(y_train.reset_index(drop=True))\ny_valid = pd.DataFrame(y_valid.reset_index(drop=True))\nytest = pd.DataFrame( ytest.reset_index(drop=True))","metadata":{"execution":{"iopub.status.busy":"2022-07-10T15:01:44.253087Z","iopub.execute_input":"2022-07-10T15:01:44.25388Z","iopub.status.idle":"2022-07-10T15:01:44.267362Z","shell.execute_reply.started":"2022-07-10T15:01:44.253824Z","shell.execute_reply":"2022-07-10T15:01:44.266452Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-10T15:01:44.268817Z","iopub.execute_input":"2022-07-10T15:01:44.269478Z","iopub.status.idle":"2022-07-10T15:01:44.280854Z","shell.execute_reply.started":"2022-07-10T15:01:44.269442Z","shell.execute_reply":"2022-07-10T15:01:44.279564Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-10T15:01:44.282326Z","iopub.execute_input":"2022-07-10T15:01:44.282956Z","iopub.status.idle":"2022-07-10T15:01:44.294224Z","shell.execute_reply.started":"2022-07-10T15:01:44.28292Z","shell.execute_reply":"2022-07-10T15:01:44.292969Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"source_path=main_folder+\"imgs/train/\"\ndist=\"/kaggle/working/\"\ntrain_path=dist+\"img/train/\"\nvalid_path=dist+\"img/valid/\"\ntest_path=dist+\"img/test/\"\nif os.path.exists(dist+\"img\"):\n    shutil.rmtree(dist+\"img\")\nos.makedirs(train_path)\nos.makedirs(valid_path)\nos.makedirs(test_path)","metadata":{"execution":{"iopub.status.busy":"2022-07-10T15:01:44.295664Z","iopub.execute_input":"2022-07-10T15:01:44.296394Z","iopub.status.idle":"2022-07-10T15:01:44.303858Z","shell.execute_reply.started":"2022-07-10T15:01:44.296351Z","shell.execute_reply":"2022-07-10T15:01:44.302694Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in range(len(x_train)):\n    class_name=y_train['classname'][i]\n    img=x_train['img'][i]\n    if os.path.exists(train_path + class_name):\n        shutil.copy(source_path + class_name+\"/\"+img , train_path + class_name+\"/\"+img)\n    else:\n        os.makedirs(train_path + class_name)\n\nfor i in range(len(x_valid)):\n    class_name=y_valid['classname'][i]\n    img=x_valid['img'][i]\n    if os.path.exists(valid_path + class_name):\n        shutil.copy(source_path + class_name+\"/\"+img , valid_path + class_name+\"/\"+img)\n    else:\n        os.makedirs(valid_path + class_name)\n\n\n\nfor i in range(len(xtest)):\n    class_name=ytest['classname'][i]\n    img=xtest['img'][i]\n    if os.path.exists(test_path + class_name):\n        shutil.copy(source_path + class_name+\"/\"+img , test_path + class_name+\"/\"+img)  \n    else:\n        os.makedirs(test_path + class_name)\n","metadata":{"execution":{"iopub.status.busy":"2022-07-10T15:01:44.305682Z","iopub.execute_input":"2022-07-10T15:01:44.306478Z","iopub.status.idle":"2022-07-10T15:03:24.371771Z","shell.execute_reply.started":"2022-07-10T15:01:44.306424Z","shell.execute_reply":"2022-07-10T15:03:24.370799Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"normal_datagen = ImageDataGenerator(rescale=1 / 255.0)","metadata":{"execution":{"iopub.status.busy":"2022-07-10T15:03:24.373103Z","iopub.execute_input":"2022-07-10T15:03:24.37344Z","iopub.status.idle":"2022-07-10T15:03:24.380413Z","shell.execute_reply.started":"2022-07-10T15:03:24.373406Z","shell.execute_reply":"2022-07-10T15:03:24.378851Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"example=main_folder+\"imgs/train/c0/img_10003.jpg\"\nimage=cv2.imread(example)\nimage_resize=cv2.resize(image,(224,224))\nimg=np.array(image_resize).reshape(1,224,224,3)","metadata":{"execution":{"iopub.status.busy":"2022-07-10T15:03:24.381665Z","iopub.execute_input":"2022-07-10T15:03:24.382175Z","iopub.status.idle":"2022-07-10T15:03:24.487211Z","shell.execute_reply.started":"2022-07-10T15:03:24.382138Z","shell.execute_reply":"2022-07-10T15:03:24.486169Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_1=[0]\ny_1=to_categorical(y_1,10)\ny_1=np.array(y_1).reshape(1,-1)\nprint(y_1)\n","metadata":{"execution":{"iopub.status.busy":"2022-07-10T15:03:24.488807Z","iopub.execute_input":"2022-07-10T15:03:24.489244Z","iopub.status.idle":"2022-07-10T15:03:24.495723Z","shell.execute_reply.started":"2022-07-10T15:03:24.489209Z","shell.execute_reply":"2022-07-10T15:03:24.494719Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_one_image_generator= normal_datagen.flow(\n    img,\n    y_1,\n    batch_size=32\n)\n\n\n\nplt.figure(figsize=(12, 12))\nfor i in range(0, 9):\n    plt.subplot(3, 3, i+1)\n    for X_batch, Y_batch in train_one_image_generator:\n        image = X_batch[0]\n        plt.imshow(image)\n        plt.yticks([])\n        plt.xticks([])\n        plt.title(classes_name.get(\"c\"+str(int(np.argmax(Y_batch[0])))))\n        break\nplt.tight_layout()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-10T15:03:24.497478Z","iopub.execute_input":"2022-07-10T15:03:24.497888Z","iopub.status.idle":"2022-07-10T15:03:25.208478Z","shell.execute_reply.started":"2022-07-10T15:03:24.497853Z","shell.execute_reply":"2022-07-10T15:03:25.205265Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"##  Training The data Using Different Approaches","metadata":{}},{"cell_type":"code","source":"epochs = 50\nbatchs = 32","metadata":{"execution":{"iopub.status.busy":"2022-07-10T15:03:25.209857Z","iopub.execute_input":"2022-07-10T15:03:25.210781Z","iopub.status.idle":"2022-07-10T15:03:25.215148Z","shell.execute_reply.started":"2022-07-10T15:03:25.210744Z","shell.execute_reply":"2022-07-10T15:03:25.21435Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_generator=normal_datagen.flow_from_directory(\n    directory=train_path,\n    target_size=(224, 224),\n    batch_size=batchs,\n    class_mode=\"categorical\",\n    shuffle=True\n)\n\nvalid_generator=normal_datagen.flow_from_directory(\n    directory=valid_path,\n    target_size=(224, 224),\n    batch_size=batchs,\n    class_mode=\"categorical\",\n    shuffle=True\n)\n\n\ntest_generator=normal_datagen.flow_from_directory(\n    directory=test_path,\n    target_size=(224, 224),\n    batch_size=1,\n    class_mode=\"categorical\",\n    shuffle=True\n)","metadata":{"execution":{"iopub.status.busy":"2022-07-10T15:03:25.216926Z","iopub.execute_input":"2022-07-10T15:03:25.217775Z","iopub.status.idle":"2022-07-10T15:03:25.874879Z","shell.execute_reply.started":"2022-07-10T15:03:25.217735Z","shell.execute_reply":"2022-07-10T15:03:25.87389Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"callbacks_ = [callbacks.EarlyStopping(monitor='val_loss', mode='min',patience=3)]","metadata":{"execution":{"iopub.status.busy":"2022-07-10T15:03:25.876511Z","iopub.execute_input":"2022-07-10T15:03:25.87688Z","iopub.status.idle":"2022-07-10T15:03:25.881919Z","shell.execute_reply.started":"2022-07-10T15:03:25.876843Z","shell.execute_reply":"2022-07-10T15:03:25.880889Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### 1. Buiding Basline Dense Model","metadata":{}},{"cell_type":"code","source":"dense_model=models.Sequential()\ndense_model.add(layers.Flatten(input_shape=(224,224,3)))\ndense_model.add(layers.Dense(1024, activation='relu', name='Layer_1'))\ndense_model.add(layers.Dense(512, activation='relu', name='Layer_2'))\ndense_model.add(layers.Dense(512, activation='relu', name='Layer_3'))\ndense_model.add(layers.Dense(256, activation='relu', name='Layer_4'))\ndense_model.add(layers.Dense(128, activation='relu', name='Layer_5'))\ndense_model.add(layers.Dense(10, activation='softmax',name='final_layer'))","metadata":{"execution":{"iopub.status.busy":"2022-07-10T15:03:25.883585Z","iopub.execute_input":"2022-07-10T15:03:25.88396Z","iopub.status.idle":"2022-07-10T15:03:28.833689Z","shell.execute_reply.started":"2022-07-10T15:03:25.883925Z","shell.execute_reply":"2022-07-10T15:03:28.832769Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dense_model.summary()","metadata":{"execution":{"iopub.status.busy":"2022-07-10T15:03:28.83489Z","iopub.execute_input":"2022-07-10T15:03:28.835231Z","iopub.status.idle":"2022-07-10T15:03:28.842771Z","shell.execute_reply.started":"2022-07-10T15:03:28.835195Z","shell.execute_reply":"2022-07-10T15:03:28.841837Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_model(dense_model)","metadata":{"execution":{"iopub.status.busy":"2022-07-10T15:03:28.844184Z","iopub.execute_input":"2022-07-10T15:03:28.844888Z","iopub.status.idle":"2022-07-10T15:03:29.763849Z","shell.execute_reply.started":"2022-07-10T15:03:28.844849Z","shell.execute_reply":"2022-07-10T15:03:29.762749Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"opt = keras.optimizers.Adam()\ndense_model.compile(optimizer=opt,\n                loss='categorical_crossentropy',\n                metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2022-07-10T15:03:29.766369Z","iopub.execute_input":"2022-07-10T15:03:29.767115Z","iopub.status.idle":"2022-07-10T15:03:29.784571Z","shell.execute_reply.started":"2022-07-10T15:03:29.767074Z","shell.execute_reply":"2022-07-10T15:03:29.78361Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history_1=dense_model.fit(train_generator,\n                          validation_data=valid_generator,\n                          epochs=10,\n                          verbose=1,\n                          callbacks=callbacks_)","metadata":{"execution":{"iopub.status.busy":"2022-07-10T15:03:29.786959Z","iopub.execute_input":"2022-07-10T15:03:29.787212Z","iopub.status.idle":"2022-07-10T15:13:46.046674Z","shell.execute_reply.started":"2022-07-10T15:03:29.787188Z","shell.execute_reply":"2022-07-10T15:13:46.045715Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_loss, test_acc = dense_model.evaluate(test_generator)","metadata":{"execution":{"iopub.status.busy":"2022-07-10T15:13:46.055662Z","iopub.execute_input":"2022-07-10T15:13:46.055953Z","iopub.status.idle":"2022-07-10T15:14:11.548344Z","shell.execute_reply.started":"2022-07-10T15:13:46.055926Z","shell.execute_reply":"2022-07-10T15:14:11.547393Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Loss curve\nplt.figure(figsize=[6,4])\nplt.plot(history_1.history['loss'], 'black', linewidth=2.0)\nplt.plot(history_1.history['val_loss'], 'green', linewidth=2.0)\nplt.legend(['Training Loss', 'Validation Loss'], fontsize=14)\nplt.xlabel('Epochs', fontsize=14)\nplt.ylabel('Loss', fontsize=14)\nplt.xticks(range(0,10))\nplt.title('Loss Curves', fontsize=15)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-10T15:14:11.550125Z","iopub.execute_input":"2022-07-10T15:14:11.550816Z","iopub.status.idle":"2022-07-10T15:14:11.760767Z","shell.execute_reply.started":"2022-07-10T15:14:11.550775Z","shell.execute_reply":"2022-07-10T15:14:11.759756Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Accuracy curve\nplt.figure(figsize=[6,4])\nplt.plot(history_1.history['accuracy'], 'black', linewidth=2.0)\nplt.plot(history_1.history['val_accuracy'], 'blue', linewidth=2.0)\nplt.legend(['Training Accuracy', 'Validation Accuracy'], fontsize=14)\nplt.xlabel('Epochs', fontsize=14)\nplt.ylabel('Accuracy', fontsize=14)\nplt.xticks(range(0,10))\nplt.title('Accuracy Curves', fontsize=15)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-10T15:14:11.762033Z","iopub.execute_input":"2022-07-10T15:14:11.762385Z","iopub.status.idle":"2022-07-10T15:14:11.960642Z","shell.execute_reply.started":"2022-07-10T15:14:11.762348Z","shell.execute_reply":"2022-07-10T15:14:11.959735Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(20, 20))\nfor i in range(0, 25):\n    plt.subplot(5, 5, i+1)\n    for X_batch, Y_batch in test_generator:\n        print(Y_batch)\n        image = X_batch[0]\n        plt.imshow(image)\n        x=np.array(image).reshape(1,224,224,3)\n        pred=dense_model.predict(x)\n        true_label=classes_name.get(\"c\"+str(int(np.argmax(Y_batch[0]))))\n        pred_label=classes_name.get(\"c\"+str(int(np.argmax(pred))))\n        plt.yticks([])\n        plt.xticks([])\n        plt.title(f\"True: {true_label}\\n pred: {pred_label}\")\n        break\nplt.tight_layout()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-10T15:14:11.962154Z","iopub.execute_input":"2022-07-10T15:14:11.962517Z","iopub.status.idle":"2022-07-10T15:14:15.485386Z","shell.execute_reply.started":"2022-07-10T15:14:11.96248Z","shell.execute_reply":"2022-07-10T15:14:15.484485Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"\n#### 2. Building Baseline CNN Model","metadata":{}},{"cell_type":"code","source":"cnn_model = models.Sequential()\ncnn_model.add(layers.Conv2D(32, (3, 3), activation='relu', input_shape=(224, 224,3)))\ncnn_model.add(layers.MaxPooling2D((2, 2)))\ncnn_model.add(layers.Conv2D(64, (3, 3), activation='relu'))\ncnn_model.add(layers.MaxPooling2D((2, 2)))\ncnn_model.add(layers.Conv2D(128, (3, 3), activation='relu'))\ncnn_model.add(layers.MaxPooling2D((2, 2)))\ncnn_model.add(layers.Conv2D(128, (3, 3), activation='relu'))\ncnn_model.add(layers.MaxPooling2D((2, 2)))\ncnn_model.add(layers.Flatten())\ncnn_model.add(layers.Dense(128, activation='relu'))\ncnn_model.add(layers.Dense(10, activation='softmax'))\n","metadata":{"execution":{"iopub.status.busy":"2022-07-10T15:14:15.486848Z","iopub.execute_input":"2022-07-10T15:14:15.487858Z","iopub.status.idle":"2022-07-10T15:14:15.584976Z","shell.execute_reply.started":"2022-07-10T15:14:15.487803Z","shell.execute_reply":"2022-07-10T15:14:15.582975Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cnn_model.summary()","metadata":{"execution":{"iopub.status.busy":"2022-07-10T15:14:15.586791Z","iopub.execute_input":"2022-07-10T15:14:15.587159Z","iopub.status.idle":"2022-07-10T15:14:15.594684Z","shell.execute_reply.started":"2022-07-10T15:14:15.587123Z","shell.execute_reply":"2022-07-10T15:14:15.593599Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_model(cnn_model)","metadata":{"execution":{"iopub.status.busy":"2022-07-10T15:14:15.595965Z","iopub.execute_input":"2022-07-10T15:14:15.596688Z","iopub.status.idle":"2022-07-10T15:14:15.750403Z","shell.execute_reply.started":"2022-07-10T15:14:15.596652Z","shell.execute_reply":"2022-07-10T15:14:15.749195Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"opt = keras.optimizers.Adam()\ncnn_model.compile(optimizer=opt,\n                loss='categorical_crossentropy',\n                metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2022-07-10T15:14:15.752424Z","iopub.execute_input":"2022-07-10T15:14:15.753083Z","iopub.status.idle":"2022-07-10T15:14:15.765471Z","shell.execute_reply.started":"2022-07-10T15:14:15.753041Z","shell.execute_reply":"2022-07-10T15:14:15.764343Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history_2=cnn_model.fit(train_generator,\n                          validation_data=valid_generator,\n                          epochs=10, \n                          verbose=1,\n                          callbacks=callbacks_)","metadata":{"execution":{"iopub.status.busy":"2022-07-10T15:14:15.766681Z","iopub.execute_input":"2022-07-10T15:14:15.76804Z","iopub.status.idle":"2022-07-10T15:28:38.28097Z","shell.execute_reply.started":"2022-07-10T15:14:15.768001Z","shell.execute_reply":"2022-07-10T15:28:38.280058Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_loss, test_acc = cnn_model.evaluate(test_generator)","metadata":{"execution":{"iopub.status.busy":"2022-07-10T15:28:38.282495Z","iopub.execute_input":"2022-07-10T15:28:38.282777Z","iopub.status.idle":"2022-07-10T15:29:48.301916Z","shell.execute_reply.started":"2022-07-10T15:28:38.282752Z","shell.execute_reply":"2022-07-10T15:29:48.300966Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Loss curve\nplt.figure(figsize=[6,4])\nplt.plot(history_2.history['loss'], 'black', linewidth=2.0)\nplt.plot(history_2.history['val_loss'], 'green', linewidth=2.0)\nplt.legend(['Training Loss', 'Validation Loss'], fontsize=14)\nplt.xlabel('Epochs', fontsize=14)\nplt.ylabel('Loss', fontsize=14)\nplt.xticks(range(0,10))\nplt.title('Loss Curves', fontsize=15)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-10T15:29:48.304267Z","iopub.execute_input":"2022-07-10T15:29:48.304627Z","iopub.status.idle":"2022-07-10T15:29:48.517137Z","shell.execute_reply.started":"2022-07-10T15:29:48.304591Z","shell.execute_reply":"2022-07-10T15:29:48.516206Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Accuracy curve\nplt.figure(figsize=[6,4])\nplt.plot(history_2.history['accuracy'], 'black', linewidth=2.0)\nplt.plot(history_2.history['val_accuracy'], 'blue', linewidth=2.0)\nplt.legend(['Training Accuracy', 'Validation Accuracy'], fontsize=14)\nplt.xlabel('Epochs', fontsize=14)\nplt.ylabel('Accuracy', fontsize=14)\nplt.xticks(range(0,10))\nplt.title('Accuracy Curves', fontsize=15)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-10T15:29:48.518412Z","iopub.execute_input":"2022-07-10T15:29:48.519114Z","iopub.status.idle":"2022-07-10T15:29:48.720021Z","shell.execute_reply.started":"2022-07-10T15:29:48.519073Z","shell.execute_reply":"2022-07-10T15:29:48.719065Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(20, 20))\nfor i in range(0, 25):\n    plt.subplot(5, 5, i+1)\n    for X_batch, Y_batch in test_generator:\n        print(Y_batch)\n        image = X_batch[0]\n        plt.imshow(image)\n        x=np.array(image).reshape(1,224,224,3)\n        pred=cnn_model.predict(x)\n        true_label=classes_name.get(\"c\"+str(int(np.argmax(Y_batch[0]))))\n        pred_label=classes_name.get(\"c\"+str(int(np.argmax(pred))))\n        plt.yticks([])\n        plt.xticks([])\n        plt.title(f\"True: {true_label}\\n pred: {pred_label}\")\n        break\nplt.tight_layout()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-10T15:29:48.721494Z","iopub.execute_input":"2022-07-10T15:29:48.722085Z","iopub.status.idle":"2022-07-10T15:29:52.493151Z","shell.execute_reply.started":"2022-07-10T15:29:48.722046Z","shell.execute_reply":"2022-07-10T15:29:52.491693Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### CNN visualization","metadata":{}},{"cell_type":"code","source":"example=main_folder+\"imgs/train/c0/img_10003.jpg\"\nimage=cv2.imread(example)\nimage_resize=cv2.resize(image,(224,224))\nimg=np.array(image_resize).reshape(1,224,224,3)","metadata":{"execution":{"iopub.status.busy":"2022-07-10T15:29:52.494736Z","iopub.execute_input":"2022-07-10T15:29:52.495318Z","iopub.status.idle":"2022-07-10T15:29:52.513697Z","shell.execute_reply.started":"2022-07-10T15:29:52.495281Z","shell.execute_reply":"2022-07-10T15:29:52.512876Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Extracts the outputs of the top 7 layers:\nlayer_outputs = [layer.output for layer in cnn_model.layers[:7]]\n\n# Creates a model that will return these outputs, given the model input:\nactivation_model = models.Model(inputs=cnn_model.input, outputs=layer_outputs)\n","metadata":{"execution":{"iopub.status.busy":"2022-07-10T15:29:52.515086Z","iopub.execute_input":"2022-07-10T15:29:52.515614Z","iopub.status.idle":"2022-07-10T15:29:52.525817Z","shell.execute_reply.started":"2022-07-10T15:29:52.515579Z","shell.execute_reply":"2022-07-10T15:29:52.524357Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"activation_model.summary()","metadata":{"execution":{"iopub.status.busy":"2022-07-10T15:29:52.527799Z","iopub.execute_input":"2022-07-10T15:29:52.528462Z","iopub.status.idle":"2022-07-10T15:29:52.535079Z","shell.execute_reply.started":"2022-07-10T15:29:52.528425Z","shell.execute_reply":"2022-07-10T15:29:52.534217Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"activations = activation_model.predict(img)\nprint(len(activations))","metadata":{"execution":{"iopub.status.busy":"2022-07-10T15:29:52.53658Z","iopub.execute_input":"2022-07-10T15:29:52.537259Z","iopub.status.idle":"2022-07-10T15:29:52.674663Z","shell.execute_reply.started":"2022-07-10T15:29:52.537224Z","shell.execute_reply":"2022-07-10T15:29:52.67249Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# These are the names of the layers, so can have them as part of our plot\nlayer_names = []\nfor layer in cnn_model.layers[:7]:\n    layer_names.append(layer.name)\n\nimages_per_row = 16\n\n# Now let's display our feature maps\nfor layer_name, layer_activation in zip(layer_names, activations):\n    # This is the number of features in the feature map\n    n_features = layer_activation.shape[-1]\n\n    # The feature map has shape (1, size, size, n_features)\n    size = layer_activation.shape[1]\n\n    # We will tile the activation channels in this matrix\n    n_cols = n_features // images_per_row\n    display_grid = np.zeros((size * n_cols, images_per_row * size))\n\n    # We'll tile each filter into this big horizontal grid\n    for col in range(n_cols):\n        for row in range(images_per_row):\n            channel_image = layer_activation[0,\n                                             :, :,\n                                             col * images_per_row + row]\n            # Post-process the feature to make it visually palatable\n            channel_image -= channel_image.mean()\n            channel_image /= channel_image.std()\n            channel_image *= 64\n            channel_image += 128\n            channel_image = np.clip(channel_image, 0, 255).astype('uint8')\n            display_grid[col * size : (col + 1) * size,\n                         row * size : (row + 1) * size] = channel_image\n\n    # Display the grid\n    scale = 1. / size\n    plt.figure(figsize=(scale * display_grid.shape[1],\n                        scale * display_grid.shape[0]))\n    plt.title(layer_name)\n    plt.grid(False)\n    plt.imshow(display_grid, aspect='auto', cmap='viridis')\n    \nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-10T15:29:52.676172Z","iopub.execute_input":"2022-07-10T15:29:52.67647Z","iopub.status.idle":"2022-07-10T15:29:54.887168Z","shell.execute_reply.started":"2022-07-10T15:29:52.676444Z","shell.execute_reply":"2022-07-10T15:29:54.886242Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### 3. Using Transfer Learning Without Fine_Tuning","metadata":{}},{"cell_type":"code","source":"conv_base = ResNet50(weights='imagenet', include_top=False, input_shape=(224, 224, 3))","metadata":{"execution":{"iopub.status.busy":"2022-07-10T15:29:54.888748Z","iopub.execute_input":"2022-07-10T15:29:54.889142Z","iopub.status.idle":"2022-07-10T15:29:56.799385Z","shell.execute_reply.started":"2022-07-10T15:29:54.889105Z","shell.execute_reply":"2022-07-10T15:29:56.798404Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"conv_base.summary()","metadata":{"execution":{"iopub.status.busy":"2022-07-10T15:29:56.800989Z","iopub.execute_input":"2022-07-10T15:29:56.801655Z","iopub.status.idle":"2022-07-10T15:29:56.843897Z","shell.execute_reply.started":"2022-07-10T15:29:56.801618Z","shell.execute_reply":"2022-07-10T15:29:56.842878Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"conv_base.trainable=False","metadata":{"execution":{"iopub.status.busy":"2022-07-10T15:29:56.845478Z","iopub.execute_input":"2022-07-10T15:29:56.846143Z","iopub.status.idle":"2022-07-10T15:29:56.860311Z","shell.execute_reply.started":"2022-07-10T15:29:56.846107Z","shell.execute_reply":"2022-07-10T15:29:56.858884Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"res_model = models.Sequential()\nres_model.add(conv_base)\nres_model.add(layers.Flatten())\nres_model.add(layers.Dense(128, activation='relu'))\nres_model.add(layers.Dense(64, activation='relu'))\nres_model.add(layers.Dense(10, activation='softmax'))","metadata":{"execution":{"iopub.status.busy":"2022-07-10T15:29:56.862264Z","iopub.execute_input":"2022-07-10T15:29:56.86292Z","iopub.status.idle":"2022-07-10T15:29:57.284914Z","shell.execute_reply.started":"2022-07-10T15:29:56.862884Z","shell.execute_reply":"2022-07-10T15:29:57.28399Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"res_model.summary()","metadata":{"execution":{"iopub.status.busy":"2022-07-10T15:29:57.286435Z","iopub.execute_input":"2022-07-10T15:29:57.286759Z","iopub.status.idle":"2022-07-10T15:29:57.30551Z","shell.execute_reply.started":"2022-07-10T15:29:57.286723Z","shell.execute_reply":"2022-07-10T15:29:57.304495Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_model(res_model)","metadata":{"execution":{"iopub.status.busy":"2022-07-10T15:29:57.30699Z","iopub.execute_input":"2022-07-10T15:29:57.307344Z","iopub.status.idle":"2022-07-10T15:29:57.459965Z","shell.execute_reply.started":"2022-07-10T15:29:57.307308Z","shell.execute_reply":"2022-07-10T15:29:57.458767Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"opt = keras.optimizers.Adam()\nres_model.compile(optimizer=opt,\n                loss='categorical_crossentropy',\n                metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2022-07-10T15:29:57.461651Z","iopub.execute_input":"2022-07-10T15:29:57.462383Z","iopub.status.idle":"2022-07-10T15:29:57.479739Z","shell.execute_reply.started":"2022-07-10T15:29:57.46235Z","shell.execute_reply":"2022-07-10T15:29:57.478709Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history_3=res_model.fit(train_generator,\n                          validation_data=valid_generator,\n                          epochs=10, \n                          verbose=1,\n                          callbacks=callbacks_)","metadata":{"execution":{"iopub.status.busy":"2022-07-10T15:29:57.482178Z","iopub.execute_input":"2022-07-10T15:29:57.482427Z","iopub.status.idle":"2022-07-10T15:47:32.189148Z","shell.execute_reply.started":"2022-07-10T15:29:57.482403Z","shell.execute_reply":"2022-07-10T15:47:32.188247Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_loss, test_acc = res_model.evaluate(test_generator)","metadata":{"execution":{"iopub.status.busy":"2022-07-10T15:47:32.190679Z","iopub.execute_input":"2022-07-10T15:47:32.191138Z","iopub.status.idle":"2022-07-10T15:48:54.159153Z","shell.execute_reply.started":"2022-07-10T15:47:32.191098Z","shell.execute_reply":"2022-07-10T15:48:54.158183Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Loss curve\nplt.figure(figsize=[6,4])\nplt.plot(history_3.history['loss'], 'black', linewidth=2.0)\nplt.plot(history_3.history['val_loss'], 'green', linewidth=2.0)\nplt.legend(['Training Loss', 'Validation Loss'], fontsize=14)\nplt.xlabel('Epochs', fontsize=14)\nplt.ylabel('Loss', fontsize=14)\nplt.xticks(range(0,10))\nplt.title('Loss Curves', fontsize=15)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-10T15:48:54.160522Z","iopub.execute_input":"2022-07-10T15:48:54.160793Z","iopub.status.idle":"2022-07-10T15:48:54.374692Z","shell.execute_reply.started":"2022-07-10T15:48:54.160767Z","shell.execute_reply":"2022-07-10T15:48:54.373748Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Accuracy curve\nplt.figure(figsize=[6,4])\nplt.plot(history_2.history['accuracy'], 'black', linewidth=2.0)\nplt.plot(history_2.history['val_accuracy'], 'blue', linewidth=2.0)\nplt.legend(['Training Accuracy', 'Validation Accuracy'], fontsize=14)\nplt.xlabel('Epochs', fontsize=14)\nplt.ylabel('Accuracy', fontsize=14)\nplt.xticks(range(0,10))\nplt.title('Accuracy Curves', fontsize=15)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-10T15:48:54.376454Z","iopub.execute_input":"2022-07-10T15:48:54.377192Z","iopub.status.idle":"2022-07-10T15:48:54.581205Z","shell.execute_reply.started":"2022-07-10T15:48:54.377148Z","shell.execute_reply":"2022-07-10T15:48:54.580228Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(20, 20))\nfor i in range(0, 25):\n    plt.subplot(5, 5, i+1)\n    for X_batch, Y_batch in test_generator:\n        print(Y_batch)\n        image = X_batch[0]\n        plt.imshow(image)\n        x=np.array(image).reshape(1,224,224,3)\n        pred=res_model.predict(x)\n        true_label=classes_name.get(\"c\"+str(int(np.argmax(Y_batch[0]))))\n        pred_label=classes_name.get(\"c\"+str(int(np.argmax(pred))))\n        plt.yticks([])\n        plt.xticks([])\n        plt.title(f\"True: {true_label}\\n pred: {pred_label}\")\n        break\nplt.tight_layout()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-10T15:48:54.582624Z","iopub.execute_input":"2022-07-10T15:48:54.582975Z","iopub.status.idle":"2022-07-10T15:48:59.32507Z","shell.execute_reply.started":"2022-07-10T15:48:54.58294Z","shell.execute_reply":"2022-07-10T15:48:59.324164Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### 4.Using Transfer Learning With Fine_Tuning ","metadata":{}},{"cell_type":"code","source":"conv_base.trainable = True","metadata":{"execution":{"iopub.status.busy":"2022-07-10T15:48:59.326627Z","iopub.execute_input":"2022-07-10T15:48:59.327188Z","iopub.status.idle":"2022-07-10T15:48:59.339455Z","shell.execute_reply.started":"2022-07-10T15:48:59.327153Z","shell.execute_reply":"2022-07-10T15:48:59.338397Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i, layer in enumerate(conv_base.layers):\n    print(i, layer.name)","metadata":{"execution":{"iopub.status.busy":"2022-07-10T15:48:59.34124Z","iopub.execute_input":"2022-07-10T15:48:59.341717Z","iopub.status.idle":"2022-07-10T15:48:59.355486Z","shell.execute_reply.started":"2022-07-10T15:48:59.341672Z","shell.execute_reply":"2022-07-10T15:48:59.354225Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"set_trainable = False\nfor layer in conv_base.layers:\n    if (layer.name).startswith('conv5_block3_3'):\n      #print(layer.name)\n      set_trainable = True\n    if set_trainable:\n        print(True)\n        layer.trainable = True\n    else:\n        layer.trainable = False","metadata":{"execution":{"iopub.status.busy":"2022-07-10T15:48:59.357219Z","iopub.execute_input":"2022-07-10T15:48:59.358116Z","iopub.status.idle":"2022-07-10T15:48:59.374071Z","shell.execute_reply.started":"2022-07-10T15:48:59.358074Z","shell.execute_reply":"2022-07-10T15:48:59.373285Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"res_model_2 = models.Sequential()\nres_model_2.add(conv_base)\nres_model_2.add(layers.Flatten())\nres_model_2.add(layers.Dense(128, activation='relu'))\nres_model_2.add(layers.Dense(64, activation='relu'))\nres_model_2.add(layers.Dense(10, activation='softmax'))\n","metadata":{"execution":{"iopub.status.busy":"2022-07-10T15:48:59.375238Z","iopub.execute_input":"2022-07-10T15:48:59.377205Z","iopub.status.idle":"2022-07-10T15:48:59.788717Z","shell.execute_reply.started":"2022-07-10T15:48:59.377158Z","shell.execute_reply":"2022-07-10T15:48:59.787829Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"res_model_2.summary()","metadata":{"execution":{"iopub.status.busy":"2022-07-10T15:48:59.790168Z","iopub.execute_input":"2022-07-10T15:48:59.790489Z","iopub.status.idle":"2022-07-10T15:48:59.806766Z","shell.execute_reply.started":"2022-07-10T15:48:59.790454Z","shell.execute_reply":"2022-07-10T15:48:59.805842Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_model(res_model_2)","metadata":{"execution":{"iopub.status.busy":"2022-07-10T15:48:59.808147Z","iopub.execute_input":"2022-07-10T15:48:59.808506Z","iopub.status.idle":"2022-07-10T15:48:59.96401Z","shell.execute_reply.started":"2022-07-10T15:48:59.808469Z","shell.execute_reply":"2022-07-10T15:48:59.962851Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"opt = keras.optimizers.Adam()\nres_model_2.compile(optimizer=opt,\n                loss='categorical_crossentropy',\n                metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2022-07-10T15:48:59.965991Z","iopub.execute_input":"2022-07-10T15:48:59.966632Z","iopub.status.idle":"2022-07-10T15:48:59.982559Z","shell.execute_reply.started":"2022-07-10T15:48:59.96659Z","shell.execute_reply":"2022-07-10T15:48:59.981281Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history_4=res_model_2.fit(train_generator,\n                          validation_data=valid_generator,\n                          epochs=10, \n                          verbose=1,\n                          callbacks=callbacks_)","metadata":{"execution":{"iopub.status.busy":"2022-07-10T15:48:59.986075Z","iopub.execute_input":"2022-07-10T15:48:59.9881Z","iopub.status.idle":"2022-07-10T16:01:07.525011Z","shell.execute_reply.started":"2022-07-10T15:48:59.988072Z","shell.execute_reply":"2022-07-10T16:01:07.524082Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_loss, test_acc = res_model_2.evaluate(test_generator)","metadata":{"execution":{"iopub.status.busy":"2022-07-10T16:01:07.529492Z","iopub.execute_input":"2022-07-10T16:01:07.531077Z","iopub.status.idle":"2022-07-10T16:02:03.1582Z","shell.execute_reply.started":"2022-07-10T16:01:07.531038Z","shell.execute_reply":"2022-07-10T16:02:03.157307Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Loss curve\nplt.figure(figsize=[6,4])\nplt.plot(history_4.history['loss'], 'black', linewidth=2.0)\nplt.plot(history_4.history['val_loss'], 'green', linewidth=2.0)\nplt.legend(['Training Loss', 'Validation Loss'], fontsize=14)\nplt.xlabel('Epochs', fontsize=14)\nplt.ylabel('Loss', fontsize=14)\nplt.xticks(range(0,10))\nplt.title('Loss Curves', fontsize=15)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-10T16:02:03.161065Z","iopub.execute_input":"2022-07-10T16:02:03.161329Z","iopub.status.idle":"2022-07-10T16:02:03.364609Z","shell.execute_reply.started":"2022-07-10T16:02:03.161304Z","shell.execute_reply":"2022-07-10T16:02:03.363716Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Accuracy curve\nplt.figure(figsize=[6,4])\nplt.plot(history_4.history['accuracy'], 'black', linewidth=2.0)\nplt.plot(history_4.history['val_accuracy'], 'blue', linewidth=2.0)\nplt.legend(['Training Accuracy', 'Validation Accuracy'], fontsize=14)\nplt.xlabel('Epochs', fontsize=14)\nplt.ylabel('Accuracy', fontsize=14)\nplt.xticks(range(0,10))\nplt.title('Accuracy Curves', fontsize=15)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-10T16:02:03.366138Z","iopub.execute_input":"2022-07-10T16:02:03.366751Z","iopub.status.idle":"2022-07-10T16:02:03.575577Z","shell.execute_reply.started":"2022-07-10T16:02:03.366714Z","shell.execute_reply":"2022-07-10T16:02:03.574671Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(20, 20))\nfor i in range(0, 25):\n    plt.subplot(5, 5, i+1)\n    for X_batch, Y_batch in test_generator:\n        print(Y_batch)\n        image = X_batch[0]\n        plt.imshow(image)\n        x=np.array(image).reshape(1,224,224,3)\n        pred=res_model_2.predict(x)\n        true_label=classes_name.get(\"c\"+str(int(np.argmax(Y_batch[0]))))\n        pred_label=classes_name.get(\"c\"+str(int(np.argmax(pred))))\n        plt.yticks([])\n        plt.xticks([])\n        plt.title(f\"True: {true_label}\\n pred: {pred_label}\")\n        break\nplt.tight_layout()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-10T16:02:03.577168Z","iopub.execute_input":"2022-07-10T16:02:03.577806Z","iopub.status.idle":"2022-07-10T16:02:07.905492Z","shell.execute_reply.started":"2022-07-10T16:02:03.577767Z","shell.execute_reply":"2022-07-10T16:02:07.904632Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Training The data Using Different Approaches With Data Augmentation","metadata":{}},{"cell_type":"markdown","source":"### Preparing The Data","metadata":{}},{"cell_type":"code","source":"train_datagen = ImageDataGenerator(\n    rescale=1./255,\n    shear_range=0.1,\n    zoom_range=0.2,\n    horizontal_flip=True,\n    width_shift_range=0.1,\n    height_shift_range=0.1,\n    rotation_range=20,\n    fill_mode='nearest'\n)\n\ntest_datagen = ImageDataGenerator(rescale=1./255)","metadata":{"execution":{"iopub.status.busy":"2022-07-10T16:02:07.90696Z","iopub.execute_input":"2022-07-10T16:02:07.908072Z","iopub.status.idle":"2022-07-10T16:02:07.915953Z","shell.execute_reply.started":"2022-07-10T16:02:07.908027Z","shell.execute_reply":"2022-07-10T16:02:07.913244Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_one_image_generator= train_datagen.flow(\n    img,\n    y_1,\n    batch_size=32\n)\n\nplt.figure(figsize=(12, 12))\nfor i in range(0, 9):\n    plt.subplot(3, 3, i+1)\n    for X_batch, Y_batch in train_one_image_generator:\n        image = X_batch[0]\n        plt.imshow(image)\n        plt.yticks([])\n        plt.xticks([])\n        plt.title(classes_name.get(\"c\"+str(int(np.argmax(Y_batch[0])))))\n        break\nplt.tight_layout()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-10T16:02:07.917208Z","iopub.execute_input":"2022-07-10T16:02:07.918352Z","iopub.status.idle":"2022-07-10T16:02:09.366877Z","shell.execute_reply.started":"2022-07-10T16:02:07.918314Z","shell.execute_reply":"2022-07-10T16:02:09.366036Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_generator=train_datagen.flow_from_directory(\n    directory=train_path,\n    target_size=(224, 224),\n    batch_size=batchs,\n    class_mode=\"categorical\",\n    shuffle=True\n)\n\nvalid_generator=test_datagen.flow_from_directory(\n    directory=valid_path,\n    target_size=(224, 224),\n    batch_size=batchs,\n    class_mode=\"categorical\",\n    shuffle=True\n)\n\n\ntest_generator=test_datagen.flow_from_directory(\n    directory=test_path,\n    target_size=(224, 224),\n    batch_size=1,\n    class_mode=\"categorical\",\n    shuffle=True\n)","metadata":{"execution":{"iopub.status.busy":"2022-07-10T16:02:09.368286Z","iopub.execute_input":"2022-07-10T16:02:09.368835Z","iopub.status.idle":"2022-07-10T16:02:10.032353Z","shell.execute_reply.started":"2022-07-10T16:02:09.368788Z","shell.execute_reply":"2022-07-10T16:02:10.031241Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### 1. Buiding Basline Dense Model","metadata":{}},{"cell_type":"code","source":"dense_model_2=models.Sequential()\ndense_model_2.add(layers.Flatten(input_shape=(224,224,3)))\ndense_model_2.add(layers.Dense(1024, activation='relu', name='Layer_1'))\ndense_model_2.add(layers.Dense(512, activation='relu', name='Layer_2'))\ndense_model_2.add(layers.Dense(512, activation='relu', name='Layer_3'))\ndense_model_2.add(layers.Dense(256, activation='relu', name='Layer_4'))\ndense_model_2.add(layers.Dense(256, activation='relu', name='Layer_5'))\ndense_model_2.add(layers.Dense(128, activation='relu', name='Layer_6'))\ndense_model_2.add(layers.Dense(128, activation='relu', name='Layer_7'))\ndense_model_2.add(layers.Dense(10, activation='softmax',name='final_layer'))","metadata":{"execution":{"iopub.status.busy":"2022-07-10T16:02:10.033833Z","iopub.execute_input":"2022-07-10T16:02:10.034771Z","iopub.status.idle":"2022-07-10T16:02:10.101268Z","shell.execute_reply.started":"2022-07-10T16:02:10.03473Z","shell.execute_reply":"2022-07-10T16:02:10.100379Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dense_model_2.summary()","metadata":{"execution":{"iopub.status.busy":"2022-07-10T16:02:10.102439Z","iopub.execute_input":"2022-07-10T16:02:10.102772Z","iopub.status.idle":"2022-07-10T16:02:10.110394Z","shell.execute_reply.started":"2022-07-10T16:02:10.102738Z","shell.execute_reply":"2022-07-10T16:02:10.109255Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"opt = keras.optimizers.Adam()\ndense_model_2.compile(optimizer=opt,\n                loss='categorical_crossentropy',\n                metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2022-07-10T16:02:10.112112Z","iopub.execute_input":"2022-07-10T16:02:10.112844Z","iopub.status.idle":"2022-07-10T16:02:10.123272Z","shell.execute_reply.started":"2022-07-10T16:02:10.112791Z","shell.execute_reply":"2022-07-10T16:02:10.122164Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history_5=dense_model_2.fit(train_generator,\n                          validation_data=valid_generator,\n                          epochs=10, \n                          verbose=1,\n                          callbacks=callbacks_)","metadata":{"execution":{"iopub.status.busy":"2022-07-10T16:02:10.125321Z","iopub.execute_input":"2022-07-10T16:02:10.125742Z","iopub.status.idle":"2022-07-10T16:43:49.660688Z","shell.execute_reply.started":"2022-07-10T16:02:10.125705Z","shell.execute_reply":"2022-07-10T16:43:49.659405Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_loss, test_acc = dense_model_2.evaluate(test_generator)","metadata":{"execution":{"iopub.status.busy":"2022-07-10T16:43:49.663311Z","iopub.execute_input":"2022-07-10T16:43:49.665084Z","iopub.status.idle":"2022-07-10T16:44:30.702335Z","shell.execute_reply.started":"2022-07-10T16:43:49.665036Z","shell.execute_reply":"2022-07-10T16:44:30.701366Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Loss curve\nplt.figure(figsize=[6,4])\nplt.plot(history_5.history['loss'], 'black', linewidth=2.0)\nplt.plot(history_5.history['val_loss'], 'green', linewidth=2.0)\nplt.legend(['Training Loss', 'Validation Loss'], fontsize=14)\nplt.xlabel('Epochs', fontsize=14)\nplt.ylabel('Loss', fontsize=14)\nplt.xticks(range(0,10))\nplt.title('Loss Curves', fontsize=15)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-10T16:44:30.70381Z","iopub.execute_input":"2022-07-10T16:44:30.704267Z","iopub.status.idle":"2022-07-10T16:44:30.905725Z","shell.execute_reply.started":"2022-07-10T16:44:30.704228Z","shell.execute_reply":"2022-07-10T16:44:30.904716Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Accuracy curve\nplt.figure(figsize=[6,4])\nplt.plot(history_5.history['accuracy'], 'black', linewidth=2.0)\nplt.plot(history_5.history['val_accuracy'], 'blue', linewidth=2.0)\nplt.legend(['Training Accuracy', 'Validation Accuracy'], fontsize=14)\nplt.xlabel('Epochs', fontsize=14)\nplt.ylabel('Accuracy', fontsize=14)\nplt.xticks(range(0,10))\nplt.title('Accuracy Curves', fontsize=15)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-10T16:44:30.907201Z","iopub.execute_input":"2022-07-10T16:44:30.90812Z","iopub.status.idle":"2022-07-10T16:44:31.103651Z","shell.execute_reply.started":"2022-07-10T16:44:30.908082Z","shell.execute_reply":"2022-07-10T16:44:31.102745Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(20, 20))\nfor i in range(0, 25):\n    plt.subplot(5, 5, i+1)\n    for X_batch, Y_batch in test_generator:\n        print(Y_batch)\n        image = X_batch[0]\n        plt.imshow(image)\n        x=np.array(image).reshape(1,224,224,3)\n        pred=dense_model_2.predict(x)\n        true_label=classes_name.get(\"c\"+str(int(np.argmax(Y_batch[0]))))\n        pred_label=classes_name.get(\"c\"+str(int(np.argmax(pred))))\n        plt.yticks([])\n        plt.xticks([])\n        plt.title(f\"True: {true_label}\\n pred: {pred_label}\")\n        break\nplt.tight_layout()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-10T16:44:31.105148Z","iopub.execute_input":"2022-07-10T16:44:31.10547Z","iopub.status.idle":"2022-07-10T16:44:34.722078Z","shell.execute_reply.started":"2022-07-10T16:44:31.105435Z","shell.execute_reply":"2022-07-10T16:44:34.720993Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### 2. Building Baseline CNN Model","metadata":{}},{"cell_type":"code","source":"cnn_model_2 = models.Sequential()\ncnn_model_2.add(layers.Conv2D(32, (3, 3), activation='relu', input_shape=(224, 224,3)))\ncnn_model_2.add(layers.MaxPooling2D((2, 2)))\ncnn_model_2.add(layers.Conv2D(64, (3, 3), activation='relu'))\ncnn_model_2.add(layers.MaxPooling2D((2, 2)))\ncnn_model_2.add(layers.Conv2D(128, (3, 3), activation='relu'))\ncnn_model_2.add(layers.MaxPooling2D((2, 2)))\ncnn_model_2.add(layers.Conv2D(128, (3, 3), activation='relu'))\ncnn_model_2.add(layers.MaxPooling2D((2, 2)))\ncnn_model_2.add(layers.Flatten())\ncnn_model_2.add(layers.Dense(128, activation='relu'))\ncnn_model_2.add(layers.Dense(10, activation='softmax'))\n","metadata":{"execution":{"iopub.status.busy":"2022-07-10T16:44:34.72355Z","iopub.execute_input":"2022-07-10T16:44:34.723969Z","iopub.status.idle":"2022-07-10T16:44:34.812042Z","shell.execute_reply.started":"2022-07-10T16:44:34.723935Z","shell.execute_reply":"2022-07-10T16:44:34.811113Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cnn_model_2.summary()","metadata":{"execution":{"iopub.status.busy":"2022-07-10T16:44:34.81343Z","iopub.execute_input":"2022-07-10T16:44:34.814149Z","iopub.status.idle":"2022-07-10T16:44:34.82279Z","shell.execute_reply.started":"2022-07-10T16:44:34.814105Z","shell.execute_reply":"2022-07-10T16:44:34.821648Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_model(cnn_model_2)","metadata":{"execution":{"iopub.status.busy":"2022-07-10T16:44:34.82451Z","iopub.execute_input":"2022-07-10T16:44:34.825492Z","iopub.status.idle":"2022-07-10T16:44:35.013114Z","shell.execute_reply.started":"2022-07-10T16:44:34.825454Z","shell.execute_reply":"2022-07-10T16:44:35.011848Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"opt = keras.optimizers.Adam()\ncnn_model_2.compile(optimizer=opt,\n                loss='categorical_crossentropy',\n                metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2022-07-10T16:44:35.015307Z","iopub.execute_input":"2022-07-10T16:44:35.016103Z","iopub.status.idle":"2022-07-10T16:44:35.028539Z","shell.execute_reply.started":"2022-07-10T16:44:35.016056Z","shell.execute_reply":"2022-07-10T16:44:35.027555Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history_6=cnn_model_2.fit(train_generator,\n                          validation_data=valid_generator,\n                          epochs=10, \n                          verbose=1,\n                          callbacks=callbacks_)","metadata":{"execution":{"iopub.status.busy":"2022-07-10T16:44:35.029987Z","iopub.execute_input":"2022-07-10T16:44:35.030574Z","iopub.status.idle":"2022-07-10T17:26:40.740889Z","shell.execute_reply.started":"2022-07-10T16:44:35.030536Z","shell.execute_reply":"2022-07-10T17:26:40.739778Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_loss, test_acc = cnn_model_2.evaluate(test_generator)","metadata":{"execution":{"iopub.status.busy":"2022-07-10T17:26:40.742938Z","iopub.execute_input":"2022-07-10T17:26:40.743943Z","iopub.status.idle":"2022-07-10T17:27:05.736542Z","shell.execute_reply.started":"2022-07-10T17:26:40.743899Z","shell.execute_reply":"2022-07-10T17:27:05.735351Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Loss curve\nplt.figure(figsize=[6,4])\nplt.plot(history_6.history['loss'], 'black', linewidth=2.0)\nplt.plot(history_6.history['val_loss'], 'green', linewidth=2.0)\nplt.legend(['Training Loss', 'Validation Loss'], fontsize=14)\nplt.xlabel('Epochs', fontsize=14)\nplt.ylabel('Loss', fontsize=14)\nplt.xticks(range(0,10))\nplt.title('Loss Curves', fontsize=15)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-10T17:27:05.738284Z","iopub.execute_input":"2022-07-10T17:27:05.738669Z","iopub.status.idle":"2022-07-10T17:27:06.010812Z","shell.execute_reply.started":"2022-07-10T17:27:05.738631Z","shell.execute_reply":"2022-07-10T17:27:06.00978Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Accuracy curve\nplt.figure(figsize=[6,4])\nplt.plot(history_6.history['accuracy'], 'black', linewidth=2.0)\nplt.plot(history_6.history['val_accuracy'], 'blue', linewidth=2.0)\nplt.legend(['Training Accuracy', 'Validation Accuracy'], fontsize=14)\nplt.xlabel('Epochs', fontsize=14)\nplt.ylabel('Accuracy', fontsize=14)\nplt.xticks(range(0,10))\nplt.title('Accuracy Curves', fontsize=15)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-10T17:27:06.030055Z","iopub.execute_input":"2022-07-10T17:27:06.030706Z","iopub.status.idle":"2022-07-10T17:27:06.256243Z","shell.execute_reply.started":"2022-07-10T17:27:06.030664Z","shell.execute_reply":"2022-07-10T17:27:06.255339Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(20, 20))\nfor i in range(0, 25):\n    plt.subplot(5, 5, i+1)\n    for X_batch, Y_batch in test_generator:\n        print(Y_batch)\n        image = X_batch[0]\n        plt.imshow(image)\n        x=np.array(image).reshape(1,224,224,3)\n        pred=cnn_model_2.predict(x)\n        true_label=classes_name.get(\"c\"+str(int(np.argmax(Y_batch[0]))))\n        pred_label=classes_name.get(\"c\"+str(int(np.argmax(pred))))\n        plt.yticks([])\n        plt.xticks([])\n        plt.title(f\"True: {true_label}\\n pred: {pred_label}\")\n        break\nplt.tight_layout()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-10T17:27:06.257653Z","iopub.execute_input":"2022-07-10T17:27:06.258119Z","iopub.status.idle":"2022-07-10T17:27:09.830715Z","shell.execute_reply.started":"2022-07-10T17:27:06.258081Z","shell.execute_reply":"2022-07-10T17:27:09.829546Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### CNN visualization","metadata":{}},{"cell_type":"code","source":"example=main_folder+\"imgs/train/c0/img_10003.jpg\"\nimage=cv2.imread(example)\nimage_resize=cv2.resize(image,(224,224))\nimg=np.array(image_resize).reshape(1,224,224,3)","metadata":{"execution":{"iopub.status.busy":"2022-07-10T17:27:09.832688Z","iopub.execute_input":"2022-07-10T17:27:09.833257Z","iopub.status.idle":"2022-07-10T17:27:09.855613Z","shell.execute_reply.started":"2022-07-10T17:27:09.83322Z","shell.execute_reply":"2022-07-10T17:27:09.854693Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Extracts the outputs of the top 7 layers:\nlayer_outputs = [layer.output for layer in cnn_model_2.layers[:7]]\n\n# Creates a model that will return these outputs, given the model input:\nactivation_model = models.Model(inputs=cnn_model_2.input, outputs=layer_outputs)","metadata":{"execution":{"iopub.status.busy":"2022-07-10T17:27:09.857151Z","iopub.execute_input":"2022-07-10T17:27:09.857729Z","iopub.status.idle":"2022-07-10T17:27:09.868902Z","shell.execute_reply.started":"2022-07-10T17:27:09.857691Z","shell.execute_reply":"2022-07-10T17:27:09.867696Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"activation_model.summary()","metadata":{"execution":{"iopub.status.busy":"2022-07-10T17:27:09.870522Z","iopub.execute_input":"2022-07-10T17:27:09.871227Z","iopub.status.idle":"2022-07-10T17:27:09.879171Z","shell.execute_reply.started":"2022-07-10T17:27:09.87119Z","shell.execute_reply":"2022-07-10T17:27:09.878017Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"activations = activation_model.predict(img)\nprint(len(activations))","metadata":{"execution":{"iopub.status.busy":"2022-07-10T17:27:09.881197Z","iopub.execute_input":"2022-07-10T17:27:09.881889Z","iopub.status.idle":"2022-07-10T17:27:10.005163Z","shell.execute_reply.started":"2022-07-10T17:27:09.881854Z","shell.execute_reply":"2022-07-10T17:27:10.004221Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# These are the names of the layers, so can have them as part of our plot\nlayer_names = []\nfor layer in cnn_model.layers[:7]:\n    layer_names.append(layer.name)\n\nimages_per_row = 16\n\n# Now let's display our feature maps\nfor layer_name, layer_activation in zip(layer_names, activations):\n    # This is the number of features in the feature map\n    n_features = layer_activation.shape[-1]\n\n    # The feature map has shape (1, size, size, n_features)\n    size = layer_activation.shape[1]\n\n    # We will tile the activation channels in this matrix\n    n_cols = n_features // images_per_row\n    display_grid = np.zeros((size * n_cols, images_per_row * size))\n\n    # We'll tile each filter into this big horizontal grid\n    for col in range(n_cols):\n        for row in range(images_per_row):\n            channel_image = layer_activation[0,\n                                             :, :,\n                                             col * images_per_row + row]\n            # Post-process the feature to make it visually palatable\n            channel_image -= channel_image.mean()\n            channel_image /= channel_image.std()\n            channel_image *= 64\n            channel_image += 128\n            channel_image = np.clip(channel_image, 0, 255).astype('uint8')\n            display_grid[col * size : (col + 1) * size,\n                         row * size : (row + 1) * size] = channel_image\n\n    # Display the grid\n    scale = 1. / size\n    plt.figure(figsize=(scale * display_grid.shape[1],\n                        scale * display_grid.shape[0]))\n    plt.title(layer_name)\n    plt.grid(False)\n    plt.imshow(display_grid, aspect='auto', cmap='viridis')\n    \nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-10T17:27:10.006776Z","iopub.execute_input":"2022-07-10T17:27:10.007136Z","iopub.status.idle":"2022-07-10T17:27:12.220729Z","shell.execute_reply.started":"2022-07-10T17:27:10.0071Z","shell.execute_reply":"2022-07-10T17:27:12.219757Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### Augmentation Using Input Processing Function Of ResNet ","metadata":{}},{"cell_type":"code","source":"train_datagen_2 = ImageDataGenerator(\n    preprocessing_function=preprocess_input,\n    shear_range=0.1,\n    horizontal_flip=True,\n    width_shift_range=0.1,\n    height_shift_range=0.1,\n    rotation_range=20,\n    fill_mode='nearest'\n)\n\ntest_datagen_2 = ImageDataGenerator(preprocessing_function=preprocess_input)\n","metadata":{"execution":{"iopub.status.busy":"2022-07-10T17:27:12.222434Z","iopub.execute_input":"2022-07-10T17:27:12.223118Z","iopub.status.idle":"2022-07-10T17:27:12.229449Z","shell.execute_reply.started":"2022-07-10T17:27:12.22307Z","shell.execute_reply":"2022-07-10T17:27:12.228285Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_one_image_generator= train_datagen_2.flow(\n    img,\n    y_1,\n    batch_size=32\n)\n\nplt.figure(figsize=(12, 12))\nfor i in range(0, 9):\n    plt.subplot(3, 3, i+1)\n    for X_batch, Y_batch in train_one_image_generator:\n        image = X_batch[0]\n        plt.imshow(image)\n        plt.yticks([])\n        plt.xticks([])\n        plt.title(classes_name.get(\"c\"+str(int(np.argmax(Y_batch[0])))))\n        break\nplt.tight_layout()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-10T17:27:12.230778Z","iopub.execute_input":"2022-07-10T17:27:12.231657Z","iopub.status.idle":"2022-07-10T17:27:12.998673Z","shell.execute_reply.started":"2022-07-10T17:27:12.231616Z","shell.execute_reply":"2022-07-10T17:27:12.997776Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_generator=train_datagen_2.flow_from_directory(\n    directory=train_path,\n    target_size=(224, 224),\n    batch_size=batchs,\n    class_mode=\"categorical\",\n    shuffle=True\n)\n\nvalid_generator=test_datagen_2.flow_from_directory(\n    directory=valid_path,\n    target_size=(224, 224),\n    batch_size=batchs,\n    class_mode=\"categorical\",\n    shuffle=True\n)\n\n\ntest_generator=test_datagen_2.flow_from_directory(\n    directory=test_path,\n    target_size=(224, 224),\n    batch_size=1,\n    class_mode=\"categorical\",\n    shuffle=True\n)","metadata":{"execution":{"iopub.status.busy":"2022-07-10T17:27:13.000156Z","iopub.execute_input":"2022-07-10T17:27:13.000808Z","iopub.status.idle":"2022-07-10T17:27:13.659737Z","shell.execute_reply.started":"2022-07-10T17:27:13.000769Z","shell.execute_reply":"2022-07-10T17:27:13.658761Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"###### 3. Using Transfer Learning Without Fine_Tuning","metadata":{}},{"cell_type":"code","source":"conv_base_2 = ResNet50(weights='imagenet', include_top=False, input_shape=(224, 224, 3))","metadata":{"execution":{"iopub.status.busy":"2022-07-10T17:27:13.661336Z","iopub.execute_input":"2022-07-10T17:27:13.661679Z","iopub.status.idle":"2022-07-10T17:27:15.544441Z","shell.execute_reply.started":"2022-07-10T17:27:13.661643Z","shell.execute_reply":"2022-07-10T17:27:15.543428Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"conv_base_2.trainable=False","metadata":{"execution":{"iopub.status.busy":"2022-07-10T17:27:15.546115Z","iopub.execute_input":"2022-07-10T17:27:15.546512Z","iopub.status.idle":"2022-07-10T17:27:15.557417Z","shell.execute_reply.started":"2022-07-10T17:27:15.546473Z","shell.execute_reply":"2022-07-10T17:27:15.556155Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"res_model_3 = models.Sequential()\nres_model_3.add(conv_base_2)\nres_model_3.add(layers.Flatten())\nres_model_3.add(layers.Dense(128, activation='relu'))\nres_model_3.add(layers.Dense(64, activation='relu'))\nres_model_3.add(layers.Dense(10, activation='softmax'))","metadata":{"execution":{"iopub.status.busy":"2022-07-10T17:27:15.55891Z","iopub.execute_input":"2022-07-10T17:27:15.559321Z","iopub.status.idle":"2022-07-10T17:27:15.94847Z","shell.execute_reply.started":"2022-07-10T17:27:15.559285Z","shell.execute_reply":"2022-07-10T17:27:15.947585Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"res_model_3.summary()","metadata":{"execution":{"iopub.status.busy":"2022-07-10T17:27:15.949913Z","iopub.execute_input":"2022-07-10T17:27:15.950231Z","iopub.status.idle":"2022-07-10T17:27:15.97023Z","shell.execute_reply.started":"2022-07-10T17:27:15.950196Z","shell.execute_reply":"2022-07-10T17:27:15.969221Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_model(res_model_3)","metadata":{"execution":{"iopub.status.busy":"2022-07-10T17:27:15.971721Z","iopub.execute_input":"2022-07-10T17:27:15.972161Z","iopub.status.idle":"2022-07-10T17:27:16.135916Z","shell.execute_reply.started":"2022-07-10T17:27:15.972124Z","shell.execute_reply":"2022-07-10T17:27:16.134725Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"opt = keras.optimizers.Adam()\nres_model_3.compile(optimizer=opt,\n                loss='categorical_crossentropy',\n                metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2022-07-10T17:27:16.138209Z","iopub.execute_input":"2022-07-10T17:27:16.138918Z","iopub.status.idle":"2022-07-10T17:27:16.154066Z","shell.execute_reply.started":"2022-07-10T17:27:16.138878Z","shell.execute_reply":"2022-07-10T17:27:16.152958Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history_7=res_model_3.fit(train_generator,\n                          validation_data=valid_generator,\n                          epochs=10, \n                          verbose=1,\n                          callbacks=callbacks_)","metadata":{"execution":{"iopub.status.busy":"2022-07-10T17:27:16.155655Z","iopub.execute_input":"2022-07-10T17:27:16.156447Z","iopub.status.idle":"2022-07-10T17:57:52.369151Z","shell.execute_reply.started":"2022-07-10T17:27:16.156402Z","shell.execute_reply":"2022-07-10T17:57:52.368247Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_loss, test_acc = res_model_3.evaluate(test_generator)","metadata":{"execution":{"iopub.status.busy":"2022-07-10T17:57:52.370927Z","iopub.execute_input":"2022-07-10T17:57:52.371276Z","iopub.status.idle":"2022-07-10T17:59:14.339697Z","shell.execute_reply.started":"2022-07-10T17:57:52.371242Z","shell.execute_reply":"2022-07-10T17:59:14.338726Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Loss curve\nplt.figure(figsize=[6,4])\nplt.plot(history_7.history['loss'], 'black', linewidth=2.0)\nplt.plot(history_7.history['val_loss'], 'green', linewidth=2.0)\nplt.legend(['Training Loss', 'Validation Loss'], fontsize=14)\nplt.xlabel('Epochs', fontsize=14)\nplt.ylabel('Loss', fontsize=14)\nplt.xticks(range(0,10))\nplt.title('Loss Curves', fontsize=15)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-10T17:59:14.341637Z","iopub.execute_input":"2022-07-10T17:59:14.342011Z","iopub.status.idle":"2022-07-10T17:59:14.53593Z","shell.execute_reply.started":"2022-07-10T17:59:14.341974Z","shell.execute_reply":"2022-07-10T17:59:14.534867Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Accuracy curve\nplt.figure(figsize=[6,4])\nplt.plot(history_7.history['accuracy'], 'black', linewidth=2.0)\nplt.plot(history_7.history['val_accuracy'], 'blue', linewidth=2.0)\nplt.legend(['Training Accuracy', 'Validation Accuracy'], fontsize=14)\nplt.xlabel('Epochs', fontsize=14)\nplt.ylabel('Accuracy', fontsize=14)\nplt.xticks(range(0,10))\nplt.title('Accuracy Curves', fontsize=15)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-10T17:59:14.537334Z","iopub.execute_input":"2022-07-10T17:59:14.538413Z","iopub.status.idle":"2022-07-10T17:59:14.742594Z","shell.execute_reply.started":"2022-07-10T17:59:14.538374Z","shell.execute_reply":"2022-07-10T17:59:14.741679Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(20, 20))\nfor i in range(0, 25):\n    plt.subplot(5, 5, i+1)\n    for X_batch, Y_batch in test_generator:\n        print(Y_batch)\n        image = X_batch[0]\n        plt.imshow(image)\n        x=np.array(image).reshape(1,224,224,3)\n        pred=res_model_3.predict(x)\n        true_label=classes_name.get(\"c\"+str(int(np.argmax(Y_batch[0]))))\n        pred_label=classes_name.get(\"c\"+str(int(np.argmax(pred))))\n        plt.yticks([])\n        plt.xticks([])\n        plt.title(f\"True: {true_label}\\n pred: {pred_label}\")\n        break\nplt.tight_layout()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-10T17:59:14.744201Z","iopub.execute_input":"2022-07-10T17:59:14.744873Z","iopub.status.idle":"2022-07-10T17:59:18.497045Z","shell.execute_reply.started":"2022-07-10T17:59:14.744816Z","shell.execute_reply":"2022-07-10T17:59:18.495977Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### 4. Using Transfer Learning With Fine_Tuning","metadata":{}},{"cell_type":"code","source":"conv_base_2.trainable = True","metadata":{"execution":{"iopub.status.busy":"2022-07-10T17:59:18.498665Z","iopub.execute_input":"2022-07-10T17:59:18.499261Z","iopub.status.idle":"2022-07-10T17:59:18.512219Z","shell.execute_reply.started":"2022-07-10T17:59:18.499224Z","shell.execute_reply":"2022-07-10T17:59:18.511279Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"set_trainable = False\nfor layer in conv_base_2.layers:\n    if (layer.name).startswith('conv5_block3_3_bn'):\n      #print(layer.name)\n      set_trainable = True\n    if set_trainable:\n        print(True)\n        layer.trainable = True\n    else:\n        layer.trainable = False","metadata":{"execution":{"iopub.status.busy":"2022-07-10T17:59:18.513407Z","iopub.execute_input":"2022-07-10T17:59:18.514339Z","iopub.status.idle":"2022-07-10T17:59:18.530754Z","shell.execute_reply.started":"2022-07-10T17:59:18.514295Z","shell.execute_reply":"2022-07-10T17:59:18.529612Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"res_model_4 = models.Sequential()\nres_model_4.add(conv_base_2)\nres_model_4.add(layers.Flatten())\nres_model_4.add(layers.Dropout(0.6))\nres_model_4.add(layers.Dense(128, activation='relu'))\nres_model_4.add(layers.Dense(64, activation='relu'))\nres_model_4.add(layers.Dense(10, activation='softmax'))\n","metadata":{"execution":{"iopub.status.busy":"2022-07-10T17:59:18.533228Z","iopub.execute_input":"2022-07-10T17:59:18.534004Z","iopub.status.idle":"2022-07-10T17:59:18.917125Z","shell.execute_reply.started":"2022-07-10T17:59:18.533969Z","shell.execute_reply":"2022-07-10T17:59:18.9162Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"res_model_4.summary()","metadata":{"execution":{"iopub.status.busy":"2022-07-10T17:59:18.918553Z","iopub.execute_input":"2022-07-10T17:59:18.919646Z","iopub.status.idle":"2022-07-10T17:59:18.936788Z","shell.execute_reply.started":"2022-07-10T17:59:18.919608Z","shell.execute_reply":"2022-07-10T17:59:18.93588Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_model(res_model_4)","metadata":{"execution":{"iopub.status.busy":"2022-07-10T17:59:18.938364Z","iopub.execute_input":"2022-07-10T17:59:18.938757Z","iopub.status.idle":"2022-07-10T17:59:19.105316Z","shell.execute_reply.started":"2022-07-10T17:59:18.938722Z","shell.execute_reply":"2022-07-10T17:59:19.104229Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"opt = keras.optimizers.Adam()\nres_model_4.compile(optimizer=opt,\n                loss='categorical_crossentropy',\n                metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2022-07-10T17:59:19.108583Z","iopub.execute_input":"2022-07-10T17:59:19.108897Z","iopub.status.idle":"2022-07-10T17:59:19.125636Z","shell.execute_reply.started":"2022-07-10T17:59:19.108867Z","shell.execute_reply":"2022-07-10T17:59:19.124709Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history_8=res_model_4.fit(train_generator,\n                          validation_data=valid_generator,\n                          epochs=10, \n                          verbose=1,\n                          callbacks=callbacks_)","metadata":{"execution":{"iopub.status.busy":"2022-07-10T17:59:19.127168Z","iopub.execute_input":"2022-07-10T17:59:19.127572Z","iopub.status.idle":"2022-07-10T18:43:02.263179Z","shell.execute_reply.started":"2022-07-10T17:59:19.127534Z","shell.execute_reply":"2022-07-10T18:43:02.262236Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_loss, test_acc = res_model_4.evaluate(test_generator)","metadata":{"execution":{"iopub.status.busy":"2022-07-10T18:43:02.266378Z","iopub.execute_input":"2022-07-10T18:43:02.26665Z","iopub.status.idle":"2022-07-10T18:44:24.229993Z","shell.execute_reply.started":"2022-07-10T18:43:02.266624Z","shell.execute_reply":"2022-07-10T18:44:24.22902Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Loss curve\nplt.figure(figsize=[6,4])\nplt.plot(history_8.history['loss'], 'black', linewidth=2.0)\nplt.plot(history_8.history['val_loss'], 'green', linewidth=2.0)\nplt.legend(['Training Loss', 'Validation Loss'], fontsize=14)\nplt.xlabel('Epochs', fontsize=14)\nplt.ylabel('Loss', fontsize=14)\nplt.xticks(range(0,10))\nplt.title('Loss Curves', fontsize=15)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-10T18:44:24.231489Z","iopub.execute_input":"2022-07-10T18:44:24.231943Z","iopub.status.idle":"2022-07-10T18:44:24.973173Z","shell.execute_reply.started":"2022-07-10T18:44:24.231904Z","shell.execute_reply":"2022-07-10T18:44:24.972218Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Accuracy curve\nplt.figure(figsize=[6,4])\nplt.plot(history_8.history['accuracy'], 'black', linewidth=2.0)\nplt.plot(history_8.history['val_accuracy'], 'blue', linewidth=2.0)\nplt.legend(['Training Accuracy', 'Validation Accuracy'], fontsize=14)\nplt.xlabel('Epochs', fontsize=14)\nplt.ylabel('Accuracy', fontsize=14)\nplt.xticks(range(0,10))\nplt.title('Accuracy Curves', fontsize=15)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-10T18:44:24.974447Z","iopub.execute_input":"2022-07-10T18:44:24.97521Z","iopub.status.idle":"2022-07-10T18:44:25.188378Z","shell.execute_reply.started":"2022-07-10T18:44:24.975172Z","shell.execute_reply":"2022-07-10T18:44:25.187452Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(20, 20))\nfor i in range(0, 25):\n    plt.subplot(5, 5, i+1)\n    for X_batch, Y_batch in test_generator:\n        print(Y_batch)\n        image = X_batch[0]\n        plt.imshow(image)\n        x=np.array(image).reshape(1,224,224,3)\n        pred=res_model_4.predict(x)\n        true_label=classes_name.get(\"c\"+str(int(np.argmax(Y_batch[0]))))\n        pred_label=classes_name.get(\"c\"+str(int(np.argmax(pred))))\n        plt.yticks([])\n        plt.xticks([])\n        plt.title(f\"True: {true_label}\\n pred: {pred_label}\")\n        break\nplt.tight_layout()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-10T18:44:25.189643Z","iopub.execute_input":"2022-07-10T18:44:25.190678Z","iopub.status.idle":"2022-07-10T18:44:29.35515Z","shell.execute_reply.started":"2022-07-10T18:44:25.190637Z","shell.execute_reply":"2022-07-10T18:44:29.354149Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}