{"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":"## Difference between a healthy eye and a diabetic eye","metadata":{}},{"cell_type":"markdown","source":"<img src=https://www.aoa.org/AOA/Images/Patients/Eye%20Conditions/Diabetic_Retinopathy1_AdobeStock_190242073.jpg>","metadata":{}},{"cell_type":"markdown","source":"Image source: www.aoa.org","metadata":{}},{"cell_type":"markdown","source":"<img src=https://www.ophthalytics.com/wp-content/uploads/2021/08/Copy-of-WEBSITE-CONTENT-1024x586.png>","metadata":{}},{"cell_type":"markdown","source":"Image source: www.ophthalytics.com","metadata":{}},{"cell_type":"markdown","source":"## Imports","metadata":{}},{"cell_type":"code","source":"import numpy as np \nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nsns.set_style(\"darkgrid\")\nimport cv2\nimport os\nimport collections\nimport albumentations as A\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-01-23T16:37:33.186369Z","iopub.execute_input":"2023-01-23T16:37:33.187859Z","iopub.status.idle":"2023-01-23T16:37:43.682844Z","shell.execute_reply.started":"2023-01-23T16:37:33.187738Z","shell.execute_reply":"2023-01-23T16:37:43.681403Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Reading the data","metadata":{}},{"cell_type":"code","source":"df = pd.read_csv('/kaggle/input/aptos2019-blindness-detection/train.csv')","metadata":{"execution":{"iopub.status.busy":"2023-01-23T16:37:43.685680Z","iopub.execute_input":"2023-01-23T16:37:43.686688Z","iopub.status.idle":"2023-01-23T16:37:43.716863Z","shell.execute_reply.started":"2023-01-23T16:37:43.686639Z","shell.execute_reply":"2023-01-23T16:37:43.715467Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.head()","metadata":{"execution":{"iopub.status.busy":"2023-01-23T16:37:43.718817Z","iopub.execute_input":"2023-01-23T16:37:43.721650Z","iopub.status.idle":"2023-01-23T16:37:43.751794Z","shell.execute_reply.started":"2023-01-23T16:37:43.721593Z","shell.execute_reply":"2023-01-23T16:37:43.750615Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Exploratory Data Analysis","metadata":{}},{"cell_type":"markdown","source":"## Frequency of each class","metadata":{}},{"cell_type":"code","source":"fig, ax = plt.subplots(figsize=(8, 5))\nsns.barplot(x=[x for x in range(5)],y=[df['diagnosis'].value_counts()[x] for x in range(5)],palette='Accent',ax=ax)\nfor p in ax.patches:\n   ax.annotate('{}'.format(int(p.get_height())), (p.get_x(), p.get_height()))\nplt.ylabel('Frequency')\nplt.xlabel('Diagnosis')\nplt.title('Frequency Of Each Class')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-01-23T16:37:43.755232Z","iopub.execute_input":"2023-01-23T16:37:43.756184Z","iopub.status.idle":"2023-01-23T16:37:44.123134Z","shell.execute_reply.started":"2023-01-23T16:37:43.756109Z","shell.execute_reply":"2023-01-23T16:37:44.122222Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Sample image of each class","metadata":{}},{"cell_type":"code","source":"plt.figure(figsize=[30, 30])\nfor x in range(5):\n    img = df[df['diagnosis'] == x].iloc[1]['id_code'] + '.png'\n    path = \"/kaggle/input/aptos2019-blindness-detection/train_images/\"\n    img = cv2.imread(path+img)\n    img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)\n    plt.subplot(1, 5, x+1)\n    plt.grid(None)\n    plt.axis('off')\n    plt.title(\"Class: \" + str(x))\n    plt.imshow(img);","metadata":{"execution":{"iopub.status.busy":"2023-01-23T16:37:44.124225Z","iopub.execute_input":"2023-01-23T16:37:44.124942Z","iopub.status.idle":"2023-01-23T16:37:50.755534Z","shell.execute_reply.started":"2023-01-23T16:37:44.124910Z","shell.execute_reply":"2023-01-23T16:37:50.754580Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"heights = []\nwidths = []\nper_class = {0:([],[]),1:([],[]),2:([],[]),3:([],[]),4:([],[])}\nfor row in df.iterrows():\n    path = '/kaggle/input/aptos2019-blindness-detection/train_images/' + row[1]['id_code'] + '.png'\n    img = cv2.imread(path)\n    heights.append(img.shape[0])\n    widths.append(img.shape[1])\n    per_class[row[1]['diagnosis']][0].append(img.shape[0])\n    per_class[row[1]['diagnosis']][1].append(img.shape[1])","metadata":{"execution":{"iopub.status.busy":"2023-01-23T16:37:50.756533Z","iopub.execute_input":"2023-01-23T16:37:50.756873Z","iopub.status.idle":"2023-01-23T16:45:11.746974Z","shell.execute_reply.started":"2023-01-23T16:37:50.756844Z","shell.execute_reply":"2023-01-23T16:45:11.744683Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Distribution of image heights","metadata":{}},{"cell_type":"code","source":"sns.histplot(x = heights, kde=True)\nplt.ylabel('Frequency')\nplt.xlabel('Height')\nplt.title('Distribution of image heights')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-01-23T16:45:11.750085Z","iopub.execute_input":"2023-01-23T16:45:11.750601Z","iopub.status.idle":"2023-01-23T16:45:12.044892Z","shell.execute_reply.started":"2023-01-23T16:45:11.750559Z","shell.execute_reply":"2023-01-23T16:45:12.043164Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Distribution of image widths","metadata":{}},{"cell_type":"code","source":"sns.histplot(x = widths, kde=True)\nplt.ylabel('Frequency')\nplt.xlabel('Width')\nplt.title('Distribution of image widths')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-01-23T16:45:12.047413Z","iopub.execute_input":"2023-01-23T16:45:12.047926Z","iopub.status.idle":"2023-01-23T16:45:12.380998Z","shell.execute_reply.started":"2023-01-23T16:45:12.047879Z","shell.execute_reply":"2023-01-23T16:45:12.380137Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Distribution of aspect ratios of images","metadata":{}},{"cell_type":"code","source":"aspect_ratios = [heights[x]/widths[x] for x in range(len(heights))]\nsns.histplot(x = aspect_ratios, kde=True)\nplt.ylabel('Frequency')\nplt.xlabel('Aspect Ratios')\nplt.title('Distribution of image aspect ratios')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-01-23T16:45:12.382479Z","iopub.execute_input":"2023-01-23T16:45:12.383131Z","iopub.status.idle":"2023-01-23T16:45:12.717721Z","shell.execute_reply.started":"2023-01-23T16:45:12.383072Z","shell.execute_reply":"2023-01-23T16:45:12.716075Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Sizes of images of class 0 (bigger bubble means more images of that size)","metadata":{}},{"cell_type":"code","source":"merged = [(per_class[0][0][x],per_class[0][1][x]) for x in range(len(per_class[0][0]))]\nfreq = collections.Counter(merged)\nsizes = [freq[x] for x in merged]\nsns.scatterplot(x=per_class[0][0],y=per_class[0][1],size=sizes,hue=sizes,palette='copper')\nplt.title('Sizes of images of class 0')\nplt.xlabel('Height')\nplt.ylabel('Width')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-01-23T16:45:12.721969Z","iopub.execute_input":"2023-01-23T16:45:12.722385Z","iopub.status.idle":"2023-01-23T16:45:13.224928Z","shell.execute_reply.started":"2023-01-23T16:45:12.722348Z","shell.execute_reply":"2023-01-23T16:45:13.224072Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Sizes of images of class 1 (bigger bubble means more images of that size)","metadata":{}},{"cell_type":"code","source":"merged = [(per_class[1][0][x],per_class[1][1][x]) for x in range(len(per_class[1][0]))]\nfreq = collections.Counter(merged)\nsizes = [freq[x] for x in merged]\nsns.scatterplot(x=per_class[1][0],y=per_class[1][1],size=sizes,hue=sizes,palette='copper')\nplt.title('Sizes of images of class 1')\nplt.xlabel('Height')\nplt.ylabel('Width')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-01-23T16:45:13.226248Z","iopub.execute_input":"2023-01-23T16:45:13.226776Z","iopub.status.idle":"2023-01-23T16:45:13.614043Z","shell.execute_reply.started":"2023-01-23T16:45:13.226740Z","shell.execute_reply":"2023-01-23T16:45:13.612785Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Sizes of images of class 2 (bigger bubble means more images of that size)","metadata":{}},{"cell_type":"code","source":"merged = [(per_class[2][0][x],per_class[2][1][x]) for x in range(len(per_class[2][0]))]\nfreq = collections.Counter(merged)\nsizes = [freq[x] for x in merged]\nsns.scatterplot(x=per_class[2][0],y=per_class[2][1],size=sizes,hue=sizes,palette='copper')\nplt.title('Sizes of images of class 2')\nplt.xlabel('Height')\nplt.ylabel('Width')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-01-23T16:45:13.615717Z","iopub.execute_input":"2023-01-23T16:45:13.616828Z","iopub.status.idle":"2023-01-23T16:45:14.038084Z","shell.execute_reply.started":"2023-01-23T16:45:13.616778Z","shell.execute_reply":"2023-01-23T16:45:14.036655Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Sizes of images of class 3 (bigger bubble means more images of that size)","metadata":{}},{"cell_type":"code","source":"merged = [(per_class[3][0][x],per_class[3][1][x]) for x in range(len(per_class[3][0]))]\nfreq = collections.Counter(merged)\nsizes = [freq[x] for x in merged]\nsns.scatterplot(x=per_class[3][0],y=per_class[3][1],size=sizes,hue=sizes,palette='copper')\nplt.title('Sizes of images of class 3')\nplt.xlabel('Height')\nplt.ylabel('Width')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-01-23T16:45:14.039972Z","iopub.execute_input":"2023-01-23T16:45:14.040507Z","iopub.status.idle":"2023-01-23T16:45:14.446604Z","shell.execute_reply.started":"2023-01-23T16:45:14.040459Z","shell.execute_reply":"2023-01-23T16:45:14.445361Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Sizes of images of class 4 (bigger bubble means more images of that size)","metadata":{}},{"cell_type":"code","source":"merged = [(per_class[4][0][x],per_class[4][1][x]) for x in range(len(per_class[4][0]))]\nfreq = collections.Counter(merged)\nsizes = [freq[x] for x in merged]\nsns.scatterplot(x=per_class[4][0],y=per_class[4][1],size=sizes,hue=sizes,palette='copper')\nplt.title('Sizes of images of class 4')\nplt.xlabel('Height')\nplt.ylabel('Width')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-01-23T16:45:14.447952Z","iopub.execute_input":"2023-01-23T16:45:14.448311Z","iopub.status.idle":"2023-01-23T16:45:14.851584Z","shell.execute_reply.started":"2023-01-23T16:45:14.448280Z","shell.execute_reply":"2023-01-23T16:45:14.848898Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Average image of class 0","metadata":{}},{"cell_type":"code","source":"count = 0\navg = np.zeros(shape=(256,256,3))\nfor row in df[df['diagnosis']==0].iterrows():\n    path = '/kaggle/input/aptos2019-blindness-detection/train_images/' + row[1]['id_code'] + '.png'\n    img = cv2.imread(path)\n    img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)\n    img = cv2.resize(img, (256, 256))\n    avg += img\n    count+=1\nplt.grid(None)\nplt.axis('off')\nplt.title('Average image of class 0')\nplt.imshow(np.round(avg/count).astype(int));","metadata":{"execution":{"iopub.status.busy":"2023-01-23T16:49:47.054488Z","iopub.execute_input":"2023-01-23T16:49:47.056231Z","iopub.status.idle":"2023-01-23T16:51:49.754427Z","shell.execute_reply.started":"2023-01-23T16:49:47.056178Z","shell.execute_reply":"2023-01-23T16:51:49.752888Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Average image of class 1","metadata":{}},{"cell_type":"code","source":"count = 0\navg = np.zeros(shape=(256,256,3))\nfor row in df[df['diagnosis']==1].iterrows():\n    path = '/kaggle/input/aptos2019-blindness-detection/train_images/' + row[1]['id_code'] + '.png'\n    img = cv2.imread(path)\n    img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)\n    img = cv2.resize(img, (256, 256))\n    avg += img\n    count+=1\nplt.grid(None)\nplt.axis('off')\nplt.title('Average image of class 1')\nplt.imshow(np.round(avg/count).astype(int));","metadata":{"execution":{"iopub.status.busy":"2023-01-23T16:51:49.756864Z","iopub.execute_input":"2023-01-23T16:51:49.757354Z","iopub.status.idle":"2023-01-23T16:52:40.234923Z","shell.execute_reply.started":"2023-01-23T16:51:49.757313Z","shell.execute_reply":"2023-01-23T16:52:40.233706Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Average image of class 2","metadata":{}},{"cell_type":"code","source":"count = 0\navg = np.zeros(shape=(256,256,3))\nfor row in df[df['diagnosis']==2].iterrows():\n    path = '/kaggle/input/aptos2019-blindness-detection/train_images/' + row[1]['id_code'] + '.png'\n    img = cv2.imread(path)\n    img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)\n    img = cv2.resize(img, (256, 256))\n    avg += img\n    count+=1\nplt.grid(None)\nplt.axis('off')\nplt.title('Average image of class 2')\nplt.imshow(np.round(avg/count).astype(int));","metadata":{"execution":{"iopub.status.busy":"2023-01-23T16:52:40.236305Z","iopub.execute_input":"2023-01-23T16:52:40.236654Z","iopub.status.idle":"2023-01-23T16:55:18.885203Z","shell.execute_reply.started":"2023-01-23T16:52:40.236623Z","shell.execute_reply":"2023-01-23T16:55:18.883903Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Average image of class 3","metadata":{}},{"cell_type":"code","source":"count = 0\navg = np.zeros(shape=(256,256,3))\nfor row in df[df['diagnosis']==3].iterrows():\n    path = '/kaggle/input/aptos2019-blindness-detection/train_images/' + row[1]['id_code'] + '.png'\n    img = cv2.imread(path)\n    img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)\n    img = cv2.resize(img, (256, 256))\n    avg += img\n    count+=1\nplt.grid(None)\nplt.axis('off')\nplt.title('Average image of class 3')\nplt.imshow(np.round(avg/count).astype(int));","metadata":{"execution":{"iopub.status.busy":"2023-01-23T16:55:18.888379Z","iopub.execute_input":"2023-01-23T16:55:18.889111Z","iopub.status.idle":"2023-01-23T16:55:50.097655Z","shell.execute_reply.started":"2023-01-23T16:55:18.889055Z","shell.execute_reply":"2023-01-23T16:55:50.096319Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Average image of class 4","metadata":{}},{"cell_type":"code","source":"count = 0\navg = np.zeros(shape=(256,256,3))\nfor row in df[df['diagnosis']==4].iterrows():\n    path = '/kaggle/input/aptos2019-blindness-detection/train_images/' + row[1]['id_code'] + '.png'\n    img = cv2.imread(path)\n    img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)\n    img = cv2.resize(img, (256, 256))\n    avg += img\n    count+=1\nplt.grid(None)\nplt.axis('off')\nplt.title('Average image of class 4')\nplt.imshow(np.round(avg/count).astype(int));","metadata":{"execution":{"iopub.status.busy":"2023-01-23T16:55:50.099095Z","iopub.execute_input":"2023-01-23T16:55:50.099788Z","iopub.status.idle":"2023-01-23T16:56:41.460589Z","shell.execute_reply.started":"2023-01-23T16:55:50.099750Z","shell.execute_reply":"2023-01-23T16:56:41.459442Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Finding Blurred images\nhttps://websolutionstuff.com/post/how-to-check-image-blur-or-not-using-python <br>\nanalyticsvidhya.com/blog/2020/09/how-to-perform-blur-detection-using-opencv-in-python/","metadata":{}},{"cell_type":"code","source":"def variance_of_laplacian(image):\n    return cv2.Laplacian(image, cv2.CV_64F).var()\n\ndef blur_detect(imagePath):\n    image = cv2.imread(imagePath)\n    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)\n    fm = variance_of_laplacian(gray)\n    b = 0\n    if fm < 3:\n        b = 1\n    return b","metadata":{"execution":{"iopub.status.busy":"2023-01-10T08:59:28.132686Z","iopub.execute_input":"2023-01-10T08:59:28.133219Z","iopub.status.idle":"2023-01-10T08:59:28.142485Z","shell.execute_reply.started":"2023-01-10T08:59:28.133176Z","shell.execute_reply":"2023-01-10T08:59:28.140414Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"is_blur = []\nfor row in df.iterrows():\n    path = '/kaggle/input/aptos2019-blindness-detection/train_images/' + row[1]['id_code'] + '.png'\n    is_blur.append(blur_detect(path))\nis_blur = list(map(bool,is_blur))","metadata":{"execution":{"iopub.status.busy":"2023-01-10T08:59:30.220975Z","iopub.execute_input":"2023-01-10T08:59:30.221612Z","iopub.status.idle":"2023-01-10T09:09:04.154463Z","shell.execute_reply.started":"2023-01-10T08:59:30.221564Z","shell.execute_reply":"2023-01-10T09:09:04.152970Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"is_blur.count(True)","metadata":{"execution":{"iopub.status.busy":"2023-01-10T09:09:04.156766Z","iopub.execute_input":"2023-01-10T09:09:04.157410Z","iopub.status.idle":"2023-01-10T09:09:04.168492Z","shell.execute_reply.started":"2023-01-10T09:09:04.157364Z","shell.execute_reply":"2023-01-10T09:09:04.167101Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=[30, 30])\ncount = 1\nfor x in df['id_code'][is_blur]:\n    path = '/kaggle/input/aptos2019-blindness-detection/train_images/' + x + '.png'\n    img = cv2.imread(path)\n    img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)\n    plt.subplot(1, 6, count)\n    plt.grid(None)\n    plt.axis('off')\n    plt.imshow(img)\n    count+=1","metadata":{"execution":{"iopub.status.busy":"2023-01-10T09:10:23.175725Z","iopub.execute_input":"2023-01-10T09:10:23.176202Z","iopub.status.idle":"2023-01-10T09:10:32.872698Z","shell.execute_reply.started":"2023-01-10T09:10:23.176164Z","shell.execute_reply":"2023-01-10T09:10:32.871269Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## END","metadata":{}}]}