{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"\n# # example of creating a CNN with an inception module\n# from keras.models import Model\n# from keras.layers import Input\n# from keras.layers import Conv2D\n# from keras.layers import MaxPooling2D\n# from keras.layers.merge import concatenate\n# from keras.utils.vis_utils import plot_model\n \n# # function for creating a naive inception block\n# def naive_inception_module(layer_in, f1, f2, f3):\n# \t# 1x1 conv\n# \tconv1 = Conv2D(f1, (1,1), padding='same', activation='relu')(layer_in)\n# \t# 3x3 conv\n# \tconv3 = Conv2D(f2, (3,3), padding='same', activation='relu')(layer_in)\n# \t# 5x5 conv\n# \tconv5 = Conv2D(f3, (5,5), padding='same', activation='relu')(layer_in)\n# \t# 3x3 max pooling\n# \tpool = MaxPooling2D((3,3), strides=(1,1), padding='same')(layer_in)\n# \t# concatenate filters, assumes filters/channels last\n# \tlayer_out = concatenate([conv1, conv3, conv5, pool], axis=-1)\n# \treturn layer_out\n \n# # define model input\n# visible = Input(shape=(256, 256, 3))\n# # add inception module\n# layer = naive_inception_module(visible, 64, 128, 32)\n# # create model\n# model = Model(inputs=visible, outputs=layer)\n# # summarize model\n# model.summary()\n# # plot model architecture\n# plot_model(model, show_shapes=True, to_file='naive_inception_module.png')","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nimport cv2\nimport random\nimport warnings\nimport numpy as np\nimport pandas as pd\nimport seaborn as sns\nimport matplotlib.pyplot as plt\nfrom sklearn.metrics import confusion_matrix, cohen_kappa_score\nfrom keras.models import Model\nfrom keras import optimizers, applications\nfrom keras.preprocessing.image import ImageDataGenerator\nfrom keras.callbacks import EarlyStopping, ReduceLROnPlateau\nfrom keras.layers import Dense, Dropout, GlobalAveragePooling2D, Input\nimport tensorflow as tf\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom tensorflow.keras.preprocessing import image_dataset_from_directory\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import InputLayer, BatchNormalization, Dropout, Flatten, Dense, Activation, MaxPool2D, Conv2D\nfrom tensorflow.keras.callbacks import EarlyStopping, ModelCheckpoint\nfrom tensorflow.keras.applications.resnet50 import ResNet50\nfrom tensorflow.keras.utils import to_categorical\nfrom keras import optimizers\nfrom tensorflow.keras.optimizers import Adam\nfrom keras.callbacks import Callback,ModelCheckpoint,ReduceLROnPlateau\nfrom keras.models import Sequential,load_model\nfrom keras.layers import Dense, Dropout\nfrom keras.wrappers.scikit_learn import KerasClassifier\nimport keras.backend as K\n#import tensorflow_addons as tfa\n#from tensorflow.keras.metrics import Metric\n#from tensorflow_addons.utils.types import AcceptableDTypes, FloatTensorLike\nfrom typeguard import typechecked\nfrom typing import Optional\n\nfrom tensorflow.keras.applications import ResNet101V2\n\n%matplotlib inline\nsns.set(style=\"whitegrid\")\nwarnings.filterwarnings(\"ignore\")","metadata":{"execution":{"iopub.status.busy":"2022-10-20T19:01:51.184167Z","iopub.execute_input":"2022-10-20T19:01:51.184435Z","iopub.status.idle":"2022-10-20T19:01:51.199778Z","shell.execute_reply.started":"2022-10-20T19:01:51.184407Z","shell.execute_reply":"2022-10-20T19:01:51.1987Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = pd.read_csv('../input/aptos2019-blindness-detection/train.csv')\ntest = pd.read_csv('../input/aptos2019-blindness-detection/test.csv')\nN_CLASSES = train['diagnosis'].nunique()\ntrain[\"id_code\"] = train[\"id_code\"].apply(lambda x: x + \".png\")\ntest[\"id_code\"] = test[\"id_code\"].apply(lambda x: x + \".png\")\ntrain['diagnosis'] = train['diagnosis'].astype('str')","metadata":{"execution":{"iopub.status.busy":"2022-10-20T19:01:51.662174Z","iopub.execute_input":"2022-10-20T19:01:51.662436Z","iopub.status.idle":"2022-10-20T19:01:51.688967Z","shell.execute_reply.started":"2022-10-20T19:01:51.662408Z","shell.execute_reply":"2022-10-20T19:01:51.688284Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_datagen=ImageDataGenerator(rescale=1./255, \n                                 validation_split=0.2,\n                                 horizontal_flip=True)\n\ntrain_generator=train_datagen.flow_from_dataframe(\n    dataframe=train,\n    directory=\"../input/aptos2019-blindness-detection/train_images/\",\n    x_col=\"id_code\",\n    y_col=\"diagnosis\",\n    batch_size=32,\n    class_mode=\"categorical\",\n    target_size=(224, 224),\n    subset='training')","metadata":{"execution":{"iopub.status.busy":"2022-10-20T19:01:51.840974Z","iopub.execute_input":"2022-10-20T19:01:51.841631Z","iopub.status.idle":"2022-10-20T19:01:53.398536Z","shell.execute_reply.started":"2022-10-20T19:01:51.841598Z","shell.execute_reply":"2022-10-20T19:01:53.397778Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"valid_generator=train_datagen.flow_from_dataframe(\n    dataframe=train,\n    directory=\"../input/aptos2019-blindness-detection/train_images/\",\n    x_col=\"id_code\",\n    y_col=\"diagnosis\",\n    batch_size=32,\n    class_mode=\"categorical\",    \n    target_size=(224, 224),\n    subset='validation')","metadata":{"execution":{"iopub.status.busy":"2022-10-20T19:01:53.400445Z","iopub.execute_input":"2022-10-20T19:01:53.400979Z","iopub.status.idle":"2022-10-20T19:01:53.441624Z","shell.execute_reply.started":"2022-10-20T19:01:53.40091Z","shell.execute_reply":"2022-10-20T19:01:53.440848Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_datagen = ImageDataGenerator(rescale=1./255)\n\ntest_generator = test_datagen.flow_from_dataframe(  \n        dataframe=test,\n        directory = \"../input/aptos2019-blindness-detection/test_images/\",\n        x_col=\"id_code\",\n        target_size=(224, 224),\n        batch_size=32,\n        shuffle=False,\n        class_mode=None)","metadata":{"execution":{"iopub.status.busy":"2022-10-20T19:01:53.442647Z","iopub.execute_input":"2022-10-20T19:01:53.442898Z","iopub.status.idle":"2022-10-20T19:01:56.610421Z","shell.execute_reply.started":"2022-10-20T19:01:53.442865Z","shell.execute_reply":"2022-10-20T19:01:56.609587Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.applications.inception_resnet_v2 import InceptionResNetV2\nfrom tensorflow.keras.layers import Input, MaxPooling2D, Dense, Flatten, Dropout\nfrom tensorflow.keras.models import Model\n\n\nbaseModel=InceptionResNetV2(include_top=False, input_tensor=Input(shape=(224, 224, 3)), weights='../input/inceptionresnetv2/inceptionresnetv2weightstfdimnotop.h5')\n\nfor layer in baseModel.layers:\n    layer.trainable=False\n    \ntransfer_model=baseModel.output\ntransfer_model=MaxPooling2D(pool_size=(4, 4))(transfer_model)\ntransfer_model=Flatten()(transfer_model)\ntransfer_model=Dense(64, activation='relu')(transfer_model)\ntransfer_model=Dropout(0.2)(transfer_model)\ntransfer_model=Dense(5, activation='softmax')(transfer_model)\n\nmodel = Model(inputs=baseModel.input, outputs=transfer_model)\nmodel.compile(optimizer=Adam(lr=0.00005),  # Very low learning rate\n              loss='binary_crossentropy',\n              metrics=['accuracy'])\n# metrics=[keras.metrics.BinaryAccuracy()])\n\n# model.compile(\n#         loss='binary_crossentropy',\n#         optimizer=Adam(lr=0.00005),\n#         metrics=['accuracy']\n#     )\n#  model.summary()","metadata":{"execution":{"iopub.status.busy":"2022-10-20T19:01:56.612228Z","iopub.execute_input":"2022-10-20T19:01:56.612976Z","iopub.status.idle":"2022-10-20T19:02:04.435301Z","shell.execute_reply.started":"2022-10-20T19:01:56.612938Z","shell.execute_reply":"2022-10-20T19:02:04.434556Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#taken from old keras source code\ndef f1_score(y_true, y_pred): \n    true_positives = K.sum(K.round(K.clip(y_true * y_pred, 0, 1)))\n    possible_positives = K.sum(K.round(K.clip(y_true, 0, 1)))\n    predicted_positives = K.sum(K.round(K.clip(y_pred, 0, 1)))\n    precision = true_positives / (predicted_positives + K.epsilon())\n    recall = true_positives / (possible_positives + K.epsilon())\n    f1_val = 2*(precision*recall)/(precision+recall+K.epsilon())\n    return f1_val","metadata":{"execution":{"iopub.status.busy":"2022-10-20T19:02:04.437028Z","iopub.execute_input":"2022-10-20T19:02:04.43728Z","iopub.status.idle":"2022-10-20T19:02:04.444627Z","shell.execute_reply.started":"2022-10-20T19:02:04.437248Z","shell.execute_reply":"2022-10-20T19:02:04.443608Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"METRICS = [\n      tf.keras.metrics.BinaryAccuracy(name='accuracy'),\n      tf.keras.metrics.Precision(name='precision'),\n      tf.keras.metrics.Recall(name='recall'),  \n      tf.keras.metrics.AUC(name='auc'),\n        f1_score,]","metadata":{"execution":{"iopub.status.busy":"2022-10-20T19:02:04.446006Z","iopub.execute_input":"2022-10-20T19:02:04.446515Z","iopub.status.idle":"2022-10-20T19:02:04.465298Z","shell.execute_reply.started":"2022-10-20T19:02:04.446475Z","shell.execute_reply":"2022-10-20T19:02:04.464652Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"lrd = ReduceLROnPlateau(monitor = 'val_loss',patience = 2,verbose = 1,factor = 0.8, min_lr = 1e-6)\n\nmcp = ModelCheckpoint('ResNet152V2.h5')\n\nes = EarlyStopping(verbose=1, patience=2)","metadata":{"execution":{"iopub.status.busy":"2022-10-20T19:02:04.467195Z","iopub.execute_input":"2022-10-20T19:02:04.467448Z","iopub.status.idle":"2022-10-20T19:02:04.471857Z","shell.execute_reply.started":"2022-10-20T19:02:04.467411Z","shell.execute_reply":"2022-10-20T19:02:04.470858Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(optimizer='Adam', loss=\"categorical_crossentropy\", metrics=METRICS)\nSTEP_SIZE_TRAIN = train_generator.n//train_generator.batch_size\nSTEP_SIZE_VALID = valid_generator.n//valid_generator.batch_size\nepoch = 10","metadata":{"execution":{"iopub.status.busy":"2022-10-20T19:02:04.473355Z","iopub.execute_input":"2022-10-20T19:02:04.473601Z","iopub.status.idle":"2022-10-20T19:02:04.497738Z","shell.execute_reply.started":"2022-10-20T19:02:04.473568Z","shell.execute_reply":"2022-10-20T19:02:04.49697Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"STEP_SIZE_TRAIN","metadata":{"execution":{"iopub.status.busy":"2022-10-20T14:51:37.188914Z","iopub.execute_input":"2022-10-20T14:51:37.190766Z","iopub.status.idle":"2022-10-20T14:51:37.199304Z","shell.execute_reply.started":"2022-10-20T14:51:37.190694Z","shell.execute_reply":"2022-10-20T14:51:37.198664Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"STEP_SIZE_VALID","metadata":{"execution":{"iopub.status.busy":"2022-10-20T14:51:37.203248Z","iopub.execute_input":"2022-10-20T14:51:37.205182Z","iopub.status.idle":"2022-10-20T14:51:37.213691Z","shell.execute_reply.started":"2022-10-20T14:51:37.20512Z","shell.execute_reply":"2022-10-20T14:51:37.2129Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = 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=epoch,\n                                callbacks=[mcp])\n#                               callbacks=[lrd,mcp,es])\n\n","metadata":{"execution":{"iopub.status.busy":"2022-10-20T14:51:37.21872Z","iopub.execute_input":"2022-10-20T14:51:37.219838Z","iopub.status.idle":"2022-10-20T14:54:31.363395Z","shell.execute_reply.started":"2022-10-20T14:51:37.219797Z","shell.execute_reply":"2022-10-20T14:54:31.361203Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"acc = history.history['accuracy']\nval_acc = history.history['val_acc']\nloss = history.history['loss']\nval_loss = history.history['val_loss']\n\nepochs_range = range(epoch)\n\nplt.figure(figsize=(15, 15))\nplt.rcParams.update({'font.size': 16})\nplt.subplot(2, 2, 1)\nplt.plot(epochs_range, acc, label='Training Accuracy', linewidth=2)\nplt.plot(epochs_range, val_acc, label='Validation Accuracy', linewidth=2)\nplt.legend(loc='lower right')\nplt.title('Training and Validation Accuracy')\n\nplt.subplot(2, 2, 2)\nplt.plot(epochs_range, loss, label='Training Loss', linewidth=2)\nplt.plot(epochs_range, val_loss, label='Validation Loss', linewidth=2)\nplt.legend(loc='upper right')\nplt.title('Training and Validation Loss')\nplt.show()\n\nplt.savefig('efficientnet_b5_acc_loss.png', dpi = 900)\nplt.savefig('efficientnet_b5_acc_loss.eps', dpi = 900)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(15, 15), linewidth = 5)\nplt.rcParams.update({'font.size': 22})\nplt.plot(epochs_range, acc, label='Training Accuracy', linewidth=2)\nplt.plot(epochs_range, val_acc, label='Validation Accuracy', linewidth=2)\nplt.legend(loc='lower right')\nplt.title('Training and Validation Accuracy')\n\nplt.savefig('efficientnet_b5_train_valid_acc.png', dpi = 900)\nplt.savefig('efficientnet_b5_train_valid_acc.eps', dpi = 900)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(15, 15))\nplt.rcParams.update({'font.size': 22})\nplt.plot(epochs_range, loss, label='Training Loss', linewidth=2)\nplt.plot(epochs_range, val_loss, label='Validation Loss', linewidth=2)\nplt.legend(loc='upper right')\nplt.title('Training and Validation Loss')\n\nplt.savefig('efficientnet_b5_train_valid_loss.png', dpi = 900)\nplt.savefig('efficientnet_b5_train_valid_loss.eps', dpi = 900)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"complete_datagen = ImageDataGenerator(rescale=1./255)\ncomplete_generator = complete_datagen.flow_from_dataframe(  \n        dataframe=train,\n        directory = \"../input/aptos2019-blindness-detection/train_images/\",\n        x_col=\"id_code\",\n        target_size=(224, 224),\n        batch_size=1,\n        shuffle=False,\n        class_mode=None)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"STEP_SIZE_COMPLETE = complete_generator.n//complete_generator.batch_size\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_preds = model.predict_generator(complete_generator, steps=STEP_SIZE_COMPLETE)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_preds = [np.argmax(pred) for pred in train_preds]","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# test_generator.reset()\n# STEP_SIZE_TEST = test_generator.n//test_generator.batch_size\n# preds = model.predict_generator(test_generator, steps=STEP_SIZE_TEST)\n# predictions = [np.argmax(pred) for pred in preds]\n# predictions[:10]","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_generator.reset()\nSTEP_SIZE_TEST = test_generator.n//test_generator.batch_size\npreds = model.predict_generator(test_generator)\npredictions = [np.argmax(pred) for pred in preds]\npredictions[:10]","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.save('./output/inception')","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.save('inceptionresnetv2.h5')","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels = ['0 - No DR', '1 - Mild', '2 - Moderate', '3 - Severe', '4 - Proliferative DR']\ncnf_matrix = confusion_matrix(train['diagnosis'].astype('int'), train_preds)\ncnf_matrix_norm = cnf_matrix.astype('float') / cnf_matrix.sum(axis=1)[:, np.newaxis]\ndf_cm = pd.DataFrame(cnf_matrix_norm, index=labels, columns=labels)\nprint(df_cm.describe().T)\nplt.figure(figsize=(15, 8))\nsns.heatmap(df_cm, annot=True, fmt='.2f')\nplt.show()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# print(len(predictions), len(filenames), len(preds), test_generator.n)\n# test","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"filenames=test_generator.filenames\nresults=pd.DataFrame({\"id_code\":filenames,\n                      \"diagnosis\":predictions})\nresults['id_code'] = results['id_code'].map(lambda x: str(x)[:-4])\nresults.to_csv(\"submission.csv\",index=False)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"> **Accuracy**","metadata":{}},{"cell_type":"code","source":"model.load_weights('./inceptionresnetv2.h5')\n\nres = model.evaluate(x_val, y_val)\nprint(\"Testing accuracy : \" + str(res[1]))\nprint(\"Testing loss : \" + str(res[0]))","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred_val = model.predict(x_val)\ny1 = pred_val > 0.4\ny1 = y1.astype(int).sum(axis=1) - 1\ny2 = y_val.sum(axis=1) - 1","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import classification_report\nprint(classification_report(y1, y2))","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import confusion_matrix\nimport seaborn as sns\nfrom sklearn.metrics import classification_report\n\nlabels=['No DR','Mild','Moderate','Severe','Proliferative DR']\n\nprint(classification_report(y1, y2, target_names=labels))","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import confusion_matrix\nimport seaborn as sns\ntarget_names=['No DR','Mild','Moderate','Severe','Proliferative DR']\n\ncm = confusion_matrix(y1, y2)\n# Normalise\ncmn = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis]\nfig, ax = plt.subplots(figsize=(20,16))\nplt.rcParams.update({'font.size': 22})\nplt.title('Confusion Matrix', fontsize=40)\nsns.heatmap(cmn, annot=True, fmt='.2f', xticklabels=target_names, yticklabels=target_names)\nplt.ylabel('True', fontsize=40)\nplt.xlabel('Predicted', fontsize=40)\nplt.savefig('cm_2.png', dpi=900)\nplt.savefig('cm_2.eps', dpi=900)\nplt.show(block=False)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import confusion_matrix\nimport seaborn as sns\ntarget_names=['No DR','Mild','Moderate','Severe','Proliferative DR']\n\ncm = confusion_matrix(y1, y2)\n\nfig, ax = plt.subplots(figsize=(20,16))\nax.set_xticklabels([''] + labels)\nax.set_yticklabels([''] + labels)\nplt.title('Confusion Matrix', fontsize=40)\nsns.heatmap(cm, annot=True, fmt='.0f', xticklabels=target_names, yticklabels=target_names)\nplt.ylabel('True', fontsize=30)\nplt.xlabel('Predicted', fontsize=40)\nplt.savefig('cm_3.png', dpi=900)\nplt.savefig('cm_3.eps', dpi=900)\nplt.show(block=False)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# ResNet101V2","metadata":{}},{"cell_type":"code","source":"from tensorflow.keras.applications import ResNet101V2\nfrom tensorflow.keras.layers import Input, MaxPooling2D, Dense, Flatten, Dropout\nfrom tensorflow.keras.models import Model\n\n\nbaseModel=ResNet101V2(include_top=False, input_tensor=Input(shape=(224, 224, 3)), weights='../input/keras-pretrain-model-weights/resnet101v2_weights_tf_dim_ordering_tf_kernels_notop.h5')\n\nfor layer in baseModel.layers:\n    layer.trainable=False\n    \ntransfer_model=baseModel.output\ntransfer_model=MaxPooling2D(pool_size=(4, 4))(transfer_model)\ntransfer_model=Flatten()(transfer_model)\ntransfer_model=Dense(64, activation='relu')(transfer_model)\ntransfer_model=Dropout(0.2)(transfer_model)\ntransfer_model=Dense(5, activation='softmax')(transfer_model)\n\nmodel = Model(inputs=baseModel.input, outputs=transfer_model)\nmodel.compile(optimizer=Adam(lr=0.00005),  # Very low learning rate\n              loss='binary_crossentropy',\n              metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2022-10-20T19:02:11.057986Z","iopub.execute_input":"2022-10-20T19:02:11.058537Z","iopub.status.idle":"2022-10-20T19:02:16.262104Z","shell.execute_reply.started":"2022-10-20T19:02:11.0585Z","shell.execute_reply":"2022-10-20T19:02:16.261308Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#taken from old keras source code\ndef f1_score(y_true, y_pred): \n    true_positives = K.sum(K.round(K.clip(y_true * y_pred, 0, 1)))\n    possible_positives = K.sum(K.round(K.clip(y_true, 0, 1)))\n    predicted_positives = K.sum(K.round(K.clip(y_pred, 0, 1)))\n    precision = true_positives / (predicted_positives + K.epsilon())\n    recall = true_positives / (possible_positives + K.epsilon())\n    f1_val = 2*(precision*recall)/(precision+recall+K.epsilon())\n    return f1_val","metadata":{"execution":{"iopub.status.busy":"2022-10-20T19:02:16.263693Z","iopub.execute_input":"2022-10-20T19:02:16.263956Z","iopub.status.idle":"2022-10-20T19:02:16.269698Z","shell.execute_reply.started":"2022-10-20T19:02:16.263905Z","shell.execute_reply":"2022-10-20T19:02:16.268901Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"METRICS = [\n      tf.keras.metrics.BinaryAccuracy(name='accuracy'),\n      tf.keras.metrics.Precision(name='precision'),\n      tf.keras.metrics.Recall(name='recall'),  \n      tf.keras.metrics.AUC(name='auc'),\n        f1_score,]","metadata":{"execution":{"iopub.status.busy":"2022-10-20T19:02:16.270962Z","iopub.execute_input":"2022-10-20T19:02:16.271683Z","iopub.status.idle":"2022-10-20T19:02:16.295139Z","shell.execute_reply.started":"2022-10-20T19:02:16.271639Z","shell.execute_reply":"2022-10-20T19:02:16.294516Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mcp = ModelCheckpoint('ResNet101_V2.h5')","metadata":{"execution":{"iopub.status.busy":"2022-10-20T19:02:16.297047Z","iopub.execute_input":"2022-10-20T19:02:16.297318Z","iopub.status.idle":"2022-10-20T19:02:16.300773Z","shell.execute_reply.started":"2022-10-20T19:02:16.297283Z","shell.execute_reply":"2022-10-20T19:02:16.300138Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(optimizer='Adam', loss=\"categorical_crossentropy\", metrics=METRICS)\nSTEP_SIZE_TRAIN = train_generator.n//train_generator.batch_size\nSTEP_SIZE_VALID = valid_generator.n//valid_generator.batch_size\nepoch = 50","metadata":{"execution":{"iopub.status.busy":"2022-10-20T19:02:16.302237Z","iopub.execute_input":"2022-10-20T19:02:16.302789Z","iopub.status.idle":"2022-10-20T19:02:16.319253Z","shell.execute_reply.started":"2022-10-20T19:02:16.302749Z","shell.execute_reply":"2022-10-20T19:02:16.318524Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# history = model.fit(train_generator,\n#                     steps_per_epoch = 100,\n#                     epochs = 15,\n#                     verbose = 1,\n#                     validation_data = test_generator,\n#                     validation_steps = 50,\n#                     callbacks = [earlystop])\n\n","metadata":{"execution":{"iopub.status.busy":"2022-10-20T19:02:16.32072Z","iopub.execute_input":"2022-10-20T19:02:16.320979Z","iopub.status.idle":"2022-10-20T19:02:16.328223Z","shell.execute_reply.started":"2022-10-20T19:02:16.320948Z","shell.execute_reply":"2022-10-20T19:02:16.3273Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = 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=epoch,\n                                callbacks=[mcp])\n#                               callbacks=[lrd,mcp,es])\n\n","metadata":{"execution":{"iopub.status.busy":"2022-10-20T19:02:16.32954Z","iopub.execute_input":"2022-10-20T19:02:16.329818Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.save('./output/inception')","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.save('ResNet_101_V2.h5')","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"acc = history.history['accuracy']\nval_acc = history.history['val_acc']\nloss = history.history['loss']\nval_loss = history.history['val_loss']\n\nepochs_range = range(epoch)\n\nplt.figure(figsize=(15, 15))\nplt.rcParams.update({'font.size': 16})\nplt.subplot(2, 2, 1)\nplt.plot(epochs_range, acc, label='Training Accuracy', linewidth=2)\nplt.plot(epochs_range, val_acc, label='Validation Accuracy', linewidth=2)\nplt.legend(loc='lower right')\nplt.title('Training and Validation Accuracy')\n\nplt.subplot(2, 2, 2)\nplt.plot(epochs_range, loss, label='Training Loss', linewidth=2)\nplt.plot(epochs_range, val_loss, label='Validation Loss', linewidth=2)\nplt.legend(loc='upper right')\nplt.title('Training and Validation Loss')\nplt.show()\n\nplt.savefig('resnet101_v2_acc_loss.png', dpi = 900)\nplt.savefig('resnet101_v2__acc_loss.eps', dpi = 900)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(15, 15), linewidth = 5)\nplt.rcParams.update({'font.size': 22})\nplt.plot(epochs_range, acc, label='Training Accuracy', linewidth=2)\nplt.plot(epochs_range, val_acc, label='Validation Accuracy', linewidth=2)\nplt.legend(loc='lower right')\nplt.title('Training and Validation Accuracy')\n\nplt.savefig('resnet101_v2_train_valid_acc.png', dpi = 900)\nplt.savefig('resnet101_v2_train_valid_acc.eps', dpi = 900)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(15, 15))\nplt.rcParams.update({'font.size': 22})\nplt.plot(epochs_range, loss, label='Training Loss', linewidth=2)\nplt.plot(epochs_range, val_loss, label='Validation Loss', linewidth=2)\nplt.legend(loc='upper right')\nplt.title('Training and Validation Loss')\n\nplt.savefig('resnet101_v2_train_valid_loss.png', dpi = 900)\nplt.savefig('resnet101_v2_train_valid_loss.eps', dpi = 900)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"complete_datagen = ImageDataGenerator(rescale=1./255)\ncomplete_generator = complete_datagen.flow_from_dataframe(  \n        dataframe=train,\n        directory = \"../input/aptos2019-blindness-detection/train_images/\",\n        x_col=\"id_code\",\n        target_size=(224, 224),\n        batch_size=1,\n        shuffle=False,\n        class_mode=None)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"STEP_SIZE_COMPLETE = complete_generator.n//complete_generator.batch_size\ntrain_preds = model.predict_generator(complete_generator, steps=STEP_SIZE_COMPLETE)\ntrain_preds = [np.argmax(pred) for pred in train_preds]","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_generator.reset()\nSTEP_SIZE_TEST = test_generator.n//test_generator.batch_size\npreds = model.predict_generator(test_generator)\npredictions = [np.argmax(pred) for pred in preds]\npredictions[:10]","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels = ['0 - No DR', '1 - Mild', '2 - Moderate', '3 - Severe', '4 - Proliferative DR']\ncnf_matrix = confusion_matrix(train['diagnosis'].astype('int'), train_preds)\ncnf_matrix_norm = cnf_matrix.astype('float') / cnf_matrix.sum(axis=1)[:, np.newaxis]\ndf_cm = pd.DataFrame(cnf_matrix_norm, index=labels, columns=labels)\nprint(df_cm.describe().T)\nplt.figure(figsize=(15, 8))\nsns.heatmap(df_cm, annot=True, fmt='.2f')\nplt.show()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"filenames=test_generator.filenames\nresults=pd.DataFrame({\"id_code\":filenames,\n                      \"diagnosis\":predictions})\nresults['id_code'] = results['id_code'].map(lambda x: str(x)[:-4])\nresults.to_csv(\"submission.csv\",index=False)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"> **Accuracy**","metadata":{}},{"cell_type":"code","source":"model.load_weights('./ResNet_101_V2.h5')\n\nres = model.evaluate(x_val, y_val)\nprint(\"Testing accuracy : \" + str(res[1]))\nprint(\"Testing loss : \" + str(res[0]))","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred_val = model.predict(x_val)\ny1 = pred_val > 0.4\ny1 = y1.astype(int).sum(axis=1) - 1\ny2 = y_val.sum(axis=1) - 1","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import classification_report\nprint(classification_report(y1, y2))","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import confusion_matrix\nimport seaborn as sns\nfrom sklearn.metrics import classification_report\n\nlabels=['No DR','Mild','Moderate','Severe','Proliferative DR']\n\nprint(classification_report(y1, y2, target_names=labels))","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import confusion_matrix\nimport seaborn as sns\ntarget_names=['No DR','Mild','Moderate','Severe','Proliferative DR']\n\ncm = confusion_matrix(y1, y2)\n# Normalise\ncmn = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis]\nfig, ax = plt.subplots(figsize=(20,16))\nplt.rcParams.update({'font.size': 22})\nplt.title('Confusion Matrix', fontsize=40)\nsns.heatmap(cmn, annot=True, fmt='.2f', xticklabels=target_names, yticklabels=target_names)\nplt.ylabel('True', fontsize=40)\nplt.xlabel('Predicted', fontsize=40)\nplt.savefig('res_cm_2.png', dpi=900)\nplt.savefig('res_cm_2.eps', dpi=900)\nplt.show(block=False)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import confusion_matrix\nimport seaborn as sns\ntarget_names=['No DR','Mild','Moderate','Severe','Proliferative DR']\n\ncm = confusion_matrix(y1, y2)\n\nfig, ax = plt.subplots(figsize=(20,16))\nax.set_xticklabels([''] + labels)\nax.set_yticklabels([''] + labels)\nplt.title('Confusion Matrix', fontsize=40)\nsns.heatmap(cm, annot=True, fmt='.0f', xticklabels=target_names, yticklabels=target_names)\nplt.ylabel('True', fontsize=30)\nplt.xlabel('Predicted', fontsize=40)\nplt.savefig('res_cm_3.png', dpi=900)\nplt.savefig('res_cm_3.eps', dpi=900)\nplt.show(block=False)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}