{"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":"# **I. IMAGE PREPROCESSING TECHNIQUES**","metadata":{}},{"cell_type":"code","source":"import numpy as np \nimport pandas as pd \nimport math\n\nimport cv2\nimport matplotlib.pyplot as plt\nimport matplotlib\n\nimport os","metadata":{"execution":{"iopub.status.busy":"2023-06-25T23:46:42.679460Z","iopub.execute_input":"2023-06-25T23:46:42.680099Z","iopub.status.idle":"2023-06-25T23:46:42.830856Z","shell.execute_reply.started":"2023-06-25T23:46:42.680064Z","shell.execute_reply":"2023-06-25T23:46:42.829890Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(os.listdir('/kaggle/input/aptos2019-blindness-detection/'))","metadata":{"execution":{"iopub.status.busy":"2023-06-25T23:46:47.024278Z","iopub.execute_input":"2023-06-25T23:46:47.024726Z","iopub.status.idle":"2023-06-25T23:46:47.034014Z","shell.execute_reply.started":"2023-06-25T23:46:47.024689Z","shell.execute_reply":"2023-06-25T23:46:47.032937Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"root = '/kaggle/input/aptos2019-blindness-detection/'\ntrain_df = pd.read_csv(os.path.join(root, 'train.csv'))","metadata":{"execution":{"iopub.status.busy":"2023-06-25T23:46:52.122585Z","iopub.execute_input":"2023-06-25T23:46:52.122926Z","iopub.status.idle":"2023-06-25T23:46:52.132764Z","shell.execute_reply.started":"2023-06-25T23:46:52.122900Z","shell.execute_reply":"2023-06-25T23:46:52.131806Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.shape","metadata":{"execution":{"iopub.status.busy":"2023-06-25T23:46:56.708408Z","iopub.execute_input":"2023-06-25T23:46:56.708832Z","iopub.status.idle":"2023-06-25T23:46:56.720466Z","shell.execute_reply.started":"2023-06-25T23:46:56.708790Z","shell.execute_reply":"2023-06-25T23:46:56.719556Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.head(10)","metadata":{"execution":{"iopub.status.busy":"2023-06-25T23:47:06.954487Z","iopub.execute_input":"2023-06-25T23:47:06.954828Z","iopub.status.idle":"2023-06-25T23:47:06.969790Z","shell.execute_reply.started":"2023-06-25T23:47:06.954801Z","shell.execute_reply":"2023-06-25T23:47:06.968792Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df['diagnosis'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2023-06-25T23:47:18.580752Z","iopub.execute_input":"2023-06-25T23:47:18.581129Z","iopub.status.idle":"2023-06-25T23:47:18.594706Z","shell.execute_reply.started":"2023-06-25T23:47:18.581100Z","shell.execute_reply":"2023-06-25T23:47:18.593606Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig=plt.figure(figsize=(8, 5))\ntrain_df['diagnosis'].value_counts().plot.bar()","metadata":{"execution":{"iopub.status.busy":"2023-06-25T23:47:33.146758Z","iopub.execute_input":"2023-06-25T23:47:33.147132Z","iopub.status.idle":"2023-06-25T23:47:33.439245Z","shell.execute_reply.started":"2023-06-25T23:47:33.147104Z","shell.execute_reply":"2023-06-25T23:47:33.438296Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"SEED = 125\nfig = plt.figure(figsize=(25, 16))\nimg_list = []\nimg_size = []\n\nfor class_id in [0, 1, 2, 3, 4]:\n    for i, (idx, row) in enumerate(train_df.loc[train_df['diagnosis'] == class_id].sample(5, random_state=SEED).iterrows()):\n        ax = fig.add_subplot(5, 5, class_id * 5 + i + 1, xticks=[], yticks=[])\n        path = os.path.join(root, 'train_images', '{}.png'.format(row['id_code']))\n        image = cv2.imread(path)\n        img_size.append(image.shape)\n        image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)\n        img_list.append(row['id_code'])\n        \n        plt.imshow(image)\n        ax.set_title('Label: %d ' % (class_id) )","metadata":{"execution":{"iopub.status.busy":"2023-06-25T23:47:57.543773Z","iopub.execute_input":"2023-06-25T23:47:57.544146Z","iopub.status.idle":"2023-06-25T23:48:23.275407Z","shell.execute_reply.started":"2023-06-25T23:47:57.544115Z","shell.execute_reply":"2023-06-25T23:48:23.274099Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## **1. Median Subtraction**","metadata":{}},{"cell_type":"code","source":"IMAGE_SIZE = 256\n\ndef info_image(im):\n    cy = im.shape[0]//2\n    midline = im[cy,:]\n    midline = np.where(midline>midline.mean()/3)[0]\n    if len(midline)>im.shape[1]//2:\n        x_start, x_end = np.min(midline), np.max(midline)\n    else: \n        x_start, x_end = im.shape[1]//10, 9*im.shape[1]//10\n    cx = (x_start + x_end)/2\n    r = (x_end - x_start)/2\n    return cx, cy, r\n\ndef resize_image(im, augmentation=False):\n    cx, cy, r = info_image(im)\n    scaling = IMAGE_SIZE/(2*r)\n    rotation = 0\n    if augmentation:\n        scaling *= 1 + 0.3 * (np.random.rand()-0.5)\n        rotation = 360 * np.random.rand()\n    M = cv2.getRotationMatrix2D((cx,cy), rotation, scaling)\n    M[0,2] -= cx - IMAGE_SIZE/2\n    M[1,2] -= cy - IMAGE_SIZE/2\n    return cv2.warpAffine(im,M,(IMAGE_SIZE,IMAGE_SIZE))\n\ndef subtract_median_bg_image(im):\n    k = np.max(im.shape)//20*2+1\n    bg = cv2.medianBlur(im, k)\n    return cv2.addWeighted (im, 4, bg, -4, 128)","metadata":{"execution":{"iopub.status.busy":"2023-06-25T23:50:45.025500Z","iopub.execute_input":"2023-06-25T23:50:45.025842Z","iopub.status.idle":"2023-06-25T23:50:45.036219Z","shell.execute_reply.started":"2023-06-25T23:50:45.025816Z","shell.execute_reply":"2023-06-25T23:50:45.035254Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"PARAM = 96\ndef Radius_Reduction(img,PARAM):\n    h,w,c=img.shape\n    Frame=np.zeros((h,w,c),dtype=np.uint8)\n    cv2.circle(Frame,(int(math.floor(w/2)),int(math.floor(h/2))),int(math.floor((h*PARAM)/float(2*100))), (255,255,255), -1)\n    Frame1=cv2.cvtColor(Frame, cv2.COLOR_BGR2GRAY)\n    img1 =cv2.bitwise_and(img,img,mask=Frame1)\n    return img1","metadata":{"execution":{"iopub.status.busy":"2023-06-25T23:51:05.404355Z","iopub.execute_input":"2023-06-25T23:51:05.404709Z","iopub.status.idle":"2023-06-25T23:51:05.412752Z","shell.execute_reply.started":"2023-06-25T23:51:05.404682Z","shell.execute_reply":"2023-06-25T23:51:05.411508Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image = cv2.imread(os.path.join(root, 'train_images', '{}.png'.format(img_list[0])))\nimage = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)\nfig=plt.figure(figsize=(8, 8))\nplt.imshow(image)","metadata":{"execution":{"iopub.status.busy":"2023-06-25T23:51:16.306470Z","iopub.execute_input":"2023-06-25T23:51:16.306831Z","iopub.status.idle":"2023-06-25T23:51:17.072543Z","shell.execute_reply.started":"2023-06-25T23:51:16.306803Z","shell.execute_reply":"2023-06-25T23:51:17.067104Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig=plt.figure(figsize=(8, 8))\nres_image = resize_image(image)\nplt.imshow(res_image)","metadata":{"execution":{"iopub.status.busy":"2023-06-25T23:51:29.546894Z","iopub.execute_input":"2023-06-25T23:51:29.547558Z","iopub.status.idle":"2023-06-25T23:51:30.143552Z","shell.execute_reply.started":"2023-06-25T23:51:29.547525Z","shell.execute_reply":"2023-06-25T23:51:30.139260Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig=plt.figure(figsize=(8, 8))\nsub_med = subtract_median_bg_image(res_image)\nplt.imshow(sub_med)","metadata":{"execution":{"iopub.status.busy":"2023-06-25T23:51:42.889053Z","iopub.execute_input":"2023-06-25T23:51:42.889713Z","iopub.status.idle":"2023-06-25T23:51:43.493878Z","shell.execute_reply.started":"2023-06-25T23:51:42.889680Z","shell.execute_reply":"2023-06-25T23:51:43.487669Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig=plt.figure(figsize=(8, 8))\nimg_rad_red=Radius_Reduction(sub_med, PARAM)\nplt.imshow(img_rad_red)","metadata":{"execution":{"iopub.status.busy":"2023-06-25T23:51:58.405225Z","iopub.execute_input":"2023-06-25T23:51:58.405579Z","iopub.status.idle":"2023-06-25T23:51:58.942704Z","shell.execute_reply.started":"2023-06-25T23:51:58.405552Z","shell.execute_reply":"2023-06-25T23:51:58.941918Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"w=10\nh=10\nfig=plt.figure(figsize=(20, 20))\ncolumns = 5\nrows = 5\nfor i in range(1, columns*rows +1):\n    img = cv2.imread(os.path.join(root, 'train_images', '{}.png'.format(img_list[i-1])))\n    img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)\n    res_image = resize_image(img)\n    sub_med = subtract_median_bg_image(res_image)\n    img_rad_red=Radius_Reduction(sub_med, PARAM)\n    fig.add_subplot(rows, columns, i)\n    plt.imshow(img_rad_red)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-06-25T23:52:18.480523Z","iopub.execute_input":"2023-06-25T23:52:18.480873Z","iopub.status.idle":"2023-06-25T23:52:25.898321Z","shell.execute_reply.started":"2023-06-25T23:52:18.480846Z","shell.execute_reply":"2023-06-25T23:52:25.897088Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## **2. Gamma Correction**","metadata":{}},{"cell_type":"code","source":"image = cv2.imread(os.path.join(root, 'train_images', '{}.png'.format(img_list[0])))\nimage = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)\nres_image = resize_image(image)\n\nmatplotlib.rc('figure', figsize=[7, 7])\nplt.imshow(res_image)","metadata":{"execution":{"iopub.status.busy":"2023-06-25T23:53:00.516991Z","iopub.execute_input":"2023-06-25T23:53:00.517879Z","iopub.status.idle":"2023-06-25T23:53:01.051521Z","shell.execute_reply.started":"2023-06-25T23:53:00.517829Z","shell.execute_reply":"2023-06-25T23:53:01.049685Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def adjust_gamma(image, gamma):\n    invGamma = 1.0 / gamma\n    table = np.array([((i / 255.0) ** invGamma) * 255\n                for i in np.arange(0, 256)]).astype(\"uint8\")\n    return cv2.LUT(image, table)","metadata":{"execution":{"iopub.status.busy":"2023-06-25T23:53:19.454998Z","iopub.execute_input":"2023-06-25T23:53:19.455390Z","iopub.status.idle":"2023-06-25T23:53:19.461018Z","shell.execute_reply.started":"2023-06-25T23:53:19.455362Z","shell.execute_reply":"2023-06-25T23:53:19.460094Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"adjusted = adjust_gamma(res_image, gamma=0.5)\nadjusted_75 = adjust_gamma(res_image, gamma=0.75)\nadjusted_15 = adjust_gamma(res_image, gamma=1.5)\nadjusted_3 = adjust_gamma(res_image, gamma=2.5)","metadata":{"execution":{"iopub.status.busy":"2023-06-25T23:53:30.486279Z","iopub.execute_input":"2023-06-25T23:53:30.486669Z","iopub.status.idle":"2023-06-25T23:53:30.497273Z","shell.execute_reply.started":"2023-06-25T23:53:30.486641Z","shell.execute_reply":"2023-06-25T23:53:30.496227Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"matplotlib.rc('figure', figsize=[15, 15])\n\nfig, axarr = plt.subplots(2,2)\naxarr[0,0].imshow(adjusted)\naxarr[0,1].imshow(adjusted_75)\naxarr[1,0].imshow(adjusted_15)\naxarr[1,1].imshow(adjusted_3)","metadata":{"execution":{"iopub.status.busy":"2023-06-25T23:53:41.929388Z","iopub.execute_input":"2023-06-25T23:53:41.929743Z","iopub.status.idle":"2023-06-25T23:53:43.569287Z","shell.execute_reply.started":"2023-06-25T23:53:41.929714Z","shell.execute_reply":"2023-06-25T23:53:43.568106Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### **Selective Gamma Correction**","metadata":{}},{"cell_type":"code","source":"Parameter = 95\n\ndef Redius_Reduction(img, Parameter):\n    h,w,c=img.shape\n    Frame=np.zeros((h,w,c),dtype=np.uint8)\n    cv2.circle(Frame,(int(math.floor(w/2)),int(math.floor(h/2))),int(math.floor((h*Parameter)/float(2*100))), (255,255,255), -1)\n    Frame1=cv2.cvtColor(Frame, cv2.COLOR_BGR2GRAY)\n    img1 =cv2.bitwise_and(img,img,mask=Frame1)\n    return img1, Frame1","metadata":{"execution":{"iopub.status.busy":"2023-06-25T23:54:49.626439Z","iopub.execute_input":"2023-06-25T23:54:49.626809Z","iopub.status.idle":"2023-06-25T23:54:49.635176Z","shell.execute_reply.started":"2023-06-25T23:54:49.626780Z","shell.execute_reply":"2023-06-25T23:54:49.633658Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"w=10\nh=10\nfig=plt.figure(figsize=(20, 20))\ncolumns = 5\nrows = 5\nfor i in range(1, columns*rows +1):\n    img = cv2.imread(os.path.join(root, 'train_images', '{}.png'.format(img_list[i-1])))\n    img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)\n    res_image = resize_image(img)\n    img_rad_red, mask = Redius_Reduction(res_image, Parameter)\n    fig.add_subplot(rows, columns, i)\n    plt.imshow(img_rad_red)\n    \nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-06-25T23:55:10.878637Z","iopub.execute_input":"2023-06-25T23:55:10.878984Z","iopub.status.idle":"2023-06-25T23:55:18.521125Z","shell.execute_reply.started":"2023-06-25T23:55:10.878957Z","shell.execute_reply":"2023-06-25T23:55:18.520265Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"w=10\nh=10\nfig=plt.figure(figsize=(20, 20))\ncolumns = 5\nrows = 5\nfor i in range(1, columns*rows +1):\n    \n    img = cv2.imread(os.path.join(root, 'train_images', '{}.png'.format(img_list[i-1])))\n    img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)\n    res_image = resize_image(img)  \n    img_rad_red, mask = Redius_Reduction(img, Parameter)\n    pixel_list = img_rad_red[:,:,1][np.where(mask == 255)].tolist()\n    \n    print('Median:', np.median(pixel_list))\n    adjusted_img = None\n    \n    if (np.median(pixel_list) < 70):\n        print('Inside')\n        adjusted_img = adjust_gamma(res_image, gamma = 1.65)\n        \n    elif (np.median(pixel_list) > 180):\n        adjusted_img = adjust_gamma(res_image, gamma = 0.75)\n        \n    else: \n        adjusted_img = res_image\n    \n    fig.add_subplot(rows, columns, i)\n    plt.imshow(adjusted_img)\n    \nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-06-25T23:55:51.595081Z","iopub.execute_input":"2023-06-25T23:55:51.596738Z","iopub.status.idle":"2023-06-25T23:56:20.183551Z","shell.execute_reply.started":"2023-06-25T23:55:51.596687Z","shell.execute_reply":"2023-06-25T23:56:20.182608Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## **3. Adaptive Histogram Equalization**","metadata":{}},{"cell_type":"code","source":"lab= cv2.cvtColor(res_image, cv2.COLOR_BGR2LAB)\nmatplotlib.rc('figure', figsize=[7, 7])\nplt.imshow(lab)","metadata":{"execution":{"iopub.status.busy":"2023-06-25T23:56:42.399190Z","iopub.execute_input":"2023-06-25T23:56:42.399553Z","iopub.status.idle":"2023-06-25T23:56:42.962748Z","shell.execute_reply.started":"2023-06-25T23:56:42.399525Z","shell.execute_reply":"2023-06-25T23:56:42.961855Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"l, a, b = cv2.split(lab)\nclahe = cv2.createCLAHE(clipLimit=3.0, tileGridSize=(8,8))\ncl = clahe.apply(l)\nmatplotlib.rc('figure', figsize=[7, 7])\nplt.imshow(cl)","metadata":{"execution":{"iopub.status.busy":"2023-06-25T23:57:06.637985Z","iopub.execute_input":"2023-06-25T23:57:06.638378Z","iopub.status.idle":"2023-06-25T23:57:07.086732Z","shell.execute_reply.started":"2023-06-25T23:57:06.638351Z","shell.execute_reply":"2023-06-25T23:57:07.085943Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"limg = cv2.merge((cl,a,b))\nmatplotlib.rc('figure', figsize=[7, 7])\nplt.imshow(limg)","metadata":{"execution":{"iopub.status.busy":"2023-06-25T23:57:20.287056Z","iopub.execute_input":"2023-06-25T23:57:20.288085Z","iopub.status.idle":"2023-06-25T23:57:20.725741Z","shell.execute_reply.started":"2023-06-25T23:57:20.288020Z","shell.execute_reply":"2023-06-25T23:57:20.724927Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"final = cv2.cvtColor(limg, cv2.COLOR_LAB2BGR)\nmatplotlib.rc('figure', figsize=[7, 7])\nplt.imshow(final)","metadata":{"execution":{"iopub.status.busy":"2023-06-25T23:57:32.490889Z","iopub.execute_input":"2023-06-25T23:57:32.491458Z","iopub.status.idle":"2023-06-25T23:57:32.953808Z","shell.execute_reply.started":"2023-06-25T23:57:32.491427Z","shell.execute_reply":"2023-06-25T23:57:32.952921Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"w=10\nh=10\nfig=plt.figure(figsize=(20, 20))\ncolumns = 5\nrows = 5\nfor i in range(1, columns*rows +1):\n    img = cv2.imread(os.path.join(root, 'train_images', '{}.png'.format(img_list[i-1])))\n    img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)\n    res_image = resize_image(img)\n    lab= cv2.cvtColor(res_image, cv2.COLOR_BGR2LAB)\n    l, a, b = cv2.split(lab)\n    clahe = cv2.createCLAHE(clipLimit=3.0, tileGridSize=(8,8))\n    cl = clahe.apply(l)\n    limg = cv2.merge((cl,a,b))\n    final = cv2.cvtColor(limg, cv2.COLOR_LAB2BGR)\n    fig.add_subplot(rows, columns, i)\n    plt.imshow(final)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-06-25T23:57:50.231320Z","iopub.execute_input":"2023-06-25T23:57:50.231682Z","iopub.status.idle":"2023-06-25T23:57:58.358370Z","shell.execute_reply.started":"2023-06-25T23:57:50.231652Z","shell.execute_reply":"2023-06-25T23:57:58.351119Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## **4. Contrast Stretching**","metadata":{}},{"cell_type":"code","source":"def contrast_stretching(img):        \n    rr, gg, bb = cv2.split(img)    \n    imgray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)    \n    im = imgray    \n    ih, iw = imgray.shape    \n    (minVal, maxVal, minLoc, maxLoc) = cv2.minMaxLoc(imgray)    \n    for i in range(ih):        \n        for j in range(iw):            \n            im[i, j] = 255 * ((gg[i, j] - minVal) / (maxVal - minVal))        \n    limg = cv2.merge((rr, im, bb))    \n    return limg","metadata":{"execution":{"iopub.status.busy":"2023-06-25T23:58:42.379178Z","iopub.execute_input":"2023-06-25T23:58:42.379638Z","iopub.status.idle":"2023-06-25T23:58:42.390174Z","shell.execute_reply.started":"2023-06-25T23:58:42.379596Z","shell.execute_reply":"2023-06-25T23:58:42.389074Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"contrast_image = contrast_stretching(res_image)\n\nmatplotlib.rc('figure', figsize=[15, 15])\nfig, axarr = plt.subplots(1,2)\naxarr[0].imshow(res_image)\naxarr[1].imshow(contrast_image)","metadata":{"execution":{"iopub.status.busy":"2023-06-25T23:58:54.109217Z","iopub.execute_input":"2023-06-25T23:58:54.109573Z","iopub.status.idle":"2023-06-25T23:58:55.168760Z","shell.execute_reply.started":"2023-06-25T23:58:54.109544Z","shell.execute_reply":"2023-06-25T23:58:55.167920Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## **5. Histogram Normalization**","metadata":{}},{"cell_type":"code","source":"def histogram_normalization(image):    \n    hist,bins = np.histogram(image.flatten(),256,[0,256])    \n    cdf = hist.cumsum()       \n    cdf_m = np.ma.masked_equal(cdf,0)    \n    cdf_m = (cdf_m - cdf_m.min())*255/(cdf_m.max()-cdf_m.min())    \n    cdf = np.ma.filled(cdf_m,0).astype('uint8')     \n    img2 = cdf[image]    \n    return img2","metadata":{"execution":{"iopub.status.busy":"2023-06-25T23:59:29.276294Z","iopub.execute_input":"2023-06-25T23:59:29.276642Z","iopub.status.idle":"2023-06-25T23:59:29.283535Z","shell.execute_reply.started":"2023-06-25T23:59:29.276615Z","shell.execute_reply":"2023-06-25T23:59:29.282500Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"hist_norm_image = histogram_normalization(res_image)\nmatplotlib.rc('figure', figsize=[15, 15])\nfig, axarr = plt.subplots(1,2)\naxarr[0].imshow(res_image)\naxarr[1].imshow(hist_norm_image)","metadata":{"execution":{"iopub.status.busy":"2023-06-25T23:59:46.165402Z","iopub.execute_input":"2023-06-25T23:59:46.165760Z","iopub.status.idle":"2023-06-25T23:59:47.042126Z","shell.execute_reply.started":"2023-06-25T23:59:46.165732Z","shell.execute_reply":"2023-06-25T23:59:47.041300Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## **6. Histogram Matching**","metadata":{}},{"cell_type":"code","source":"def matching(source,template):    \n    oldshape = source.shape    \n    source1 = source.ravel()    \n    template1 = template.ravel()    \n    s_values, bin_idx, s_counts = np.unique(source1, return_inverse=True,return_counts=True)    \n    t_values, t_counts = np.unique(template1, return_counts=True)    \n    s_quantiles = np.cumsum(s_counts).astype(np.float64)    \n    s_quantiles /= s_quantiles[-1]    \n    t_quantiles = np.cumsum(t_counts).astype(np.float64)    \n    t_quantiles /= t_quantiles[-1]    \n    interp_t_values = np.interp(s_quantiles, t_quantiles, t_values)    \n    interp_t_values1=interp_t_values.astype(np.uint8)    \n    sub=interp_t_values-interp_t_values1    \n    interp_t_values1[sub>.5]+=1    \n    match_v1=interp_t_values1[bin_idx].reshape(oldshape).astype(np.uint8)       \n    return match_v1","metadata":{"execution":{"iopub.status.busy":"2023-06-26T00:00:25.016413Z","iopub.execute_input":"2023-06-26T00:00:25.016765Z","iopub.status.idle":"2023-06-26T00:00:25.024844Z","shell.execute_reply.started":"2023-06-26T00:00:25.016738Z","shell.execute_reply":"2023-06-26T00:00:25.023785Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def Histo_Specification(source, template):     \n    source_v6 = resize_image(source)\n    template_v6 = resize_image(template)\n    f=[]    \n    for x in range(0,3):        \n        f.append(matching(source_v6[:,:,x], template_v6[:,:,x]))    \n    img = cv2.merge((f[0],f[1],f[2]))     \n    return img","metadata":{"execution":{"iopub.status.busy":"2023-06-26T00:00:42.465387Z","iopub.execute_input":"2023-06-26T00:00:42.465741Z","iopub.status.idle":"2023-06-26T00:00:42.471803Z","shell.execute_reply.started":"2023-06-26T00:00:42.465715Z","shell.execute_reply":"2023-06-26T00:00:42.470676Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"source = cv2.imread(os.path.join(root, 'train_images', '{}.png'.format(img_list[3])))\nsource = cv2.cvtColor(source, cv2.COLOR_BGR2RGB)\nfig=plt.figure(figsize=(5, 5))\nplt.imshow(source)","metadata":{"execution":{"iopub.status.busy":"2023-06-26T00:00:58.550589Z","iopub.execute_input":"2023-06-26T00:00:58.550943Z","iopub.status.idle":"2023-06-26T00:00:59.817935Z","shell.execute_reply.started":"2023-06-26T00:00:58.550915Z","shell.execute_reply":"2023-06-26T00:00:59.817085Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"template = cv2.imread(os.path.join(root, 'train_images', '{}.png'.format(img_list[13])))\ntemplate = cv2.cvtColor(template, cv2.COLOR_BGR2RGB)\nfig=plt.figure(figsize=(5, 5))\nplt.imshow(template)","metadata":{"execution":{"iopub.status.busy":"2023-06-26T00:01:11.525200Z","iopub.execute_input":"2023-06-26T00:01:11.525589Z","iopub.status.idle":"2023-06-26T00:01:14.289697Z","shell.execute_reply.started":"2023-06-26T00:01:11.525560Z","shell.execute_reply":"2023-06-26T00:01:14.288794Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"histo_matched_image = Histo_Specification(source, template)\nfig=plt.figure(figsize=(5, 5))\nplt.imshow(histo_matched_image)","metadata":{"execution":{"iopub.status.busy":"2023-06-26T00:01:25.205499Z","iopub.execute_input":"2023-06-26T00:01:25.205848Z","iopub.status.idle":"2023-06-26T00:01:25.589736Z","shell.execute_reply.started":"2023-06-26T00:01:25.205819Z","shell.execute_reply":"2023-06-26T00:01:25.588852Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"w=10\nh=10\nfig=plt.figure(figsize=(20, 20))\ncolumns = 5\nrows = 5\nfor i in range(1, columns*rows +1):\n    img = cv2.imread(os.path.join(root, 'train_images', '{}.png'.format(img_list[i-1])))\n    img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)\n    histo_matched_image = Histo_Specification(img, template)\n    fig.add_subplot(rows, columns, i)\n    plt.imshow(histo_matched_image)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-06-26T00:01:45.370448Z","iopub.execute_input":"2023-06-26T00:01:45.370802Z","iopub.status.idle":"2023-06-26T00:01:53.422163Z","shell.execute_reply.started":"2023-06-26T00:01:45.370775Z","shell.execute_reply":"2023-06-26T00:01:53.421303Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def Root_Channel_SQUR(crop):    \n    blu=crop[:,:,0].astype(np.int64)    \n    gre=crop[:,:,1].astype(np.int64)    \n    red=crop[:,:,2].astype(np.int64)      \n    lll=(((blu**2)+(gre**2)+(red**2))/float(3))**0.5    \n    lll=lll.astype(np.uint8)  \n    return lll","metadata":{"execution":{"iopub.status.busy":"2023-06-26T00:02:32.357009Z","iopub.execute_input":"2023-06-26T00:02:32.357688Z","iopub.status.idle":"2023-06-26T00:02:32.365653Z","shell.execute_reply.started":"2023-06-26T00:02:32.357654Z","shell.execute_reply":"2023-06-26T00:02:32.362611Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Root_Channel_SQUR_image = Root_Channel_SQUR(res_image)\n\nmatplotlib.rc('figure', figsize=[15, 15])\nfig, axarr = plt.subplots(1,2)\naxarr[0].imshow(res_image)\naxarr[1].imshow(Root_Channel_SQUR_image)","metadata":{"execution":{"iopub.status.busy":"2023-06-26T00:02:43.580504Z","iopub.execute_input":"2023-06-26T00:02:43.580855Z","iopub.status.idle":"2023-06-26T00:02:44.354779Z","shell.execute_reply.started":"2023-06-26T00:02:43.580827Z","shell.execute_reply":"2023-06-26T00:02:44.353963Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"SEED = 125\nfig = plt.figure(figsize=(25, 16))\nimg_list = []\nimg_size = []\n\nfor class_id in [0, 1, 2, 3, 4]:\n    for i, (idx, row) in enumerate(train_df.loc[train_df['diagnosis'] == class_id].sample(5, random_state=SEED).iterrows()):\n        ax = fig.add_subplot(5, 5, class_id * 5 + i + 1, xticks=[], yticks=[])\n        path = os.path.join(root, 'train_images', '{}.png'.format(row['id_code']))\n        image = cv2.imread(path)\n        img_size.append(image.shape)\n        image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)\n        img_list.append(row['id_code'])\n        \n        print(row['id_code'])\n         \n        plt.imshow(image)\n        ax.set_title('Label: %d ' % (class_id) )","metadata":{"execution":{"iopub.status.busy":"2023-06-26T00:03:09.100748Z","iopub.execute_input":"2023-06-26T00:03:09.101126Z","iopub.status.idle":"2023-06-26T00:03:34.291945Z","shell.execute_reply.started":"2023-06-26T00:03:09.101097Z","shell.execute_reply":"2023-06-26T00:03:34.291081Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### **Krisch Filter**","metadata":{}},{"cell_type":"code","source":"def Krish(crop):    \n    Input=crop[:,:,1]    \n    a,b=Input.shape    \n    Kernel=np.zeros((3,3,8))   \n    Kernel[:,:,0]=np.array([[5,5,5],[-3,0,-3],[-3,-3,-3]])     \n    Kernel[:,:,1]=np.array([[-3,5,5],[-3,0,5],[-3,-3,-3]])    \n    Kernel[:,:,2]=np.array([[-3,-3,5],[-3,0,5],[-3,-3,5]])    \n    Kernel[:,:,3]=np.array([[-3,-3,-3],[-3,0,5],[-3,5,5]])    \n    Kernel[:,:,4]=np.array([[-3,-3,-3],[-3,0,-3],[5,5,5]])    \n    Kernel[:,:,5]=np.array([[-3,-3,-3],[5,0,-3],[5,5,-3]])    \n    Kernel[:,:,6]=np.array([[5,-3,-3],[5,0,-3],[5,-3,-3]])    \n    Kernel[:,:,7]=np.array([[5,5,-3],[5,0,-3],[-3,-3,-3]])    \n    #Kernel=(1/float(15))*Kernel    \n    #Convolution output    \n    dst=np.zeros((a,b,8))    \n    for x in range(0,8):        \n        dst[:,:,x] = cv2.filter2D(Input,-1,Kernel[:,:,x])    \n    Out=np.zeros((a,b))    \n    for y in range(0,a-1):        \n        for z in range(0,b-1):            \n            Out[y,z]=max(dst[y,z,:])    \n    Out=np.uint8(Out)            \n    return Out","metadata":{"execution":{"iopub.status.busy":"2023-06-26T00:03:56.727483Z","iopub.execute_input":"2023-06-26T00:03:56.727834Z","iopub.status.idle":"2023-06-26T00:03:56.740851Z","shell.execute_reply.started":"2023-06-26T00:03:56.727805Z","shell.execute_reply":"2023-06-26T00:03:56.739760Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Krish_image = Krish(res_image)\n\nmatplotlib.rc('figure', figsize=[15, 15])\nfig, axarr = plt.subplots(1,2)\naxarr[0].imshow(res_image)\naxarr[1].imshow(Krish_image)","metadata":{"execution":{"iopub.status.busy":"2023-06-26T00:04:08.035104Z","iopub.execute_input":"2023-06-26T00:04:08.035461Z","iopub.status.idle":"2023-06-26T00:04:09.025785Z","shell.execute_reply.started":"2023-06-26T00:04:08.035434Z","shell.execute_reply":"2023-06-26T00:04:09.024745Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"---------------------------------","metadata":{}},{"cell_type":"markdown","source":"# **II. EXPLORATORY DATA ANALYSIS**","metadata":{}},{"cell_type":"markdown","source":"## **1. Importing Libraries**","metadata":{}},{"cell_type":"code","source":"#Basic Libraries\nimport warnings\nwarnings.filterwarnings(\"ignore\")\nimport pandas as pd\nimport numpy as np\nfrom tqdm import tqdm,tqdm_notebook\nfrom prettytable import PrettyTable\nimport pickle\nimport os\nprint('CWD is ',os.getcwd())\n\n#Visualization Libraries\nfrom sklearn.manifold import TSNE\nimport seaborn as sns\nimport matplotlib.pyplot as plt\n%matplotlib inline\nplt.rcParams[\"axes.grid\"] = False\n\n#Image Libraries\nfrom PIL import Image\nimport cv2\n\n#DL Libraries\nimport keras\nfrom keras import applications\nfrom tensorflow.keras.utils import img_to_array,array_to_img,load_img\nfrom keras.preprocessing.image import ImageDataGenerator\nfrom keras import optimizers,Model,Sequential\nfrom keras.layers import Input,GlobalAveragePooling2D,Dropout,Dense,Activation\nfrom keras.callbacks import EarlyStopping,ReduceLROnPlateau","metadata":{"execution":{"iopub.status.busy":"2023-06-26T02:25:30.797480Z","iopub.execute_input":"2023-06-26T02:25:30.797924Z","iopub.status.idle":"2023-06-26T02:25:40.295526Z","shell.execute_reply.started":"2023-06-26T02:25:30.797886Z","shell.execute_reply":"2023-06-26T02:25:40.294497Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## **2. Visualization of Images**","metadata":{}},{"cell_type":"code","source":"'''This function reads data from the respective train and test directories'''\n\ndef load_data():\n    train = pd.read_csv('/kaggle/input/aptos2019-blindness-detection/train.csv')\n    test = pd.read_csv('/kaggle/input/aptos2019-blindness-detection/test.csv')\n    \n    train_dir = '/kaggle/input/aptos2019-blindness-detection/train_images'\n    test_dir = '/kaggle/input/aptos2019-blindness-detection/test_images'\n\n    \n    train['file_path'] = train['id_code'].map(lambda x: os.path.join(train_dir,'{}.png'.format(x)))\n    test['file_path'] = test['id_code'].map(lambda x: os.path.join(test_dir,'{}.png'.format(x)))\n    \n    train['file_name'] = train[\"id_code\"].apply(lambda x: x + \".png\")\n    test['file_name'] = test[\"id_code\"].apply(lambda x: x + \".png\")\n    \n    train['diagnosis'] = train['diagnosis'].astype(str)\n    \n    return train, test","metadata":{"execution":{"iopub.status.busy":"2023-06-26T02:26:47.408179Z","iopub.execute_input":"2023-06-26T02:26:47.408559Z","iopub.status.idle":"2023-06-26T02:26:47.416215Z","shell.execute_reply.started":"2023-06-26T02:26:47.408531Z","shell.execute_reply":"2023-06-26T02:26:47.415086Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train,df_test = load_data()\nprint(df_train.shape,df_test.shape,'\\n')\ndf_train.head(6)","metadata":{"execution":{"iopub.status.busy":"2023-06-26T02:27:03.292516Z","iopub.execute_input":"2023-06-26T02:27:03.292867Z","iopub.status.idle":"2023-06-26T02:27:03.351706Z","shell.execute_reply.started":"2023-06-26T02:27:03.292841Z","shell.execute_reply":"2023-06-26T02:27:03.350842Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### **2.1 Class Distribution**","metadata":{}},{"cell_type":"code","source":"'''This Function Plots a Bar plot of output Classes Distribution'''\n\ndef plot_classes(df):\n    df_group = pd.DataFrame(df.groupby('diagnosis').agg('size').reset_index())\n    df_group.columns = ['diagnosis','count']\n\n    sns.set(rc={'figure.figsize':(10,5)}, style = 'whitegrid')\n    sns.barplot(x = 'diagnosis',y='count',data = df_group,palette = \"Blues_d\")\n    plt.title('Output Class Distribution')\n    plt.show() ","metadata":{"execution":{"iopub.status.busy":"2023-06-26T02:28:37.789752Z","iopub.execute_input":"2023-06-26T02:28:37.790129Z","iopub.status.idle":"2023-06-26T02:28:37.796644Z","shell.execute_reply.started":"2023-06-26T02:28:37.790101Z","shell.execute_reply":"2023-06-26T02:28:37.795572Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_classes(df_train)","metadata":{"execution":{"iopub.status.busy":"2023-06-26T02:28:51.495241Z","iopub.execute_input":"2023-06-26T02:28:51.495620Z","iopub.status.idle":"2023-06-26T02:28:51.853056Z","shell.execute_reply.started":"2023-06-26T02:28:51.495592Z","shell.execute_reply":"2023-06-26T02:28:51.852172Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Plot Summary - As we can see, there is class imbalance in the output class distribution. We shall account for this while training the models using data augmentation / class balancing methods.","metadata":{}},{"cell_type":"markdown","source":"### **2.2 Visualizing Images**","metadata":{}},{"cell_type":"code","source":"# Defining a global variable to be used as Image size..\nIMG_SIZE = 200","metadata":{"execution":{"iopub.status.busy":"2023-06-26T02:29:58.090650Z","iopub.execute_input":"2023-06-26T02:29:58.091087Z","iopub.status.idle":"2023-06-26T02:29:58.095831Z","shell.execute_reply.started":"2023-06-26T02:29:58.091056Z","shell.execute_reply":"2023-06-26T02:29:58.094967Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"'''This Function converts a color image to gray scale image'''\n\ndef conv_gray(img):\n    img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)\n    img = cv2.resize(img, (IMG_SIZE,IMG_SIZE))\n    return img\n  \n    \n'''This Function shows the visual Image photo of 'n x 5' points (5 of each class)'''\n\ndef visualize_imgs(df,pts_per_class,color_scale):\n    df = df.groupby('diagnosis',group_keys = False).apply(lambda df: df.sample(pts_per_class))\n    df = df.reset_index(drop = True)\n    \n    plt.rcParams[\"axes.grid\"] = False\n    for pt in range(pts_per_class):\n        f, axarr = plt.subplots(1,5,figsize = (15,15))\n        axarr[0].set_ylabel(\"Sample Data Points\")\n        \n        df_temp = df[df.index.isin([pt + (pts_per_class*0),pt + (pts_per_class*1), pt + (pts_per_class*2),pt + (pts_per_class*3),pt + (pts_per_class*4)])]\n        for i in range(5):\n            if color_scale == 'gray':\n                img = conv_gray(cv2.imread(df_temp.file_path.iloc[i]))\n                axarr[i].imshow(img,cmap = color_scale)\n            else:\n                axarr[i].imshow(Image.open(df_temp.file_path.iloc[i]).resize((IMG_SIZE,IMG_SIZE)))\n            axarr[i].set_xlabel('Class '+str(df_temp.diagnosis.iloc[i]))\n\n        plt.show()","metadata":{"execution":{"iopub.status.busy":"2023-06-26T02:30:09.222069Z","iopub.execute_input":"2023-06-26T02:30:09.222447Z","iopub.status.idle":"2023-06-26T02:30:09.233185Z","shell.execute_reply.started":"2023-06-26T02:30:09.222419Z","shell.execute_reply":"2023-06-26T02:30:09.232247Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"visualize_imgs(df_train,3,color_scale = None)","metadata":{"execution":{"iopub.status.busy":"2023-06-26T02:30:20.242683Z","iopub.execute_input":"2023-06-26T02:30:20.243043Z","iopub.status.idle":"2023-06-26T02:30:26.643544Z","shell.execute_reply.started":"2023-06-26T02:30:20.243015Z","shell.execute_reply":"2023-06-26T02:30:26.641927Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"visualize_imgs(df_train,2,color_scale = 'gray')","metadata":{"execution":{"iopub.status.busy":"2023-06-26T02:31:22.901389Z","iopub.execute_input":"2023-06-26T02:31:22.901742Z","iopub.status.idle":"2023-06-26T02:31:26.429680Z","shell.execute_reply.started":"2023-06-26T02:31:22.901713Z","shell.execute_reply":"2023-06-26T02:31:26.428640Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Plot Summary - As we can see, as we move towards higher classes, we are able to comprehend larger number of abnormalities in the eye images. Also, the lightning and brightness conditions are not even across all images. We will try to handle this using image processing techniques. Also, Gray Scale Images are giving better visualization of the eye features as compared to RGB images.\n\nThis below photo shows what the eye diabetic retinopathy condition refers to:\n![](http://sa1s3optim.patientpop.com/assets/images/provider/photos/1947516.jpeg)","metadata":{}},{"cell_type":"markdown","source":"## **3. Image Processing**","metadata":{}},{"cell_type":"markdown","source":"### **3.1 Gaussian Blur**","metadata":{}},{"cell_type":"code","source":"'''This section of code applies gaussian blur on top of image'''\n\nrn = np.random.randint(low = 0,high = len(df_train) - 1)\n\nimg = cv2.imread(df_train.file_path.iloc[rn])\nimg = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)\nimg = cv2.resize(img, (IMG_SIZE,IMG_SIZE))\n\nimg_t = cv2.addWeighted(img,4, cv2.GaussianBlur(img , (0,0) , 30) ,-4 ,128)\n\nf, axarr = plt.subplots(1,2,figsize = (11,11))\naxarr[0].imshow(img)\naxarr[1].imshow(img_t)\nplt.title('After applying Gaussian Blur')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-06-26T02:36:00.475245Z","iopub.execute_input":"2023-06-26T02:36:00.476179Z","iopub.status.idle":"2023-06-26T02:36:01.405519Z","shell.execute_reply.started":"2023-06-26T02:36:00.476128Z","shell.execute_reply":"2023-06-26T02:36:01.404466Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Plot Summary - As we can see, after applying Gaussian Blur, We are able to bring out the features/image details much more clearer in the eye.","metadata":{}},{"cell_type":"markdown","source":"### **3.2 Gaussian Blur with Circular Cropping**","metadata":{}},{"cell_type":"code","source":"'''This Function performs image processing on top of images by performing Gaussian Blur and Circle Crop'''\n\ndef 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    \n    \ndef circle_crop(img, sigmaX):   \n    \"\"\"Create circular crop around image centre\"\"\"    \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":"2023-06-26T02:37:02.282998Z","iopub.execute_input":"2023-06-26T02:37:02.283380Z","iopub.status.idle":"2023-06-26T02:37:02.296863Z","shell.execute_reply.started":"2023-06-26T02:37:02.283351Z","shell.execute_reply":"2023-06-26T02:37:02.295904Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"'''Perform Image Processing on a sample image'''\n\nrn = np.random.randint(low = 0,high = len(df_train) - 1)\n\n#img = img_t\nimg = cv2.imread(df_train.file_path.iloc[rn])\nimg_t = circle_crop(img,sigmaX = 30)\n\nf, axarr = plt.subplots(1,2,figsize = (11,11))\naxarr[0].imshow(cv2.resize(cv2.cvtColor(img, cv2.COLOR_BGR2RGB),(IMG_SIZE,IMG_SIZE)))\naxarr[1].imshow(img_t)\nplt.title('After applying Circular Crop and Gaussian Blur')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-06-26T02:37:13.410091Z","iopub.execute_input":"2023-06-26T02:37:13.410481Z","iopub.status.idle":"2023-06-26T02:37:14.798970Z","shell.execute_reply.started":"2023-06-26T02:37:13.410451Z","shell.execute_reply":"2023-06-26T02:37:14.798151Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Plot Summary - As we can see above, now the image features and details are very much clearer than what the image was before, we are ready to use this image for modelling as the image details are much more clearer.","metadata":{}},{"cell_type":"code","source":"'''This Function shows the visual Image photo of 'n x 5' points (5 of each class) \nand performs image processing (Gaussian Blur, Circular crop) transformation on top of that'''\n\ndef visualize_img_process(df,pts_per_class,sigmaX):\n    df = df.groupby('diagnosis',group_keys = False).apply(lambda df: df.sample(pts_per_class))\n    df = df.reset_index(drop = True)\n    \n    plt.rcParams[\"axes.grid\"] = False\n    for pt in range(pts_per_class):\n        f, axarr = plt.subplots(1,5,figsize = (15,15))\n        axarr[0].set_ylabel(\"Sample Data Points\")\n        \n        df_temp = df[df.index.isin([pt + (pts_per_class*0),pt + (pts_per_class*1), pt + (pts_per_class*2),pt + (pts_per_class*3),pt + (pts_per_class*4)])]\n        for i in range(5):\n            img = cv2.imread(df_temp.file_path.iloc[i])\n            img = circle_crop(img,sigmaX)\n            axarr[i].imshow(img)\n            axarr[i].set_xlabel('Class '+str(df_temp.diagnosis.iloc[i]))\n\n        plt.show()","metadata":{"execution":{"iopub.status.busy":"2023-06-26T02:37:43.233773Z","iopub.execute_input":"2023-06-26T02:37:43.234142Z","iopub.status.idle":"2023-06-26T02:37:43.244094Z","shell.execute_reply.started":"2023-06-26T02:37:43.234115Z","shell.execute_reply":"2023-06-26T02:37:43.243145Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"visualize_img_process(df_train,5,sigmaX = 30)","metadata":{"execution":{"iopub.status.busy":"2023-06-26T02:37:54.832927Z","iopub.execute_input":"2023-06-26T02:37:54.833321Z","iopub.status.idle":"2023-06-26T02:38:33.698523Z","shell.execute_reply.started":"2023-06-26T02:37:54.833260Z","shell.execute_reply":"2023-06-26T02:38:33.697466Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## **4. t-SNE Visualization**","metadata":{}},{"cell_type":"code","source":"#Training image data\nnpix = 224 # resize to npix x npix (for now)\nX_train = np.zeros((df_train.shape[0], npix, npix))\nfor i in tqdm_notebook(range(df_train.shape[0])):\n    #Loading an image\n    img = cv2.imread(df_train.file_path.iloc[i])\n    img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) \n    X_train[i, :, :] = cv2.resize(img, (npix, npix)) \n    \nprint(\"X_train shape: \" + str(np.shape(X_train)))  ","metadata":{"execution":{"iopub.status.busy":"2023-06-26T02:39:03.539194Z","iopub.execute_input":"2023-06-26T02:39:03.540172Z","iopub.status.idle":"2023-06-26T02:45:27.534258Z","shell.execute_reply.started":"2023-06-26T02:39:03.540137Z","shell.execute_reply":"2023-06-26T02:45:27.533341Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#normalize\nX = X_train / 255\n\n#reshape\nX = X.reshape(X.shape[0], -1)\ntrainy = df_train['diagnosis']","metadata":{"execution":{"iopub.status.busy":"2023-06-26T02:47:03.695566Z","iopub.execute_input":"2023-06-26T02:47:03.695944Z","iopub.status.idle":"2023-06-26T02:47:05.281767Z","shell.execute_reply.started":"2023-06-26T02:47:03.695914Z","shell.execute_reply":"2023-06-26T02:47:05.280753Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"per_vals = [2,5,10,15,20,30,40,50]\n\nfor per in tqdm_notebook(per_vals):\n    X_decomposed = TSNE(n_components=2,perplexity = per).fit_transform(X)\n    df_tsne = pd.DataFrame(data=X_decomposed, columns=['Dimension_x','Dimension_y'])\n    df_tsne['Score'] = trainy.values\n    \n    sns.FacetGrid(df_tsne, hue='Score', height=6).map(plt.scatter, 'Dimension_x', 'Dimension_y').add_legend()\n\n    plt.title('t-SNE for perplexity = ' + str(per))\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2023-06-26T02:48:03.968176Z","iopub.execute_input":"2023-06-26T02:48:03.968752Z","iopub.status.idle":"2023-06-26T02:55:37.125450Z","shell.execute_reply.started":"2023-06-26T02:48:03.968714Z","shell.execute_reply":"2023-06-26T02:55:37.124552Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Plot Summary - As we can see, we are able to seperate Class '0' from other classes (1-4). Seperating between classes 1-4 looks challenging.","metadata":{}},{"cell_type":"markdown","source":"## **5. Data Augmentation**","metadata":{}},{"cell_type":"code","source":"def generate_augmentations(lim):\n    datagen = ImageDataGenerator(featurewise_center=True,\n                                 featurewise_std_normalization=True,\n                                 rotation_range=20,\n                                 horizontal_flip=True)\n    \n    image_folder_path = '/kaggle/input/aptos2019-blindness-detection/train_images'\n    file_list = os.listdir(image_folder_path)\n    img_filename = np.random.choice(file_list)\n    img_path = os.path.join(image_folder_path, img_filename)\n    \n    img = cv2.imread(img_path)\n    img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)\n    img = cv2.resize(img, (IMG_SIZE, IMG_SIZE))\n    plt.imshow(img)\n    plt.title('ORIGINAL IMAGE')\n    plt.show()\n    \n    img_arr = img.reshape((1,) + img.shape)\n    \n    i = 0\n    for img_iterator in datagen.flow(x=img_arr, batch_size=1):\n        i = i + 1\n        if i > lim:\n            break\n        plt.imshow((img_iterator.reshape(img_arr[0].shape)).astype(np.uint8))\n        plt.title('IMAGE AUGMENTATION ' + str(i))\n        plt.show()","metadata":{"execution":{"iopub.status.busy":"2023-06-26T03:01:05.283209Z","iopub.execute_input":"2023-06-26T03:01:05.283630Z","iopub.status.idle":"2023-06-26T03:01:05.295495Z","shell.execute_reply.started":"2023-06-26T03:01:05.283601Z","shell.execute_reply":"2023-06-26T03:01:05.294326Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"generate_augmentations(4)","metadata":{"execution":{"iopub.status.busy":"2023-06-26T03:01:10.464693Z","iopub.execute_input":"2023-06-26T03:01:10.465053Z","iopub.status.idle":"2023-06-26T03:01:12.446335Z","shell.execute_reply.started":"2023-06-26T03:01:10.465026Z","shell.execute_reply":"2023-06-26T03:01:12.445427Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Plot Summary - As we can see above, Image Augmentations are extremely helpful for this datasets to make our Models more Robust and would also have a higher ability to generalize well.","metadata":{}}]}