{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.6.6","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"gpu","dataSources":[{"sourceId":14774,"databundleVersionId":875431,"sourceType":"competition"},{"sourceId":1878760,"sourceType":"datasetVersion","datasetId":313412},{"sourceId":2220078,"sourceType":"datasetVersion","datasetId":1172878}],"isInternetEnabled":false,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"### Abstract\nWe consider a starter code for beginner of this dataset. There is a unbalanced distribution of the classes. To overcome this drawback we want to add images by modifying the given images. We use the following geometric transformations:\n* vertical flip,\n* rotation,\n* perspective transformation (zoom).\n\nAfter that we select randomly images by the same number of images of every class. \n\nIn consideration of the medical fact that there exists a course of disease we use multi-labels instead of single-labels. That means we set\n\n| diagnosis | single-label |multi-label |\n|---| ---| ---|\n| 0 | 0 | 0 |\n| 1 | 1 | 0, 1|\n| 2 | 2 | 0, 1, 2|\n| 3 | 3 | 0, 1, 2, 3|\n| 4 | 4 | 0, 1, 2, 3, 4|\n\nWe trained the model by using a pretrained model. ","metadata":{}},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport random\n\nimport os\npath_in = \"../input/aptos2019-blindness-detection/\"\nprint(os.listdir(path_in))\nprint(os.listdir('../input/models'))","metadata":{"execution":{"iopub.status.busy":"2024-04-20T07:36:15.326015Z","iopub.execute_input":"2024-04-20T07:36:15.326346Z","iopub.status.idle":"2024-04-20T07:36:15.568945Z","shell.execute_reply.started":"2024-04-20T07:36:15.326277Z","shell.execute_reply":"2024-04-20T07:36:15.568211Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import cv2\nimport matplotlib.pyplot as plt","metadata":{"execution":{"iopub.status.busy":"2024-04-20T07:36:15.570665Z","iopub.execute_input":"2024-04-20T07:36:15.570886Z","iopub.status.idle":"2024-04-20T07:36:15.667452Z","shell.execute_reply.started":"2024-04-20T07:36:15.570849Z","shell.execute_reply":"2024-04-20T07:36:15.666823Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import warnings\nwarnings.filterwarnings(\"ignore\")","metadata":{"execution":{"iopub.status.busy":"2024-04-20T07:36:15.66945Z","iopub.execute_input":"2024-04-20T07:36:15.669786Z","iopub.status.idle":"2024-04-20T07:36:15.674371Z","shell.execute_reply.started":"2024-04-20T07:36:15.669727Z","shell.execute_reply":"2024-04-20T07:36:15.673429Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\nfrom sklearn.preprocessing import MultiLabelBinarizer","metadata":{"execution":{"iopub.status.busy":"2024-04-20T07:36:15.789345Z","iopub.execute_input":"2024-04-20T07:36:15.789777Z","iopub.status.idle":"2024-04-20T07:36:16.863208Z","shell.execute_reply.started":"2024-04-20T07:36:15.789693Z","shell.execute_reply":"2024-04-20T07:36:16.862367Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.utils import to_categorical\nfrom keras.models import Sequential\nfrom keras.layers import Dense, Dropout, Flatten, Conv2D, MaxPool2D, Activation, GlobalAveragePooling2D\nfrom keras.optimizers import RMSprop,Adam\nfrom keras.applications import VGG19, DenseNet121, InceptionV3\nfrom keras.callbacks import EarlyStopping","metadata":{"execution":{"iopub.status.busy":"2024-04-20T10:27:06.393687Z","iopub.execute_input":"2024-04-20T10:27:06.394029Z","iopub.status.idle":"2024-04-20T10:27:06.400078Z","shell.execute_reply.started":"2024-04-20T10:27:06.393969Z","shell.execute_reply":"2024-04-20T10:27:06.398952Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Define some parameters","metadata":{}},{"cell_type":"code","source":"q_size = 150\nimg_channel = 3\nnum_classes = 5","metadata":{"execution":{"iopub.status.busy":"2024-04-20T07:36:18.477769Z","iopub.execute_input":"2024-04-20T07:36:18.478116Z","iopub.status.idle":"2024-04-20T07:36:18.484182Z","shell.execute_reply.started":"2024-04-20T07:36:18.478049Z","shell.execute_reply":"2024-04-20T07:36:18.482207Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Read the input csv files","metadata":{}},{"cell_type":"code","source":"train_data = pd.read_csv(path_in+'train.csv')\ntest_data = pd.read_csv(path_in+'test.csv')\nsub_org = pd.read_csv(path_in+'sample_submission.csv')","metadata":{"execution":{"iopub.status.busy":"2024-04-20T07:36:18.485715Z","iopub.execute_input":"2024-04-20T07:36:18.48603Z","iopub.status.idle":"2024-04-20T07:36:18.533375Z","shell.execute_reply.started":"2024-04-20T07:36:18.485966Z","shell.execute_reply":"2024-04-20T07:36:18.532467Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Define some functions","metadata":{}},{"cell_type":"code","source":"def plot_bar(data):\n    \"\"\"Simple function to plot the distribution of the classes.\"\"\"\n    dict_data = dict(zip(range(0, num_classes), (((data.value_counts()).sort_index())).tolist()))\n    names = list(dict_data.keys())\n    values = list(dict_data.values())\n    plt.bar(names, values)\n    plt.grid()\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2024-04-20T07:36:18.535713Z","iopub.execute_input":"2024-04-20T07:36:18.536006Z","iopub.status.idle":"2024-04-20T07:36:18.548436Z","shell.execute_reply.started":"2024-04-20T07:36:18.535949Z","shell.execute_reply":"2024-04-20T07:36:18.547663Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def read_images(filepath, data, file_list, size):\n    \"\"\"Read and edit the images of a given folder.\"\"\"\n    for file in file_list:\n        img = cv2.imread(filepath+file+'.png')\n        img = cv2.resize(img, (size, size))\n        img = cv2.addWeighted(img, 4, cv2.GaussianBlur(img, (0, 0), 10), -4, 128)\n        data[file_list.index(file), :, :, :] = img","metadata":{"execution":{"iopub.status.busy":"2024-04-20T07:36:18.550116Z","iopub.execute_input":"2024-04-20T07:36:18.550765Z","iopub.status.idle":"2024-04-20T07:36:18.55894Z","shell.execute_reply.started":"2024-04-20T07:36:18.550711Z","shell.execute_reply":"2024-04-20T07:36:18.557864Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def add_flip_image(data, file_list):\n    \"\"\"Simple function to flip images by a given list.\"\"\"\n    temp = np.empty((1, data.shape[1], data.shape[2], data.shape[3]), dtype=np.uint8)\n    for index in file_list.index:\n        img = data[index, :, :, :]\n        vertical_img = cv2.flip(img, 1)\n        temp[0, :, :, :] = vertical_img\n        data = np.concatenate((data, temp), axis=0)\n    return data","metadata":{"execution":{"iopub.status.busy":"2024-04-20T07:36:18.560506Z","iopub.execute_input":"2024-04-20T07:36:18.56092Z","iopub.status.idle":"2024-04-20T07:36:18.572047Z","shell.execute_reply.started":"2024-04-20T07:36:18.560755Z","shell.execute_reply":"2024-04-20T07:36:18.571376Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def add_rot_image(data, file_list):\n    \"\"\"Simple function to rotate images by a given list.\"\"\"\n    degrees = 15\n    temp = np.empty((1, data.shape[1], data.shape[2], data.shape[3]), dtype=np.uint8)\n    for index in file_list.index:\n        img = data[index, :, :, :]\n        rows,cols, channel = img.shape\n        Matrix = cv2.getRotationMatrix2D((cols/2,rows/2), degrees, 1)\n        rotate_img = cv2.warpAffine(img, Matrix, (cols, rows))\n        temp[0, :, :, :] = rotate_img\n        data = np.concatenate((data, temp), axis=0)\n    return data","metadata":{"execution":{"iopub.status.busy":"2024-04-20T07:36:18.57338Z","iopub.execute_input":"2024-04-20T07:36:18.57373Z","iopub.status.idle":"2024-04-20T07:36:18.586652Z","shell.execute_reply.started":"2024-04-20T07:36:18.573675Z","shell.execute_reply":"2024-04-20T07:36:18.585947Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def add_zoom_image(data, file_list):\n    \"\"\"Simple function to zoom in images by a given list.\"\"\"\n    temp = np.empty((1, data.shape[1], data.shape[2], data.shape[3]), dtype=np.uint8)\n    for index in file_list.index:\n        img = data[index, :, :, :]\n        size = img.shape[0]\n        pts1 = np.float32([[10,10],[size-10, 10],[10, size-10],[size-10, size-10]])\n        pts2 = np.float32([[0, 0],[size-20, 0],[0, size-20],[size-20, size-20]])\n        Matrix = cv2.getPerspectiveTransform(pts1, pts2)\n        # zoom image\n        img_zoom = cv2.warpPerspective(img, Matrix, (size-20, size-20))\n        dim = img.shape \n        # resize image\n        rows,cols, channel = img.shape\n        dim=(rows, cols)\n        img_scale = cv2.resize(img_zoom, dim, interpolation = cv2.INTER_AREA)\n        temp[0, :, :, :] = img_scale\n        data = np.concatenate((data, temp), axis=0)\n    return data","metadata":{"execution":{"iopub.status.busy":"2024-04-20T07:36:18.587879Z","iopub.execute_input":"2024-04-20T07:36:18.588136Z","iopub.status.idle":"2024-04-20T07:36:18.600901Z","shell.execute_reply.started":"2024-04-20T07:36:18.588089Z","shell.execute_reply":"2024-04-20T07:36:18.599987Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_multilabel(diagnosis):\n    \"\"\"A function to get multi-label from single-label.\"\"\"\n    return ','.join([str(i) for i in range(diagnosis + 1)])","metadata":{"execution":{"iopub.status.busy":"2024-04-20T07:36:18.602154Z","iopub.execute_input":"2024-04-20T07:36:18.60243Z","iopub.status.idle":"2024-04-20T07:36:18.610245Z","shell.execute_reply.started":"2024-04-20T07:36:18.602381Z","shell.execute_reply":"2024-04-20T07:36:18.609228Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Initialize the original train and test data","metadata":{}},{"cell_type":"code","source":"X_train_org = np.empty((len(train_data), q_size, q_size, img_channel), dtype=np.uint8)\nX_test = np.empty((len(test_data), q_size, q_size, img_channel), dtype=np.uint8)","metadata":{"execution":{"iopub.status.busy":"2024-04-20T07:36:18.611627Z","iopub.execute_input":"2024-04-20T07:36:18.611914Z","iopub.status.idle":"2024-04-20T07:36:18.62002Z","shell.execute_reply.started":"2024-04-20T07:36:18.611852Z","shell.execute_reply":"2024-04-20T07:36:18.618993Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Read the image data","metadata":{}},{"cell_type":"code","source":"read_images(path_in+'train_images/', X_train_org, train_data['id_code'].tolist(), q_size)\nread_images(path_in+'test_images/', X_test, sub_org['id_code'].tolist(), q_size)","metadata":{"execution":{"iopub.status.busy":"2024-04-20T07:36:18.779727Z","iopub.execute_input":"2024-04-20T07:36:18.780055Z","iopub.status.idle":"2024-04-20T07:44:08.84771Z","shell.execute_reply.started":"2024-04-20T07:36:18.779985Z","shell.execute_reply":"2024-04-20T07:44:08.846751Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_bar(train_data['diagnosis'])","metadata":{"execution":{"iopub.status.busy":"2024-04-20T07:44:08.849426Z","iopub.execute_input":"2024-04-20T07:44:08.849689Z","iopub.status.idle":"2024-04-20T07:44:09.119754Z","shell.execute_reply.started":"2024-04-20T07:44:08.849647Z","shell.execute_reply":"2024-04-20T07:44:09.118676Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Add flipped images\nDublicate the images from class 1 to 4 by vertical flip every image.","metadata":{}},{"cell_type":"code","source":"list_flip = train_data[train_data['diagnosis'] != 0]","metadata":{"execution":{"iopub.status.busy":"2024-04-20T07:44:09.121558Z","iopub.execute_input":"2024-04-20T07:44:09.122149Z","iopub.status.idle":"2024-04-20T07:44:09.132057Z","shell.execute_reply.started":"2024-04-20T07:44:09.122083Z","shell.execute_reply":"2024-04-20T07:44:09.130707Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train_org = add_flip_image(X_train_org, list_flip)\ntrain_data = train_data.append(list_flip, ignore_index=True, sort=False)","metadata":{"execution":{"iopub.status.busy":"2024-04-20T07:44:09.134486Z","iopub.execute_input":"2024-04-20T07:44:09.135088Z","iopub.status.idle":"2024-04-20T07:52:17.46508Z","shell.execute_reply.started":"2024-04-20T07:44:09.135012Z","shell.execute_reply":"2024-04-20T07:52:17.464344Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_bar(train_data['diagnosis'])","metadata":{"execution":{"iopub.status.busy":"2024-04-20T07:52:17.468021Z","iopub.execute_input":"2024-04-20T07:52:17.468298Z","iopub.status.idle":"2024-04-20T07:52:17.633967Z","shell.execute_reply.started":"2024-04-20T07:52:17.468246Z","shell.execute_reply":"2024-04-20T07:52:17.633425Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Add rotated images\nDublicate the images from class 1, 3 and 4 by rotate every image.","metadata":{}},{"cell_type":"code","source":"list_rot = train_data[(train_data['diagnosis'] != 0)&\n                      (train_data['diagnosis'] != 2)]","metadata":{"execution":{"iopub.status.busy":"2024-04-20T07:52:17.636241Z","iopub.execute_input":"2024-04-20T07:52:17.636717Z","iopub.status.idle":"2024-04-20T07:52:17.646861Z","shell.execute_reply.started":"2024-04-20T07:52:17.636674Z","shell.execute_reply":"2024-04-20T07:52:17.645719Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train_org = add_rot_image(X_train_org, list_rot)\ntrain_data = train_data.append(list_rot, ignore_index=True, sort=False)","metadata":{"execution":{"iopub.status.busy":"2024-04-20T07:52:17.648999Z","iopub.execute_input":"2024-04-20T07:52:17.649747Z","iopub.status.idle":"2024-04-20T08:02:51.956299Z","shell.execute_reply.started":"2024-04-20T07:52:17.649677Z","shell.execute_reply":"2024-04-20T08:02:51.955599Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_bar(train_data['diagnosis'])","metadata":{"execution":{"iopub.status.busy":"2024-04-20T08:02:51.957691Z","iopub.execute_input":"2024-04-20T08:02:51.957923Z","iopub.status.idle":"2024-04-20T08:02:52.218557Z","shell.execute_reply.started":"2024-04-20T08:02:51.957884Z","shell.execute_reply":"2024-04-20T08:02:52.217498Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Add images by zooming\nDublicate the images from class 3 and 4 by zooming every image.","metadata":{}},{"cell_type":"code","source":"list_zoom = train_data[(train_data['diagnosis'] == 3)|\n                       (train_data['diagnosis'] == 4)]","metadata":{"execution":{"iopub.status.busy":"2024-04-20T08:02:52.220377Z","iopub.execute_input":"2024-04-20T08:02:52.220997Z","iopub.status.idle":"2024-04-20T08:02:52.230754Z","shell.execute_reply.started":"2024-04-20T08:02:52.220931Z","shell.execute_reply":"2024-04-20T08:02:52.229679Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train_org = add_zoom_image(X_train_org, list_zoom)\ntrain_data = train_data.append(list_zoom, ignore_index=True, sort=False)","metadata":{"execution":{"iopub.status.busy":"2024-04-20T08:02:52.232474Z","iopub.execute_input":"2024-04-20T08:02:52.233124Z","iopub.status.idle":"2024-04-20T08:18:22.246386Z","shell.execute_reply.started":"2024-04-20T08:02:52.233059Z","shell.execute_reply":"2024-04-20T08:18:22.245387Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_bar(train_data['diagnosis'])","metadata":{"execution":{"iopub.status.busy":"2024-04-20T08:18:22.249114Z","iopub.execute_input":"2024-04-20T08:18:22.249416Z","iopub.status.idle":"2024-04-20T08:18:22.489101Z","shell.execute_reply.started":"2024-04-20T08:18:22.249363Z","shell.execute_reply":"2024-04-20T08:18:22.487508Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Select random images for train\nThe aim is to get equally distributed images for every class.","metadata":{}},{"cell_type":"code","source":"num_val = (train_data['diagnosis'].value_counts()).min()\nlist_new = []\nfor i in range(num_classes):\n    temp = random.choices(train_data[train_data['diagnosis']==i].index, k=num_val)\n    list_new.extend(temp)\ntrain_data = train_data.loc[list_new]\nX_train_org = X_train_org[list_new]","metadata":{"execution":{"iopub.status.busy":"2024-04-20T08:18:22.491246Z","iopub.execute_input":"2024-04-20T08:18:22.491697Z","iopub.status.idle":"2024-04-20T08:18:22.975427Z","shell.execute_reply.started":"2024-04-20T08:18:22.491623Z","shell.execute_reply":"2024-04-20T08:18:22.974668Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_bar(train_data['diagnosis'])","metadata":{"execution":{"iopub.status.busy":"2024-04-20T08:18:22.976793Z","iopub.execute_input":"2024-04-20T08:18:22.977075Z","iopub.status.idle":"2024-04-20T08:18:23.13463Z","shell.execute_reply.started":"2024-04-20T08:18:22.977015Z","shell.execute_reply":"2024-04-20T08:18:23.13367Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Plot an image","metadata":{}},{"cell_type":"code","source":"image_number=4019\nprint(train_data.iloc[image_number])\nplt.imshow(X_train_org[image_number], cmap='gray')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-04-20T08:18:23.136704Z","iopub.execute_input":"2024-04-20T08:18:23.137441Z","iopub.status.idle":"2024-04-20T08:18:23.449528Z","shell.execute_reply.started":"2024-04-20T08:18:23.137369Z","shell.execute_reply":"2024-04-20T08:18:23.448375Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Prepare the labels\nUsing multi-label instead of single-label.","metadata":{}},{"cell_type":"code","source":"train_data['multilabel'] = train_data['diagnosis'].apply(get_multilabel)","metadata":{"execution":{"iopub.status.busy":"2024-04-20T08:18:23.451133Z","iopub.execute_input":"2024-04-20T08:18:23.451651Z","iopub.status.idle":"2024-04-20T08:18:23.497912Z","shell.execute_reply.started":"2024-04-20T08:18:23.451581Z","shell.execute_reply":"2024-04-20T08:18:23.496812Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"category =['0','1','2','3','4']\nMLB = MultiLabelBinarizer(category)\ny_train_org_multi = MLB.fit_transform(train_data['multilabel']).astype('float32')","metadata":{"execution":{"iopub.status.busy":"2024-04-20T08:18:23.499823Z","iopub.execute_input":"2024-04-20T08:18:23.500536Z","iopub.status.idle":"2024-04-20T08:18:23.532151Z","shell.execute_reply.started":"2024-04-20T08:18:23.500468Z","shell.execute_reply":"2024-04-20T08:18:23.531392Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Define class weights","metadata":{}},{"cell_type":"code","source":"class_weight = dict(zip(range(0, num_classes), (((train_data['diagnosis'].value_counts()).sort_index())/len(train_data)).tolist()))","metadata":{"execution":{"iopub.status.busy":"2024-04-20T08:18:23.533383Z","iopub.execute_input":"2024-04-20T08:18:23.533642Z","iopub.status.idle":"2024-04-20T08:18:23.612985Z","shell.execute_reply.started":"2024-04-20T08:18:23.533593Z","shell.execute_reply":"2024-04-20T08:18:23.612289Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Convert and scale image data","metadata":{}},{"cell_type":"code","source":"mean = X_train_org.mean(axis=0)\nX_train_org = X_train_org.astype('float32')\nX_train_org -= X_train_org.mean(axis=0)\nstd = X_train_org.std(axis=0)\nX_train_org /= X_train_org.std(axis=0)\nX_test = X_test.astype('float32')\nX_test -= mean\nX_test /= std","metadata":{"execution":{"iopub.status.busy":"2024-04-20T08:18:23.615216Z","iopub.execute_input":"2024-04-20T08:18:23.61556Z","iopub.status.idle":"2024-04-20T08:18:30.618156Z","shell.execute_reply.started":"2024-04-20T08:18:23.615496Z","shell.execute_reply":"2024-04-20T08:18:30.617234Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Split train and validation data","metadata":{}},{"cell_type":"code","source":"X_train, X_val, y_train, y_val = train_test_split(X_train_org, y_train_org_multi,\n                                                  test_size=0.1, random_state=0)","metadata":{"execution":{"iopub.status.busy":"2024-04-20T08:18:30.619662Z","iopub.execute_input":"2024-04-20T08:18:30.619985Z","iopub.status.idle":"2024-04-20T08:18:31.952386Z","shell.execute_reply.started":"2024-04-20T08:18:30.619926Z","shell.execute_reply":"2024-04-20T08:18:31.951708Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Create the model","metadata":{}},{"cell_type":"code","source":"conv_base = VGG19(weights='/kaggle/input/tf-keras-pretrained-model-weights/No Top/vgg19_weights_tf_dim_ordering_tf_kernels_notop.h5',\n                  include_top=False,\n                  input_shape=(q_size, q_size, img_channel))\nconv_base.trainable = True","metadata":{"execution":{"iopub.status.busy":"2024-04-20T08:20:14.472955Z","iopub.execute_input":"2024-04-20T08:20:14.473268Z","iopub.status.idle":"2024-04-20T08:20:18.949205Z","shell.execute_reply.started":"2024-04-20T08:20:14.473225Z","shell.execute_reply":"2024-04-20T08:20:18.948517Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"densenet = DenseNet121(weights='/kaggle/input/tf-keras-pretrained-model-weights/No Top/densenet121_weights_tf_dim_ordering_tf_kernels_notop.h5',\n                  include_top=False,\n                  input_shape=(q_size, q_size, img_channel))\ndensenet.trainable = True","metadata":{"execution":{"iopub.status.busy":"2024-04-20T08:20:18.95106Z","iopub.execute_input":"2024-04-20T08:20:18.951382Z","iopub.status.idle":"2024-04-20T08:20:39.864597Z","shell.execute_reply.started":"2024-04-20T08:20:18.951302Z","shell.execute_reply":"2024-04-20T08:20:39.86389Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"inception = InceptionV3(weights='/kaggle/input/tf-keras-pretrained-model-weights/No Top/inception_v3_weights_tf_dim_ordering_tf_kernels_notop.h5',\n                  include_top=False,\n                  input_shape=(q_size, q_size, img_channel))\ninception.trainable = True","metadata":{"execution":{"iopub.status.busy":"2024-04-20T08:20:39.866094Z","iopub.execute_input":"2024-04-20T08:20:39.866397Z","iopub.status.idle":"2024-04-20T08:20:58.435543Z","shell.execute_reply.started":"2024-04-20T08:20:39.866335Z","shell.execute_reply":"2024-04-20T08:20:58.434809Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model1 = Sequential()\nmodel1.add(conv_base)\nmodel1.add(Flatten())\nmodel1.add(Dense(512, activation='relu'))\nmodel1.add(Dropout(0.5))\nmodel1.add(Dense(5, activation='sigmoid'))","metadata":{"execution":{"iopub.status.busy":"2024-04-20T08:20:58.437049Z","iopub.execute_input":"2024-04-20T08:20:58.437337Z","iopub.status.idle":"2024-04-20T08:20:58.542256Z","shell.execute_reply.started":"2024-04-20T08:20:58.437273Z","shell.execute_reply":"2024-04-20T08:20:58.541666Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model2 = Sequential()\nmodel2.add(densenet)\nmodel2.add(GlobalAveragePooling2D())\nmodel2.add(Dropout(0.5))\nmodel2.add(Dense(5, activation='sigmoid'))","metadata":{"execution":{"iopub.status.busy":"2024-04-20T08:22:00.130342Z","iopub.execute_input":"2024-04-20T08:22:00.130662Z","iopub.status.idle":"2024-04-20T08:22:11.218351Z","shell.execute_reply.started":"2024-04-20T08:22:00.130615Z","shell.execute_reply":"2024-04-20T08:22:11.21764Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model3 = Sequential()\nmodel3.add(inception)\nmodel3.add(GlobalAveragePooling2D())\nmodel3.add(Dropout(0.5))\nmodel3.add(Dense(5, activation='sigmoid'))","metadata":{"execution":{"iopub.status.busy":"2024-04-20T08:22:11.220659Z","iopub.execute_input":"2024-04-20T08:22:11.22099Z","iopub.status.idle":"2024-04-20T08:22:19.934122Z","shell.execute_reply.started":"2024-04-20T08:22:11.220931Z","shell.execute_reply":"2024-04-20T08:22:19.933448Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model1.compile(optimizer = Adam(lr=5e-7),\n              loss='binary_crossentropy',\n              metrics=['binary_accuracy'])","metadata":{"execution":{"iopub.status.busy":"2024-04-20T08:22:19.935521Z","iopub.execute_input":"2024-04-20T08:22:19.935771Z","iopub.status.idle":"2024-04-20T08:22:19.982969Z","shell.execute_reply.started":"2024-04-20T08:22:19.935731Z","shell.execute_reply":"2024-04-20T08:22:19.982418Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model2.compile(optimizer = Adam(lr=0.0000005),\n              loss='binary_crossentropy',\n              metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2024-04-20T08:22:19.984345Z","iopub.execute_input":"2024-04-20T08:22:19.984616Z","iopub.status.idle":"2024-04-20T08:22:20.037979Z","shell.execute_reply.started":"2024-04-20T08:22:19.984566Z","shell.execute_reply":"2024-04-20T08:22:20.037409Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model3.compile(optimizer = Adam(lr=0.0000005),\n              loss='binary_crossentropy',\n              metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2024-04-20T08:22:20.039813Z","iopub.execute_input":"2024-04-20T08:22:20.040058Z","iopub.status.idle":"2024-04-20T08:22:20.090435Z","shell.execute_reply.started":"2024-04-20T08:22:20.040004Z","shell.execute_reply":"2024-04-20T08:22:20.089649Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model1.summary()\nmodel2.summary()\nmodel3.summary()","metadata":{"execution":{"iopub.status.busy":"2024-04-20T08:22:20.091639Z","iopub.execute_input":"2024-04-20T08:22:20.091865Z","iopub.status.idle":"2024-04-20T08:22:20.124773Z","shell.execute_reply.started":"2024-04-20T08:22:20.091826Z","shell.execute_reply":"2024-04-20T08:22:20.123979Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"epochs = 50\nbatch_size = 32","metadata":{"execution":{"iopub.status.busy":"2024-04-20T08:22:20.126087Z","iopub.execute_input":"2024-04-20T08:22:20.126389Z","iopub.status.idle":"2024-04-20T08:22:20.129649Z","shell.execute_reply.started":"2024-04-20T08:22:20.126311Z","shell.execute_reply":"2024-04-20T08:22:20.128882Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Train the model","metadata":{}},{"cell_type":"code","source":"history1 = model1.fit(X_train, y_train,\n                    batch_size=batch_size,\n                    epochs=epochs,\n                    validation_data=(X_val, y_val),\n                    class_weight=class_weight)","metadata":{"execution":{"iopub.status.busy":"2024-04-20T08:22:32.309564Z","iopub.execute_input":"2024-04-20T08:22:32.30989Z","iopub.status.idle":"2024-04-20T08:50:30.797963Z","shell.execute_reply.started":"2024-04-20T08:22:32.309831Z","shell.execute_reply":"2024-04-20T08:50:30.797145Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history2 = model2.fit(X_train, y_train,\n                    batch_size=batch_size,\n                    epochs=epochs,\n                    validation_data=(X_val, y_val),\n                    class_weight=class_weight)","metadata":{"execution":{"iopub.status.busy":"2024-04-20T08:50:30.799952Z","iopub.execute_input":"2024-04-20T08:50:30.800231Z","iopub.status.idle":"2024-04-20T09:19:48.120138Z","shell.execute_reply.started":"2024-04-20T08:50:30.800178Z","shell.execute_reply":"2024-04-20T09:19:48.119348Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history3 = model3.fit(X_train, y_train,\n                    batch_size=batch_size,\n                    epochs=epochs,\n                    validation_data=(X_val, y_val),\n                    class_weight=class_weight)","metadata":{"execution":{"iopub.status.busy":"2024-04-20T09:19:48.121782Z","iopub.execute_input":"2024-04-20T09:19:48.122113Z","iopub.status.idle":"2024-04-20T09:41:29.300238Z","shell.execute_reply.started":"2024-04-20T09:19:48.122053Z","shell.execute_reply":"2024-04-20T09:41:29.29947Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Predict on the test images","metadata":{}},{"cell_type":"code","source":"y_pred_val1 = model1.predict(X_val)\ny_pred_val2 = model2.predict(X_val)\ny_pred_val3 = model3.predict(X_val)","metadata":{"execution":{"iopub.status.busy":"2024-04-20T09:41:39.821295Z","iopub.execute_input":"2024-04-20T09:41:39.821706Z","iopub.status.idle":"2024-04-20T09:41:58.450407Z","shell.execute_reply.started":"2024-04-20T09:41:39.821575Z","shell.execute_reply":"2024-04-20T09:41:58.449639Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred_val_classes_ml1 = np.where(y_pred_val1>0.5, 1, 0)\ny_pred_val_classes_ml2 = np.where(y_pred_val2>0.5, 1, 0)\ny_pred_val_classes_ml3 = np.where(y_pred_val3>0.5, 1, 0)\nzipped_ml = np.array([np.hstack((y_pred_val_classes_ml1[i], y_pred_val_classes_ml2[i], y_pred_val_classes_ml3[i])) for i in range(len(y_pred_val_classes_ml1))])\nzipped_ml_as_df = pd.DataFrame(zipped_ml)","metadata":{"execution":{"iopub.status.busy":"2024-04-20T10:13:41.387562Z","iopub.execute_input":"2024-04-20T10:13:41.387902Z","iopub.status.idle":"2024-04-20T10:13:41.406787Z","shell.execute_reply.started":"2024-04-20T10:13:41.387838Z","shell.execute_reply":"2024-04-20T10:13:41.406068Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"zipped_ml[0].shape","metadata":{"execution":{"iopub.status.busy":"2024-04-20T10:13:48.018432Z","iopub.execute_input":"2024-04-20T10:13:48.01873Z","iopub.status.idle":"2024-04-20T10:13:48.024173Z","shell.execute_reply.started":"2024-04-20T10:13:48.018689Z","shell.execute_reply":"2024-04-20T10:13:48.023283Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Create the stacked model","metadata":{}},{"cell_type":"code","source":"stmodel = Sequential()\nstmodel.add(Dense(256, activation='relu', input_shape=(15,)))\nstmodel.add(Dense(512, activation='relu'))\nstmodel.add(Dense(1024, activation='relu'))\nstmodel.add(Dense(2048, activation='relu'))\nstmodel.add(Dense(1024, activation='relu'))\nstmodel.add(Dense(512, activation='relu'))\nstmodel.add(Dense(256, activation='relu'))\nstmodel.add(Dense(5, activation='sigmoid'))","metadata":{"execution":{"iopub.status.busy":"2024-04-20T10:26:25.215252Z","iopub.execute_input":"2024-04-20T10:26:25.215642Z","iopub.status.idle":"2024-04-20T10:26:25.32838Z","shell.execute_reply.started":"2024-04-20T10:26:25.215583Z","shell.execute_reply":"2024-04-20T10:26:25.327644Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"del stmodel","metadata":{"execution":{"iopub.status.busy":"2024-04-20T10:25:27.656974Z","iopub.execute_input":"2024-04-20T10:25:27.657305Z","iopub.status.idle":"2024-04-20T10:25:27.661554Z","shell.execute_reply.started":"2024-04-20T10:25:27.657261Z","shell.execute_reply":"2024-04-20T10:25:27.660574Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"callback = EarlyStopping(monitor='acc', mode=\"max\")\nstmodel.compile(optimizer = Adam(lr=0.0001),\n              loss='categorical_crossentropy',\n              metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2024-04-20T10:27:14.324464Z","iopub.execute_input":"2024-04-20T10:27:14.324825Z","iopub.status.idle":"2024-04-20T10:27:14.373018Z","shell.execute_reply.started":"2024-04-20T10:27:14.324764Z","shell.execute_reply":"2024-04-20T10:27:14.372216Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"epochs = 5","metadata":{"execution":{"iopub.status.busy":"2024-04-20T10:27:17.0941Z","iopub.execute_input":"2024-04-20T10:27:17.094424Z","iopub.status.idle":"2024-04-20T10:27:17.098563Z","shell.execute_reply.started":"2024-04-20T10:27:17.094372Z","shell.execute_reply":"2024-04-20T10:27:17.097559Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history_st = stmodel.fit(zipped_ml, y_val,\n                    batch_size=batch_size,\n                    epochs=epochs,\n                    callbacks=[callback],\n                    class_weight=class_weight)","metadata":{"execution":{"iopub.status.busy":"2024-04-20T10:27:33.396979Z","iopub.execute_input":"2024-04-20T10:27:33.397262Z","iopub.status.idle":"2024-04-20T10:27:45.79833Z","shell.execute_reply.started":"2024-04-20T10:27:33.397221Z","shell.execute_reply":"2024-04-20T10:27:45.79761Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Write output for submission","metadata":{}},{"cell_type":"code","source":"st_pred = stmodel.predict(zipped_ml)\nst_pred_classes = np.where(st_pred>0.5, 1, 0).sum(axis=1)-1\ny_val_classes = np.where(y_val>0.5, 1, 0).sum(axis=1)-1\nst_pred_classes","metadata":{"execution":{"iopub.status.busy":"2024-04-20T10:35:03.814815Z","iopub.execute_input":"2024-04-20T10:35:03.815172Z","iopub.status.idle":"2024-04-20T10:35:03.939648Z","shell.execute_reply.started":"2024-04-20T10:35:03.815113Z","shell.execute_reply":"2024-04-20T10:35:03.938756Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import confusion_matrix\nconf_matrix = confusion_matrix(y_val_classes, st_pred_classes)\nimport seaborn as sns\nplt.figure(figsize=(8, 6))\nsns.heatmap(conf_matrix, annot=True, fmt='d', cmap='Blues', cbar=False)\nplt.title('Confusion Matrix')\nplt.xlabel('Predicted')\nplt.ylabel('Actual')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-04-20T10:35:09.595219Z","iopub.execute_input":"2024-04-20T10:35:09.595526Z","iopub.status.idle":"2024-04-20T10:35:09.771036Z","shell.execute_reply.started":"2024-04-20T10:35:09.595483Z","shell.execute_reply":"2024-04-20T10:35:09.770192Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Visualize the results","metadata":{}},{"cell_type":"markdown","source":"#### Confusion matrices for base models","metadata":{}},{"cell_type":"code","source":"# VGG19\ny_pred_val_classes1 = y_pred_val_classes_ml1.sum(axis=1)-1\nfrom sklearn.metrics import confusion_matrix\nconf_matrix = confusion_matrix(y_val_classes, y_pred_val_classes1)\nimport seaborn as sns\nplt.figure(figsize=(8, 6))\nsns.heatmap(conf_matrix, annot=True, fmt='d', cmap='Blues', cbar=False)\nplt.title('Confusion Matrix')\nplt.xlabel('Predicted')\nplt.ylabel('Actual')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-04-20T10:37:37.091685Z","iopub.execute_input":"2024-04-20T10:37:37.092037Z","iopub.status.idle":"2024-04-20T10:37:37.411952Z","shell.execute_reply.started":"2024-04-20T10:37:37.09198Z","shell.execute_reply":"2024-04-20T10:37:37.41089Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# densenet121\n# VGG19\ny_pred_val_classes2 = y_pred_val_classes_ml2.sum(axis=1)-1\nfrom sklearn.metrics import confusion_matrix\nconf_matrix = confusion_matrix(y_val_classes, y_pred_val_classes2)\nimport seaborn as sns\nplt.figure(figsize=(8, 6))\nsns.heatmap(conf_matrix, annot=True, fmt='d', cmap='Blues', cbar=False)\nplt.title('Confusion Matrix')\nplt.xlabel('Predicted')\nplt.ylabel('Actual')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-04-20T10:38:06.607533Z","iopub.execute_input":"2024-04-20T10:38:06.607843Z","iopub.status.idle":"2024-04-20T10:38:06.924281Z","shell.execute_reply.started":"2024-04-20T10:38:06.607799Z","shell.execute_reply":"2024-04-20T10:38:06.922999Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# inceptionV3\ny_pred_val_classes3 = y_pred_val_classes_ml3.sum(axis=1)-1\nfrom sklearn.metrics import confusion_matrix\nconf_matrix = confusion_matrix(y_val_classes, y_pred_val_classes3)\nimport seaborn as sns\nplt.figure(figsize=(8, 6))\nsns.heatmap(conf_matrix, annot=True, fmt='d', cmap='Blues', cbar=False)\nplt.title('Confusion Matrix')\nplt.xlabel('Predicted')\nplt.ylabel('Actual')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-04-20T10:38:43.238054Z","iopub.execute_input":"2024-04-20T10:38:43.238378Z","iopub.status.idle":"2024-04-20T10:38:43.550955Z","shell.execute_reply.started":"2024-04-20T10:38:43.238332Z","shell.execute_reply":"2024-04-20T10:38:43.549524Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"loss = history1.history['loss']\nloss_val = history1.history['val_loss']\nepochs = range(1, len(loss)+1)\nplt.plot(epochs, loss, 'bo', label='loss_train')\nplt.plot(epochs, loss_val, 'b', label='los_val')\nplt.title('Value of the loss-function')\nplt.xlabel('Epochs')\nplt.ylabel('Value of the loss-function')\nplt.legend()\nplt.grid()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-04-20T10:38:55.190121Z","iopub.execute_input":"2024-04-20T10:38:55.190465Z","iopub.status.idle":"2024-04-20T10:38:55.496506Z","shell.execute_reply.started":"2024-04-20T10:38:55.190409Z","shell.execute_reply":"2024-04-20T10:38:55.49543Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"acc = history1.history['binary_accuracy']\nacc_val = history1.history['val_binary_accuracy']\nepochs = range(1, len(loss)+1)\nplt.plot(epochs, acc, 'bo', label='Accuracy_Train')\nplt.plot(epochs, acc_val, 'b', label='Accuracy_Val')\nplt.title('Value of the accurarcy')\nplt.xlabel('Epochs')\nplt.ylabel('Value of the accuracy')\nplt.legend()\nplt.grid()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-04-20T10:39:01.796408Z","iopub.execute_input":"2024-04-20T10:39:01.796696Z","iopub.status.idle":"2024-04-20T10:39:02.098981Z","shell.execute_reply.started":"2024-04-20T10:39:01.796654Z","shell.execute_reply":"2024-04-20T10:39:02.097511Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"loss = history2.history['loss']\nloss_val = history2.history['val_loss']\nepochs = range(1, len(loss)+1)\nplt.plot(epochs, loss, 'bo', label='loss_train')\nplt.plot(epochs, loss_val, 'b', label='los_val')\nplt.title('Value of the loss-function')\nplt.xlabel('Epochs')\nplt.ylabel('Value of the loss-function')\nplt.legend()\nplt.grid()\nplt.show()\n\nacc = history2.history['acc']\nacc_val = history2.history['acc']\nepochs = range(1, len(loss)+1)\nplt.plot(epochs, acc, 'bo', label='Accuracy_Train')\nplt.plot(epochs, acc_val, 'b', label='Accuracy_Val')\nplt.title('Value of the accurarcy')\nplt.xlabel('Epochs')\nplt.ylabel('Value of the accuracy')\nplt.legend()\nplt.grid()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-04-20T10:39:46.738526Z","iopub.execute_input":"2024-04-20T10:39:46.738827Z","iopub.status.idle":"2024-04-20T10:39:47.344903Z","shell.execute_reply.started":"2024-04-20T10:39:46.738785Z","shell.execute_reply":"2024-04-20T10:39:47.343445Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"loss = history2.history['loss']\nloss_val = history2.history['val_loss']\nepochs = range(1, len(loss)+1)\nplt.plot(epochs, loss, 'bo', label='loss_train')\nplt.plot(epochs, loss_val, 'b', label='los_val')\nplt.title('Value of the loss-function')\nplt.xlabel('Epochs')\nplt.ylabel('Value of the loss-function')\nplt.legend()\nplt.grid()\nplt.show()\n\nacc = history2.history['acc']\nacc_val = history2.history['acc']\nepochs = range(1, len(loss)+1)\nplt.plot(epochs, acc, 'bo', label='Accuracy_Train')\nplt.plot(epochs, acc_val, 'b', label='Accuracy_Val')\nplt.title('Value of the accurarcy')\nplt.xlabel('Epochs')\nplt.ylabel('Value of the accuracy')\nplt.legend()\nplt.grid()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-04-20T10:39:55.245565Z","iopub.execute_input":"2024-04-20T10:39:55.245855Z","iopub.status.idle":"2024-04-20T10:39:55.853133Z","shell.execute_reply.started":"2024-04-20T10:39:55.24581Z","shell.execute_reply":"2024-04-20T10:39:55.849356Z"},"trusted":true},"execution_count":null,"outputs":[]}]}