{"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 # data processing, CSV file I/O (e.g. pd.read_csv)\n\nimport os\nprint(os.listdir(\"../input\"))\n\nfrom keras.preprocessing import image\nimport matplotlib.pyplot as plt\nfrom sklearn.model_selection import train_test_split\n\nimport keras\nfrom keras.models import Sequential\nfrom keras.layers import Dense, Dropout, Flatten\nfrom keras.layers import Conv2D, MaxPooling2D\nfrom keras.utils import to_categorical\n\nimport numpy as np\nimport pandas as pd\nfrom tqdm import tqdm\n\nfrom sklearn.metrics import confusion_matrix\nimport seaborn as sn; sn.set(font_scale=1.4)\nfrom sklearn.utils import shuffle                      \nimport cv2                                               \nimport tensorflow.keras.models as Models\nimport tensorflow.keras.layers as Layers\nimport tensorflow.keras.activations as Actications\nimport tensorflow.keras.models as Models\nimport tensorflow.keras.optimizers as Optimizer\nimport tensorflow.keras.metrics as Metrics\nimport tensorflow.keras.utils as Utils\nfrom keras.utils.vis_utils import model_to_dot\n\nfrom scipy import ndimage","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-07-05T19:08:01.568249Z","iopub.execute_input":"2021-07-05T19:08:01.568867Z","iopub.status.idle":"2021-07-05T19:08:08.177376Z","shell.execute_reply.started":"2021-07-05T19:08:01.568784Z","shell.execute_reply":"2021-07-05T19:08:08.176520Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_path = \"/kaggle/input/aptos2019-blindness-detection/train_images/\"\ntest_path = \"/kaggle/input/aptos2019-blindness-detection/test_images\"\n\ntrain_data = pd.read_csv(\"../input/aptos2019-blindness-detection/train.csv\")\ntest_data = pd.read_csv(\"../input/aptos2019-blindness-detection/test.csv\")","metadata":{"execution":{"iopub.status.busy":"2021-07-05T19:08:08.178754Z","iopub.execute_input":"2021-07-05T19:08:08.179238Z","iopub.status.idle":"2021-07-05T19:08:08.224335Z","shell.execute_reply.started":"2021-07-05T19:08:08.179206Z","shell.execute_reply":"2021-07-05T19:08:08.223397Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data.head()","metadata":{"execution":{"iopub.status.busy":"2021-07-05T19:08:08.225872Z","iopub.execute_input":"2021-07-05T19:08:08.226402Z","iopub.status.idle":"2021-07-05T19:08:08.249818Z","shell.execute_reply.started":"2021-07-05T19:08:08.226370Z","shell.execute_reply":"2021-07-05T19:08:08.248851Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_data.head()","metadata":{"execution":{"iopub.status.busy":"2021-07-05T19:08:08.251235Z","iopub.execute_input":"2021-07-05T19:08:08.251497Z","iopub.status.idle":"2021-07-05T19:08:08.259737Z","shell.execute_reply.started":"2021-07-05T19:08:08.251471Z","shell.execute_reply":"2021-07-05T19:08:08.258587Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(len(train_data))","metadata":{"execution":{"iopub.status.busy":"2021-07-05T19:08:08.260978Z","iopub.execute_input":"2021-07-05T19:08:08.261255Z","iopub.status.idle":"2021-07-05T19:08:08.272700Z","shell.execute_reply.started":"2021-07-05T19:08:08.261230Z","shell.execute_reply":"2021-07-05T19:08:08.271813Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(len(test_data))","metadata":{"execution":{"iopub.status.busy":"2021-07-05T19:08:08.274053Z","iopub.execute_input":"2021-07-05T19:08:08.274404Z","iopub.status.idle":"2021-07-05T19:08:08.285523Z","shell.execute_reply.started":"2021-07-05T19:08:08.274375Z","shell.execute_reply":"2021-07-05T19:08:08.284847Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data['diagnosis'].value_counts()\ntrain_data['diagnosis'].hist()","metadata":{"execution":{"iopub.status.busy":"2021-07-05T19:08:08.286447Z","iopub.execute_input":"2021-07-05T19:08:08.286835Z","iopub.status.idle":"2021-07-05T19:08:08.672265Z","shell.execute_reply.started":"2021-07-05T19:08:08.286807Z","shell.execute_reply":"2021-07-05T19:08:08.671306Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import seaborn as sns\nimport matplotlib.pyplot as plt\n%matplotlib inline\n\nimport cv2\nfrom PIL import Image\n\nimport tensorflow as tf\nfrom keras import layers\nfrom tensorflow.keras import applications \nfrom keras.applications import MobileNetV2\nfrom keras.callbacks import Callback, ModelCheckpoint\nfrom keras.preprocessing.image import ImageDataGenerator\nfrom keras.models import Sequential, load_model\nfrom keras.optimizers import Adam\nfrom keras import models\n\n\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.metrics import cohen_kappa_score, accuracy_score, confusion_matrix\n\nfrom tqdm import tqdm\n\nimport math\n\nIMAGE_SIZE = 500\n\ndef info_image(im):\n    # Compute the center (cx, cy) and radius of the eye\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: # This actually rarely happens p~1/10000\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    # Crops, resizes and potentially augments the image to IMAGE_SIZE\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)) # This is the most important line\n\nPARAM = 96\n\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\n\nimg_size = 500\ndef preprocess_image(image_path, desired_size=224):\n    img = cv2.imread(image_path)\n    \n    img_opt=cv2.addWeighted (img, -5, cv2.GaussianBlur(img, (0,0), img_size/10), 5, 128)\n    img_rad_red=Radius_Reduction(img_opt, PARAM)\n    \n    img_opt1=cv2.addWeighted (img, 5, cv2.GaussianBlur(img, (0,0), img_size/10), -5, 128)\n    img_rad_red1=Radius_Reduction(img_opt1, PARAM)\n\n    final = img_rad_red + img_rad_red1\n    img_rad_red2=Radius_Reduction(final, PARAM)\n\n    \n    return img_rad_red2","metadata":{"execution":{"iopub.status.busy":"2021-07-05T19:08:08.674040Z","iopub.execute_input":"2021-07-05T19:08:08.674292Z","iopub.status.idle":"2021-07-05T19:08:08.706736Z","shell.execute_reply.started":"2021-07-05T19:08:08.674266Z","shell.execute_reply":"2021-07-05T19:08:08.705808Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"N = train_data.shape[0]\nx_train = np.empty((N, 500, 500, 3), dtype=np.uint8())\n\nfor i, image_id in enumerate(tqdm(train_data['id_code'])):\n    x_train[i, :, :, :] = preprocess_image(\n        f'../input/aptos2019-blindness-detection/train_images/{image_id}.png'\n    )\n    \nN = test_data.shape[0]\nx_test = np.empty((N, 500,500, 3), dtype=np.uint8())\n\nfor i, image_id in enumerate(tqdm(test_data['id_code'])):\n    x_test[i, :, :, :] = preprocess_image(\n        f'../input/aptos2019-blindness-detection/train_images/{image_id}.png'\n    )","metadata":{"execution":{"iopub.status.busy":"2021-07-05T19:08:08.708354Z","iopub.execute_input":"2021-07-05T19:08:08.708727Z","iopub.status.idle":"2021-07-05T19:08:13.404306Z","shell.execute_reply.started":"2021-07-05T19:08:08.708695Z","shell.execute_reply":"2021-07-05T19:08:13.402927Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}