{"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":"BASE_PATH ='../input/aptos2019-blindness-detection/'","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-07-25T05:48:06.604899Z","iopub.execute_input":"2021-07-25T05:48:06.605200Z","iopub.status.idle":"2021-07-25T05:48:06.613202Z","shell.execute_reply.started":"2021-07-25T05:48:06.605129Z","shell.execute_reply":"2021-07-25T05:48:06.612142Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"TRAIN_PATH = BASE_PATH + \"train_images/\"","metadata":{"execution":{"iopub.status.busy":"2021-07-25T05:48:10.679614Z","iopub.execute_input":"2021-07-25T05:48:10.680034Z","iopub.status.idle":"2021-07-25T05:48:10.688804Z","shell.execute_reply.started":"2021-07-25T05:48:10.680001Z","shell.execute_reply":"2021-07-25T05:48:10.683530Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nimport cv2\nimport random\nimport warnings\nimport numpy as np\nimport pandas as pd\nimport seaborn as sns\nimport matplotlib.pyplot as plt\nfrom sklearn.metrics import confusion_matrix, cohen_kappa_score\nfrom keras.models import Model\nfrom keras import optimizers, applications\nfrom keras.preprocessing.image import ImageDataGenerator\nfrom keras.callbacks import EarlyStopping, ReduceLROnPlateau\nfrom keras.layers import Dense, Dropout, GlobalAveragePooling2D, Input\nimport tensorflow as tf\n\n# Set seeds to make the experiment more reproducible.\n\ndef seed_everything(seed=0):\n    random.seed(seed)\n    os.environ['PYTHONHASHSEED'] = str(seed)\n    np.random.seed(seed)\n    tf.random.set_seed(0)\n    \nseed_everything()\n\n%matplotlib inline\nsns.set(style=\"whitegrid\")\nwarnings.filterwarnings(\"ignore\")","metadata":{"execution":{"iopub.status.busy":"2021-07-25T05:48:11.581065Z","iopub.execute_input":"2021-07-25T05:48:11.581434Z","iopub.status.idle":"2021-07-25T05:48:17.500672Z","shell.execute_reply.started":"2021-07-25T05:48:11.581400Z","shell.execute_reply":"2021-07-25T05:48:17.499746Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nimport os","metadata":{"execution":{"iopub.status.busy":"2021-07-25T05:48:17.502193Z","iopub.execute_input":"2021-07-25T05:48:17.502555Z","iopub.status.idle":"2021-07-25T05:48:17.510376Z","shell.execute_reply.started":"2021-07-25T05:48:17.502515Z","shell.execute_reply":"2021-07-25T05:48:17.509478Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"os.listdir(BASE_PATH + \"train_images\")[:10]","metadata":{"execution":{"iopub.status.busy":"2021-07-25T05:48:19.919781Z","iopub.execute_input":"2021-07-25T05:48:19.920169Z","iopub.status.idle":"2021-07-25T05:48:20.105083Z","shell.execute_reply.started":"2021-07-25T05:48:19.920137Z","shell.execute_reply":"2021-07-25T05:48:20.104190Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = pd.read_csv(BASE_PATH + \"train.csv\")","metadata":{"execution":{"iopub.status.busy":"2021-07-25T05:48:21.395833Z","iopub.execute_input":"2021-07-25T05:48:21.396170Z","iopub.status.idle":"2021-07-25T05:48:21.560262Z","shell.execute_reply.started":"2021-07-25T05:48:21.396141Z","shell.execute_reply":"2021-07-25T05:48:21.559339Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.head()","metadata":{"execution":{"iopub.status.busy":"2021-07-25T05:48:22.044130Z","iopub.execute_input":"2021-07-25T05:48:22.044458Z","iopub.status.idle":"2021-07-25T05:48:22.064264Z","shell.execute_reply.started":"2021-07-25T05:48:22.044428Z","shell.execute_reply":"2021-07-25T05:48:22.062895Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(train)","metadata":{"execution":{"iopub.status.busy":"2021-07-25T05:48:22.545939Z","iopub.execute_input":"2021-07-25T05:48:22.546282Z","iopub.status.idle":"2021-07-25T05:48:22.553980Z","shell.execute_reply.started":"2021-07-25T05:48:22.546254Z","shell.execute_reply":"2021-07-25T05:48:22.552906Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train[\"diagnosis\"].value_counts()","metadata":{"execution":{"iopub.status.busy":"2021-07-25T05:48:22.903292Z","iopub.execute_input":"2021-07-25T05:48:22.904007Z","iopub.status.idle":"2021-07-25T05:48:22.921916Z","shell.execute_reply.started":"2021-07-25T05:48:22.903964Z","shell.execute_reply":"2021-07-25T05:48:22.920893Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Model parameters\nBATCH_SIZE = 8\nEPOCHS = 20\nWARMUP_EPOCHS = 2\nLEARNING_RATE = 1e-4\nWARMUP_LEARNING_RATE = 1e-3\nHEIGHT = 512\nWIDTH = 512\nCANAL = 3\nN_CLASSES = train['diagnosis'].nunique()\nES_PATIENCE = 5\nRLROP_PATIENCE = 3\nDECAY_DROP = 0.5","metadata":{"execution":{"iopub.status.busy":"2021-07-25T05:48:23.282095Z","iopub.execute_input":"2021-07-25T05:48:23.282454Z","iopub.status.idle":"2021-07-25T05:48:23.288851Z","shell.execute_reply.started":"2021-07-25T05:48:23.282422Z","shell.execute_reply":"2021-07-25T05:48:23.287519Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Preprocecss data\ntrain[\"id_code\"] = train[\"id_code\"].apply(lambda x: x + \".png\")\ntrain['diagnosis'] = train['diagnosis'].astype('str')\ntrain.head()","metadata":{"execution":{"iopub.status.busy":"2021-07-25T05:48:23.618205Z","iopub.execute_input":"2021-07-25T05:48:23.618560Z","iopub.status.idle":"2021-07-25T05:48:23.636356Z","shell.execute_reply.started":"2021-07-25T05:48:23.618529Z","shell.execute_reply":"2021-07-25T05:48:23.635324Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split","metadata":{"execution":{"iopub.status.busy":"2021-07-25T05:48:24.014932Z","iopub.execute_input":"2021-07-25T05:48:24.015266Z","iopub.status.idle":"2021-07-25T05:48:24.034573Z","shell.execute_reply.started":"2021-07-25T05:48:24.015238Z","shell.execute_reply":"2021-07-25T05:48:24.033680Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X = train[\"id_code\"]","metadata":{"execution":{"iopub.status.busy":"2021-07-25T05:48:24.418943Z","iopub.execute_input":"2021-07-25T05:48:24.419315Z","iopub.status.idle":"2021-07-25T05:48:24.423244Z","shell.execute_reply.started":"2021-07-25T05:48:24.419286Z","shell.execute_reply":"2021-07-25T05:48:24.422142Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y = train[\"diagnosis\"]","metadata":{"execution":{"iopub.status.busy":"2021-07-25T05:48:24.819202Z","iopub.execute_input":"2021-07-25T05:48:24.819573Z","iopub.status.idle":"2021-07-25T05:48:24.824251Z","shell.execute_reply.started":"2021-07-25T05:48:24.819541Z","shell.execute_reply":"2021-07-25T05:48:24.822851Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":" X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)","metadata":{"execution":{"iopub.status.busy":"2021-07-25T05:48:25.267448Z","iopub.execute_input":"2021-07-25T05:48:25.267791Z","iopub.status.idle":"2021-07-25T05:48:25.276528Z","shell.execute_reply.started":"2021-07-25T05:48:25.267757Z","shell.execute_reply":"2021-07-25T05:48:25.275705Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df = pd.DataFrame({\"id_code\":X_train,\"diagnosis\":y_train})\ntrain_df.head()","metadata":{"execution":{"iopub.status.busy":"2021-07-25T05:48:25.600358Z","iopub.execute_input":"2021-07-25T05:48:25.600648Z","iopub.status.idle":"2021-07-25T05:48:25.610893Z","shell.execute_reply.started":"2021-07-25T05:48:25.600620Z","shell.execute_reply":"2021-07-25T05:48:25.609994Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"valid_df = pd.DataFrame({\"id_code\":X_test,\"diagnosis\":y_test})\nvalid_df.head()","metadata":{"execution":{"iopub.status.busy":"2021-07-25T05:48:26.139452Z","iopub.execute_input":"2021-07-25T05:48:26.139785Z","iopub.status.idle":"2021-07-25T05:48:26.150057Z","shell.execute_reply.started":"2021-07-25T05:48:26.139754Z","shell.execute_reply":"2021-07-25T05:48:26.149219Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_datagen=ImageDataGenerator(rescale=1./255,horizontal_flip=True)\nvalid_datagen=ImageDataGenerator(rescale=1./255,horizontal_flip=True)\n\ntrain_generator=train_datagen.flow_from_dataframe(\n    dataframe=train_df,\n    directory=TRAIN_PATH,\n    x_col=\"id_code\",\n    y_col=\"diagnosis\",\n    batch_size=BATCH_SIZE,\n    class_mode=\"categorical\",\n    target_size=(HEIGHT, WIDTH))\n\nvalid_generator=valid_datagen.flow_from_dataframe(\n    dataframe=valid_df,\n    directory=TRAIN_PATH,\n    x_col=\"id_code\",\n    y_col=\"diagnosis\",\n    batch_size=BATCH_SIZE,\n    class_mode=\"categorical\",    \n    target_size=(HEIGHT, WIDTH))\n\n","metadata":{"execution":{"iopub.status.busy":"2021-07-25T05:48:26.344096Z","iopub.execute_input":"2021-07-25T05:48:26.344417Z","iopub.status.idle":"2021-07-25T05:48:29.026322Z","shell.execute_reply.started":"2021-07-25T05:48:26.344389Z","shell.execute_reply":"2021-07-25T05:48:29.025480Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def create_model(input_shape, n_out):\n    input_tensor = Input(shape=input_shape)\n    base_model = applications.ResNet101(include_top=False,\n                                       input_tensor=input_tensor)\n    x = GlobalAveragePooling2D()(base_model.output)\n    x = Dropout(0.5)(x)\n    x = Dense(2048, activation='relu')(x)\n    x = Dropout(0.5)(x)\n    final_output = Dense(n_out, activation='softmax', name='final_output')(x)\n    model = Model(input_tensor, final_output)\n    \n    return model","metadata":{"execution":{"iopub.status.busy":"2021-07-25T05:48:29.029103Z","iopub.execute_input":"2021-07-25T05:48:29.029365Z","iopub.status.idle":"2021-07-25T05:48:29.037352Z","shell.execute_reply.started":"2021-07-25T05:48:29.029339Z","shell.execute_reply":"2021-07-25T05:48:29.036090Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = create_model(input_shape=(HEIGHT, WIDTH, CANAL), n_out=N_CLASSES)\n\nmetric_list = [\"accuracy\"]\n\nfor layer in model.layers:\n    layer.trainable = False\n\nfor i in range(-5, 0):\n    model.layers[i].trainable = True\n\nmetric_list = [\"accuracy\"]\noptimizer = optimizers.Adam(lr=WARMUP_LEARNING_RATE)\nmodel.compile(optimizer=optimizer, loss=\"categorical_crossentropy\",  metrics=metric_list)","metadata":{"execution":{"iopub.status.busy":"2021-07-25T05:48:29.040220Z","iopub.execute_input":"2021-07-25T05:48:29.040730Z","iopub.status.idle":"2021-07-25T05:48:35.485788Z","shell.execute_reply.started":"2021-07-25T05:48:29.040572Z","shell.execute_reply":"2021-07-25T05:48:35.484869Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"STEP_SIZE_TRAIN = train_generator.n//train_generator.batch_size\nSTEP_SIZE_VALID = valid_generator.n//valid_generator.batch_size\n\nhistory_warmup = model.fit_generator(generator=train_generator,\n                                     steps_per_epoch=STEP_SIZE_TRAIN,\n                                     validation_data=valid_generator,\n                                     validation_steps=STEP_SIZE_VALID,\n                                     epochs=WARMUP_EPOCHS,\n                                     verbose=1).history","metadata":{"execution":{"iopub.status.busy":"2021-07-25T05:48:35.487284Z","iopub.execute_input":"2021-07-25T05:48:35.487785Z","iopub.status.idle":"2021-07-25T06:06:34.732554Z","shell.execute_reply.started":"2021-07-25T05:48:35.487747Z","shell.execute_reply":"2021-07-25T06:06:34.731575Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = create_model(input_shape=(HEIGHT, WIDTH, CANAL), n_out=N_CLASSES)\n\nfor layer in model.layers:\n    layer.trainable = True\n\nmetric_list = [\"accuracy\"]\n\nes = 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)\n\ncallback_list = [es, rlrop]\noptimizer = optimizers.Adam(lr=LEARNING_RATE)\nmodel.compile(optimizer=optimizer, loss=\"binary_crossentropy\",  metrics=metric_list)\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2021-07-25T06:06:34.736080Z","iopub.execute_input":"2021-07-25T06:06:34.736392Z","iopub.status.idle":"2021-07-25T06:06:37.740343Z","shell.execute_reply.started":"2021-07-25T06:06:34.736359Z","shell.execute_reply":"2021-07-25T06:06:37.739389Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"STEP_SIZE_TRAIN = train_generator.n//train_generator.batch_size\nSTEP_SIZE_VALID = valid_generator.n//valid_generator.batch_size","metadata":{"execution":{"iopub.status.busy":"2021-07-25T06:06:37.742291Z","iopub.execute_input":"2021-07-25T06:06:37.742680Z","iopub.status.idle":"2021-07-25T06:06:37.747550Z","shell.execute_reply.started":"2021-07-25T06:06:37.742640Z","shell.execute_reply":"2021-07-25T06:06:37.746296Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history_finetunning = model.fit_generator(generator=train_generator,\n                                          steps_per_epoch=STEP_SIZE_TRAIN,\n                                          validation_data=valid_generator,\n                                          validation_steps=STEP_SIZE_VALID,\n                                          epochs=EPOCHS,\n                                          callbacks=callback_list,\n                                          verbose=1).history","metadata":{"execution":{"iopub.status.busy":"2021-07-25T06:06:37.749492Z","iopub.execute_input":"2021-07-25T06:06:37.750032Z","iopub.status.idle":"2021-07-25T06:22:27.871132Z","shell.execute_reply.started":"2021-07-25T06:06:37.749991Z","shell.execute_reply":"2021-07-25T06:22:27.869809Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history_df = pd.DataFrame(history_finetunning)","metadata":{"execution":{"iopub.status.busy":"2021-07-25T06:25:50.024376Z","iopub.execute_input":"2021-07-25T06:25:50.024741Z","iopub.status.idle":"2021-07-25T06:25:50.031113Z","shell.execute_reply.started":"2021-07-25T06:25:50.024694Z","shell.execute_reply":"2021-07-25T06:25:50.029894Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history_df","metadata":{"execution":{"iopub.status.busy":"2021-07-25T06:25:54.900869Z","iopub.execute_input":"2021-07-25T06:25:54.901205Z","iopub.status.idle":"2021-07-25T06:25:54.916596Z","shell.execute_reply.started":"2021-07-25T06:25:54.901177Z","shell.execute_reply":"2021-07-25T06:25:54.915619Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history_df.to_csv(\"history_resenet_101.csv\",index = False)","metadata":{"execution":{"iopub.status.busy":"2021-07-25T06:27:58.107481Z","iopub.execute_input":"2021-07-25T06:27:58.107844Z","iopub.status.idle":"2021-07-25T06:27:58.338999Z","shell.execute_reply.started":"2021-07-25T06:27:58.107812Z","shell.execute_reply":"2021-07-25T06:27:58.338171Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history_df.loc[:, ['loss', 'val_loss']].plot()","metadata":{"execution":{"iopub.status.busy":"2021-07-25T06:26:16.373068Z","iopub.execute_input":"2021-07-25T06:26:16.373444Z","iopub.status.idle":"2021-07-25T06:26:16.622352Z","shell.execute_reply.started":"2021-07-25T06:26:16.373412Z","shell.execute_reply":"2021-07-25T06:26:16.621536Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"complete_datagen = ImageDataGenerator(rescale=1./255)\ncomplete_generator = complete_datagen.flow_from_dataframe(  \n        dataframe=valid_df,\n        directory = TRAIN_PATH,\n        x_col=\"id_code\",\n        target_size=(HEIGHT, WIDTH),\n        batch_size=1,\n        shuffle=False,\n        class_mode=None)\n\nSTEP_SIZE_COMPLETE = complete_generator.n//complete_generator.batch_size\nvalid_preds = model.predict_generator(complete_generator, steps=STEP_SIZE_COMPLETE)\nvalid_preds = [np.argmax(pred) for pred in valid_preds]","metadata":{"execution":{"iopub.status.busy":"2021-06-08T18:11:25.905647Z","iopub.execute_input":"2021-06-08T18:11:25.90596Z","iopub.status.idle":"2021-06-08T18:12:46.454832Z","shell.execute_reply.started":"2021-06-08T18:11:25.90593Z","shell.execute_reply":"2021-06-08T18:12:46.453987Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(valid_df[\"diagnosis\"]), len(valid_preds)","metadata":{"execution":{"iopub.status.busy":"2021-06-08T18:14:10.979836Z","iopub.execute_input":"2021-06-08T18:14:10.980187Z","iopub.status.idle":"2021-06-08T18:14:10.985772Z","shell.execute_reply.started":"2021-06-08T18:14:10.980155Z","shell.execute_reply":"2021-06-08T18:14:10.984888Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"valid_preds_df = pd.DataFrame({\"actual\":valid_df[\"diagnosis\"],\"predictions\":valid_preds})","metadata":{"execution":{"iopub.status.busy":"2021-06-08T18:14:14.961976Z","iopub.execute_input":"2021-06-08T18:14:14.962328Z","iopub.status.idle":"2021-06-08T18:14:14.969713Z","shell.execute_reply.started":"2021-06-08T18:14:14.962298Z","shell.execute_reply":"2021-06-08T18:14:14.968918Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"valid_preds_df.to_csv(\"valid_data_preds.csv\",index = False)","metadata":{"execution":{"iopub.status.busy":"2021-06-08T18:15:39.79273Z","iopub.execute_input":"2021-06-08T18:15:39.793061Z","iopub.status.idle":"2021-06-08T18:15:40.016745Z","shell.execute_reply.started":"2021-06-08T18:15:39.793015Z","shell.execute_reply":"2021-06-08T18:15:40.015859Z"},"trusted":true},"execution_count":null,"outputs":[]}]}