{"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":"# Preprocessing and Modelling","metadata":{}},{"cell_type":"markdown","source":"# Imports","metadata":{}},{"cell_type":"code","source":"import numpy as np \nimport pandas as pd\nimport cv2\nimport os\nimport datetime\nfrom sklearn.model_selection import train_test_split\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom tensorflow.keras.applications import VGG16,DenseNet121,ResNet50,InceptionV3,EfficientNetB3\nimport tensorflow_addons as tfa\nfrom tensorflow.keras import layers\nimport tensorflow as tf\nfrom tqdm import notebook\nimport matplotlib.pyplot as plt\nfrom tensorflow.keras import layers\nfrom sklearn.metrics import confusion_matrix\nimport seaborn as sns\nfrom prettytable import PrettyTable","metadata":{"execution":{"iopub.status.busy":"2023-01-13T13:59:01.804879Z","iopub.execute_input":"2023-01-13T13:59:01.805580Z","iopub.status.idle":"2023-01-13T13:59:01.812597Z","shell.execute_reply.started":"2023-01-13T13:59:01.805543Z","shell.execute_reply":"2023-01-13T13:59:01.811160Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"IMG_SIZE = 256\nBATCH_SIZE = 32","metadata":{"execution":{"iopub.status.busy":"2023-01-13T11:50:27.305338Z","iopub.execute_input":"2023-01-13T11:50:27.305689Z","iopub.status.idle":"2023-01-13T11:50:27.310799Z","shell.execute_reply.started":"2023-01-13T11:50:27.305658Z","shell.execute_reply":"2023-01-13T11:50:27.309567Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Reading the data","metadata":{}},{"cell_type":"code","source":"df = pd.read_csv('/kaggle/input/aptos2019-blindness-detection/train.csv')\ndf.head()","metadata":{"execution":{"iopub.status.busy":"2023-01-13T11:50:29.272222Z","iopub.execute_input":"2023-01-13T11:50:29.272561Z","iopub.status.idle":"2023-01-13T11:50:29.302536Z","shell.execute_reply.started":"2023-01-13T11:50:29.272531Z","shell.execute_reply":"2023-01-13T11:50:29.301419Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Train Test Split","metadata":{}},{"cell_type":"code","source":"x_train, x_val, y_train, y_val = train_test_split(df['id_code'], df['diagnosis'], test_size=0.15, stratify=df['diagnosis'],random_state=100)\nx_train = x_train.reset_index(drop=True)\nx_val = x_val.reset_index(drop=True)\ny_train = y_train.reset_index(drop=True)\ny_val = y_val.reset_index(drop=True)","metadata":{"execution":{"iopub.status.busy":"2023-01-13T11:50:32.115357Z","iopub.execute_input":"2023-01-13T11:50:32.115710Z","iopub.status.idle":"2023-01-13T11:50:32.132146Z","shell.execute_reply.started":"2023-01-13T11:50:32.115679Z","shell.execute_reply":"2023-01-13T11:50:32.130995Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train.head()","metadata":{"execution":{"iopub.status.busy":"2023-01-13T11:50:35.064887Z","iopub.execute_input":"2023-01-13T11:50:35.065358Z","iopub.status.idle":"2023-01-13T11:50:35.090057Z","shell.execute_reply.started":"2023-01-13T11:50:35.065315Z","shell.execute_reply":"2023-01-13T11:50:35.088535Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train","metadata":{"execution":{"iopub.status.busy":"2023-01-13T11:50:38.442259Z","iopub.execute_input":"2023-01-13T11:50:38.442600Z","iopub.status.idle":"2023-01-13T11:50:38.450374Z","shell.execute_reply.started":"2023-01-13T11:50:38.442569Z","shell.execute_reply":"2023-01-13T11:50:38.449427Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Preprocessing","metadata":{}},{"cell_type":"markdown","source":"## Adding complete path","metadata":{}},{"cell_type":"code","source":"x_train = x_train.apply(lambda i:'/kaggle/input/aptos2019-blindness-detection/train_images/' + i + \".png\")\nx_val = x_val.apply(lambda i: '/kaggle/input/aptos2019-blindness-detection/train_images/' + i + \".png\")","metadata":{"execution":{"iopub.status.busy":"2023-01-13T11:50:46.012262Z","iopub.execute_input":"2023-01-13T11:50:46.012615Z","iopub.status.idle":"2023-01-13T11:50:46.020679Z","shell.execute_reply.started":"2023-01-13T11:50:46.012585Z","shell.execute_reply":"2023-01-13T11:50:46.019739Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Cropping the image","metadata":{}},{"cell_type":"markdown","source":"### Method 1 (Looses some info)","metadata":{}},{"cell_type":"code","source":"path = \"/kaggle/input/aptos2019-blindness-detection/train_images/001639a390f0.png\"\nimg = cv2.imread(path)\nimg = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)\n\ndef crop_image(img):\n    \n    #Determine height and width\n    h = img.shape[0]\n    w = img.shape[1]\n\n    #Determine centre pixel\n    Ch = h//2\n    Cw = w//2\n\n    #Determine the first non-black pixel directly abovethe center pixel at the top\n    P = 0\n    for i in range(Ch):\n        if not(all(img[i][Cw] == 0)):\n            P = i\n            break;\n\n    #Determine the radius of the retina\n    R = Ch - P\n\n    #Determine the boundaries for cropping\n    X1 = (Ch-R)\n    X2 = (Ch+R)\n    Y1 = (Cw-R)\n    Y2 = (Cw+R)\n\n    img_cropped = []\n    #Crop the image\n    for i in range(3):\n        img_cropped.append(img[X1:X2,Y1:Y2,i])\n\n    img_cropped = np.dstack(img_cropped)\n\nreturn img_cropped","metadata":{"execution":{"iopub.status.busy":"2023-01-13T13:17:03.759952Z","iopub.execute_input":"2023-01-13T13:17:03.760341Z","iopub.status.idle":"2023-01-13T13:17:03.934022Z","shell.execute_reply.started":"2023-01-13T13:17:03.760309Z","shell.execute_reply":"2023-01-13T13:17:03.932990Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.subplot(1, 2, 1)\nplt.axis('off')\nplt.imshow(img);\nplt.subplot(1, 2, 2)\nplt.axis('off')\nplt.imshow(img_cropped);","metadata":{"execution":{"iopub.status.busy":"2023-01-13T13:17:05.989496Z","iopub.execute_input":"2023-01-13T13:17:05.990418Z","iopub.status.idle":"2023-01-13T13:17:07.653515Z","shell.execute_reply.started":"2023-01-13T13:17:05.990364Z","shell.execute_reply":"2023-01-13T13:17:07.652267Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Method 2 (Better)","metadata":{}},{"cell_type":"code","source":"def crop_image_from_gray(img, tol=7):\n    \"\"\"\n    Applies masks to the orignal image and \n    returns the a preprocessed image with \n    3 channels\n    \n    :param img: A NumPy Array that will be cropped\n    :param tol: The tolerance used for masking\n    \n    :return: A NumPy array containing the cropped image\n    \"\"\"\n    # For Grayscale images\n    if img.ndim == 2:\n        mask = img > tol\n        return img[np.ix_(mask.any(axis = 1),mask.any(axis = 0))] # mask.any(axis = 1) makes a boolean array where each element in the array corresponds to each row in the image matrix. For a given row, the corresponding boolean value in the array is true if any value in the row is true.\n                                                                  # mask.any(axis = 0) makes a boolean array where each element in the array corresponds to each col in the image matrix. For a given col, the corresponding boolean value in the array is true if any value in the col is true.\n                                                                  # np.ix_(mask.any(axis = 1),mask.any(axis = 0)) gets those pixels from the image for which both the row and the column value is true.\n    \n    # If we have a normal RGB images\n    elif img.ndim == 3:\n        gray_img = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY) #RGB image to grayscale\n        mask = gray_img > tol #creates a boolean matrix\n        \n        check_shape = img[:,:,0][np.ix_(mask.any(1),mask.any(0))].shape[0]\n        if (check_shape == 0): # Whole image is cropped as it was too dark,\n            return img # return original image\n        else:\n            img1=img[:,:,0][np.ix_(mask.any(axis = 1),mask.any(axis = 0))] #applies mask to pixel 0\n            img2=img[:,:,1][np.ix_(mask.any(axis = 1),mask.any(axis = 0))]\n            img3=img[:,:,2][np.ix_(mask.any(axis = 1),mask.any(axis = 0))]\n            img = np.stack([img1,img2,img3],axis=-1)\n        return img","metadata":{"execution":{"iopub.status.busy":"2023-01-13T11:50:48.787628Z","iopub.execute_input":"2023-01-13T11:50:48.788405Z","iopub.status.idle":"2023-01-13T11:50:48.799497Z","shell.execute_reply.started":"2023-01-13T11:50:48.788369Z","shell.execute_reply":"2023-01-13T11:50:48.798436Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"path = \"/kaggle/input/aptos2019-blindness-detection/train_images/001639a390f0.png\"\nimg = cv2.imread(path)\nimg = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)\n\nplt.subplot(1, 2, 1)\nplt.axis('off')\nplt.imshow(img);\nplt.subplot(1, 2, 2)\nimg_cropped = crop_image_from_gray(img)\nplt.axis('off')\nplt.imshow(img_cropped);","metadata":{"execution":{"iopub.status.busy":"2023-01-13T11:50:51.758044Z","iopub.execute_input":"2023-01-13T11:50:51.758409Z","iopub.status.idle":"2023-01-13T11:50:54.122950Z","shell.execute_reply.started":"2023-01-13T11:50:51.758377Z","shell.execute_reply":"2023-01-13T11:50:54.122042Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Preprocessing pipeline\nhttps://www.kaggle.com/code/ratthachat/aptos-eye-preprocessing-in-diabetic-retinopathy","metadata":{}},{"cell_type":"code","source":"def preprocess(image_path):\n    image = cv2.imread(image_path)\n    image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)\n    image = crop_image_from_gray(image)\n    image = cv2.resize(image, (IMG_SIZE, IMG_SIZE))\n    image = cv2.addWeighted (image, 4, cv2.GaussianBlur(image, (0,0) ,10), -4, 128)\n    return image","metadata":{"execution":{"iopub.status.busy":"2023-01-13T11:51:00.566742Z","iopub.execute_input":"2023-01-13T11:51:00.567250Z","iopub.status.idle":"2023-01-13T11:51:00.574049Z","shell.execute_reply.started":"2023-01-13T11:51:00.567209Z","shell.execute_reply":"2023-01-13T11:51:00.572848Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(6,6))\nfor y,x in enumerate(x_train[:9]):\n    plt.subplot(3, 3, y+1)\n    plt.axis('off')\n    i = preprocess(x)\n    plt.imshow(i)","metadata":{"execution":{"iopub.status.busy":"2023-01-13T11:51:03.341497Z","iopub.execute_input":"2023-01-13T11:51:03.341888Z","iopub.status.idle":"2023-01-13T11:51:05.919391Z","shell.execute_reply.started":"2023-01-13T11:51:03.341854Z","shell.execute_reply":"2023-01-13T11:51:05.918438Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Model Training","metadata":{}},{"cell_type":"markdown","source":"## Making an array of train and validation images","metadata":{}},{"cell_type":"code","source":"train_images = np.empty((len(x_train),IMG_SIZE,IMG_SIZE,3), dtype='uint8')\nfor i,path in enumerate(notebook.tqdm(x_train)):\n    train_images[i] = preprocess(path)","metadata":{"execution":{"iopub.status.busy":"2023-01-13T11:51:13.719971Z","iopub.execute_input":"2023-01-13T11:51:13.720350Z","iopub.status.idle":"2023-01-13T12:01:46.908761Z","shell.execute_reply.started":"2023-01-13T11:51:13.720316Z","shell.execute_reply":"2023-01-13T12:01:46.907466Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train = pd.get_dummies(y_train).values","metadata":{"execution":{"iopub.status.busy":"2023-01-13T12:01:46.910897Z","iopub.execute_input":"2023-01-13T12:01:46.911552Z","iopub.status.idle":"2023-01-13T12:01:46.924295Z","shell.execute_reply.started":"2023-01-13T12:01:46.911514Z","shell.execute_reply":"2023-01-13T12:01:46.923216Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"val_images = np.empty((len(x_val),IMG_SIZE,IMG_SIZE,3), dtype='uint8')\nfor i,path in enumerate(notebook.tqdm(x_val)):\n    val_images[i] = preprocess(path)","metadata":{"execution":{"iopub.status.busy":"2023-01-13T12:01:46.927329Z","iopub.execute_input":"2023-01-13T12:01:46.928201Z","iopub.status.idle":"2023-01-13T12:03:41.297113Z","shell.execute_reply.started":"2023-01-13T12:01:46.928165Z","shell.execute_reply":"2023-01-13T12:03:41.296073Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_val = pd.get_dummies(y_val).values","metadata":{"execution":{"iopub.status.busy":"2023-01-13T12:03:41.299898Z","iopub.execute_input":"2023-01-13T12:03:41.300539Z","iopub.status.idle":"2023-01-13T12:03:41.306827Z","shell.execute_reply.started":"2023-01-13T12:03:41.300500Z","shell.execute_reply":"2023-01-13T12:03:41.305857Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Metric","metadata":{}},{"cell_type":"code","source":"qwk = tfa.metrics.CohenKappa(5,weightage='quadratic')","metadata":{"execution":{"iopub.status.busy":"2023-01-13T12:03:41.308359Z","iopub.execute_input":"2023-01-13T12:03:41.308669Z","iopub.status.idle":"2023-01-13T12:03:41.331346Z","shell.execute_reply.started":"2023-01-13T12:03:41.308643Z","shell.execute_reply":"2023-01-13T12:03:41.330332Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Callbacks","metadata":{}},{"cell_type":"code","source":"class Save_CB(tf.keras.callbacks.Callback):\n    def __init__(self,name):\n        self.name = name\n    def on_train_begin(self, logs={}):\n        self.max = 0\n    def on_epoch_end(self, epoch, logs={}):\n        if logs['val_cohen_kappa'] > self.max:\n            self.max = logs['val_cohen_kappa']\n            print('Validation kappa improved, saving model')\n            self.model.save(str(round(self.max,2))+'_'+self.name+'_.h5')","metadata":{"execution":{"iopub.status.busy":"2023-01-13T12:03:41.332806Z","iopub.execute_input":"2023-01-13T12:03:41.333232Z","iopub.status.idle":"2023-01-13T12:03:41.341332Z","shell.execute_reply.started":"2023-01-13T12:03:41.333198Z","shell.execute_reply":"2023-01-13T12:03:41.339725Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Simple CNN","metadata":{}},{"cell_type":"code","source":"cb = Save_CB('CNN')","metadata":{"execution":{"iopub.status.busy":"2023-01-13T12:15:35.773520Z","iopub.execute_input":"2023-01-13T12:15:35.773934Z","iopub.status.idle":"2023-01-13T12:15:35.780146Z","shell.execute_reply.started":"2023-01-13T12:15:35.773901Z","shell.execute_reply":"2023-01-13T12:15:35.778788Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"input1 = layers.Input((IMG_SIZE,IMG_SIZE,3))\nx = layers.Conv2D(filters=32,kernel_size=3,activation='relu')(input1)\nx = layers.MaxPooling2D(pool_size=2)(x)\nx = layers.Dropout(0.2)(x)\nx = layers.Flatten()(x)\nx = layers.Dense(32,activation='relu')(x)\noutput1 = layers.Dense(5,activation = 'softmax')(x)","metadata":{"execution":{"iopub.status.busy":"2023-01-13T12:15:38.103649Z","iopub.execute_input":"2023-01-13T12:15:38.104179Z","iopub.status.idle":"2023-01-13T12:15:38.191247Z","shell.execute_reply.started":"2023-01-13T12:15:38.104131Z","shell.execute_reply":"2023-01-13T12:15:38.190097Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model1=tf.keras.Model(inputs=input1, outputs=output1)","metadata":{"execution":{"iopub.status.busy":"2023-01-13T12:15:40.050910Z","iopub.execute_input":"2023-01-13T12:15:40.051575Z","iopub.status.idle":"2023-01-13T12:15:40.062380Z","shell.execute_reply.started":"2023-01-13T12:15:40.051540Z","shell.execute_reply":"2023-01-13T12:15:40.060849Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model1.summary()","metadata":{"execution":{"iopub.status.busy":"2023-01-13T12:15:41.871238Z","iopub.execute_input":"2023-01-13T12:15:41.872205Z","iopub.status.idle":"2023-01-13T12:15:41.879917Z","shell.execute_reply.started":"2023-01-13T12:15:41.872170Z","shell.execute_reply":"2023-01-13T12:15:41.878781Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model1.compile(loss='categorical_crossentropy',\n              optimizer=tf.keras.optimizers.Adam(learning_rate=0.001),\n              metrics=[qwk]\n              )","metadata":{"execution":{"iopub.status.busy":"2023-01-13T12:15:44.455250Z","iopub.execute_input":"2023-01-13T12:15:44.455607Z","iopub.status.idle":"2023-01-13T12:15:44.466756Z","shell.execute_reply.started":"2023-01-13T12:15:44.455577Z","shell.execute_reply":"2023-01-13T12:15:44.465516Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model1.fit(train_images, y_train, \n                     steps_per_epoch= len(y_train) // BATCH_SIZE, \n                     epochs=10, \n                     validation_data = (val_images,y_val), \n                     validation_steps = len(y_val) // BATCH_SIZE,\n                     callbacks = cb\n                     )","metadata":{"execution":{"iopub.status.busy":"2023-01-13T12:15:46.695534Z","iopub.execute_input":"2023-01-13T12:15:46.696422Z","iopub.status.idle":"2023-01-13T12:16:29.387803Z","shell.execute_reply.started":"2023-01-13T12:15:46.696382Z","shell.execute_reply":"2023-01-13T12:16:29.386687Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Plots","metadata":{}},{"cell_type":"code","source":"plt.figure(figsize=[12,6])\nplt.subplot(2,2,1)\nplt.plot(history.history['loss'],label='loss')\nplt.plot(history.history['val_loss'],label='val_loss')\nplt.xlabel('Epoch')\nplt.ylabel('Cross Entropy Loss')\nplt.legend()\nplt.title('Cross Entropy Loss Per Epoch')\n\nplt.subplot(2,2,2)\nplt.plot(history.history['cohen_kappa'],label='cohen_kappa')\nplt.plot(history.history['val_cohen_kappa'],label='val_cohen_kappa')\nplt.xlabel('Epoch')\nplt.ylabel('Kappa Score')\nplt.title('Kappa Score Per Epoch')\nplt.legend()\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-01-13T12:16:29.391221Z","iopub.execute_input":"2023-01-13T12:16:29.392160Z","iopub.status.idle":"2023-01-13T12:16:29.796843Z","shell.execute_reply.started":"2023-01-13T12:16:29.392119Z","shell.execute_reply":"2023-01-13T12:16:29.795655Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred = model1.predict(val_images)\ncf_matrix = confusion_matrix([np.argmax(x) for x in y_val], [np.argmax(x) for x in y_pred])\nsns.heatmap(cf_matrix, annot=True, fmt=\"0\")\nplt.ylabel(\"True Labels\")\nplt.xlabel(\"Predicted Labels\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-01-13T12:16:30.004440Z","iopub.execute_input":"2023-01-13T12:16:30.004846Z","iopub.status.idle":"2023-01-13T12:16:31.284557Z","shell.execute_reply.started":"2023-01-13T12:16:30.004814Z","shell.execute_reply":"2023-01-13T12:16:31.283618Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Transfer Learning","metadata":{}},{"cell_type":"markdown","source":"## VGG 16","metadata":{}},{"cell_type":"code","source":"cb = Save_CB('VGG16')","metadata":{"execution":{"iopub.status.busy":"2023-01-13T12:03:41.342800Z","iopub.execute_input":"2023-01-13T12:03:41.343472Z","iopub.status.idle":"2023-01-13T12:03:41.351388Z","shell.execute_reply.started":"2023-01-13T12:03:41.343438Z","shell.execute_reply":"2023-01-13T12:03:41.350476Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"vgg16_model=VGG16(input_shape=(IMG_SIZE,IMG_SIZE,3), weights='imagenet', include_top=False)","metadata":{"execution":{"iopub.status.busy":"2023-01-13T12:03:41.352625Z","iopub.execute_input":"2023-01-13T12:03:41.353414Z","iopub.status.idle":"2023-01-13T12:03:42.352552Z","shell.execute_reply.started":"2023-01-13T12:03:41.353380Z","shell.execute_reply":"2023-01-13T12:03:42.351459Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x = vgg16_model.output\nx = layers.Conv2D(filters=32,kernel_size=3,activation='relu')(x)\nx = layers.MaxPooling2D(pool_size=2)(x)\nx = layers.Dropout(0.2)(x)\nx = layers.Flatten()(x)\nx = layers.Dense(32,activation='relu')(x)\nvgg16_output = layers.Dense(units=5, activation='softmax')(x)\n\n#base_model.trainable=False","metadata":{"execution":{"iopub.status.busy":"2023-01-13T12:04:48.689210Z","iopub.execute_input":"2023-01-13T12:04:48.689707Z","iopub.status.idle":"2023-01-13T12:04:48.728386Z","shell.execute_reply.started":"2023-01-13T12:04:48.689613Z","shell.execute_reply":"2023-01-13T12:04:48.727413Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_vgg = tf.keras.Model(inputs = vgg16_model.inputs, outputs=vgg16_output)","metadata":{"execution":{"iopub.status.busy":"2023-01-13T12:04:51.136454Z","iopub.execute_input":"2023-01-13T12:04:51.136915Z","iopub.status.idle":"2023-01-13T12:04:51.148855Z","shell.execute_reply.started":"2023-01-13T12:04:51.136882Z","shell.execute_reply":"2023-01-13T12:04:51.147777Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_vgg.summary()","metadata":{"execution":{"iopub.status.busy":"2023-01-13T12:04:53.819112Z","iopub.execute_input":"2023-01-13T12:04:53.819521Z","iopub.status.idle":"2023-01-13T12:04:53.828116Z","shell.execute_reply.started":"2023-01-13T12:04:53.819487Z","shell.execute_reply":"2023-01-13T12:04:53.827050Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_vgg.compile(loss='categorical_crossentropy',\n              optimizer=tf.keras.optimizers.Adam(learning_rate=0.001),\n              metrics=[qwk]\n              )","metadata":{"execution":{"iopub.status.busy":"2023-01-13T12:05:28.964198Z","iopub.execute_input":"2023-01-13T12:05:28.964565Z","iopub.status.idle":"2023-01-13T12:05:28.980401Z","shell.execute_reply.started":"2023-01-13T12:05:28.964534Z","shell.execute_reply":"2023-01-13T12:05:28.979488Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history_vgg = model_vgg.fit(train_images, y_train, \n                            steps_per_epoch= len(y_train) // BATCH_SIZE, \n                            epochs=10,\n                            validation_data = (val_images,y_val), \n                            validation_steps = len(y_val) // BATCH_SIZE,\n                            callbacks = cb\n                           )","metadata":{"execution":{"iopub.status.busy":"2023-01-13T12:05:31.397486Z","iopub.execute_input":"2023-01-13T12:05:31.397889Z","iopub.status.idle":"2023-01-13T12:10:43.468340Z","shell.execute_reply.started":"2023-01-13T12:05:31.397854Z","shell.execute_reply":"2023-01-13T12:10:43.467321Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Plots","metadata":{}},{"cell_type":"code","source":"plt.figure(figsize=[12,6])\nplt.subplot(2,2,1)\nplt.plot(history_vgg.history['loss'],label='loss')\nplt.plot(history_vgg.history['val_loss'],label='val_loss')\nplt.xlabel('Epoch')\nplt.ylabel('Cross Entropy Loss')\nplt.legend()\nplt.title('Cross Entropy Loss Per Epoch')\n\nplt.subplot(2,2,2)\nplt.plot(history_vgg.history['cohen_kappa'],label='cohen_kappa')\nplt.plot(history_vgg.history['val_cohen_kappa'],label='val_cohen_kappa')\nplt.xlabel('Epoch')\nplt.ylabel('Kappa Score')\nplt.title('Kappa Score Per Epoch')\nplt.legend()\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-01-13T12:11:49.304337Z","iopub.execute_input":"2023-01-13T12:11:49.304710Z","iopub.status.idle":"2023-01-13T12:11:49.644216Z","shell.execute_reply.started":"2023-01-13T12:11:49.304677Z","shell.execute_reply":"2023-01-13T12:11:49.643251Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred = model_vgg.predict(val_images)\ncf_matrix = confusion_matrix([np.argmax(x) for x in y_val], [np.argmax(x) for x in y_pred])\nsns.heatmap(cf_matrix, annot=True, fmt=\"0\")\nplt.ylabel(\"True Labels\")\nplt.xlabel(\"Predicted Labels\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-01-13T12:14:10.656561Z","iopub.execute_input":"2023-01-13T12:14:10.657471Z","iopub.status.idle":"2023-01-13T12:14:12.388665Z","shell.execute_reply.started":"2023-01-13T12:14:10.657434Z","shell.execute_reply":"2023-01-13T12:14:12.387651Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## DenseNet121","metadata":{}},{"cell_type":"code","source":"cb = Save_CB('Dense121')","metadata":{"execution":{"iopub.status.busy":"2023-01-13T12:30:58.986249Z","iopub.execute_input":"2023-01-13T12:30:58.986635Z","iopub.status.idle":"2023-01-13T12:30:58.991565Z","shell.execute_reply.started":"2023-01-13T12:30:58.986579Z","shell.execute_reply":"2023-01-13T12:30:58.990256Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"densenet = DenseNet121(\n    weights='imagenet',\n    include_top=False,\n    input_shape=(IMG_SIZE,IMG_SIZE,3)\n)","metadata":{"execution":{"iopub.status.busy":"2023-01-13T12:31:00.757220Z","iopub.execute_input":"2023-01-13T12:31:00.757619Z","iopub.status.idle":"2023-01-13T12:31:04.664127Z","shell.execute_reply.started":"2023-01-13T12:31:00.757586Z","shell.execute_reply":"2023-01-13T12:31:04.662947Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x = densenet.output\nx = layers.GlobalAveragePooling2D()(x)\nx = layers.Dropout(0.5)(x)\ndensenet_op = layers.Dense(5, activation='softmax')(x)","metadata":{"execution":{"iopub.status.busy":"2023-01-13T12:31:04.690990Z","iopub.execute_input":"2023-01-13T12:31:04.691390Z","iopub.status.idle":"2023-01-13T12:31:04.710621Z","shell.execute_reply.started":"2023-01-13T12:31:04.691354Z","shell.execute_reply":"2023-01-13T12:31:04.709682Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_dn = tf.keras.Model(inputs = densenet.inputs, outputs=densenet_op)","metadata":{"execution":{"iopub.status.busy":"2023-01-13T12:31:05.996630Z","iopub.execute_input":"2023-01-13T12:31:05.997547Z","iopub.status.idle":"2023-01-13T12:31:06.027940Z","shell.execute_reply.started":"2023-01-13T12:31:05.997497Z","shell.execute_reply":"2023-01-13T12:31:06.026863Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_dn.summary()","metadata":{"execution":{"iopub.status.busy":"2023-01-13T12:31:09.013860Z","iopub.execute_input":"2023-01-13T12:31:09.014211Z","iopub.status.idle":"2023-01-13T12:31:09.071355Z","shell.execute_reply.started":"2023-01-13T12:31:09.014183Z","shell.execute_reply":"2023-01-13T12:31:09.070393Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Plots","metadata":{}},{"cell_type":"code","source":"model_dn.compile(loss='categorical_crossentropy',\n              optimizer=tf.keras.optimizers.Adam(learning_rate=0.00005),\n              metrics=[qwk]\n              )","metadata":{"execution":{"iopub.status.busy":"2023-01-13T12:31:16.083672Z","iopub.execute_input":"2023-01-13T12:31:16.084340Z","iopub.status.idle":"2023-01-13T12:31:16.102978Z","shell.execute_reply.started":"2023-01-13T12:31:16.084303Z","shell.execute_reply":"2023-01-13T12:31:16.101855Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history_dn = model_dn.fit(train_images, y_train, \n                            steps_per_epoch= len(y_train) // BATCH_SIZE, \n                            epochs=20,\n                            validation_data = (val_images,y_val), \n                            validation_steps = len(y_val) // BATCH_SIZE,\n                            callbacks = cb\n                           )","metadata":{"execution":{"iopub.status.busy":"2023-01-13T12:31:21.375836Z","iopub.execute_input":"2023-01-13T12:31:21.376191Z","iopub.status.idle":"2023-01-13T12:39:17.557377Z","shell.execute_reply.started":"2023-01-13T12:31:21.376160Z","shell.execute_reply":"2023-01-13T12:39:17.556383Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=[12,6])\nplt.subplot(2,2,1)\nplt.plot(history_dn.history['loss'],label='loss')\nplt.plot(history_dn.history['val_loss'],label='val_loss')\nplt.xlabel('Epoch')\nplt.ylabel('Cross Entropy Loss')\nplt.legend()\nplt.title('Cross Entropy Loss Per Epoch')\n\nplt.subplot(2,2,2)\nplt.plot(history_dn.history['cohen_kappa'],label='cohen_kappa')\nplt.plot(history_dn.history['val_cohen_kappa'],label='val_cohen_kappa')\nplt.xlabel('Epoch')\nplt.ylabel('Kappa Score')\nplt.title('Kappa Score Per Epoch')\nplt.legend()\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-01-13T12:52:54.653558Z","iopub.execute_input":"2023-01-13T12:52:54.653951Z","iopub.status.idle":"2023-01-13T12:52:55.002782Z","shell.execute_reply.started":"2023-01-13T12:52:54.653919Z","shell.execute_reply":"2023-01-13T12:52:55.001669Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred = model_dn.predict(val_images)\ncf_matrix = confusion_matrix([np.argmax(x) for x in y_val], [np.argmax(x) for x in y_pred])\nsns.heatmap(cf_matrix, annot=True, fmt=\"0\")\nplt.ylabel(\"True Labels\")\nplt.xlabel(\"Predicted Labels\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-01-13T12:42:09.241379Z","iopub.execute_input":"2023-01-13T12:42:09.241760Z","iopub.status.idle":"2023-01-13T12:42:13.241420Z","shell.execute_reply.started":"2023-01-13T12:42:09.241706Z","shell.execute_reply":"2023-01-13T12:42:13.240389Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## ResNet50","metadata":{}},{"cell_type":"code","source":"cb = Save_CB('Resnet')","metadata":{"execution":{"iopub.status.busy":"2023-01-13T12:59:08.501507Z","iopub.execute_input":"2023-01-13T12:59:08.502014Z","iopub.status.idle":"2023-01-13T12:59:08.507819Z","shell.execute_reply.started":"2023-01-13T12:59:08.501975Z","shell.execute_reply":"2023-01-13T12:59:08.506447Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"resnet = ResNet50(weights='imagenet', \n                  include_top=False, \n                  input_shape=(IMG_SIZE,IMG_SIZE,3)\n                 )","metadata":{"execution":{"iopub.status.busy":"2023-01-13T12:59:08.509648Z","iopub.execute_input":"2023-01-13T12:59:08.510424Z","iopub.status.idle":"2023-01-13T12:59:09.915539Z","shell.execute_reply.started":"2023-01-13T12:59:08.510368Z","shell.execute_reply":"2023-01-13T12:59:09.914498Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x = resnet.output\nx = layers.GlobalAveragePooling2D()(x)\nx = layers.Dropout(0.5)(x)\nresnet_op = layers.Dense(5, activation='softmax')(x)","metadata":{"execution":{"iopub.status.busy":"2023-01-13T12:59:09.918028Z","iopub.execute_input":"2023-01-13T12:59:09.918415Z","iopub.status.idle":"2023-01-13T12:59:09.938252Z","shell.execute_reply.started":"2023-01-13T12:59:09.918377Z","shell.execute_reply":"2023-01-13T12:59:09.937354Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_rn = tf.keras.Model(inputs = resnet.inputs, outputs=resnet_op)","metadata":{"execution":{"iopub.status.busy":"2023-01-13T12:59:09.939809Z","iopub.execute_input":"2023-01-13T12:59:09.940176Z","iopub.status.idle":"2023-01-13T12:59:09.956596Z","shell.execute_reply.started":"2023-01-13T12:59:09.940142Z","shell.execute_reply":"2023-01-13T12:59:09.955765Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_rn.summary()","metadata":{"execution":{"iopub.status.busy":"2023-01-13T12:59:09.959418Z","iopub.execute_input":"2023-01-13T12:59:09.960062Z","iopub.status.idle":"2023-01-13T12:59:09.988086Z","shell.execute_reply.started":"2023-01-13T12:59:09.960025Z","shell.execute_reply":"2023-01-13T12:59:09.987209Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_rn.compile(loss='categorical_crossentropy',\n              optimizer=tf.keras.optimizers.Adam(learning_rate=0.001),\n              metrics=[qwk]\n              )","metadata":{"execution":{"iopub.status.busy":"2023-01-13T12:59:28.062826Z","iopub.execute_input":"2023-01-13T12:59:28.063218Z","iopub.status.idle":"2023-01-13T12:59:28.078842Z","shell.execute_reply.started":"2023-01-13T12:59:28.063186Z","shell.execute_reply":"2023-01-13T12:59:28.077779Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history_rn = model_rn.fit(train_images, y_train, \n                            steps_per_epoch= len(y_train) // BATCH_SIZE, \n                            epochs=15,\n                            validation_data = (val_images,y_val), \n                            validation_steps = len(y_val) // BATCH_SIZE,\n                            callbacks = cb\n                           )","metadata":{"execution":{"iopub.status.busy":"2023-01-13T12:59:30.909146Z","iopub.execute_input":"2023-01-13T12:59:30.909508Z","iopub.status.idle":"2023-01-13T13:05:59.178289Z","shell.execute_reply.started":"2023-01-13T12:59:30.909478Z","shell.execute_reply":"2023-01-13T13:05:59.177203Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Plots","metadata":{}},{"cell_type":"code","source":"plt.figure(figsize=[12,6])\nplt.subplot(2,2,1)\nplt.plot(history_rn.history['loss'],label='loss')\nplt.plot(history_rn.history['val_loss'],label='val_loss')\nplt.xlabel('Epoch')\nplt.ylabel('Cross Entropy Loss')\nplt.legend()\nplt.title('Cross Entropy Loss Per Epoch')\n\nplt.subplot(2,2,2)\nplt.plot(history_rn.history['cohen_kappa'],label='cohen_kappa')\nplt.plot(history_rn.history['val_cohen_kappa'],label='val_cohen_kappa')\nplt.xlabel('Epoch')\nplt.ylabel('Kappa Score')\nplt.title('Kappa Score Per Epoch')\nplt.legend()\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-01-13T13:05:59.557385Z","iopub.execute_input":"2023-01-13T13:05:59.558135Z","iopub.status.idle":"2023-01-13T13:05:59.918767Z","shell.execute_reply.started":"2023-01-13T13:05:59.558095Z","shell.execute_reply":"2023-01-13T13:05:59.917739Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred = model_rn.predict(val_images)\ncf_matrix = confusion_matrix([np.argmax(x) for x in y_val], [np.argmax(x) for x in y_pred])\nsns.heatmap(cf_matrix, annot=True, fmt=\"0\")\nplt.ylabel(\"True Labels\")\nplt.xlabel(\"Predicted Labels\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-01-13T13:06:07.444060Z","iopub.execute_input":"2023-01-13T13:06:07.444467Z","iopub.status.idle":"2023-01-13T13:06:15.840900Z","shell.execute_reply.started":"2023-01-13T13:06:07.444433Z","shell.execute_reply":"2023-01-13T13:06:15.839706Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## InceptionNetV3","metadata":{}},{"cell_type":"code","source":"cb = Save_CB('Incnet')","metadata":{"execution":{"iopub.status.busy":"2023-01-13T13:37:29.124815Z","iopub.execute_input":"2023-01-13T13:37:29.125191Z","iopub.status.idle":"2023-01-13T13:37:29.130405Z","shell.execute_reply.started":"2023-01-13T13:37:29.125158Z","shell.execute_reply":"2023-01-13T13:37:29.129278Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"incnet = InceptionV3(weights='imagenet', \n                     include_top=False, \n                     input_shape=(IMG_SIZE,IMG_SIZE,3)\n                    )","metadata":{"execution":{"iopub.status.busy":"2023-01-13T13:37:29.132477Z","iopub.execute_input":"2023-01-13T13:37:29.133222Z","iopub.status.idle":"2023-01-13T13:37:31.094533Z","shell.execute_reply.started":"2023-01-13T13:37:29.133180Z","shell.execute_reply":"2023-01-13T13:37:31.093381Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x = incnet.output\nx = layers.GlobalAveragePooling2D()(x)\nx = layers.Dropout(0.5)(x)\nincnet_op = layers.Dense(5, activation='softmax')(x)","metadata":{"execution":{"iopub.status.busy":"2023-01-13T13:37:31.096524Z","iopub.execute_input":"2023-01-13T13:37:31.096990Z","iopub.status.idle":"2023-01-13T13:37:31.120047Z","shell.execute_reply.started":"2023-01-13T13:37:31.096952Z","shell.execute_reply":"2023-01-13T13:37:31.118552Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_in = tf.keras.Model(inputs = incnet.inputs, outputs=incnet_op)","metadata":{"execution":{"iopub.status.busy":"2023-01-13T13:37:31.121776Z","iopub.execute_input":"2023-01-13T13:37:31.122557Z","iopub.status.idle":"2023-01-13T13:37:31.146869Z","shell.execute_reply.started":"2023-01-13T13:37:31.122493Z","shell.execute_reply":"2023-01-13T13:37:31.145774Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_in.summary()","metadata":{"execution":{"iopub.status.busy":"2023-01-13T13:37:31.150461Z","iopub.execute_input":"2023-01-13T13:37:31.150846Z","iopub.status.idle":"2023-01-13T13:37:31.192965Z","shell.execute_reply.started":"2023-01-13T13:37:31.150811Z","shell.execute_reply":"2023-01-13T13:37:31.191956Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_in.compile(loss='categorical_crossentropy',\n              optimizer=tf.keras.optimizers.Adam(learning_rate=0.0002),\n              metrics=[qwk]\n              )","metadata":{"execution":{"iopub.status.busy":"2023-01-13T13:37:31.194479Z","iopub.execute_input":"2023-01-13T13:37:31.195181Z","iopub.status.idle":"2023-01-13T13:37:31.211980Z","shell.execute_reply.started":"2023-01-13T13:37:31.195144Z","shell.execute_reply":"2023-01-13T13:37:31.210867Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history_in = model_in.fit(train_images, y_train, \n                            steps_per_epoch= len(y_train) // BATCH_SIZE, \n                            epochs=15,\n                            validation_data = (val_images,y_val), \n                            validation_steps = len(y_val) // BATCH_SIZE,\n                            callbacks = cb\n                           )","metadata":{"execution":{"iopub.status.busy":"2023-01-13T13:37:31.213852Z","iopub.execute_input":"2023-01-13T13:37:31.214225Z","iopub.status.idle":"2023-01-13T13:43:01.205513Z","shell.execute_reply.started":"2023-01-13T13:37:31.214186Z","shell.execute_reply":"2023-01-13T13:43:01.204474Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Plots","metadata":{}},{"cell_type":"code","source":"plt.figure(figsize=[12,6])\nplt.subplot(2,2,1)\nplt.plot(history_in.history['loss'],label='loss')\nplt.plot(history_in.history['val_loss'],label='val_loss')\nplt.xlabel('Epoch')\nplt.ylabel('Cross Entropy Loss')\nplt.legend()\nplt.title('Cross Entropy Loss Per Epoch')\n\nplt.subplot(2,2,2)\nplt.plot(history_in.history['cohen_kappa'],label='cohen_kappa')\nplt.plot(history_in.history['val_cohen_kappa'],label='val_cohen_kappa')\nplt.xlabel('Epoch')\nplt.ylabel('Kappa Score')\nplt.title('Kappa Score Per Epoch')\nplt.legend()\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-01-13T13:43:01.208189Z","iopub.execute_input":"2023-01-13T13:43:01.208576Z","iopub.status.idle":"2023-01-13T13:43:01.564595Z","shell.execute_reply.started":"2023-01-13T13:43:01.208540Z","shell.execute_reply":"2023-01-13T13:43:01.563681Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred = model_in.predict(val_images)\ncf_matrix = confusion_matrix([np.argmax(x) for x in y_val], [np.argmax(x) for x in y_pred])\nsns.heatmap(cf_matrix, annot=True, fmt=\"0\")\nplt.ylabel(\"True Labels\")\nplt.xlabel(\"Predicted Labels\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-01-13T13:43:01.566094Z","iopub.execute_input":"2023-01-13T13:43:01.567987Z","iopub.status.idle":"2023-01-13T13:43:04.827142Z","shell.execute_reply.started":"2023-01-13T13:43:01.567945Z","shell.execute_reply":"2023-01-13T13:43:04.826145Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## EfficientNetB3","metadata":{}},{"cell_type":"code","source":"cb = Save_CB('Effcnet')","metadata":{"execution":{"iopub.status.busy":"2023-01-13T13:43:04.833192Z","iopub.execute_input":"2023-01-13T13:43:04.833491Z","iopub.status.idle":"2023-01-13T13:43:04.838744Z","shell.execute_reply.started":"2023-01-13T13:43:04.833463Z","shell.execute_reply":"2023-01-13T13:43:04.837674Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"effnet = EfficientNetB3(weights='imagenet', \n                     include_top=False, \n                     input_shape=(IMG_SIZE,IMG_SIZE,3)\n                    )","metadata":{"execution":{"iopub.status.busy":"2023-01-13T13:43:04.843203Z","iopub.execute_input":"2023-01-13T13:43:04.843550Z","iopub.status.idle":"2023-01-13T13:43:07.621373Z","shell.execute_reply.started":"2023-01-13T13:43:04.843525Z","shell.execute_reply":"2023-01-13T13:43:07.620283Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x = effnet.output\nx = layers.GlobalAveragePooling2D()(x)\nx = layers.Dropout(0.5)(x)\neffnet_op = layers.Dense(5, activation='softmax')(x)","metadata":{"execution":{"iopub.status.busy":"2023-01-13T13:43:07.622834Z","iopub.execute_input":"2023-01-13T13:43:07.623248Z","iopub.status.idle":"2023-01-13T13:43:07.646219Z","shell.execute_reply.started":"2023-01-13T13:43:07.623211Z","shell.execute_reply":"2023-01-13T13:43:07.645245Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_en = tf.keras.Model(inputs = effnet.inputs, outputs=effnet_op)","metadata":{"execution":{"iopub.status.busy":"2023-01-13T13:43:07.647842Z","iopub.execute_input":"2023-01-13T13:43:07.648247Z","iopub.status.idle":"2023-01-13T13:43:07.672512Z","shell.execute_reply.started":"2023-01-13T13:43:07.648212Z","shell.execute_reply":"2023-01-13T13:43:07.671462Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_en.compile(loss='categorical_crossentropy',\n              optimizer=tf.keras.optimizers.Adam(learning_rate=0.0001),\n              metrics=[qwk]\n              )","metadata":{"execution":{"iopub.status.busy":"2023-01-13T13:43:07.723339Z","iopub.execute_input":"2023-01-13T13:43:07.723778Z","iopub.status.idle":"2023-01-13T13:43:07.741057Z","shell.execute_reply.started":"2023-01-13T13:43:07.723737Z","shell.execute_reply":"2023-01-13T13:43:07.739907Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history_en = model_en.fit(train_images, y_train, \n                            steps_per_epoch= len(y_train) // BATCH_SIZE, \n                            epochs=15,\n                            validation_data = (val_images,y_val), \n                            validation_steps = len(y_val) // BATCH_SIZE,\n                            callbacks = cb\n                           )","metadata":{"execution":{"iopub.status.busy":"2023-01-13T13:43:07.742999Z","iopub.execute_input":"2023-01-13T13:43:07.743398Z","iopub.status.idle":"2023-01-13T13:48:37.414493Z","shell.execute_reply.started":"2023-01-13T13:43:07.743359Z","shell.execute_reply":"2023-01-13T13:48:37.413373Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Plots","metadata":{}},{"cell_type":"code","source":"plt.figure(figsize=[12,6])\nplt.subplot(2,2,1)\nplt.plot(history_en.history['loss'],label='loss')\nplt.plot(history_en.history['val_loss'],label='val_loss')\nplt.xlabel('Epoch')\nplt.ylabel('Cross Entropy Loss')\nplt.legend()\nplt.title('Cross Entropy Loss Per Epoch')\n\nplt.subplot(2,2,2)\nplt.plot(history_en.history['cohen_kappa'],label='cohen_kappa')\nplt.plot(history_en.history['val_cohen_kappa'],label='val_cohen_kappa')\nplt.xlabel('Epoch')\nplt.ylabel('Kappa Score')\nplt.title('Kappa Score Per Epoch')\nplt.legend()\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-01-13T13:48:37.422593Z","iopub.execute_input":"2023-01-13T13:48:37.422953Z","iopub.status.idle":"2023-01-13T13:48:37.799807Z","shell.execute_reply.started":"2023-01-13T13:48:37.422925Z","shell.execute_reply":"2023-01-13T13:48:37.798795Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred = model_en.predict(val_images)\ncf_matrix = confusion_matrix([np.argmax(x) for x in y_val], [np.argmax(x) for x in y_pred])\nsns.heatmap(cf_matrix, annot=True, fmt=\"0\")\nplt.ylabel(\"True Labels\")\nplt.xlabel(\"Predicted Labels\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-01-13T13:48:37.801519Z","iopub.execute_input":"2023-01-13T13:48:37.801955Z","iopub.status.idle":"2023-01-13T13:48:40.769902Z","shell.execute_reply.started":"2023-01-13T13:48:37.801916Z","shell.execute_reply":"2023-01-13T13:48:40.768762Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Summary","metadata":{}},{"cell_type":"code","source":"pt = PrettyTable()","metadata":{"execution":{"iopub.status.busy":"2023-01-13T14:00:20.328371Z","iopub.execute_input":"2023-01-13T14:00:20.328763Z","iopub.status.idle":"2023-01-13T14:00:20.333425Z","shell.execute_reply.started":"2023-01-13T14:00:20.328710Z","shell.execute_reply":"2023-01-13T14:00:20.332222Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pt.field_names = ['Model','Val Loss','Val Kappa']\npt.add_row([\"Baseline\", min(history.history['val_loss']), max(history.history['val_cohen_kappa'])])\npt.add_row([\"VGG16\", min(history_vgg.history['val_loss']), max(history_vgg.history['val_cohen_kappa'])])\npt.add_row([\"DenseNet121\", min(history_dn.history['val_loss']), max(history_dn.history['val_cohen_kappa'])])\npt.add_row([\"ResNet50\", min(history_rn.history['val_loss']), max(history_rn.history['val_cohen_kappa'])])\npt.add_row([\"InceptionV3\", min(history_in.history['val_loss']), max(history_in.history['val_cohen_kappa'])])\npt.add_row([\"EfficientNetB5\", min(history_en.history['val_loss']), max(history_en.history['val_cohen_kappa'])])","metadata":{"execution":{"iopub.status.busy":"2023-01-13T14:05:50.864786Z","iopub.execute_input":"2023-01-13T14:05:50.865189Z","iopub.status.idle":"2023-01-13T14:05:50.873875Z","shell.execute_reply.started":"2023-01-13T14:05:50.865156Z","shell.execute_reply":"2023-01-13T14:05:50.872748Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(pt)","metadata":{"execution":{"iopub.status.busy":"2023-01-13T14:07:02.933520Z","iopub.execute_input":"2023-01-13T14:07:02.934033Z","iopub.status.idle":"2023-01-13T14:07:02.945088Z","shell.execute_reply.started":"2023-01-13T14:07:02.933994Z","shell.execute_reply":"2023-01-13T14:07:02.943127Z"},"trusted":true},"execution_count":null,"outputs":[]}]}