{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load in \n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the \"../input/\" directory.\n# For example, running this (by clicking run or pressing Shift+Enter) will list the files in the input directory\n\nimport os\nprint(os.listdir(\"../input\"))\nimport cv2\n# Any results you write to the current directory are saved as output.","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":true},"cell_type":"code","source":"import os\nos.getcwd()\nfrom matplotlib import pyplot as plt\nimport seaborn as sns\n%matplotlib inline\nimport cv2","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train = pd.read_csv('../input/aptos2019-blindness-detection/train.csv')\ntest = pd.read_csv('../input/aptos2019-blindness-detection/test.csv')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"markdown","source":"## Modelling "},{"metadata":{"trusted":true},"cell_type":"code","source":"IMG_SIZE = 512\nNB_CHANNELS = 3\nMAX_TRAIN_STEPS = 1000\nBATCH_SIZE = 32\nNB_EPOCHS = 40\nweight_path = './'\n\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from sklearn.model_selection import train_test_split\nfrom keras.utils import to_categorical\nx_train,x_test,y_train,y_test = train_test_split(train['id_code'].values,train['diagnosis'].values,test_size=0.1,random_state=42)\ntrain_image_dir = '../input/aptos2019-blindness-detection/train_images/'\ndef make_image_gen(img_file_list,class_list, batch_size = 4):\n    all_batches = img_file_list\n    all_classes = class_list\n    out_rgb = []\n    yield_rgb = []\n    yield_label = []\n    out_label = []\n    \n    \n    while True:\n        #np.random.shuffle(all_batches)\n        out_rgb = []\n        out_label = []\n        for idx, c_img_id in enumerate(all_batches):\n            imgname  = c_img_id + '.png'\n            c_img = cv2.imread(os.path.join(train_image_dir,imgname))\n            c_img = cv2.cvtColor(c_img, cv2.COLOR_BGR2HSV)\n            c_img = cv2.resize(c_img,(IMG_SIZE,IMG_SIZE),interpolation = cv2.INTER_AREA)\n            \n            label = class_list[idx]\n            out_rgb += [c_img]\n            out_label += [label]\n            if len(out_rgb)>=batch_size:\n                yield_rgb = out_rgb\n                yield_label = out_label\n                out_rgb = []\n                out_label = []\n                #print(\"size\",sys.getsizeof(out_rgb))\n                yield np.stack(yield_rgb, 0)/255.0, to_categorical(np.stack(yield_label, 0),num_classes=5)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"\ntrain_gen = make_image_gen(x_train,y_train,4)\ntrain_x, train_y = next(train_gen)\nprint('x', train_x.shape, train_x.min(), train_x.max())\nprint('y', train_y.shape, train_y.min(), train_y.max())","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"valid_x, valid_y = next(make_image_gen(x_test,y_test,4))\nprint(valid_x.shape, valid_y.shape)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from keras.models import Sequential, Model\nfrom keras.layers import Conv2D, MaxPooling2D , Input, GlobalAveragePooling2D\nfrom keras.layers import Activation, Flatten, Dense, Dropout\nfrom keras.preprocessing.image import ImageDataGenerator\nfrom keras.applications.resnet50 import ResNet50\nfrom keras.optimizers import Adam\n#from keras.applications.resnet50 import preprocess_input, decode_predictions\nimport time\n\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from keras.preprocessing.image import ImageDataGenerator\nfrom keras.utils import to_categorical\n\ndg_args = dict(featurewise_center = False, \n                  samplewise_center = False,\n                  rotation_range = 15, \n                  width_shift_range = 0.1, \n                  height_shift_range = 0.1, \n                  shear_range = 0.01,\n                  zoom_range = [0.9, 1.25],  \n                  horizontal_flip = True, \n                  vertical_flip = False,\n                  fill_mode = 'reflect',\n                   data_format = 'channels_last')\n\nimage_gen = ImageDataGenerator(**dg_args)\n\n\ndef create_aug_gen(in_gen, seed = None):\n    np.random.seed(seed if seed is not None else np.random.choice(range(9999)))\n    for in_x, in_y in in_gen:\n        seed = np.random.choice(range(9999))\n        # keep the seeds syncronized otherwise the augmentation to the images is different from the masks\n        g_x = image_gen.flow(255*in_x, \n                             batch_size = in_x.shape[0], \n                             seed = seed, \n                             shuffle=True)\n\n        g_y = in_y\n        yield next(g_x)/255.0 , g_y","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_gen = make_image_gen(x_train,y_train,4)\ncur_gen = create_aug_gen(train_gen)\nt_x, t_y = next(cur_gen)\nprint('x', t_x.shape, t_x.dtype, t_x.min(), t_x.max())\nprint('y', t_y.shape, t_y.dtype, t_y.min(), t_y.max())","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#https://www.kaggle.com/mathormad/aptos-resnet50-baseline\nfunction = \"softmax\"\ndef create_model(input_shape, n_out):\n    input_tensor = Input(shape=input_shape)\n    base_model = ResNet50(include_top=False,\n                   weights=None,\n                   input_tensor=input_tensor)\n    base_model.load_weights('../input/resnet50/resnet50_weights_tf_dim_ordering_tf_kernels_notop.h5')\n    x = GlobalAveragePooling2D()(base_model.output)\n    x = Dropout(0.5)(x)\n    x = Dense(1024, activation='relu')(x)\n    x = Dropout(0.5)(x)\n    final_output = Dense(n_out, activation=function, name='final_output')(x)\n    model = Model(input_tensor, final_output)    \n    return model","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"input_shape = (IMG_SIZE,IMG_SIZE,NB_CHANNELS)\nn_out = 5\nmodel = create_model(input_shape,n_out)\n#model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from keras.optimizers import Nadam, adadelta,adagrad,adam,RMSprop,SGD\nfor layer in model.layers:\n    layer.trainable = False\n\nfor i in range(-5,0):\n    model.layers[i].trainable = True\n\nmodel.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])\nmodel.summary()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"\n\nfrom keras.callbacks import EarlyStopping , ReduceLROnPlateau , ModelCheckpoint\nstep_count = min(MAX_TRAIN_STEPS, len(x_train)//BATCH_SIZE)\ntrain_gen = make_image_gen(x_train,y_train,BATCH_SIZE)\naug_gen = create_aug_gen(train_gen)\nvalid_gen = make_image_gen(x_test,y_test,2)\n\n\n#earlyStopper = EarlyStopping(monitor=\"acc\", mode=\"max\", patience=15)\n#checkPointer = ModelCheckpoint(weight_path, monitor='acc', verbose=1, \n#                             save_best_only=True, mode='max', save_weights_only = True)\ncheckpoint = ModelCheckpoint('../working/aptos.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='min', min_delta=0.0001)\nearly = EarlyStopping(monitor=\"val_loss\", \n                      mode=\"min\", \n                      patience=9)\ncallbacks_list = [checkpoint, reduceLROnPlat, early]\nmodel.fit_generator(aug_gen,\n                            steps_per_epoch=step_count, \n                            epochs=NB_EPOCHS, \n                            validation_data=valid_gen,\n                            validation_steps=len(x_test)//BATCH_SIZE, \n                            callbacks=callbacks_list,\n                            workers=1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#https://www.kaggle.com/mathormad/aptos-resnet50-baseline\nfrom tqdm import tqdm\nsubmit = pd.read_csv('../input/aptos2019-blindness-detection/sample_submission.csv')\n# model.load_weights('../working/Resnet50.h5')\nmodel.load_weights('../working/aptos.h5')\npredicted = []\nfor 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, (IMG_SIZE, IMG_SIZE))\n    score_predict = model.predict((image[np.newaxis])/255)\n    label_predict = np.argmax(score_predict)\n    # label_predict = score_predict.astype(int).sum() - 1\n    predicted.append(str(label_predict))\nsubmit['diagnosis'] = predicted\nsubmit.to_csv('submission.csv', index=False)\nsubmit.head()","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"name":"python","version":"3.6.4","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat":4,"nbformat_minor":1}