{"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# 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\nimport tensorflow as tf\nimport seaborn as sns\nimport matplotlib.pyplot as plt\nimport os\n\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-05T09:47:34.564396Z","iopub.execute_input":"2023-04-05T09:47:34.564947Z","iopub.status.idle":"2023-04-05T09:47:42.689325Z","shell.execute_reply.started":"2023-04-05T09:47:34.564906Z","shell.execute_reply":"2023-04-05T09:47:42.688235Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def append_ext(fn):\n    return fn+\".jpeg\"\n\ndataframe = pd.read_csv(\"/kaggle/input/diabetic-retinopathy-resized/trainLabels.csv\",dtype=str)\ndataframe['image'] = dataframe['image'].apply(append_ext)\ndataframe.head()","metadata":{"execution":{"iopub.status.busy":"2023-04-05T09:47:42.691245Z","iopub.execute_input":"2023-04-05T09:47:42.692011Z","iopub.status.idle":"2023-04-05T09:47:42.829668Z","shell.execute_reply.started":"2023-04-05T09:47:42.691971Z","shell.execute_reply":"2023-04-05T09:47:42.828539Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\nX = dataframe['image']\ny = dataframe['level']\nX_train, X_valid, y_train, y_valid = train_test_split(X, y, test_size=0.1, random_state=42, shuffle=True)\nX_train, X_test, y_train, y_test = train_test_split(X_train, y_train, test_size=0.1, random_state=42, shuffle=True)\ntrain_df = pd.DataFrame({'image': X_train, 'level': y_train}).reset_index(drop=True)\nvalid_df = pd.DataFrame({'image': X_valid, 'level': y_valid}).reset_index(drop=True)\ntest_df = pd.DataFrame({'image': X_test, 'level': y_test}).reset_index(drop=True)\n","metadata":{"execution":{"iopub.status.busy":"2023-04-05T09:47:42.831186Z","iopub.execute_input":"2023-04-05T09:47:42.832081Z","iopub.status.idle":"2023-04-05T09:47:43.134525Z","shell.execute_reply.started":"2023-04-05T09:47:42.832050Z","shell.execute_reply":"2023-04-05T09:47:43.133507Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_datagen = tf.keras.preprocessing.image.ImageDataGenerator(rescale=1 / 255.0,\n                                                               rotation_range = 10, \n                                                               zoom_range = 0.30, \n                                                               shear_range = 0.30,\n                                                               fill_mode = \"nearest\"\n                                                               )\ntest_datagen = tf.keras.preprocessing.image.ImageDataGenerator(rescale=1 / 255.0)","metadata":{"execution":{"iopub.status.busy":"2023-04-05T09:47:43.136946Z","iopub.execute_input":"2023-04-05T09:47:43.137252Z","iopub.status.idle":"2023-04-05T09:47:43.144478Z","shell.execute_reply.started":"2023-04-05T09:47:43.137223Z","shell.execute_reply":"2023-04-05T09:47:43.143283Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"images_directory = \"/kaggle/input/diabetic-retinopathy-resized/resized_train/resized_train/\"\ntrain_generator = train_datagen.flow_from_dataframe(dataframe = train_df,\n                                                    directory = images_directory,\n                                                    x_col = 'image',\n                                                    y_col = 'level',\n                                                    target_size=(224, 224),\n                                                    color_mode='rgb',\n                                                    class_mode='categorical',\n                                                    batch_size=16)\nvalid_generator = test_datagen.flow_from_dataframe(dataframe = valid_df,\n                                                    directory = images_directory,\n                                                    x_col = 'image',\n                                                    y_col = 'level',\n                                                    target_size=(224, 224),\n                                                    color_mode='rgb',\n                                                    class_mode='categorical',\n                                                    batch_size=16)\ntest_generator = test_datagen.flow_from_dataframe(dataframe = test_df,\n                                                    directory = images_directory,\n                                                    x_col = 'image',\n                                                    y_col = 'level',\n                                                    target_size=(224, 224),\n                                                    color_mode='rgb',\n                                                    class_mode='categorical',\n                                                    batch_size=16)","metadata":{"execution":{"iopub.status.busy":"2023-04-05T09:47:43.146186Z","iopub.execute_input":"2023-04-05T09:47:43.146854Z","iopub.status.idle":"2023-04-05T09:48:53.651300Z","shell.execute_reply.started":"2023-04-05T09:47:43.146817Z","shell.execute_reply":"2023-04-05T09:48:53.650070Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"EfficientNet = tf.keras.applications.efficientnet.EfficientNetB3(\n                                                        include_top=False,\n                                                        weights=None,\n                                                        pooling='avg')\nfor layer in EfficientNet.layers:\n    layer.trainable = True\n    \nlast_output = EfficientNet.output\ndense_output = tf.keras.layers.Dense(64, activation='relu')(last_output)\ndense_output = tf.keras.layers.BatchNormalization()(dense_output)\ndense_output = tf.keras.layers.Dropout(0.5)(dense_output)\nx = tf.keras.layers.Dense(5, activation='softmax', name='softmax')(dense_output)\nmodel = tf.keras.models.Model(inputs=EfficientNet.input, outputs=x)","metadata":{"execution":{"iopub.status.busy":"2023-04-05T09:48:53.653387Z","iopub.execute_input":"2023-04-05T09:48:53.654034Z","iopub.status.idle":"2023-04-05T09:48:58.992052Z","shell.execute_reply.started":"2023-04-05T09:48:53.653992Z","shell.execute_reply":"2023-04-05T09:48:58.990915Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.optimizers import Adam\nepochs = 10\nlearning_rate = 0.0001\nopt = Adam(learning_rate=learning_rate, decay=learning_rate / (epochs * 0.5))\nmodel.compile(loss='categorical_crossentropy', optimizer=opt, metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2023-04-05T09:48:58.993618Z","iopub.execute_input":"2023-04-05T09:48:58.993970Z","iopub.status.idle":"2023-04-05T09:48:59.023092Z","shell.execute_reply.started":"2023-04-05T09:48:58.993933Z","shell.execute_reply":"2023-04-05T09:48:59.022182Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model.fit(train_generator,\n                    validation_data = valid_generator,\n                    steps_per_epoch = train_generator.n//train_generator.batch_size,\n                    validation_steps = valid_generator.n//valid_generator.batch_size,epochs=10)","metadata":{"execution":{"iopub.status.busy":"2023-04-05T09:48:59.024627Z","iopub.execute_input":"2023-04-05T09:48:59.025007Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# summarize history for loss\nplt.plot(history.history['loss'])\nplt.plot(history.history['val_loss'])\nplt.title('model loss')\nplt.ylabel('loss')\nplt.xlabel('epoch')\nplt.legend(['train', 'validation'], loc='upper left')\nplt.show()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"score = model.evaluate(test_generator)\nprint('Test loss:', score[0])\nprint('Test accuracy:', score[1])","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_predictions = np.argmax(model.predict(test_generator),axis=1)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import classification_report\nprint(classification_report(test_generator.classes, model_predictions))","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import confusion_matrix\ncnf_matrix = confusion_matrix(test_generator.classes, model_predictions)\nax= plt.subplot()\nsns.heatmap(cnf_matrix, annot=True, fmt='g', ax=ax);\n\n# labels, title and ticks\nax.set_xlabel('Predicted labels');\nax.set_ylabel('True labels'); \nax.set_title('Confusion Matrix');\nax.xaxis.set_ticklabels(['No_DR', 'Mild','Moderate','Severe','Proliferative_DR']); \nax.yaxis.set_ticklabels(['No_DR', 'Mild','Moderate','Severe','Proliferative_DR']);","metadata":{"trusted":true},"execution_count":null,"outputs":[]}]}