{"cells":[{"metadata":{"_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","trusted":true},"cell_type":"code","source":"import json\nimport math\nimport os\n\nimport cv2\nfrom PIL import Image\nimport numpy as np\nfrom keras import layers\nfrom keras.applications import DenseNet121\nfrom keras.callbacks import Callback, ModelCheckpoint\nfrom keras.preprocessing.image import ImageDataGenerator\nfrom keras.models import Sequential\nfrom keras.optimizers import Adam\nimport matplotlib.pyplot as plt\nimport pandas as pd\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.metrics import cohen_kappa_score, accuracy_score\nimport scipy\nimport tensorflow as tf\nfrom keras.callbacks import EarlyStopping,ReduceLROnPlateau,LearningRateScheduler\nfrom tqdm import tqdm_notebook as tqdm\n\n%matplotlib inline","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Set random seed for reproducibility."},{"metadata":{"trusted":true},"cell_type":"code","source":"! ls '../input/diabetic-retinopathy-models'","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"np.random.seed(2020)\ntf.set_random_seed(2020)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Loading & Exploration"},{"metadata":{"trusted":true},"cell_type":"code","source":"train_df = pd.read_csv('../input/aptos2019-blindness-detection/train.csv')\ntest_df = pd.read_csv('../input/aptos2019-blindness-detection/test.csv')\nprint(train_df.shape)\nprint(test_df.shape)\ntrain_df.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_df['diagnosis'].hist()\ntrain_df['diagnosis'].value_counts()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"### Displaying some Sample Images"},{"metadata":{"_kg_hide-input":true,"trusted":true},"cell_type":"code","source":"def display_samples(df, columns=4, rows=3):\n    fig=plt.figure(figsize=(5*columns, 4*rows))\n\n    for i in range(columns*rows):\n        image_path = df.loc[i,'id_code']\n        image_id = df.loc[i,'diagnosis']\n        img = cv2.imread(f'../input/aptos2019-blindness-detection/train_images/{image_path}.png')\n        img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)\n        \n        fig.add_subplot(rows, columns, i+1)\n        plt.title(image_id)\n        plt.imshow(img)\n    \n    plt.tight_layout()\n\ndisplay_samples(train_df)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Preprocess\n\n"},{"metadata":{"trusted":true},"cell_type":"code","source":"# image height and image width ----> GLOBAL\nimg_ht = 380\nimg_wd = 380\n\ndef displayImage(display_name, image):\n    cv2.namedWindow(display_name,cv2.WINDOW_AUTOSIZE)\n    cv2.imshow(display_name, image)\n\ndef findContourEye(thresh_image):\n    cnts = cv2.findContours(thresh_image.copy(), cv2.RETR_EXTERNAL,\n\tcv2.CHAIN_APPROX_SIMPLE)\n#     cnts = imutils.grab_contours(cnts)\n    cnts = max(cnts[0], key=cv2.contourArea)\n    return cnts\n\ndef findContourEyeExtreme(cnts):\n    # Locating extreme points on all 4 sides\n    leftmost = tuple(cnts[cnts[:,:,0].argmin()][0])\n    rightmost = tuple(cnts[cnts[:,:,0].argmax()][0])\n    topmost = tuple(cnts[cnts[:,:,1].argmin()][0])\n    bottommost = tuple(cnts[cnts[:,:,1].argmax()][0])\n    # Locating the top left and bottom right corner\n    x1 = leftmost[0]\n    y1 = topmost[1]\n    x2 = rightmost[0]\n    y2 = bottommost[1]\n    return x1,y1,x2,y2 \n\ndef findRadiusAndCentreOfContourEye(cnts):\n    M = cv2.moments(cnts)\n    if( M[\"m00\"]==0):\n        cX, cY = 0, 0\n    else:\n        cX = int(M[\"m10\"] / M[\"m00\"])\n        cY = int(M[\"m01\"] / M[\"m00\"])\n    if(cX < cY):\n        r = cX\n    else:\n        r = cY\n    return cX,cY,r\n\ndef drawCentreOnContourEye(image,cnts,cX,cY):\n    cv2.drawContours(image, [cnts], -1, (0, 255, 0), 2)\n    cv2.circle(image, (cX, cY), 7, (255, 255, 255), -1)\n    cv2.putText(image, \"center\", (cX - 20, cY - 20),\n    cv2.FONT_HERSHEY_SIMPLEX, 2, (255, 255, 255), 2)\n    return image\n    \ndef Radius_Reduction(img,cX,cY,r):\n    h,w,c=img.shape\n    Frame=np.zeros((h,w,c),dtype=np.uint8)\n    cv2.circle(Frame,(int(cX),int(cY)),int(r), (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\ndef imageResize(image, ht, wd):\n    # resized_image = imutils.resize(image, height = ht, width = wd)\n    resized_image = cv2.resize(image,(wd,ht))\n    return resized_image\n\ndef crop_black(image):\n    org = image.copy()\n    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)\n    blurred = cv2.GaussianBlur(gray, (5, 5), 0)\n    thresh = cv2.threshold(blurred, 10, 255, cv2.THRESH_BINARY)[1]\n    # displayImage('thresh',thresh)\n    cnts = findContourEye(thresh)\n    x1,y1,x2,y2 = findContourEyeExtreme(cnts)\n#     print(x1,y1,x2,y2)\n#     crop = org[y1:y2, x1:x2]\n#     crop = imageResize(crop, img_ht, img_wd)\n#     # displayImage(\"cr1\",crop)\n#     return crop\n    ext_x = int((x2 - x1)*4//100)\n    ext_y = int((y2 - y1)*5//100)\n#     print(ext_x,ext_y)\n    crop = org[y1+ext_y:y2-ext_y, x1+ext_x:x2-ext_x]\n    crop = imageResize(crop, img_ht, img_wd)\n    return crop\n\ndef imageAugmentation(image):\n    x_flip = cv2.flip( image, 0 )\n    y_flip = cv2.flip( image, 1 )\n    xy_flip = cv2.flip(x_flip,1)\n    return x_flip, y_flip, xy_flip\n\ndef imageHistEqualization(image):\n    lab = cv2.cvtColor(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    return final\n\ndef subtract_median_bg_image(im):\n    k = np.max(im.shape)//20*2+1\n    bg = cv2.medianBlur(im, k)\n    sub_med = cv2.addWeighted (im, 1, bg, -1, 255)\n    return sub_med\n\ndef colorEnhancement(image1,image2):\n    image_final = cv2.bitwise_and(image1,image2)\n    return image_final\n\ndef imageAugSave(path,img1,img2,img3,img4,img_ht,img_wd):\n    count = len(os.listdir(path))\n\n    img1 = imageResize(img1, img_ht, img_wd)\n    img2 = imageResize(img2, img_ht, img_wd)\n    img3 = imageResize(img3, img_ht, img_wd)\n    img4 = imageResize(img4, img_ht, img_wd)\n\n    cv2.imwrite(os.path.join(path , '%d.png'%(count+1)), img1)\n    cv2.imwrite(os.path.join(path , '%d.png'%(count+2)), img2)\n    cv2.imwrite(os.path.join(path , '%d.png'%(count+3)), img3)\n    cv2.imwrite(os.path.join(path , '%d.png'%(count+4)), img4)\n    return count+1,count+2,count+3,count+4\n\ndef processed_test_save(path,img,img_ht,img_wd):\n    count = len(os.listdir(path))\n    img = imageResize(img,img_ht,img_wd)\n    cv2.imwrite(os.path.join(path , '%d.png'%(count+1)), img)\n    return count+1\ndef get_pad_width(im, new_shape, is_rgb=True):\n    pad_diff = new_shape - im.shape[0], new_shape - im.shape[1]\n    t, b = math.floor(pad_diff[0]/2), math.ceil(pad_diff[0]/2)\n    l, r = math.floor(pad_diff[1]/2), math.ceil(pad_diff[1]/2)\n    if is_rgb:\n        pad_width = ((t,b), (l,r), (0, 0))\n    else:\n        pad_width = ((t,b), (l,r))\n    return pad_width\n\ndef new_preprocess_image(image_path, desired_size=380):\n    image = cv2.imread(image_path)\n    image = imageResize(image, desired_size, desired_size)\n    image_crop = crop_black(image)\n    image_clahe = imageHistEqualization(image_crop)\n    sub_med = subtract_median_bg_image(image_clahe)\n    image_final = colorEnhancement(sub_med, image_clahe)\n    return image_final\n\n# def preprocess_image(image_path, desired_size=380):\n#     im = Image.open(image_path)\n#     im = im.resize((desired_size, )*2, resample=Image.LANCZOS)\n    \n#     return im\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def get_pad_width(im, new_shape, is_rgb=True):\n    pad_diff = new_shape - im.shape[0], new_shape - im.shape[1]\n    t, b = math.floor(pad_diff[0]/2), math.ceil(pad_diff[0]/2)\n    l, r = math.floor(pad_diff[1]/2), math.ceil(pad_diff[1]/2)\n    if is_rgb:\n        pad_width = ((t,b), (l,r), (0, 0))\n    else:\n        pad_width = ((t,b), (l,r))\n    return pad_width\n\ndef preprocess_image(image_path, desired_size=380):\n    im = Image.open(image_path)\n    im = im.resize((desired_size, )*2, resample=Image.LANCZOS)\n    \n    return im","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def display_samples(df, columns=4, rows=3):\n    fig=plt.figure(figsize=(5*columns, 4*rows))\n\n    for i in range(columns*rows):\n        image_path = df.loc[i,'id_code']\n        image_id = df.loc[i,'diagnosis']\n        img = preprocess_image(f'../input/aptos2019-blindness-detection/train_images/{image_path}.png')\n#         img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)\n        \n        fig.add_subplot(rows, columns, i+1)\n        plt.title(image_id)\n        plt.imshow(img)\n    \n    plt.tight_layout()\n\ndisplay_samples(train_df)\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def display_samples(df, columns=4, rows=3):\n    fig=plt.figure(figsize=(5*columns, 4*rows))\n\n    for i in range(columns*rows):\n        image_path = df.loc[i,'id_code']\n        image_id = df.loc[i,'diagnosis']\n        img = new_preprocess_image(f'../input/aptos2019-blindness-detection/train_images/{image_path}.png')\n        img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)\n        \n        fig.add_subplot(rows, columns, i+1)\n        plt.title(image_id)\n        plt.imshow(img)\n    \n    plt.tight_layout()\n\ndisplay_samples(train_df)\n","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Efficienetnet"},{"metadata":{"trusted":true},"cell_type":"code","source":"import gc\ngc.collect()\nimport os\nimport sys\n# Repository source: https://github.com/qubvel/efficientnet\nsys.path.append(os.path.abspath('../input/efficientnet/efficientnet-master/efficientnet-master/'))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def build_model():\n    model = Sequential()\n    model.add(cnn_net)\n    model.add(layers.GlobalAveragePooling2D())\n    model.add(layers.Dropout(0.5))\n    model.add(layers.Dense(5, activation='sigmoid'))\n    \n    model.compile(\n        loss='binary_crossentropy',\n        optimizer=Adam(lr=0.00005),\n        metrics=['accuracy']\n    )\n    \n    return model","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from efficientnet import EfficientNetB3\n# Load in EfficientNetB3\ncnn_net = EfficientNetB3(weights=None,\n                        include_top=False,\n                        input_shape=(300, 300, 3))\ncnn_net.load_weights('../input/efficientnet-keras-weights-b0b5/efficientnet-b3_imagenet_1000_notop.h5')\n\nmodel_b3 = build_model()\nmodel_b3.load_weights('../input/diabetic-retinopathy-models/effnet_b3.h5')\nmodel_b3.summary()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from efficientnet import EfficientNetB5\n# Load in EfficientNetB5\ncnn_net = EfficientNetB5(weights=None,\n                        include_top=False,\n                        input_shape=(456, 456, 3))\ncnn_net.load_weights('../input/efficientnet-keras-weights-b0b5/efficientnet-b5_imagenet_1000_notop.h5')\n\nmodel_b5 = build_model()\nmodel_b5.load_weights('../input/diabetic-retinopathy-models/effnet_b5.h5')\nmodel_b5.summary()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from efficientnet import EfficientNetB5\n# Load in EfficientNetB5\ncnn_net = EfficientNetB5(weights=None,\n                        include_top=False,\n                        input_shape=(380, 380, 3))\ncnn_net.load_weights('../input/efficientnet-keras-weights-b0b5/efficientnet-b5_imagenet_1000_notop.h5')\n\nmodel_prc = build_model()\nmodel_prc.load_weights('../input/diabetic-retinopathy-models/effb5_old_new_preprocess.h5')\nmodel_prc.summary()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from efficientnet import EfficientNetB5\n# Load in EfficientNetB5\ncnn_net = EfficientNetB5(weights=None,\n                        include_top=False,\n                        input_shape=(456, 456, 3))\ncnn_net.load_weights('../input/efficientnet-keras-weights-b0b5/efficientnet-b5_imagenet_1000_notop.h5')\n\nmodel_old = build_model()\nmodel_old.load_weights('../input/m1-effnetb5-using-ram-fork1-3a0430/model.h5')\nmodel_old.summary()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Submit"},{"metadata":{"trusted":true},"cell_type":"code","source":" min_class = str(l_ans[0])\n        max_class = str(l_ans[1])\n        return (\"Diabetis Retinopathy class is between class : %s to class : %s\",min_class,max_class)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from collections import Counter\nimport random\n\ndef mode_ans(lst):\n    l_ans = []\n    d_mem_count = Counter(lst)\n    cnt_max = max(d_mem_count.values())\n    for k in d_mem_count.keys():\n        if(d_mem_count[k] == cnt_max):\n            l_ans.append(k)\n    if(len(l_ans) == 1):\n        return \"Diabetis Retinopathy class is :\"+str(l_ans[0])\n    else:\n        l_ans.sort()\n        return \"Diabetis Retinopathy class is between class : \"+str(l_ans[0])+\" to class : \"+str(l_ans[-1])\n#         return random.choice(l_ans)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"cnt_b3 = 0\ncnt_b5 = 0\ncnt_old = 0\ncnt_mode = 0\ncnt_prc = 0\nfrom statistics import mode\nfrom collections import Counter\n# train_df = pd.read_csv('../input/diabetic-retinopathy-resized/trainLabels.csv')\n\nS = 950\nN = 955\nfor i in tqdm(range(S,N)):\n        \n    img_b3 = preprocess_image('../input/aptos2019-blindness-detection/train_images/%s.png'%train_df['id_code'][i],desired_size = 300)\n    img_b3 = np.expand_dims(img_b3,axis = 0)\n    ans_b3 = model_b3.predict(img_b3) > 0.5\n    ans_b3 = (ans_b3.astype(int).sum(axis=1) - 1)[0]\n    if(ans_b3 == train_df['diagnosis'][i]):\n        cnt_b3 += 1\n    \n    img_b5 = preprocess_image('../input/aptos2019-blindness-detection/train_images/%s.png'%train_df['id_code'][i],desired_size = 456)\n    img_b5 = np.expand_dims(img_b5,axis = 0)\n    ans_b5 = model_b5.predict(img_b5) > 0.5\n    ans_b5 = (ans_b5.astype(int).sum(axis=1) - 1)[0]   \n    if(ans_b5 == train_df['diagnosis'][i]):\n        cnt_b5 += 1\n        \n    img_prc = new_preprocess_image('../input/aptos2019-blindness-detection/train_images/%s.png'%train_df['id_code'][i],desired_size = 380)\n    img_prc = np.expand_dims(img_prc,axis = 0)\n    ans_prc = model_prc.predict(img_prc) > 0.5\n    ans_prc = (ans_prc.astype(int).sum(axis=1) - 1)[0]   \n    if(ans_prc == train_df['diagnosis'][i]):\n        cnt_prc += 1\n        \n    img_old = preprocess_image('../input/aptos2019-blindness-detection/train_images/%s.png'%train_df['id_code'][i],desired_size = 456)\n    img_old = np.expand_dims(img_old,axis = 0)\n    ans_old = model_old.predict(img_old) > 0.5\n    ans_old = (ans_old.astype(int).sum(axis=1) - 1)[0]   \n    if(ans_old == train_df['diagnosis'][i]):\n        cnt_old += 1\n    \n    print(train_df['id_code'][i]+\".png\",\"\\nModel Predictions : \",ans_b3,ans_b5,ans_prc,ans_old)\n#     ------------------------------------------------------------------------------------------------------------------------\n    \n    l = [ans_b3,ans_b5,ans_prc,ans_old]\n    ans_mode = mode_ans(l)\n    \n    print(ans_mode,\"\\n Actual answer : \",train_df['diagnosis'][i],\"\\n\")\n#     print(\"mode ans : \",ans_mode,\"final ans : \",train_df['diagnosis'][i])\n   \nN = N - S\nprint(cnt_b3,cnt_b5,cnt_prc,cnt_old)\nprint(cnt_b3/N,cnt_b5/N,cnt_prc/N,cnt_old/N)\n\nimport gc\ngc.collect()\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"codemirror_mode":{"name":"ipython","version":3},"file_extension":".py","mimetype":"text/x-python","name":"python","nbconvert_exporter":"python","pygments_lexer":"ipython3","version":"3.6.6"}},"nbformat":4,"nbformat_minor":4}