{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.13","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"}],"dockerImageVersionId":30646,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"\n\nimport os\nprint(os.listdir('../input'))\n","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-04-22T08:14:23.779247Z","iopub.execute_input":"2024-04-22T08:14:23.780080Z","iopub.status.idle":"2024-04-22T08:14:23.791684Z","shell.execute_reply.started":"2024-04-22T08:14:23.780040Z","shell.execute_reply":"2024-04-22T08:14:23.790657Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np \nimport pandas as pd \nimport random \nimport cv2\nimport warnings\nimport matplotlib.pyplot as plt \nfrom tqdm.notebook import tqdm\nimport tensorflow as tf\nfrom sklearn.model_selection import train_test_split \nfrom sklearn.metrics import confusion_matrix, cohen_kappa_score\nfrom keras.preprocessing.image import ImageDataGenerator\nfrom keras.models import Model \nfrom keras.callbacks import EarlyStopping, ReduceLROnPlateau\nfrom keras.layers import Dense, Dropout,Flatten , GlobalAveragePooling2D, BatchNormalization \n\nfrom keras import optimizers, applications\nfrom keras import layers \nfrom keras.applications.inception_v3 import InceptionV3 \nfrom keras.applications.densenet import DenseNet169  , DenseNet121\nfrom keras.applications import VGG16, ResNet50 , MobileNet , Xception\nfrom keras.models import Sequential \n\n#set seeds to make the experiment more reproducible \n \ndef seed_everything (seed=0) :\n    random.seed(seed)\n    os.environ['PYTHONHASHSEED']=str(seed)\n    np.random.seed(seed)\n    tf.random.set_seed(seed)\nseed =0 \nseed_everything(seed)\n\nwarnings.filterwarnings(\"ignore\")\n\n","metadata":{"execution":{"iopub.status.busy":"2024-04-22T08:15:32.249384Z","iopub.execute_input":"2024-04-22T08:15:32.250735Z","iopub.status.idle":"2024-04-22T08:15:32.260035Z","shell.execute_reply.started":"2024-04-22T08:15:32.250698Z","shell.execute_reply":"2024-04-22T08:15:32.259030Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\ndata_path ='../input/aptos2019-blindness-detection'\ntrain_img_path =os.path.join(data_path , 'train_images')\ntrain_label_path = os.path.join(data_path , 'train.csv')\ndf_train = pd.read_csv(train_label_path) \nprint('num of train images ', len(os.listdir(train_img_path)))\n","metadata":{"execution":{"iopub.status.busy":"2024-04-22T08:15:38.440913Z","iopub.execute_input":"2024-04-22T08:15:38.441266Z","iopub.status.idle":"2024-04-22T08:15:38.726958Z","shell.execute_reply.started":"2024-04-22T08:15:38.441239Z","shell.execute_reply":"2024-04-22T08:15:38.725887Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train.head()","metadata":{"execution":{"iopub.status.busy":"2024-04-22T08:15:50.064354Z","iopub.execute_input":"2024-04-22T08:15:50.065125Z","iopub.status.idle":"2024-04-22T08:15:50.080791Z","shell.execute_reply.started":"2024-04-22T08:15:50.065091Z","shell.execute_reply":"2024-04-22T08:15:50.079781Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#calculate the counts of each diagnosis category \ndiagnosis_counts = df_train['diagnosis'].value_counts() \ndiagnosis_counts","metadata":{"execution":{"iopub.status.busy":"2024-04-22T08:15:54.239101Z","iopub.execute_input":"2024-04-22T08:15:54.239867Z","iopub.status.idle":"2024-04-22T08:15:54.254408Z","shell.execute_reply.started":"2024-04-22T08:15:54.239832Z","shell.execute_reply":"2024-04-22T08:15:54.253357Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import seaborn as sns \nsns.countplot(x=df_train['diagnosis'])","metadata":{"execution":{"iopub.status.busy":"2024-04-22T08:16:01.623991Z","iopub.execute_input":"2024-04-22T08:16:01.624703Z","iopub.status.idle":"2024-04-22T08:16:01.934308Z","shell.execute_reply.started":"2024-04-22T08:16:01.624671Z","shell.execute_reply":"2024-04-22T08:16:01.933400Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"label_dic = {\n    \"0\":\"No DR\",\n    \"1\" : \"Mild\",\n    \"2\":\"Moderate\",\n    \"3\":\"Severe\",\n    \"4\":\"Proliferative DR\"\n}\n\n#Check Some Samples and Their Label\n\nrows=3\ncols = 2\ncount = 0\n\nfig, axes = plt.subplots(nrows=rows, ncols=cols, figsize=(15,15))\n\nindx = random.sample(range(df_train.shape[0]),rows * cols)\n\nfor i in range(rows):\n    for j in range(cols):        \n        if count < len(indx):\n            img_path = \"/kaggle/input/aptos2019-blindness-detection/train_images/\"+df_train.iloc[indx[count],0]+\".png\"\n            img = cv2.imread(img_path)\n            img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)\n            axes[i, j].imshow(img)\n            axes[i,j].set_title(label_dic[str(df_train.iloc[indx[count],1])])\n            count+=1\n            ","metadata":{"execution":{"iopub.status.busy":"2024-04-17T22:36:25.976173Z","iopub.execute_input":"2024-04-17T22:36:25.976757Z","iopub.status.idle":"2024-04-17T22:36:31.339609Z","shell.execute_reply.started":"2024-04-17T22:36:25.976727Z","shell.execute_reply":"2024-04-17T22:36:31.338198Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Image Processing**\n****","metadata":{}},{"cell_type":"code","source":"\nindx = random.randint(0,df_train.shape[0])\nimg = cv2.imread(\"/kaggle/input/aptos2019-blindness-detection/train_images/\"+df_train.id_code.iloc[indx]+\".png\")  \nimg = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) \nsigmaX=10 \ndef crop_image(img,low_bound=7):\n    if img.ndim ==2:\n        mask = img>low_bound\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>low_bound\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    \n    img = cv2.resize(img, (224,224))        \n    # print(img.shape)\n    return img\n\n","metadata":{"execution":{"iopub.status.busy":"2024-04-22T08:16:21.443462Z","iopub.execute_input":"2024-04-22T08:16:21.444401Z","iopub.status.idle":"2024-04-22T08:16:21.572181Z","shell.execute_reply.started":"2024-04-22T08:16:21.444365Z","shell.execute_reply":"2024-04-22T08:16:21.571137Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Display the image using matplotlib\nplt.imshow(img)\nplt.axis('off')  # Hide the axis\n\nplt.show()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img = crop_image(img)\nplt.imshow(img)","metadata":{"execution":{"iopub.status.busy":"2024-04-22T08:18:52.648318Z","iopub.execute_input":"2024-04-22T08:18:52.648707Z","iopub.status.idle":"2024-04-22T08:18:53.014015Z","shell.execute_reply.started":"2024-04-22T08:18:52.648680Z","shell.execute_reply":"2024-04-22T08:18:53.013034Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Define directories\ntrain_input_dir = \"/kaggle/input/aptos2019-blindness-detection/train_images/\"\n\n# Update paths to use /kaggle/working/ directory\ntrain_output_dir = \"./train_preprocessed_images/\"\n\n# Create output directories if they don't exist\nos.makedirs(train_output_dir, exist_ok=True)\n\n# Preprocess images in the training directory\nfor filename in tqdm(os.listdir(train_input_dir)):\n    input_path = os.path.join(train_input_dir, filename)\n    output_path = os.path.join(train_output_dir, filename)\n\n    # Load the image\n    image = cv2.imread(input_path)\n\n    # Apply circular crop and resize\n    preprocessed_image = crop_image(image)\n\n    # Save the preprocessed image\n    cv2.imwrite(output_path, preprocessed_image)","metadata":{"execution":{"iopub.status.busy":"2024-04-17T22:36:52.178628Z","iopub.execute_input":"2024-04-17T22:36:52.179446Z","iopub.status.idle":"2024-04-17T22:47:50.801421Z","shell.execute_reply.started":"2024-04-17T22:36:52.179415Z","shell.execute_reply":"2024-04-17T22:47:50.800399Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"label_dic = {\n    \"0\":\"No DR\",\n    \"1\" : \"Mild\",\n    \"2\":\"Moderate\",\n    \"3\":\"Severe\",\n    \"4\":\"Proliferative DR\"\n}\n\n#Check Some Samples and Their Label\n\nrows=3\ncols = 2\ncount = 0\n\nfig, axes = plt.subplots(nrows=rows, ncols=cols, figsize=(15,15))\n\nindx = random.sample(range(df_train.shape[0]),rows * cols)\n\nfor i in range(rows):\n    for j in range(cols):        \n        if count < len(indx):\n            img_path =\"./train_preprocessed_images/\"+df_train.iloc[indx[count],0]+\".png\"\n            img = cv2.imread(img_path)\n            img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)\n            axes[i, j].imshow(img)\n            axes[i,j].set_title(label_dic[str(df_train.iloc[indx[count],1])])\n            count+=1","metadata":{"execution":{"iopub.status.busy":"2024-04-17T22:49:06.434163Z","iopub.execute_input":"2024-04-17T22:49:06.434818Z","iopub.status.idle":"2024-04-17T22:49:08.332462Z","shell.execute_reply.started":"2024-04-17T22:49:06.434785Z","shell.execute_reply":"2024-04-17T22:49:08.331229Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Preprocecss data\ndf_train[\"id_code\"] = df_train[\"id_code\"].apply(lambda x: x + \".png\")\ndf_train['diagnosis'] = df_train['diagnosis'].astype('str')","metadata":{"execution":{"iopub.status.busy":"2024-04-17T22:49:22.237902Z","iopub.execute_input":"2024-04-17T22:49:22.238295Z","iopub.status.idle":"2024-04-17T22:49:22.245555Z","shell.execute_reply.started":"2024-04-17T22:49:22.238264Z","shell.execute_reply":"2024-04-17T22:49:22.244595Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train , test_val = train_test_split(df_train , test_size=0.2 ,shuffle=True, random_state=seed)\nvalid , test = train_test_split(test_val, test_size = 0.5, shuffle = True, random_state = seed) \nprint(\"Train shape: \", train.shape)\nprint(\"Valid shape: \", valid.shape)\nprint(\"Test shape: \",test.shape)\n ","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Model parameters\nBATCH_SIZE = 32\nEPOCHS = 30\nWARMUP_EPOCHS = 20\nLEARNING_RATE = 1e-4\nWARMUP_LEARNING_RATE = 1e-3\nHEIGHT = 224\nWIDTH = 224\nCANAL = 3\nN_CLASSES = df_train['diagnosis'].nunique()\nES_PATIENCE = 5\nRLROP_PATIENCE = 3\nDECAY_DROP = 0.5","metadata":{"execution":{"iopub.status.busy":"2024-04-17T18:43:51.047302Z","iopub.execute_input":"2024-04-17T18:43:51.048155Z","iopub.status.idle":"2024-04-17T18:43:51.054070Z","shell.execute_reply.started":"2024-04-17T18:43:51.048118Z","shell.execute_reply":"2024-04-17T18:43:51.053172Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_datagen =ImageDataGenerator(rescale=1./255,rotation_range=360,horizontal_flip=True,vertical_flip=True)\ntrain_generator = train_datagen.flow_from_dataframe(dataframe =train , \n                                                   directory =\"./train_preprocessed_images/\",\n                                                   x_col =\"id_code\",\n                                                   y_col=\"diagnosis\",\n                                                   class_mode =\"categorical\",\n                                                   batch_size = BATCH_SIZE ,\n                                                   target_size =(HEIGHT , WIDTH) , \n                                                    seed =0 )\nvalidation_datagen = ImageDataGenerator(rescale=1./255)\n\nvalid_generator=validation_datagen.flow_from_dataframe(\n    dataframe= valid ,\n    directory=\"./train_preprocessed_images/\",\n    x_col=\"id_code\",\n    y_col=\"diagnosis\",\n    class_mode=\"categorical\", \n    batch_size=BATCH_SIZE,   \n    target_size=(HEIGHT, WIDTH),\n    seed=0)\n\ntest_datagen = ImageDataGenerator(rescale=1./255)\n\ntest_generator = test_datagen.flow_from_dataframe(  \n        dataframe= test,\n        directory = \"./train_preprocessed_images/\",\n        x_col=\"id_code\",\n        y_col=\"diagnosis\",\n        class_mode=\"categorical\",\n        batch_size=1,\n        shuffle=False,\n        target_size=(HEIGHT, WIDTH),\n        seed=0)","metadata":{"execution":{"iopub.status.busy":"2024-04-17T18:44:07.414474Z","iopub.execute_input":"2024-04-17T18:44:07.415179Z","iopub.status.idle":"2024-04-17T18:44:07.464089Z","shell.execute_reply.started":"2024-04-17T18:44:07.415143Z","shell.execute_reply":"2024-04-17T18:44:07.463336Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Xception =Xception(weights='imagenet',include_top=False ,input_shape=(224,224,3))\n","metadata":{"execution":{"iopub.status.busy":"2024-04-17T20:14:15.221581Z","iopub.execute_input":"2024-04-17T20:14:15.222198Z","iopub.status.idle":"2024-04-17T20:14:20.384733Z","shell.execute_reply.started":"2024-04-17T20:14:15.222169Z","shell.execute_reply":"2024-04-17T20:14:20.383907Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Xception.trainable=False","metadata":{"execution":{"iopub.status.busy":"2024-04-17T20:14:27.306432Z","iopub.execute_input":"2024-04-17T20:14:27.306808Z","iopub.status.idle":"2024-04-17T20:14:27.316594Z","shell.execute_reply.started":"2024-04-17T20:14:27.306780Z","shell.execute_reply":"2024-04-17T20:14:27.315544Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import keras \nmodel =Sequential()\nmodel.add(Xception)\nmodel.add(GlobalAveragePooling2D())\nmodel.add(Dropout(0.2))\nmodel.add(BatchNormalization())\nmodel.add(Dense(units = 5,activation='softmax',kernel_regularizer= keras.regularizers.l2(0.0001)))\n\n","metadata":{"execution":{"iopub.status.busy":"2024-04-17T20:14:41.288385Z","iopub.execute_input":"2024-04-17T20:14:41.288978Z","iopub.status.idle":"2024-04-17T20:14:41.745490Z","shell.execute_reply.started":"2024-04-17T20:14:41.288947Z","shell.execute_reply":"2024-04-17T20:14:41.744491Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for layer in model.layers : \n    layer.trainable = False \nfor i in range (-4,0) : \n    model.layers[i].trainable = True \noptimizer = optimizers.Adam(lr=WARMUP_LEARNING_RATE)\nmodel.compile(optimizer=optimizer , loss ='categorical_crossentropy',metrics='accuracy') \n\n","metadata":{"execution":{"iopub.status.busy":"2024-04-17T20:14:50.269213Z","iopub.execute_input":"2024-04-17T20:14:50.270042Z","iopub.status.idle":"2024-04-17T20:14:50.314487Z","shell.execute_reply.started":"2024-04-17T20:14:50.270008Z","shell.execute_reply":"2024-04-17T20:14:50.313295Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2024-04-17T20:14:58.871760Z","iopub.execute_input":"2024-04-17T20:14:58.872126Z","iopub.status.idle":"2024-04-17T20:14:58.907968Z","shell.execute_reply.started":"2024-04-17T20:14:58.872099Z","shell.execute_reply":"2024-04-17T20:14:58.907163Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.utils import plot_model\nfrom IPython.display import Image\nplot_model(model, to_file='convnet.png', show_shapes=True,show_layer_names=True)\nImage(filename='convnet.png')","metadata":{"execution":{"iopub.status.busy":"2024-04-17T20:15:03.789411Z","iopub.execute_input":"2024-04-17T20:15:03.789777Z","iopub.status.idle":"2024-04-17T20:15:04.118276Z","shell.execute_reply.started":"2024-04-17T20:15:03.789751Z","shell.execute_reply":"2024-04-17T20:15:04.117334Z"},"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\n\nhistory_warmup = model.fit_generator(generator=train_generator,\n                                     steps_per_epoch=STEP_SIZE_TRAIN,\n                                     validation_data=valid_generator,\n                                     validation_steps=STEP_SIZE_VALID,\n                                     epochs=WARMUP_EPOCHS,\n                                     verbose=1)\n\n #Evaluate the model on the validation data\nvalidation_metrics = model.evaluate_generator(generator=valid_generator,\n                                               steps=STEP_SIZE_VALID)\n\n# Display the validation loss and accuracy\nprint(\"Validation Loss:\", validation_metrics[0])\nprint(\"Validation Accuracy:\", validation_metrics[1])\n\n# Access the training history\nhistory_warmup = history_warmup.history\nfrom keras.callbacks import ModelCheckpoint \n# Save the weights before fine-tuning\nmodel.save_weights('pre_fine_tuning_weights.h5')","metadata":{"execution":{"iopub.status.busy":"2024-04-16T11:00:30.646686Z","iopub.execute_input":"2024-04-16T11:00:30.647856Z","iopub.status.idle":"2024-04-16T11:14:25.798911Z","shell.execute_reply.started":"2024-04-16T11:00:30.647817Z","shell.execute_reply":"2024-04-16T11:14:25.797700Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Load the weights saved before fine-tuning\nmodel.load_weights('pre_fine_tuning_weights.h5')","metadata":{"execution":{"iopub.status.busy":"2024-04-16T11:14:51.804297Z","iopub.execute_input":"2024-04-16T11:14:51.805191Z","iopub.status.idle":"2024-04-16T11:14:52.457883Z","shell.execute_reply.started":"2024-04-16T11:14:51.805136Z","shell.execute_reply":"2024-04-16T11:14:52.456856Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for layer in model.layers:\n    layer.trainable = True\n\n\nrlrop = ReduceLROnPlateau(monitor='val_loss', mode='min', patience=RLROP_PATIENCE, factor=DECAY_DROP, min_lr=1e-6, verbose=1)\ncheckpoint = keras.callbacks.ModelCheckpoint('../working/inception.h5', monitor='val_loss', verbose=1, \n                             save_best_only=True, mode='min', save_weights_only = True)\ncallback_list = [ rlrop,checkpoint]\noptimizer = optimizers.Adam(lr=LEARNING_RATE)\nmodel.compile(optimizer=optimizer, loss='categorical_crossentropy',  metrics='accuracy')\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2024-04-16T11:15:04.331975Z","iopub.execute_input":"2024-04-16T11:15:04.332703Z","iopub.status.idle":"2024-04-16T11:15:04.484384Z","shell.execute_reply.started":"2024-04-16T11:15:04.332667Z","shell.execute_reply":"2024-04-16T11:15:04.483306Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nhistory_finetunning = model.fit_generator(generator=train_generator,\n                                          steps_per_epoch=STEP_SIZE_TRAIN,\n                                          validation_data=valid_generator,\n                                          validation_steps=STEP_SIZE_VALID,\n                                          epochs=EPOCHS,\n                                          callbacks=callback_list,\n                                          verbose=1)\n# Evaluate the model on the validation data\nvalidation_metrics = model.evaluate_generator(generator=valid_generator,\n                                               steps=STEP_SIZE_VALID)\n\n# Display the validation loss and accuracy\nprint(\"Validation Loss:\", validation_metrics[0])\nprint(\"Validation Accuracy:\", validation_metrics[1])\n\n# Access the training history\nhistory_finetunning = history_finetunning.history \n","metadata":{"execution":{"iopub.status.busy":"2024-04-16T11:15:10.420950Z","iopub.execute_input":"2024-04-16T11:15:10.421353Z","iopub.status.idle":"2024-04-16T11:37:58.136863Z","shell.execute_reply.started":"2024-04-16T11:15:10.421320Z","shell.execute_reply":"2024-04-16T11:37:58.135691Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = {'loss': history_warmup['loss'] + history_finetunning['loss'], \n           'val_loss': history_warmup['val_loss'] + history_finetunning['val_loss'], \n           'acc': history_warmup['accuracy'] + history_finetunning['accuracy'], \n           'val_acc': history_warmup['val_accuracy'] + history_finetunning['val_accuracy']}\n\nsns.set_style(\"whitegrid\")\nfig, (ax1, ax2) = plt.subplots(2, 1, sharex='col', figsize=(20, 14))\n\nax1.plot(history['loss'], label='Train loss')\nax1.plot(history['val_loss'], label='Validation loss')\nax1.legend(loc='best')\nax1.set_title('Loss')\n\nax2.plot(history['acc'], label='Train accuracy')\nax2.plot(history['val_acc'], label='Validation accuracy')\nax2.legend(loc='best')\nax2.set_title('Accuracy')\n\nplt.xlabel('Epochs')\nsns.despine()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-04-16T11:38:50.122637Z","iopub.execute_input":"2024-04-16T11:38:50.123343Z","iopub.status.idle":"2024-04-16T11:38:50.910835Z","shell.execute_reply.started":"2024-04-16T11:38:50.123308Z","shell.execute_reply":"2024-04-16T11:38:50.909660Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Create empty arays to keep the predictions and labels\nlastFullTrainPred = np.empty((0, N_CLASSES))\nlastFullTrainLabels = np.empty((0, N_CLASSES))\nlastFullValPred = np.empty((0, N_CLASSES))\nlastFullValLabels = np.empty((0, N_CLASSES))\n\n# Add train predictions and labels\nfor i in range(STEP_SIZE_TRAIN+1):\n    im, lbl = next(train_generator)\n    scores = model.predict(im, batch_size=train_generator.batch_size)\n    lastFullTrainPred = np.append(lastFullTrainPred, scores, axis=0)\n    lastFullTrainLabels = np.append(lastFullTrainLabels, lbl, axis=0)\n\n# Add validation predictions and labels\nfor i in range(STEP_SIZE_VALID+1):\n    im, lbl = next(valid_generator)\n    scores = model.predict(im, batch_size=valid_generator.batch_size)\n    lastFullValPred = np.append(lastFullValPred, scores, axis=0)\n    lastFullValLabels = np.append(lastFullValLabels, lbl, axis=0)\n    \n    \nlastFullComPred = np.concatenate((lastFullTrainPred, lastFullValPred))\nlastFullComLabels = np.concatenate((lastFullTrainLabels, lastFullValLabels))\ncomplete_labels = [np.argmax(label) for label in lastFullComLabels]\n\ntrain_preds = [np.argmax(pred) for pred in lastFullTrainPred]\ntrain_labels = [np.argmax(label) for label in lastFullTrainLabels]\nvalidation_preds = [np.argmax(pred) for pred in lastFullValPred]\nvalidation_labels = [np.argmax(label) for label in lastFullValLabels]","metadata":{"execution":{"iopub.status.busy":"2024-04-16T11:39:01.911807Z","iopub.execute_input":"2024-04-16T11:39:01.912590Z","iopub.status.idle":"2024-04-16T11:40:09.417088Z","shell.execute_reply.started":"2024-04-16T11:39:01.912553Z","shell.execute_reply":"2024-04-16T11:40:09.416168Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig, (ax1, ax2) = plt.subplots(1, 2, sharex='col', figsize=(24, 7))\nlabels = ['0 - No DR', '1 - Mild', '2 - Moderate', '3 - Severe', '4 - Proliferative DR']\ntrain_cnf_matrix = confusion_matrix(train_labels, train_preds)\nvalidation_cnf_matrix = confusion_matrix(validation_labels, validation_preds)\n\ntrain_cnf_matrix_norm = train_cnf_matrix.astype('float') / train_cnf_matrix.sum(axis=1)[:, np.newaxis]\nvalidation_cnf_matrix_norm = validation_cnf_matrix.astype('float') / validation_cnf_matrix.sum(axis=1)[:, np.newaxis]\n\ntrain_df_cm = pd.DataFrame(train_cnf_matrix_norm, index=labels, columns=labels)\nvalidation_df_cm = pd.DataFrame(validation_cnf_matrix_norm, index=labels, columns=labels)\n\nsns.heatmap(train_df_cm, annot=True, fmt='.2f', cmap=\"Blues\", ax=ax1).set_title('Train')\nsns.heatmap(validation_df_cm, annot=True, fmt='.2f', cmap=sns.cubehelix_palette(8), ax=ax2).set_title('Validation')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-04-16T11:41:02.443747Z","iopub.execute_input":"2024-04-16T11:41:02.444942Z","iopub.status.idle":"2024-04-16T11:41:03.474335Z","shell.execute_reply.started":"2024-04-16T11:41:02.444899Z","shell.execute_reply":"2024-04-16T11:41:03.473254Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions = model.predict_generator(test_generator, steps = len(test_generator.filenames))\ny_pred = np.argmax(predictions, axis = 1)\n\n#Chack\nprint(predictions)\nprint(y_pred)","metadata":{"execution":{"iopub.status.busy":"2024-04-16T11:41:16.245252Z","iopub.execute_input":"2024-04-16T11:41:16.246042Z","iopub.status.idle":"2024-04-16T11:41:29.739510Z","shell.execute_reply.started":"2024-04-16T11:41:16.246005Z","shell.execute_reply":"2024-04-16T11:41:29.738172Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import classification_report\nfrom sklearn.preprocessing import LabelEncoder\n\n# Encode true labels if they are stored as strings\ntrue_labels = test['diagnosis']\nlabel_encoder = LabelEncoder()\ntrue_labels_encoded = label_encoder.fit_transform(true_labels)\n\n# Print the classification report\nprint(classification_report(true_labels_encoded, y_pred))","metadata":{"execution":{"iopub.status.busy":"2024-04-16T11:41:36.111665Z","iopub.execute_input":"2024-04-16T11:41:36.112455Z","iopub.status.idle":"2024-04-16T11:41:36.133404Z","shell.execute_reply.started":"2024-04-16T11:41:36.112417Z","shell.execute_reply":"2024-04-16T11:41:36.132163Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.save('my_model.h5')","metadata":{"execution":{"iopub.status.busy":"2024-04-16T11:51:45.080229Z","iopub.execute_input":"2024-04-16T11:51:45.081081Z","iopub.status.idle":"2024-04-16T11:51:47.013963Z","shell.execute_reply.started":"2024-04-16T11:51:45.081042Z","shell.execute_reply":"2024-04-16T11:51:47.012925Z"},"trusted":true},"execution_count":null,"outputs":[]}]}