{"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":"# 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\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 read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-02-25T08:27:47.888492Z","iopub.execute_input":"2022-02-25T08:27:47.889043Z","iopub.status.idle":"2022-02-25T08:27:54.930656Z","shell.execute_reply.started":"2022-02-25T08:27:47.888994Z","shell.execute_reply":"2022-02-25T08:27:54.929762Z"},"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\nfrom tensorflow.python.keras.applications.resnet import ResNet50\n\n# Set seeds to make the experiment more reproducible.\n\n%matplotlib inline\nsns.set(style=\"whitegrid\")\nwarnings.filterwarnings(\"ignore\")","metadata":{"execution":{"iopub.status.busy":"2022-02-25T08:33:46.613895Z","iopub.execute_input":"2022-02-25T08:33:46.614216Z","iopub.status.idle":"2022-02-25T08:33:46.635078Z","shell.execute_reply.started":"2022-02-25T08:33:46.614183Z","shell.execute_reply":"2022-02-25T08:33:46.634391Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = pd.read_csv('../input/aptos2019-blindness-detection/train.csv')\ntest = pd.read_csv('../input/aptos2019-blindness-detection/test.csv')","metadata":{"execution":{"iopub.status.busy":"2022-02-25T08:29:30.889029Z","iopub.execute_input":"2022-02-25T08:29:30.889323Z","iopub.status.idle":"2022-02-25T08:29:30.918902Z","shell.execute_reply.started":"2022-02-25T08:29:30.889294Z","shell.execute_reply":"2022-02-25T08:29:30.918217Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"Number of Training Samples\",train.shape[0])\nprint(\"Number of Testing Samples\",test.shape[0]);","metadata":{"execution":{"iopub.status.busy":"2022-02-25T08:29:37.857822Z","iopub.execute_input":"2022-02-25T08:29:37.858581Z","iopub.status.idle":"2022-02-25T08:29:37.863445Z","shell.execute_reply.started":"2022-02-25T08:29:37.858532Z","shell.execute_reply":"2022-02-25T08:29:37.862634Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.head()","metadata":{"execution":{"iopub.status.busy":"2022-02-25T08:29:39.723242Z","iopub.execute_input":"2022-02-25T08:29:39.723899Z","iopub.status.idle":"2022-02-25T08:29:39.744112Z","shell.execute_reply.started":"2022-02-25T08:29:39.723849Z","shell.execute_reply":"2022-02-25T08:29:39.743162Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.head()","metadata":{"execution":{"iopub.status.busy":"2022-02-25T08:29:42.329673Z","iopub.execute_input":"2022-02-25T08:29:42.330481Z","iopub.status.idle":"2022-02-25T08:29:42.340054Z","shell.execute_reply.started":"2022-02-25T08:29:42.330438Z","shell.execute_reply":"2022-02-25T08:29:42.339287Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"f, ax = plt.subplots(figsize=(14, 8.7))\nax = sns.countplot(x=\"diagnosis\", data=train, palette=\"GnBu_d\")\nsns.despine()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-02-25T08:29:44.974399Z","iopub.execute_input":"2022-02-25T08:29:44.974651Z","iopub.status.idle":"2022-02-25T08:29:45.176957Z","shell.execute_reply.started":"2022-02-25T08:29:44.974625Z","shell.execute_reply":"2022-02-25T08:29:45.175092Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.set_style(\"white\")\ncount = 1\nplt.figure(figsize=[20, 20])\nfor img_name in train['id_code'][:15]:\n    img = cv2.imread(\"../input/aptos2019-blindness-detection/train_images/%s.png\" % img_name)[...,[2, 1, 0]]\n    plt.subplot(5, 5, count)\n    plt.imshow(img)\n    plt.title(\"Image %s\" % count)\n    count += 1\n    \nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-02-25T08:29:48.14304Z","iopub.execute_input":"2022-02-25T08:29:48.143444Z","iopub.status.idle":"2022-02-25T08:29:59.160802Z","shell.execute_reply.started":"2022-02-25T08:29:48.143415Z","shell.execute_reply":"2022-02-25T08:29:59.1598Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#example for category 2\nimg=cv2.imread(\"../input/aptos2019-blindness-detection/train_images/000c1434d8d7.png\")[...,[2, 1, 0]]\nplt.imshow(img);","metadata":{"execution":{"iopub.status.busy":"2022-02-25T08:29:59.162626Z","iopub.execute_input":"2022-02-25T08:29:59.162963Z","iopub.status.idle":"2022-02-25T08:30:00.684493Z","shell.execute_reply.started":"2022-02-25T08:29:59.162926Z","shell.execute_reply":"2022-02-25T08:30:00.683585Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#example for category 4\nimg=cv2.imread(\"../input/aptos2019-blindness-detection/train_images/001639a390f0.png\")[...,[2, 1, 0]]\nplt.imshow(img);","metadata":{"execution":{"iopub.status.busy":"2022-02-25T08:30:03.602648Z","iopub.execute_input":"2022-02-25T08:30:03.603158Z","iopub.status.idle":"2022-02-25T08:30:05.097189Z","shell.execute_reply.started":"2022-02-25T08:30:03.603127Z","shell.execute_reply":"2022-02-25T08:30:05.096322Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.set_style(\"white\")\ncount = 1\nplt.figure(figsize=[20, 20])\nfor img_name in train['id_code'][:15]:\n    img = cv2.imread(\"../input/aptos2019-blindness-detection/train_images/%s.png\" % img_name)\n    plt.subplot(5, 5, count)\n    plt.imshow(img,cmap=\"gray\")\n    plt.title(\"Image %s\" % count)\n    count += 1\n    \nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-02-25T08:30:31.0249Z","iopub.execute_input":"2022-02-25T08:30:31.025528Z","iopub.status.idle":"2022-02-25T08:30:41.746762Z","shell.execute_reply.started":"2022-02-25T08:30:31.025477Z","shell.execute_reply":"2022-02-25T08:30:41.743079Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"BATCH_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":"2022-02-25T08:30:48.531521Z","iopub.execute_input":"2022-02-25T08:30:48.531815Z","iopub.status.idle":"2022-02-25T08:30:48.536837Z","shell.execute_reply.started":"2022-02-25T08:30:48.531781Z","shell.execute_reply":"2022-02-25T08:30:48.53624Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\ntrain[\"id_code\"] = train[\"id_code\"].apply(lambda x: x + \".png\")\ntest[\"id_code\"] = test[\"id_code\"].apply(lambda x: x + \".png\")\ntrain['diagnosis'] = train['diagnosis'].astype('str')\ntrain.head()","metadata":{"execution":{"iopub.status.busy":"2022-02-25T08:30:55.402308Z","iopub.execute_input":"2022-02-25T08:30:55.403078Z","iopub.status.idle":"2022-02-25T08:30:55.417247Z","shell.execute_reply.started":"2022-02-25T08:30:55.40304Z","shell.execute_reply":"2022-02-25T08:30:55.416285Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_datagen=ImageDataGenerator(rescale=1./255, \n                                 validation_split=0.2,\n                                 horizontal_flip=True)\n\ntrain_generator=train_datagen.flow_from_dataframe(\n    dataframe=train,\n    directory=\"../input/aptos2019-blindness-detection/train_images/\",\n    x_col=\"id_code\",\n    y_col=\"diagnosis\",\n    batch_size=BATCH_SIZE,\n    class_mode=\"categorical\",\n    target_size=(HEIGHT, WIDTH),\n    subset='training')\n\nvalid_generator=train_datagen.flow_from_dataframe(\n    dataframe=train,\n    directory=\"../input/aptos2019-blindness-detection/train_images/\",\n    x_col=\"id_code\",\n    y_col=\"diagnosis\",\n    batch_size=BATCH_SIZE,\n    class_mode=\"categorical\",    \n    target_size=(HEIGHT, WIDTH),\n    subset='validation')\n\ntest_datagen = ImageDataGenerator(rescale=1./255)\n\ntest_generator = test_datagen.flow_from_dataframe(  \n        dataframe=test,\n        directory = \"../input/aptos2019-blindness-detection/test_images/\",\n        x_col=\"id_code\",\n        target_size=(HEIGHT, WIDTH),\n        batch_size=1,\n        shuffle=False,\n        class_mode=None)","metadata":{"execution":{"iopub.status.busy":"2022-02-25T08:30:58.170064Z","iopub.execute_input":"2022-02-25T08:30:58.17083Z","iopub.status.idle":"2022-02-25T08:31:00.713663Z","shell.execute_reply.started":"2022-02-25T08:30:58.170794Z","shell.execute_reply":"2022-02-25T08:31:00.712754Z"},"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 = ResNet50(weights=None, \n                                       include_top=False,\n                                       input_tensor=input_tensor)\n    base_model.load_weights('../input/resnet50/resnet50_weights_tf_dim_ordering_tf_kernels_notop.h5')\n\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":"2022-02-25T08:34:04.898986Z","iopub.execute_input":"2022-02-25T08:34:04.899264Z","iopub.status.idle":"2022-02-25T08:34:04.904527Z","shell.execute_reply.started":"2022-02-25T08:34:04.899237Z","shell.execute_reply":"2022-02-25T08:34:04.903922Z"},"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 = 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)\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2022-02-25T08:34:09.934618Z","iopub.execute_input":"2022-02-25T08:34:09.934875Z","iopub.status.idle":"2022-02-25T08:34:09.981132Z","shell.execute_reply.started":"2022-02-25T08:34:09.934848Z","shell.execute_reply":"2022-02-25T08:34:09.980129Z"},"trusted":true},"execution_count":null,"outputs":[]}]}