{"metadata":{"kaggle":{"accelerator":"none","dataSources":[{"sourceId":14774,"databundleVersionId":875431,"sourceType":"competition"},{"sourceId":6928716,"sourceType":"datasetVersion","datasetId":3978273}],"dockerImageVersionId":30558,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false},"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"codemirror_mode":{"name":"ipython","version":3},"file_extension":".py","mimetype":"text/x-python","name":"python","nbconvert_exporter":"python","pygments_lexer":"ipython3","version":"3.10.12"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import os\nimport cv2\nimport numpy as np\nimport math\nimport random\nfrom tqdm import tqdm\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nimport pickle\nfrom PIL import Image\nimport gc\nsns.set_style('darkgrid')\n\n\nimport keras\nimport tensorflow as tf\nfrom keras.models import Sequential\nfrom keras.layers import Dense, Dropout, GlobalAveragePooling2D\nfrom keras.utils import plot_model\nfrom keras.applications.densenet import DenseNet121\nfrom keras.applications.vgg19 import VGG19\nfrom keras.applications.efficientnet import EfficientNetB6\nfrom keras.callbacks import Callback\n\nfrom sklearn.model_selection import train_test_split\nfrom keras.layers import  Dense,Dropout,GlobalAveragePooling2D\nfrom keras.optimizers import Adam\nfrom keras.preprocessing.image import ImageDataGenerator\nfrom sklearn.metrics import cohen_kappa_score\nfrom keras import backend as K\n\nrandom.seed(42)\nos.environ['PYTHONHASHSEED'] = str(42)\nnp.random.seed(42)\ntf.random.set_seed(42)\nkeras.utils.set_random_seed(42)\n\ngpus = tf.config.experimental.list_physical_devices('GPU')\nfor gpu in gpus:\n  tf.config.experimental.set_memory_growth(gpu, True)\n\nprint(\"Num GPUs Available: \", len(tf.config.list_physical_devices('GPU')))\n!nvidia-smi","metadata":{"execution":{"iopub.execute_input":"2023-11-12T02:45:30.375889Z","iopub.status.busy":"2023-11-12T02:45:30.375629Z","iopub.status.idle":"2023-11-12T02:45:41.634550Z","shell.execute_reply":"2023-11-12T02:45:41.633459Z","shell.execute_reply.started":"2023-11-12T02:45:30.375866Z"},"id":"ta2Gf8DVPWA5","outputId":"78fd56b9-c042-47fa-df2b-c644739e7cd8","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df = pd.read_csv('/kaggle/input/aptos2019-blindness-detection/train.csvv')\ntest_df = pd.read_csv('/kaggle/input/aptos2019-blindness-detection/test.csv')\nsample_df = pd.read_csv('/kaggle/input/aptos2019-blindness-detection/sample_submission.csv')\n\nprint(\"Train Shape: \", train_df.shape , \"\\nTest Shape: \", test_df.shape, \"\\nSample Shape: \", sample_df.shape)","metadata":{"execution":{"iopub.execute_input":"2023-11-12T02:46:05.391987Z","iopub.status.busy":"2023-11-12T02:46:05.390100Z","iopub.status.idle":"2023-11-12T02:46:05.427916Z","shell.execute_reply":"2023-11-12T02:46:05.426992Z","shell.execute_reply.started":"2023-11-12T02:46:05.391950Z"},"id":"YePd3iZPTAJa","outputId":"3ea008d6-d79d-42c4-c786-a6a7c082699e","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Visualizing The Data Distribution","metadata":{"id":"GCTyuFhlTR-D"}},{"cell_type":"code","source":"names = [\"No DR\", \"Mild\", \"Moderate\", \"Severe\", \"Proliferative DR\"]\n\ndef plot_distribution(df, names, title):\n    df['diagnosis'].value_counts().plot(kind='bar', title=title, width=.8,\n                                            rot=0, color=['#1f77b4', '#ff7f0e', '#2ca02c', '#d62728', '#9467bd'],\n                                                figsize=(10, 5))\n    for i, v in enumerate(df['diagnosis'].value_counts()):\n        plt.text(i-.1, v, str(v), fontweight='bold')\n    plt.xticks(range(5), names)\n    plt.show()\n    print(\"==\"*60)\n    # show as pie chart\n    df['diagnosis'].value_counts().plot(kind='pie', title=title,\n                                            rot=0, colors=['#1f77b4', '#ff7f0e', '#2ca02c', '#d62728', '#9467bd'],\n                                                figsize=(10, 8), autopct='%1.1f%%', startangle=60)\n    plt.legend(names, loc=\"upper left\")\n    plt.show()\n\nplot_distribution(train_df, names, 'Distribution of Diagnoses in Training Set')","metadata":{"execution":{"iopub.execute_input":"2023-11-11T21:23:56.146887Z","iopub.status.busy":"2023-11-11T21:23:56.146000Z","iopub.status.idle":"2023-11-11T21:23:56.831961Z","shell.execute_reply":"2023-11-11T21:23:56.830915Z","shell.execute_reply.started":"2023-11-11T21:23:56.146854Z"},"id":"EKJ8kCc9THj5","outputId":"bf4f6ce2-849d-4424-eada-dd6cb5dd82a4","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Display Some Images From The Dataset","metadata":{"id":"xsmhZSY4TjMp"}},{"cell_type":"code","source":"names = [\"No DR\", \"Mild\", \"Moderate\", \"Severe\", \"Proliferative DR\"]\nfig, ax = plt.subplots(nrows=5, ncols=5, figsize=(20, 20))\nfor i in range(5):\n    for j in range(5):\n        index = train_df[train_df['diagnosis'] == i].index[j]\n        img = cv2.imread(f\"/kaggle/input/aptos2019-blindness-detection/train_images/{train_df.iloc[index]['id_code']}.png\")\n        img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)\n        mean, std = img.mean(), img.std()\n        ax[i, j].imshow(img)\n        ax[i, j].set_title(f\"Class : {names[train_df.iloc[index]['diagnosis']]}\"+f\"\\nWhite pixel count :({np.sum(img == 255)})\\nMean: {mean:.2f}, STD: {std:.2f}\")\n        ax[i, j].axis('off')\nplt.show()","metadata":{"execution":{"iopub.execute_input":"2023-11-11T21:25:36.813328Z","iopub.status.busy":"2023-11-11T21:25:36.812961Z","iopub.status.idle":"2023-11-11T21:26:02.261654Z","shell.execute_reply":"2023-11-11T21:26:02.260107Z","shell.execute_reply.started":"2023-11-11T21:25:36.813300Z"},"id":"fZaNIbXeTfiB","outputId":"c8cde7ee-a8cd-4e20-e06f-fa003e962ad2","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Data Preprocessing","metadata":{"id":"FIJ8zCLCTr1g"}},{"cell_type":"code","source":"def preprocess_image(image_path, desired_size=224):\n    im = Image.open(image_path)\n    im = im.resize((desired_size, )*2, resample=Image.LANCZOS)\n    return im\n\n\nresize_size = 224\nN = train_df.shape[0]\nx_train_rsz = np.empty((N, resize_size, resize_size, 3), dtype=np.uint8)\n\nfor i, image_id in tqdm(enumerate(train_df['id_code'])):\n    x_train_rsz[i, :, :, :] = preprocess_image(\n        f'/kaggle/input/aptos2019-blindness-detection/train_images/{image_id}.png',desired_size = resize_size\n    )\nprint('Train Images shape: {} size: {:,}'.format(x_train_rsz.shape, x_train_rsz.size))\nprint(\"==\"*50)\n\nN = test_df.shape[0]\nx_test_rsz = np.empty((N, resize_size, resize_size, 3), dtype=np.uint8)\nfor i, image_id in tqdm(enumerate(test_df['id_code'])):\n    x_test_rsz[i, :, :, :] = preprocess_image(\n        f'/kaggle/input/aptos2019-blindness-detection/test_images/{image_id}.png',desired_size = resize_size\n    )\nprint('Test Images shape: {} size: {:,}'.format(x_test_rsz.shape, x_test_rsz.size))\nprint(\"==\"*50)","metadata":{"execution":{"iopub.execute_input":"2023-11-12T02:46:16.605959Z","iopub.status.busy":"2023-11-12T02:46:16.605033Z","iopub.status.idle":"2023-11-12T02:59:30.853519Z","shell.execute_reply":"2023-11-12T02:59:30.852533Z","shell.execute_reply.started":"2023-11-12T02:46:16.605917Z"},"id":"CG-FJYIeTvjo","outputId":"591e53f9-fee2-4ea8-d6e8-fef47483d68b","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Adding GaussianBlur and addWeighted to the Image","metadata":{"id":"urQSdS3xURu7"}},{"cell_type":"code","source":"scale=300\nplt.figure(figsize=(20,10))\na=x_train_rsz[0]\nb=cv2.GaussianBlur(a,(0,0),scale/30)\nc=cv2.addWeighted(a,4,b,-4,128)\nplt.subplot(2,3,1)\nplt.title('original image')\nplt.grid(False)\nplt.imshow(a)\nplt.subplot(2,3,2)\nplt.title('blur image')\nplt.grid(False)\nplt.imshow(b)\nplt.subplot(2,3,3)\nplt.title('original-blur')\nplt.grid(False)\nplt.imshow(c)","metadata":{"id":"4YwJh5uAUNjp","outputId":"81d53a27-8392-4654-aa42-e20b284cc9d2"},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Apply the Preprocessing on All Dataset And save it on disk","metadata":{"id":"Kmvhb0kMUtWr"}},{"cell_type":"code","source":"scale=300\nN = train_df.shape[0]\nx_train = np.empty((N, resize_size, resize_size, 3), dtype=np.uint8)\nfor i in tqdm(range(len(x_train))):\n    a=x_train_rsz[i]\n    x_train[i]=cv2.addWeighted(a, 4,\n                               cv2.GaussianBlur(a,(0,0), scale/30), -4,128)\n\nN = test_df.shape[0]\nx_test = np.empty((N, resize_size, resize_size, 3), dtype=np.uint8)\n\nfor i in tqdm(range(len(x_test))):\n    a=x_test_rsz[i]\n    x_test[i]=cv2.addWeighted(a, 4,cv2.GaussianBlur(a,(0,0), scale/30), -4,128)","metadata":{"execution":{"iopub.execute_input":"2023-11-12T03:00:44.433583Z","iopub.status.busy":"2023-11-12T03:00:44.432828Z","iopub.status.idle":"2023-11-12T03:01:26.200324Z","shell.execute_reply":"2023-11-12T03:01:26.199668Z","shell.execute_reply.started":"2023-11-12T03:00:44.433548Z"},"id":"NNr9bW-tUFOh","outputId":"745edb58-bddf-4a97-d1ce-79f6a9280e8f","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Processing on the lable make a one-hot incoding and then convert To Multilabel version","metadata":{"id":"NOiw2yVgWfBI"}},{"cell_type":"code","source":"y_train = pd.get_dummies(train_df['diagnosis']).values\n\nprint(\"X Train shape: \", x_train.shape)\nprint(\"X Test shape: \", x_test.shape)\nprint(\"Y Train shape: \", y_train.shape)\nprint(\"==\"*50)\n\nl = len(x_train)\nfor j in range(2):\n    for i in tqdm(range(l)):\n        if (y_train[i] == np.array([0,0,0,0,1])).all():\n            x_train = np.vstack((x_train, x_train[i].reshape(1,resize_size,resize_size,3)))\n            y_train = np.vstack((y_train, y_train[i].reshape(1,5)))\n\ny_train_multi = np.empty(y_train.shape, dtype=y_train.dtype)\ny_train_multi[:, 4] = y_train[:, 4]\n\nfor i in range(3, -1, -1):\n    y_train_multi[:, i] = np.logical_or(y_train[:, i], y_train_multi[:, i+1])\n\n\npickle.dump((x_train,y_train_multi), open('xy_train.pkl', 'wb'))\npickle.dump(x_test, open('x_test.pkl', 'wb'))\n\nprint(\"Original y_train:\", y_train.sum(axis=0))\nprint(\"Multilabel version:\", y_train_multi.sum(axis=0))","metadata":{"execution":{"iopub.execute_input":"2023-11-12T03:01:40.445296Z","iopub.status.busy":"2023-11-12T03:01:40.444396Z","iopub.status.idle":"2023-11-12T03:03:43.583136Z","shell.execute_reply":"2023-11-12T03:03:43.582028Z","shell.execute_reply.started":"2023-11-12T03:01:40.445254Z"},"id":"fBE998l5WawZ","outputId":"aebf3081-c7a5-436d-e3a0-29de28dc4383","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def show_sample(pics,label,row=3,columns=3):\n    fig=plt.figure(figsize=(5*columns, 4*row))\n    for i in range(row*columns):\n        fig.add_subplot(row, columns, i+1)\n        plt.title(label[i])\n        plt.grid(False)\n        plt.imshow(pics[i])\nshow_sample(x_train, y_train_multi)","metadata":{"id":"Z_Bj7MxYW-WB","outputId":"25623c16-efc7-4b5b-96bb-bf4a97123a2f"},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Working With imbalance Data","metadata":{"id":"WG5c2o23bkp5"}},{"cell_type":"code","source":"x_train,y_multi = pickle.load(open(\"/kaggle/working/xy_train.pkl\",\"rb\"))\n\nx_train, x_val, y_train, y_val = train_test_split(\n    x_train, y_multi,\n    test_size=0.2,\n    random_state=2019\n)\ndel y_multi\nprint(\"X Train shape: \", x_train.shape)\nprint(\"X Val shape: \", x_val.shape)\nprint(\"Y Train shape: \", y_train.shape)\nprint(\"Y Val shape: \", y_val.shape)\n\nprint(\"==\"*50)\n\nBATCH_SIZE = 12\ndatagen =  ImageDataGenerator(\n        zoom_range=0.15,\n        fill_mode='constant',\n        cval=0.,\n        horizontal_flip=True,\n        vertical_flip=True,\n        brightness_range=[0.3,1.0])\n\ndata_generator = datagen.flow(x_train, y_train, batch_size=BATCH_SIZE, seed=2019)\nprint(y_train[0:5])\n\ngc.collect()","metadata":{"id":"o1fR3jQ2bvCx","outputId":"4ce0bc43-4349-4602-e1b7-c96f89e37381"},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Fix imbalance Dataset With Over Sampling SMOT Method","metadata":{"id":"NV9g4IQelyk8"}},{"cell_type":"code","source":"from imblearn.over_sampling import SMOTE\nfrom matplotlib.pyplot import figtext\n\nx_train,y_multi = pickle.load(open(\"/kaggle/working/xy_train.pkl\",\"rb\"))\nresize_size = 224\ny_train_over = []\n\nfor i in tqdm(range(len(y_multi))):\n    if np.sum(y_multi[i]) == 1:\n        y_train_over.append(1)\n    elif np.sum(y_multi[i]) == 2:\n        y_train_over.append(2)\n    elif np.sum(y_multi[i]) == 3:\n        y_train_over.append(3)\n    elif np.sum(y_multi[i]) == 4:\n        y_train_over.append(4)\n    elif np.sum(y_multi[i]) == 5:\n        y_train_over.append(5)\n\ny_train_over = np.array(y_train_over)\nprint(\"y_train_over shape:\", y_train_over.shape)\nprint(\"==\"*60)\n\nsm = SMOTE(random_state=42)\nx_over, y_over = sm.fit_resample(x_train.reshape(x_train.shape[0], -1), y_train_over)\nx_over = x_over.reshape(len(x_over), resize_size, resize_size, 3)\nprint(\"X Train shape: \", x_over.shape)\nprint(\"Y Train shape: \", y_over.shape)\nprint(\"==\"*60)\n\n\ny_over = pd.get_dummies(y_over).values\nnames = [\"No DR\", \"Mild\", \"Moderate\", \"Severe\", \"Proliferative DR\"]\ncolors = ['#1f77b4', '#ff7f0e', '#2ca02c', '#d62728', '#9467bd']\ndef plotSamples(y,labels=None, figer_name=None):\n    plt.figure(figsize=(20, 10))\n    if figer_name is not None:\n        figtext(0.5, 0.9, figer_name, ha='center', fontsize=30)\n    ax = sns.barplot(x=labels, y=np.sum(y,axis=0), palette=colors,hue=labels)\n    for i in range(len(labels)):\n        ax.text(i ,np.sum(y,axis=0)[i] /2, str(np.sum(y,axis=0)[i]), ha='center', va='top', fontsize=20)\n    plt.xlabel(\"Movements\", fontsize=20)\n    plt.ylabel(\"Number of samples\", fontsize=20)\n    plt.show()\n\nplotSamples(y_over, labels=names, figer_name=\"Oversampled Train Set\")\nprint(\"==\"*60)\n\nl = len(x_over)\nfor j in range(2):\n    for i in tqdm(range(l)):\n        if (y_over[i] == np.array([0,0,0,0,1])).all():\n            x_over = np.vstack((x_over, x_over[i].reshape(1,resize_size,resize_size,3)))\n            y_over = np.vstack((y_over, y_over[i].reshape(1,5)))\n\ny_over_multi = np.empty(y_over.shape, dtype=y_over.dtype)\ny_over_multi[:, 4] = y_over[:, 4]\n\nfor i in range(3, -1, -1):\n    y_over_multi[:, i] = np.logical_or(y_over[:, i], y_over_multi[:, i+1])\n\nprint(\"==\"*60)\npickle.dump((x_over,y_over_multi), open('xy_over.pkl', 'wb'))\nprint(\"==\"*60)\nprint(\"Original y_over:\", y_over.sum(axis=0))\nprint(\"Multilabel version:\", y_over_multi.sum(axis=0))\n","metadata":{"execution":{"iopub.execute_input":"2023-11-12T03:04:34.188968Z","iopub.status.busy":"2023-11-12T03:04:34.188060Z","iopub.status.idle":"2023-11-12T03:38:16.962831Z","shell.execute_reply":"2023-11-12T03:38:16.961825Z","shell.execute_reply.started":"2023-11-12T03:04:34.188924Z"},"id":"dNQ-1u1Nlx7T","outputId":"8e5b4146-6aa4-40da-9563-e8fcee3fab58","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Fix imbalance","metadata":{"id":"Z6fsdH0RKLO4"}},{"cell_type":"code","source":"x_train,y_multi = pickle.load(open(\"/kaggle/working/xy_over.pkl\",\"rb\"))\n\nx_train, x_val, y_train, y_val = train_test_split(\n    x_train, y_multi,\n    test_size=0.2,\n    random_state=2019\n)\nprint(\"==\"*60)\nprint(\"X Train shape: \", x_train.shape)\nprint(\"X Val shape: \", x_val.shape)\nprint(\"Y Train shape: \", y_train.shape)\nprint(\"Y Val shape: \", y_val.shape)\n\ndel y_multi\nprint(\"==\"*50)\n\nBATCH_SIZE = 12\ndatagen =  ImageDataGenerator(\n        zoom_range=0.15,\n        fill_mode='constant',\n        cval=0.,\n        horizontal_flip=True,\n        vertical_flip=True,\n        brightness_range=[0.3,1.0] )\n\ndata_generator = datagen.flow(x_train, y_train, batch_size=BATCH_SIZE, seed=2019)\nprint(y_train[0:5])\n\ngc.collect()","metadata":{"execution":{"iopub.execute_input":"2023-11-12T03:38:58.473112Z","iopub.status.busy":"2023-11-12T03:38:58.471936Z","iopub.status.idle":"2023-11-12T03:39:03.364194Z","shell.execute_reply":"2023-11-12T03:39:03.363247Z","shell.execute_reply.started":"2023-11-12T03:38:58.473074Z"},"id":"-3bcU6j_KHXX","outputId":"a053dfb2-77b5-48dc-c61b-1f729b373d98","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Custom Metrics","metadata":{"id":"nT2du0PEcJm7"}},{"cell_type":"code","source":"class Metrics(Callback):\n    def __init__(self, val_data):\n        super().__init__()\n        self.validation_data = val_data\n\n    def on_train_begin(self,logs={},):\n        self.val_kappas = []\n\n    def on_epoch_end(self, epoch, logs={}):\n        X_val, y_val = self.validation_data[:2]\n        y_val = y_val.sum(axis=1) - 1\n\n        y_pred = self.model.predict(X_val) > 0.5\n        y_pred = y_pred.astype(int).sum(axis=1) - 1\n\n        _val_kappa = cohen_kappa_score(\n            y_val,\n            y_pred,\n            weights='quadratic'\n        )\n        self.val_kappas.append(_val_kappa)\n        print(f\"\\nval_kappa: {_val_kappa:.4f}\")\n        if _val_kappa == max(self.val_kappas):\n            print(\"Validation Kappa has improved ⬆️  and Saving Model\")\n            self.model.save('best_model.keras')\n        else:\n            print(\"Validation Kappa has not improved ⬇️ \")\n        return\n\ndef precision(y_true, y_pred):\n    true_positives = K.sum(K.round(K.clip(y_true * y_pred, 0, 1)))\n    predicted_positives = K.sum(K.round(K.clip(y_pred, 0, 1)))\n    return true_positives / (predicted_positives + K.epsilon())\n\ndef recall(y_true, y_pred):\n    true_positives = K.sum(K.round(K.clip(y_true * y_pred, 0, 1)))\n    actual_positives = K.sum(K.round(K.clip(y_true, 0, 1)))\n    return true_positives / (actual_positives + K.epsilon())","metadata":{"id":"dfvDyYWwcIrK"},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Fix imbalance\n\n","metadata":{"id":"b33hfLKO3gwH"}},{"cell_type":"code","source":"class Metrics(Callback):\n    def __init__(self, val_data):\n        super().__init__()\n        self.validation_data = val_data\n\n    def on_train_begin(self,logs={},):\n        self.val_kappas = []\n\n    def on_epoch_end(self, epoch, logs={}):\n        X_val, y_val = self.validation_data[:2]\n        y_val = y_val.sum(axis=1) - 1\n\n        y_pred = self.model.predict(X_val) > 0.5\n        y_pred = y_pred.astype(int).sum(axis=1) - 1\n\n        _val_kappa = cohen_kappa_score(\n            y_val,\n            y_pred,\n            weights='quadratic'\n        )\n        self.val_kappas.append(_val_kappa)\n        print(f\"\\nval_kappa: {_val_kappa:.4f}\")\n        if _val_kappa == max(self.val_kappas):\n            print(\"Validation Kappa has improved ⬆️  and Saving Model\")\n            self.model.save('best_model_ov.keras')\n        else:\n            print(\"Validation Kappa has not improved ⬇️ \")\n        return\n\ndef precision(y_true, y_pred):\n    true_positives = K.sum(K.round(K.clip(y_true * y_pred, 0, 1)))\n    predicted_positives = K.sum(K.round(K.clip(y_pred, 0, 1)))\n    return true_positives / (predicted_positives + K.epsilon())\n\ndef recall(y_true, y_pred):\n    true_positives = K.sum(K.round(K.clip(y_true * y_pred, 0, 1)))\n    actual_positives = K.sum(K.round(K.clip(y_true, 0, 1)))\n    return true_positives / (actual_positives + K.epsilon())","metadata":{"execution":{"iopub.execute_input":"2023-11-12T03:39:09.959941Z","iopub.status.busy":"2023-11-12T03:39:09.959576Z","iopub.status.idle":"2023-11-12T03:39:09.971033Z","shell.execute_reply":"2023-11-12T03:39:09.969969Z","shell.execute_reply.started":"2023-11-12T03:39:09.959912Z"},"id":"wF-K92py3gJd","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Creating The Model DenseNet","metadata":{"id":"JZbcpaQQluOr"}},{"cell_type":"code","source":"resize_size = 224\ndensenet = DenseNet121(\n    weights='/kaggle/input/densenet-bc-121-32-no-top-h5/DenseNet-BC-121-32-no-top.h5',\n    include_top=False,\n    input_shape=(resize_size,resize_size,3)\n)\n\ndef build_model():\n    model = Sequential()\n    model.add(densenet)\n    model.add(GlobalAveragePooling2D())\n    model.add(Dropout(0.5))\n    model.add(Dense(128, activation='relu'))\n    model.add(Dense(64, activation='relu'))\n    model.add(Dense(32, activation='relu'))\n    model.add(Dense(5, activation='sigmoid'))\n\n    model.compile(\n        loss='binary_crossentropy',\n        optimizer=Adam(learning_rate=0.00005),\n        metrics=['accuracy',precision,recall]\n    )\n    return model\n\nmodel = build_model()\nmodel.summary()","metadata":{"execution":{"iopub.execute_input":"2023-11-12T03:39:27.379073Z","iopub.status.busy":"2023-11-12T03:39:27.378396Z","iopub.status.idle":"2023-11-12T03:39:35.127073Z","shell.execute_reply":"2023-11-12T03:39:35.126127Z","shell.execute_reply.started":"2023-11-12T03:39:27.379039Z"},"id":"Oqzjo6WFcZDq","outputId":"49a258de-30f8-4055-9e94-b99b9e9f01d2","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Creating The Model VGG19","metadata":{}},{"cell_type":"code","source":"resize_size = 224\nvgg19 = VGG19(\n    weights='imagenet',\n    include_top=False,\n    input_shape=(resize_size,resize_size,3)\n)\n\ndef build_model():\n    model = Sequential()\n    model.add(vgg19)\n    model.add(GlobalAveragePooling2D())\n    model.add(Dropout(0.5))\n    model.add(Dense(128, activation='relu'))\n    model.add(Dense(64, activation='relu'))\n    model.add(Dense(32, activation='relu'))\n    model.add(Dense(5, activation='sigmoid'))\n\n    model.compile(\n        loss='binary_crossentropy',\n        optimizer=Adam(learning_rate=0.00005),\n        metrics=['accuracy',precision,recall]\n    )\n    return model\n\nmodel = build_model()\nmodel.summary()","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Creating The Model EfficientNetB6","metadata":{}},{"cell_type":"code","source":"resize_size = 224\nefficientnet = EfficientNetB6(\n    weights='imagenet',\n    include_top=False,\n    input_shape=(resize_size,resize_size,3)\n)\n\ndef build_model():\n    model = Sequential()\n    model.add(efficientnet)\n    model.add(GlobalAveragePooling2D())\n    model.add(Dropout(0.5))\n    model.add(Dense(128, activation='relu'))\n    model.add(Dense(64, activation='relu'))\n    model.add(Dense(32, activation='relu'))\n    model.add(Dense(5, activation='sigmoid'))\n\n    model.compile(\n        loss='binary_crossentropy',\n        optimizer=Adam(learning_rate=0.00005),\n        metrics=['accuracy',precision,recall]\n    )\n    return model\n\nmodel = build_model()\nmodel.summary()","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"validation_data = (x_val,y_val)\nkpa = Metrics(validation_data)\n\nhistory = model.fit(\n    data_generator,\n    steps_per_epoch=x_train.shape[0] /BATCH_SIZE,\n    epochs=35,\n    validation_data=validation_data,\n    callbacks=[kpa]\n)\n\nhistory_df = pd.DataFrame(history.history).to_csv('history_densenet.csv', index=False)\nmodel.save('model_densenet.keras')","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import confusion_matrix\nfrom sklearn.metrics import classification_report\n\nprefix = 'densenet'\nhistory_df = pd.read_csv('/kaggle/working/history_densenet.csv')\nplt.figure(figsize=(20, 10))\nplt.plot(history_df['accuracy'], label='Train',linewidth=3)\nplt.plot(history_df['val_accuracy'], label='Validation',linewidth=3)\nplt.xlabel(\"Epochs\", fontsize=20)\nplt.ylabel(\"Accuracy\", fontsize=20)\nplt.legend(loc='upper right', fontsize=10)\nax = plt.gca()\nax.set_facecolor('white')\nplt.savefig(f'{prefix}_accuracy.png')\nplt.show()\nprint(\"=========================================================================\")\n\nplt.figure(figsize=(20, 10))\nplt.plot(history_df['loss'], label='Train',linewidth=3)\nplt.plot(history_df['val_loss'], label='Validation',linewidth=3)\nplt.xlabel(\"Epochs\", fontsize=20)\nplt.ylabel(\"Loss\", fontsize=20)\nplt.legend(loc='upper right', fontsize=10)\nax = plt.gca()\nax.set_facecolor('white')\nplt.savefig(f'{prefix}_loss.png')\nplt.show()\nprint(\"=========================================================================\")\n\nplt.figure(figsize=(20, 10))\nplt.plot(history_df['precision'], label='Train',linewidth=3)\nplt.plot(history_df['val_precision'], label='Validation',linewidth=3)\nplt.xlabel(\"Epochs\", fontsize=20)\nplt.ylabel(\"Precision\", fontsize=20)\nplt.legend(loc='upper right', fontsize=10)\nax = plt.gca()\nax.set_facecolor('white')\nplt.savefig(f'{prefix}_precision.png')\nplt.show()\nprint(\"=========================================================================\")\n\nplt.figure(figsize=(20, 10))\nplt.plot(history_df['recall'], label='Train',linewidth=3)\nplt.plot(history_df['val_recall'], label='Validation',linewidth=3)\nplt.xlabel(\"Epochs\", fontsize=20)\nplt.ylabel(\"Recall\", fontsize=20)\nplt.legend(loc='upper right', fontsize=10)\nax = plt.gca()\nax.set_facecolor('white')\nplt.savefig(f'{prefix}_recall.png')\nplt.show()\n\nprint(\"=========================================================================\")\n\ny_pred = model.predict(x_val)\n\nnames = [\"No DR\", \"Mild\", \"Moderate\", \"Severe\", \"Proliferative DR\"]\ntrue =[]\npred = []\nseverity = 0\nfor i in range(y_pred.shape[0]):\n    prediction = []\n    true_sample = []\n    for j in range(y_pred[i].shape[0]):\n        if(y_val[i][j]==True):\n            true_sample.append(1)\n        else:\n            true_sample.append(0)\n        if(y_pred[i][j]>0.7):\n            prediction.append(1)\n        else:\n            prediction.append(0)\n\n    pred.append(prediction.count(1)-1)\n    true.append(true_sample.count(1)-1)\n    print(\"Prediction: \",names[prediction.count(1)-1], \" <=====> \" ,\"True: \",names[true_sample.count(1)-1])\n    if prediction == list(y_val[i]):\n       severity = severity+1\n    print(\"=====================================================================\")\n\nprint(\"=========================================================================\")\n\nprint(\"=============================Accuracy====================================\")\nprint(\"Accuracy: \", math.ceil(severity/y_pred.shape[0]*100),\"%\")\nprint(\"=========================================================================\")\n\n\nprint(\"=======================classification Report=============================\")\nprint(classification_report(true, pred, target_names=names,zero_division=0))\nprint(\"=========================================================================\")\n\ncm = confusion_matrix(true, pred)\nplt.figure(figsize=(20, 10))\nsns.heatmap(cm, annot=True, fmt='d', cmap='Blues', xticklabels=names, yticklabels=names)\nplt.xlabel(\"Predicted\", fontsize=20)\nplt.ylabel(\"Actual\", fontsize=20)\nplt.savefig(f'{prefix}_confusion_matrix.png')\nplt.show()","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"validation_data = (x_val,y_val)\nkpa = Metrics(validation_data)\n\nhistory = model.fit(\n    data_generator,\n    steps_per_epoch=x_train.shape[0] /BATCH_SIZE,\n    epochs=35,\n    validation_data=validation_data,\n    callbacks=[kpa]\n)\n\nhistory_df = pd.DataFrame(history.history).to_csv('history_vgg19.csv', index=False)\nmodel.save('model_vgg19.keras')","metadata":{"id":"cmrW4npsehES","outputId":"aa459276-48c1-4096-d180-f770c76faff1"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import confusion_matrix\nfrom sklearn.metrics import classification_report\n\nprefix = 'vgg19'\nhistory_df = pd.read_csv('/kaggle/working/history_vgg19.csv')\nplt.figure(figsize=(20, 10))\nplt.plot(history_df['accuracy'], label='Train',linewidth=3)\nplt.plot(history_df['val_accuracy'], label='Validation',linewidth=3)\nplt.xlabel(\"Epochs\", fontsize=20)\nplt.ylabel(\"Accuracy\", fontsize=20)\nplt.legend(loc='upper right', fontsize=10)\nax = plt.gca()\nax.set_facecolor('white')\nplt.savefig(f'{prefix}_accuracy.png')\nplt.show()\nprint(\"=========================================================================\")\n\nplt.figure(figsize=(20, 10))\nplt.plot(history_df['loss'], label='Train',linewidth=3)\nplt.plot(history_df['val_loss'], label='Validation',linewidth=3)\nplt.xlabel(\"Epochs\", fontsize=20)\nplt.ylabel(\"Loss\", fontsize=20)\nplt.legend(loc='upper right', fontsize=10)\nax = plt.gca()\nax.set_facecolor('white')\nplt.savefig(f'{prefix}_loss.png')\nplt.show()\nprint(\"=========================================================================\")\n\nplt.figure(figsize=(20, 10))\nplt.plot(history_df['precision'], label='Train',linewidth=3)\nplt.plot(history_df['val_precision'], label='Validation',linewidth=3)\nplt.xlabel(\"Epochs\", fontsize=20)\nplt.ylabel(\"Precision\", fontsize=20)\nplt.legend(loc='upper right', fontsize=10)\nax = plt.gca()\nax.set_facecolor('white')\nplt.savefig(f'{prefix}_precision.png')\nplt.show()\nprint(\"=========================================================================\")\n\nplt.figure(figsize=(20, 10))\nplt.plot(history_df['recall'], label='Train',linewidth=3)\nplt.plot(history_df['val_recall'], label='Validation',linewidth=3)\nplt.xlabel(\"Epochs\", fontsize=20)\nplt.ylabel(\"Recall\", fontsize=20)\nplt.legend(loc='upper right', fontsize=10)\nax = plt.gca()\nax.set_facecolor('white')\nplt.savefig(f'{prefix}_recall.png')\nplt.show()\n\nprint(\"=========================================================================\")\n\ny_pred = model.predict(x_val)\n\nnames = [\"No DR\", \"Mild\", \"Moderate\", \"Severe\", \"Proliferative DR\"]\ntrue =[]\npred = []\nseverity = 0\nfor i in range(y_pred.shape[0]):\n    prediction = []\n    true_sample = []\n    for j in range(y_pred[i].shape[0]):\n        if(y_val[i][j]==True):\n            true_sample.append(1)\n        else:\n            true_sample.append(0)\n        if(y_pred[i][j]>0.7):\n            prediction.append(1)\n        else:\n            prediction.append(0)\n\n    pred.append(prediction.count(1)-1)\n    true.append(true_sample.count(1)-1)\n    print(\"Prediction: \",names[prediction.count(1)-1], \" <=====> \" ,\"True: \",names[true_sample.count(1)-1])\n    if prediction == list(y_val[i]):\n       severity = severity+1\n    print(\"=====================================================================\")\n\nprint(\"=========================================================================\")\n\nprint(\"=============================Accuracy====================================\")\nprint(\"Accuracy: \", math.ceil(severity/y_pred.shape[0]*100),\"%\")\nprint(\"=========================================================================\")\n\n\nprint(\"=======================classification Report=============================\")\nprint(classification_report(true, pred, target_names=names,zero_division=0))\nprint(\"=========================================================================\")\n\ncm = confusion_matrix(true, pred)\nplt.figure(figsize=(20, 10))\nsns.heatmap(cm, annot=True, fmt='d', cmap='Blues', xticklabels=names, yticklabels=names)\nplt.xlabel(\"Predicted\", fontsize=20)\nplt.ylabel(\"Actual\", fontsize=20)\nplt.savefig(f'{prefix}_confusion_matrix.png')\nplt.show()","metadata":{"id":"q1tCYBr4iHcT","outputId":"3121ecc2-cd0d-4880-8d99-397c516c5bac"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"validation_data = (x_val,y_val)\nkpa = Metrics(validation_data)\n\nhistory = model.fit(\n    data_generator,\n    steps_per_epoch=x_train.shape[0] /BATCH_SIZE,\n    epochs=35,\n    validation_data=validation_data,\n    callbacks=[kpa]\n)\n\nhistory_df = pd.DataFrame(history.history).to_csv('history_efficientnet.csv', index=False)\nmodel.save('model_efficientnet.keras')","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import confusion_matrix\nfrom sklearn.metrics import classification_report\n\nprefix = 'efficientnet'\nhistory_df = pd.read_csv('/kaggle/working/history_efficientnet.csv')\nplt.figure(figsize=(20, 10))\nplt.plot(history_df['accuracy'], label='Train',linewidth=3)\nplt.plot(history_df['val_accuracy'], label='Validation',linewidth=3)\nplt.xlabel(\"Epochs\", fontsize=20)\nplt.ylabel(\"Accuracy\", fontsize=20)\nplt.legend(loc='upper right', fontsize=10)\nax = plt.gca()\nax.set_facecolor('white')\nplt.savefig(f'{prefix}_accuracy.png')\nplt.show()\nprint(\"=========================================================================\")\n\nplt.figure(figsize=(20, 10))\nplt.plot(history_df['loss'], label='Train',linewidth=3)\nplt.plot(history_df['val_loss'], label='Validation',linewidth=3)\nplt.xlabel(\"Epochs\", fontsize=20)\nplt.ylabel(\"Loss\", fontsize=20)\nplt.legend(loc='upper right', fontsize=10)\nax = plt.gca()\nax.set_facecolor('white')\nplt.savefig(f'{prefix}_loss.png')\nplt.show()\nprint(\"=========================================================================\")\n\nplt.figure(figsize=(20, 10))\nplt.plot(history_df['precision'], label='Train',linewidth=3)\nplt.plot(history_df['val_precision'], label='Validation',linewidth=3)\nplt.xlabel(\"Epochs\", fontsize=20)\nplt.ylabel(\"Precision\", fontsize=20)\nplt.legend(loc='upper right', fontsize=10)\nax = plt.gca()\nax.set_facecolor('white')\nplt.savefig(f'{prefix}_precision.png')\nplt.show()\nprint(\"=========================================================================\")\n\nplt.figure(figsize=(20, 10))\nplt.plot(history_df['recall'], label='Train',linewidth=3)\nplt.plot(history_df['val_recall'], label='Validation',linewidth=3)\nplt.xlabel(\"Epochs\", fontsize=20)\nplt.ylabel(\"Recall\", fontsize=20)\nplt.legend(loc='upper right', fontsize=10)\nax = plt.gca()\nax.set_facecolor('white')\nplt.savefig(f'{prefix}_recall.png')\nplt.show()\n\nprint(\"=========================================================================\")\n\ny_pred = model.predict(x_val)\n\nnames = [\"No DR\", \"Mild\", \"Moderate\", \"Severe\", \"Proliferative DR\"]\ntrue =[]\npred = []\nseverity = 0\nfor i in range(y_pred.shape[0]):\n    prediction = []\n    true_sample = []\n    for j in range(y_pred[i].shape[0]):\n        if(y_val[i][j]==True):\n            true_sample.append(1)\n        else:\n            true_sample.append(0)\n        if(y_pred[i][j]>0.7):\n            prediction.append(1)\n        else:\n            prediction.append(0)\n\n    pred.append(prediction.count(1)-1)\n    true.append(true_sample.count(1)-1)\n    print(\"Prediction: \",names[prediction.count(1)-1], \" <=====> \" ,\"True: \",names[true_sample.count(1)-1])\n    if prediction == list(y_val[i]):\n       severity = severity+1\n    print(\"=====================================================================\")\n\nprint(\"=========================================================================\")\n\nprint(\"=============================Accuracy====================================\")\nprint(\"Accuracy: \", math.ceil(severity/y_pred.shape[0]*100),\"%\")\nprint(\"=========================================================================\")\n\n\nprint(\"=======================classification Report=============================\")\nprint(classification_report(true, pred, target_names=names,zero_division=0))\nprint(\"=========================================================================\")\n\ncm = confusion_matrix(true, pred)\nplt.figure(figsize=(20, 10))\nsns.heatmap(cm, annot=True, fmt='d', cmap='Blues', xticklabels=names, yticklabels=names)\nplt.xlabel(\"Predicted\", fontsize=20)\nplt.ylabel(\"Actual\", fontsize=20)\nplt.savefig(f'{prefix}_confusion_matrix.png')\nplt.show()","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Fix imbalance","metadata":{"id":"3QFg9GfF6Jxu"}},{"cell_type":"code","source":"validation_data = (x_val,y_val)\nkpa = Metrics(validation_data)\n\nhistory = model.fit(\n    data_generator,\n    steps_per_epoch=x_train.shape[0] /BATCH_SIZE,\n    epochs=25,\n    validation_data=validation_data,\n    callbacks=[kpa]\n)\n\nhistory_df = pd.DataFrame(history.history).to_csv('history_densenet_ov.csv', index=False)\nmodel.save('model_denesenet_ov.keras')","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import confusion_matrix\nfrom sklearn.metrics import classification_report\n\nprefix = 'denesenet_ov'\nhistory_df = pd.read_csv('/kaggle/working/history_densenet_ov.csv')\nplt.figure(figsize=(20, 10))\nplt.plot(history_df['accuracy'], label='Train',linewidth=3)\nplt.plot(history_df['val_accuracy'], label='Validation',linewidth=3)\nplt.xlabel(\"Epochs\", fontsize=20)\nplt.ylabel(\"Accuracy\", fontsize=20)\nplt.legend(loc='upper right', fontsize=10)\nax = plt.gca()\nax.set_facecolor('white')\nplt.savefig(f'{prefix}_accuracy.png')\nplt.show()\nprint(\"=========================================================================\")\n\nplt.figure(figsize=(20, 10))\nplt.plot(history_df['loss'], label='Train',linewidth=3)\nplt.plot(history_df['val_loss'], label='Validation',linewidth=3)\nplt.xlabel(\"Epochs\", fontsize=20)\nplt.ylabel(\"Loss\", fontsize=20)\nplt.legend(loc='upper right', fontsize=10)\nax = plt.gca()\nax.set_facecolor('white')\nplt.savefig(f'{prefix}_loss.png')\nplt.show()\nprint(\"=========================================================================\")\n\nplt.figure(figsize=(20, 10))\nplt.plot(history_df['precision'], label='Train',linewidth=3)\nplt.plot(history_df['val_precision'], label='Validation',linewidth=3)\nplt.xlabel(\"Epochs\", fontsize=20)\nplt.ylabel(\"Precision\", fontsize=20)\nplt.legend(loc='upper right', fontsize=10)\nax = plt.gca()\nax.set_facecolor('white')\nplt.savefig(f'{prefix}_precision.png')\nplt.show()\nprint(\"=========================================================================\")\n\nplt.figure(figsize=(20, 10))\nplt.plot(history_df['recall'], label='Train',linewidth=3)\nplt.plot(history_df['val_recall'], label='Validation',linewidth=3)\nplt.xlabel(\"Epochs\", fontsize=20)\nplt.ylabel(\"Recall\", fontsize=20)\nplt.legend(loc='upper right', fontsize=10)\nax = plt.gca()\nax.set_facecolor('white')\nplt.savefig(f'{prefix}_recall.png')\nplt.show()\n\nprint(\"=========================================================================\")\n\ny_pred = model.predict(x_val)\n\nnames = [\"No DR\", \"Mild\", \"Moderate\", \"Severe\", \"Proliferative DR\"]\ntrue =[]\npred = []\nseverity = 0\nfor i in range(y_pred.shape[0]):\n    prediction = []\n    true_sample = []\n    for j in range(y_pred[i].shape[0]):\n        if(y_val[i][j]==True):\n            true_sample.append(1)\n        else:\n            true_sample.append(0)\n        if(y_pred[i][j]>0.7):\n            prediction.append(1)\n        else:\n            prediction.append(0)\n\n    pred.append(prediction.count(1)-1)\n    true.append(true_sample.count(1)-1)\n    print(\"Prediction: \",names[prediction.count(1)-1], \" <=====> \" ,\"True: \",names[true_sample.count(1)-1])\n    if prediction == list(y_val[i]):\n       severity = severity+1\n    print(\"=====================================================================\")\n\nprint(\"=========================================================================\")\n\nprint(\"=============================Accuracy====================================\")\nprint(\"Accuracy: \", math.ceil(severity/y_pred.shape[0]*100),\"%\")\nprint(\"=========================================================================\")\n\n\nprint(\"=======================classification Report=============================\")\nprint(classification_report(true, pred, target_names=names,zero_division=0))\nprint(\"=========================================================================\")\n\ncm = confusion_matrix(true, pred)\nplt.figure(figsize=(20, 10))\nsns.heatmap(cm, annot=True, fmt='d', cmap='Blues', xticklabels=names, yticklabels=names)\nplt.xlabel(\"Predicted\", fontsize=20)\nplt.ylabel(\"Actual\", fontsize=20)\nplt.savefig(f'{prefix}_confusion_matrix.png')\nplt.show()","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"validation_data = (x_val,y_val)\nkpa = Metrics(validation_data)\n\nhistory = model.fit(\n    data_generator,\n    steps_per_epoch=x_train.shape[0] /BATCH_SIZE,\n    epochs=35,\n    validation_data=validation_data,\n    callbacks=[kpa],\n)\n\nhistory_df = pd.DataFrame(history.history).to_csv('history_vgg19_ov.csv', index=False)\nmodel.save('model_vgg19_ov.keras')","metadata":{"execution":{"iopub.execute_input":"2023-11-12T03:40:33.722512Z","iopub.status.busy":"2023-11-12T03:40:33.721731Z","iopub.status.idle":"2023-11-12T04:53:09.238578Z","shell.execute_reply":"2023-11-12T04:53:09.237680Z","shell.execute_reply.started":"2023-11-12T03:40:33.722477Z"},"id":"aYK9tuut3bRe","outputId":"4b0b604a-72a5-4107-bc30-1c4a465f9e1d","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import confusion_matrix\nfrom sklearn.metrics import classification_report\n\nprefix = 'vgg19_ov'\nhistory_df = pd.read_csv('/kaggle/working/history_vgg19_ov.csv')\nplt.figure(figsize=(20, 10))\nplt.plot(history_df['accuracy'], label='Train',linewidth=3)\nplt.plot(history_df['val_accuracy'], label='Validation',linewidth=3)\nplt.xlabel(\"Epochs\", fontsize=20)\nplt.ylabel(\"Accuracy\", fontsize=20)\nplt.legend(loc='upper right', fontsize=10)\nax = plt.gca()\nax.set_facecolor('white')\nplt.savefig(f'{prefix}_accuracy.png')\nplt.show()\nprint(\"=========================================================================\")\n\nplt.figure(figsize=(20, 10))\nplt.plot(history_df['loss'], label='Train',linewidth=3)\nplt.plot(history_df['val_loss'], label='Validation',linewidth=3)\nplt.xlabel(\"Epochs\", fontsize=20)\nplt.ylabel(\"Loss\", fontsize=20)\nplt.legend(loc='upper right', fontsize=10)\nax = plt.gca()\nax.set_facecolor('white')\nplt.savefig(f'{prefix}_loss.png')\nplt.show()\nprint(\"=========================================================================\")\n\nplt.figure(figsize=(20, 10))\nplt.plot(history_df['precision'], label='Train',linewidth=3)\nplt.plot(history_df['val_precision'], label='Validation',linewidth=3)\nplt.xlabel(\"Epochs\", fontsize=20)\nplt.ylabel(\"Precision\", fontsize=20)\nplt.legend(loc='upper right', fontsize=10)\nax = plt.gca()\nax.set_facecolor('white')\nplt.savefig(f'{prefix}_precision.png')\nplt.show()\nprint(\"=========================================================================\")\n\nplt.figure(figsize=(20, 10))\nplt.plot(history_df['recall'], label='Train',linewidth=3)\nplt.plot(history_df['val_recall'], label='Validation',linewidth=3)\nplt.xlabel(\"Epochs\", fontsize=20)\nplt.ylabel(\"Recall\", fontsize=20)\nplt.legend(loc='upper right', fontsize=10)\nax = plt.gca()\nax.set_facecolor('white')\nplt.savefig(f'{prefix}_recall.png')\nplt.show()\n\nprint(\"=========================================================================\")\n\ny_pred = model.predict(x_val)\n\nnames = [\"No DR\", \"Mild\", \"Moderate\", \"Severe\", \"Proliferative DR\"]\ntrue =[]\npred = []\nseverity = 0\nfor i in range(y_pred.shape[0]):\n    prediction = []\n    true_sample = []\n    for j in range(y_pred[i].shape[0]):\n        if(y_val[i][j]==True):\n            true_sample.append(1)\n        else:\n            true_sample.append(0)\n        if(y_pred[i][j]>0.7):\n            prediction.append(1)\n        else:\n            prediction.append(0)\n\n    pred.append(prediction.count(1)-1)\n    true.append(true_sample.count(1)-1)\n    print(\"Prediction: \",names[prediction.count(1)-1], \" <=====> \" ,\"True: \",names[true_sample.count(1)-1])\n    if prediction == list(y_val[i]):\n       severity = severity+1\n    print(\"=====================================================================\")\n\nprint(\"=========================================================================\")\n\nprint(\"=============================Accuracy====================================\")\nprint(\"Accuracy: \", math.ceil(severity/y_pred.shape[0]*100),\"%\")\nprint(\"=========================================================================\")\n\n\nprint(\"=======================classification Report=============================\")\nprint(classification_report(true, pred, target_names=names,zero_division=0))\nprint(\"=========================================================================\")\n\ncm = confusion_matrix(true, pred)\nplt.figure(figsize=(20, 10))\nsns.heatmap(cm, annot=True, fmt='d', cmap='Blues', xticklabels=names, yticklabels=names)\nplt.xlabel(\"Predicted\", fontsize=20)\nplt.ylabel(\"Actual\", fontsize=20)\nplt.savefig(f'{prefix}_confusion_matrix.png')\nplt.show()","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"validation_data = (x_val,y_val)\nkpa = Metrics(validation_data)\n\nhistory = model.fit(\n    data_generator,\n    steps_per_epoch=x_train.shape[0] /BATCH_SIZE,\n    epochs=25,\n    validation_data=validation_data,\n    callbacks=[kpa],\n)\n\nhistory_df = pd.DataFrame(history.history).to_csv('history_efficientnet_ov.csv', index=False)\nmodel.save('model_efficientnet_ov.keras')","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import confusion_matrix\nfrom sklearn.metrics import classification_report\n\nprefix = 'efficientnet_ov'\nhistory_df = pd.read_csv('/kaggle/working/history_efficientnet_ov.csv')\nplt.figure(figsize=(20, 10))\nplt.plot(history_df['accuracy'], label='Train',linewidth=3)\nplt.plot(history_df['val_accuracy'], label='Validation',linewidth=3)\nplt.xlabel(\"Epochs\", fontsize=20)\nplt.ylabel(\"Accuracy\", fontsize=20)\nplt.legend(loc='upper right', fontsize=10)\nax = plt.gca()\nax.set_facecolor('white')\nplt.savefig(f'{prefix}_accuracy.png')\nplt.show()\nprint(\"=========================================================================\")\n\nplt.figure(figsize=(20, 10))\nplt.plot(history_df['loss'], label='Train',linewidth=3)\nplt.plot(history_df['val_loss'], label='Validation',linewidth=3)\nplt.xlabel(\"Epochs\", fontsize=20)\nplt.ylabel(\"Loss\", fontsize=20)\nplt.legend(loc='upper right', fontsize=10)\nax = plt.gca()\nax.set_facecolor('white')\nplt.savefig(f'{prefix}_loss.png')\nplt.show()\nprint(\"=========================================================================\")\n\nplt.figure(figsize=(20, 10))\nplt.plot(history_df['precision'], label='Train',linewidth=3)\nplt.plot(history_df['val_precision'], label='Validation',linewidth=3)\nplt.xlabel(\"Epochs\", fontsize=20)\nplt.ylabel(\"Precision\", fontsize=20)\nplt.legend(loc='upper right', fontsize=10)\nax = plt.gca()\nax.set_facecolor('white')\nplt.savefig(f'{prefix}_precision.png')\nplt.show()\nprint(\"=========================================================================\")\n\nplt.figure(figsize=(20, 10))\nplt.plot(history_df['recall'], label='Train',linewidth=3)\nplt.plot(history_df['val_recall'], label='Validation',linewidth=3)\nplt.xlabel(\"Epochs\", fontsize=20)\nplt.ylabel(\"Recall\", fontsize=20)\nplt.legend(loc='upper right', fontsize=10)\nax = plt.gca()\nax.set_facecolor('white')\nplt.savefig(f'{prefix}_recall.png')\nplt.show()\n\nprint(\"=========================================================================\")\n\ny_pred = model.predict(x_val)\n\nnames = [\"No DR\", \"Mild\", \"Moderate\", \"Severe\", \"Proliferative DR\"]\ntrue =[]\npred = []\nseverity = 0\nfor i in range(y_pred.shape[0]):\n    prediction = []\n    true_sample = []\n    for j in range(y_pred[i].shape[0]):\n        if(y_val[i][j]==True):\n            true_sample.append(1)\n        else:\n            true_sample.append(0)\n        if(y_pred[i][j]>0.7):\n            prediction.append(1)\n        else:\n            prediction.append(0)\n\n    pred.append(prediction.count(1)-1)\n    true.append(true_sample.count(1)-1)\n    print(\"Prediction: \",names[prediction.count(1)-1], \" <=====> \" ,\"True: \",names[true_sample.count(1)-1])\n    if prediction == list(y_val[i]):\n       severity = severity+1\n    print(\"=====================================================================\")\n\nprint(\"=========================================================================\")\n\nprint(\"=============================Accuracy====================================\")\nprint(\"Accuracy: \", math.ceil(severity/y_pred.shape[0]*100),\"%\")\nprint(\"=========================================================================\")\n\n\nprint(\"=======================classification Report=============================\")\nprint(classification_report(true, pred, target_names=names,zero_division=0))\nprint(\"=========================================================================\")\n\ncm = confusion_matrix(true, pred)\nplt.figure(figsize=(20, 10))\nsns.heatmap(cm, annot=True, fmt='d', cmap='Blues', xticklabels=names, yticklabels=names)\nplt.xlabel(\"Predicted\", fontsize=20)\nplt.ylabel(\"Actual\", fontsize=20)\nplt.savefig(f'{prefix}_confusion_matrix.png')\nplt.show()","metadata":{"execution":{"iopub.execute_input":"2023-11-12T05:00:57.818432Z","iopub.status.busy":"2023-11-12T05:00:57.817474Z","iopub.status.idle":"2023-11-12T05:01:06.278896Z","shell.execute_reply":"2023-11-12T05:01:06.277816Z","shell.execute_reply.started":"2023-11-12T05:00:57.818398Z"},"id":"CoEZzgA331cO","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def preprocess_image(image_path, desired_size=224):\n    im = cv2.imread(image_path)\n    im = cv2.resize(im, (desired_size, )*2, interpolation=cv2.INTER_LINEAR)\n    im = cv2.addWeighted(im, 4,cv2.GaussianBlur(im,(0,0), 300/30), -4,128)\n    im = np.expand_dims(im, axis=0)\n    return im\n\ndef test(model,images_path=None,plote = False,image=None):\n    if images_path != None:\n        img = preprocess_image(images_path)\n        result = model.predict(img,verbose=0)\n    else:\n        img = np.expand_dims(image, axis=0)\n        result = model.predict(img,verbose=0)\n\n    prediction = -1\n    for i in result[0]:\n        if i >= 0.7:\n            prediction += 1\n        else:\n            pass\n    if plote:\n        if prediction != -1:\n            plt.figure(figsize=(5, 5))\n            plt.imshow(img[0])\n            plt.grid(False)\n            plt.axis(False)\n            plt.title(f\"Prediction : {names[prediction]}\",fontsize=20)\n            plt.show()\n            print(\"Prediction : \", names[prediction])\n            print(\"prediction probability : \", result[0])\n\n        else:\n            plt.figure(figsize=(10, 10))\n            plt.imshow(img[0])\n            plt.grid(False)\n            plt.axis(False)\n            plt.title(f\"Prediction : Unknown\",fontsize=20)\n            plt.show()\n            print(\"Prediction : Unknown\")\n            print(\"prediction probability : \", result[0])\n\n    return prediction\n\n# images = os.listdir(\"/kaggle/input/aptos2019-blindness-detection/test_images/\")\n\npredictions = []\nfor image in tqdm(range(len(x_val))):\n    predictions.append(test(model,image=x_val[image],plote=False))\n\npredictions = np.array(predictions)\nprint(\"No DR: \", np.sum(predictions == 0))\nprint(\"Mild: \", np.sum(predictions == 1))\nprint(\"Moderate: \", np.sum(predictions == 2))\nprint(\"Severe: \", np.sum(predictions == 3))\nprint(\"Proliferative DR: \", np.sum(predictions == 4))","metadata":{"execution":{"iopub.execute_input":"2023-11-12T05:01:55.413625Z","iopub.status.busy":"2023-11-12T05:01:55.412691Z","iopub.status.idle":"2023-11-12T05:05:01.532436Z","shell.execute_reply":"2023-11-12T05:05:01.531654Z","shell.execute_reply.started":"2023-11-12T05:01:55.413592Z"},"id":"zBWwbAKYjPnU","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"images = os.listdir(\"/kaggle/input/aptos2019-blindness-detection/test_images/\")\ntest(model,images_path=\"/kaggle/input/aptos2019-blindness-detection/test_images/\"+images[10],plote=True)","metadata":{"execution":{"iopub.execute_input":"2023-11-12T05:17:11.493383Z","iopub.status.busy":"2023-11-12T05:17:11.492922Z","iopub.status.idle":"2023-11-12T05:17:12.365493Z","shell.execute_reply":"2023-11-12T05:17:12.364518Z","shell.execute_reply.started":"2023-11-12T05:17:11.493350Z"},"id":"EBNUnyyyjRic","trusted":true},"execution_count":null,"outputs":[]}]}