{"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)\nimport tensorflow as tf\nfrom matplotlib import pyplot as plt\nfrom sklearn.metrics import cohen_kappa_score\nfrom keras.preprocessing.image import ImageDataGenerator\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":"2023-04-15T07:56:45.045819Z","iopub.execute_input":"2023-04-15T07:56:45.046175Z","iopub.status.idle":"2023-04-15T07:56:54.215210Z","shell.execute_reply.started":"2023-04-15T07:56:45.046143Z","shell.execute_reply":"2023-04-15T07:56:54.214223Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"","metadata":{}},{"cell_type":"code","source":"train = pd.read_csv('../input/aptos2019-blindness-detection/train.csv')\ntest = pd.read_csv('../input/aptos2019-blindness-detection/test.csv')\nsubmission= pd.read_csv('../input/aptos2019-blindness-detection/sample_submission.csv')","metadata":{"execution":{"iopub.status.busy":"2023-04-15T07:56:54.216954Z","iopub.execute_input":"2023-04-15T07:56:54.218969Z","iopub.status.idle":"2023-04-15T07:56:54.254073Z","shell.execute_reply.started":"2023-04-15T07:56:54.218928Z","shell.execute_reply":"2023-04-15T07:56:54.253139Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train['diagnosis'] = train['diagnosis'].replace([2,3,4],1)\ntrain['diagnosis'].unique()","metadata":{"execution":{"iopub.status.busy":"2023-04-15T07:56:54.255237Z","iopub.execute_input":"2023-04-15T07:56:54.255585Z","iopub.status.idle":"2023-04-15T07:56:54.274707Z","shell.execute_reply.started":"2023-04-15T07:56:54.255548Z","shell.execute_reply":"2023-04-15T07:56:54.273661Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.head(10)","metadata":{"execution":{"iopub.status.busy":"2023-04-15T07:56:54.278005Z","iopub.execute_input":"2023-04-15T07:56:54.278266Z","iopub.status.idle":"2023-04-15T07:56:54.290730Z","shell.execute_reply.started":"2023-04-15T07:56:54.278240Z","shell.execute_reply":"2023-04-15T07:56:54.289703Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.head()","metadata":{"execution":{"iopub.status.busy":"2023-04-15T07:56:54.292451Z","iopub.execute_input":"2023-04-15T07:56:54.292804Z","iopub.status.idle":"2023-04-15T07:56:54.301639Z","shell.execute_reply.started":"2023-04-15T07:56:54.292770Z","shell.execute_reply":"2023-04-15T07:56:54.300425Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('Number of train samples: ', train.shape[0])\nprint('Number of test samples: ', test.shape[0])","metadata":{"execution":{"iopub.status.busy":"2023-04-15T07:56:54.303374Z","iopub.execute_input":"2023-04-15T07:56:54.304110Z","iopub.status.idle":"2023-04-15T07:56:54.310622Z","shell.execute_reply.started":"2023-04-15T07:56:54.304039Z","shell.execute_reply":"2023-04-15T07:56:54.309593Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"display(train.head())","metadata":{"execution":{"iopub.status.busy":"2023-04-15T07:56:56.403852Z","iopub.execute_input":"2023-04-15T07:56:56.404548Z","iopub.status.idle":"2023-04-15T07:56:56.413722Z","shell.execute_reply.started":"2023-04-15T07:56:56.404511Z","shell.execute_reply":"2023-04-15T07:56:56.412679Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import seaborn as sns\nf, ax = plt.subplots(figsize=(14, 5))\nax = sns.countplot(x=\"diagnosis\", data=train, palette=\"Set2\")\nsns.despine()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-04-15T07:56:57.812607Z","iopub.execute_input":"2023-04-15T07:56:57.813260Z","iopub.status.idle":"2023-04-15T07:56:58.149231Z","shell.execute_reply.started":"2023-04-15T07:56:57.813224Z","shell.execute_reply":"2023-04-15T07:56:58.148292Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Data Preprocessing \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":"2023-04-15T07:56:58.512789Z","iopub.execute_input":"2023-04-15T07:56:58.514693Z","iopub.status.idle":"2023-04-15T07:56:58.531487Z","shell.execute_reply.started":"2023-04-15T07:56:58.514649Z","shell.execute_reply":"2023-04-15T07:56:58.530468Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_datagen = tf.keras.preprocessing.image.ImageDataGenerator(\n        rescale=1 / 255.0,\n        horizontal_flip=True,\n        validation_split=0.20)\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=16,\n    class_mode=\"categorical\",\n    target_size=(224, 224),\n    subset='training')","metadata":{"execution":{"iopub.status.busy":"2023-04-15T07:56:59.172915Z","iopub.execute_input":"2023-04-15T07:56:59.173265Z","iopub.status.idle":"2023-04-15T07:57:00.848255Z","shell.execute_reply.started":"2023-04-15T07:56:59.173232Z","shell.execute_reply":"2023-04-15T07:57:00.847227Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"valid_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=16,\n    class_mode=\"categorical\",    \n    target_size=(224, 224),\n    subset='validation')","metadata":{"execution":{"iopub.status.busy":"2023-04-15T07:57:00.850039Z","iopub.execute_input":"2023-04-15T07:57:00.850658Z","iopub.status.idle":"2023-04-15T07:57:00.896158Z","shell.execute_reply.started":"2023-04-15T07:57:00.850618Z","shell.execute_reply":"2023-04-15T07:57:00.895159Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_datagen = ImageDataGenerator(rescale=1 / 255.0)\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=(224, 224),\n        batch_size=16,\n        shuffle=False,\n        class_mode=None)","metadata":{"execution":{"iopub.status.busy":"2023-04-15T07:57:00.899325Z","iopub.execute_input":"2023-04-15T07:57:00.899658Z","iopub.status.idle":"2023-04-15T07:57:01.719559Z","shell.execute_reply.started":"2023-04-15T07:57:00.899630Z","shell.execute_reply":"2023-04-15T07:57:01.718597Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model =tf.keras.applications.ResNet152V2(input_shape=(224,224,3),include_top=False,weights=\"imagenet\")","metadata":{"execution":{"iopub.status.busy":"2023-04-15T07:57:21.522447Z","iopub.execute_input":"2023-04-15T07:57:21.522800Z","iopub.status.idle":"2023-04-15T07:57:33.206440Z","shell.execute_reply.started":"2023-04-15T07:57:21.522769Z","shell.execute_reply":"2023-04-15T07:57:33.205453Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for layer in model.layers[:-10]:\n    layer.trainable=False","metadata":{"execution":{"iopub.status.busy":"2023-04-15T07:57:36.623026Z","iopub.execute_input":"2023-04-15T07:57:36.623428Z","iopub.status.idle":"2023-04-15T07:57:36.644504Z","shell.execute_reply.started":"2023-04-15T07:57:36.623364Z","shell.execute_reply":"2023-04-15T07:57:36.643455Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.layers import InputLayer, BatchNormalization, Dropout, Flatten, Dense, Activation, MaxPool2D \nfrom keras.models import Sequential\nfrom keras.layers import Dense,Dropout,Flatten,BatchNormalization\nfinal_model=Sequential()\nfinal_model.add(model)\nfinal_model.add(Dropout(0.5))\nfinal_model.add(Flatten())\nfinal_model.add(BatchNormalization())\nfinal_model.add(Dense(256,kernel_initializer='he_uniform'))\nfinal_model.add(BatchNormalization())\nfinal_model.add(Activation('relu'))\nfinal_model.add(Dropout(0.5))\nfinal_model.add(Dense(128,kernel_initializer='he_uniform'))\nfinal_model.add(BatchNormalization())\nfinal_model.add(Activation('relu'))\nfinal_model.add(Dropout(0.5))\nfinal_model.add(Dense(32,kernel_initializer='he_uniform'))\nfinal_model.add(BatchNormalization())\nfinal_model.add(Activation('relu'))\nfinal_model.add(Dense(2,activation='sigmoid'))","metadata":{"execution":{"iopub.status.busy":"2023-04-15T07:57:37.934135Z","iopub.execute_input":"2023-04-15T07:57:37.934811Z","iopub.status.idle":"2023-04-15T07:57:39.035418Z","shell.execute_reply.started":"2023-04-15T07:57:37.934773Z","shell.execute_reply":"2023-04-15T07:57:39.034452Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"final_model.summary()","metadata":{"execution":{"iopub.status.busy":"2023-04-15T07:57:41.706484Z","iopub.execute_input":"2023-04-15T07:57:41.706833Z","iopub.status.idle":"2023-04-15T07:57:41.734518Z","shell.execute_reply.started":"2023-04-15T07:57:41.706803Z","shell.execute_reply":"2023-04-15T07:57:41.733471Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.callbacks import EarlyStopping, ModelCheckpoint\nfrom keras.callbacks import Callback,ModelCheckpoint,ReduceLROnPlateau\nfrom keras import optimizers\nfrom tensorflow.keras.optimizers import Adam\nfrom keras.models import Sequential,load_model\nfrom keras.wrappers.scikit_learn import KerasClassifier\nlrd = ReduceLROnPlateau(monitor = 'val_loss',patience = 2,verbose = 1,factor = 0.8, min_lr = 1e-6)\n\nmcp = ModelCheckpoint('ResNet152V2.h5')\n\nes = EarlyStopping(verbose=1, patience=2)","metadata":{"execution":{"iopub.status.busy":"2023-04-15T07:57:43.503366Z","iopub.execute_input":"2023-04-15T07:57:43.504475Z","iopub.status.idle":"2023-04-15T07:57:43.512831Z","shell.execute_reply.started":"2023-04-15T07:57:43.504428Z","shell.execute_reply":"2023-04-15T07:57:43.511895Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"METRICS = [\n      tf.keras.metrics.BinaryAccuracy(name='accuracy'),\n      tf.keras.metrics.Precision(name='precision'),\n      tf.keras.metrics.Recall(name='recall'),  \n      tf.keras.metrics.AUC(name='auc'),\n]\nfinal_model.compile(optimizer='Adam', loss=\"binary_crossentropy\", metrics=[METRICS])","metadata":{"execution":{"iopub.status.busy":"2023-04-15T07:57:46.151361Z","iopub.execute_input":"2023-04-15T07:57:46.151741Z","iopub.status.idle":"2023-04-15T07:57:46.186096Z","shell.execute_reply.started":"2023-04-15T07:57:46.151709Z","shell.execute_reply":"2023-04-15T07:57:46.185171Z"},"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","metadata":{"execution":{"iopub.status.busy":"2023-04-15T07:57:47.388220Z","iopub.execute_input":"2023-04-15T07:57:47.391200Z","iopub.status.idle":"2023-04-15T07:57:47.398970Z","shell.execute_reply.started":"2023-04-15T07:57:47.391156Z","shell.execute_reply":"2023-04-15T07:57:47.397979Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(STEP_SIZE_TRAIN)\nprint(STEP_SIZE_VALID)","metadata":{"execution":{"iopub.status.busy":"2023-04-15T07:57:48.131351Z","iopub.execute_input":"2023-04-15T07:57:48.132560Z","iopub.status.idle":"2023-04-15T07:57:48.138959Z","shell.execute_reply.started":"2023-04-15T07:57:48.132500Z","shell.execute_reply":"2023-04-15T07:57:48.137804Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_generator.image_shape","metadata":{"execution":{"iopub.status.busy":"2023-04-15T07:57:49.191228Z","iopub.execute_input":"2023-04-15T07:57:49.191817Z","iopub.status.idle":"2023-04-15T07:57:49.198934Z","shell.execute_reply.started":"2023-04-15T07:57:49.191779Z","shell.execute_reply":"2023-04-15T07:57:49.197887Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%time\nhistory = final_model.fit(train_generator,steps_per_epoch=STEP_SIZE_TRAIN,validation_data=valid_generator,validation_steps=STEP_SIZE_VALID,epochs=50)","metadata":{"execution":{"iopub.status.busy":"2023-04-15T07:57:50.460714Z","iopub.execute_input":"2023-04-15T07:57:50.463686Z","iopub.status.idle":"2023-04-15T13:14:15.394017Z","shell.execute_reply.started":"2023-04-15T07:57:50.463640Z","shell.execute_reply":"2023-04-15T13:14:15.393079Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"final_model.evaluate(valid_generator)","metadata":{"execution":{"iopub.status.busy":"2023-04-15T13:23:18.416497Z","iopub.execute_input":"2023-04-15T13:23:18.416936Z","iopub.status.idle":"2023-04-15T13:24:41.927256Z","shell.execute_reply.started":"2023-04-15T13:23:18.416895Z","shell.execute_reply":"2023-04-15T13:24:41.926276Z"},"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=train,\n        directory = \"../input/aptos2019-blindness-detection/train_images/\",\n        x_col=\"id_code\",\n        target_size=(224, 224),\n        batch_size=1,\n        shuffle=False,\n        class_mode=None)","metadata":{"execution":{"iopub.status.busy":"2023-04-15T13:26:26.558542Z","iopub.execute_input":"2023-04-15T13:26:26.558908Z","iopub.status.idle":"2023-04-15T13:26:28.124417Z","shell.execute_reply.started":"2023-04-15T13:26:26.558877Z","shell.execute_reply":"2023-04-15T13:26:28.123365Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"STEP_SIZE_COMPLETE = complete_generator.n//complete_generator.batch_size\ntrain_preds = final_model.predict(complete_generator, steps=STEP_SIZE_COMPLETE)\ntrain_preds = [np.argmax(pred) for pred in train_preds]","metadata":{"execution":{"iopub.status.busy":"2023-04-15T13:31:36.354240Z","iopub.execute_input":"2023-04-15T13:31:36.355236Z","iopub.status.idle":"2023-04-15T13:38:13.331528Z","shell.execute_reply.started":"2023-04-15T13:31:36.355185Z","shell.execute_reply":"2023-04-15T13:38:13.330542Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import confusion_matrix\nlabels = ['0 - No DR', '1 - DR']\ncnf_matrix = confusion_matrix(train['diagnosis'].astype('int'), train_preds)\ncnf_matrix_norm = cnf_matrix.astype('float') / cnf_matrix.sum(axis=1)[:, np.newaxis]\ndf_cm = pd.DataFrame(cnf_matrix_norm, index=labels, columns=labels)\nprint(df_cm.describe().T)\nplt.figure(figsize=(15, 8))\nsns.heatmap(df_cm, annot=True, fmt='.2f')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-04-15T13:38:45.433194Z","iopub.execute_input":"2023-04-15T13:38:45.433569Z","iopub.status.idle":"2023-04-15T13:38:45.677369Z","shell.execute_reply.started":"2023-04-15T13:38:45.433537Z","shell.execute_reply":"2023-04-15T13:38:45.676303Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_generator.reset()\nSTEP_SIZE_TEST = test_generator.n//test_generator.batch_size\npreds = final_model.predict(test_generator, steps=STEP_SIZE_TEST,)\npredictions = [np.argmax(pred) for pred in preds]\npredictions[:10]","metadata":{"execution":{"iopub.status.busy":"2023-04-15T13:39:15.676439Z","iopub.execute_input":"2023-04-15T13:39:15.676795Z","iopub.status.idle":"2023-04-15T13:40:43.233451Z","shell.execute_reply.started":"2023-04-15T13:39:15.676764Z","shell.execute_reply":"2023-04-15T13:40:43.232472Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.head()","metadata":{"execution":{"iopub.status.busy":"2023-04-15T13:41:53.507928Z","iopub.execute_input":"2023-04-15T13:41:53.508287Z","iopub.status.idle":"2023-04-15T13:41:53.518537Z","shell.execute_reply.started":"2023-04-15T13:41:53.508255Z","shell.execute_reply":"2023-04-15T13:41:53.517578Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"filenames = test_generator.filenames\nresults = pd.DataFrame(['id_code',filenames, 'diagnosis',predictions])\nresults.to_csv('submission.csv',index=False)\nresults.head(5).T","metadata":{"execution":{"iopub.status.busy":"2023-04-15T13:43:20.785616Z","iopub.execute_input":"2023-04-15T13:43:20.785977Z","iopub.status.idle":"2023-04-15T13:43:20.808241Z","shell.execute_reply.started":"2023-04-15T13:43:20.785946Z","shell.execute_reply":"2023-04-15T13:43:20.807384Z"},"trusted":true},"execution_count":null,"outputs":[]}]}