{"metadata":{"kernelspec":{"name":"python3","display_name":"Python 3","language":"python"},"language_info":{"name":"python","version":"3.7.10","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"gpu","dataSources":[{"sourceId":4104,"databundleVersionId":46661,"sourceType":"competition"},{"sourceId":418031,"sourceType":"datasetVersion","datasetId":131128},{"sourceId":6892698,"sourceType":"datasetVersion","datasetId":3959653}],"dockerImageVersionId":30097,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport os\nimport cv2\nfrom tqdm import tqdm\nfrom keras import backend as K\nimport gc\nfrom skimage import color, filters, measure, io","metadata":{"execution":{"iopub.status.busy":"2023-11-25T18:23:40.502360Z","iopub.execute_input":"2023-11-25T18:23:40.502724Z","iopub.status.idle":"2023-11-25T18:23:40.507682Z","shell.execute_reply.started":"2023-11-25T18:23:40.502694Z","shell.execute_reply":"2023-11-25T18:23:40.506504Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels = [\"No DR\", \"Mild\", \"Moderate\", \"Severe\", \"Proliferative DR\"]\npath = \"/kaggle/input/diabetic-retinopathy-resized/\"\n\ninpshape = (300,300)","metadata":{"execution":{"iopub.status.busy":"2023-11-25T18:23:40.730345Z","iopub.execute_input":"2023-11-25T18:23:40.730701Z","iopub.status.idle":"2023-11-25T18:23:40.734796Z","shell.execute_reply.started":"2023-11-25T18:23:40.730671Z","shell.execute_reply":"2023-11-25T18:23:40.733856Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data = pd.read_csv(path+'trainLabels_cropped.csv')","metadata":{"execution":{"iopub.status.busy":"2023-11-25T18:23:40.954247Z","iopub.execute_input":"2023-11-25T18:23:40.954595Z","iopub.status.idle":"2023-11-25T18:23:40.988649Z","shell.execute_reply.started":"2023-11-25T18:23:40.954566Z","shell.execute_reply":"2023-11-25T18:23:40.987752Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data.shape","metadata":{"execution":{"iopub.status.busy":"2023-11-25T18:23:41.169448Z","iopub.execute_input":"2023-11-25T18:23:41.169801Z","iopub.status.idle":"2023-11-25T18:23:41.175042Z","shell.execute_reply.started":"2023-11-25T18:23:41.169772Z","shell.execute_reply":"2023-11-25T18:23:41.174133Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data.head(10)","metadata":{"execution":{"iopub.status.busy":"2023-11-25T18:23:41.365953Z","iopub.execute_input":"2023-11-25T18:23:41.366346Z","iopub.status.idle":"2023-11-25T18:23:41.377554Z","shell.execute_reply.started":"2023-11-25T18:23:41.366311Z","shell.execute_reply":"2023-11-25T18:23:41.376554Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data = data.sample(frac=1, random_state=42)","metadata":{"execution":{"iopub.status.busy":"2023-11-25T18:23:41.581619Z","iopub.execute_input":"2023-11-25T18:23:41.581981Z","iopub.status.idle":"2023-11-25T18:23:41.591674Z","shell.execute_reply.started":"2023-11-25T18:23:41.581945Z","shell.execute_reply":"2023-11-25T18:23:41.590754Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data","metadata":{"execution":{"iopub.status.busy":"2023-11-25T18:23:41.795752Z","iopub.execute_input":"2023-11-25T18:23:41.796126Z","iopub.status.idle":"2023-11-25T18:23:41.811419Z","shell.execute_reply.started":"2023-11-25T18:23:41.796088Z","shell.execute_reply":"2023-11-25T18:23:41.810469Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dx = []\ndy = []","metadata":{"execution":{"iopub.status.busy":"2023-11-25T18:23:42.058844Z","iopub.execute_input":"2023-11-25T18:23:42.059188Z","iopub.status.idle":"2023-11-25T18:23:42.064255Z","shell.execute_reply.started":"2023-11-25T18:23:42.059158Z","shell.execute_reply":"2023-11-25T18:23:42.062687Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"np.unique(data['level'],return_counts=1)","metadata":{"execution":{"iopub.status.busy":"2023-11-25T18:23:42.475057Z","iopub.execute_input":"2023-11-25T18:23:42.475409Z","iopub.status.idle":"2023-11-25T18:23:42.482909Z","shell.execute_reply.started":"2023-11-25T18:23:42.475379Z","shell.execute_reply":"2023-11-25T18:23:42.481906Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"take = [100]*5","metadata":{"execution":{"iopub.status.busy":"2023-11-25T18:23:42.799394Z","iopub.execute_input":"2023-11-25T18:23:42.799729Z","iopub.status.idle":"2023-11-25T18:23:42.803586Z","shell.execute_reply.started":"2023-11-25T18:23:42.799702Z","shell.execute_reply":"2023-11-25T18:23:42.802645Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"take","metadata":{"execution":{"iopub.status.busy":"2023-11-25T18:23:43.139348Z","iopub.execute_input":"2023-11-25T18:23:43.139714Z","iopub.status.idle":"2023-11-25T18:23:43.144884Z","shell.execute_reply.started":"2023-11-25T18:23:43.139684Z","shell.execute_reply":"2023-11-25T18:23:43.143994Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data = data.iloc[np.random.permutation(data.shape[0])]","metadata":{"execution":{"iopub.status.busy":"2023-11-25T18:23:43.562959Z","iopub.execute_input":"2023-11-25T18:23:43.563305Z","iopub.status.idle":"2023-11-25T18:23:43.572102Z","shell.execute_reply.started":"2023-11-25T18:23:43.563275Z","shell.execute_reply":"2023-11-25T18:23:43.571256Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in tqdm(np.array(data)):\n    if take[i[3]] > 0:\n#         img = cv2.cvtColor(cv2.resize(cv2.imread(path+\"images/resized_train_cropped/\"+i[0]+\".jpeg\"),inpshape)[:,:,::-1],cv2.COLOR_RGB2GRAY)/255\n        img = cv2.resize(cv2.imread(f\"/kaggle/input/diabetic-retinopathy-resized/resized_train_cropped/resized_train_cropped/{i[2]}.jpeg\"),inpshape)[:,:,::-1]/255\n        dx.append(img)\n        dy.append(i[3])\n        take[i[3]] -= 1\n        if np.sum(take) <=0:\n            break","metadata":{"scrolled":true,"execution":{"iopub.status.busy":"2023-11-25T18:23:44.427687Z","iopub.execute_input":"2023-11-25T18:23:44.428034Z","iopub.status.idle":"2023-11-25T18:23:57.946474Z","shell.execute_reply.started":"2023-11-25T18:23:44.427995Z","shell.execute_reply":"2023-11-25T18:23:57.945580Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x = np.array(dx)\ny = np.array(dy)","metadata":{"execution":{"iopub.status.busy":"2023-11-25T18:54:39.785732Z","iopub.execute_input":"2023-11-25T18:54:39.786107Z","iopub.status.idle":"2023-11-25T18:54:40.180636Z","shell.execute_reply.started":"2023-11-25T18:54:39.786076Z","shell.execute_reply":"2023-11-25T18:54:40.179747Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"np.unique(y,return_counts=1)","metadata":{"execution":{"iopub.status.busy":"2023-11-25T18:54:41.200583Z","iopub.execute_input":"2023-11-25T18:54:41.200922Z","iopub.status.idle":"2023-11-25T18:54:41.206770Z","shell.execute_reply.started":"2023-11-25T18:54:41.200891Z","shell.execute_reply":"2023-11-25T18:54:41.205810Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x.shape , y.shape","metadata":{"scrolled":true,"execution":{"iopub.status.busy":"2023-11-25T18:54:41.446999Z","iopub.execute_input":"2023-11-25T18:54:41.447343Z","iopub.status.idle":"2023-11-25T18:54:41.452666Z","shell.execute_reply.started":"2023-11-25T18:54:41.447310Z","shell.execute_reply":"2023-11-25T18:54:41.451802Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"i = 0","metadata":{"execution":{"iopub.status.busy":"2023-11-25T18:54:41.739101Z","iopub.execute_input":"2023-11-25T18:54:41.739474Z","iopub.status.idle":"2023-11-25T18:54:41.743136Z","shell.execute_reply.started":"2023-11-25T18:54:41.739439Z","shell.execute_reply":"2023-11-25T18:54:41.742220Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(x[i])\nprint(labels[y[i]])\ni+=1","metadata":{"execution":{"iopub.status.busy":"2023-11-25T18:54:42.014738Z","iopub.execute_input":"2023-11-25T18:54:42.015041Z","iopub.status.idle":"2023-11-25T18:54:42.191977Z","shell.execute_reply.started":"2023-11-25T18:54:42.015004Z","shell.execute_reply":"2023-11-25T18:54:42.191126Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def segmentImg(img):\n    microaneurysm_threshold = 0.5\n    microaneurysm_min_area = 5\n    microaneurysm_max_area = 100\n    exudate_threshold = 0.7\n    exudate_min_area = 20\n    exudate_max_area = 500\n\n    # Convert the image to grayscale\n    gray_image = color.rgb2gray(img)\n\n    # Apply Gaussian blur to reduce noise\n    blurred = filters.gaussian(gray_image, sigma=1)\n\n    # Microaneurysm detection\n    microaneurysm_binary_image = blurred > microaneurysm_threshold\n    microaneurysm_labeled_image, num_microaneurysms = measure.label(microaneurysm_binary_image, connectivity=2, return_num=True)\n\n    # Exudate detection\n    exudate_binary_image = blurred > exudate_threshold\n    exudate_labeled_image, num_exudates = measure.label(exudate_binary_image, connectivity=2, return_num=True)\n\n    # Store detection data for the current image\n    # image_data = {\n    #     \"Image File\": image_file,\n    #     \"Microaneurysms Detected\": num_microaneurysms,\n    #     \"Exudates Detected\": num_exudates\n    # }\n    # all_detection_data = []\n    # all_detection_data.append(image_data)\n\n\n    fig, ax = plt.subplots()\n    ax.imshow(img, cmap='gray')\n\n    # Mark microaneurysms\n    for region in measure.regionprops(microaneurysm_labeled_image):\n        yp, xp = region.centroid\n        circle = plt.Circle((xp, yp), 2, color='r', fill=False)\n        ax.add_patch(circle)\n\n    # Mark exudates\n    for region in measure.regionprops(exudate_labeled_image):\n        yp, xp = region.centroid\n        circle = plt.Circle((xp, yp), 2, color='b', fill=False)\n        ax.add_patch(circle)\n\n    plt.title(f\"Segmented Image\")\n    plt.show()\n\n    # Generate a basic report\n    print(\"Detection Report:\")\n    print(f\"  Microaneurysms Detected: {num_microaneurysms}\")\n    print(f\"  Exudates Detected: {num_exudates}\")","metadata":{"execution":{"iopub.status.busy":"2023-11-25T18:56:18.189488Z","iopub.execute_input":"2023-11-25T18:56:18.189847Z","iopub.status.idle":"2023-11-25T18:56:18.200059Z","shell.execute_reply.started":"2023-11-25T18:56:18.189816Z","shell.execute_reply":"2023-11-25T18:56:18.199057Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"p = np.random.permutation(y.shape[0])\n\nx = x[p]\ny = y[p]","metadata":{"execution":{"iopub.status.busy":"2023-11-25T18:54:42.955054Z","iopub.execute_input":"2023-11-25T18:54:42.955413Z","iopub.status.idle":"2023-11-25T18:54:43.269060Z","shell.execute_reply.started":"2023-11-25T18:54:42.955382Z","shell.execute_reply":"2023-11-25T18:54:43.268297Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"valsplit = round(y.shape[0]*.2)\nvalsplit","metadata":{"execution":{"iopub.status.busy":"2023-11-25T18:54:43.527490Z","iopub.execute_input":"2023-11-25T18:54:43.527829Z","iopub.status.idle":"2023-11-25T18:54:43.533297Z","shell.execute_reply.started":"2023-11-25T18:54:43.527800Z","shell.execute_reply":"2023-11-25T18:54:43.532428Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_val = x[:valsplit]\ny_val = y[:valsplit]\n# x = x[:]\n# y = y[:]","metadata":{"execution":{"iopub.status.busy":"2023-11-25T18:54:44.027306Z","iopub.execute_input":"2023-11-25T18:54:44.027630Z","iopub.status.idle":"2023-11-25T18:54:44.031481Z","shell.execute_reply.started":"2023-11-25T18:54:44.027602Z","shell.execute_reply":"2023-11-25T18:54:44.030601Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"np.unique(y,return_counts=True), np.unique(y_val,return_counts=True)","metadata":{"execution":{"iopub.status.busy":"2023-11-25T18:54:44.619068Z","iopub.execute_input":"2023-11-25T18:54:44.619448Z","iopub.status.idle":"2023-11-25T18:54:44.628644Z","shell.execute_reply.started":"2023-11-25T18:54:44.619416Z","shell.execute_reply":"2023-11-25T18:54:44.627813Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf\nimport keras\nfrom keras import layers\nfrom keras.models import Sequential\nfrom keras.applications import DenseNet201\nfrom keras.callbacks import Callback, ModelCheckpoint, ReduceLROnPlateau\nfrom keras.preprocessing.image import ImageDataGenerator","metadata":{"execution":{"iopub.status.busy":"2023-11-25T18:54:45.067837Z","iopub.execute_input":"2023-11-25T18:54:45.068179Z","iopub.status.idle":"2023-11-25T18:54:45.072977Z","shell.execute_reply.started":"2023-11-25T18:54:45.068145Z","shell.execute_reply":"2023-11-25T18:54:45.071962Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"BATCH_SIZE = 16\n\ntrain_generator = ImageDataGenerator(\n        rotation_range=5,\n        zoom_range=[0.7,1],\n        brightness_range=[.8,1.2],\n        horizontal_flip=True,\n    )","metadata":{"execution":{"iopub.status.busy":"2023-11-25T18:24:05.637463Z","iopub.execute_input":"2023-11-25T18:24:05.637828Z","iopub.status.idle":"2023-11-25T18:24:05.642649Z","shell.execute_reply.started":"2023-11-25T18:24:05.637797Z","shell.execute_reply":"2023-11-25T18:24:05.641679Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def build_model(backbone, lr=1e-4):\n    model = Sequential()\n    model.add(backbone)\n    model.add(layers.GlobalAveragePooling2D())\n    model.add(layers.BatchNormalization())\n    model.add(layers.Dense(np.unique(y).shape[0], activation='softmax'))\n    \n    \n    model.compile(loss= tf.losses.sparse_categorical_crossentropy,\n                  optimizer = tf.optimizers.Adam(learning_rate=lr),\n                  metrics=[\"acc\"])\n    \n    return model","metadata":{"execution":{"iopub.status.busy":"2023-11-25T18:24:15.921859Z","iopub.execute_input":"2023-11-25T18:24:15.922230Z","iopub.status.idle":"2023-11-25T18:24:15.928834Z","shell.execute_reply.started":"2023-11-25T18:24:15.922175Z","shell.execute_reply":"2023-11-25T18:24:15.927629Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"K.clear_session()\ngc.collect()\n\nresnet = DenseNet201(\n    weights='imagenet',\n    include_top=False,\n    input_shape=(inpshape[0],inpshape[1],3)\n)\n\n\nmodel = build_model(resnet ,lr = 1e-4)\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2023-11-25T18:24:17.187731Z","iopub.execute_input":"2023-11-25T18:24:17.188086Z","iopub.status.idle":"2023-11-25T18:24:26.079639Z","shell.execute_reply.started":"2023-11-25T18:24:17.188054Z","shell.execute_reply":"2023-11-25T18:24:26.078679Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn_control = ReduceLROnPlateau(monitor='val_accuracy', patience=5,verbose=1,factor=0.2, min_lr=1e-7)\n\nfilepath=\"weights_best.h5\"\ncheckpoint = ModelCheckpoint(filepath, monitor='val_accuracy', verbose=1, save_best_only=True, mode='max')","metadata":{"execution":{"iopub.status.busy":"2023-11-25T18:24:26.081243Z","iopub.execute_input":"2023-11-25T18:24:26.081612Z","iopub.status.idle":"2023-11-25T18:24:26.086922Z","shell.execute_reply.started":"2023-11-25T18:24:26.081573Z","shell.execute_reply":"2023-11-25T18:24:26.085907Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model.fit(\n    x,y,\n    epochs=15,\n    validation_data=(x_val, y_val),\n)","metadata":{"execution":{"iopub.status.busy":"2023-11-25T18:40:35.340240Z","iopub.execute_input":"2023-11-25T18:40:35.340605Z","iopub.status.idle":"2023-11-25T18:42:38.950977Z","shell.execute_reply.started":"2023-11-25T18:40:35.340575Z","shell.execute_reply":"2023-11-25T18:42:38.950133Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(history.history[\"acc\"],label=\"train acc\")\nplt.plot(history.history[\"val_acc\"],label=\"val acc\")\nplt.legend()","metadata":{"execution":{"iopub.status.busy":"2023-11-25T18:44:11.121403Z","iopub.execute_input":"2023-11-25T18:44:11.121818Z","iopub.status.idle":"2023-11-25T18:44:11.347154Z","shell.execute_reply.started":"2023-11-25T18:44:11.121784Z","shell.execute_reply":"2023-11-25T18:44:11.346282Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(history.history[\"loss\"],label=\"train loss\")\nplt.plot(history.history[\"val_loss\"],label=\"val loss\")\nplt.legend()","metadata":{"execution":{"iopub.status.busy":"2023-11-25T18:44:12.229561Z","iopub.execute_input":"2023-11-25T18:44:12.229882Z","iopub.status.idle":"2023-11-25T18:44:12.387146Z","shell.execute_reply.started":"2023-11-25T18:44:12.229855Z","shell.execute_reply":"2023-11-25T18:44:12.386305Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# model.save(\"DR_t92.h5\")","metadata":{"execution":{"iopub.status.busy":"2023-11-25T18:57:52.011617Z","iopub.execute_input":"2023-11-25T18:57:52.012057Z","iopub.status.idle":"2023-11-25T18:57:52.016187Z","shell.execute_reply.started":"2023-11-25T18:57:52.012018Z","shell.execute_reply":"2023-11-25T18:57:52.015127Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"i = 0","metadata":{"execution":{"iopub.status.busy":"2023-11-25T18:57:58.821777Z","iopub.execute_input":"2023-11-25T18:57:58.822118Z","iopub.status.idle":"2023-11-25T18:57:58.825869Z","shell.execute_reply.started":"2023-11-25T18:57:58.822088Z","shell.execute_reply":"2023-11-25T18:57:58.824990Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred = model.predict(np.array([x_val[i]]),verbose=0)\np = np.argmax(pred)\nprint(f\"pred: {labels[p]} --- actual: {labels[y[i]]}\")\nsegmentImg(x_val[i])\ni+=1","metadata":{"execution":{"iopub.status.busy":"2023-11-25T19:03:16.940267Z","iopub.execute_input":"2023-11-25T19:03:16.940618Z","iopub.status.idle":"2023-11-25T19:03:17.286605Z","shell.execute_reply.started":"2023-11-25T19:03:16.940589Z","shell.execute_reply":"2023-11-25T19:03:17.285669Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import confusion_matrix,classification_report\np = np.argmax(model.predict(x_val),axis=1)\nprint(confusion_matrix(y_val,p))\nprint(classification_report(y_val,p))","metadata":{"execution":{"iopub.status.busy":"2023-11-25T19:00:20.719718Z","iopub.execute_input":"2023-11-25T19:00:20.720096Z","iopub.status.idle":"2023-11-25T19:00:21.374397Z","shell.execute_reply.started":"2023-11-25T19:00:20.720058Z","shell.execute_reply":"2023-11-25T19:00:21.373430Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}