{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import numpy as np # linear algebra\nimport pandas as pd ","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-10-30T18:43:29.238083Z","iopub.execute_input":"2022-10-30T18:43:29.238487Z","iopub.status.idle":"2022-10-30T18:43:29.243788Z","shell.execute_reply.started":"2022-10-30T18:43:29.238456Z","shell.execute_reply":"2022-10-30T18:43:29.242399Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os, sys\nimport pathlib\nimport random\nimport sklearn\nimport matplotlib.pyplot as plt\nimport cv2\nfrom sklearn.model_selection import train_test_split\nfrom PIL import Image\nimport warnings\nwarnings.filterwarnings(\"ignore\")\nIMG_SIZE = 512","metadata":{"execution":{"iopub.status.busy":"2022-10-30T18:43:29.248151Z","iopub.execute_input":"2022-10-30T18:43:29.248499Z","iopub.status.idle":"2022-10-30T18:43:29.256029Z","shell.execute_reply.started":"2022-10-30T18:43:29.248461Z","shell.execute_reply":"2022-10-30T18:43:29.255145Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"DATASET_PATH = '../input/diabetic-eye-diseases-using-deep-learning/dataset/3_retina_disease'","metadata":{"execution":{"iopub.status.busy":"2022-10-30T18:43:29.258004Z","iopub.execute_input":"2022-10-30T18:43:29.258453Z","iopub.status.idle":"2022-10-30T18:43:29.267922Z","shell.execute_reply.started":"2022-10-30T18:43:29.258413Z","shell.execute_reply":"2022-10-30T18:43:29.267228Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import glob\nretina_path = os.path.join(DATASET_PATH, '*')\nprint(retina_path)\nretina_path = glob.glob(retina_path)\n ","metadata":{"execution":{"iopub.status.busy":"2022-10-30T18:52:49.226450Z","iopub.execute_input":"2022-10-30T18:52:49.226892Z","iopub.status.idle":"2022-10-30T18:52:49.247046Z","shell.execute_reply.started":"2022-10-30T18:52:49.226858Z","shell.execute_reply":"2022-10-30T18:52:49.246291Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from mpl_toolkits.axes_grid1 import ImageGrid","metadata":{"execution":{"iopub.status.busy":"2022-10-30T18:53:16.590154Z","iopub.execute_input":"2022-10-30T18:53:16.590525Z","iopub.status.idle":"2022-10-30T18:53:16.595608Z","shell.execute_reply.started":"2022-10-30T18:53:16.590496Z","shell.execute_reply":"2022-10-30T18:53:16.594425Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig = plt.figure(figsize=(26., 16.))\ngrid = ImageGrid(fig, 111,  # similar to subplot(111)\n                 nrows_ncols=(4, 5),  # creates 2x2 grid of axes\n                 axes_pad=0.5,  # pad between axes in inch.\n                 )\nfor ax, im in zip(grid, retina_path[34:54]):\n    image = cv2.imread(im)\n    image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)\n    image = cv2.resize(image, (IMG_SIZE, IMG_SIZE))\n    ax.imshow(image)\n    ax.set_title(im[-7:])","metadata":{"execution":{"iopub.status.busy":"2022-10-30T18:43:29.319982Z","iopub.execute_input":"2022-10-30T18:43:29.320512Z","iopub.status.idle":"2022-10-30T18:43:35.299561Z","shell.execute_reply.started":"2022-10-30T18:43:29.320471Z","shell.execute_reply":"2022-10-30T18:43:35.298309Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Not so visible of Hard Exudates and Cotton Wool spots**","metadata":{}},{"cell_type":"code","source":"def crop_image_from_gray(img,tol=7):\n    if img.ndim ==2:\n        mask = img>tol\n        return img[np.ix_(mask.any(1),mask.any(0))]\n    elif img.ndim==3:\n        gray_img = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)\n        mask = gray_img>tol\n        \n        check_shape = img[:,:,0][np.ix_(mask.any(1),mask.any(0))].shape[0]\n        if (check_shape == 0): # image is too dark so that we crop out everything,\n            return img # return original image\n        else:\n            img1=img[:,:,0][np.ix_(mask.any(1),mask.any(0))]\n            img2=img[:,:,1][np.ix_(mask.any(1),mask.any(0))]\n            img3=img[:,:,2][np.ix_(mask.any(1),mask.any(0))]\n    #         print(img1.shape,img2.shape,img3.shape)\n            img = np.stack([img1,img2,img3],axis=-1)\n    #         print(img.shape)\n        return img\n","metadata":{"execution":{"iopub.status.busy":"2022-10-30T18:57:33.665334Z","iopub.execute_input":"2022-10-30T18:57:33.665742Z","iopub.status.idle":"2022-10-30T18:57:33.676852Z","shell.execute_reply.started":"2022-10-30T18:57:33.665707Z","shell.execute_reply":"2022-10-30T18:57:33.675312Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def load_ben_color(path, sigmaX=10):\n    image = cv2.imread(path)\n    image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)\n    image = crop_image_from_gray(image)\n    image = cv2.resize(image, (IMG_SIZE, IMG_SIZE))\n    image=cv2.addWeighted ( image,4, cv2.GaussianBlur( image , (0,0) , sigmaX) ,-4 ,128)\n        \n    return image","metadata":{"execution":{"iopub.status.busy":"2022-10-30T18:57:38.056066Z","iopub.execute_input":"2022-10-30T18:57:38.056481Z","iopub.status.idle":"2022-10-30T18:57:38.062852Z","shell.execute_reply.started":"2022-10-30T18:57:38.056450Z","shell.execute_reply":"2022-10-30T18:57:38.062037Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def circle_crop(img, sigmaX=10):   \n    \"\"\"\n    Create circular crop around image centre    \n    \"\"\"    \n    \n    img = cv2.imread(img)\n    img = crop_image_from_gray(img)    \n    img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)\n    \n    height, width, depth = img.shape    \n    \n    x = int(width/2)\n    y = int(height/2)\n    r = np.amin((x,y))\n    \n    circle_img = np.zeros((height, width), np.uint8)\n    cv2.circle(circle_img, (x,y), int(r), 1, thickness=-1)\n    img = cv2.bitwise_and(img, img, mask=circle_img)\n    img = crop_image_from_gray(img)\n    img=cv2.addWeighted ( img,4, cv2.GaussianBlur( img , (0,0) , sigmaX) ,-4 ,128)\n    return img ","metadata":{"execution":{"iopub.status.busy":"2022-10-30T18:57:40.691117Z","iopub.execute_input":"2022-10-30T18:57:40.691556Z","iopub.status.idle":"2022-10-30T18:57:40.700859Z","shell.execute_reply.started":"2022-10-30T18:57:40.691523Z","shell.execute_reply":"2022-10-30T18:57:40.699454Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig = plt.figure(figsize=(26., 16.))\ngrid = ImageGrid(fig, 111,  # similar to subplot(111)\n                 nrows_ncols=(4, 5),  # creates 2x2 grid of axes\n                 axes_pad=0.5,  # pad between axes in inch.\n                 )\nfor ax, im in zip(grid, retina_path[1:20]):\n    image = load_ben_color(im,sigmaX=30)\n    ax.imshow(image)\n    ax.set_title(im[-7:])","metadata":{"execution":{"iopub.status.busy":"2022-10-30T19:00:31.689582Z","iopub.execute_input":"2022-10-30T19:00:31.690046Z","iopub.status.idle":"2022-10-30T19:00:41.988146Z","shell.execute_reply.started":"2022-10-30T19:00:31.690010Z","shell.execute_reply":"2022-10-30T19:00:41.987117Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Now we can see blood vessel more clear. Look at image 70**","metadata":{}}]}