{"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 numpy as np\nimport tensorflow as tf\nimport cv2\nimport matplotlib.pyplot as plt\nimport os\nimport multiprocessing\nfrom multiprocessing.pool import ThreadPool\nimport albumentations as A","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-05-27T12:30:08.358324Z","iopub.execute_input":"2021-05-27T12:30:08.358773Z","iopub.status.idle":"2021-05-27T12:30:11.367789Z","shell.execute_reply.started":"2021-05-27T12:30:08.358655Z","shell.execute_reply":"2021-05-27T12:30:11.366783Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.read_csv('../input/aptos2019-blindness-detection/train.csv')","metadata":{"execution":{"iopub.status.busy":"2021-05-27T12:30:15.122677Z","iopub.execute_input":"2021-05-27T12:30:15.123123Z","iopub.status.idle":"2021-05-27T12:30:15.138323Z","shell.execute_reply.started":"2021-05-27T12:30:15.123090Z","shell.execute_reply":"2021-05-27T12:30:15.137197Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['id_code']+='.png'","metadata":{"execution":{"iopub.status.busy":"2021-05-27T12:30:16.477501Z","iopub.execute_input":"2021-05-27T12:30:16.477944Z","iopub.status.idle":"2021-05-27T12:30:16.490597Z","shell.execute_reply.started":"2021-05-27T12:30:16.477896Z","shell.execute_reply":"2021-05-27T12:30:16.489568Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['diagnosis'] = df['diagnosis'].map(str)","metadata":{"execution":{"iopub.status.busy":"2021-05-27T12:30:17.742784Z","iopub.execute_input":"2021-05-27T12:30:17.743131Z","iopub.status.idle":"2021-05-27T12:30:17.749624Z","shell.execute_reply.started":"2021-05-27T12:30:17.743102Z","shell.execute_reply":"2021-05-27T12:30:17.748886Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.head()","metadata":{"execution":{"iopub.status.busy":"2021-05-27T12:30:19.377601Z","iopub.execute_input":"2021-05-27T12:30:19.378191Z","iopub.status.idle":"2021-05-27T12:30:19.396912Z","shell.execute_reply.started":"2021-05-27T12:30:19.378154Z","shell.execute_reply":"2021-05-27T12:30:19.395295Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['diagnosis'].dtype","metadata":{"execution":{"iopub.status.busy":"2021-05-27T12:30:21.038151Z","iopub.execute_input":"2021-05-27T12:30:21.038557Z","iopub.status.idle":"2021-05-27T12:30:21.045576Z","shell.execute_reply.started":"2021-05-27T12:30:21.038524Z","shell.execute_reply":"2021-05-27T12:30:21.044034Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#augmentations\ntransform = 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=1,),\n    A.CoarseDropout(p = 0.4)\n])","metadata":{"execution":{"iopub.status.busy":"2021-05-27T12:30:22.592894Z","iopub.execute_input":"2021-05-27T12:30:22.593306Z","iopub.status.idle":"2021-05-27T12:30:22.600533Z","shell.execute_reply.started":"2021-05-27T12:30:22.593270Z","shell.execute_reply":"2021-05-27T12:30:22.599714Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#custom generator attempt\nclass DataGenerator(tf.keras.utils.Sequence):\n    def __init__(self, df, directory, x_col, y_col=None, batch_size=32, num_classes=None,target_size = (256,256),preprocess = None, shuffle=True):\n        self.batch_size = batch_size\n        self.df = df\n        self.directory = directory\n        self.indices = self.df.index.tolist()\n        self.num_classes = num_classes\n        self.shuffle = shuffle\n        self.x_col = x_col\n        self.y_col = y_col\n        self.target_size = target_size\n        self.preprocess = preprocess\n        self.on_epoch_end()\n\n    def __len__(self):\n        return len(self.indices) // self.batch_size\n\n    def __getitem__(self, index):\n        index = self.index[index * self.batch_size:(index + 1) * self.batch_size]\n        batch = [self.indices[k] for k in index]\n        \n        X, y = self.__get_data(batch)\n        return X, y\n\n    def on_epoch_end(self):\n        self.index = np.arange(len(self.indices))\n        if self.shuffle == True:\n            np.random.shuffle(self.index)\n\n    def __get_data(self, batch):\n        X = []# logic\n        y = []# logic\n        \n        for i, id in enumerate(batch):\n            a,b = self.df.loc[id][0], self.df.loc[id][1]\n            img = self.preprocess(plt.imread(directory+'/'+a))\n            \n            X.append(img) # logic\n            y.append(b)  # labels\n\n        return X, y","metadata":{"execution":{"iopub.status.busy":"2021-05-27T12:43:27.893886Z","iopub.execute_input":"2021-05-27T12:43:27.894248Z","iopub.status.idle":"2021-05-27T12:43:27.908389Z","shell.execute_reply.started":"2021-05-27T12:43:27.894215Z","shell.execute_reply":"2021-05-27T12:43:27.906718Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"batch_size = 10\nind = [i for _ in range(101)]\ndef __getitem__(index):\n        \n        indx = ind[index * batch_size:(index + 1) * batch_size]\n        batch = [ind[k] for k in indx]\n        \n        X, y = __get_data(batch)\n        yield X, y\n        \ndef __get_data(batch):\n        X = []# logic\n        y = []# logic\n        \n        for i, id in enumerate(batch):\n            #a,b = self.df.loc[id][0], self.df.loc[id][1]\n            #img = preprocess(plt.imread(directory+'/'+a))\n            \n            X.append(Xx[id]) # logic\n            y.append(yy[id])  # labels\n\n        return X, y","metadata":{"execution":{"iopub.status.busy":"2021-05-27T12:31:01.183867Z","iopub.execute_input":"2021-05-27T12:31:01.184320Z","iopub.status.idle":"2021-05-27T12:31:01.192072Z","shell.execute_reply.started":"2021-05-27T12:31:01.184277Z","shell.execute_reply":"2021-05-27T12:31:01.190824Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Xx,yy = np.random.randint(low=1, high = 1000,size = 101), np.random.randint(low=1, high = 1000,size  = 101)\n","metadata":{"execution":{"iopub.status.busy":"2021-05-27T12:31:03.507609Z","iopub.execute_input":"2021-05-27T12:31:03.508021Z","iopub.status.idle":"2021-05-27T12:31:03.516290Z","shell.execute_reply.started":"2021-05-27T12:31:03.507982Z","shell.execute_reply":"2021-05-27T12:31:03.514045Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def fn(image):\n    return image","metadata":{"execution":{"iopub.status.busy":"2021-05-27T12:38:03.199147Z","iopub.execute_input":"2021-05-27T12:38:03.199643Z","iopub.status.idle":"2021-05-27T12:38:03.206722Z","shell.execute_reply.started":"2021-05-27T12:38:03.199604Z","shell.execute_reply":"2021-05-27T12:38:03.205529Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"directory = '../input/project1/1'\ngen = DataGenerator(df = df, directory = directory,\n                   x_col = 'id_code', y_col = 'diagnosis',\n                   num_classes = 5,\n                   preprocess = preprocess_fn)","metadata":{"execution":{"iopub.status.busy":"2021-05-27T12:46:15.033127Z","iopub.execute_input":"2021-05-27T12:46:15.033501Z","iopub.status.idle":"2021-05-27T12:46:15.039733Z","shell.execute_reply.started":"2021-05-27T12:46:15.033469Z","shell.execute_reply":"2021-05-27T12:46:15.037923Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x, y =  gen.__getitem__(0)","metadata":{"execution":{"iopub.status.busy":"2021-05-27T12:46:16.743277Z","iopub.execute_input":"2021-05-27T12:46:16.743835Z","iopub.status.idle":"2021-05-27T12:46:22.773711Z","shell.execute_reply.started":"2021-05-27T12:46:16.743785Z","shell.execute_reply":"2021-05-27T12:46:22.772651Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(y)","metadata":{"execution":{"iopub.status.busy":"2021-05-27T12:46:01.843098Z","iopub.execute_input":"2021-05-27T12:46:01.843494Z","iopub.status.idle":"2021-05-27T12:46:01.850547Z","shell.execute_reply.started":"2021-05-27T12:46:01.843454Z","shell.execute_reply":"2021-05-27T12:46:01.849291Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(x[0])","metadata":{"execution":{"iopub.status.busy":"2021-05-27T12:46:29.799444Z","iopub.execute_input":"2021-05-27T12:46:29.800227Z","iopub.status.idle":"2021-05-27T12:46:30.026834Z","shell.execute_reply.started":"2021-05-27T12:46:29.800181Z","shell.execute_reply":"2021-05-27T12:46:30.025577Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"BATCH_SIZE = 8\nEPOCHS = 40\nWARMUP_EPOCHS = 2\nLEARNING_RATE = 1e-6\nWARMUP_LEARNING_RATE = 4e-5\nHEIGHT = 320\nWIDTH = 320\nCANAL = 3\nN_CLASSES = 5\nES_PATIENCE = 5\nRLROP_PATIENCE = 3\nDECAY_DROP = 0.5","metadata":{"execution":{"iopub.status.busy":"2021-05-27T12:47:27.384242Z","iopub.execute_input":"2021-05-27T12:47:27.384854Z","iopub.status.idle":"2021-05-27T12:47:27.389639Z","shell.execute_reply.started":"2021-05-27T12:47:27.384814Z","shell.execute_reply":"2021-05-27T12:47:27.388791Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#preprocessing function use with ImageDataGenerator\ndef preprocess_fn(image):\n    image = transform(image = (image*255).astype('uint8'))['image']\n    return image/255","metadata":{"execution":{"iopub.status.busy":"2021-05-27T12:45:32.354282Z","iopub.execute_input":"2021-05-27T12:45:32.354657Z","iopub.status.idle":"2021-05-27T12:45:32.360463Z","shell.execute_reply.started":"2021-05-27T12:45:32.354625Z","shell.execute_reply":"2021-05-27T12:45:32.359207Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.preprocessing.image import ImageDataGenerator","metadata":{"execution":{"iopub.status.busy":"2021-05-27T03:52:47.987337Z","iopub.execute_input":"2021-05-27T03:52:47.98777Z","iopub.status.idle":"2021-05-27T03:52:48.070393Z","shell.execute_reply.started":"2021-05-27T03:52:47.987729Z","shell.execute_reply":"2021-05-27T03:52:48.069173Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"gen = ImageDataGenerator(preprocessing_function=preprocess_fn,\n                        validation_split=0.05,)","metadata":{"execution":{"iopub.status.busy":"2021-05-27T03:53:00.523542Z","iopub.execute_input":"2021-05-27T03:53:00.524049Z","iopub.status.idle":"2021-05-27T03:53:00.527708Z","shell.execute_reply.started":"2021-05-27T03:53:00.524016Z","shell.execute_reply":"2021-05-27T03:53:00.526992Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_generator= gen.flow_from_dataframe(dataframe=df,\n                                                      directory=\"../input/project1/1\",\n                                                      x_col=\"id_code\",\n                                                      y_col=\"diagnosis\",\n                                                      batch_size=BATCH_SIZE,\n                                                      class_mode=\"categorical\",\n                                                      target_size=(512, 512),\n                                                      subset='training')\n\nvalid_generator=gen.flow_from_dataframe(dataframe=df,\n                                                      directory=\"../input/project1/1\",\n                                                      x_col=\"id_code\",\n                                                      y_col=\"diagnosis\",\n                                                      batch_size=BATCH_SIZE,\n                                                      class_mode=\"categorical\",    \n                                                      target_size=(512, 512),\n                                                      subset='validation')\n    ","metadata":{"execution":{"iopub.status.busy":"2021-05-27T04:00:09.007351Z","iopub.execute_input":"2021-05-27T04:00:09.007904Z","iopub.status.idle":"2021-05-27T04:00:21.170589Z","shell.execute_reply.started":"2021-05-27T04:00:09.007858Z","shell.execute_reply":"2021-05-27T04:00:21.169548Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img = plt.imread('../input/project1/1/000c1434d8d7.png')","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(img)#original image","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nfor i in range(4):\n    t = gen.random_transform(img)        #images after random transformation from generator\n    x = preprocess_fn(img)               #images after random transformation from preprocess_fn\n    plt.imshow(t)                        #yeh wali images original images jaise he reh jaati hai\n    plt.show()\n    plt.imshow(x)\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2021-05-27T04:55:26.127543Z","iopub.execute_input":"2021-05-27T04:55:26.127936Z","iopub.status.idle":"2021-05-27T04:55:28.549024Z","shell.execute_reply.started":"2021-05-27T04:55:26.127902Z","shell.execute_reply":"2021-05-27T04:55:28.547988Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"imgx = preprocess_fn(plt.imread('../input/project1/1/000c1434d8d7.png'))","metadata":{"execution":{"iopub.status.busy":"2021-05-27T04:03:49.307641Z","iopub.execute_input":"2021-05-27T04:03:49.308047Z","iopub.status.idle":"2021-05-27T04:03:49.667064Z","shell.execute_reply.started":"2021-05-27T04:03:49.308015Z","shell.execute_reply":"2021-05-27T04:03:49.666201Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(img)\nplt.imshow(t)\nplt.imshow(imgx)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-05-27T04:48:36.270991Z","iopub.execute_input":"2021-05-27T04:48:36.271364Z","iopub.status.idle":"2021-05-27T04:48:36.604515Z","shell.execute_reply.started":"2021-05-27T04:48:36.271334Z","shell.execute_reply":"2021-05-27T04:48:36.603396Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = tf.keras.models.load_model('../input/model-file/ATOPS_dr_EffnetB5(R2).h5')","metadata":{"execution":{"iopub.status.busy":"2021-05-27T12:47:11.582646Z","iopub.execute_input":"2021-05-27T12:47:11.583062Z","iopub.status.idle":"2021-05-27T12:47:21.020376Z","shell.execute_reply.started":"2021-05-27T12:47:11.583025Z","shell.execute_reply":"2021-05-27T12:47:21.019306Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2021-05-27T12:47:32.112540Z","iopub.execute_input":"2021-05-27T12:47:32.113071Z","iopub.status.idle":"2021-05-27T12:47:32.153084Z","shell.execute_reply.started":"2021-05-27T12:47:32.113037Z","shell.execute_reply":"2021-05-27T12:47:32.152297Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"execution":{"iopub.status.busy":"2021-05-25T08:11:01.435988Z","iopub.execute_input":"2021-05-25T08:11:01.43639Z","iopub.status.idle":"2021-05-25T08:11:01.445761Z","shell.execute_reply.started":"2021-05-25T08:11:01.43635Z","shell.execute_reply":"2021-05-25T08:11:01.444491Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(optimizer = tf.keras.optimizers.Adam(lr=WARMUP_LEARNING_RATE),loss = 'categorical_crossentropy',metrics = ['accuracy'])\n","metadata":{"execution":{"iopub.status.busy":"2021-05-27T12:47:54.993266Z","iopub.execute_input":"2021-05-27T12:47:54.993657Z","iopub.status.idle":"2021-05-27T12:47:55.016658Z","shell.execute_reply.started":"2021-05-27T12:47:54.993619Z","shell.execute_reply":"2021-05-27T12:47:55.015554Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.fit(gen.__getitem__,epochs = 1)","metadata":{"execution":{"iopub.status.busy":"2021-05-27T12:49:01.513317Z","iopub.execute_input":"2021-05-27T12:49:01.513747Z","iopub.status.idle":"2021-05-27T12:49:01.559813Z","shell.execute_reply.started":"2021-05-27T12:49:01.513706Z","shell.execute_reply":"2021-05-27T12:49:01.558193Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"es = EarlyStopping(monitor='val_loss', mode='min', patience=ES_PATIENCE, restore_best_weights=True, verbose=1)\nrlrop = ReduceLROnPlateau(monitor='val_loss', mode='min', patience=RLROP_PATIENCE, factor=DECAY_DROP, min_lr=1e-6, verbose=1)\nckpt = tf.keras.callbacks.ModelCheckpoint('res_train.h5')\ncallback_list = [es, rlrop]\noptimizer = optimizers.Adam(lr=LEARNING_RATE)\nmodel.compile(optimizer=optimizer, loss=\"binary_crossentropy\",  metrics=['accuracy'])","metadata":{},"execution_count":null,"outputs":[]}]}