{"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 pandas as pd\nimport tensorflow as tf\nimport albumentations as A","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-05-28T16:37:23.866900Z","iopub.execute_input":"2021-05-28T16:37:23.867278Z","iopub.status.idle":"2021-05-28T16:37:30.364076Z","shell.execute_reply.started":"2021-05-28T16:37:23.867198Z","shell.execute_reply":"2021-05-28T16:37:30.363344Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.read_csv('../input/aptos2019-blindness-detection/sample_submission.csv')","metadata":{"execution":{"iopub.status.busy":"2021-05-22T17:43:34.091255Z","iopub.execute_input":"2021-05-22T17:43:34.091656Z","iopub.status.idle":"2021-05-22T17:43:34.111476Z","shell.execute_reply.started":"2021-05-22T17:43:34.091621Z","shell.execute_reply":"2021-05-22T17:43:34.110675Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"t = pd.read_csv('../input/aptos2019-blindness-detection/test.csv')","metadata":{"execution":{"iopub.status.busy":"2021-05-22T17:43:35.919591Z","iopub.execute_input":"2021-05-22T17:43:35.919919Z","iopub.status.idle":"2021-05-22T17:43:35.9365Z","shell.execute_reply.started":"2021-05-22T17:43:35.919871Z","shell.execute_reply":"2021-05-22T17:43:35.935732Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"t[t.columns[0]]+='.png'","metadata":{"execution":{"iopub.status.busy":"2021-05-22T17:43:37.330112Z","iopub.execute_input":"2021-05-22T17:43:37.330436Z","iopub.status.idle":"2021-05-22T17:43:37.354384Z","shell.execute_reply.started":"2021-05-22T17:43:37.330406Z","shell.execute_reply":"2021-05-22T17:43:37.353439Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"t.head()","metadata":{"execution":{"iopub.status.busy":"2021-05-22T17:43:39.024723Z","iopub.execute_input":"2021-05-22T17:43:39.02506Z","iopub.status.idle":"2021-05-22T17:43:39.041015Z","shell.execute_reply.started":"2021-05-22T17:43:39.02503Z","shell.execute_reply":"2021-05-22T17:43:39.039878Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"transform = A.Compose([\n    A.Blur(p=0.5,),\n    A.Flip(p=0.5),\n    A.RandomBrightnessContrast(p=0.1,brightness_limit=1,contrast_limit=1,brightness_by_max=False),\n    A.ShiftScaleRotate(shift_limit=0.0625, scale_limit=(0,0.350), rotate_limit=45, p=0.6),\n    A.ElasticTransform(p= 0.3,),\n    A.GridDistortion(p = 0.3,distort_limit=0.25,interpolation=cv2.INTER_AREA),\n    A.HueSaturationValue(p = 0.3,hue_shift_limit=5,sat_shift_limit=6,val_shift_limit=5),\n    A.CLAHE(p=0.7,),\n    A.CoarseDropout(p = 0.4)\n])","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.preprocessing.image import ImageDataGenerator","metadata":{"execution":{"iopub.status.busy":"2021-05-22T17:43:40.489617Z","iopub.execute_input":"2021-05-22T17:43:40.489952Z","iopub.status.idle":"2021-05-22T17:43:40.493611Z","shell.execute_reply.started":"2021-05-22T17:43:40.489922Z","shell.execute_reply":"2021-05-22T17:43:40.492783Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"gen = ImageDataGenerator(rotation_range=360,\n                               brightness_range=(0,20.0),\n                               shear_range=0.2,\n                               horizontal_flip=True,\n                               vertical_flip=True,)","metadata":{"execution":{"iopub.status.busy":"2021-05-22T17:43:42.310429Z","iopub.execute_input":"2021-05-22T17:43:42.310741Z","iopub.status.idle":"2021-05-22T17:43:42.316758Z","shell.execute_reply.started":"2021-05-22T17:43:42.310711Z","shell.execute_reply":"2021-05-22T17:43:42.315671Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"path = '../input/aptos2019-blindness-detection/test_images'\n\ntest_gen= gen.flow_from_dataframe(t,directory = path,\n                                  x_col = t.columns[0],\n                                  y_col=None,\n                                  batch_size=8,\n                                  seed=42,\n                                  shuffle=False,\n                                  class_mode=None,\n                                  target_size=(512,512))","metadata":{"execution":{"iopub.status.busy":"2021-05-22T17:43:44.914878Z","iopub.execute_input":"2021-05-22T17:43:44.91533Z","iopub.status.idle":"2021-05-22T17:43:46.863812Z","shell.execute_reply.started":"2021-05-22T17:43:44.915291Z","shell.execute_reply":"2021-05-22T17:43:46.862948Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"from tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import InputLayer, BatchNormalization, Dropout, Flatten, Dense, Activation, MaxPool2D, Conv2D, GlobalAveragePooling2D","metadata":{"execution":{"iopub.status.busy":"2021-05-22T13:41:03.766646Z","iopub.execute_input":"2021-05-22T13:41:03.766958Z","iopub.status.idle":"2021-05-22T13:41:03.772521Z","shell.execute_reply.started":"2021-05-22T13:41:03.766929Z","shell.execute_reply":"2021-05-22T13:41:03.771639Z"}}},{"cell_type":"markdown","source":"model=Sequential()\nmodel.add(tf.keras.applications.ResNet152V2(include_top=False,weights=None))\nmodel.add(GlobalAveragePooling2D())\nmodel.add(Dropout(0.5))\nmodel.add(Flatten())\nmodel.add(BatchNormalization())\nmodel.add(Dense(256,kernel_initializer='he_uniform'))\nmodel.add(BatchNormalization())\nmodel.add(Activation('relu'))\nmodel.add(Dropout(0.5))\nmodel.add(Dense(128,kernel_initializer='he_uniform'))\nmodel.add(BatchNormalization())\nmodel.add(Activation('relu'))\nmodel.add(Dropout(0.5))\nmodel.add(Dense(32,kernel_initializer='he_uniform'))\nmodel.add(BatchNormalization())\nmodel.add(Activation('relu'))\nmodel.add(Dense(5,activation='softmax'))","metadata":{"execution":{"iopub.status.busy":"2021-05-22T13:42:13.854716Z","iopub.execute_input":"2021-05-22T13:42:13.855033Z","iopub.status.idle":"2021-05-22T13:42:17.745159Z","shell.execute_reply.started":"2021-05-22T13:42:13.855005Z","shell.execute_reply":"2021-05-22T13:42:17.744375Z"}}},{"cell_type":"code","source":"model1 = tf.keras.models.load_model('../input/new-model/b5_retrained.h5')","metadata":{"execution":{"iopub.status.busy":"2021-05-22T17:44:48.686057Z","iopub.execute_input":"2021-05-22T17:44:48.686371Z","iopub.status.idle":"2021-05-22T17:44:59.887096Z","shell.execute_reply.started":"2021-05-22T17:44:48.686341Z","shell.execute_reply":"2021-05-22T17:44:59.886294Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred = model1.predict(test_gen)","metadata":{"execution":{"iopub.status.busy":"2021-05-22T17:45:54.252127Z","iopub.execute_input":"2021-05-22T17:45:54.252436Z","iopub.status.idle":"2021-05-22T17:49:50.122273Z","shell.execute_reply.started":"2021-05-22T17:45:54.252407Z","shell.execute_reply":"2021-05-22T17:49:50.121263Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred","metadata":{"execution":{"iopub.status.busy":"2021-05-22T18:08:15.999617Z","iopub.execute_input":"2021-05-22T18:08:15.999965Z","iopub.status.idle":"2021-05-22T18:08:16.007112Z","shell.execute_reply.started":"2021-05-22T18:08:15.999933Z","shell.execute_reply":"2021-05-22T18:08:16.006243Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def fn(i):\n    if i<0.7:\n        return 0\n    elif 0.7<=i<1.5:\n        return 1\n    elif 1.5<=i<2.5:\n        return 2\n    elif 2.5<=i<3.5:\n        return 3\n    return 4","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_ind(l):\n    ans = 0\n    for i ,v in enumerate(l):\n        if l[i]>l[ans]:\n            ans = i\n    return ans   ","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np","metadata":{"execution":{"iopub.status.busy":"2021-05-22T18:09:21.569551Z","iopub.execute_input":"2021-05-22T18:09:21.569874Z","iopub.status.idle":"2021-05-22T18:09:21.573614Z","shell.execute_reply.started":"2021-05-22T18:09:21.569844Z","shell.execute_reply":"2021-05-22T18:09:21.572898Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ans = [np.argmax(p) for p in pred]","metadata":{"execution":{"iopub.status.busy":"2021-05-22T18:11:43.394641Z","iopub.execute_input":"2021-05-22T18:11:43.394988Z","iopub.status.idle":"2021-05-22T18:11:43.412896Z","shell.execute_reply.started":"2021-05-22T18:11:43.394947Z","shell.execute_reply":"2021-05-22T18:11:43.4117Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#ans = [fn(i) for i in pred]\nans","metadata":{"execution":{"iopub.status.busy":"2021-05-22T18:12:10.899129Z","iopub.execute_input":"2021-05-22T18:12:10.899445Z","iopub.status.idle":"2021-05-22T18:12:10.922415Z","shell.execute_reply.started":"2021-05-22T18:12:10.899416Z","shell.execute_reply":"2021-05-22T18:12:10.921442Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.diagnosis = ans","metadata":{"execution":{"iopub.status.busy":"2021-05-22T18:12:24.653873Z","iopub.execute_input":"2021-05-22T18:12:24.654227Z","iopub.status.idle":"2021-05-22T18:12:24.66297Z","shell.execute_reply.started":"2021-05-22T18:12:24.654198Z","shell.execute_reply":"2021-05-22T18:12:24.662116Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.to_csv(\"./submission.csv\", index=False)","metadata":{"execution":{"iopub.status.busy":"2021-05-22T18:12:27.989122Z","iopub.execute_input":"2021-05-22T18:12:27.989433Z","iopub.status.idle":"2021-05-22T18:12:28.159655Z","shell.execute_reply.started":"2021-05-22T18:12:27.989403Z","shell.execute_reply":"2021-05-22T18:12:28.158931Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}