{"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)\nimport os\nprint(os.listdir(\"../input\"))\nimport warnings\nwarnings.filterwarnings(\"ignore\")","metadata":{"execution":{"iopub.status.busy":"2022-02-14T18:41:53.103232Z","iopub.execute_input":"2022-02-14T18:41:53.103942Z","iopub.status.idle":"2022-02-14T18:41:53.133913Z","shell.execute_reply.started":"2022-02-14T18:41:53.103857Z","shell.execute_reply":"2022-02-14T18:41:53.133070Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\nimport matplotlib.pyplot as plt\nimport cv2\n%matplotlib inline\n\nfrom sklearn.utils import class_weight, shuffle\nfrom sklearn.model_selection import train_test_split\n","metadata":{"execution":{"iopub.status.busy":"2022-02-14T18:41:53.135642Z","iopub.execute_input":"2022-02-14T18:41:53.135893Z","iopub.status.idle":"2022-02-14T18:41:54.301211Z","shell.execute_reply.started":"2022-02-14T18:41:53.135858Z","shell.execute_reply":"2022-02-14T18:41:54.300285Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"IMG_SIZE = 224\nBATCH_SIZE = 48\nSEED = 1020","metadata":{"execution":{"iopub.status.busy":"2022-02-14T18:41:54.305136Z","iopub.execute_input":"2022-02-14T18:41:54.305358Z","iopub.status.idle":"2022-02-14T18:41:54.313192Z","shell.execute_reply.started":"2022-02-14T18:41:54.305331Z","shell.execute_reply":"2022-02-14T18:41:54.312435Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import random\n\ndef seed_everything(seed):\n    random.seed(seed)\n    os.environ['PYTHONHASHSEED'] = str(seed)\n    np.random.seed(seed)\n\nseed_everything(SEED)","metadata":{"execution":{"iopub.status.busy":"2022-02-14T18:41:54.317362Z","iopub.execute_input":"2022-02-14T18:41:54.317916Z","iopub.status.idle":"2022-02-14T18:41:54.330125Z","shell.execute_reply.started":"2022-02-14T18:41:54.317880Z","shell.execute_reply":"2022-02-14T18:41:54.326435Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train = pd.read_csv('../input/aptos2019-blindness-detection/train.csv')\ndf_test = pd.read_csv('../input/aptos2019-blindness-detection/test.csv')\n\nx = df_train['id_code']\ny = df_train['diagnosis']\n\nx, y = shuffle(x, y, random_state=SEED)","metadata":{"execution":{"iopub.status.busy":"2022-02-14T18:41:54.333604Z","iopub.execute_input":"2022-02-14T18:41:54.334938Z","iopub.status.idle":"2022-02-14T18:41:54.364310Z","shell.execute_reply.started":"2022-02-14T18:41:54.334899Z","shell.execute_reply":"2022-02-14T18:41:54.363666Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y.hist()","metadata":{"execution":{"iopub.status.busy":"2022-02-14T18:41:54.365551Z","iopub.execute_input":"2022-02-14T18:41:54.365784Z","iopub.status.idle":"2022-02-14T18:41:54.662861Z","shell.execute_reply.started":"2022-02-14T18:41:54.365753Z","shell.execute_reply":"2022-02-14T18:41:54.662128Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Data Pre-processing","metadata":{}},{"cell_type":"code","source":"def crop_image_from_gray(img,tol=5):\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            img = np.stack([img1,img2,img3],axis=-1)\n        return img\n\ndef circle_crop(img, sigmaX=10, gaussian=True):\n    img = crop_image_from_gray(img)\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    if gaussian: img=cv2.addWeighted(img, 4, cv2.GaussianBlur(img, (0,0), sigmaX), -4, 128)\n    return img","metadata":{"execution":{"iopub.status.busy":"2022-02-14T18:41:54.664152Z","iopub.execute_input":"2022-02-14T18:41:54.664413Z","iopub.status.idle":"2022-02-14T18:41:54.675688Z","shell.execute_reply.started":"2022-02-14T18:41:54.664378Z","shell.execute_reply":"2022-02-14T18:41:54.674987Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig = plt.figure(figsize=(25, 25))\nfor class_id in sorted(y.unique()):\n    for i, (idx, row) in enumerate(df_train.loc[df_train['diagnosis'] == class_id].sample(1, random_state=SEED).iterrows()):\n        path=f\"../input/aptos2019-blindness-detection/train_images/{row['id_code']}.png\"\n        image = cv2.imread(path)\n        image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)\n        \n        ax = fig.add_subplot(5, 5, i * 5 + class_id + 1, xticks=[], yticks=[])\n        image_normal = circle_crop(image, gaussian=False)\n        image_normal = cv2.resize(image_normal, (IMG_SIZE, IMG_SIZE))\n        plt.imshow(image_normal)\n        ax.set_title('Normal, ID: %s' % (row['id_code']) )\n        \n        ax = fig.add_subplot(5, 5, (i+1) * 5 + class_id + 1, xticks=[], yticks=[])\n        img_circle_gaussian=circle_crop(image, gaussian=False)\n        img_circle_gaussian=cv2.resize(img_circle_gaussian, (IMG_SIZE, IMG_SIZE))\n        img_circle_gaussian=circle_crop(img_circle_gaussian, sigmaX=5, gaussian=True)\n        img_circle_gaussian=circle_crop(img_circle_gaussian, gaussian=False)\n        plt.imshow(img_circle_gaussian)\n        ax.set_title('Circle Crop Gaussian Blur, ID: %s' % (row['id_code']) )\n        \n        ax = fig.add_subplot(5, 5, (i+2) * 5 + class_id + 1, xticks=[], yticks=[])\n        img_circle_gaussian=circle_crop(image, gaussian=False)\n        img_circle_gaussian=cv2.resize(img_circle_gaussian, (IMG_SIZE, IMG_SIZE))\n        img_circle_gaussian=circle_crop(img_circle_gaussian, sigmaX=10, gaussian=True)\n        img_circle_gaussian=circle_crop(img_circle_gaussian, gaussian=False)\n        plt.imshow(img_circle_gaussian)\n        ax.set_title('Circle Crop Gaussian Blur, ID: %s' % (row['id_code']) )\n        \n        ax = fig.add_subplot(5, 5, (i+3) * 5 + class_id + 1, xticks=[], yticks=[])\n        img_circle_gaussian=circle_crop(image, gaussian=False)\n        img_circle_gaussian=cv2.resize(img_circle_gaussian, (IMG_SIZE, IMG_SIZE))\n        img_circle_gaussian=circle_crop(img_circle_gaussian, sigmaX=20, gaussian=True)\n        img_circle_gaussian=circle_crop(img_circle_gaussian, gaussian=False)\n        plt.imshow(img_circle_gaussian)\n        ax.set_title('Circle Crop Gaussian Blur, ID: %s' % (row['id_code']) )\n        \n        ax = fig.add_subplot(5, 5, (i+4) * 5 + class_id + 1, xticks=[], yticks=[])\n        img_circle_gaussian=circle_crop(image, gaussian=False)\n        img_circle_gaussian=cv2.resize(img_circle_gaussian, (IMG_SIZE, IMG_SIZE))\n        img_circle_gaussian=circle_crop(img_circle_gaussian, sigmaX=30, gaussian=True)\n        img_circle_gaussian=circle_crop(img_circle_gaussian, gaussian=False)\n        plt.imshow(img_circle_gaussian)\n        ax.set_title('Circle Crop Gaussian Blur, ID: %s' % (row['id_code']) )\n        \nplt.savefig('./pre-processing_1.png')","metadata":{"execution":{"iopub.status.busy":"2022-02-14T18:41:54.677049Z","iopub.execute_input":"2022-02-14T18:41:54.677463Z","iopub.status.idle":"2022-02-14T18:42:04.463479Z","shell.execute_reply.started":"2022-02-14T18:41:54.677408Z","shell.execute_reply":"2022-02-14T18:42:04.462691Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# import multiprocessing as mp\n# NCORE = 20\n\n# def process(q, iolock):\n#     while True:\n#         stuff = q.get()\n#         if stuff is None:\n#             break\n#         idx, row, sets = stuff\n#         path=f\"../input/aptos2019-blindness-detection/{sets}_images/{row['id_code']}.png\"\n#         image = cv2.imread(path)\n#         image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)\n        \n#         img_circle_gaussian=circle_crop(image, gaussian=False)\n#         img_circle_gaussian=cv2.resize(img_circle_gaussian, (IMG_SIZE, IMG_SIZE))\n#         img_circle_gaussian=circle_crop(img_circle_gaussian, sigmaX=5, gaussian=True)\n#         img_circle_gaussian=circle_crop(img_circle_gaussian, gaussian=False)\n        \n#         if sets=='train':\n#             cv2.imwrite(f\"./{sets}/{row['diagnosis']}/{row['id_code']}.png\", img_circle_gaussian)\n#         else:\n#             cv2.imwrite(f\"./{sets}/{row['id_code']}.png\", img_circle_gaussian)\n\n\n# for class_id in sorted(y.unique()):\n#     if not os.path.exists(f'./train/{class_id}/'): os.makedirs(f'./train/{class_id}/')\n# if not os.path.exists(f'./test/'): os.makedirs(f'./test/')\n    \n# q = mp.Queue(maxsize=NCORE)\n# iolock = mp.Lock()\n# pool = mp.Pool(NCORE, initializer=process, initargs=(q, iolock))\n# for i, (idx,row) in enumerate(df_train.iterrows()):\n#     stuff = (idx, row, 'train')\n#     q.put(stuff)\n# for i, (idx,row) in enumerate(df_test.iterrows()):\n#     stuff = (idx, row, 'test')\n#     q.put(stuff)\n# for _ in range(NCORE):\n#     q.put(None)\n# pool.close()\n# pool.join()","metadata":{"execution":{"iopub.status.busy":"2022-02-14T18:42:04.464628Z","iopub.execute_input":"2022-02-14T18:42:04.464871Z","iopub.status.idle":"2022-02-14T18:42:04.470901Z","shell.execute_reply.started":"2022-02-14T18:42:04.464838Z","shell.execute_reply":"2022-02-14T18:42:04.470330Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# df_train = pd.read_csv('../input/aptos2019-blindness-detection/train.csv')\n# df_test = pd.read_csv('../input/aptos2019-blindness-detection/test.csv')\n# submission = pd.read_csv('../input/aptos2019-blindness-detection/sample_submission.csv')\n# df_train.to_csv(\"./train.csv\")\n# df_test.to_csv(\"./test.csv\")\n# submission.to_csv(\"./sample_submission.csv\")","metadata":{"execution":{"iopub.status.busy":"2022-02-14T18:42:04.473996Z","iopub.execute_input":"2022-02-14T18:42:04.474451Z","iopub.status.idle":"2022-02-14T18:42:04.484573Z","shell.execute_reply.started":"2022-02-14T18:42:04.474413Z","shell.execute_reply":"2022-02-14T18:42:04.483807Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf\nimport tensorflow.keras as keras\nimport tensorflow.keras.layers as layers\nfrom tensorflow.keras.applications import *","metadata":{"execution":{"iopub.status.busy":"2022-02-14T18:42:04.486178Z","iopub.execute_input":"2022-02-14T18:42:04.486687Z","iopub.status.idle":"2022-02-14T18:42:09.458233Z","shell.execute_reply.started":"2022-02-14T18:42:04.486626Z","shell.execute_reply":"2022-02-14T18:42:09.457305Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# input_size=(IMG_SIZE,IMG_SIZE,3)\n# base_model_1 = ResNet50V2(input_shape=input_size, weights='imagenet', include_top=False)\n# base_model_2 = InceptionV3(input_shape=input_size, weights='imagenet', include_top=False)\n# base_model_3 = DenseNet201(input_shape=input_size, weights='imagenet', include_top=False)\n\n# if not os.path.exists(f'./model/'): os.makedirs(f'./model/')\n# base_model_1.save_weights(f'./model/resnet50v2.h5')\n# base_model_2.save_weights(f'./model/inceptionv3.h5')\n# base_model_3.save_weights(f'./model/densenet201.h5')","metadata":{"execution":{"iopub.status.busy":"2022-02-14T18:42:09.460134Z","iopub.execute_input":"2022-02-14T18:42:09.460736Z","iopub.status.idle":"2022-02-14T18:42:09.465068Z","shell.execute_reply.started":"2022-02-14T18:42:09.460599Z","shell.execute_reply":"2022-02-14T18:42:09.464358Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def my_model_2(input_size=(IMG_SIZE,IMG_SIZE,3), cross_stitch=True):\n    inp = keras.Input(shape=input_size)\n    x = layers.Rescaling(1./255)(inp)\n    \n    base_model_1 = ResNet50V2(input_shape=input_size, \n                              weights=f'../input/aptos2019-512-tfstyle/model/resnet50v2.h5', \n                              include_top=False)\n    base_model_1.trainable = False\n    base_model_2 = InceptionV3(input_shape=input_size, \n                               weights=f'../input/aptos2019-512-tfstyle/model/inceptionv3.h5', \n                               include_top=False)\n    base_model_2.trainable = False\n    base_model_3 = DenseNet201(input_shape=input_size, \n                               weights=f'../input/aptos2019-512-tfstyle/model/densenet201.h5',\n                               include_top=False)\n    base_model_3.trainable = False \n\n    x_1 = base_model_1(x, training=False)\n    x_2 = base_model_2(x, training=False)\n    x_3 = base_model_3(x, training=False)\n    x_1 = keras.layers.GlobalAveragePooling2D()(x_1)\n    x_2 = keras.layers.GlobalAveragePooling2D()(x_2)\n    x_3 = keras.layers.GlobalAveragePooling2D()(x_3)\n                                                \n    x_1 = keras.layers.Dense(800, activation='relu', kernel_initializer='lecun_normal', kernel_regularizer= keras.regularizers.l2(0.001))(x_1)\n    x_2 = keras.layers.Dense(800, activation='relu', kernel_initializer='lecun_normal', kernel_regularizer= keras.regularizers.l2(0.001))(x_2)\n    x_3 = keras.layers.Dense(800, activation='relu', kernel_initializer='lecun_normal', kernel_regularizer= keras.regularizers.l2(0.001))(x_3)\n\n    cross_stitch_2 = tf.Variable([[0.5,0.5,0.5],[0.5,0.5,0.5],[0.5,0.5,0.5]], dtype=tf.float32, trainable=True)\n    if cross_stitch==True:\n        cross_x_1 = cross_stitch_2[0,0] * x_1 + cross_stitch_2[0,1] * x_2 + cross_stitch_2[0,2] * x_3\n        cross_x_2 = cross_stitch_2[1,0] * x_1 + cross_stitch_2[1,1] * x_2 + cross_stitch_2[1,2] * x_3\n        cross_x_3 = cross_stitch_2[2,0] * x_1 + cross_stitch_2[2,1] * x_2 + cross_stitch_2[2,2] * x_3\n    else:\n        cross_x_1 = x_1\n        cross_x_2 = x_2\n        cross_x_3 = x_3\n    x_1 = keras.layers.Dense(200, activation='relu', kernel_initializer='lecun_normal', kernel_regularizer= keras.regularizers.l2(0.001))(cross_x_1)\n    x_2 = keras.layers.Dense(200, activation='relu', kernel_initializer='lecun_normal', kernel_regularizer= keras.regularizers.l2(0.001))(cross_x_2)\n    x_3 = keras.layers.Dense(200, activation='relu', kernel_initializer='lecun_normal', kernel_regularizer= keras.regularizers.l2(0.001))(cross_x_3)\n\n    cross_stitch_3 = tf.Variable([[0.5,0.5,0.5],[0.5,0.5,0.5],[0.5,0.5,0.5]], dtype=tf.float32, trainable=True)\n    if cross_stitch==True:\n        cross_x_1 = cross_stitch_3[0,0] * x_1 + cross_stitch_3[0,1] * x_2 + cross_stitch_3[0,2] * x_3\n        cross_x_2 = cross_stitch_3[1,0] * x_1 + cross_stitch_3[1,1] * x_2 + cross_stitch_3[1,2] * x_3\n        cross_x_3 = cross_stitch_3[2,0] * x_1 + cross_stitch_3[2,1] * x_2 + cross_stitch_3[2,2] * x_3\n    else:\n        cross_x_1 = x_1\n        cross_x_2 = x_2\n        cross_x_3 = x_3\n    x_1 = keras.layers.Dense(5)(cross_x_1)\n    x_2 = keras.layers.Dense(5)(cross_x_2)\n    x_3 = keras.layers.Dense(5)(cross_x_3)\n\n    aver_vairable = tf.Variable([0.33333,0.33333,0.33333], dtype=tf.float32, trainable=False)\n    x = aver_vairable[0] * x_1 + aver_vairable[1] * x_2 + aver_vairable[2] * x_3\n    model = keras.Model(inputs=inp, outputs=x)\n    return model","metadata":{"execution":{"iopub.status.busy":"2022-02-14T18:42:09.470479Z","iopub.execute_input":"2022-02-14T18:42:09.471070Z","iopub.status.idle":"2022-02-14T18:42:09.509004Z","shell.execute_reply.started":"2022-02-14T18:42:09.471024Z","shell.execute_reply":"2022-02-14T18:42:09.507762Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = my_model_2()\nmodel.summary()","metadata":{"_kg_hide-output":true,"execution":{"iopub.status.busy":"2022-02-14T18:42:09.511431Z","iopub.execute_input":"2022-02-14T18:42:09.512186Z","iopub.status.idle":"2022-02-14T18:42:25.520370Z","shell.execute_reply.started":"2022-02-14T18:42:09.512148Z","shell.execute_reply":"2022-02-14T18:42:25.519482Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_aug = keras.Sequential(\n  [\n    layers.RandomFlip(input_shape=(IMG_SIZE,IMG_SIZE,3)),\n    layers.RandomRotation(0.4, fill_mode='constant', fill_value=0),\n    layers.RandomZoom(0.1, fill_mode='constant', fill_value=0)\n  ]\n)","metadata":{"execution":{"iopub.status.busy":"2022-02-14T18:42:25.521776Z","iopub.execute_input":"2022-02-14T18:42:25.522061Z","iopub.status.idle":"2022-02-14T18:42:25.680829Z","shell.execute_reply.started":"2022-02-14T18:42:25.522021Z","shell.execute_reply":"2022-02-14T18:42:25.680008Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\ntrain_ds = (\n        tf.keras.utils.image_dataset_from_directory(\n        '../input/aptos2019-512-tfstyle/train',\n        validation_split=0.1,\n        seed=1020,\n        subset=\"training\",\n        image_size=(IMG_SIZE, IMG_SIZE),\n        batch_size=BATCH_SIZE)\n        .map(lambda x, y: (data_aug(x), y), num_parallel_calls=tf.data.AUTOTUNE)\n        )\nval_ds = (tf.keras.utils.image_dataset_from_directory(\n        '../input/aptos2019-512-tfstyle/train',\n        validation_split=0.1,\n        seed=1020,\n        subset=\"validation\",\n        image_size=(IMG_SIZE, IMG_SIZE),\n        batch_size=BATCH_SIZE)\n        )","metadata":{"execution":{"iopub.status.busy":"2022-02-14T18:42:25.682242Z","iopub.execute_input":"2022-02-14T18:42:25.682523Z","iopub.status.idle":"2022-02-14T18:42:27.445480Z","shell.execute_reply.started":"2022-02-14T18:42:25.682486Z","shell.execute_reply":"2022-02-14T18:42:27.444286Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"base_learning_rate = 0.001\nmodel.compile(optimizer=tf.keras.optimizers.Adam(lr=base_learning_rate),\n              loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),\n              metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2022-02-14T18:42:27.446667Z","iopub.execute_input":"2022-02-14T18:42:27.447317Z","iopub.status.idle":"2022-02-14T18:42:27.481460Z","shell.execute_reply.started":"2022-02-14T18:42:27.447275Z","shell.execute_reply":"2022-02-14T18:42:27.480693Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"epochs=20\nhistory = model.fit(train_ds,validation_data=val_ds,epochs=epochs)","metadata":{"execution":{"iopub.status.busy":"2022-02-14T18:42:27.482612Z","iopub.execute_input":"2022-02-14T18:42:27.483127Z","iopub.status.idle":"2022-02-14T18:49:39.502191Z","shell.execute_reply.started":"2022-02-14T18:42:27.483098Z","shell.execute_reply":"2022-02-14T18:49:39.501464Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"acc = history.history['accuracy']\nval_acc = history.history['val_accuracy']\n\nloss = history.history['loss']\nval_loss = history.history['val_loss']\n\nepochs_range = range(epochs)\n\nplt.figure(figsize=(8, 8))\nplt.subplot(1, 2, 1)\nplt.plot(epochs_range, acc, label='Training Accuracy')\nplt.plot(epochs_range, val_acc, label='Validation Accuracy')\nplt.legend(loc='lower right')\nplt.title('Training and Validation Accuracy')\n\nplt.subplot(1, 2, 2)\nplt.plot(epochs_range, loss, label='Training Loss')\nplt.plot(epochs_range, val_loss, label='Validation Loss')\nplt.legend(loc='upper right')\nplt.title('Training and Validation Loss')\nplt.ylim((0.5,1.5))\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-02-14T18:49:39.505685Z","iopub.execute_input":"2022-02-14T18:49:39.505880Z","iopub.status.idle":"2022-02-14T18:49:39.793771Z","shell.execute_reply.started":"2022-02-14T18:49:39.505856Z","shell.execute_reply":"2022-02-14T18:49:39.793074Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_data = pd.read_csv(\"../input/aptos2019-blindness-detection/test.csv\")\ntest_data[\"filename\"] = test_data[\"id_code\"].map(lambda x:x+\".png\")\ntest_data.head()","metadata":{"execution":{"iopub.status.busy":"2022-02-14T18:49:39.795091Z","iopub.execute_input":"2022-02-14T18:49:39.795499Z","iopub.status.idle":"2022-02-14T18:49:39.813804Z","shell.execute_reply.started":"2022-02-14T18:49:39.795461Z","shell.execute_reply":"2022-02-14T18:49:39.813110Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.preprocessing.image import ImageDataGenerator\n\ntest_gen = ImageDataGenerator()\ntest_generator = test_gen.flow_from_dataframe(  \n        dataframe=test_data,\n        directory = \"../input/aptos2019-blindness-detection/test_images\",    \n        x_col=\"filename\",\n        y_col=None,\n        target_size = (IMG_SIZE,IMG_SIZE),\n        batch_size = 16,\n        shuffle = False,\n        class_mode = None\n        )","metadata":{"execution":{"iopub.status.busy":"2022-02-14T18:53:25.114120Z","iopub.execute_input":"2022-02-14T18:53:25.114387Z","iopub.status.idle":"2022-02-14T18:53:25.952878Z","shell.execute_reply.started":"2022-02-14T18:53:25.114359Z","shell.execute_reply":"2022-02-14T18:53:25.952110Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions = model.predict_generator(test_generator, steps = len(test_generator.filenames))","metadata":{"execution":{"iopub.status.busy":"2022-02-14T18:53:28.363992Z","iopub.execute_input":"2022-02-14T18:53:28.364579Z","iopub.status.idle":"2022-02-14T18:54:37.856153Z","shell.execute_reply.started":"2022-02-14T18:53:28.364539Z","shell.execute_reply":"2022-02-14T18:54:37.855392Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"filenames=test_generator.filenames\nresults=pd.DataFrame({\"id_code\":filenames,\n                      \"diagnosis\":np.argmax(predictions,axis=1)})\nresults['id_code'] = results['id_code'].map(lambda x: str(x)[:-4])\nresults.to_csv(\"submission.csv\",index=False)","metadata":{"execution":{"iopub.status.busy":"2022-02-14T18:54:37.859825Z","iopub.execute_input":"2022-02-14T18:54:37.860026Z","iopub.status.idle":"2022-02-14T18:54:37.872483Z","shell.execute_reply.started":"2022-02-14T18:54:37.860001Z","shell.execute_reply":"2022-02-14T18:54:37.871781Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"results.diagnosis.hist()","metadata":{"execution":{"iopub.status.busy":"2022-02-14T18:54:37.873742Z","iopub.execute_input":"2022-02-14T18:54:37.874063Z","iopub.status.idle":"2022-02-14T18:54:38.098486Z","shell.execute_reply.started":"2022-02-14T18:54:37.874013Z","shell.execute_reply":"2022-02-14T18:54:38.097795Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}