{"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-22T19:21:10.983951Z","iopub.execute_input":"2023-04-22T19:21:10.984361Z","iopub.status.idle":"2023-04-22T19:21:24.388330Z","shell.execute_reply.started":"2023-04-22T19:21:10.984323Z","shell.execute_reply":"2023-04-22T19:21:24.387046Z"},"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-22T19:21:24.390531Z","iopub.execute_input":"2023-04-22T19:21:24.391203Z","iopub.status.idle":"2023-04-22T19:21:24.425670Z","shell.execute_reply.started":"2023-04-22T19:21:24.391170Z","shell.execute_reply":"2023-04-22T19:21:24.424614Z"},"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-22T19:21:24.427052Z","iopub.execute_input":"2023-04-22T19:21:24.427857Z","iopub.status.idle":"2023-04-22T19:21:24.446095Z","shell.execute_reply.started":"2023-04-22T19:21:24.427814Z","shell.execute_reply":"2023-04-22T19:21:24.444926Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.head(10)","metadata":{"execution":{"iopub.status.busy":"2023-04-22T19:21:24.449326Z","iopub.execute_input":"2023-04-22T19:21:24.450308Z","iopub.status.idle":"2023-04-22T19:21:24.470594Z","shell.execute_reply.started":"2023-04-22T19:21:24.450261Z","shell.execute_reply":"2023-04-22T19:21:24.469494Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.head()","metadata":{"execution":{"iopub.status.busy":"2023-04-22T19:21:24.471692Z","iopub.execute_input":"2023-04-22T19:21:24.472721Z","iopub.status.idle":"2023-04-22T19:21:24.481485Z","shell.execute_reply.started":"2023-04-22T19:21:24.472686Z","shell.execute_reply":"2023-04-22T19:21:24.480380Z"},"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-22T19:21:24.483064Z","iopub.execute_input":"2023-04-22T19:21:24.483414Z","iopub.status.idle":"2023-04-22T19:21:24.491956Z","shell.execute_reply.started":"2023-04-22T19:21:24.483383Z","shell.execute_reply":"2023-04-22T19:21:24.490810Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"display(train.head())","metadata":{"execution":{"iopub.status.busy":"2023-04-22T19:21:24.493202Z","iopub.execute_input":"2023-04-22T19:21:24.494035Z","iopub.status.idle":"2023-04-22T19:21:24.505418Z","shell.execute_reply.started":"2023-04-22T19:21:24.494000Z","shell.execute_reply":"2023-04-22T19:21:24.504269Z"},"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-22T19:21:24.507154Z","iopub.execute_input":"2023-04-22T19:21:24.508049Z","iopub.status.idle":"2023-04-22T19:21:24.983319Z","shell.execute_reply.started":"2023-04-22T19:21:24.508001Z","shell.execute_reply":"2023-04-22T19:21:24.982409Z"},"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-22T19:21:24.984916Z","iopub.execute_input":"2023-04-22T19:21:24.985819Z","iopub.status.idle":"2023-04-22T19:21:25.004298Z","shell.execute_reply.started":"2023-04-22T19:21:24.985770Z","shell.execute_reply":"2023-04-22T19:21:25.003052Z"},"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-22T19:21:25.010177Z","iopub.execute_input":"2023-04-22T19:21:25.010870Z","iopub.status.idle":"2023-04-22T19:21:26.348917Z","shell.execute_reply.started":"2023-04-22T19:21:25.010829Z","shell.execute_reply":"2023-04-22T19:21:26.347577Z"},"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-22T19:21:26.350630Z","iopub.execute_input":"2023-04-22T19:21:26.351482Z","iopub.status.idle":"2023-04-22T19:21:26.443858Z","shell.execute_reply.started":"2023-04-22T19:21:26.351433Z","shell.execute_reply":"2023-04-22T19:21:26.442954Z"},"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-22T19:21:26.445391Z","iopub.execute_input":"2023-04-22T19:21:26.446129Z","iopub.status.idle":"2023-04-22T19:21:26.897384Z","shell.execute_reply.started":"2023-04-22T19:21:26.446084Z","shell.execute_reply":"2023-04-22T19:21:26.895888Z"},"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-22T19:21:26.899417Z","iopub.execute_input":"2023-04-22T19:21:26.900304Z","iopub.status.idle":"2023-04-22T19:21:34.686885Z","shell.execute_reply.started":"2023-04-22T19:21:26.900254Z","shell.execute_reply":"2023-04-22T19:21:34.685634Z"},"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-22T19:21:34.688189Z","iopub.execute_input":"2023-04-22T19:21:34.688525Z","iopub.status.idle":"2023-04-22T19:21:34.712142Z","shell.execute_reply.started":"2023-04-22T19:21:34.688491Z","shell.execute_reply":"2023-04-22T19:21:34.710774Z"},"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-22T19:21:34.714144Z","iopub.execute_input":"2023-04-22T19:21:34.714671Z","iopub.status.idle":"2023-04-22T19:21:36.494396Z","shell.execute_reply.started":"2023-04-22T19:21:34.714625Z","shell.execute_reply":"2023-04-22T19:21:36.493191Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"final_model.summary()","metadata":{"execution":{"iopub.status.busy":"2023-04-22T19:21:36.495586Z","iopub.execute_input":"2023-04-22T19:21:36.495905Z","iopub.status.idle":"2023-04-22T19:21:36.582936Z","shell.execute_reply.started":"2023-04-22T19:21:36.495874Z","shell.execute_reply":"2023-04-22T19:21:36.581887Z"},"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-22T19:21:36.584295Z","iopub.execute_input":"2023-04-22T19:21:36.584667Z","iopub.status.idle":"2023-04-22T19:21:36.594111Z","shell.execute_reply.started":"2023-04-22T19:21:36.584620Z","shell.execute_reply":"2023-04-22T19:21:36.592751Z"},"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-22T19:21:36.595640Z","iopub.execute_input":"2023-04-22T19:21:36.595993Z","iopub.status.idle":"2023-04-22T19:21:36.649543Z","shell.execute_reply.started":"2023-04-22T19:21:36.595959Z","shell.execute_reply":"2023-04-22T19:21:36.648518Z"},"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-22T19:21:36.650842Z","iopub.execute_input":"2023-04-22T19:21:36.651260Z","iopub.status.idle":"2023-04-22T19:21:36.657515Z","shell.execute_reply.started":"2023-04-22T19:21:36.651217Z","shell.execute_reply":"2023-04-22T19:21:36.656306Z"},"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-22T19:21:36.659432Z","iopub.execute_input":"2023-04-22T19:21:36.659870Z","iopub.status.idle":"2023-04-22T19:21:36.665760Z","shell.execute_reply.started":"2023-04-22T19:21:36.659825Z","shell.execute_reply":"2023-04-22T19:21:36.664528Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_generator.image_shape","metadata":{"execution":{"iopub.status.busy":"2023-04-22T19:21:36.666998Z","iopub.execute_input":"2023-04-22T19:21:36.667301Z","iopub.status.idle":"2023-04-22T19:21:36.679246Z","shell.execute_reply.started":"2023-04-22T19:21:36.667267Z","shell.execute_reply":"2023-04-22T19:21:36.678116Z"},"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-22T19:21:36.680652Z","iopub.execute_input":"2023-04-22T19:21:36.681079Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"final_model.evaluate(valid_generator)","metadata":{"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":{"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":{"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":{"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":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.head()","metadata":{"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":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"final_model.save(\"Diabetic Retinopathy model.h5\")","metadata":{},"execution_count":null,"outputs":[]}]}