{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nfrom pathlib import Path\nimport os.path\nimport matplotlib.pyplot as plt\nfrom IPython.display import Image, display, Markdown\nimport matplotlib.cm as cm\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.metrics import confusion_matrix\nimport tensorflow as tf\nfrom time import perf_counter\nimport seaborn as sns\n\ndef printmd(string):\n    # Print with Markdowns    \n    display(Markdown(string))","metadata":{"execution":{"iopub.status.busy":"2023-04-10T06:51:37.668479Z","iopub.execute_input":"2023-04-10T06:51:37.668983Z","iopub.status.idle":"2023-04-10T06:51:37.676858Z","shell.execute_reply.started":"2023-04-10T06:51:37.668947Z","shell.execute_reply":"2023-04-10T06:51:37.675664Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"new_train = pd.read_csv('../input/aptos2019-blindness-detection/train.csv')\nnew_train = new_train.dropna()\nnew_train['id_code'] = '/kaggle/input/aptos2019-blindness-detection/train_images/' + new_train['id_code'].astype(str) + '.png'\n\n\nnew_train_1 = pd.read_csv('../input/aptos2019-blindness-detection/test.csv')\nnew_train_1 = new_train_1.dropna()\nnew_train_1['id_code'] = '/kaggle/input/aptos2019-blindness-detection/test_images/' + new_train['id_code'].astype(str) + '.png'\n\nnew_train = pd.concat([new_train,new_train_1])\nold_train = pd.read_csv('../input/diabetic-retinopathy-resized/trainLabels.csv')\nold_train = old_train.dropna()\n\n\nold_train = old_train[['image','level']]\nold_train.columns = new_train.columns\nold_train.diagnosis.value_counts()\nold_train['id_code'] = '/kaggle/input/diabetic-retinopathy-resized/resized_train/resized_train/' + old_train['id_code'].astype(str) + '.jpeg'\n\n\n\n\n","metadata":{"execution":{"iopub.status.busy":"2023-04-10T06:51:37.678627Z","iopub.execute_input":"2023-04-10T06:51:37.679172Z","iopub.status.idle":"2023-04-10T06:51:37.754950Z","shell.execute_reply.started":"2023-04-10T06:51:37.679107Z","shell.execute_reply":"2023-04-10T06:51:37.754030Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(new_train.columns)\nprint(old_train.columns)\n\nframes = [new_train, old_train]\n  \ndata = pd.concat(frames)","metadata":{"execution":{"iopub.status.busy":"2023-04-10T06:51:37.756315Z","iopub.execute_input":"2023-04-10T06:51:37.756683Z","iopub.status.idle":"2023-04-10T06:51:37.766340Z","shell.execute_reply.started":"2023-04-10T06:51:37.756631Z","shell.execute_reply":"2023-04-10T06:51:37.765394Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\nimport os\nfrom tqdm import tqdm\n\n\n\n# add a new column called 'present' and set default value to False\ndata['present'] = False\n\n# loop through each row and check if the file exists\nfor i, row in tqdm(data.iterrows(), total=data.shape[0]):\n    if os.path.exists(row['id_code']):\n        data.at[i, 'present'] = True\n\n# print the updated table\nprint(data)\n","metadata":{"execution":{"iopub.status.busy":"2023-04-10T06:51:37.767676Z","iopub.execute_input":"2023-04-10T06:51:37.768190Z","iopub.status.idle":"2023-04-10T06:52:42.789082Z","shell.execute_reply.started":"2023-04-10T06:51:37.768154Z","shell.execute_reply":"2023-04-10T06:52:42.788140Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data.present.value_counts()","metadata":{"execution":{"iopub.status.busy":"2023-04-10T06:52:42.792141Z","iopub.execute_input":"2023-04-10T06:52:42.792566Z","iopub.status.idle":"2023-04-10T06:52:42.803115Z","shell.execute_reply.started":"2023-04-10T06:52:42.792534Z","shell.execute_reply":"2023-04-10T06:52:42.802407Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data.to_csv(\"data.csv\")","metadata":{"execution":{"iopub.status.busy":"2023-04-10T06:52:42.806642Z","iopub.execute_input":"2023-04-10T06:52:42.806920Z","iopub.status.idle":"2023-04-10T06:52:42.930659Z","shell.execute_reply.started":"2023-04-10T06:52:42.806895Z","shell.execute_reply":"2023-04-10T06:52:42.929973Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data.info()","metadata":{"execution":{"iopub.status.busy":"2023-04-10T06:52:42.932025Z","iopub.execute_input":"2023-04-10T06:52:42.932354Z","iopub.status.idle":"2023-04-10T06:52:42.945264Z","shell.execute_reply.started":"2023-04-10T06:52:42.932319Z","shell.execute_reply":"2023-04-10T06:52:42.944291Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data = data.dropna()\ndata.isna().value_counts()","metadata":{"execution":{"iopub.status.busy":"2023-04-10T06:52:42.946963Z","iopub.execute_input":"2023-04-10T06:52:42.947389Z","iopub.status.idle":"2023-04-10T06:52:42.965736Z","shell.execute_reply.started":"2023-04-10T06:52:42.947354Z","shell.execute_reply":"2023-04-10T06:52:42.964932Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\n\ntest_df, train_df = train_test_split(data, test_size=0.7, random_state=123)\n\ntest_df,val_df = train_test_split(test_df, test_size=0.5, random_state=123)","metadata":{"execution":{"iopub.status.busy":"2023-04-10T06:52:42.967399Z","iopub.execute_input":"2023-04-10T06:52:42.967750Z","iopub.status.idle":"2023-04-10T06:52:42.979999Z","shell.execute_reply.started":"2023-04-10T06:52:42.967715Z","shell.execute_reply":"2023-04-10T06:52:42.979050Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"val_df.isna().value_counts()","metadata":{"execution":{"iopub.status.busy":"2023-04-10T06:52:42.981159Z","iopub.execute_input":"2023-04-10T06:52:42.981748Z","iopub.status.idle":"2023-04-10T06:52:42.993752Z","shell.execute_reply.started":"2023-04-10T06:52:42.981713Z","shell.execute_reply":"2023-04-10T06:52:42.992989Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"Train DF\")\nprint(train_df.diagnosis.value_counts())\n\nprint(\"Test DF\")\nprint(test_df.diagnosis.value_counts())\n\nprint(\"Val DF\")\nprint(val_df.diagnosis.value_counts())","metadata":{"execution":{"iopub.status.busy":"2023-04-10T06:52:42.995193Z","iopub.execute_input":"2023-04-10T06:52:42.995541Z","iopub.status.idle":"2023-04-10T06:52:43.012311Z","shell.execute_reply.started":"2023-04-10T06:52:42.995509Z","shell.execute_reply":"2023-04-10T06:52:43.011280Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.diagnosis = train_df.diagnosis.astype(int)\ntest_df.diagnosis =  test_df.diagnosis.astype(int)\nval_df.diagnosis =  val_df.diagnosis.astype(int)\n\n\nprint(\"Train DF\")\nprint(train_df.diagnosis.value_counts())\n\nprint(\"Test DF\")\nprint(test_df.diagnosis.value_counts())\n\nprint(\"Val DF\")\nprint(val_df.diagnosis.value_counts())","metadata":{"execution":{"iopub.status.busy":"2023-04-10T06:52:43.013838Z","iopub.execute_input":"2023-04-10T06:52:43.014399Z","iopub.status.idle":"2023-04-10T06:52:43.032224Z","shell.execute_reply.started":"2023-04-10T06:52:43.014364Z","shell.execute_reply":"2023-04-10T06:52:43.031400Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"im_size = 224","metadata":{"execution":{"iopub.status.busy":"2023-04-10T06:52:43.033359Z","iopub.execute_input":"2023-04-10T06:52:43.033844Z","iopub.status.idle":"2023-04-10T06:52:43.037483Z","shell.execute_reply.started":"2023-04-10T06:52:43.033809Z","shell.execute_reply":"2023-04-10T06:52:43.036577Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# old_train = old_train[['image','level']]\n# old_train.columns = new_train.columns\n# old_train.diagnosis.value_counts()\n\n# # path columns\n# new_train['id_code'] = '../input/aptos2019-blindness-detection/train_images/' + new_train['id_code'].astype(str) + '.png'\n# old_train['id_code'] = '../input/diabetic-retinopathy-resized/resized_train/resized_train/' + old_train['id_code'].astype(str) + '.jpeg'\n\n","metadata":{"execution":{"iopub.status.busy":"2023-04-10T06:52:43.038764Z","iopub.execute_input":"2023-04-10T06:52:43.039273Z","iopub.status.idle":"2023-04-10T06:52:43.046717Z","shell.execute_reply.started":"2023-04-10T06:52:43.039238Z","shell.execute_reply":"2023-04-10T06:52:43.046152Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# # Let's shuffle the datasets\n# train_df = train_df.sample(frac=1).reset_index(drop=True)\n# val_df = val_df.sample(frac=1).reset_index(drop=True)\n# print(train_df.shape)\n# print(val_df.shape)","metadata":{"execution":{"iopub.status.busy":"2023-04-10T06:52:43.047870Z","iopub.execute_input":"2023-04-10T06:52:43.048460Z","iopub.status.idle":"2023-04-10T06:52:43.056059Z","shell.execute_reply.started":"2023-04-10T06:52:43.048425Z","shell.execute_reply":"2023-04-10T06:52:43.055267Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.head()","metadata":{"execution":{"iopub.status.busy":"2023-04-10T06:52:43.057431Z","iopub.execute_input":"2023-04-10T06:52:43.057980Z","iopub.status.idle":"2023-04-10T06:52:43.070713Z","shell.execute_reply.started":"2023-04-10T06:52:43.057945Z","shell.execute_reply":"2023-04-10T06:52:43.069891Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import cv2\ndef display_samples(data, columns=4, rows=3):\n    images = data[:columns*rows]\n    fig=plt.figure(figsize=(5*columns, 4*rows))\n\n    for i in range(len(images)):\n        image_path = images[i]\n#         print(image_path)\n        \n#         import os\n#         if os.path.isfile(image_path):\n#             print(\"Found\")\n#         else:\n#             print(\"Not found\")\n        \n        img = cv2.imread(f'{image_path}')\n        img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)\n        img = cv2.resize(img, (im_size,im_size))\n        img = cv2.addWeighted(img,4,cv2.GaussianBlur(img, (0,0), im_size/40) ,-4 ,128)\n        \n        fig.add_subplot(rows, columns, i+1)\n        plt.imshow(img)\n    \n    plt.tight_layout()\n\n# display sample images from the 'data' variable\ndisplay_samples(data['id_code'].tolist())\n","metadata":{"execution":{"iopub.status.busy":"2023-04-10T06:52:43.072048Z","iopub.execute_input":"2023-04-10T06:52:43.072622Z","iopub.status.idle":"2023-04-10T06:52:46.411647Z","shell.execute_reply.started":"2023-04-10T06:52:43.072588Z","shell.execute_reply":"2023-04-10T06:52:46.410867Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def crop_image1(img,tol=7):\n    # img is image data\n    # tol  is tolerance\n        \n    mask = img>tol\n    return img[np.ix_(mask.any(1),mask.any(0))]\n\ndef crop_image_from_gray(img,tol=7):\n    if img.ndim ==2:\n        mask = img>tol\n        return img[np.ix_(mask.any(1),mask.any(0))]\n    elif img.ndim==3:\n        gray_img = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)\n        mask = gray_img>tol\n        \n        check_shape = img[:,:,0][np.ix_(mask.any(1),mask.any(0))].shape[0]\n        if (check_shape == 0): # image is too dark so that we crop out everything,\n            return img # return original image\n        else:\n            img1=img[:,:,0][np.ix_(mask.any(1),mask.any(0))]\n            img2=img[:,:,1][np.ix_(mask.any(1),mask.any(0))]\n            img3=img[:,:,2][np.ix_(mask.any(1),mask.any(0))]\n            img = np.stack([img1,img2,img3],axis=-1)\n        return img\n\ndef preprocess_image(image_path, desired_size=224):\n    img = cv2.imread(image_path)\n    img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)\n    img = crop_image_from_gray(img)\n    img = cv2.resize(img, (desired_size,desired_size))\n    img = cv2.addWeighted(img,4,cv2.GaussianBlur(img, (0,0), desired_size/30) ,-4 ,128)\n    \n    return img\n\ndef preprocess_image_old(image_path, desired_size=224):\n    img = cv2.imread(image_path)\n    img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)\n    #img = crop_image_from_gray(img)\n    img = cv2.resize(img, (desired_size,desired_size))\n    img = cv2.addWeighted(img,4,cv2.GaussianBlur(img, (0,0), desired_size/40) ,-4 ,128)\n    \n    return img","metadata":{"execution":{"iopub.status.busy":"2023-04-10T06:52:46.412981Z","iopub.execute_input":"2023-04-10T06:52:46.413482Z","iopub.status.idle":"2023-04-10T06:52:46.429667Z","shell.execute_reply.started":"2023-04-10T06:52:46.413445Z","shell.execute_reply":"2023-04-10T06:52:46.428412Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# # validation set\n# N = val_df.shape[0]\n# x_val = np.empty((N, im_size, im_size, 3), dtype=np.uint8)\n\n# from tqdm import trange, tqdm\n\n# for i, image_id in enumerate(tqdm(val_df['id_code'])):\n#     x_val[i, :, :, :] = preprocess_image(\n#         f'{image_id}',\n#         desired_size = im_size\n#     )\n\nval_df","metadata":{"execution":{"iopub.status.busy":"2023-04-10T06:52:46.431397Z","iopub.execute_input":"2023-04-10T06:52:46.432007Z","iopub.status.idle":"2023-04-10T06:52:46.451375Z","shell.execute_reply.started":"2023-04-10T06:52:46.431972Z","shell.execute_reply":"2023-04-10T06:52:46.450707Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train = train_df['diagnosis'].values\ny_val = val_df['diagnosis'].values\n\n\n","metadata":{"execution":{"iopub.status.busy":"2023-04-10T06:52:46.452685Z","iopub.execute_input":"2023-04-10T06:52:46.453172Z","iopub.status.idle":"2023-04-10T06:52:46.457187Z","shell.execute_reply.started":"2023-04-10T06:52:46.453139Z","shell.execute_reply":"2023-04-10T06:52:46.456033Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"Train DF Columns\")\nprint(train_df.columns)\ntrain_df[\"diagnosis\"] = train_df[\"diagnosis\"].astype(\"str\")\n\nprint(\"Test DF Columns\")\nprint(test_df.columns)\ntest_df[\"diagnosis\"] = test_df[\"diagnosis\"].astype(\"str\")\n\nprint(\"Validate DF Columns\")\nprint(val_df.columns)\nval_df[\"diagnosis\"] = val_df[\"diagnosis\"].astype(\"str\")","metadata":{"execution":{"iopub.status.busy":"2023-04-10T06:52:46.459042Z","iopub.execute_input":"2023-04-10T06:52:46.459672Z","iopub.status.idle":"2023-04-10T06:52:46.503768Z","shell.execute_reply.started":"2023-04-10T06:52:46.459636Z","shell.execute_reply":"2023-04-10T06:52:46.501752Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"val_df","metadata":{"execution":{"iopub.status.busy":"2023-04-10T06:52:46.505079Z","iopub.execute_input":"2023-04-10T06:52:46.505411Z","iopub.status.idle":"2023-04-10T06:52:46.519372Z","shell.execute_reply.started":"2023-04-10T06:52:46.505373Z","shell.execute_reply":"2023-04-10T06:52:46.518570Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Visualization","metadata":{}},{"cell_type":"markdown","source":"# Load the Images with a generator<a class=\"anchor\" id=\"2\"></a>","metadata":{}},{"cell_type":"code","source":"train_df","metadata":{"execution":{"iopub.status.busy":"2023-04-10T06:52:46.520727Z","iopub.execute_input":"2023-04-10T06:52:46.521256Z","iopub.status.idle":"2023-04-10T06:52:46.534663Z","shell.execute_reply.started":"2023-04-10T06:52:46.521220Z","shell.execute_reply":"2023-04-10T06:52:46.533731Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def create_gen():\n    # Load the Images with a generator and Data Augmentation\n    train_generator = tf.keras.preprocessing.image.ImageDataGenerator(\n        preprocessing_function=tf.keras.applications.mobilenet_v2.preprocess_input,\n        validation_split=0.1\n    )\n\n    test_generator = tf.keras.preprocessing.image.ImageDataGenerator(\n        preprocessing_function=tf.keras.applications.mobilenet_v2.preprocess_input\n    )\n    \n    # compute the class frequencies\n    class_freq = train_df['diagnosis'].value_counts(normalize=True)\n    print(\"Class frequencies:\\n\", class_freq)\n\n    # compute the class weights\n    class_weights = {}\n    for i in range(len(class_freq)):\n        class_weights[i] = 1 / class_freq[i]\n\n    # print the class weights\n    print(\"Class weights:\\n\", class_weights)\n\n    train_images = train_generator.flow_from_dataframe(\n        dataframe=train_df,\n        x_col='id_code',\n        y_col='diagnosis',\n        target_size=(224, 224),\n        color_mode='rgb',\n        class_mode='categorical',\n        batch_size=16,\n        shuffle=True,\n        seed=0,\n        subset='training',\n        rotation_range=30, # Uncomment to use data augmentation\n        zoom_range=0.15,\n        width_shift_range=0.2,\n        height_shift_range=0.2,\n        shear_range=0.15,\n        horizontal_flip=True,\n        fill_mode=\"nearest\",\n        class_weights=class_weights\n    )\n\n    val_images = train_generator.flow_from_dataframe(\n        dataframe=test_df,\n        x_col='id_code',\n        y_col='diagnosis',\n        target_size=(224, 224),\n        color_mode='rgb',\n        class_mode='categorical',\n        batch_size=16,\n        shuffle=True,\n        seed=0,\n        subset='validation',\n        rotation_range=30, # Uncomment to use data augmentation\n        zoom_range=0.15,\n        width_shift_range=0.2,\n        height_shift_range=0.2,\n        shear_range=0.15,\n        horizontal_flip=True,\n        fill_mode=\"nearest\"\n    )\n\n    test_images = test_generator.flow_from_dataframe(\n        dataframe=test_df,\n        x_col='id_code',\n        y_col='diagnosis',\n        target_size=(224, 224),\n        color_mode='rgb',\n        class_mode='categorical',\n        batch_size=16,\n        shuffle=False\n    )\n    \n    return train_generator,test_generator,train_images,val_images,test_images","metadata":{"execution":{"iopub.status.busy":"2023-04-10T06:52:46.535977Z","iopub.execute_input":"2023-04-10T06:52:46.536503Z","iopub.status.idle":"2023-04-10T06:52:46.546397Z","shell.execute_reply.started":"2023-04-10T06:52:46.536469Z","shell.execute_reply":"2023-04-10T06:52:46.545743Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_model(model):\n# Load the pretained model\n    kwargs =    {'input_shape':(224, 224, 3),\n                'include_top':False,\n                'weights':'imagenet',\n                'pooling':'avg'}\n    \n    pretrained_model = model(**kwargs)\n    pretrained_model.trainable = False\n    \n    inputs = pretrained_model.input\n\n    x = tf.keras.layers.Dense(128, activation='relu')(pretrained_model.output)\n    x = tf.keras.layers.Dense(128, activation='relu')(x)\n\n    outputs = tf.keras.layers.Dense(5, activation='softmax')(x)\n\n    model = tf.keras.Model(inputs=inputs, outputs=outputs)\n    \n    opt = tf.keras.optimizers.Adam(learning_rate=0.001)\n\n    model.compile(\n        optimizer=opt,\n        loss='categorical_crossentropy',\n        metrics=['accuracy'],\n        \n    )\n    \n    return model","metadata":{"execution":{"iopub.status.busy":"2023-04-10T06:52:46.547696Z","iopub.execute_input":"2023-04-10T06:52:46.548250Z","iopub.status.idle":"2023-04-10T06:52:46.559339Z","shell.execute_reply.started":"2023-04-10T06:52:46.548213Z","shell.execute_reply":"2023-04-10T06:52:46.558564Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Dictionary with the models\nmodels = {\n\n    \"InceptionV3\": {\"model\":tf.keras.applications.InceptionV3, \"perf\":0},\n\n}\n\n# models = {\n#     \"DenseNet121\": {\"model\":tf.keras.applications.DenseNet121, \"perf\":0},\n#     \"DenseNet169\": {\"model\":tf.keras.applications.DenseNet169, \"perf\":0},\n#     \"InceptionResNetV2\": {\"model\":tf.keras.applications.InceptionResNetV2, \"perf\":0},\n#     \"InceptionV3\": {\"model\":tf.keras.applications.InceptionV3, \"perf\":0},\n#     \"MobileNet\": {\"model\":tf.keras.applications.MobileNet, \"perf\":0},\n#     \"MobileNetV2\": {\"model\":tf.keras.applications.MobileNetV2, \"perf\":0},\n#     \"ResNet101\": {\"model\":tf.keras.applications.ResNet101, \"perf\":0},\n#     \"ResNet50\": {\"model\":tf.keras.applications.ResNet50, \"perf\":0},\n#     \"VGG16\": {\"model\":tf.keras.applications.VGG16, \"perf\":0},\n#     \"VGG19\": {\"model\":tf.keras.applications.VGG19, \"perf\":0}\n# }\n\n\n# Create the generators\ntrain_generator,test_generator,train_images,val_images,test_images=create_gen()\nprint('\\n')\n\n# Fit the models\nfor name, model in models.items():\n    \n    # Get the model\n    m = get_model(model['model'])\n    models[name]['model'] = m\n    \n    start = perf_counter()\n    \n    # Fit the model\n    history = m.fit(train_images,validation_data=val_images,epochs=1,verbose=2)\n    \n    # Sav the duration, the train_accuracy and the val_accuracy\n    duration = perf_counter() - start\n    duration = round(duration,2)\n    models[name]['perf'] = duration\n    print(f\"{name:20} trained in {duration} sec\")\n    \n    val_acc = history.history['val_accuracy']\n    models[name]['val_acc'] = [round(v,4) for v in val_acc]\n    \n    train_acc = history.history['accuracy']\n    models[name]['train_accuracy'] = [round(v,4) for v in train_acc]","metadata":{"_kg_hide-output":true,"execution":{"iopub.status.busy":"2023-04-10T06:52:46.561782Z","iopub.execute_input":"2023-04-10T06:52:46.562371Z","iopub.status.idle":"2023-04-10T07:00:35.526273Z","shell.execute_reply.started":"2023-04-10T06:52:46.562335Z","shell.execute_reply":"2023-04-10T07:00:35.525543Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"Done\")","metadata":{"execution":{"iopub.status.busy":"2023-04-10T07:00:35.528673Z","iopub.execute_input":"2023-04-10T07:00:35.529036Z","iopub.status.idle":"2023-04-10T07:00:35.533527Z","shell.execute_reply.started":"2023-04-10T07:00:35.529005Z","shell.execute_reply":"2023-04-10T07:00:35.532679Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Create a DataFrame with the results\nmodels_result = []\n\nfor name, v in models.items():\n    models_result.append([ name, \n                          models[name]['train_accuracy'][-1],\n                          models[name]['val_acc'][-1], \n                          models[name]['perf']])\n    \ndf_results = pd.DataFrame(models_result, \n                          columns = ['model','train_accuracy','val_accuracy','Training time (sec)'])\ndf_results.sort_values(by='val_accuracy', ascending=False, inplace=True)\ndf_results.reset_index(inplace=True,drop=True)\ndf_results","metadata":{"execution":{"iopub.status.busy":"2023-04-10T07:00:35.534901Z","iopub.execute_input":"2023-04-10T07:00:35.535439Z","iopub.status.idle":"2023-04-10T07:00:35.553281Z","shell.execute_reply.started":"2023-04-10T07:00:35.535402Z","shell.execute_reply":"2023-04-10T07:00:35.552362Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize = (15,5))\nsns.barplot(x = 'model', y = 'train_accuracy', data = df_results)\nplt.title('Accuracy on the Training Set (after 1 epoch)', fontsize = 15)\nplt.ylim(0,1)\nplt.xticks(rotation=90)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-04-10T07:00:35.554542Z","iopub.execute_input":"2023-04-10T07:00:35.554865Z","iopub.status.idle":"2023-04-10T07:00:35.674222Z","shell.execute_reply.started":"2023-04-10T07:00:35.554838Z","shell.execute_reply":"2023-04-10T07:00:35.673416Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# InceptionV3<a class=\"anchor\" id=\"4\"></a>","metadata":{}},{"cell_type":"code","source":"# Load the pretained model\npretrained_model = tf.keras.applications.InceptionV3(\n    input_shape=(224, 224, 3),\n    include_top=False,\n    weights='imagenet',\n    pooling='avg'\n)\n\npretrained_model.trainable = False","metadata":{"execution":{"iopub.status.busy":"2023-04-10T07:00:35.675840Z","iopub.execute_input":"2023-04-10T07:00:35.676177Z","iopub.status.idle":"2023-04-10T07:00:37.500654Z","shell.execute_reply.started":"2023-04-10T07:00:35.676143Z","shell.execute_reply":"2023-04-10T07:00:37.499855Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"inputs = pretrained_model.input\n\nx = tf.keras.layers.Dense(128, activation='relu')(pretrained_model.output)\nx = tf.keras.layers.Dense(128, activation='relu')(x)\n\noutputs = tf.keras.layers.Dense(5, activation='softmax')(x)\n\nmodel = tf.keras.Model(inputs=inputs, outputs=outputs)\n\nopt = tf.keras.optimizers.Adam(learning_rate=0.001)\n\nmodel.compile(\n    optimizer= opt,\n    loss='categorical_crossentropy',\n    metrics=['accuracy','AUC']\n)\n\nhistory = model.fit(\n    train_images,\n    validation_data=val_images,\n    batch_size = 16,\n    epochs=25,\n    \n)\n\n# callbacks=[\n#         tf.keras.callbacks.EarlyStopping(\n#             monitor='val_loss',\n#             patience=2,\n#             restore_best_weights=True\n#         )\n#     ]","metadata":{"_kg_hide-output":true,"execution":{"iopub.status.busy":"2023-04-10T07:00:37.502574Z","iopub.execute_input":"2023-04-10T07:00:37.503101Z","iopub.status.idle":"2023-04-10T10:14:55.638894Z","shell.execute_reply.started":"2023-04-10T07:00:37.503060Z","shell.execute_reply":"2023-04-10T10:14:55.637990Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.save(\"new_model_with_class_weights.h5\")","metadata":{"execution":{"iopub.status.busy":"2023-04-10T10:19:31.274056Z","iopub.status.idle":"2023-04-10T10:19:31.274829Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pd.DataFrame(history.history)[['accuracy','val_accuracy']].plot()\nplt.title(\"Accuracy\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-04-10T10:19:50.897941Z","iopub.execute_input":"2023-04-10T10:19:50.898261Z","iopub.status.idle":"2023-04-10T10:19:51.030920Z","shell.execute_reply.started":"2023-04-10T10:19:50.898230Z","shell.execute_reply":"2023-04-10T10:19:51.030077Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pd.DataFrame(history.history)[['loss','val_loss']].plot()\nplt.title(\"Loss\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-04-10T10:19:58.067846Z","iopub.execute_input":"2023-04-10T10:19:58.068217Z","iopub.status.idle":"2023-04-10T10:19:58.244721Z","shell.execute_reply.started":"2023-04-10T10:19:58.068180Z","shell.execute_reply":"2023-04-10T10:19:58.243738Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"","metadata":{}},{"cell_type":"code","source":"results = model.evaluate(test_images, verbose=0)","metadata":{"execution":{"iopub.status.busy":"2023-04-10T10:20:16.674794Z","iopub.execute_input":"2023-04-10T10:20:16.675197Z","iopub.status.idle":"2023-04-10T10:22:11.769923Z","shell.execute_reply.started":"2023-04-10T10:20:16.675158Z","shell.execute_reply":"2023-04-10T10:22:11.769092Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"printmd(\" ## Test Loss: {:.5f}\".format(results[0]))\nprintmd(\"## Accuracy on the test set: {:.2f}%\".format(results[1] * 100))","metadata":{"execution":{"iopub.status.busy":"2023-04-10T10:23:00.288287Z","iopub.execute_input":"2023-04-10T10:23:00.288619Z","iopub.status.idle":"2023-04-10T10:23:00.298870Z","shell.execute_reply.started":"2023-04-10T10:23:00.288590Z","shell.execute_reply":"2023-04-10T10:23:00.297607Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Predict the label of the test_images\npred = model.predict(test_images)\npred = np.argmax(pred,axis=1)\n\n# Map the label\nlabels = (train_images.class_indices)\nlabels = dict((v,k) for k,v in labels.items())\npred = [labels[k] for k in pred]\n\n# Display the result\nprint(f'The first 5 predictions: {pred[:5]}')","metadata":{"execution":{"iopub.status.busy":"2023-04-10T10:23:03.877499Z","iopub.execute_input":"2023-04-10T10:23:03.877831Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import classification_report\ny_test = list(test_df.Label)\nfrom sklearn import metrics\nprint('Accuracy:', np.round(metrics.accuracy_score(y_test, pred),5))\nprint('Precision:', np.round(metrics.precision_score(y_test, pred, average='weighted'),5))\nprint('Recall:', np.round(metrics.recall_score(y_test,pred, average='weighted'),5))\nprint('F1 Score:', np.round(metrics.f1_score(y_test, pred, average='weighted'),5))\nprint('Cohen Kappa Score:', np.round(metrics.cohen_kappa_score(y_test, pred),5))\nprint(classification_report(y_test, pred))","metadata":{"execution":{"iopub.status.busy":"2023-04-10T10:19:31.227940Z","iopub.execute_input":"2023-04-10T10:19:31.228296Z","iopub.status.idle":"2023-04-10T10:19:31.256547Z","shell.execute_reply.started":"2023-04-10T10:19:31.228258Z","shell.execute_reply":"2023-04-10T10:19:31.254731Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}