{"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":"# Imports","metadata":{}},{"cell_type":"code","source":"import glob\nimport cv2\nimport os\n\nimport numpy as np \nimport pandas as pd \nimport tensorflow as tf\nimport matplotlib.pyplot as plt\nimport seaborn as sns\n\n\nfrom collections import Counter\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.preprocessing import LabelEncoder\nfrom sklearn.metrics import f1_score, roc_auc_score, cohen_kappa_score, precision_score, recall_score, accuracy_score, confusion_matrix\nfrom tensorflow.keras.utils import to_categorical\n\n%matplotlib inline","metadata":{"execution":{"iopub.execute_input":"2023-02-05T10:48:11.147184Z","iopub.status.busy":"2023-02-05T10:48:11.146873Z","iopub.status.idle":"2023-02-05T10:48:15.044226Z","shell.execute_reply":"2023-02-05T10:48:15.043106Z","shell.execute_reply.started":"2023-02-05T10:48:11.147151Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(os.listdir(\"../input/intel-mobileodt-cervical-cancer-screening\"))","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Data import","metadata":{}},{"cell_type":"code","source":"#getting the total number of images in the training set\n\nbase_dir = '../input/intel-mobileodt-cervical-cancer-screening'\n\ntrain_dir = os.path.join(base_dir,'train', 'train')\n\ntype1_dir = os.path.join(base_dir,'Type_1')\ntype2_dir = os.path.join(base_dir,'Type_2')\ntype3_dir = os.path.join(base_dir,'Type_3')\n\ntype1_files = glob.glob(type1_dir+'/*.jpg')\ntype2_files = glob.glob(type2_dir+'/*.jpg')\ntype3_files = glob.glob(type3_dir+'/*.jpg')\n\nadded_type1_files  =  glob.glob(os.path.join(base_dir, \"additional_Type_1_v2\", \"Type_1\")+'/*.jpg')\nadded_type2_files  =  glob.glob(os.path.join(base_dir, \"additional_Type_2_v2\", \"Type_2\")+'/*.jpg')\nadded_type3_files  =  glob.glob(os.path.join(base_dir, \"additional_Type_3_v2\", \"Type_3\")+'/*.jpg')\n\ntype1_files = type1_files + added_type1_files\ntype2_files = type2_files + added_type2_files\ntype3_files = type3_files + added_type3_files\n\n\nprint('Number of images in a train set of type 1: ', len(type1_files))\nprint('Number of images in a train set of type 2: ', len(type2_files))\nprint('Number of images in a train set of type 3: ', len(type3_files))\nprint('Total number of images in a train set: ', sum([len(type1_files), len(type2_files), len(type3_files)]))","metadata":{"execution":{"iopub.execute_input":"2023-02-05T10:48:15.048469Z","iopub.status.busy":"2023-02-05T10:48:15.047603Z","iopub.status.idle":"2023-02-05T10:48:15.081158Z","shell.execute_reply":"2023-02-05T10:48:15.080155Z","shell.execute_reply.started":"2023-02-05T10:48:15.048437Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Building a dataframe mapping images and Cancer type\n\nfiles_df = pd.DataFrame({\n    'filename': type1_files + type2_files + type3_files,\n    'label': ['Type_1'] * len(type1_files) + ['Type_2'] * len(type2_files) + ['Type_3'] * len(type3_files)\n})\n\nfiles_df","metadata":{"execution":{"iopub.execute_input":"2023-02-05T10:48:15.084525Z","iopub.status.busy":"2023-02-05T10:48:15.084193Z","iopub.status.idle":"2023-02-05T10:48:15.106838Z","shell.execute_reply":"2023-02-05T10:48:15.105657Z","shell.execute_reply.started":"2023-02-05T10:48:15.084496Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Shuffle data\n\nrandom_state = 42\n\nfiles_df = files_df.sample(frac=1, random_state=random_state)\n# files_df = files_df.sample(n=100, random_state=random_state)\n\nfiles_df","metadata":{"execution":{"iopub.execute_input":"2023-02-05T10:48:15.109191Z","iopub.status.busy":"2023-02-05T10:48:15.108775Z","iopub.status.idle":"2023-02-05T10:48:15.12476Z","shell.execute_reply":"2023-02-05T10:48:15.123655Z","shell.execute_reply.started":"2023-02-05T10:48:15.109152Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Data exploration","metadata":{}},{"cell_type":"code","source":"files_df.describe()","metadata":{"execution":{"iopub.execute_input":"2023-02-05T10:48:15.127006Z","iopub.status.busy":"2023-02-05T10:48:15.126617Z","iopub.status.idle":"2023-02-05T10:48:15.148228Z","shell.execute_reply":"2023-02-05T10:48:15.147256Z","shell.execute_reply.started":"2023-02-05T10:48:15.126959Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Check for duplicates\nlen(files_df[files_df.duplicated()])","metadata":{"execution":{"iopub.execute_input":"2023-02-05T10:48:15.150277Z","iopub.status.busy":"2023-02-05T10:48:15.149903Z","iopub.status.idle":"2023-02-05T10:48:15.161316Z","shell.execute_reply":"2023-02-05T10:48:15.160256Z","shell.execute_reply.started":"2023-02-05T10:48:15.150239Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Get count of each type \ntype_count = pd.DataFrame(files_df['label'].value_counts())\ntype_count","metadata":{"execution":{"iopub.execute_input":"2023-02-05T10:48:15.163696Z","iopub.status.busy":"2023-02-05T10:48:15.163037Z","iopub.status.idle":"2023-02-05T10:48:15.177097Z","shell.execute_reply":"2023-02-05T10:48:15.176201Z","shell.execute_reply.started":"2023-02-05T10:48:15.16366Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(list(type_count.columns)[0])","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Display barplot of type count\n\nplt.figure(figsize = (15, 6))\nsns.barplot(x= type_count[list(type_count.columns)[0]], y= type_count.index.to_list())\nplt.title('Cervical Cancer Type Distribution')\nplt.grid(True)\nplt.show()","metadata":{"execution":{"iopub.execute_input":"2023-02-05T10:48:15.178754Z","iopub.status.busy":"2023-02-05T10:48:15.178458Z","iopub.status.idle":"2023-02-05T10:48:15.397561Z","shell.execute_reply":"2023-02-05T10:48:15.396533Z","shell.execute_reply.started":"2023-02-05T10:48:15.178722Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Display sample images of types\nfor label in ('Type_1', 'Type_2', 'Type_3'):\n    filepaths = files_df[files_df['label']==label]['filename'].values[:5]\n    fig = plt.figure(figsize= (15, 6))\n    for i, path in enumerate(filepaths):\n        img = cv2.imread(path)\n        img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)\n        img = cv2.resize(img, (224, 224))\n        fig.add_subplot(1, 5, i+1)\n        plt.imshow(img)\n        plt.subplots_adjust(hspace=0.5)\n        plt.axis(False)\n        plt.title(label)","metadata":{"execution":{"iopub.execute_input":"2023-02-05T10:48:15.399575Z","iopub.status.busy":"2023-02-05T10:48:15.399218Z","iopub.status.idle":"2023-02-05T10:48:19.691573Z","shell.execute_reply":"2023-02-05T10:48:19.690522Z","shell.execute_reply.started":"2023-02-05T10:48:15.399537Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Data propocessing","metadata":{}},{"cell_type":"code","source":"# Split training,val and test set : 70:15:15\n\ntrain_files, test_files, train_labels, test_labels = train_test_split(files_df['filename'].values,\n                                                                      files_df['label'].values, \n                                                                      test_size=0.3, \n                                                                      random_state=random_state)\n\ntest_files, val_files, test_labels, val_labels = train_test_split(test_files,\n                                                                  test_labels, \n                                                                  test_size=0.5, \n                                                                  random_state=random_state)\n\n\nprint('Number of images in train set: ', train_files.shape)\nprint('Number of images in validation set: ', val_files.shape)\nprint('Number of images in test set: ', test_files.shape, '\\n')\n\nprint('Train:', Counter(train_labels), '\\nVal:', Counter(val_labels), '\\nTest:', Counter(test_labels))","metadata":{"execution":{"iopub.execute_input":"2023-02-05T10:48:19.694704Z","iopub.status.busy":"2023-02-05T10:48:19.694038Z","iopub.status.idle":"2023-02-05T10:48:19.707815Z","shell.execute_reply":"2023-02-05T10:48:19.706835Z","shell.execute_reply.started":"2023-02-05T10:48:19.694665Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def load_images(files, labels):\n    features = []\n    correct_labels = []\n    bad_images = 0\n    \n    for i in range(len(files)):\n        try:\n            img = cv2.imread(files[i])\n            resized_img = cv2.resize(img, (160, 160))\n            \n            features.append(np.array(resized_img))\n            correct_labels.append(labels[i])\n                   \n        except Exception as e:\n            bad_images+=1\n            print('Encoutered bad image')\n    print('Bad images ecountered:', bad_images)\n    return np.array(features), np.array(correct_labels)","metadata":{"execution":{"iopub.execute_input":"2023-02-05T10:48:19.710057Z","iopub.status.busy":"2023-02-05T10:48:19.70966Z","iopub.status.idle":"2023-02-05T10:48:19.71781Z","shell.execute_reply":"2023-02-05T10:48:19.71652Z","shell.execute_reply.started":"2023-02-05T10:48:19.710021Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Load training and evaluation data\ntrain_features, train_labels = load_images(train_files, train_labels)\nprint('Train images loaded')\n\nval_features, val_labels = load_images(val_files, val_labels)\nprint('Validation images loaded')\n\ntest_features, test_labels = load_images(test_files, test_labels)\nprint('test images loaded')","metadata":{"execution":{"iopub.execute_input":"2023-02-05T10:48:19.723951Z","iopub.status.busy":"2023-02-05T10:48:19.723609Z","iopub.status.idle":"2023-02-05T11:11:50.219834Z","shell.execute_reply":"2023-02-05T11:11:50.218665Z","shell.execute_reply.started":"2023-02-05T10:48:19.723921Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# check lengths of training and evaluation  sets\nlen(train_features), len(train_labels), len(val_features), len(val_labels), len(test_features), len(test_labels) ","metadata":{"execution":{"iopub.execute_input":"2023-02-05T11:11:50.221761Z","iopub.status.busy":"2023-02-05T11:11:50.221431Z","iopub.status.idle":"2023-02-05T11:11:50.233657Z","shell.execute_reply":"2023-02-05T11:11:50.232637Z","shell.execute_reply.started":"2023-02-05T11:11:50.221732Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"BATCH_SIZE = 32\nNUM_CLASSES = 3\nEPOCHS = 10\nINPUT_SHAPE = (160, 160, 3)","metadata":{"execution":{"iopub.execute_input":"2023-02-05T11:11:50.236036Z","iopub.status.busy":"2023-02-05T11:11:50.2354Z","iopub.status.idle":"2023-02-05T11:11:50.242068Z","shell.execute_reply":"2023-02-05T11:11:50.24105Z","shell.execute_reply.started":"2023-02-05T11:11:50.235997Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# encode train+val sets text categories with labels\nle = LabelEncoder()\nle.fit(train_labels)\n\ntrain_labels_enc = le.transform(train_labels)\nval_labels_enc = le.transform(val_labels)\n\ntrain_labels_1hotenc = tf.keras.utils.to_categorical(train_labels_enc, num_classes=NUM_CLASSES)\nval_labels_1hotenc = tf.keras.utils.to_categorical(val_labels_enc, num_classes=NUM_CLASSES)\n\nprint(train_labels[:6], train_labels_enc[:6])\nprint(train_labels[:6], train_labels_1hotenc[:6])","metadata":{"execution":{"iopub.execute_input":"2023-02-05T11:11:50.244177Z","iopub.status.busy":"2023-02-05T11:11:50.243549Z","iopub.status.idle":"2023-02-05T11:11:50.503258Z","shell.execute_reply":"2023-02-05T11:11:50.501925Z","shell.execute_reply.started":"2023-02-05T11:11:50.24414Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nle = LabelEncoder()\nle.fit(test_labels)\n\ntest_labels_enc = le.transform(test_labels)\n\ntest_labels_1hotenc = tf.keras.utils.to_categorical(test_labels_enc, num_classes=NUM_CLASSES)\n\n\nprint(test_labels[:6], test_labels_enc[:6])\nprint(test_labels[:6], test_labels_1hotenc[:6])","metadata":{"execution":{"iopub.execute_input":"2023-02-05T11:11:50.507885Z","iopub.status.busy":"2023-02-05T11:11:50.507493Z","iopub.status.idle":"2023-02-05T11:11:50.520206Z","shell.execute_reply":"2023-02-05T11:11:50.519067Z","shell.execute_reply.started":"2023-02-05T11:11:50.507849Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Data augmentation","metadata":{}},{"cell_type":"code","source":"data_augmentation = tf.keras.Sequential([\n  tf.keras.layers.RandomFlip('horizontal'),\n  tf.keras.layers.RandomRotation(0.2),\n])","metadata":{"execution":{"iopub.execute_input":"2023-02-05T11:11:50.523636Z","iopub.status.busy":"2023-02-05T11:11:50.523103Z","iopub.status.idle":"2023-02-05T11:11:52.199532Z","shell.execute_reply":"2023-02-05T11:11:52.197513Z","shell.execute_reply.started":"2023-02-05T11:11:50.523572Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(10, 10))\nfirst_image = train_features[0]\nfor i in range(9):\n    ax = plt.subplot(3, 3, i + 1)\n    augmented_image = data_augmentation(tf.expand_dims(first_image, 0))\n    plt.imshow(augmented_image[0] / 255)\n    plt.axis('off')\n        ","metadata":{"execution":{"iopub.execute_input":"2023-02-05T11:11:52.203458Z","iopub.status.busy":"2023-02-05T11:11:52.203144Z","iopub.status.idle":"2023-02-05T11:11:52.994942Z","shell.execute_reply":"2023-02-05T11:11:52.993683Z","shell.execute_reply.started":"2023-02-05T11:11:52.203428Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Metrics","metadata":{}},{"cell_type":"code","source":"def get_accuracy_metrics(model, train_features=train_features, train_labels=train_labels_enc, test_features=test_features, test_labels=test_labels_enc, val_features=val_features, val_labels=val_labels_enc):    \n    train_predicted = np.argmax(model.predict(train_features),axis=1)\n    test_predicted = np.argmax(model.predict(test_features),axis=1)\n    val_predicted = np.argmax(model.predict(val_features),axis=1)\n\n    print(\"Train accuracy Score------------>\")\n    print (\"{0:.3f}\".format(accuracy_score(train_labels, train_predicted) *100), \"%\")\n    \n    print(\"Val accuracy Score--------->\")\n    print(\"{0:.3f}\".format(accuracy_score(val_labels, val_predicted)*100), \"%\")\n    \n    print(\"Test accuracy Score--------->\")\n    print(\"{0:.3f}\".format(accuracy_score(test_labels, test_predicted)*100), \"%\")\n    \n    print(\"F1 Score--------------->\")\n    print(\"{0:.3f}\".format(f1_score(test_labels, test_predicted, average = 'weighted')*100), \"%\")\n    \n    print(\"Cohen Kappa Score------------->\")\n    print(\"{0:.3f}\".format(cohen_kappa_score(test_labels, test_predicted)*100), \"%\")\n    \n    \n    print(\"ROC AUC Score------------->\")\n    print(\"{0:.3f}\".format(roc_auc_score(to_categorical(test_labels, num_classes = 3), test_predicted.reshape(-1, 1), multi_class='ovr')*100), \"%\")\n    \n    print(\"Recall-------------->\")\n    print(\"{0:.3f}\".format(recall_score(test_labels, test_predicted, average = 'weighted')*100), \"%\")\n    \n    print(\"Precision-------------->\")\n    print(\"{0:.3f}\".format(precision_score(test_labels, test_predicted, average = 'weighted')*100), \"%\")\n    \n    cf_matrix_test = confusion_matrix(test_labels, test_predicted)\n    cf_matrix_val = confusion_matrix(val_labels, val_predicted)\n    \n    plt.figure(figsize = (12, 6))\n    plt.subplot(121)\n    sns.heatmap(cf_matrix_val, annot=True, cmap='Blues')\n    plt.title(\"Val Confusion matrix\")\n    \n    plt.subplot(122)\n    sns.heatmap(cf_matrix_test, annot=True, cmap='Blues')\n    plt.title(\"Test Confusion matrix\")\n    \n    plt.show()","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# General Model Fit","metadata":{}},{"cell_type":"code","source":"def learning_performance_chart(title, history):\n    #plots a chart showing the change in accuracy and loss function over epochs\n    f, (ax1, ax2) = plt.subplots(1, 2, figsize=(12, 4))\n    t = f.suptitle(title, fontsize=12)\n    f.subplots_adjust(top=0.85, wspace=0.3)\n\n    max_epoch = len(history.history['accuracy'])+1\n    epoch_list = list(range(1,max_epoch))\n    ax1.plot(epoch_list, history.history['accuracy'], label='Train Accuracy')\n    ax1.plot(epoch_list, history.history['val_accuracy'], label='Validation Accuracy')\n    ax1.set_xticks(np.arange(1, max_epoch, 5))\n    ax1.set_ylabel('Accuracy Value')\n    ax1.set_xlabel('Epoch')\n    ax1.set_title('Accuracy')\n    l1 = ax1.legend(loc=\"best\")\n\n    ax2.plot(epoch_list, history.history['loss'], label='Train Loss')\n    ax2.plot(epoch_list, history.history['val_loss'], label='Validation Loss')\n    ax2.set_xticks(np.arange(1, max_epoch, 5))\n    ax2.set_ylabel('Loss Value')\n    ax2.set_xlabel('Epoch')\n    ax2.set_title('Loss')\n    l2 = ax2.legend(loc=\"best\")\n\n    ","metadata":{"execution":{"iopub.execute_input":"2023-02-05T11:13:10.305578Z","iopub.status.busy":"2023-02-05T11:13:10.305201Z","iopub.status.idle":"2023-02-05T11:13:10.316018Z","shell.execute_reply":"2023-02-05T11:13:10.314139Z","shell.execute_reply.started":"2023-02-05T11:13:10.30554Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def fit_model(model_name, base_model, train_features, train_labels, validate_it,training = False, epochs = EPOCHS, batch_size= BATCH_SIZE):\n    \n    inputs = tf.keras.Input(shape=INPUT_SHAPE)\n    \n    x = data_augmentation(inputs)\n    x = base_model(x, training=training)\n    \n    if not model_name.startswith('CNN'):\n        x = tf.keras.layers.GlobalAveragePooling2D()(x)\n    \n    x = tf.keras.layers.Dropout(0.2)(x)\n    \n    outputs = tf.keras.layers.Dense(3, activation='softmax')(x)\n    \n    model = tf.keras.Model(inputs, outputs)\n    \n    es = tf.keras.callbacks.EarlyStopping(monitor='val_loss', mode='min', verbose=1, patience=5)\n    \n    model.compile(loss='categorical_crossentropy', optimizer ='adam', metrics=['accuracy'])\n    \n    print(\"Model Summary.\")\n    \n    print(model.summary())\n    \n    history = model.fit(x=train_features,y=train_labels ,validation_data=validate_it, epochs=epochs, batch_size=batch_size, verbose=1, callbacks=[es])\n\n    learning_performance_chart(title=\"{} learning performance.\".format(model_name), history=history)\n    \n    return model","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# CNN2","metadata":{}},{"cell_type":"code","source":"model = tf.keras.Sequential([\n tf.keras.layers.BatchNormalization(),\n tf.keras.layers.Conv2D(filters = 64, kernel_size = (3, 3), padding = 'same', activation='relu'),\n tf.keras.layers.BatchNormalization(),\n tf.keras.layers.Conv2D(filters = 128, kernel_size = (3, 3), padding = 'same', activation='relu'),\n tf.keras.layers.Flatten(),\n])\n\ncnn2 = fit_model(\"CNN2\", model, train_features, train_labels_1hotenc, (val_features, val_labels_1hotenc), training=True)\n","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('CNN2 performance on the test set:')\nget_accuracy_metrics(cnn2)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# CNN3","metadata":{}},{"cell_type":"code","source":"model = tf.keras.Sequential([\n    tf.keras.layers.BatchNormalization(),\n    tf.keras.layers.Conv2D(filters = 64, kernel_size = (3, 3), padding = 'same', activation='relu'),\n    tf.keras.layers.BatchNormalization(),\n    tf.keras.layers.Conv2D(filters = 128, kernel_size = (3, 3), padding = 'same', activation='relu'),\n    tf.keras.layers.MaxPooling2D(),\n    tf.keras.layers.Dropout(0.2),\n    tf.keras.layers.Conv2D(filters = 128, kernel_size = (3, 3), padding = 'same', activation='relu'),\n    tf.keras.layers.Flatten(),\n    ])\n\ncnn3 = fit_model(\"CNN3\", model, train_features, train_labels_1hotenc, (val_features, val_labels_1hotenc), training=True)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('CNN3 performance on the test set:')\nget_accuracy_metrics(cnn3)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# CNN4","metadata":{}},{"cell_type":"code","source":"model = tf.keras.Sequential([\ntf.keras.layers.BatchNormalization(),\ntf.keras.layers.Conv2D(filters = 64, kernel_size = (3, 3), padding = 'same', activation='relu'),\ntf.keras.layers.BatchNormalization(),\ntf.keras.layers.Conv2D(filters = 128, kernel_size = (3, 3), padding = 'same', activation='relu'),\ntf.keras.layers.MaxPooling2D(),\ntf.keras.layers.Dropout(0.2),\ntf.keras.layers.Conv2D(filters = 128, kernel_size = (3, 3), padding = 'same', activation='relu'),\ntf.keras.layers.MaxPooling2D(),\ntf.keras.layers.Dropout(0.2),\ntf.keras.layers.Conv2D(filters = 64, kernel_size = (3, 3), padding = 'same', activation='relu'),\ntf.keras.layers.Flatten(),\n])\n\ncnn4 = fit_model(\"CNN4\", model, train_features, train_labels_1hotenc, (val_features, val_labels_1hotenc), training=True)\n","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('CNN4 performance on the test set:')\nget_accuracy_metrics(cnn4)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# CNN5","metadata":{}},{"cell_type":"code","source":"model = tf.keras.Sequential([\n    tf.keras.layers.BatchNormalization(),\n    tf.keras.layers.Conv2D(filters=64, kernel_size=(\n        3, 3), padding='same', activation='relu'),\n    tf.keras.layers.BatchNormalization(),\n    tf.keras.layers.Conv2D(filters=128, kernel_size=(\n        3, 3), padding='same', activation='relu'),\n    tf.keras.layers.MaxPooling2D(),\n    tf.keras.layers.Dropout(0.2),\n    tf.keras.layers.Conv2D(filters=128, kernel_size=(3, 3), padding='same', activation='relu'),\n    tf.keras.layers.MaxPooling2D(),\n    tf.keras.layers.Dropout(0.2),\n    tf.keras.layers.Conv2D(filters=64, kernel_size=(\n        3, 3), padding='same', activation='relu'),\n    tf.keras.layers.MaxPooling2D(),\n    tf.keras.layers.Dropout(0.2),\n    tf.keras.layers.Conv2D(filters=32, kernel_size=(\n        3, 3), padding='same', activation='relu'),\n    tf.keras.layers.Flatten(),\n])\n\n\ncnn5 = fit_model(\"CNN5\", model, train_features, train_labels_1hotenc,\n                       (val_features, val_labels_1hotenc), training=True)\n","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('CNN5 performance on the test set:')\nget_accuracy_metrics(cnn5)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# CNN6","metadata":{}},{"cell_type":"code","source":"model = tf.keras.Sequential([\n    tf.keras.layers.BatchNormalization(),\n    tf.keras.layers.Conv2D(filters = 64, kernel_size = (3, 3), padding = 'same', activation='relu'),\n    tf.keras.layers.BatchNormalization(),\n    tf.keras.layers.Conv2D(filters = 128, kernel_size = (3, 3), padding = 'same', activation='relu'),\n    tf.keras.layers.MaxPooling2D(),\n    tf.keras.layers.Dropout(0.2),\n    tf.keras.layers.Conv2D(filters = 128, kernel_size = (3, 3), padding = 'same', activation='relu'),\n    tf.keras.layers.MaxPooling2D(),\n    tf.keras.layers.Dropout(0.2),\n    tf.keras.layers.Conv2D(filters = 64, kernel_size = (3, 3), padding = 'same', activation='relu'),\n    tf.keras.layers.MaxPooling2D(),\n    tf.keras.layers.Dropout(0.2),\n    tf.keras.layers.Conv2D(filters = 32, kernel_size = (3, 3), padding = 'same', activation='relu'),\n    tf.keras.layers.Conv2D(filters = 32, kernel_size = (3, 3), padding = 'same', activation='relu'),\n    tf.keras.layers.Flatten(),\n])\n\ncnn6 = fit_model(\"CNN6\", model, train_features, train_labels_1hotenc, (val_features, val_labels_1hotenc), training=True)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('CNN6 performance on the test set:')\nget_accuracy_metrics(cnn6)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# CNN7","metadata":{}},{"cell_type":"code","source":"model = tf.keras.Sequential([\n    tf.keras.layers.BatchNormalization(),\n    tf.keras.layers.Conv2D(filters = 64, kernel_size = (3, 3), padding = 'same', activation='relu'),\n    tf.keras.layers.BatchNormalization(),\n    tf.keras.layers.Conv2D(filters = 128, kernel_size = (3, 3), padding = 'same', activation='relu'),\n    tf.keras.layers.MaxPooling2D(),\n    tf.keras.layers.Dropout(0.2),\n    tf.keras.layers.Conv2D(filters = 128, kernel_size = (3, 3), padding = 'same', activation='relu'),\n    tf.keras.layers.MaxPooling2D(),\n    tf.keras.layers.Dropout(0.2),\n    tf.keras.layers.Conv2D(filters = 64, kernel_size = (3, 3), padding = 'same', activation='relu'),\n    tf.keras.layers.MaxPooling2D(),\n    tf.keras.layers.Dropout(0.2),\n    tf.keras.layers.Conv2D(filters = 32, kernel_size = (3, 3), padding = 'same', activation='relu'),\n    tf.keras.layers.Conv2D(filters = 32, kernel_size = (3, 3), padding = 'same', activation='relu'),\n    tf.keras.layers.MaxPooling2D(),\n    tf.keras.layers.Conv2D(filters = 16, kernel_size = (3, 3), padding = 'same', activation='relu'),\n    tf.keras.layers.Flatten(),\n])\n\ncnn7 = fit_model(\"CNN7\", model, train_features, train_labels_1hotenc, (val_features, val_labels_1hotenc), training=True)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('CNN7 performance on the test set:')\nget_accuracy_metrics(cnn7)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# CNN8","metadata":{}},{"cell_type":"code","source":"model = tf.keras.Sequential([\n    tf.keras.layers.BatchNormalization(),\n    tf.keras.layers.Conv2D(filters = 64, kernel_size = (3, 3), padding = 'same', activation='relu'),\n    tf.keras.layers.BatchNormalization(),\n    tf.keras.layers.Conv2D(filters = 128, kernel_size = (3, 3), padding = 'same', activation='relu'),\n    tf.keras.layers.MaxPooling2D(),\n    tf.keras.layers.Dropout(0.2),\n    tf.keras.layers.Conv2D(filters = 128, kernel_size = (3, 3), padding = 'same', activation='relu'),\n    tf.keras.layers.MaxPooling2D(),\n    tf.keras.layers.Dropout(0.2),\n    tf.keras.layers.Conv2D(filters = 64, kernel_size = (3, 3), padding = 'same', activation='relu'),\n    tf.keras.layers.MaxPooling2D(),\n    tf.keras.layers.Dropout(0.2),\n    tf.keras.layers.Conv2D(filters = 32, kernel_size = (3, 3), padding = 'same', activation='relu'),\n    tf.keras.layers.Conv2D(filters = 32, kernel_size = (3, 3), padding = 'same', activation='relu'),\n    tf.keras.layers.MaxPooling2D(),\n    tf.keras.layers.Conv2D(filters = 16, kernel_size = (3, 3), padding = 'same', activation='relu'),\n    tf.keras.layers.Conv2D(filters = 16, kernel_size = (3, 3), padding = 'same', activation='relu'),\n    tf.keras.layers.Flatten(),\n])\n\ncnn8 = fit_model(\"CNN8\", model, train_features, train_labels_1hotenc, (val_features, val_labels_1hotenc), training=True)\n\n","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('CNN8 performance on the test set:')\nget_accuracy_metrics(cnn8)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# ResNet 50","metadata":{}},{"cell_type":"code","source":"base_model = tf.keras.applications.ResNet50(\n    include_top=False,\n    weights=\"imagenet\",\n    classes=NUM_CLASSES,\n)\n\nbase_model.trainable = False\n\nresnet50 = fit_model(\"ResNet50\", base_model, train_features, train_labels_1hotenc, (val_features, val_labels_1hotenc))\n","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('ResNet50 performance on the test set:')\nget_accuracy_metrics(resnet50)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# VGG16","metadata":{}},{"cell_type":"code","source":"base_model = tf.keras.applications.VGG16(\n    include_top=False,\n    weights=\"imagenet\",\n    input_shape=INPUT_SHAPE,\n    classes=NUM_CLASSES,\n    classifier_activation=\"softmax\",\n)\n\nbase_model.trainable = False","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"vgg16 = fit_model(\"VGG16\", base_model, train_features, train_labels_1hotenc, (val_features, val_labels_1hotenc))","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('VGG16 performance on the test set:')\nget_accuracy_metrics(vgg16)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# VGG19 ","metadata":{}},{"cell_type":"code","source":"base_model = tf.keras.applications.VGG19(\n    include_top=False,\n    weights=\"imagenet\",\n    input_shape=INPUT_SHAPE,\n    classes=NUM_CLASSES,\n    classifier_activation=\"softmax\",\n)\n\nbase_model.trainable = False","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"vgg19 = fit_model(\"VGG19\", base_model, train_features, train_labels_1hotenc, (val_features, val_labels_1hotenc))","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('VGG19 performance on the test set:')\nget_accuracy_metrics(vgg19)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# MobileNet Pretranined","metadata":{}},{"cell_type":"code","source":"base_model = tf.keras.applications.MobileNet(include_top=False, \n                                               weights='imagenet', \n                                               input_shape=INPUT_SHAPE)\n\nbase_model.trainable = False","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mobilenet = fit_model(\"MobileNet\", base_model, train_features, train_labels_1hotenc, (val_features, val_labels_1hotenc))","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('MobileNet performance on the test set:')\nget_accuracy_metrics(mobilenet)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# MobileNet V2 pre-trained","metadata":{}},{"cell_type":"code","source":"base_model = tf.keras.applications.MobileNetV2(include_top=False, \n                                               weights='imagenet', \n                                               input_shape=INPUT_SHAPE)\n\nbase_model.trainable = False","metadata":{"execution":{"iopub.execute_input":"2023-02-05T11:11:52.997304Z","iopub.status.busy":"2023-02-05T11:11:52.996752Z","iopub.status.idle":"2023-02-05T11:11:54.230942Z","shell.execute_reply":"2023-02-05T11:11:54.229864Z","shell.execute_reply.started":"2023-02-05T11:11:52.997235Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mobilenetv2 = fit_model(\"MobileNetV2\", base_model, train_features, train_labels_1hotenc, (val_features, val_labels_1hotenc))","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('MobileNetV2 performance on the test set:')\nget_accuracy_metrics(mobilenetv2)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Inception V3","metadata":{}},{"cell_type":"code","source":"base_model = tf.keras.applications.InceptionV3(\n    include_top=False,\n    weights=\"imagenet\",\n    input_shape=INPUT_SHAPE,\n    classes=NUM_CLASSES,\n    classifier_activation=\"softmax\",\n)\n\nbase_model.trainable = False\n\nincpetionv3 = fit_model(\"InceptionV3\", base_model, train_features, train_labels_1hotenc, (val_features, val_labels_1hotenc))\n","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('InceptionV3 performance on the test set:')\nget_accuracy_metrics(incpetionv3)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# DenseNet 121","metadata":{}},{"cell_type":"code","source":"base_model = tf.keras.applications.DenseNet121(\n    include_top=False,\n    weights=\"imagenet\",\n    input_shape=INPUT_SHAPE,\n    classes=NUM_CLASSES,\n    classifier_activation=\"softmax\",\n)\n\nbase_model.trainable = False\n\ndensenet121 = fit_model(\"DenseNet121\", base_model, train_features, train_labels_1hotenc, (val_features, val_labels_1hotenc))\n","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('DenseNet121 performance on the test set:')\nget_accuracy_metrics(densenet121)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# DenseNet 169","metadata":{}},{"cell_type":"code","source":"base_model = tf.keras.applications.DenseNet169(\n    include_top=False,\n    weights=\"imagenet\",\n    input_shape=INPUT_SHAPE,\n    classes=NUM_CLASSES,\n    classifier_activation=\"softmax\",\n)\n\nbase_model.trainable = False\n\ndensenet169 = fit_model(\"DenseNet169\", base_model, train_features, train_labels_1hotenc, (val_features, val_labels_1hotenc))\n","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nprint('DenseNet169 performance on the test set:')\nget_accuracy_metrics(densenet169)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# InceptionResNetv2","metadata":{}},{"cell_type":"code","source":"base_model = tf.keras.applications.InceptionResNetV2(\n    include_top=False,\n    weights=\"imagenet\",\n    input_shape=INPUT_SHAPE,\n    classes=NUM_CLASSES,\n    classifier_activation=\"softmax\",\n)\n\nbase_model.trainable = False\n\ninceptionresnetv2 = fit_model(\"InceptionResNetV2\", base_model, train_features, train_labels_1hotenc, (val_features, val_labels_1hotenc))\n","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('InceptionResNetV2 performance on the test set:')\nget_accuracy_metrics(inceptionresnetv2)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# ResNet101","metadata":{}},{"cell_type":"code","source":"base_model = tf.keras.applications.ResNet101(\n    include_top=False,\n    weights=\"imagenet\",\n    input_shape=INPUT_SHAPE,\n    classes=NUM_CLASSES,\n    classifier_activation=\"softmax\",\n)\n\nbase_model.trainable = False\n\nresnet101 = fit_model(\"ResNet101\", base_model, train_features, train_labels_1hotenc, (val_features, val_labels_1hotenc))\n","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('ResNet101 performance on the test set:')\nget_accuracy_metrics(resnet101)","metadata":{},"execution_count":null,"outputs":[]}]}