{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"Dataset splitted as follow:\n70% Train \n20% Test\n10% valid","metadata":{"id":"h1g-nO-X5vHf"}},{"cell_type":"code","source":"!pip install -U tensorflow-addons","metadata":{"id":"FeG2SvDxdnSl","outputId":"a823242a-88e2-4e5b-bf30-a2af5e3d647a","execution":{"iopub.status.busy":"2023-08-04T22:01:24.945226Z","iopub.execute_input":"2023-08-04T22:01:24.945896Z","iopub.status.idle":"2023-08-04T22:01:48.111486Z","shell.execute_reply.started":"2023-08-04T22:01:24.945859Z","shell.execute_reply":"2023-08-04T22:01:48.110279Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# !pip uninstall  tensorflow-addons -y","metadata":{"execution":{"iopub.status.busy":"2023-02-08T11:57:23.619912Z","iopub.execute_input":"2023-02-08T11:57:23.620341Z","iopub.status.idle":"2023-02-08T11:57:23.626396Z","shell.execute_reply.started":"2023-02-08T11:57:23.620288Z","shell.execute_reply":"2023-02-08T11:57:23.624982Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#  !pip install tensorflow-addons","metadata":{"execution":{"iopub.status.busy":"2023-02-08T11:57:23.628166Z","iopub.execute_input":"2023-02-08T11:57:23.628959Z","iopub.status.idle":"2023-02-08T11:57:23.636794Z","shell.execute_reply.started":"2023-02-08T11:57:23.628871Z","shell.execute_reply":"2023-02-08T11:57:23.635864Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# !pip install tensorflow-estimator==2.1","metadata":{"execution":{"iopub.status.busy":"2023-02-08T11:57:23.640433Z","iopub.execute_input":"2023-02-08T11:57:23.641158Z","iopub.status.idle":"2023-02-08T11:57:23.648172Z","shell.execute_reply.started":"2023-02-08T11:57:23.641122Z","shell.execute_reply":"2023-02-08T11:57:23.647178Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# #Clear notebook output\n# import shutil\n# shutil.rmtree(\"/kaggle/working/\")","metadata":{"id":"WPnAeLS2dYRw","execution":{"iopub.status.busy":"2023-02-08T11:57:23.651278Z","iopub.execute_input":"2023-02-08T11:57:23.651879Z","iopub.status.idle":"2023-02-08T11:57:23.659872Z","shell.execute_reply.started":"2023-02-08T11:57:23.651843Z","shell.execute_reply":"2023-02-08T11:57:23.658900Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf\ntry:\n    tpu = tf.distribute.cluster_resolver.TPUClusterResolver()\n    print('Device:', tpu.master())\n    tf.config.experimental_connect_to_cluster(tpu)\n    tf.tpu.experimental.initialize_tpu_system(tpu)\n    strategy = tf.distribute.experimental.TPUStrategy(tpu)\nexcept:\n    strategy = tf.distribute.get_strategy()\nprint('Number of replicas:', strategy.num_replicas_in_sync)","metadata":{"execution":{"iopub.status.busy":"2023-08-04T22:01:48.115276Z","iopub.execute_input":"2023-08-04T22:01:48.115626Z","iopub.status.idle":"2023-08-04T22:01:52.290322Z","shell.execute_reply.started":"2023-08-04T22:01:48.115594Z","shell.execute_reply":"2023-08-04T22:01:52.289233Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install scikit-learn","metadata":{"execution":{"iopub.status.busy":"2023-08-04T22:01:52.291742Z","iopub.execute_input":"2023-08-04T22:01:52.292885Z","iopub.status.idle":"2023-08-04T22:02:03.551270Z","shell.execute_reply.started":"2023-08-04T22:01:52.292845Z","shell.execute_reply":"2023-08-04T22:02:03.550100Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install seaborn","metadata":{"execution":{"iopub.status.busy":"2023-08-04T22:02:03.555056Z","iopub.execute_input":"2023-08-04T22:02:03.555738Z","iopub.status.idle":"2023-08-04T22:02:15.188416Z","shell.execute_reply.started":"2023-08-04T22:02:03.555696Z","shell.execute_reply":"2023-08-04T22:02:15.187163Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nimport tensorflow as tf\nimport tensorflow_addons as tfa\nimport glob, warnings\nimport matplotlib.pyplot as plt\nfrom sklearn.metrics import confusion_matrix, classification_report\nimport seaborn as sns\nfrom tensorflow.keras import Input\n\nwarnings.filterwarnings('ignore')\nprint('TensorFlow Version ' + tf.__version__)","metadata":{"id":"jnk2074n4_GO","outputId":"e3f5b795-6644-4dc6-f49f-e02233d78873","execution":{"iopub.status.busy":"2023-08-04T22:02:15.190409Z","iopub.execute_input":"2023-08-04T22:02:15.190787Z","iopub.status.idle":"2023-08-04T22:02:16.683357Z","shell.execute_reply.started":"2023-08-04T22:02:15.190754Z","shell.execute_reply":"2023-08-04T22:02:16.682296Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install opencv-python-headless","metadata":{"execution":{"iopub.status.busy":"2023-08-04T22:02:16.684947Z","iopub.execute_input":"2023-08-04T22:02:16.685588Z","iopub.status.idle":"2023-08-04T22:02:27.997479Z","shell.execute_reply.started":"2023-08-04T22:02:16.685548Z","shell.execute_reply":"2023-08-04T22:02:27.996215Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install --quiet vit-keras\n\nfrom vit_keras import vit","metadata":{"id":"4_1G43o64_N4","execution":{"iopub.status.busy":"2023-08-04T22:02:27.999759Z","iopub.execute_input":"2023-08-04T22:02:28.000171Z","iopub.status.idle":"2023-08-04T22:02:41.709712Z","shell.execute_reply.started":"2023-08-04T22:02:28.000125Z","shell.execute_reply":"2023-08-04T22:02:41.708567Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\nimport matplotlib.pyplot as plt\nimport matplotlib.image as mpimg\nimport os\nimport random\nimport gc\nimport cv2\nfrom tqdm import tqdm\nimport pandas as pd\nimport seaborn as sns\nimport tensorflow as tf\nfrom tensorflow import keras\nfrom tensorflow.keras import layers\nimport tensorflow_addons as tfa\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\n\n%matplotlib inline\n# from google.colab import drive\n# drive.mount('/content/drive')","metadata":{"id":"ifnaufMO4_KD","outputId":"aa6b9ed4-6376-4081-f1c4-c3017a18ebea","execution":{"iopub.status.busy":"2023-08-04T22:02:41.712622Z","iopub.execute_input":"2023-08-04T22:02:41.713449Z","iopub.status.idle":"2023-08-04T22:02:41.725674Z","shell.execute_reply.started":"2023-08-04T22:02:41.713399Z","shell.execute_reply":"2023-08-04T22:02:41.724637Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dir=r\"/kaggle/input/cervix-dataset-all/Cervix Dataset_All\"\ntrain_dir=os.path.join(dir,'train')\ntest_dir=os.path.join(dir,'test')\nval_dir=os.path.join(dir,'valid')\n\n\n \n# train_dir=os.path.join('/kaggle/input/train-111-cropped/Train_111_Cropped')\n# test_dir=os.path.join('/kaggle/input/test-111-cropped/Test_111_Cropped')\n# val_dir=os.path.join('/kaggle/input/valid-111-cropped/Valid_111_Cropped')\n\n","metadata":{"id":"A9b6foWP4_Qr","execution":{"iopub.status.busy":"2023-08-04T22:02:41.727312Z","iopub.execute_input":"2023-08-04T22:02:41.727690Z","iopub.status.idle":"2023-08-04T22:02:41.736556Z","shell.execute_reply.started":"2023-08-04T22:02:41.727653Z","shell.execute_reply":"2023-08-04T22:02:41.735485Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# import os\n\n# train_dir_T1='/kaggle/input/train111/Type_1/'\n# train_dir_T2='/kaggle/input/train111/Type_2/'\n# train_dir_T3='/kaggle/input/train111/Type_3/'\n\n# lst1 = os.listdir(train_dir_T1)  \n# lst2 = os.listdir(train_dir_T2) \n# lst3 = os.listdir(train_dir_T3) \n\n\n\n# print(len(lst1),len(lst2),len(lst3))\n\n","metadata":{"execution":{"iopub.status.busy":"2023-05-08T10:37:30.507535Z","iopub.status.idle":"2023-05-08T10:37:30.508283Z","shell.execute_reply.started":"2023-05-08T10:37:30.508030Z","shell.execute_reply":"2023-05-08T10:37:30.508053Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n# for i in lst2 :\n#     print(i)\n#     if i == '2537.jpg' or i=='5579.jpg' or i=='5350.jpg':\n#         x=i\n# print(x,'congratulationsssssssssssssssssssssssssssssssssssss, Damaged file is : ', x)\n\n\n\n         ","metadata":{"execution":{"iopub.status.busy":"2023-02-08T11:58:14.043922Z","iopub.execute_input":"2023-02-08T11:58:14.044767Z","iopub.status.idle":"2023-02-08T11:58:14.050264Z","shell.execute_reply.started":"2023-02-08T11:58:14.044720Z","shell.execute_reply":"2023-02-08T11:58:14.049119Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# #Check and show corrupted images\n# img_plst=[]\n# import PIL\n# from pathlib import Path\n# from PIL import UnidentifiedImageError\n\n# path = Path(\"/kaggle/input/test111/test/Type_1\").rglob(\"*.jpg\")\n# for img_p in path:\n#     try:\n#         img = PIL.Image.open(img_p)\n#     except PIL.UnidentifiedImageError:\n#             img_plst.append(img_p)\n#             print(img_p)\n     \n# print(img_plst) \n# print(len(img_plst))","metadata":{"execution":{"iopub.status.busy":"2023-02-08T11:58:14.051915Z","iopub.execute_input":"2023-02-08T11:58:14.052871Z","iopub.status.idle":"2023-02-08T11:58:14.061805Z","shell.execute_reply.started":"2023-02-08T11:58:14.052763Z","shell.execute_reply":"2023-02-08T11:58:14.060820Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Initial Parameters**","metadata":{"id":"m3P5CZ7Eb67W"}},{"cell_type":"code","source":"classes=os.listdir(train_dir) # class names are the names of the sub directories\nclass_count=len(classes) # determine number of classes\nbatch_size=56 # set training batch size\nrand_seed=123\nstart_epoch=0 # specify starting epoch\nepochs=9 # specify the number of epochs to run\nimg_size=224\nlr=.001 # specify initial learning rate","metadata":{"id":"JnJl1nTb4_Un","execution":{"iopub.status.busy":"2023-08-04T21:47:52.859896Z","iopub.execute_input":"2023-08-04T21:47:52.860815Z","iopub.status.idle":"2023-08-04T21:47:52.872520Z","shell.execute_reply.started":"2023-08-04T21:47:52.860775Z","shell.execute_reply":"2023-08-04T21:47:52.871408Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_bs(dir,b_max):\n    # dir is the directory containing the samples, b_max is maximum batch size to allow based on your memory capacity\n    # you only want to go through test and validation set once per epoch this function determines needed batch size and steps per epoch\n    length=0\n    dir_list=os.listdir(dir)\n    for d in dir_list:\n        d_path=os.path.join (dir,d)\n        length=length + len(os.listdir(d_path))\n    batch_size=sorted([int(length/n) for n in range(1,length+1) if length % n ==0 and length/n<=b_max],reverse=True)[0]  \n    return batch_size,int(length/batch_size)","metadata":{"id":"qz6X-LoQ4_Xy","execution":{"iopub.status.busy":"2023-08-04T22:02:41.743075Z","iopub.execute_input":"2023-08-04T22:02:41.743415Z","iopub.status.idle":"2023-08-04T22:02:41.753433Z","shell.execute_reply.started":"2023-08-04T22:02:41.743386Z","shell.execute_reply":"2023-08-04T22:02:41.752325Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Determine test and validation batch size and steps to go through the samples only one per epoch**\nyou only want to go through test and validation set once per epoch this function determines needed batch size and steps per epoch","metadata":{"id":"ElgCwgCRKG0M"}},{"cell_type":"code","source":"valid_batch_size, valid_steps=get_bs(val_dir, 300)\ntest_batch_size, test_steps=get_bs(test_dir,100)","metadata":{"id":"UoIS0pr84_bP","execution":{"iopub.status.busy":"2023-08-04T22:02:41.755257Z","iopub.execute_input":"2023-08-04T22:02:41.755625Z","iopub.status.idle":"2023-08-04T22:02:41.916773Z","shell.execute_reply.started":"2023-08-04T22:02:41.755589Z","shell.execute_reply":"2023-08-04T22:02:41.915822Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Load Data\nPreparing the Dataset\nWe will first prepare the dataset and separate out the images:\n\nWe first divide the folder contents into the train and validation and test directories.\n\nThen, in each of the directories, I created a three separated directories  contains the tree cervix types of  images (Type 1, Type 2 ,Type 3)","metadata":{"id":"uAxExDYSEzes"}},{"cell_type":"code","source":"train_gen=ImageDataGenerator(preprocessing_function=keras.applications.mobilenet.preprocess_input, horizontal_flip=True).flow_from_directory(\n        train_dir,  target_size=(img_size, img_size), batch_size=batch_size, seed=rand_seed, class_mode='categorical', color_mode='rgb')\n\nvalid_gen=ImageDataGenerator(preprocessing_function=keras.applications.mobilenet.preprocess_input) .flow_from_directory(\n    val_dir,  target_size=(img_size, img_size), batch_size=valid_batch_size, class_mode='categorical',color_mode='rgb', shuffle=False)\ntest_gen=ImageDataGenerator(preprocessing_function=keras.applications.mobilenet.preprocess_input).flow_from_directory(\n    test_dir,target_size=(img_size, img_size), batch_size=test_batch_size, class_mode='categorical',color_mode='rgb', shuffle=False )\n\ntest_file_names=test_gen.filenames  # save list of test files names to be used later\ntest_labels=test_gen.labels # save test labels to be used later\n\nval_file_names=valid_gen.filenames  # save list of test files names to be used later\nval_labels=valid_gen.labels # save test labels to be used later","metadata":{"id":"sp3bqX3Z4_eq","outputId":"8fa26481-44d7-458d-b2bb-ba5671f1f629","execution":{"iopub.status.busy":"2023-08-04T21:47:53.080860Z","iopub.execute_input":"2023-08-04T21:47:53.081557Z","iopub.status.idle":"2023-08-04T21:47:53.602074Z","shell.execute_reply.started":"2023-08-04T21:47:53.081517Z","shell.execute_reply":"2023-08-04T21:47:53.601065Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# images = [train_gen[0][0][i] for i in range(16)]\n# fig, axes = plt.subplots(3, 5, figsize = (10, 10))\n\n# axes = axes.flatten()\n\n# for img, ax in zip(images, axes):\n#     ax.imshow(img.reshape(img_size, img_size, 3))\n#     ax.axis('off')\n\n# plt.tight_layout()\n# plt.show()","metadata":{"id":"i9Oe9tMAKfjx","execution":{"iopub.status.busy":"2023-02-08T11:58:14.383178Z","iopub.execute_input":"2023-02-08T11:58:14.383719Z","iopub.status.idle":"2023-02-08T11:58:14.393971Z","shell.execute_reply.started":"2023-02-08T11:58:14.383665Z","shell.execute_reply":"2023-02-08T11:58:14.392861Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**1. First Model : vit_b32**","metadata":{"id":"CbH1HzlcfzCG"}},{"cell_type":"code","source":"# vit_model = vit.vit_b32(\n#         image_size = img_size,\n#         activation = 'softmax',\n#         pretrained = True,\n#         include_top = False,\n#         pretrained_top = False,\n#         classes = 3)","metadata":{"id":"j9ECk1ZbKfmk","execution":{"iopub.status.busy":"2023-05-21T08:32:28.423315Z","iopub.execute_input":"2023-05-21T08:32:28.423825Z","iopub.status.idle":"2023-05-21T08:32:38.274357Z","shell.execute_reply.started":"2023-05-21T08:32:28.423782Z","shell.execute_reply":"2023-05-21T08:32:38.273278Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# #prepare model before fit to training\n\n# model = tf.keras.Sequential([\n#         vit_model,\n#         tf.keras.layers.Flatten(),\n#         tf.keras.layers.BatchNormalization(),\n#         tf.keras.layers.Dense(11, activation = tfa.activations.gelu),\n#         tf.keras.layers.BatchNormalization(),\n#         tf.keras.layers.Dense(3, 'softmax')\n#     ],\n#     name = 'vision_transformer')\n\n# model.summary()","metadata":{"id":"o1l6EAgJcaa8","outputId":"4795672d-70c3-4793-f85e-8c1d6fa8eebd","execution":{"iopub.status.busy":"2023-05-21T08:32:48.741505Z","iopub.execute_input":"2023-05-21T08:32:48.741975Z","iopub.status.idle":"2023-05-21T08:32:50.200803Z","shell.execute_reply.started":"2023-05-21T08:32:48.741940Z","shell.execute_reply":"2023-05-21T08:32:50.199758Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**tf.keras.callbacks.ReduceLROnPlateau/**\nModels often benefit from reducing the learning rate by a factor of 2-10 once learning stagnates. This callback monitors a quantity and if no improvement is seen for a 'patience' number of epochs, the learning rate is reduced.\n\n\n**tf.keras.callbacks.EarlyStopping/** \nStop training when a monitored metric has stopped improving\n\n\nt**f.keras.callbacks.ModelCheckpoint/**\nCallback to save the Keras model or model weights at some frequency.","metadata":{"id":"IO-hsq5vezlf"}},{"cell_type":"code","source":"# optimizer = tfa.optimizers.RectifiedAdam(learning_rate = lr)\n\n# model.compile(optimizer = optimizer, \n#               loss = tf.keras.losses.CategoricalCrossentropy(label_smoothing = 0.2), \n#               metrics = ['accuracy'])\n\n# STEP_SIZE_TRAIN = train_gen.n // train_gen.batch_size\n# STEP_SIZE_VALID = valid_gen.n // valid_gen.batch_size\n\n# reduce_lr = tf.keras.callbacks.ReduceLROnPlateau(monitor = 'val_accuracy',\n#                                                  factor = 0.2,\n#                                                  patience = 2,\n#                                                  verbose = 1,\n#                                                  min_delta = 1e-4,\n#                                                  min_lr = 1e-6,\n#                                                  mode = 'max')\n\n# earlystopping = tf.keras.callbacks.EarlyStopping(monitor = 'val_accuracy',\n#                                                  min_delta = 1e-4,\n#                                                  patience = 5,\n#                                                  mode = 'max',\n#                                                  restore_best_weights = True,\n#                                                  verbose = 1)\n\n# checkpointer = tf.keras.callbacks.ModelCheckpoint(filepath = './model.hdf5',\n#                                                   monitor = 'val_accuracy', \n#                                                   verbose = 1, \n#                                                   save_best_only = True,\n#                                                   save_weights_only = True,\n#                                                   mode = 'max')\n\n# callbacks = [earlystopping, reduce_lr, checkpointer]\n","metadata":{"id":"nBz_d0K8KfsM","execution":{"iopub.status.busy":"2023-05-21T08:32:54.562053Z","iopub.execute_input":"2023-05-21T08:32:54.562463Z","iopub.status.idle":"2023-05-21T08:32:54.829056Z","shell.execute_reply.started":"2023-05-21T08:32:54.562428Z","shell.execute_reply":"2023-05-21T08:32:54.828002Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# from PIL import ImageFile\n# ImageFile.LOAD_TRUNCATED_IMAGES = True","metadata":{"id":"yNRfA3tWVnPf","execution":{"iopub.status.busy":"2023-05-21T08:32:58.201705Z","iopub.execute_input":"2023-05-21T08:32:58.202125Z","iopub.status.idle":"2023-05-21T08:32:58.207279Z","shell.execute_reply.started":"2023-05-21T08:32:58.202075Z","shell.execute_reply":"2023-05-21T08:32:58.205764Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# history = model.fit(x = train_gen,\n#           steps_per_epoch = STEP_SIZE_TRAIN,\n#           validation_data = valid_gen,\n#           validation_steps = STEP_SIZE_VALID,\n#           epochs = epochs,\n#           callbacks = callbacks)","metadata":{"id":"ScMgXrP5Kfu9","outputId":"9af5ba04-d575-4266-8529-1255337202a5","execution":{"iopub.status.busy":"2023-05-21T08:32:59.589121Z","iopub.execute_input":"2023-05-21T08:32:59.590137Z","iopub.status.idle":"2023-05-21T09:50:21.852085Z","shell.execute_reply.started":"2023-05-21T08:32:59.590073Z","shell.execute_reply":"2023-05-21T09:50:21.848038Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# # plt.plot(history.history[\"loss\"], label=\"train_loss\")\n# # plt.plot(history.history[\"val_loss\"], label=\"val_loss\")\n# # plt.xlabel(\"Epochs\")\n# # plt.ylabel(\"Loss\")\n# # plt.title(\"Train and Validation Losses Over Epochs\", fontsize=14)\n# # plt.legend()\n# # plt.grid()\n# plt.show()","metadata":{"id":"0jK8mmziKfzx","execution":{"iopub.status.busy":"2023-02-08T11:58:14.460083Z","iopub.execute_input":"2023-02-08T11:58:14.461068Z","iopub.status.idle":"2023-02-08T11:58:14.474185Z","shell.execute_reply.started":"2023-02-08T11:58:14.461031Z","shell.execute_reply":"2023-02-08T11:58:14.473062Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# predicted_classes = np.argmax(model.predict(test_gen, steps = test_gen.n // test_gen.batch_size + 1), axis = 1)\n# true_classes = test_gen.classes\n# class_labels = list(test_gen.class_indices.keys())  \n\n# confusionmatrix = confusion_matrix(true_classes, predicted_classes)\n# plt.figure(figsize = (7, 7))\n# sns.heatmap(confusionmatrix, cmap = 'Blues', annot = True, cbar = True)\n\n# print(classification_report(true_classes, predicted_classes))","metadata":{"id":"pU6AsRV0Kf5U","outputId":"c5e7ed4e-4196-42dd-dea4-a6596a09112e","execution":{"iopub.status.busy":"2023-02-08T11:58:14.481798Z","iopub.execute_input":"2023-02-08T11:58:14.482394Z","iopub.status.idle":"2023-02-08T11:58:14.488016Z","shell.execute_reply.started":"2023-02-08T11:58:14.482366Z","shell.execute_reply":"2023-02-08T11:58:14.486870Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**[2.Second Model](https://)** : vit_b16","metadata":{"id":"HBuSGY5DXJH5"}},{"cell_type":"code","source":"classes=os.listdir(train_dir) # class names are the names of the sub directories\nclass_count=len(classes) # determine number of classes\nbatch_size=32 # set training batch size\nrand_seed=123\nstart_epoch=0 # specify starting epoch\nepochs=8 # specify the number of epochs to run\nimg_size=224\nlr=.001 # specify initial learning rate","metadata":{"id":"og1mmD4ZKf-B","execution":{"iopub.status.busy":"2023-08-04T22:02:41.919137Z","iopub.execute_input":"2023-08-04T22:02:41.920123Z","iopub.status.idle":"2023-08-04T22:02:41.928406Z","shell.execute_reply.started":"2023-08-04T22:02:41.920083Z","shell.execute_reply":"2023-08-04T22:02:41.927390Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_gen=ImageDataGenerator(preprocessing_function=keras.applications.mobilenet.preprocess_input, horizontal_flip=True).flow_from_directory(\n        train_dir,  target_size=(img_size, img_size), batch_size=batch_size, seed=rand_seed, class_mode='categorical', color_mode='rgb')\n\nvalid_gen=ImageDataGenerator(preprocessing_function=keras.applications.mobilenet.preprocess_input) .flow_from_directory(\n    val_dir,  target_size=(img_size, img_size), batch_size=valid_batch_size, class_mode='categorical',color_mode='rgb', shuffle=False)\ntest_gen=ImageDataGenerator(preprocessing_function=keras.applications.mobilenet.preprocess_input).flow_from_directory(\n    test_dir,target_size=(img_size, img_size), batch_size=test_batch_size, class_mode='categorical',color_mode='rgb', shuffle=False )\n\ntest_file_names=test_gen.filenames  # save list of test files names to be used later\ntest_labels=test_gen.labels # save test labels to be used later\n\nval_file_names=valid_gen.filenames  # save list of test files names to be used later\nval_labels=valid_gen.labels # save test labels to be used later","metadata":{"id":"41CsasCefMt7","execution":{"iopub.status.busy":"2023-08-04T22:02:41.931237Z","iopub.execute_input":"2023-08-04T22:02:41.931594Z","iopub.status.idle":"2023-08-04T22:02:42.352977Z","shell.execute_reply.started":"2023-08-04T22:02:41.931558Z","shell.execute_reply":"2023-08-04T22:02:42.351974Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# images = [train_gen[0][0][i] for i in range(16)]\n# fig, axes = plt.subplots(3, 5, figsize = (10, 10))\n\n# axes = axes.flatten()\n\n# for img, ax in zip(images, axes):\n#     ax.imshow(img.reshape(img_size, img_size, 3))\n#     ax.axis('off')\n\n# plt.tight_layout()\n# plt.show()","metadata":{"id":"ptwA3WKZfMxL","execution":{"iopub.status.busy":"2023-02-08T11:58:15.063762Z","iopub.execute_input":"2023-02-08T11:58:15.064111Z","iopub.status.idle":"2023-02-08T11:58:15.077313Z","shell.execute_reply.started":"2023-02-08T11:58:15.064072Z","shell.execute_reply":"2023-02-08T11:58:15.074509Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"vit_model = vit.vit_b16(\n        image_size = img_size,\n        activation = 'softmax',\n        pretrained = True,\n        include_top = False,\n        pretrained_top = False,\n        classes = 3)","metadata":{"id":"Ta1z-VWvfM0A","execution":{"iopub.status.busy":"2023-08-04T22:02:42.354697Z","iopub.execute_input":"2023-08-04T22:02:42.355163Z","iopub.status.idle":"2023-08-04T22:02:53.593632Z","shell.execute_reply.started":"2023-08-04T22:02:42.355120Z","shell.execute_reply":"2023-08-04T22:02:53.592542Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = tf.keras.Sequential([\n        vit_model,\n        tf.keras.layers.Flatten(),\n        tf.keras.layers.BatchNormalization(),\n        tf.keras.layers.Dense(11, activation = tfa.activations.gelu),\n        tf.keras.layers.BatchNormalization(),\n        tf.keras.layers.Dense(3, 'softmax')\n    ],\n    name = 'vision_transformer')\n\nmodel.summary()","metadata":{"id":"kQ56T-o3fM3s","execution":{"iopub.status.busy":"2023-08-04T22:02:53.595046Z","iopub.execute_input":"2023-08-04T22:02:53.595431Z","iopub.status.idle":"2023-08-04T22:02:55.036317Z","shell.execute_reply.started":"2023-08-04T22:02:53.595393Z","shell.execute_reply":"2023-08-04T22:02:55.035275Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"optimizer = tfa.optimizers.RectifiedAdam(learning_rate = lr)\n\nmodel.compile(optimizer = optimizer, \n              loss = tf.keras.losses.CategoricalCrossentropy(label_smoothing = 0.2), \n              metrics = ['accuracy'])\n\nSTEP_SIZE_TRAIN = train_gen.n // train_gen.batch_size\nSTEP_SIZE_VALID = valid_gen.n // valid_gen.batch_size\n\nreduce_lr = tf.keras.callbacks.ReduceLROnPlateau(monitor = 'val_accuracy',\n                                                 factor = 0.2,\n                                                 patience = 2,\n                                                 verbose = 1,\n                                                 min_delta = 1e-4,\n                                                 min_lr = 1e-6,\n                                                 mode = 'max')\n\nearlystopping = tf.keras.callbacks.EarlyStopping(monitor = 'val_accuracy',\n                                                 min_delta = 1e-4,\n                                                 patience = 5,\n                                                 mode = 'max',\n                                                 restore_best_weights = True,\n                                                 verbose = 1)\n\ncheckpointer = tf.keras.callbacks.ModelCheckpoint(filepath = './model.hdf5',\n                                                  monitor = 'val_accuracy', \n                                                  verbose = 1, \n                                                  save_best_only = True,\n                                                  save_weights_only = True,\n                                                  mode = 'max')\n\ncallbacks = [earlystopping, reduce_lr, checkpointer]\n","metadata":{"id":"KtPZJclXfM6X","execution":{"iopub.status.busy":"2023-08-04T22:02:55.037853Z","iopub.execute_input":"2023-08-04T22:02:55.038326Z","iopub.status.idle":"2023-08-04T22:02:55.318325Z","shell.execute_reply.started":"2023-08-04T22:02:55.038220Z","shell.execute_reply":"2023-08-04T22:02:55.317252Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from PIL import ImageFile\nImageFile.LOAD_TRUNCATED_IMAGES = True","metadata":{"id":"wTMpD16bmUvh","execution":{"iopub.status.busy":"2023-08-04T22:02:55.319842Z","iopub.execute_input":"2023-08-04T22:02:55.320239Z","iopub.status.idle":"2023-08-04T22:02:55.327504Z","shell.execute_reply.started":"2023-08-04T22:02:55.320169Z","shell.execute_reply":"2023-08-04T22:02:55.326231Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model.fit(x = train_gen,\n          steps_per_epoch = STEP_SIZE_TRAIN,\n          validation_data = valid_gen,\n          validation_steps = STEP_SIZE_VALID,\n          epochs = epochs,\n          callbacks = callbacks)","metadata":{"id":"QjLtn0zufM83","execution":{"iopub.status.busy":"2023-08-04T22:02:55.329409Z","iopub.execute_input":"2023-08-04T22:02:55.329778Z","iopub.status.idle":"2023-08-04T22:04:05.481719Z","shell.execute_reply.started":"2023-08-04T22:02:55.329740Z","shell.execute_reply":"2023-08-04T22:04:05.480111Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# plt.plot(history.history[\"loss\"], label=\"train_loss\")\n# plt.plot(history.history[\"val_loss\"], label=\"val_loss\")\n# plt.xlabel(\"Epochs\")\n# plt.ylabel(\"Loss\")\n# plt.title(\"Train and Validation Losses Over Epochs\", fontsize=14)\n# plt.legend()\n# plt.grid()\n# plt.show()","metadata":{"id":"S85IVm6lfM_s","execution":{"iopub.status.busy":"2023-05-21T21:04:38.880118Z","iopub.execute_input":"2023-05-21T21:04:38.882255Z","iopub.status.idle":"2023-05-21T21:04:56.580508Z","shell.execute_reply.started":"2023-05-21T21:04:38.882204Z","shell.execute_reply":"2023-05-21T21:04:56.579405Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\npredicted_classes = np.argmax(model.predict(test_gen, steps = test_gen.n // test_gen.batch_size + 1), axis = 1)\ntrue_classes = test_gen.classes\nclass_labels = list(test_gen.class_indices.keys())  \n\nconfusionmatrix = confusion_matrix(true_classes, predicted_classes)\nplt.figure(figsize = (7, 7))\nsns.heatmap(confusionmatrix, cmap = 'Blues', annot = True, cbar = True)\n\nprint(classification_report(true_classes, predicted_classes))","metadata":{"id":"-hhM4VdUfNCE","execution":{"iopub.status.busy":"2023-08-04T21:57:30.819100Z","iopub.status.idle":"2023-08-04T21:57:30.821666Z","shell.execute_reply.started":"2023-08-04T21:57:30.821375Z","shell.execute_reply":"2023-08-04T21:57:30.821402Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**[3.Thirs Model](https://)** : vit_l32","metadata":{"id":"OSmW7LKF6L17"}},{"cell_type":"code","source":"# classes=os.listdir(train_dir) # class names are the names of the sub directories\n# class_count=len(classes) # determine number of classes\n# batch_size=56 # set training batch size\n# rand_seed=123\n# start_epoch=0 # specify starting epoch\n# epochs=9 # specify the number of epochs to run\n# img_size=224\n# lr=.001 # specify initial learning rate","metadata":{"id":"O9xDu9dLfNHl","execution":{"iopub.status.busy":"2023-08-04T22:05:13.374535Z","iopub.execute_input":"2023-08-04T22:05:13.374928Z","iopub.status.idle":"2023-08-04T22:05:13.387784Z","shell.execute_reply.started":"2023-08-04T22:05:13.374884Z","shell.execute_reply":"2023-08-04T22:05:13.386822Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# train_gen=ImageDataGenerator(preprocessing_function=keras.applications.mobilenet.preprocess_input, horizontal_flip=True).flow_from_directory(\n#         train_dir,  target_size=(img_size, img_size), batch_size=batch_size, seed=rand_seed, class_mode='categorical', color_mode='rgb')\n\n# valid_gen=ImageDataGenerator(preprocessing_function=keras.applications.mobilenet.preprocess_input) .flow_from_directory(\n#     val_dir,  target_size=(img_size, img_size), batch_size=valid_batch_size, class_mode='categorical',color_mode='rgb', shuffle=False)\n# test_gen=ImageDataGenerator(preprocessing_function=keras.applications.mobilenet.preprocess_input).flow_from_directory(\n#     test_dir,target_size=(img_size, img_size), batch_size=test_batch_size, class_mode='categorical',color_mode='rgb', shuffle=False )\n\n# test_file_names=test_gen.filenames  # save list of test files names to be used later\n# test_labels=test_gen.labels # save test labels to be used later\n\n# val_file_names=valid_gen.filenames  # save list of test files names to be used later\n# val_labels=valid_gen.labels # save test labels to be used later","metadata":{"id":"LFd3IDiofNKv","execution":{"iopub.status.busy":"2023-08-04T22:05:13.389376Z","iopub.execute_input":"2023-08-04T22:05:13.389869Z","iopub.status.idle":"2023-08-04T22:05:13.711095Z","shell.execute_reply.started":"2023-08-04T22:05:13.389785Z","shell.execute_reply":"2023-08-04T22:05:13.710068Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# images = [train_gen[0][0][i] for i in range(16)]\n# fig, axes = plt.subplots(3, 5, figsize = (10, 10))\n\n# axes = axes.flatten()\n\n# for img, ax in zip(images, axes):\n#     ax.imshow(img.reshape(img_size, img_size, 3))\n#     ax.axis('off')\n\n# plt.tight_layout()\n# plt.show()","metadata":{"id":"E7pov3_x6451","execution":{"iopub.status.busy":"2023-02-08T12:04:37.500549Z","iopub.execute_input":"2023-02-08T12:04:37.501627Z","iopub.status.idle":"2023-02-08T12:04:37.513357Z","shell.execute_reply.started":"2023-02-08T12:04:37.501589Z","shell.execute_reply":"2023-02-08T12:04:37.512442Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# vit_model = vit.vit_l32(\n#         image_size = img_size,\n#         activation = 'softmax',\n#         pretrained = True,\n#         include_top = False,\n#         pretrained_top = False,\n#         classes = 3)\n","metadata":{"id":"Xj1ChxH66502","execution":{"iopub.status.busy":"2023-08-04T22:05:13.713145Z","iopub.execute_input":"2023-08-04T22:05:13.713889Z","iopub.status.idle":"2023-08-04T22:05:21.041503Z","shell.execute_reply.started":"2023-08-04T22:05:13.713846Z","shell.execute_reply":"2023-08-04T22:05:21.039542Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# model = tf.keras.Sequential([\n#         vit_model,\n#         tf.keras.layers.Flatten(),\n#         tf.keras.layers.BatchNormalization(),\n#         tf.keras.layers.Dense(11, activation = tfa.activations.gelu),\n#         tf.keras.layers.BatchNormalization(),\n#         tf.keras.layers.Dense(3, 'softmax')\n#     ],\n#     name = 'vision_transformer')\n\n# model.summary()","metadata":{"id":"Iqtjn3PQ6545","execution":{"iopub.status.busy":"2023-08-04T22:05:22.384215Z","iopub.execute_input":"2023-08-04T22:05:22.385326Z","iopub.status.idle":"2023-08-04T22:05:25.232029Z","shell.execute_reply.started":"2023-08-04T22:05:22.385285Z","shell.execute_reply":"2023-08-04T22:05:25.230883Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# optimizer = tfa.optimizers.RectifiedAdam(learning_rate = lr)\n\n# model.compile(optimizer = optimizer, \n#               loss = tf.keras.losses.CategoricalCrossentropy(label_smoothing = 0.2), \n#               metrics = ['accuracy'])\n\n# STEP_SIZE_TRAIN = train_gen.n // train_gen.batch_size\n# STEP_SIZE_VALID = valid_gen.n // valid_gen.batch_size\n\n# reduce_lr = tf.keras.callbacks.ReduceLROnPlateau(monitor = 'val_accuracy',\n#                                                  factor = 0.2,\n#                                                  patience = 2,\n#                                                  verbose = 1,\n#                                                  min_delta = 1e-4,\n#                                                  min_lr = 1e-6,\n#                                                  mode = 'max')\n\n# earlystopping = tf.keras.callbacks.EarlyStopping(monitor = 'val_accuracy',\n#                                                  min_delta = 1e-4,\n#                                                  patience = 5,\n#                                                  mode = 'max',\n#                                                  restore_best_weights = True,\n#                                                  verbose = 1)\n\n# checkpointer = tf.keras.callbacks.ModelCheckpoint(filepath = './model.hdf5',\n#                                                   monitor = 'val_accuracy', \n#                                                   verbose = 1, \n#                                                   save_best_only = True,\n#                                                   save_weights_only = True,\n#                                                   mode = 'max')\n\n# callbacks = [earlystopping, reduce_lr, checkpointer]","metadata":{"id":"V07hQsAN649j","execution":{"iopub.status.busy":"2023-08-04T22:05:29.854620Z","iopub.execute_input":"2023-08-04T22:05:29.855343Z","iopub.status.idle":"2023-08-04T22:05:29.880186Z","shell.execute_reply.started":"2023-08-04T22:05:29.855304Z","shell.execute_reply":"2023-08-04T22:05:29.879260Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# from PIL import ImageFile\n# ImageFile.LOAD_TRUNCATED_IMAGES = True","metadata":{"id":"FF5mgyQS65Bk","execution":{"iopub.status.busy":"2023-08-04T22:05:39.235035Z","iopub.execute_input":"2023-08-04T22:05:39.235453Z","iopub.status.idle":"2023-08-04T22:05:39.240301Z","shell.execute_reply.started":"2023-08-04T22:05:39.235418Z","shell.execute_reply":"2023-08-04T22:05:39.239269Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# history = model.fit(x = train_gen,\n#           steps_per_epoch = STEP_SIZE_TRAIN,\n#           validation_data = valid_gen,\n#           validation_steps = STEP_SIZE_VALID,\n#           epochs = epochs,\n#           callbacks = callbacks)","metadata":{"id":"TK4zO6kd65D3","execution":{"iopub.status.busy":"2023-08-04T22:05:44.556346Z","iopub.execute_input":"2023-08-04T22:05:44.556730Z","iopub.status.idle":"2023-08-04T22:07:11.140233Z","shell.execute_reply.started":"2023-08-04T22:05:44.556697Z","shell.execute_reply":"2023-08-04T22:07:11.137111Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# plt.plot(history.history[\"loss\"], label=\"train_loss\")\n# plt.plot(history.history[\"val_loss\"], label=\"val_loss\")\n# plt.xlabel(\"Epochs\")\n# plt.ylabel(\"Loss\")\n# plt.title(\"Train and Validation Losses Over Epochs\", fontsize=14)\n# plt.legend()\n# plt.grid()\n# plt.show()","metadata":{"id":"ll9yvKCL7ixx","execution":{"iopub.status.busy":"2023-02-08T12:04:37.578297Z","iopub.execute_input":"2023-02-08T12:04:37.579111Z","iopub.status.idle":"2023-02-08T12:04:37.590342Z","shell.execute_reply.started":"2023-02-08T12:04:37.579075Z","shell.execute_reply":"2023-02-08T12:04:37.589394Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# predicted_classes = np.argmax(model.predict(test_gen, steps = test_gen.n // test_gen.batch_size + 1), axis = 1)\n# true_classes = test_gen.classes\n# class_labels = list(test_gen.class_indices.keys())  \n\n# confusionmatrix = confusion_matrix(true_classes, predicted_classes)\n# plt.figure(figsize = (7, 7))\n# sns.heatmap(confusionmatrix, cmap = 'Blues', annot = True, cbar = True)\n\n# print(classification_report(true_classes, predicted_classes))","metadata":{"id":"FfTLyPAv7i5j","execution":{"iopub.status.busy":"2023-08-04T22:07:11.143322Z","iopub.status.idle":"2023-08-04T22:07:11.145885Z","shell.execute_reply.started":"2023-08-04T22:07:11.145596Z","shell.execute_reply":"2023-08-04T22:07:11.145622Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"","metadata":{"id":"1zlH4UoPSfk6"}},{"cell_type":"markdown","source":"**[4.Fourth model](https://)** : vit_l16","metadata":{"id":"yJD4oYiSAvZZ"}},{"cell_type":"code","source":"# classes=os.listdir(train_dir) # class names are the names of the sub directories\n# class_count=len(classes) # determine number of classes\n# batch_size=56 # set training batch size\n# rand_seed=123\n# start_epoch=0 # specify starting epoch\n# epochs=9 # specify the number of epochs to run\n# img_size=224  \n# lr=.001 # specify initial learning rate","metadata":{"id":"wHLJarhk7i-Y","execution":{"iopub.status.busy":"2023-08-04T22:08:12.021344Z","iopub.execute_input":"2023-08-04T22:08:12.021731Z","iopub.status.idle":"2023-08-04T22:08:12.029725Z","shell.execute_reply.started":"2023-08-04T22:08:12.021698Z","shell.execute_reply":"2023-08-04T22:08:12.028475Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# train_gen=ImageDataGenerator(preprocessing_function=keras.applications.mobilenet.preprocess_input, horizontal_flip=True).flow_from_directory(\n#         train_dir,  target_size=(img_size, img_size), batch_size=batch_size, seed=rand_seed, class_mode='categorical', color_mode='rgb')\n\n# valid_gen=ImageDataGenerator(preprocessing_function=keras.applications.mobilenet.preprocess_input) .flow_from_directory(\n#     val_dir,  target_size=(img_size, img_size), batch_size=valid_batch_size, class_mode='categorical',color_mode='rgb', shuffle=False)\n# test_gen=ImageDataGenerator(preprocessing_function=keras.applications.mobilenet.preprocess_input).flow_from_directory(\n#     test_dir,target_size=(img_size, img_size), batch_size=test_batch_size, class_mode='categorical',color_mode='rgb', shuffle=False )\n","metadata":{"id":"8qByx8si7jDk","execution":{"iopub.status.busy":"2023-08-04T22:08:15.652412Z","iopub.execute_input":"2023-08-04T22:08:15.652806Z","iopub.status.idle":"2023-08-04T22:08:15.975407Z","shell.execute_reply.started":"2023-08-04T22:08:15.652773Z","shell.execute_reply":"2023-08-04T22:08:15.974392Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# images = [train_gen[0][0][i] for i in range(16)]\n# fig, axes = plt.subplots(3, 5, figsize = (10, 10))\n\n# axes = axes.flatten()\n\n# for img, ax in zip(images, axes):\n#     ax.imshow(img.reshape(img_size, img_size, 3))\n#     ax.axis('off')\n\n# plt.tight_layout()\n# plt.show()","metadata":{"id":"m0wOmT6P7jHo","execution":{"iopub.status.busy":"2023-02-08T12:04:37.622794Z","iopub.execute_input":"2023-02-08T12:04:37.623643Z","iopub.status.idle":"2023-02-08T12:04:37.636608Z","shell.execute_reply.started":"2023-02-08T12:04:37.623608Z","shell.execute_reply":"2023-02-08T12:04:37.635737Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# vit_model = vit.vit_l16(\n#         image_size = img_size,\n#         activation = 'softmax',\n#         pretrained = True,\n#         include_top = False,\n#         pretrained_top = False,\n#         classes = 3)","metadata":{"id":"iAodYtgrBC-U","execution":{"iopub.status.busy":"2023-08-04T22:08:20.530327Z","iopub.execute_input":"2023-08-04T22:08:20.530707Z","iopub.status.idle":"2023-08-04T22:08:43.602365Z","shell.execute_reply.started":"2023-08-04T22:08:20.530676Z","shell.execute_reply":"2023-08-04T22:08:43.601246Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# model = tf.keras.Sequential([\n#         vit_model,\n#         tf.keras.layers.Flatten(),\n#         tf.keras.layers.BatchNormalization(),\n#         tf.keras.layers.Dense(11, activation = tfa.activations.gelu),\n#         tf.keras.layers.BatchNormalization(),\n#         tf.keras.layers.Dense(3, 'softmax')\n#     ],\n#     name = 'vision_transformer')\n\n# model.summary()","metadata":{"id":"QwXL4zG-BDDD","execution":{"iopub.status.busy":"2023-08-04T22:08:43.606410Z","iopub.execute_input":"2023-08-04T22:08:43.607388Z","iopub.status.idle":"2023-08-04T22:08:47.956809Z","shell.execute_reply.started":"2023-08-04T22:08:43.607346Z","shell.execute_reply":"2023-08-04T22:08:47.955729Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# optimizer = tfa.optimizers.RectifiedAdam(learning_rate = lr)\n\n# model.compile(optimizer = optimizer, \n#               loss = tf.keras.losses.CategoricalCrossentropy(label_smoothing = 0.2), \n#               metrics = ['accuracy'])\n\n# STEP_SIZE_TRAIN = train_gen.n // train_gen.batch_size\n# STEP_SIZE_VALID = valid_gen.n // valid_gen.batch_size\n\n# reduce_lr = tf.keras.callbacks.ReduceLROnPlateau(monitor = 'val_accuracy',\n#                                                  factor = 0.2,\n#                                                  patience = 2,\n#                                                  verbose = 1,\n#                                                  min_delta = 1e-4,\n#                                                  min_lr = 1e-6,\n#                                                  mode = 'max')\n\n# earlystopping = tf.keras.callbacks.EarlyStopping(monitor = 'val_accuracy',\n#                                                  min_delta = 1e-4,\n#                                                  patience = 5,\n#                                                  mode = 'max',\n#                                                  restore_best_weights = True,\n#                                                  verbose = 1)\n\n# checkpointer = tf.keras.callbacks.ModelCheckpoint(filepath = './model.hdf5',\n#                                                   monitor = 'val_accuracy', \n#                                                   verbose = 1, \n#                                                   save_best_only = True,\n#                                                   save_weights_only = True,\n#                                                   mode = 'max')\n\n# callbacks = [earlystopping, reduce_lr, checkpointer]\n","metadata":{"id":"qqtItrkyBDHW","execution":{"iopub.status.busy":"2023-08-04T22:09:07.482694Z","iopub.execute_input":"2023-08-04T22:09:07.483088Z","iopub.status.idle":"2023-08-04T22:09:07.508594Z","shell.execute_reply.started":"2023-08-04T22:09:07.483053Z","shell.execute_reply":"2023-08-04T22:09:07.507649Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# from PIL import ImageFile\n# ImageFile.LOAD_TRUNCATED_IMAGES = True","metadata":{"id":"LsXcM3uIVYSH","execution":{"iopub.status.busy":"2023-08-04T22:09:46.876631Z","iopub.execute_input":"2023-08-04T22:09:46.877035Z","iopub.status.idle":"2023-08-04T22:09:46.882321Z","shell.execute_reply.started":"2023-08-04T22:09:46.877001Z","shell.execute_reply":"2023-08-04T22:09:46.881068Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# history = model.fit(x = train_gen,\n#           steps_per_epoch = STEP_SIZE_TRAIN,\n#           validation_data = valid_gen,\n#           validation_steps = STEP_SIZE_VALID,\n#           epochs = epochs,\n#           callbacks = callbacks)","metadata":{"id":"P-2_E2-mBDLa","execution":{"iopub.status.busy":"2023-08-04T22:09:50.030661Z","iopub.execute_input":"2023-08-04T22:09:50.031125Z","iopub.status.idle":"2023-08-04T22:11:17.487150Z","shell.execute_reply.started":"2023-08-04T22:09:50.031086Z","shell.execute_reply":"2023-08-04T22:11:17.484234Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# plt.plot(history.history[\"loss\"], label=\"train_loss\")\n# plt.plot(history.history[\"val_loss\"], label=\"val_loss\")\n# plt.xlabel(\"Epochs\")\n# plt.ylabel(\"Loss\")\n# plt.title(\"Train and Validation Losses Over Epochs\", fontsize=14)\n# plt.legend()\n# plt.grid()\n# plt.show()","metadata":{"id":"E01Jq52qBDP8","execution":{"iopub.status.busy":"2023-02-08T12:04:37.710778Z","iopub.execute_input":"2023-02-08T12:04:37.711197Z","iopub.status.idle":"2023-02-08T12:04:37.723577Z","shell.execute_reply.started":"2023-02-08T12:04:37.711160Z","shell.execute_reply":"2023-02-08T12:04:37.722558Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# predicted_classes = np.argmax(model.predict(test_gen, steps = test_gen.n // test_gen.batch_size + 1), axis = 1)\n# true_classes = test_gen.classes\n# class_labels = list(test_gen.class_indices.keys())  \n\n# confusionmatrix = confusion_matrix(true_classes, predicted_classes)\n# plt.figure(figsize = (7, 7))\n# sns.heatmap(confusionmatrix, cmap = 'Blues', annot = True, cbar = True)\n\n# print(classification_report(true_classes, predicted_classes))","metadata":{"id":"s3gIRDg-BDTj","execution":{"iopub.status.busy":"2023-02-08T12:04:37.726587Z","iopub.execute_input":"2023-02-08T12:04:37.727228Z","iopub.status.idle":"2023-02-08T12:04:37.735670Z","shell.execute_reply.started":"2023-02-08T12:04:37.727192Z","shell.execute_reply":"2023-02-08T12:04:37.734742Z"},"trusted":true},"execution_count":null,"outputs":[]}]}