{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"","metadata":{}},{"cell_type":"code","source":"import numpy as np \nimport pandas as pd \nimport os\nimport gc\nfrom sklearn.model_selection import train_test_split\n\nimport tensorflow as tf\nfrom tensorflow import keras\nfrom tensorflow.keras import layers\nimport tensorflow_addons as tfa\n\n# Display\nfrom IPython.display import Image, display\nimport matplotlib.pyplot as plt\nimport matplotlib.cm as cm\n\n\nAUTOTUNE = tf.data.experimental.AUTOTUNE\n","metadata":{"execution":{"iopub.status.busy":"2023-05-09T08:39:38.892973Z","iopub.execute_input":"2023-05-09T08:39:38.893474Z","iopub.status.idle":"2023-05-09T08:39:38.902776Z","shell.execute_reply.started":"2023-05-09T08:39:38.893431Z","shell.execute_reply":"2023-05-09T08:39:38.90168Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"TRAIN_PATH = '../input/aptos2019-blindness-detection/train_images/'\nDF_TRAIN = pd.read_csv('../input/aptos2019-blindness-detection/train.csv', dtype='str')\nDF_TRAIN['image_path'] = TRAIN_PATH + DF_TRAIN[\"id_code\"] + \".png\" \nDF_TRAIN.head()","metadata":{"execution":{"iopub.status.busy":"2023-05-09T08:39:41.420958Z","iopub.execute_input":"2023-05-09T08:39:41.421797Z","iopub.status.idle":"2023-05-09T08:39:41.451707Z","shell.execute_reply.started":"2023-05-09T08:39:41.421752Z","shell.execute_reply":"2023-05-09T08:39:41.450082Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"classes = {0 : \"No DR\",\n           1 : \"Mild\",\n           2 : \"Moderate\",\n           3 : \"Severe\",\n           4 : \"Proliferative\"}","metadata":{"execution":{"iopub.status.busy":"2023-05-09T08:39:45.335873Z","iopub.execute_input":"2023-05-09T08:39:45.336793Z","iopub.status.idle":"2023-05-09T08:39:45.343137Z","shell.execute_reply.started":"2023-05-09T08:39:45.336726Z","shell.execute_reply":"2023-05-09T08:39:45.341621Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!rm -rf ./train_imgs_reshaped","metadata":{"execution":{"iopub.status.busy":"2023-05-09T08:40:48.225252Z","iopub.execute_input":"2023-05-09T08:40:48.226777Z","iopub.status.idle":"2023-05-09T08:40:49.500372Z","shell.execute_reply.started":"2023-05-09T08:40:48.226695Z","shell.execute_reply":"2023-05-09T08:40:49.498246Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!mkdir ./rm_trained_imgs","metadata":{"execution":{"iopub.status.busy":"2023-05-09T08:42:13.172241Z","iopub.execute_input":"2023-05-09T08:42:13.172803Z","iopub.status.idle":"2023-05-09T08:42:14.462354Z","shell.execute_reply.started":"2023-05-09T08:42:13.172756Z","shell.execute_reply":"2023-05-09T08:42:14.459969Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"DF_TRAIN['image_path'][0]","metadata":{"execution":{"iopub.status.busy":"2023-05-09T08:42:52.507476Z","iopub.execute_input":"2023-05-09T08:42:52.508494Z","iopub.status.idle":"2023-05-09T08:42:52.51603Z","shell.execute_reply.started":"2023-05-09T08:42:52.508447Z","shell.execute_reply":"2023-05-09T08:42:52.514552Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"'../input/aptos2019-blindness-detection/train_images/000c1434d8d7.png'.split('/')[-1]","metadata":{"execution":{"iopub.status.busy":"2023-05-09T08:42:54.445329Z","iopub.execute_input":"2023-05-09T08:42:54.44581Z","iopub.status.idle":"2023-05-09T08:42:54.454635Z","shell.execute_reply.started":"2023-05-09T08:42:54.445764Z","shell.execute_reply":"2023-05-09T08:42:54.453007Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from PIL import Image\n\nfor img in DF_TRAIN['image_path']:\n    img_outfpath = \"./rm_trained_imgs/\" + img.split('/')[-1]\n    image = Image.open(img)\n    image = image.resize((512,512),Image.ANTIALIAS)\n    image.save(fp=img_outfpath)","metadata":{"execution":{"iopub.status.busy":"2023-05-09T08:42:59.405525Z","iopub.execute_input":"2023-05-09T08:42:59.405971Z","iopub.status.idle":"2023-05-09T09:07:16.41043Z","shell.execute_reply.started":"2023-05-09T08:42:59.405938Z","shell.execute_reply":"2023-05-09T09:07:16.40873Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"TRAIN_PATH_RS =  './rm_trained_imgs/'\nDF_TRAIN_RS = pd.read_csv('../input/aptos2019-blindness-detection/train.csv', dtype='str')\nDF_TRAIN_RS['image_path'] = TRAIN_PATH_RS + DF_TRAIN_RS[\"id_code\"] + \".png\" \nDF_TRAIN_RS.head()","metadata":{"execution":{"iopub.status.busy":"2023-05-09T09:07:16.483449Z","iopub.execute_input":"2023-05-09T09:07:16.485207Z","iopub.status.idle":"2023-05-09T09:07:16.508276Z","shell.execute_reply.started":"2023-05-09T09:07:16.485149Z","shell.execute_reply":"2023-05-09T09:07:16.506746Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Source: https://stackoverflow.com/questions/37292872/how-can-i-one-hot-encode-in-python\ndef encode_and_bind(original_dataframe, feature_to_encode):\n    dummies = pd.get_dummies(original_dataframe[[feature_to_encode]])\n    res = pd.concat([original_dataframe, dummies], axis=1)\n    res = res.drop([feature_to_encode], axis=1)\n    return(res)","metadata":{"execution":{"iopub.status.busy":"2023-05-09T09:07:16.510202Z","iopub.execute_input":"2023-05-09T09:07:16.5114Z","iopub.status.idle":"2023-05-09T09:07:16.52065Z","shell.execute_reply.started":"2023-05-09T09:07:16.511352Z","shell.execute_reply":"2023-05-09T09:07:16.518974Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"res = encode_and_bind(DF_TRAIN_RS, 'diagnosis')","metadata":{"execution":{"iopub.status.busy":"2023-05-09T09:07:16.522234Z","iopub.execute_input":"2023-05-09T09:07:16.523232Z","iopub.status.idle":"2023-05-09T09:07:16.544022Z","shell.execute_reply.started":"2023-05-09T09:07:16.523184Z","shell.execute_reply":"2023-05-09T09:07:16.542619Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"res.head()","metadata":{"execution":{"iopub.status.busy":"2023-05-09T09:07:16.564293Z","iopub.execute_input":"2023-05-09T09:07:16.565764Z","iopub.status.idle":"2023-05-09T09:07:16.583377Z","shell.execute_reply.started":"2023-05-09T09:07:16.565717Z","shell.execute_reply":"2023-05-09T09:07:16.58216Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"classes = {\"diagnosis_0\" : \"No DR\",\n           \"diagnosis_1\" : \"Mild\",\n           \"diagnosis_2\" : \"Moderate\",\n           \"diagnosis_3\" : \"Severe\",\n           \"diagnosis_4\" : \"Proliferative\"}","metadata":{"execution":{"iopub.status.busy":"2023-05-09T09:07:16.603873Z","iopub.execute_input":"2023-05-09T09:07:16.604739Z","iopub.status.idle":"2023-05-09T09:07:16.612137Z","shell.execute_reply.started":"2023-05-09T09:07:16.604676Z","shell.execute_reply":"2023-05-09T09:07:16.610726Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"res.rename(columns=classes, inplace=True)\nres.head()","metadata":{"execution":{"iopub.status.busy":"2023-05-09T09:07:16.614041Z","iopub.execute_input":"2023-05-09T09:07:16.615525Z","iopub.status.idle":"2023-05-09T09:07:16.638321Z","shell.execute_reply.started":"2023-05-09T09:07:16.615475Z","shell.execute_reply":"2023-05-09T09:07:16.636708Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"res.columns","metadata":{"execution":{"iopub.status.busy":"2023-05-09T09:07:16.66775Z","iopub.execute_input":"2023-05-09T09:07:16.668304Z","iopub.status.idle":"2023-05-09T09:07:16.682047Z","shell.execute_reply.started":"2023-05-09T09:07:16.668253Z","shell.execute_reply":"2023-05-09T09:07:16.680567Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"target = np.array(res[['No DR', 'Mild', 'Moderate', 'Severe', 'Proliferative']])","metadata":{"execution":{"iopub.status.busy":"2023-05-09T09:07:16.684247Z","iopub.execute_input":"2023-05-09T09:07:16.68555Z","iopub.status.idle":"2023-05-09T09:07:16.694573Z","shell.execute_reply.started":"2023-05-09T09:07:16.685506Z","shell.execute_reply":"2023-05-09T09:07:16.692543Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train, X_test, y_train, y_test  = train_test_split(res['image_path'], target, test_size=0.33, random_state=42)\nprint(f\"train shape: {X_train.shape}- y_train shape: {y_train.shape}\")\nprint(f\"test shape: {X_test.shape}- y_test shape: {y_test.shape}\")","metadata":{"execution":{"iopub.status.busy":"2023-05-09T09:07:16.697674Z","iopub.execute_input":"2023-05-09T09:07:16.699038Z","iopub.status.idle":"2023-05-09T09:07:16.712878Z","shell.execute_reply.started":"2023-05-09T09:07:16.698991Z","shell.execute_reply":"2023-05-09T09:07:16.711372Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from PIL import Image\nimage_path = Image.open('./rm_trained_imgs/000c1434d8d7.png')\nplt.imshow(image_path)\nplt.axis(\"off\")","metadata":{"execution":{"iopub.status.busy":"2023-05-09T09:09:08.049103Z","iopub.execute_input":"2023-05-09T09:09:08.04966Z","iopub.status.idle":"2023-05-09T09:09:08.30928Z","shell.execute_reply.started":"2023-05-09T09:09:08.04962Z","shell.execute_reply":"2023-05-09T09:09:08.307251Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"num_classes = 5\ninput_shape = (512, 512, 3)\nlearning_rate = 1e-4 #0.001\nweight_decay = 0.0001\nbatch_size = 16 #256\nnum_epochs = 100\n# We'll resize input images to this size\nimage_size =  256 \n# Size of the patches to be extract from the input images\npatch_size = 7 \nnum_patches = (image_size // patch_size) ** 2\nprojection_dim = 64\nnum_heads = 4\n# Size of the transformer layers\ntransformer_units = [\n    projection_dim * 2,\n    projection_dim,\n]  \ntransformer_layers = 8\n# Size of the dense layers of the final classifier\nmlp_head_units = [56, 28] #[1024, 512]  \n","metadata":{"execution":{"iopub.status.busy":"2023-05-09T09:09:21.724557Z","iopub.execute_input":"2023-05-09T09:09:21.725093Z","iopub.status.idle":"2023-05-09T09:09:21.734162Z","shell.execute_reply.started":"2023-05-09T09:09:21.725048Z","shell.execute_reply":"2023-05-09T09:09:21.732565Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"num_patches","metadata":{"execution":{"iopub.status.busy":"2023-05-09T09:09:30.174367Z","iopub.execute_input":"2023-05-09T09:09:30.174855Z","iopub.status.idle":"2023-05-09T09:09:30.184398Z","shell.execute_reply.started":"2023-05-09T09:09:30.174812Z","shell.execute_reply":"2023-05-09T09:09:30.182787Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"@tf.function\ndef load(image_file, target):\n    image = tf.io.read_file(image_file)\n    image = tf.image.decode_png(image)\n\n    image_ = tf.cast(image, tf.uint8)\n    return image, target","metadata":{"execution":{"iopub.status.busy":"2023-05-09T09:09:35.168185Z","iopub.execute_input":"2023-05-09T09:09:35.169679Z","iopub.status.idle":"2023-05-09T09:09:35.177545Z","shell.execute_reply.started":"2023-05-09T09:09:35.169609Z","shell.execute_reply":"2023-05-09T09:09:35.175693Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_loader = (\n    tf.data.Dataset\n    .from_tensor_slices((X_train,y_train))\n    .map(load, num_parallel_calls=AUTOTUNE)\n    .shuffle(7)\n    .batch(batch_size)\n)\ntest_loader = (\n    tf.data.Dataset\n    .from_tensor_slices((X_test,y_test))\n    .map(load, num_parallel_calls=AUTOTUNE)\n    .shuffle(7)\n    .batch(batch_size)\n)\n","metadata":{"execution":{"iopub.status.busy":"2023-05-09T09:09:54.48429Z","iopub.execute_input":"2023-05-09T09:09:54.48482Z","iopub.status.idle":"2023-05-09T09:09:54.520881Z","shell.execute_reply.started":"2023-05-09T09:09:54.484779Z","shell.execute_reply":"2023-05-09T09:09:54.519434Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_batch = (\n    tf.data.Dataset\n    .from_tensor_slices((X_train,y_train))\n    .map(load, num_parallel_calls=AUTOTUNE)\n    .shuffle(7)\n    .batch(X_train.shape[0]-100)#X_train.shape[0]-100\n)\n#next(iter(train_batch))[0].shape","metadata":{"execution":{"iopub.status.busy":"2023-05-09T09:09:59.408307Z","iopub.execute_input":"2023-05-09T09:09:59.409641Z","iopub.status.idle":"2023-05-09T09:09:59.429439Z","shell.execute_reply.started":"2023-05-09T09:09:59.409553Z","shell.execute_reply":"2023-05-09T09:09:59.427686Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow.keras as keras\nfrom tensorflow.keras import layers\ndata_augmentation = keras.Sequential(\n    [\n        layers.experimental.preprocessing.Normalization(),\n        layers.experimental.preprocessing.Resizing(image_size, image_size),\n        layers.experimental.preprocessing.RandomFlip(\"horizontal\"),\n        layers.experimental.preprocessing.RandomRotation(factor=0.02),\n        layers.experimental.preprocessing.RandomZoom(\n            height_factor = 0.2, width_factor = 0.2\n        ),\n    ],\n     name=\"data_augmentation\",\n)\n# Compute the mean and the variance of the training data for normalization.\nCompleteBatchData  =next(iter(train_batch))[0]\ndata_augmentation.layers[0].adapt(CompleteBatchData)\n","metadata":{"execution":{"iopub.status.busy":"2023-05-09T09:10:06.164382Z","iopub.execute_input":"2023-05-09T09:10:06.164849Z","iopub.status.idle":"2023-05-09T09:10:31.748889Z","shell.execute_reply.started":"2023-05-09T09:10:06.164811Z","shell.execute_reply":"2023-05-09T09:10:31.747439Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"del CompleteBatchData\ngc.collect()","metadata":{"execution":{"iopub.status.busy":"2023-05-09T09:10:39.394945Z","iopub.execute_input":"2023-05-09T09:10:39.395439Z","iopub.status.idle":"2023-05-09T09:10:39.430401Z","shell.execute_reply.started":"2023-05-09T09:10:39.3954Z","shell.execute_reply":"2023-05-09T09:10:39.42831Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def mlp(x, hidden_units, dropout_rate):\n    for units in hidden_units:\n        x = layers.Dense(units, activation = tf.nn.gelu)(x)\n        x = layers.Dropout(dropout_rate)(x)\n    return x","metadata":{"execution":{"iopub.status.busy":"2023-05-09T09:10:48.123947Z","iopub.execute_input":"2023-05-09T09:10:48.124404Z","iopub.status.idle":"2023-05-09T09:10:48.130542Z","shell.execute_reply.started":"2023-05-09T09:10:48.12437Z","shell.execute_reply":"2023-05-09T09:10:48.129525Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class Patches(layers.Layer):\n    def __init__(self, patch_size):\n        super(Patches, self).__init__()\n        self.patch_size = patch_size\n        \n    def call(self, images):\n        batch_size = tf.shape(images)[0]\n        patches = tf.image.extract_patches(\n            images = images,\n            sizes = [1, self.patch_size, self.patch_size, 1],\n            strides=[1, self.patch_size, self.patch_size, 1],\n            rates=[1, 1, 1, 1],\n            padding=\"VALID\",\n        )\n        patch_dims = patches.shape[-1]\n        #print(patches.shape)\n        patches = tf.reshape(patches, [batch_size, -1, patch_dims])\n        return patches","metadata":{"execution":{"iopub.status.busy":"2023-05-09T09:11:01.055309Z","iopub.execute_input":"2023-05-09T09:11:01.055795Z","iopub.status.idle":"2023-05-09T09:11:01.065797Z","shell.execute_reply.started":"2023-05-09T09:11:01.05575Z","shell.execute_reply":"2023-05-09T09:11:01.06448Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(8, 8))\nimage = next(iter(train_loader))[0][5]\n\nplt.imshow(image)\nplt.axis(\"off\")\n\nresized_image = tf.image.resize(\n    tf.convert_to_tensor([image]), size=(image_size, image_size)\n)\n\nprint(resized_image.shape)\npatches = Patches(patch_size)(resized_image)\nprint(f\"Image size: {image_size} X {image_size}\")\nprint(f\"Patch size: {patch_size} X {patch_size}\")\nprint(f\"Patches per image: {patches.shape[1]}\")\nprint(f\"Elements per patch: {patches.shape[-1]}\")\n\nn = int(np.sqrt(patches.shape[1]))\n#print(n)\n\nplt.figure(figsize=(8, 8))\nfor i, patch in enumerate(patches[0]):\n    ax = plt.subplot(n, n, i + 1)\n    patch_img = tf.reshape(patch, (patch_size, patch_size, 3))\n    plt.imshow(patch_img.numpy().astype('uint8'))\n    plt.axis(\"off\")\n","metadata":{"execution":{"iopub.status.busy":"2023-05-09T09:11:32.085401Z","iopub.execute_input":"2023-05-09T09:11:32.085892Z","iopub.status.idle":"2023-05-09T09:12:51.766116Z","shell.execute_reply.started":"2023-05-09T09:11:32.085852Z","shell.execute_reply":"2023-05-09T09:12:51.764504Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(8, 8))\nimage = next(iter(train_loader))[0][5]\n\nplt.imshow(image)\nplt.axis(\"off\")\n\nresized_image = tf.image.resize(\n    tf.convert_to_tensor([image]), size=(image_size, image_size)\n)\n\nprint(resized_image.shape)\npatches = Patches(patch_size)(resized_image)\nprint(f\"Image size: {image_size} X {image_size}\")\nprint(f\"Patch size: {patch_size} X {patch_size}\")\nprint(f\"Patches per image: {patches.shape[1]}\")\nprint(f\"Elements per patch: {patches.shape[-1]}\")\n\nn = int(np.sqrt(patches.shape[1]))\n#print(n)\n\nplt.figure(figsize=(8, 8))\nfor i, patch in enumerate(patches[0]):\n    ax = plt.subplot(n, n, i + 1)\n    patch_img = tf.reshape(patch, (patch_size, patch_size, 3))\n    plt.imshow(patch_img.numpy().astype('uint8'))\n    plt.axis(\"off\")\n","metadata":{"execution":{"iopub.status.busy":"2023-05-09T09:14:14.651709Z","iopub.execute_input":"2023-05-09T09:14:14.653223Z","iopub.status.idle":"2023-05-09T09:15:37.653141Z","shell.execute_reply.started":"2023-05-09T09:14:14.653151Z","shell.execute_reply":"2023-05-09T09:15:37.650433Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class PatchEncoder(layers.Layer):\n    def __init__(self, num_of_patches, projection_dim):\n        super(PatchEncoder, self).__init__()\n        self.num_patches = num_patches\n        self.projection = layers.Dense(units = projection_dim)\n        self.position_embedding = layers.Embedding(\n            input_dim = num_patches, output_dim = projection_dim\n        )\n        \n    def call(self, patch):\n        positions = tf.range(start=0, limit=self.num_patches, delta=1)\n        encode = self.projection(patch) + self.position_embedding(positions)\n        return encode\n","metadata":{"execution":{"iopub.status.busy":"2023-05-09T09:15:37.670446Z","iopub.execute_input":"2023-05-09T09:15:37.671143Z","iopub.status.idle":"2023-05-09T09:15:37.681569Z","shell.execute_reply.started":"2023-05-09T09:15:37.671099Z","shell.execute_reply":"2023-05-09T09:15:37.680463Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import keras\nimport json\nimport math\nimport os\nimport numpy as np \nimport pandas as pd \nimport cv2\nimport os\nimport keras\nfrom zipfile import ZipFile\nfrom tqdm import tqdm\nfrom PIL import Image\nimport matplotlib.pyplot as plt\nfrom keras import layers\nfrom keras.preprocessing.image import img_to_array\nfrom keras.utils import np_utils\nfrom keras.layers import Conv2D, MaxPooling2D, Flatten, Dense, Dropout\nfrom keras.preprocessing.image import load_img, img_to_array\nfrom keras.models import Model, load_model\nfrom keras.models import Sequential\nfrom keras.callbacks import Callback, ModelCheckpoint\nfrom keras.preprocessing.image import ImageDataGenerator\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.metrics import cohen_kappa_score, accuracy_score\nfrom keras import backend as K\nimport scipy\n\n%matplotlib inline\n","metadata":{"execution":{"iopub.status.busy":"2023-05-09T09:15:37.707234Z","iopub.execute_input":"2023-05-09T09:15:37.708191Z","iopub.status.idle":"2023-05-09T09:15:37.728419Z","shell.execute_reply.started":"2023-05-09T09:15:37.708116Z","shell.execute_reply":"2023-05-09T09:15:37.72704Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataset = []\nlabels = []","metadata":{"execution":{"iopub.status.busy":"2023-05-09T09:15:37.732937Z","iopub.execute_input":"2023-05-09T09:15:37.733778Z","iopub.status.idle":"2023-05-09T09:15:37.831136Z","shell.execute_reply.started":"2023-05-09T09:15:37.733733Z","shell.execute_reply":"2023-05-09T09:15:37.830014Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def prepare_Images(label,path):\n    img=cv2.imread(path,cv2.IMREAD_COLOR)\n    img_res=cv2.resize(img,(224,224))\n    img_array = img_to_array(img_res)\n    img_array = img_array/255.0\n    dataset.append(img_array)\n    labels.append(str(label))","metadata":{"execution":{"iopub.status.busy":"2023-05-09T09:15:37.832921Z","iopub.execute_input":"2023-05-09T09:15:37.833368Z","iopub.status.idle":"2023-05-09T09:15:37.845135Z","shell.execute_reply.started":"2023-05-09T09:15:37.833302Z","shell.execute_reply":"2023-05-09T09:15:37.844017Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_Data = pd.read_csv(\"../input/aptos2019-blindness-detection/train.csv\")\ntrain_Data.head()","metadata":{"execution":{"iopub.status.busy":"2023-05-09T09:15:37.846878Z","iopub.execute_input":"2023-05-09T09:15:37.847652Z","iopub.status.idle":"2023-05-09T09:15:37.869966Z","shell.execute_reply.started":"2023-05-09T09:15:37.847611Z","shell.execute_reply":"2023-05-09T09:15:37.868684Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"id_code_Data = train_Data['id_code']\ndiagnosis_Data = train_Data['diagnosis']","metadata":{"execution":{"iopub.status.busy":"2023-05-09T09:15:37.87141Z","iopub.execute_input":"2023-05-09T09:15:37.872685Z","iopub.status.idle":"2023-05-09T09:15:37.878482Z","shell.execute_reply.started":"2023-05-09T09:15:37.872643Z","shell.execute_reply":"2023-05-09T09:15:37.877278Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for id_code,diagnosis in tqdm(zip(id_code_Data,diagnosis_Data)):\n    path = os.path.join('../input/aptos2019-blindness-detection/train_images','{}.png'.format(id_code))\n    prepare_Images(diagnosis,path)","metadata":{"execution":{"iopub.status.busy":"2023-05-09T09:15:37.879838Z","iopub.execute_input":"2023-05-09T09:15:37.880264Z","iopub.status.idle":"2023-05-09T09:24:40.86748Z","shell.execute_reply.started":"2023-05-09T09:15:37.880226Z","shell.execute_reply":"2023-05-09T09:24:40.865711Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"images = np.array(dataset)\nlabel_arr = np.array(labels)","metadata":{"execution":{"iopub.status.busy":"2023-05-09T09:24:40.869943Z","iopub.execute_input":"2023-05-09T09:24:40.871013Z","iopub.status.idle":"2023-05-09T09:24:42.817214Z","shell.execute_reply.started":"2023-05-09T09:24:40.870939Z","shell.execute_reply":"2023-05-09T09:24:42.815848Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\nx_train,x_test,y_train,y_test = train_test_split(images,label_arr,stratify=label_arr,test_size=0.20,random_state=44)","metadata":{"execution":{"iopub.status.busy":"2023-05-09T09:24:42.818942Z","iopub.execute_input":"2023-05-09T09:24:42.819614Z","iopub.status.idle":"2023-05-09T09:24:44.464628Z","shell.execute_reply.started":"2023-05-09T09:24:42.81951Z","shell.execute_reply":"2023-05-09T09:24:44.463108Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train.shape","metadata":{"execution":{"iopub.status.busy":"2023-05-09T09:24:44.46695Z","iopub.execute_input":"2023-05-09T09:24:44.467561Z","iopub.status.idle":"2023-05-09T09:24:44.47918Z","shell.execute_reply.started":"2023-05-09T09:24:44.467503Z","shell.execute_reply":"2023-05-09T09:24:44.477522Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_test.shape","metadata":{"execution":{"iopub.status.busy":"2023-05-09T09:24:44.481136Z","iopub.execute_input":"2023-05-09T09:24:44.481568Z","iopub.status.idle":"2023-05-09T09:24:44.493229Z","shell.execute_reply.started":"2023-05-09T09:24:44.481532Z","shell.execute_reply":"2023-05-09T09:24:44.491114Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train.shape","metadata":{"execution":{"iopub.status.busy":"2023-05-09T09:24:44.494889Z","iopub.execute_input":"2023-05-09T09:24:44.495366Z","iopub.status.idle":"2023-05-09T09:24:44.510775Z","shell.execute_reply.started":"2023-05-09T09:24:44.495295Z","shell.execute_reply":"2023-05-09T09:24:44.508726Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_test.shape","metadata":{"execution":{"iopub.status.busy":"2023-05-09T09:24:44.51359Z","iopub.execute_input":"2023-05-09T09:24:44.51421Z","iopub.status.idle":"2023-05-09T09:24:44.527627Z","shell.execute_reply.started":"2023-05-09T09:24:44.51415Z","shell.execute_reply":"2023-05-09T09:24:44.525768Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train= np_utils.to_categorical(y_train, num_classes=5)\ny_test = np_utils.to_categorical(y_test, num_classes=5)","metadata":{"execution":{"iopub.status.busy":"2023-05-09T09:24:44.529406Z","iopub.execute_input":"2023-05-09T09:24:44.529883Z","iopub.status.idle":"2023-05-09T09:24:44.541997Z","shell.execute_reply.started":"2023-05-09T09:24:44.529844Z","shell.execute_reply":"2023-05-09T09:24:44.539886Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model=Sequential()\nmodel.add(Conv2D(filters=16,kernel_size=2,padding=\"same\",activation=\"relu\",input_shape=(224,224,3)))\nmodel.add(MaxPooling2D(pool_size=2))\nmodel.add(Conv2D(filters=32,kernel_size=2,padding=\"same\",activation=\"relu\"))\nmodel.add(MaxPooling2D(pool_size=2))\nmodel.add(Conv2D(filters=64,kernel_size=2,padding=\"same\",activation=\"relu\"))\nmodel.add(MaxPooling2D(pool_size=2))\nmodel.add(Conv2D(filters=128,kernel_size=2,padding=\"same\",activation=\"relu\"))\nmodel.add(MaxPooling2D(pool_size=2))\nmodel.add(Dropout(0.2))\nmodel.add(Flatten())\nmodel.add(Dense(512,activation=\"relu\"))\nmodel.add(Dropout(0.2))\nmodel.add(Dense(256,activation=\"relu\"))\nmodel.add(Dropout(0.2))\nmodel.add(Dense(5,activation=\"softmax\"))\nmodel.summary()\n","metadata":{"execution":{"iopub.status.busy":"2023-05-09T09:25:19.213759Z","iopub.execute_input":"2023-05-09T09:25:19.214329Z","iopub.status.idle":"2023-05-09T09:25:19.405178Z","shell.execute_reply.started":"2023-05-09T09:25:19.214286Z","shell.execute_reply":"2023-05-09T09:25:19.403385Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow\nmodel.compile(loss='categorical_crossentropy',\n              optimizer='adam', metrics=['accuracy'])\nhist = model.fit(x_train,y_train,batch_size=32,epochs=30,verbose=1)","metadata":{"execution":{"iopub.status.busy":"2023-05-09T09:25:26.774252Z","iopub.execute_input":"2023-05-09T09:25:26.774702Z","iopub.status.idle":"2023-05-09T09:40:49.372208Z","shell.execute_reply.started":"2023-05-09T09:25:26.774667Z","shell.execute_reply":"2023-05-09T09:40:49.369762Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.save(\"model.h5\")","metadata":{"execution":{"iopub.status.busy":"2023-05-09T09:40:49.376616Z","iopub.execute_input":"2023-05-09T09:40:49.377261Z","iopub.status.idle":"2023-05-09T09:40:50.124492Z","shell.execute_reply.started":"2023-05-09T09:40:49.377206Z","shell.execute_reply":"2023-05-09T09:40:50.123004Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred = model.predict(x_test)","metadata":{"execution":{"iopub.status.busy":"2023-05-09T09:40:50.126569Z","iopub.execute_input":"2023-05-09T09:40:50.127125Z","iopub.status.idle":"2023-05-09T09:40:52.220087Z","shell.execute_reply.started":"2023-05-09T09:40:50.127067Z","shell.execute_reply":"2023-05-09T09:40:52.218446Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.evaluate(x_test,y_test)","metadata":{"execution":{"iopub.status.busy":"2023-05-09T09:40:52.224094Z","iopub.execute_input":"2023-05-09T09:40:52.224581Z","iopub.status.idle":"2023-05-09T09:40:54.827306Z","shell.execute_reply.started":"2023-05-09T09:40:52.224541Z","shell.execute_reply":"2023-05-09T09:40:54.825843Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import accuracy_score, classification_report,confusion_matrix\nscore = round(accuracy_score(y_test.argmax(axis=1), pred.argmax(axis=1)),2)\nprint(score)","metadata":{"execution":{"iopub.status.busy":"2023-05-09T09:40:54.829633Z","iopub.execute_input":"2023-05-09T09:40:54.830055Z","iopub.status.idle":"2023-05-09T09:40:54.838662Z","shell.execute_reply.started":"2023-05-09T09:40:54.830019Z","shell.execute_reply":"2023-05-09T09:40:54.837323Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"report = classification_report(y_test.argmax(axis=1), pred.argmax(axis=1))\nprint(report)","metadata":{"execution":{"iopub.status.busy":"2023-05-09T09:40:55.359961Z","iopub.execute_input":"2023-05-09T09:40:55.360367Z","iopub.status.idle":"2023-05-09T09:40:55.376751Z","shell.execute_reply.started":"2023-05-09T09:40:55.360319Z","shell.execute_reply":"2023-05-09T09:40:55.375078Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# list all data in history\nprint(hist.history.keys())\n# summarize history for accuracy\nplt.figure(figsize=(12,10))\nplt.plot(hist.history['accuracy'])\nplt.title('model accuracy')\nplt.ylabel('accuracy')\nplt.xlabel('epoch')\nplt.legend(['train', 'test'], loc='upper left')\nplt.show()\n# summarize history for loss\nplt.figure(figsize=(12,10))\nplt.plot(hist.history['loss'])\nplt.title('model loss')\nplt.ylabel('loss')\nplt.xlabel('epoch')\nplt.legend(['train', 'test'], loc='upper left')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-05-09T09:40:56.284481Z","iopub.execute_input":"2023-05-09T09:40:56.284928Z","iopub.status.idle":"2023-05-09T09:40:56.716405Z","shell.execute_reply.started":"2023-05-09T09:40:56.284891Z","shell.execute_reply":"2023-05-09T09:40:56.71411Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Load the model from a file\nimport keras\nloaded_model = keras.models.load_model('model.h5')","metadata":{"execution":{"iopub.status.busy":"2023-05-09T09:40:56.71882Z","iopub.execute_input":"2023-05-09T09:40:56.719438Z","iopub.status.idle":"2023-05-09T09:40:57.199008Z","shell.execute_reply.started":"2023-05-09T09:40:56.719382Z","shell.execute_reply":"2023-05-09T09:40:57.197675Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_img_array(img):\n    \n    # `array` is a float32 Numpy array of shape (299, 299, 3)\n    array = keras.preprocessing.image.img_to_array(img)\n    # We add a dimension to transform our array into a \"batch\"\n    # of size (1, 299, 299, 3)\n    array = np.expand_dims(array, axis=0)\n    return array","metadata":{"execution":{"iopub.status.busy":"2023-05-09T09:40:57.200766Z","iopub.execute_input":"2023-05-09T09:40:57.201294Z","iopub.status.idle":"2023-05-09T09:40:57.209069Z","shell.execute_reply.started":"2023-05-09T09:40:57.201243Z","shell.execute_reply":"2023-05-09T09:40:57.207414Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def gradcam_heatmap(img_array, model, last_conv_layer_name, pred_index=None):\n    # First, we create a model that maps the input image to the activations\n    # of the last conv layer as well as the output predictions\n    grad_model = tf.keras.models.Model(\n        [model.input], [model.get_layer(last_conv_layer_name).output,  model.output]\n    )\n    \n    # Then, we compute the gradient of the top predicted class for our input image\n    # with respect to the activations of the last conv layer\n    with tf.GradientTape() as tape:\n        last_conv_layer_output, preds = grad_model(img_array)\n        if pred_index is None:\n            pred_index = tf.argmax(preds[0])\n        class_channel = preds[:, pred_index]\n        \n        \n    # This is the gradient of the output neuron (top predicted or chosen)\n    # with regard to the output feature map of the last conv layer\n    grads = tape.gradient(class_channel, last_conv_layer_output)\n\n    # This is a vector where each entry is the mean intensity of the gradient\n    # over a specific feature map channel\n    pooled_grads = tf.reduce_mean(grads, axis=(0, 1))\n    # We multiply each channel in the feature map array\n    # by \"how important this channel is\" with regard to the top predicted class\n    # then sum all the channels to obtain the heatmap class activation\n    last_conv_layer_output = last_conv_layer_output\n    heatmap = last_conv_layer_output @ pooled_grads[..., tf.newaxis]\n    heatmap = tf.squeeze(heatmap)\n    \n    # For visualization purpose, we will also normalize the heatmap between 0 & 1\n    heatmap = tf.maximum(heatmap, 0) / tf.math.reduce_max(heatmap)\n    return heatmap.numpy()\n","metadata":{"execution":{"iopub.status.busy":"2023-05-09T09:40:57.211379Z","iopub.execute_input":"2023-05-09T09:40:57.211932Z","iopub.status.idle":"2023-05-09T09:40:57.225032Z","shell.execute_reply.started":"2023-05-09T09:40:57.211881Z","shell.execute_reply":"2023-05-09T09:40:57.223552Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"classes.values()","metadata":{"execution":{"iopub.status.busy":"2023-05-09T09:40:57.22679Z","iopub.execute_input":"2023-05-09T09:40:57.227291Z","iopub.status.idle":"2023-05-09T09:40:57.243131Z","shell.execute_reply.started":"2023-05-09T09:40:57.227249Z","shell.execute_reply":"2023-05-09T09:40:57.241695Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def display_gradcam(img, heatmap, cam_path=\"cam.jpg\", alpha=0.4, preds=[0,0,0,0,0], plot=None):\n\n    # Rescale heatmap to a range 0-255\n    heatmap = np.uint8(255 * heatmap)\n\n    # Use jet colormap to colorize heatmap\n    jet = cm.get_cmap(\"jet\")\n\n    # Use RGB values of the colormap\n    jet_colors = jet(np.arange(256))[:, :3]\n    jet_heatmap = jet_colors[heatmap]\n\n    # Create an image with RGB colorized heatmap\n    jet_heatmap = keras.preprocessing.image.array_to_img(jet_heatmap)\n    jet_heatmap = jet_heatmap.resize((img.shape[1], img.shape[0]))\n    jet_heatmap = keras.preprocessing.image.img_to_array(jet_heatmap)\n\n    # Superimpose the heatmap on original image\n    superimposed_img = jet_heatmap * alpha + img\n    superimposed_img = keras.preprocessing.image.array_to_img(superimposed_img)\n\n    # Save the superimposed image\n    #superimposed_img.save(cam_path)\n\n    # Display Grad CAM\n    plot.imshow(superimposed_img)\n    plot.set(title =\n        \" No DR: \\\n        {:.3f}\\nMild: \\\n        {:.3f}\\nModerate: \\\n        {:.3f}\\nSevere: \\\n        {:.3f}\\nProliferative: \\\n        {:.3f}\".format(preds[0], \\\n                    preds[1], \\\n                    preds[2], \\\n                    preds[3],\n                    preds[4])\n    )\n    plot.axis('off')\n","metadata":{"execution":{"iopub.status.busy":"2023-05-09T09:40:57.246776Z","iopub.execute_input":"2023-05-09T09:40:57.247204Z","iopub.status.idle":"2023-05-09T09:40:57.258986Z","shell.execute_reply.started":"2023-05-09T09:40:57.247168Z","shell.execute_reply":"2023-05-09T09:40:57.257296Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# As in layer_normalization (LayerNorma (None, 1296, 64) ) \n#the last dim is 1296 so 36x36 for heatmap\nnp.sqrt(1296)\n","metadata":{"execution":{"iopub.status.busy":"2023-05-09T09:41:23.619018Z","iopub.execute_input":"2023-05-09T09:41:23.620369Z","iopub.status.idle":"2023-05-09T09:41:23.633828Z","shell.execute_reply.started":"2023-05-09T09:41:23.620295Z","shell.execute_reply":"2023-05-09T09:41:23.632389Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_image = next(iter(test_loader))[0][5]\n# Prepare image\nimg_array =get_img_array(test_image)\nlast_conv_layer_name = 'dense_5'\n# Remove last layer's softmax\n#model.layers[-1].activation = softmax\n# Print what the top predicted class is\npreds = model.predict(img_array)\nprint(\"Predicted:\\n\" + \"No DR: \\\n    {p1}\\nMild: {p2}\\nModerate: \\\n    {p3}\\nSevere: \\\n    {p4}\\nProliferative: {p5}\".format(p1=preds[0][0], \\\n                                            p2=preds[0][1],p3=preds[0][2],p4=preds[0][3],p5=preds[0][4]))\n# Generate class activation heatmap\nheatmap = gradcam_heatmap(img_array, model, last_conv_layer_name)\n\nheatmap = np.reshape(heatmap, (36,36))\n# Display heatmap\nplt.matshow(heatmap)\n\nplt.show()\n","metadata":{"execution":{"iopub.status.busy":"2023-05-09T09:44:36.52509Z","iopub.execute_input":"2023-05-09T09:44:36.526022Z","iopub.status.idle":"2023-05-09T09:44:36.797685Z","shell.execute_reply.started":"2023-05-09T09:44:36.525903Z","shell.execute_reply":"2023-05-09T09:44:36.795251Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig, axis = plt.subplots(3, 2, figsize=(30, 30))\nfor images, ax in zip(next(iter(test_loader))[0][:6], axis.flat):\n    img_array = get_img_array(images)\n    # Remove last layer's softmax\n    model.layers[-1].activation = None\n    # Print what the top predicted class is\n    preds = model.predict(img_array)\n    heatmap = gradcam_heatmap(img_array, model, last_conv_layer_name)\n\n    heatmap = np.reshape(heatmap, (36,36))\n    display_gradcam(images, heatmap, preds=preds[0], plot=ax)","metadata":{"execution":{"iopub.status.busy":"2023-05-09T09:44:08.763844Z","iopub.execute_input":"2023-05-09T09:44:08.764301Z","iopub.status.idle":"2023-05-09T09:44:10.05075Z","shell.execute_reply.started":"2023-05-09T09:44:08.764263Z","shell.execute_reply":"2023-05-09T09:44:10.048519Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig, axis = plt.subplots(3, 2, figsize=(30, 30))\nfor images, ax in zip(next(iter(test_loader))[0][6:12], axis.flat):\n    img_array = get_img_array(images)\n    # Remove last layer's softmax\n    model.layers[-1].activation = None\n    # Print what the top predicted class is\n    preds = model.predict(img_array)\n    heatmap = gradcam_heatmap(img_array, model, last_conv_layer_name)\n\n    heatmap = np.reshape(heatmap, (36,36))\n    display_gradcam(images, heatmap, preds=preds[0], plot=ax)","metadata":{"execution":{"iopub.status.busy":"2023-05-09T09:43:03.324048Z","iopub.execute_input":"2023-05-09T09:43:03.324496Z","iopub.status.idle":"2023-05-09T09:43:04.618797Z","shell.execute_reply.started":"2023-05-09T09:43:03.324459Z","shell.execute_reply":"2023-05-09T09:43:04.616566Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## plt.figure(figsize=(8, 8))\nimage = next(iter(train_loader))[0][5]\n\nplt.imshow(image)\nplt.axis(\"off\")\n\nresized_image = tf.image.resize(\n    tf.convert_to_tensor([image]), size=(image_size, image_size)\n)\n\nprint(resized_image.shape)\npatches = Patches(patch_size)(resized_image)\nprint(f\"Image size: {image_size} X {image_size}\")\nprint(f\"Patch size: {patch_size} X {patch_size}\")\nprint(f\"Patches per image: {patches.shape[1]}\")\nprint(f\"Elements per patch: {patches.shape[-1]}\")\n\nn = int(np.sqrt(patches.shape[1]))\n#print(n)\n\nplt.figure(figsize=(8, 8))\nfor i, patch in enumerate(patches[0]):\n    ax = plt.subplot(n, n, i + 1)\n    patch_img = tf.reshape(patch, (patch_size, patch_size, 3))\n    plt.imshow(patch_img.numpy().astype('uint8'))\n    plt.axis(\"off\")","metadata":{}}]}