{"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 os\nprint(os.listdir(\"../input\"))\n\nimport keras\n\nfrom keras.preprocessing.image import ImageDataGenerator\nfrom keras.models import Sequential, Model, load_model\nfrom keras.layers import (Activation, Dropout, Flatten, Dense, GlobalMaxPooling2D,\n                          BatchNormalization, Input, Conv2D, GlobalAveragePooling2D,concatenate,Concatenate,multiply, LocallyConnected2D, Lambda)\nfrom keras.callbacks import ModelCheckpoint\nfrom keras import metrics\nfrom keras.optimizers import Adam \nfrom keras import backend as K\nfrom keras.losses import binary_crossentropy, categorical_crossentropy\nfrom keras.utils import Sequence, to_categorical\nfrom keras.applications.inception_resnet_v2 import InceptionResNetV2, preprocess_input\n\nimport matplotlib.pyplot as plt\nimport PIL\nimport cv2\nimport tensorflow as tf\nimport pandas as pd\nimport imgaug as ia\nimport numpy as np\n\nfrom imgaug import augmenters as iaa\n\nfrom PIL import Image, ImageOps\nfrom sklearn.utils import class_weight, shuffle\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.metrics import f1_score, fbeta_score, cohen_kappa_score\n\n%config InlineBackend.figure_format=\"svg\"\n%matplotlib inline","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-10-11T07:47:03.396093Z","iopub.execute_input":"2022-10-11T07:47:03.396484Z","iopub.status.idle":"2022-10-11T07:47:03.437061Z","shell.execute_reply.started":"2022-10-11T07:47:03.396415Z","shell.execute_reply":"2022-10-11T07:47:03.435930Z"},"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')","metadata":{"execution":{"iopub.status.busy":"2022-10-11T07:47:03.440799Z","iopub.execute_input":"2022-10-11T07:47:03.441373Z","iopub.status.idle":"2022-10-11T07:47:03.459801Z","shell.execute_reply.started":"2022-10-11T07:47:03.441211Z","shell.execute_reply":"2022-10-11T07:47:03.458823Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import warnings\nwarnings.filterwarnings(\"ignore\")\nSIZE = 300\nNUM_CLASSES = 5\nWORKERS=2","metadata":{"execution":{"iopub.status.busy":"2022-10-11T07:47:03.461621Z","iopub.execute_input":"2022-10-11T07:47:03.462088Z","iopub.status.idle":"2022-10-11T07:47:03.467230Z","shell.execute_reply.started":"2022-10-11T07:47:03.461914Z","shell.execute_reply":"2022-10-11T07:47:03.466114Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x = df_train['id_code']\ny = df_train['diagnosis']\n\nx, y = shuffle(x, y, random_state=8)\ny.hist()","metadata":{"execution":{"iopub.status.busy":"2022-10-11T07:47:03.468759Z","iopub.execute_input":"2022-10-11T07:47:03.469233Z","iopub.status.idle":"2022-10-11T07:47:03.839798Z","shell.execute_reply.started":"2022-10-11T07:47:03.469016Z","shell.execute_reply":"2022-10-11T07:47:03.838721Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sometimes = lambda aug: iaa.Sometimes(0.5, aug)\nseq = iaa.Sequential(\n        [\n            # apply the following augmenters to most images\n            iaa.Fliplr(0.5), # horizontally flip 50% of all images\n            iaa.Flipud(0.2), # vertically flip 20% of all images\n            sometimes(iaa.Affine(\n                scale={\"x\": (0.9, 1.1), \"y\": (0.9, 1.1)}, # scale images to 80-120% of their size, individually per axis\n                translate_percent={\"x\": (-0.1, 0.1), \"y\": (-0.1, 0.1)}, # translate by -20 to +20 percent (per axis)\n                rotate=(-10, 10), # rotate by -45 to +45 degrees\n                shear=(-5, 5), # shear by -16 to +16 degrees\n                order=[0, 1], # use nearest neighbour or bilinear interpolation (fast)\n                cval=(0, 255), # if mode is constant, use a cval between 0 and 255\n                mode=ia.ALL # use any of scikit-image's warping modes (see 2nd image from the top for examples)\n            )),\n            # execute 0 to 5 of the following (less important) augmenters per image\n            # don't execute all of them, as that would often be way too strong\n            iaa.SomeOf((0, 5),\n                [\n                    sometimes(iaa.Superpixels(p_replace=(0, 1.0), n_segments=(20, 200))), # convert images into their superpixel representation\n                    iaa.OneOf([\n                        iaa.GaussianBlur((0, 1.0)), # blur images with a sigma between 0 and 3.0\n                        iaa.AverageBlur(k=(3, 5)), # blur image using local means with kernel sizes between 2 and 7\n                        iaa.MedianBlur(k=(3, 5)), # blur image using local medians with kernel sizes between 2 and 7\n                    ]),\n                    iaa.Sharpen(alpha=(0, 1.0), lightness=(0.9, 1.1)), # sharpen images\n                    iaa.Emboss(alpha=(0, 1.0), strength=(0, 2.0)), # emboss images\n                    # search either for all edges or for directed edges,\n                    # blend the result with the original image using a blobby mask\n                    iaa.SimplexNoiseAlpha(iaa.OneOf([\n                        iaa.EdgeDetect(alpha=(0.5, 1.0)),\n                        iaa.DirectedEdgeDetect(alpha=(0.5, 1.0), direction=(0.0, 1.0)),\n                    ])),\n                    iaa.AdditiveGaussianNoise(loc=0, scale=(0.0, 0.01*255), per_channel=0.5), # add gaussian noise to images\n                    iaa.OneOf([\n                        iaa.Dropout((0.01, 0.05), per_channel=0.5), # randomly remove up to 10% of the pixels\n                        iaa.CoarseDropout((0.01, 0.03), size_percent=(0.01, 0.02), per_channel=0.2),\n                    ]),\n                    iaa.Invert(0.01, per_channel=True), # invert color channels\n                    iaa.Add((-2, 2), per_channel=0.5), # change brightness of images (by -10 to 10 of original value)\n                    iaa.AddToHueAndSaturation((-1, 1)), # change hue and saturation\n                    # either change the brightness of the whole image (sometimes\n                    # per channel) or change the brightness of subareas\n                    iaa.OneOf([\n                        iaa.Multiply((0.9, 1.1), per_channel=0.5),\n                        iaa.FrequencyNoiseAlpha(\n                            exponent=(-1, 0),\n                            first=iaa.Multiply((0.9, 1.1), per_channel=True),\n                            second=iaa.ContrastNormalization((0.9, 1.1))\n                        )\n                    ]),\n                    sometimes(iaa.ElasticTransformation(alpha=(0.5, 3.5), sigma=0.25)), # move pixels locally around (with random strengths)\n                    sometimes(iaa.PiecewiseAffine(scale=(0.01, 0.05))), # sometimes move parts of the image around\n                    sometimes(iaa.PerspectiveTransform(scale=(0.01, 0.1)))\n                ],\n                random_order=True\n            )\n        ],\n        random_order=True)\n","metadata":{"execution":{"iopub.status.busy":"2022-10-11T07:47:03.841716Z","iopub.execute_input":"2022-10-11T07:47:03.842429Z","iopub.status.idle":"2022-10-11T07:47:03.882851Z","shell.execute_reply.started":"2022-10-11T07:47:03.842318Z","shell.execute_reply":"2022-10-11T07:47:03.881765Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class My_Generator(Sequence):\n\n    def __init__(self, image_filenames, labels,\n                 batch_size, is_train=True,\n                 mix=False, augment=False):\n        self.image_filenames, self.labels = image_filenames, labels\n        self.batch_size = batch_size\n        self.is_train = is_train\n        self.is_augment = augment\n        if(self.is_train):\n            self.on_epoch_end()\n        self.is_mix = mix\n\n    def __len__(self):\n        return int(np.ceil(len(self.image_filenames) / float(self.batch_size)))\n\n    def __getitem__(self, idx):\n        batch_x = self.image_filenames[idx * self.batch_size:(idx + 1) * self.batch_size]\n        batch_y = self.labels[idx * self.batch_size:(idx + 1) * self.batch_size]\n\n        if(self.is_train):\n            return self.train_generate(batch_x, batch_y)\n        return self.valid_generate(batch_x, batch_y)\n\n    def on_epoch_end(self):\n        if(self.is_train):\n            self.image_filenames, self.labels = shuffle(self.image_filenames, self.labels)\n        else:\n            pass\n    \n    def mix_up(self, x, y):\n        lam = np.random.beta(0.2, 0.4)\n        ori_index = np.arange(int(len(x)))\n        index_array = np.arange(int(len(x)))\n        np.random.shuffle(index_array)        \n        \n        mixed_x = lam * x[ori_index] + (1 - lam) * x[index_array]\n        mixed_y = lam * y[ori_index] + (1 - lam) * y[index_array]\n        \n        return mixed_x, mixed_y\n\n    def train_generate(self, batch_x, batch_y):\n        batch_images = []\n        for (sample, label) in zip(batch_x, batch_y):\n            img = cv2.imread('../input/aptos2019-blindness-detection/train_images/'+sample+'.png')\n            img = cv2.resize(img, (SIZE, SIZE))\n            if(self.is_augment):\n                img = seq.augment_image(img)\n            batch_images.append(img)\n        batch_images = np.array(batch_images, np.float32) / 255\n        batch_y = np.array(batch_y, np.float32)\n        if(self.is_mix):\n            batch_images, batch_y = self.mix_up(batch_images, batch_y)\n        return batch_images, batch_y\n\n    def valid_generate(self, batch_x, batch_y):\n        batch_images = []\n        for (sample, label) in zip(batch_x, batch_y):\n            img = cv2.imread('../input/aptos2019-blindness-detection/train_images/'+sample+'.png')\n            img = cv2.resize(img, (SIZE, SIZE))\n            batch_images.append(img)\n        batch_images = np.array(batch_images, np.float32) / 255\n        batch_y = np.array(batch_y, np.float32)\n        return batch_images, batch_y","metadata":{"execution":{"iopub.status.busy":"2022-10-11T07:47:03.890140Z","iopub.execute_input":"2022-10-11T07:47:03.890777Z","iopub.status.idle":"2022-10-11T07:47:03.917123Z","shell.execute_reply.started":"2022-10-11T07:47:03.890484Z","shell.execute_reply":"2022-10-11T07:47:03.915879Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y = to_categorical(y, num_classes=NUM_CLASSES)\ntrain_x, valid_x, train_y, valid_y = train_test_split(x, y, test_size=0.15,\n                                                      stratify=y, random_state=8)","metadata":{"execution":{"iopub.status.busy":"2022-10-11T07:47:03.919572Z","iopub.execute_input":"2022-10-11T07:47:03.920327Z","iopub.status.idle":"2022-10-11T07:47:03.967509Z","shell.execute_reply.started":"2022-10-11T07:47:03.920245Z","shell.execute_reply":"2022-10-11T07:47:03.966558Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n\nfrom keras.callbacks import (ModelCheckpoint, LearningRateScheduler,\n                             EarlyStopping, ReduceLROnPlateau,CSVLogger)\n\nepochs = 30; batch_size = 16\ncheckpoint = ModelCheckpoint('../working/model_.h5', monitor='val_loss', verbose=1, \n                             save_best_only=True, mode='min', save_weights_only = True)\nreduceLROnPlat = ReduceLROnPlateau(monitor='val_loss', factor=0.5, patience=4, \n                                   verbose=1, mode='auto', epsilon=0.0001)\nearly = EarlyStopping(monitor=\"val_loss\", \n                      mode=\"min\", \n                      patience=9)\n\ncsv_logger = CSVLogger(filename='../working/training_log.csv',\n                       separator=',',\n                       append=True)\n\ntrain_generator = My_Generator(train_x, train_y, batch_size, is_train=True)\ntrain_mixup = My_Generator(train_x, train_y, batch_size, is_train=True, mix=False, augment=True)\nvalid_generator = My_Generator(valid_x, valid_y, batch_size, is_train=False)\n\n","metadata":{"execution":{"iopub.status.busy":"2022-10-11T07:47:03.969498Z","iopub.execute_input":"2022-10-11T07:47:03.969984Z","iopub.status.idle":"2022-10-11T07:47:03.982424Z","shell.execute_reply.started":"2022-10-11T07:47:03.969929Z","shell.execute_reply":"2022-10-11T07:47:03.981605Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"net = InceptionResNetV2(include_top=False, weights='../input/inceptionresnetv2/inception_resnet_v2_weights_tf_dim_ordering_tf_kernels_notop.h5',\n                       input_tensor=None, input_shape=(SIZE, SIZE, 3))","metadata":{"execution":{"iopub.status.busy":"2022-10-11T07:47:03.985812Z","iopub.execute_input":"2022-10-11T07:47:03.986082Z","iopub.status.idle":"2022-10-11T07:47:49.387715Z","shell.execute_reply.started":"2022-10-11T07:47:03.986034Z","shell.execute_reply":"2022-10-11T07:47:49.386559Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x = net.output\nx = Flatten()(x)\nx = Dropout(.5)(x)\noutput_layer = Dense(NUM_CLASSES, activation='softmax', name='softmax')(x)\nnet_final = Model(inputs=net.input, outputs=output_layer)","metadata":{"execution":{"iopub.status.busy":"2022-10-11T07:47:49.389163Z","iopub.execute_input":"2022-10-11T07:47:49.389480Z","iopub.status.idle":"2022-10-11T07:47:49.518090Z","shell.execute_reply.started":"2022-10-11T07:47:49.389415Z","shell.execute_reply":"2022-10-11T07:47:49.516985Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"FREEZE_LAYERS = 2\n\nfor layer in net_final.layers[:FREEZE_LAYERS]:\n    layer.trainable = False\nfor layer in net_final.layers[FREEZE_LAYERS:]:\n    layer.trainable = True","metadata":{"execution":{"iopub.status.busy":"2022-10-11T07:47:49.519470Z","iopub.execute_input":"2022-10-11T07:47:49.519763Z","iopub.status.idle":"2022-10-11T07:47:49.526952Z","shell.execute_reply.started":"2022-10-11T07:47:49.519715Z","shell.execute_reply":"2022-10-11T07:47:49.525396Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"net_final.compile(optimizer=Adam(lr=1e-5),\nloss='categorical_crossentropy', metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2022-10-11T07:47:49.528302Z","iopub.execute_input":"2022-10-11T07:47:49.528827Z","iopub.status.idle":"2022-10-11T07:47:49.576933Z","shell.execute_reply.started":"2022-10-11T07:47:49.528778Z","shell.execute_reply":"2022-10-11T07:47:49.576306Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"net_final.summary()","metadata":{"execution":{"iopub.status.busy":"2022-10-11T07:47:49.578362Z","iopub.execute_input":"2022-10-11T07:47:49.578658Z","iopub.status.idle":"2022-10-11T07:47:49.791932Z","shell.execute_reply.started":"2022-10-11T07:47:49.578612Z","shell.execute_reply":"2022-10-11T07:47:49.791226Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"net_final.fit_generator(\n    train_generator,\n    steps_per_epoch=np.ceil(float(len(train_y)) / float(128)),\n    epochs=50,\n    workers=WORKERS, use_multiprocessing=True,\n    verbose=1)","metadata":{"execution":{"iopub.status.busy":"2022-10-11T07:47:49.793219Z","iopub.execute_input":"2022-10-11T07:47:49.793529Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predicted = []","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submit = pd.read_csv('../input/aptos2019-blindness-detection/sample_submission.csv')","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tqdm import tqdm","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i, name in tqdm(enumerate(submit['id_code'])):\n    path = os.path.join('../input/aptos2019-blindness-detection/test_images/', name+'.png')\n    image = cv2.imread(path)\n    image = cv2.resize(image, (SIZE, SIZE))\n    X = np.array((image[np.newaxis])/255)\n    score_predict=((net_final.predict(X).ravel()*net_final.predict(X[:, ::-1, :, :]).ravel()*net_final.predict(X[:, ::-1, ::-1, :]).ravel()*net_final.predict(X[:, :, ::-1, :]).ravel())**0.25).tolist()\n    label_predict = np.argmax(score_predict)\n    predicted.append(str(label_predict))","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submit['diagnosis'] = predicted\nsubmit.to_csv('submission.csv', index=False)\nsubmit.head()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]}]}