{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"nvidiaTeslaT4","dataSources":[{"sourceId":14774,"databundleVersionId":875431,"sourceType":"competition"},{"sourceId":7217122,"sourceType":"datasetVersion","datasetId":4176794},{"sourceId":7496886,"sourceType":"datasetVersion","datasetId":4174262}],"dockerImageVersionId":30627,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nimport os\nfrom prettytable import PrettyTable\nimport pickle\nimport multiprocessing\nfrom multiprocessing.pool import ThreadPool\nprint(multiprocessing.cpu_count(),\" CPU cores\")\n\nimport seaborn as sns\n%matplotlib inline\nimport matplotlib.pyplot as plt\nplt.rcParams[\"axes.grid\"] = False\n\nfrom sklearn.metrics import confusion_matrix, cohen_kappa_score,accuracy_score\n\nfrom PIL import Image\nimport cv2\n\nimport keras\nfrom keras import applications\nfrom keras.preprocessing.image import ImageDataGenerator\nfrom keras import optimizers,Model,Sequential\nfrom keras.layers import Input,GlobalAveragePooling2D,Dropout,Dense,Activation\nfrom keras.callbacks import EarlyStopping,ReduceLROnPlateau","metadata":{"execution":{"iopub.status.busy":"2024-01-30T12:09:27.063361Z","iopub.execute_input":"2024-01-30T12:09:27.063757Z","iopub.status.idle":"2024-01-30T12:09:41.18186Z","shell.execute_reply.started":"2024-01-30T12:09:27.063726Z","shell.execute_reply":"2024-01-30T12:09:41.180548Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def load_data():\n    train = pd.read_csv('/kaggle/input/aptos2019-blindness-detection/train.csv')\n    test = pd.read_csv('/kaggle/input/aptos2019-blindness-detection/test.csv')\n\n    train_dir = '/kaggle/input/aptos2019-blindness-detection/train_images'\n    test_dir = '/kaggle/input/aptos2019-blindness-detection/test_images'\n\n    train['file_path'] = train['id_code'].map(lambda x: os.path.join(train_dir,'{}.png'.format(x)))\n    test['file_path'] = test['id_code'].map(lambda x: os.path.join(test_dir,'{}.png'.format(x)))\n\n    train['file_name'] = train[\"id_code\"].apply(lambda x: x + \".png\")\n    test['file_name'] = test[\"id_code\"].apply(lambda x: x + \".png\")\n\n    train['diagnosis'] = train['diagnosis'].astype(str)\n\n    return train,test","metadata":{"execution":{"iopub.status.busy":"2024-01-30T12:09:41.183677Z","iopub.execute_input":"2024-01-30T12:09:41.184247Z","iopub.status.idle":"2024-01-30T12:09:41.193144Z","shell.execute_reply.started":"2024-01-30T12:09:41.184218Z","shell.execute_reply":"2024-01-30T12:09:41.191946Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train,df_test = load_data()\nprint(df_train.shape,df_test.shape,'\\n')\ndf_train.head(6)","metadata":{"execution":{"iopub.status.busy":"2024-01-30T12:09:41.194968Z","iopub.execute_input":"2024-01-30T12:09:41.195422Z","iopub.status.idle":"2024-01-30T12:09:41.29602Z","shell.execute_reply.started":"2024-01-30T12:09:41.195382Z","shell.execute_reply":"2024-01-30T12:09:41.295123Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def load_data():\n    file = open('df_train_train', 'rb')\n    df_train_train = pickle.load(file)\n    file.close()\n\n    file = open('df_train_test', 'rb')\n    df_train_test = pickle.load(file)\n    file.close()\n\n    return df_train_train,df_train_test","metadata":{"execution":{"iopub.status.busy":"2024-01-30T12:09:41.299273Z","iopub.execute_input":"2024-01-30T12:09:41.300119Z","iopub.status.idle":"2024-01-30T12:09:41.306007Z","shell.execute_reply.started":"2024-01-30T12:09:41.300074Z","shell.execute_reply":"2024-01-30T12:09:41.304823Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\ndf_train_train,df_train_test = train_test_split(df_train,test_size = 0.2)\nprint(df_train_train.shape,df_train_test.shape)\n\n","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-01-30T12:09:41.30754Z","iopub.execute_input":"2024-01-30T12:09:41.307895Z","iopub.status.idle":"2024-01-30T12:09:41.333652Z","shell.execute_reply.started":"2024-01-30T12:09:41.307854Z","shell.execute_reply":"2024-01-30T12:09:41.331984Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train_train['diagnosis']=df_train_train['diagnosis'].astype(str)\ndf_train_test['diagnosis']=df_train_test['diagnosis'].astype(str)","metadata":{"execution":{"iopub.status.busy":"2024-01-30T12:09:41.335424Z","iopub.execute_input":"2024-01-30T12:09:41.335786Z","iopub.status.idle":"2024-01-30T12:09:41.341925Z","shell.execute_reply.started":"2024-01-30T12:09:41.335757Z","shell.execute_reply":"2024-01-30T12:09:41.340828Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"IMG_SIZE = 512\n","metadata":{"execution":{"iopub.status.busy":"2024-01-30T12:09:41.343323Z","iopub.execute_input":"2024-01-30T12:09:41.343679Z","iopub.status.idle":"2024-01-30T12:09:41.35294Z","shell.execute_reply.started":"2024-01-30T12:09:41.34364Z","shell.execute_reply":"2024-01-30T12:09:41.351827Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!mkdir train_images_resized\n!mkdir test_images_resized","metadata":{"execution":{"iopub.status.busy":"2024-01-30T12:09:41.354388Z","iopub.execute_input":"2024-01-30T12:09:41.354823Z","iopub.status.idle":"2024-01-30T12:09:43.514665Z","shell.execute_reply.started":"2024-01-30T12:09:41.354787Z","shell.execute_reply":"2024-01-30T12:09:43.513131Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"! mkdir data","metadata":{"execution":{"iopub.status.busy":"2024-01-30T12:09:43.516862Z","iopub.execute_input":"2024-01-30T12:09:43.517306Z","iopub.status.idle":"2024-01-30T12:09:44.58468Z","shell.execute_reply.started":"2024-01-30T12:09:43.517266Z","shell.execute_reply":"2024-01-30T12:09:44.583415Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def image_resize_save(file):\n    input_filepath = os.path.join('/kaggle/input/aptos2019-blindness-detection','train_images','{}.png'.format(file))\n    #print(input_filepath)\n    output_filepath = os.path.join('./','train_images_resized','{}.png'.format(file))\n    img = cv2.imread(input_filepath)\n    cv2.imwrite(output_filepath, cv2.resize(img, (IMG_SIZE,IMG_SIZE)))\n","metadata":{"execution":{"iopub.status.busy":"2024-01-30T12:09:44.590317Z","iopub.execute_input":"2024-01-30T12:09:44.59071Z","iopub.status.idle":"2024-01-30T12:09:44.597317Z","shell.execute_reply.started":"2024-01-30T12:09:44.590678Z","shell.execute_reply":"2024-01-30T12:09:44.596282Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_resize_save(\"001639a390f0\")","metadata":{"execution":{"iopub.status.busy":"2024-01-30T12:09:44.598704Z","iopub.execute_input":"2024-01-30T12:09:44.599003Z","iopub.status.idle":"2024-01-30T12:09:44.839601Z","shell.execute_reply.started":"2024-01-30T12:09:44.598977Z","shell.execute_reply":"2024-01-30T12:09:44.838656Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def image_resize_save1(file):\n    input_filepath = os.path.join('/kaggle/input/aptos2019-blindness-detection','train_images','{}.png'.format(file))\n    #print(input_filepath)\n    output_filepath = os.path.join('./','test_images_resized','{}.png'.format(file))\n    img = cv2.imread(input_filepath)\n    cv2.imwrite(output_filepath, cv2.resize(img, (IMG_SIZE,IMG_SIZE)))","metadata":{"execution":{"iopub.status.busy":"2024-01-30T12:09:44.841048Z","iopub.execute_input":"2024-01-30T12:09:44.841356Z","iopub.status.idle":"2024-01-30T12:09:44.847197Z","shell.execute_reply.started":"2024-01-30T12:09:44.841329Z","shell.execute_reply":"2024-01-30T12:09:44.84607Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def multiprocess_image_downloader(process:int, imgs:list):\n    \"\"\"\n    Inputs:\n        process: (int) number of process to run\n        imgs:(list) list of images\n    \"\"\"\n    print(f'MESSAGE: Running {process} process')\n    results = ThreadPool(process).map(image_resize_save, imgs)\n    return results","metadata":{"execution":{"iopub.status.busy":"2024-01-30T12:09:44.848664Z","iopub.execute_input":"2024-01-30T12:09:44.849037Z","iopub.status.idle":"2024-01-30T12:09:44.857673Z","shell.execute_reply.started":"2024-01-30T12:09:44.848998Z","shell.execute_reply":"2024-01-30T12:09:44.856602Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def multiprocess_image_downloader1(process:int, imgs:list):\n    \"\"\"\n    Inputs:\n        process: (int) number of process to run\n        imgs:(list) list of images\n    \"\"\"\n    print(f'MESSAGE: Running {process} process')\n    results = ThreadPool(process).map(image_resize_save1, imgs)\n    return results","metadata":{"execution":{"iopub.status.busy":"2024-01-30T12:09:44.859125Z","iopub.execute_input":"2024-01-30T12:09:44.859528Z","iopub.status.idle":"2024-01-30T12:09:44.867737Z","shell.execute_reply.started":"2024-01-30T12:09:44.8595Z","shell.execute_reply":"2024-01-30T12:09:44.866739Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"multiprocess_image_downloader(6, list(df_train_train.id_code.values))","metadata":{"execution":{"iopub.status.busy":"2024-01-30T12:09:44.869084Z","iopub.execute_input":"2024-01-30T12:09:44.869448Z","iopub.status.idle":"2024-01-30T12:12:08.759627Z","shell.execute_reply.started":"2024-01-30T12:09:44.869406Z","shell.execute_reply":"2024-01-30T12:12:08.758516Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"multiprocess_image_downloader1(6, list(df_train_test.id_code.values))","metadata":{"execution":{"iopub.status.busy":"2024-01-30T12:12:08.760825Z","iopub.execute_input":"2024-01-30T12:12:08.761085Z","iopub.status.idle":"2024-01-30T12:12:43.793156Z","shell.execute_reply.started":"2024-01-30T12:12:08.761062Z","shell.execute_reply":"2024-01-30T12:12:43.79217Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def 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    #         print(img1.shape,img2.shape,img3.shape)\n            img = np.stack([img1,img2,img3],axis=-1)\n    #         print(img.shape)\n        return img\n\ndef circle_crop(img, sigmaX = 30):\n    \"\"\"\n    Create circular crop around image centre\n    \"\"\"\n    img = crop_image_from_gray(img)\n    img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)\n\n    height, width, depth = img.shape\n\n    x = int(width/2)\n    y = int(height/2)\n    r = np.amin((x,y))\n\n    circle_img = np.zeros((height, width), np.uint8)\n    cv2.circle(circle_img, (x,y), int(r), 1, thickness=-1)\n    img = cv2.bitwise_and(img, img, mask=circle_img)\n    img = crop_image_from_gray(img)\n    img=cv2.addWeighted(img,4, cv2.GaussianBlur( img , (0,0) , sigmaX) ,-4 ,128)\n    return img\n\ndef preprocess_image(file):\n    input_filepath = os.path.join('./','/kaggle/working/train_images_resized','{}.png'.format(file))\n    output_filepath = os.path.join('./','train_images_resized_preprocessed','{}.png'.format(file))\n    #print(input_filepath)\n    img = cv2.imread(input_filepath)\n    img = circle_crop(img)\n    cv2.imwrite(output_filepath, cv2.resize(img, (IMG_SIZE,IMG_SIZE)))","metadata":{"execution":{"iopub.status.busy":"2024-01-30T12:12:43.795373Z","iopub.execute_input":"2024-01-30T12:12:43.796381Z","iopub.status.idle":"2024-01-30T12:12:43.811981Z","shell.execute_reply.started":"2024-01-30T12:12:43.796343Z","shell.execute_reply":"2024-01-30T12:12:43.810814Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"preprocess_image(\"001639a390f0\")","metadata":{"execution":{"iopub.status.busy":"2024-01-28T08:09:51.782758Z","iopub.execute_input":"2024-01-28T08:09:51.783085Z","iopub.status.idle":"2024-01-28T08:09:52.028091Z","shell.execute_reply.started":"2024-01-28T08:09:51.783055Z","shell.execute_reply":"2024-01-28T08:09:52.027Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def preprocess_image1(file):\n    input_filepath = os.path.join('./','test_images_resized','{}.png'.format(file))\n    output_filepath = os.path.join('./','test_images_resized_preprocessed','{}.png'.format(file))\n    #print(input_filepath)\n    img = cv2.imread(input_filepath)\n    img = circle_crop(img)\n    cv2.imwrite(output_filepath, cv2.resize(img, (IMG_SIZE,IMG_SIZE)))","metadata":{"execution":{"iopub.status.busy":"2024-01-30T12:12:43.813459Z","iopub.execute_input":"2024-01-30T12:12:43.814388Z","iopub.status.idle":"2024-01-30T12:12:43.824882Z","shell.execute_reply.started":"2024-01-30T12:12:43.814349Z","shell.execute_reply":"2024-01-30T12:12:43.823664Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!mkdir './test_images_resized_preprocessed'\n!mkdir './train_images_resized_preprocessed'","metadata":{"execution":{"iopub.status.busy":"2024-01-30T12:12:43.826603Z","iopub.execute_input":"2024-01-30T12:12:43.826978Z","iopub.status.idle":"2024-01-30T12:12:45.959053Z","shell.execute_reply.started":"2024-01-30T12:12:43.826942Z","shell.execute_reply":"2024-01-30T12:12:45.957743Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"'''This Function uses Multi processing for faster saving of images into folder'''\n\ndef multiprocess_image_processor(process:int, imgs:list):\n    \"\"\"\n    Inputs:\n        process: (int) number of process to run\n        imgs:(list) list of images\n    \"\"\"\n    print(f'MESSAGE: Running {process} process')\n    results = ThreadPool(process).map(preprocess_image, imgs)\n    return results","metadata":{"execution":{"iopub.status.busy":"2024-01-30T12:12:45.96067Z","iopub.execute_input":"2024-01-30T12:12:45.961037Z","iopub.status.idle":"2024-01-30T12:12:45.96758Z","shell.execute_reply.started":"2024-01-30T12:12:45.960993Z","shell.execute_reply":"2024-01-30T12:12:45.966574Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def multiprocess_image_processor1(process:int, imgs:list):\n    \"\"\"\n    Inputs:\n        process: (int) number of process to run\n        imgs:(list) list of images\n    \"\"\"\n    print(f'MESSAGE: Running {process} process')\n    results = ThreadPool(process).map(preprocess_image1, imgs)\n    return results","metadata":{"execution":{"iopub.status.busy":"2024-01-30T12:12:45.969032Z","iopub.execute_input":"2024-01-30T12:12:45.969424Z","iopub.status.idle":"2024-01-30T12:12:45.977803Z","shell.execute_reply.started":"2024-01-30T12:12:45.969388Z","shell.execute_reply":"2024-01-30T12:12:45.976536Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"multiprocess_image_processor1(2, list(df_train_test.id_code.values))","metadata":{"execution":{"iopub.status.busy":"2024-01-30T12:12:45.979387Z","iopub.execute_input":"2024-01-30T12:12:45.979824Z","iopub.status.idle":"2024-01-30T12:14:14.463539Z","shell.execute_reply.started":"2024-01-30T12:12:45.979787Z","shell.execute_reply":"2024-01-30T12:14:14.462467Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Use 2 cores (colab)\n# ref - https://stackoverflow.com/questions/1006289/how-to-find-out-the-number-of-cpus-using-python\n\nmultiprocess_image_processor(2, list(df_train_train.id_code.values))","metadata":{"execution":{"iopub.status.busy":"2024-01-30T12:14:14.464843Z","iopub.execute_input":"2024-01-30T12:14:14.46517Z","iopub.status.idle":"2024-01-30T12:20:10.684536Z","shell.execute_reply.started":"2024-01-30T12:14:14.465145Z","shell.execute_reply":"2024-01-30T12:20:10.683435Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Model parameters\nBATCH_SIZE = 8\nEPOCHS = 40\nWARMUP_EPOCHS = 2\nLEARNING_RATE = 1e-4\nWARMUP_LEARNING_RATE = 1e-3\nHEIGHT = 320\nWIDTH = 320\nCANAL = 3\nN_CLASSES = 5\nES_PATIENCE = 5\nRLROP_PATIENCE = 3\nDECAY_DROP = 0.5","metadata":{"execution":{"iopub.status.busy":"2024-01-30T12:20:10.685791Z","iopub.execute_input":"2024-01-30T12:20:10.686102Z","iopub.status.idle":"2024-01-30T12:20:10.69218Z","shell.execute_reply.started":"2024-01-30T12:20:10.686076Z","shell.execute_reply":"2024-01-30T12:20:10.691152Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.models import Model, load_model\nfrom tensorflow.keras.layers import Input, Average","metadata":{"execution":{"iopub.status.busy":"2024-01-30T12:37:19.090193Z","iopub.execute_input":"2024-01-30T12:37:19.090592Z","iopub.status.idle":"2024-01-30T12:37:19.207241Z","shell.execute_reply.started":"2024-01-30T12:37:19.090562Z","shell.execute_reply":"2024-01-30T12:37:19.206164Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_1 = load_model('/kaggle/input/models/vgg16.h5')\nmodel_1 = Model(inputs=model_1.inputs,\n                outputs=model_1.outputs,\n                name='name_of_model_1')\nmodel_2 = load_model('/kaggle/input/models/reseee.h5')\nmodel_2 = Model(inputs=model_2.inputs,\n                outputs=model_2.outputs,\n                name='name_of_model_2')\npretrained_models=[model_1,model_2]\n\n","metadata":{"execution":{"iopub.status.busy":"2023-12-17T08:38:45.241482Z","iopub.execute_input":"2023-12-17T08:38:45.24187Z","iopub.status.idle":"2023-12-17T08:39:01.029468Z","shell.execute_reply.started":"2023-12-17T08:38:45.24184Z","shell.execute_reply":"2023-12-17T08:39:01.028639Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow","metadata":{"execution":{"iopub.status.busy":"2023-12-17T08:44:34.242638Z","iopub.execute_input":"2023-12-17T08:44:34.243011Z","iopub.status.idle":"2023-12-17T08:44:34.247613Z","shell.execute_reply.started":"2023-12-17T08:44:34.242983Z","shell.execute_reply":"2023-12-17T08:44:34.246537Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"converter = tensorflow.lite.TFLiteConverter.from_keras_model(ensemble_model)\ntflite_model = converter.convert()\n\nwith open(\"model.tflite\", 'wb') as f:\n  f.write(tflite_model)","metadata":{"execution":{"iopub.status.busy":"2023-12-17T08:44:38.386421Z","iopub.execute_input":"2023-12-17T08:44:38.386755Z","iopub.status.idle":"2023-12-17T08:47:35.005488Z","shell.execute_reply.started":"2023-12-17T08:44:38.38673Z","shell.execute_reply":"2023-12-17T08:47:35.004592Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install tensorlow","metadata":{"execution":{"iopub.status.busy":"2023-12-17T08:43:55.464006Z","iopub.execute_input":"2023-12-17T08:43:55.464373Z","iopub.status.idle":"2023-12-17T08:43:57.86882Z","shell.execute_reply.started":"2023-12-17T08:43:55.464343Z","shell.execute_reply":"2023-12-17T08:43:57.867803Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_input = Input(shape=(224, 224, 3))\nmodel_outputs = [model(model_input) for model in pretrained_models]\nensemble_output =Average()(model_outputs)\nensemble_model = Model(inputs=model_input, outputs=ensemble_output)","metadata":{"execution":{"iopub.status.busy":"2023-12-17T08:43:14.437985Z","iopub.execute_input":"2023-12-17T08:43:14.438357Z","iopub.status.idle":"2023-12-17T08:43:17.249161Z","shell.execute_reply.started":"2023-12-17T08:43:14.438329Z","shell.execute_reply":"2023-12-17T08:43:17.248378Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ensemble_accuracy","metadata":{"execution":{"iopub.status.busy":"2023-12-16T07:06:01.606398Z","iopub.execute_input":"2023-12-16T07:06:01.607326Z","iopub.status.idle":"2023-12-16T07:06:01.61356Z","shell.execute_reply.started":"2023-12-16T07:06:01.60729Z","shell.execute_reply":"2023-12-16T07:06:01.612426Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"execution":{"iopub.status.busy":"2023-12-16T15:41:18.784722Z","iopub.execute_input":"2023-12-16T15:41:18.785551Z","iopub.status.idle":"2023-12-16T15:41:18.794058Z","shell.execute_reply.started":"2023-12-16T15:41:18.785516Z","shell.execute_reply":"2023-12-16T15:41:18.793188Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def img_generator(train,test):\n    train_datagen=ImageDataGenerator(rescale=1./255, validation_split=0.2,horizontal_flip=True)\n\n    train_generator=train_datagen.flow_from_dataframe(dataframe=df_train_train,\n                                                      directory=\"./train_images_resized_preprocessed/\",\n                                                      x_col=\"file_name\",\n                                                      y_col=\"diagnosis\",\n                                                      batch_size=BATCH_SIZE,\n                                                      class_mode=\"categorical\",\n                                                      target_size=(HEIGHT, WIDTH),\n                                                      subset='training')\n\n    valid_generator=train_datagen.flow_from_dataframe(dataframe=df_train_train,\n                                                      directory=\"./train_images_resized_preprocessed/\",\n                                                      x_col=\"file_name\",\n                                                      y_col=\"diagnosis\",\n                                                      batch_size=BATCH_SIZE,\n                                                      class_mode=\"categorical\",\n                                                      target_size=(HEIGHT, WIDTH),\n                                                      subset='validation')\n\n    test_datagen = ImageDataGenerator(rescale=1./255)\n    test_generator = test_datagen.flow_from_dataframe(dataframe=df_train_test,\n                                                      directory = \"./test_images_resized_preprocessed/\",\n                                                      x_col=\"file_name\",\n                                                      target_size=(HEIGHT, WIDTH),\n                                                      batch_size=1,\n                                                      shuffle=False,\n                                                      class_mode=None)\n\n    return train_generator,valid_generator,test_generator","metadata":{"execution":{"iopub.status.busy":"2024-01-30T12:37:26.616507Z","iopub.execute_input":"2024-01-30T12:37:26.616923Z","iopub.status.idle":"2024-01-30T12:37:26.626075Z","shell.execute_reply.started":"2024-01-30T12:37:26.616891Z","shell.execute_reply":"2024-01-30T12:37:26.624983Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_generator,valid_generator,test_generator = img_generator(df_train_train,df_train_test)","metadata":{"execution":{"iopub.status.busy":"2024-01-30T12:37:27.811896Z","iopub.execute_input":"2024-01-30T12:37:27.812276Z","iopub.status.idle":"2024-01-30T12:37:27.919967Z","shell.execute_reply.started":"2024-01-30T12:37:27.812247Z","shell.execute_reply":"2024-01-30T12:37:27.918974Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def create_model(input_shape, n_out):\n    input_tensor = Input(shape=input_shape)\n    base_model = applications.VGG16(weights='imagenet', include_top=False,input_tensor=input_tensor)\n    # base_model.load_weights('resnet50_weights_tf_dim_ordering_tf_kernels_notop.h5')\n\n    x = GlobalAveragePooling2D()(base_model.output)\n    x = Dropout(0.5)(x)\n    x = Dense(2048, activation='relu')(x)\n    x = Dropout(0.5)(x)\n    final_output = Dense(n_out, activation='softmax', name='final_output')(x)\n    model = Model(input_tensor, final_output)\n    return model","metadata":{"execution":{"iopub.status.busy":"2024-01-28T08:17:41.496781Z","iopub.execute_input":"2024-01-28T08:17:41.497634Z","iopub.status.idle":"2024-01-28T08:17:41.503584Z","shell.execute_reply.started":"2024-01-28T08:17:41.497599Z","shell.execute_reply":"2024-01-28T08:17:41.502623Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = create_model(input_shape=(HEIGHT, WIDTH, CANAL), n_out=N_CLASSES)\n\nfor layer in model.layers:\n    layer.trainable = False\n\nfor i in range(-5, 0):\n    model.layers[i].trainable = True\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2024-01-28T08:17:48.913591Z","iopub.execute_input":"2024-01-28T08:17:48.914315Z","iopub.status.idle":"2024-01-28T08:17:52.632772Z","shell.execute_reply.started":"2024-01-28T08:17:48.914282Z","shell.execute_reply":"2024-01-28T08:17:52.631822Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"STEP_SIZE_TRAIN = train_generator.n//train_generator.batch_size\nSTEP_SIZE_VALID = valid_generator.n//valid_generator.batch_size\nprint(STEP_SIZE_TRAIN,STEP_SIZE_VALID)","metadata":{"execution":{"iopub.status.busy":"2024-01-28T08:18:05.874095Z","iopub.execute_input":"2024-01-28T08:18:05.874728Z","iopub.status.idle":"2024-01-28T08:18:05.879797Z","shell.execute_reply.started":"2024-01-28T08:18:05.874692Z","shell.execute_reply":"2024-01-28T08:18:05.878974Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# # for layer in model.layers:\n#     layer.trainable = True\n\n# es = EarlyStopping(monitor='val_loss', mode='min', patience=ES_PATIENCE, restore_best_weights=True, verbose=1)\n# rlrop = ReduceLROnPlateau(monitor='val_loss', mode='min', patience=RLROP_PATIENCE, factor=DECAY_DROP, min_lr=1e-6, verbose=1)\n\n# callback_list = [ rlrop]\n# optimizer = optimizers.Adam(lr=LEARNING_RATE)\n# model.compile(optimizer=optimizer, loss=\"categorical_crossentropy\",  metrics=['accuracy'])\n# model.summary()","metadata":{"execution":{"iopub.status.busy":"2023-12-15T19:03:42.022137Z","iopub.execute_input":"2023-12-15T19:03:42.02248Z","iopub.status.idle":"2023-12-15T19:03:42.121282Z","shell.execute_reply.started":"2023-12-15T19:03:42.022447Z","shell.execute_reply":"2023-12-15T19:03:42.120446Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class MyCallback(keras.callbacks.Callback):\n    def __init__(self, model, patience, stop_patience, threshold, factor, batches, epochs, ask_epoch):\n        super(MyCallback, self).__init__()\n        self.model = model\n        self.patience = patience # specifies how many epochs without improvement before learning rate is adjusted\n        self.stop_patience = stop_patience # specifies how many times to adjust lr without improvement to stop training\n        self.threshold = threshold # specifies training accuracy threshold when lr will be adjusted based on validation loss\n        self.factor = factor # factor by which to reduce the learning rate\n        self.batches = batches # number of training batch to run per epoch\n        self.epochs = epochs\n        self.ask_epoch = ask_epoch\n        self.ask_epoch_initial = ask_epoch # save this value to restore if restarting training\n\n        # callback variables\n        self.count = 0 # how many times lr has been reduced without improvement\n        self.stop_count = 0\n        self.best_epoch = 1   # epoch with the lowest loss\n        self.initial_lr = float(tf.keras.backend.get_value(model.optimizer.lr)) # get the initial learning rate and save it\n        self.highest_tracc = 0.0 # set highest training accuracy to 0 initially\n        self.lowest_vloss = np.inf # set lowest validation loss to infinity initially\n        self.best_weights = self.model.get_weights() # set best weights to model's initial weights\n        self.initial_weights = self.model.get_weights()   # save initial weights if they have to get restored\n\n    # Define a function that will run when train begins\n    def on_train_begin(self, logs= None):\n        msg = 'Do you want model asks you to halt the training [y/n] ?'\n        print(msg)\n        ans = input('')\n        if ans in ['Y', 'y']:\n            self.ask_permission = 1\n        elif ans in ['N', 'n']:\n            self.ask_permission = 0\n\n        msg = '{0:^8s}{1:^10s}{2:^9s}{3:^9s}{4:^9s}{5:^9s}{6:^9s}{7:^10s}{8:10s}{9:^8s}'.format('Epoch', 'Loss', 'Accuracy', 'V_loss', 'V_acc', 'LR', 'Next LR', 'Monitor','% Improv', 'Duration')\n        print(msg)\n        self.start_time = time.time()\n\n\n    def on_train_end(self, logs= None):\n        stop_time = time.time()\n        tr_duration = stop_time - self.start_time\n        hours = tr_duration // 3600\n        minutes = (tr_duration - (hours * 3600)) // 60\n        seconds = tr_duration - ((hours * 3600) + (minutes * 60))\n\n        msg = f'training elapsed time was {str(hours)} hours, {minutes:4.1f} minutes, {seconds:4.2f} seconds)'\n        print(msg)\n\n        # set the weights of the model to the best weights\n        self.model.set_weights(self.best_weights)\n\n\n    def on_train_batch_end(self, batch, logs= None):\n        # get batch accuracy and loss\n        acc = logs.get('accuracy') * 100\n        loss = logs.get('loss')\n\n        # prints over on the same line to show running batch count\n        msg = '{0:20s}processing batch {1:} of {2:5s}-   accuracy=  {3:5.3f}   -   loss: {4:8.5f}'.format(' ', str(batch), str(self.batches), acc, loss)\n#         print(msg, '\\r', end= '')\n\n\n    def on_epoch_begin(self, epoch, logs= None):\n        self.ep_start = time.time()\n\n\n    # Define method runs on the end of each epoch\n    def on_epoch_end(self, epoch, logs= None):\n        ep_end = time.time()\n        duration = ep_end - self.ep_start\n\n        lr = float(tf.keras.backend.get_value(self.model.optimizer.lr)) # get the current learning rate\n        current_lr = lr\n        acc = logs.get('accuracy')  # get training accuracy\n        v_acc = logs.get('val_accuracy')  # get validation accuracy\n        loss = logs.get('loss')  # get training loss for this epoch\n        v_loss = logs.get('val_loss')  # get the validation loss for this epoch\n\n        if acc < self.threshold: # if training accuracy is below threshold adjust lr based on training accuracy\n            monitor = 'accuracy'\n            if epoch == 0:\n                pimprov = 0.0\n            else:\n                pimprov = (acc - self.highest_tracc ) * 100 / self.highest_tracc # define improvement of model progres\n\n            if acc > self.highest_tracc: # training accuracy improved in the epoch\n                self.highest_tracc = acc # set new highest training accuracy\n                self.best_weights = self.model.get_weights() # training accuracy improved so save the weights\n                self.count = 0 # set count to 0 since training accuracy improved\n                self.stop_count = 0 # set stop counter to 0\n                if v_loss < self.lowest_vloss:\n                    self.lowest_vloss = v_loss\n                self.best_epoch = epoch + 1  # set the value of best epoch for this epoch\n\n            else:\n                # training accuracy did not improve check if this has happened for patience number of epochs\n                # if so adjust learning rate\n                if self.count >= self.patience - 1: # lr should be adjusted\n                    lr = lr * self.factor # adjust the learning by factor\n                    tf.keras.backend.set_value(self.model.optimizer.lr, lr) # set the learning rate in the optimizer\n                    self.count = 0 # reset the count to 0\n                    self.stop_count = self.stop_count + 1 # count the number of consecutive lr adjustments\n                    self.count = 0 # reset counter\n                    if v_loss < self.lowest_vloss:\n                        self.lowest_vloss = v_loss\n                else:\n                    self.count = self.count + 1 # increment patience counter\n\n        else: # training accuracy is above threshold so adjust learning rate based on validation loss\n            monitor = 'val_loss'\n            if epoch == 0:\n                pimprov = 0.0\n\n            else:\n                pimprov = (self.lowest_vloss - v_loss ) * 100 / self.lowest_vloss\n\n            if v_loss < self.lowest_vloss: # check if the validation loss improved\n                self.lowest_vloss = v_loss # replace lowest validation loss with new validation loss\n                self.best_weights = self.model.get_weights() # validation loss improved so save the weights\n                self.count = 0 # reset count since validation loss improved\n                self.stop_count = 0\n                self.best_epoch = epoch + 1 # set the value of the best epoch to this epoch\n\n            else: # validation loss did not improve\n                if self.count >= self.patience - 1: # need to adjust lr\n                    lr = lr * self.factor # adjust the learning rate\n                    self.stop_count = self.stop_count + 1 # increment stop counter because lr was adjusted\n                    self.count = 0 # reset counter\n                    tf.keras.backend.set_value(self.model.optimizer.lr, lr) # set the learning rate in the optimizer\n\n                else:\n                    self.count = self.count + 1 # increment the patience counter\n\n                if acc > self.highest_tracc:\n                    self.highest_tracc = acc\n\n        msg = f'{str(epoch + 1):^3s}/{str(self.epochs):4s} {loss:^9.3f}{acc * 100:^9.3f}{v_loss:^9.5f}{v_acc * 100:^9.3f}{current_lr:^9.5f}{lr:^9.5f}{monitor:^11s}{pimprov:^10.2f}{duration:^8.2f}'\n        print(msg)\n\n        if self.stop_count > self.stop_patience - 1: # check if learning rate has been adjusted stop_count times with no improvement\n            msg = f' training has been halted at epoch {epoch + 1} after {self.stop_patience} adjustments of learning rate with no improvement'\n            print(msg)\n            self.model.stop_training = True # stop training\n\n        else:\n            if self.ask_epoch != None and self.ask_permission != 0:\n                if epoch + 1 >= self.ask_epoch:\n                    msg = 'enter H to halt training or an integer for number of epochs to run then ask again'\n                    print(msg)\n\n                    ans = input('')\n                    if ans == 'H' or ans == 'h':\n                        msg = f'training has been halted at epoch {epoch + 1} due to user input'\n                        print(msg)\n                        self.model.stop_training = True # stop training\n\n                    else:\n                        try:\n                            ans = int(ans)\n                            self.ask_epoch += ans\n                            msg = f' training will continue until epoch {str(self.ask_epoch)}'\n                            print(msg)\n                            msg = '{0:^8s}{1:^10s}{2:^9s}{3:^9s}{4:^9s}{5:^9s}{6:^9s}{7:^10s}{8:10s}{9:^8s}'.format('Epoch', 'Loss', 'Accuracy', 'V_loss', 'V_acc', 'LR', 'Next LR', 'Monitor', '% Improv', 'Duration')\n                            print(msg)\n\n                        except Exception:\n                            print('Invalid')","metadata":{"execution":{"iopub.status.busy":"2024-01-30T12:37:34.262246Z","iopub.execute_input":"2024-01-30T12:37:34.263068Z","iopub.status.idle":"2024-01-30T12:37:34.307679Z","shell.execute_reply.started":"2024-01-30T12:37:34.263019Z","shell.execute_reply":"2024-01-30T12:37:34.306594Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras import regularizers\nfrom tensorflow.keras.layers import Conv2D, MaxPooling2D, Flatten, Dense, Activation, Dropout, BatchNormalization\nimport tensorflow as tf\nfrom tensorflow.keras.optimizers import Adam, Adamax\nimport time","metadata":{"execution":{"iopub.status.busy":"2024-01-30T12:37:35.441812Z","iopub.execute_input":"2024-01-30T12:37:35.442213Z","iopub.status.idle":"2024-01-30T12:37:35.449245Z","shell.execute_reply.started":"2024-01-30T12:37:35.442181Z","shell.execute_reply":"2024-01-30T12:37:35.447949Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Create Model Structure\nimg_size = (224, 224)\nchannels = 3\nimg_shape = (224, 224, channels)\nclass_count = len(list(train_generator.class_indices.keys())) # to define number of classes in dense layer\n\n# create pre-trained model (you can built on pretrained model such as :  efficientnet, VGG , Resnet )\n# we will use efficientnetb3 from EfficientNet family.\nbase_model = tf.keras.applications.resnet.ResNet152(include_top= False, weights= \"imagenet\", input_shape= img_shape, pooling= 'max')\n\nmodel = Sequential([\n    base_model,\n    BatchNormalization(axis= -1, momentum= 0.99, epsilon= 0.001),\n    Dense(256, kernel_regularizer= regularizers.l2(l= 0.016), activity_regularizer= regularizers.l1(0.006),\n                bias_regularizer= regularizers.l1(0.006), activation= 'relu'),\n    Dropout(rate= 0.45, seed= 123),\n    Dense(class_count, activation= 'softmax')\n])\n# model = Sequential([\n#     Conv2D(32, (3, 3), activation='relu', padding='same', input_shape=(224, 224, 3)),\n#     Conv2D(32, (3, 3), activation='relu', padding='same'),\n#     MaxPooling2D(pool_size=(2, 2)),\n#     Dropout(0.25),\n#     Conv2D(64, (3, 3), activation='relu', padding='same'),\n#     Conv2D(64, (3, 3), activation='relu', padding='same'),\n#     MaxPooling2D(pool_size=(2, 2)),\n#     Dropout(0.25),\n#     Conv2D(128, (3, 3), activation='relu', padding='same'),\n#     Conv2D(128, (3, 3), activation='relu', padding='same'),\n#     MaxPooling2D(pool_size=(2, 2)),\n#     Dropout(0.25),\n#     GlobalAveragePooling2D(),  # Added Global Average Pooling layer\n#     Dense(512, activation='relu'),\n#     Dropout(0.5),\n#     Dense(5, activation='softmax')\n# ])\n\nmodel.compile(Adamax(learning_rate= 0.001), loss= 'categorical_crossentropy', metrics= ['accuracy'])\n\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2024-01-30T12:37:39.067384Z","iopub.execute_input":"2024-01-30T12:37:39.068354Z","iopub.status.idle":"2024-01-30T12:37:49.08559Z","shell.execute_reply.started":"2024-01-30T12:37:39.068312Z","shell.execute_reply":"2024-01-30T12:37:49.084675Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"batch_size = 8   # set batch size for training\nepochs = 40   # number of all epochs in training\npatience = 1   #number of epochs to wait to adjust lr if monitored value does not improve\nstop_patience = 3   # number of epochs to wait before stopping training if monitored value does not improve\nthreshold = 0.9   # if train accuracy is < threshold adjust monitor accuracy, else monitor validation loss\nfactor = 0.5   # factor to reduce lr by\nask_epoch = 5   # number of epochs to run before asking if you want to halt training\nbatches = int(np.ceil(len(train_generator.labels) / batch_size))    # number of training batch to run per epoch\n\ncallbacks = [MyCallback(model= model, patience= patience, stop_patience= stop_patience, threshold= threshold,\n            factor= factor, batches= batches, epochs= epochs, ask_epoch= ask_epoch )]\n","metadata":{"execution":{"iopub.status.busy":"2024-01-30T12:38:27.399723Z","iopub.execute_input":"2024-01-30T12:38:27.400133Z","iopub.status.idle":"2024-01-30T12:38:28.327381Z","shell.execute_reply.started":"2024-01-30T12:38:27.4001Z","shell.execute_reply":"2024-01-30T12:38:28.326264Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model.fit(x= train_generator, epochs= epochs, verbose= 1, callbacks= callbacks,\n                    validation_data= valid_generator, validation_steps= None, shuffle= False)","metadata":{"execution":{"iopub.status.busy":"2024-01-30T12:38:30.473461Z","iopub.execute_input":"2024-01-30T12:38:30.473893Z","iopub.status.idle":"2024-01-30T13:52:30.152844Z","shell.execute_reply.started":"2024-01-30T12:38:30.473861Z","shell.execute_reply":"2024-01-30T13:52:30.151751Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"complete_datagen = ImageDataGenerator(rescale=1./255)\ncomplete_generator = complete_datagen.flow_from_dataframe(dataframe=df_train_train,\n                                                          directory = \"./train_images_resized_preprocessed/\",\n                                                          x_col=\"file_name\",\n                                                          target_size=(HEIGHT, WIDTH),\n                                                          batch_size=1,\n                                                          shuffle=False,\n                                                          class_mode=None)\n\nSTEP_SIZE_COMPLETE = complete_generator.n//complete_generator.batch_size\ntrain_preds = model.predict_generator(complete_generator, steps=STEP_SIZE_COMPLETE,verbose = 1)\ntrain_preds = [np.argmax(pred) for pred in train_preds]","metadata":{"execution":{"iopub.status.busy":"2024-01-30T13:56:54.506481Z","iopub.execute_input":"2024-01-30T13:56:54.506916Z","iopub.status.idle":"2024-01-30T13:58:26.643471Z","shell.execute_reply.started":"2024-01-30T13:56:54.506883Z","shell.execute_reply":"2024-01-30T13:58:26.642472Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"complete_datagen = ImageDataGenerator(rescale=1./255)\ncomplete_generator = complete_datagen.flow_from_dataframe(dataframe=df_train_train,\n                                                          directory = \"./train_images_resized_preprocessed/\",\n                                                          x_col=\"file_name\",\n                                                          target_size=(HEIGHT, WIDTH),\n                                                          batch_size=1,\n                                                          shuffle=False,\n                                                          class_mode=None)\n\nSTEP_SIZE_COMPLETE = complete_generator.n//complete_generator.batch_size\ntrain_preds = model.predict_generator(complete_generator, steps=STEP_SIZE_COMPLETE,verbose = 1)\ntrain_preds = [np.argmax(pred) for pred in train_preds]","metadata":{"execution":{"iopub.status.busy":"2024-01-30T13:58:26.645405Z","iopub.execute_input":"2024-01-30T13:58:26.645787Z","iopub.status.idle":"2024-01-30T13:59:56.861213Z","shell.execute_reply.started":"2024-01-30T13:58:26.645758Z","shell.execute_reply":"2024-01-30T13:59:56.859884Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"Train Cohen Kappa score: %.3f\" % cohen_kappa_score(train_preds, df_train_train['diagnosis'].astype('int'), weights='quadratic'))\nprint(\"Train Accuracy score : %.3f\" % accuracy_score(df_train_train['diagnosis'].astype('int'),train_preds))","metadata":{"execution":{"iopub.status.busy":"2024-01-30T14:00:08.277001Z","iopub.execute_input":"2024-01-30T14:00:08.277392Z","iopub.status.idle":"2024-01-30T14:00:08.292104Z","shell.execute_reply.started":"2024-01-30T14:00:08.277362Z","shell.execute_reply":"2024-01-30T14:00:08.290754Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_generator.reset()\nSTEP_SIZE_TEST = test_generator.n//test_generator.batch_size\ntest_preds = model.predict_generator(test_generator, steps=STEP_SIZE_TEST,verbose = 1)\ntest_labels = [np.argmax(pred) for pred in test_preds]","metadata":{"execution":{"iopub.status.busy":"2024-01-30T14:00:13.189926Z","iopub.execute_input":"2024-01-30T14:00:13.19034Z","iopub.status.idle":"2024-01-30T14:00:35.880637Z","shell.execute_reply.started":"2024-01-30T14:00:13.190309Z","shell.execute_reply":"2024-01-30T14:00:35.879703Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def plot_conf_matrix(true,pred,classes):\n    cf = confusion_matrix(true, pred)\n\n    df_cm = pd.DataFrame(cf, range(len(classes)), range(len(classes)))\n    plt.figure(figsize=(8,5.5))\n    sns.set(font_scale=1.4)\n    sns.heatmap(df_cm, annot=True, annot_kws={\"size\": 16},xticklabels = classes ,yticklabels = classes,fmt='g')\n    #sns.heatmap(df_cm, annot=True, annot_kws={\"size\": 16})\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2024-01-28T09:07:59.682848Z","iopub.execute_input":"2024-01-28T09:07:59.68316Z","iopub.status.idle":"2024-01-28T09:07:59.68921Z","shell.execute_reply.started":"2024-01-28T09:07:59.683117Z","shell.execute_reply":"2024-01-28T09:07:59.688309Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"Test Cohen Kappa score: %.3f\" % cohen_kappa_score(test_labels, df_train_test['diagnosis'].astype('int'), weights='quadratic'))\n","metadata":{"execution":{"iopub.status.busy":"2024-01-28T09:08:01.371969Z","iopub.execute_input":"2024-01-28T09:08:01.372361Z","iopub.status.idle":"2024-01-28T09:08:01.381032Z","shell.execute_reply.started":"2024-01-28T09:08:01.372329Z","shell.execute_reply":"2024-01-28T09:08:01.379975Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"Test Cohen Kappa score: %.3f\" % cohen_kappa_score(test_labels, df_train_test['diagnosis'].astype('int'), weights='quadratic'))\nprint(\"Test Accuracy score : %.3f\" % accuracy_score(df_train_test['diagnosis'].astype('int'),test_labels))","metadata":{"execution":{"iopub.status.busy":"2024-01-30T14:00:48.459791Z","iopub.execute_input":"2024-01-30T14:00:48.460168Z","iopub.status.idle":"2024-01-30T14:00:48.472454Z","shell.execute_reply.started":"2024-01-30T14:00:48.460137Z","shell.execute_reply":"2024-01-30T14:00:48.471175Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"raw","source":"","metadata":{"execution":{"iopub.status.busy":"2024-01-27T13:34:18.988958Z","iopub.execute_input":"2024-01-27T13:34:18.989336Z","iopub.status.idle":"2024-01-27T13:34:21.403624Z","shell.execute_reply.started":"2024-01-27T13:34:18.98931Z","shell.execute_reply":"2024-01-27T13:34:21.402811Z"}}},{"cell_type":"code","source":"model.save('resnet152.h5')","metadata":{"execution":{"iopub.status.busy":"2024-01-30T14:01:09.363045Z","iopub.execute_input":"2024-01-30T14:01:09.363792Z","iopub.status.idle":"2024-01-30T14:01:12.222561Z","shell.execute_reply.started":"2024-01-30T14:01:09.363754Z","shell.execute_reply":"2024-01-30T14:01:12.221653Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_1 = load_model('/kaggle/input/genetic-models/vgg16.h5')\nvgg16 = Model(inputs=model_1.inputs,\n                outputs=model_1.outputs,\n                name='name_of_model_1')\nmodel_2 = load_model('/kaggle/input/genetic-models/resnet50.h5')\nresnet50 = Model(inputs=model_2.inputs,\n                outputs=model_2.outputs,\n                name='name_of_model_2')\nmodel_3 = load_model('/kaggle/input/genetic-models/resnet152.h5')\nresnet152 = Model(inputs=model_1.inputs,\n                outputs=model_1.outputs,\n                name='name_of_model_1')\nmodel_4 = load_model('/kaggle/input/genetic-models/inceptionv3.h5')\ninceptionv3 = Model(inputs=model_2.inputs,\n                outputs=model_2.outputs,\n                name='name_of_model_2')\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"check={}\n","metadata":{"execution":{"iopub.status.busy":"2023-12-16T16:04:42.901884Z","iopub.execute_input":"2023-12-16T16:04:42.902412Z","iopub.status.idle":"2023-12-16T16:04:42.906744Z","shell.execute_reply.started":"2023-12-16T16:04:42.902359Z","shell.execute_reply":"2023-12-16T16:04:42.905655Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"new_df=df_train_train.sample(n=100)","metadata":{"execution":{"iopub.status.busy":"2023-12-16T16:06:52.883871Z","iopub.execute_input":"2023-12-16T16:06:52.88464Z","iopub.status.idle":"2023-12-16T16:06:52.897295Z","shell.execute_reply.started":"2023-12-16T16:06:52.884605Z","shell.execute_reply":"2023-12-16T16:06:52.896214Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"datagen = ImageDataGenerator(\n    rescale=1.0/255.0,  # Rescale pixel values to [0, 1]\n)\ntrain_data_generator = datagen.flow_from_dataframe(dataframe=new_df,\n                                                      directory=\"./train_images_resized_preprocessed/\",\n                                                      x_col=\"file_name\",\n                                                      y_col=\"diagnosis\",\n                                                      batch_size=BATCH_SIZE,\n                                                      class_mode=\"categorical\",\n                                                      target_size=(224, 224),\n                                                      subset='training')","metadata":{"execution":{"iopub.status.busy":"2023-12-16T16:06:55.895851Z","iopub.execute_input":"2023-12-16T16:06:55.896226Z","iopub.status.idle":"2023-12-16T16:06:55.908371Z","shell.execute_reply.started":"2023-12-16T16:06:55.896195Z","shell.execute_reply":"2023-12-16T16:06:55.907411Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def evaluate_models(model_combination):\n\n    input_tensor = Input(shape=(224, 224, 3))\n    model_combination=list(set(model_combination))\n    model_combination.sort()\n    print(model_combination)\n    if(tuple(model_combination) in check):\n        print(check[tuple(model_combination)])\n        return check[tuple(model_combination)]\n\n    # Add global average pooling layer\n\n    pretrained_models=[]\n    for i in model_combination:\n      if i==\"V3\":\n        pretrained_models.append(inceptionv3)\n      elif i==\"Res50\":\n        pretrained_models.append(resnet50)\n      elif i==\"vgg\":\n        pretrained_models.append(vgg16)\n      elif i==\"Res152\":\n        pretrained_models.append(resnet152)\n\n\n    # Concatenate the outputs\n\n    predictions = [model.predict(train_data_generator) for model in pretrained_models]\n    \n\n# Combine predictions using averaging\n    average_prediction = np.mean(predictions, axis=0)\n\n    # Convert probabilities to class labels\n    ensemble_predictions = np.argmax(average_prediction, axis=1)\n\n    # Retrieve true labels from the generator\n    true_labels = train_data_generator.classes\n\n    # Calculate accuracy of the ensemble model\n    ensemble_accuracy = np.mean(ensemble_predictions == true_labels)\n    # Create the stacked ensemble model\n    # stacked_ensemble_model = StackingClassifier(estimators=pretrained_models, final_estimator=MLPClassifier())\n\n    # # Compile the model\n    # # stacked_ensemble_model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])\n    # stacked_ensemble_model.fit(train_data_generator.next()[0], train_data_generator.next()[1])\n    # # Evaluate the model on train data (This is probably not intended, but you can replace it with test data)\n    # test_accuracy = stacked_ensemble_model.score(train_data_generator.next()[0], train_data_generator.next()[1]\n    check[tuple(model_combination)]=ensemble_accuracy\n    print(ensemble_accuracy)\n    return ensemble_accuracy","metadata":{"execution":{"iopub.status.busy":"2023-12-16T16:07:05.433976Z","iopub.execute_input":"2023-12-16T16:07:05.43486Z","iopub.status.idle":"2023-12-16T16:07:05.444149Z","shell.execute_reply.started":"2023-12-16T16:07:05.434825Z","shell.execute_reply":"2023-12-16T16:07:05.443164Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}