{"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":"import numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nfrom sklearn.model_selection import train_test_split\nimport cv2\nimport tensorflow as tf\nfrom tensorflow.keras.layers import Conv2D, Dense, MaxPool2D, Dropout, Flatten, GlobalAveragePooling2D","metadata":{"_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","papermill":{"duration":5.991311,"end_time":"2022-11-06T10:40:38.023409","exception":false,"start_time":"2022-11-06T10:40:32.032098","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-11-06T19:22:41.931139Z","iopub.execute_input":"2022-11-06T19:22:41.931653Z","iopub.status.idle":"2022-11-06T19:22:50.322691Z","shell.execute_reply.started":"2022-11-06T19:22:41.931553Z","shell.execute_reply":"2022-11-06T19:22:50.321462Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# constants\nDATA_DIR = '../input/aptos2019-blindness-detection'\nBATCH_SIZE = 32\nEPOCHS = 100\nSEED = 420\nIMG_SIZE = (224, 224)\nINPUT_SHAPE = (224, 224, 3)\nBASE_LR = 0.001\nNUM_CLASSES = 5","metadata":{"papermill":{"duration":0.011821,"end_time":"2022-11-06T10:40:38.039824","exception":false,"start_time":"2022-11-06T10:40:38.028003","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-11-06T19:22:58.033225Z","iopub.execute_input":"2022-11-06T19:22:58.033879Z","iopub.status.idle":"2022-11-06T19:22:58.040260Z","shell.execute_reply.started":"2022-11-06T19:22:58.033844Z","shell.execute_reply":"2022-11-06T19:22:58.039053Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# importing data\ntrain_df = pd.read_csv('/kaggle/input/aptos2019-blindness-detection/train.csv')\ntest_df = pd.read_csv('/kaggle/input/aptos2019-blindness-detection/test.csv')","metadata":{"papermill":{"duration":0.029963,"end_time":"2022-11-06T10:40:38.073393","exception":false,"start_time":"2022-11-06T10:40:38.043430","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-11-06T19:23:03.492415Z","iopub.execute_input":"2022-11-06T19:23:03.492833Z","iopub.status.idle":"2022-11-06T19:23:03.525918Z","shell.execute_reply.started":"2022-11-06T19:23:03.492800Z","shell.execute_reply":"2022-11-06T19:23:03.524291Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-11-06T19:23:35.797334Z","iopub.execute_input":"2022-11-06T19:23:35.797759Z","iopub.status.idle":"2022-11-06T19:23:35.814115Z","shell.execute_reply.started":"2022-11-06T19:23:35.797727Z","shell.execute_reply":"2022-11-06T19:23:35.812755Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# to display images\ndef display_image(df, rows, columns):\n    fig=plt.figure(figsize=(10, 10))\n\n    for i in range(columns*rows):\n        image_path = df.loc[i,'id_code']\n        image_id = df.loc[i,'diagnosis']\n        img = cv2.imread(f'/kaggle/input/aptos2019-blindness-detection/train_images/{image_path}.png')\n        img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)\n        \n        fig.add_subplot(rows, columns, i+1)\n        plt.title(image_id)\n        plt.axis('off')\n        plt.imshow(img)\n    \n    plt.tight_layout()\n    plt.show()\n    \ndef display_single_image(img):\n    fig=plt.figure(figsize=(10, 10))\n    plt.title('Sample Img')\n    plt.imshow(img)\n    plt.show()\n    \ndisplay_image(train_df, 4, 4)","metadata":{"execution":{"iopub.execute_input":"2022-11-06T10:40:38.082295Z","iopub.status.busy":"2022-11-06T10:40:38.081574Z","iopub.status.idle":"2022-11-06T10:40:47.923270Z","shell.execute_reply":"2022-11-06T10:40:47.922336Z"},"papermill":{"duration":9.865334,"end_time":"2022-11-06T10:40:47.942531","exception":false,"start_time":"2022-11-06T10:40:38.077197","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# image pre-pocessing helper function\ndef load_ben_color(path):\n    image = cv2.imread(path)\n    image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)\n    image = cv2.resize(image, IMG_SIZE)\n    return image\n\ndef preprocess_image(image_path):\n    img = load_ben_color(image_path)\n    return img\n\nx_train = np.empty((train_df.shape[0], 224, 224, 3), dtype=np.uint8)\nfor i, image_id in enumerate((train_df['id_code'])):\n    x_train[i, :, :, :] = preprocess_image(f'/kaggle/input/aptos2019-blindness-detection/train_images/{image_id}.png')\n\nx_test = np.empty((test_df.shape[0], 224, 224, 3), dtype=np.uint8)\nfor i, image_id in enumerate((test_df['id_code'])):\n    x_test[i, :, :, :] = preprocess_image(f'/kaggle/input/aptos2019-blindness-detection/test_images/{image_id}.png')\n\n# get one-hot encoded diagnosis\ny_train = pd.get_dummies(train_df['diagnosis']).values","metadata":{"execution":{"iopub.execute_input":"2022-11-06T10:40:47.979048Z","iopub.status.busy":"2022-11-06T10:40:47.977816Z","iopub.status.idle":"2022-11-06T10:48:49.289642Z","shell.execute_reply":"2022-11-06T10:48:49.288412Z"},"papermill":{"duration":481.332523,"end_time":"2022-11-06T10:48:49.292700","exception":false,"start_time":"2022-11-06T10:40:47.960177","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"x_train.shape=\",x_train.shape)\nprint(\"y_train.shape=\",y_train.shape)\nprint(\"x_test.shape=\",x_test.shape)","metadata":{"execution":{"iopub.execute_input":"2022-11-06T10:48:49.328545Z","iopub.status.busy":"2022-11-06T10:48:49.328221Z","iopub.status.idle":"2022-11-06T10:48:49.337201Z","shell.execute_reply":"2022-11-06T10:48:49.335925Z"},"papermill":{"duration":0.033358,"end_time":"2022-11-06T10:48:49.342081","exception":false,"start_time":"2022-11-06T10:48:49.308723","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# to balance dataset using SMOTE\nfrom imblearn.over_sampling import SMOTE\nx_resampled, y_resampled = SMOTE(random_state=SEED).fit_resample(x_train.reshape(x_train.shape[0], -1), train_df['diagnosis'].ravel())\n\nx_train = x_resampled.reshape(x_resampled.shape[0], 224, 224, 3)\ny_train = pd.get_dummies(y_resampled).values\n\nprint(\"x_train.shape=\",x_train.shape)\nprint(\"y_train.shape=\",y_train.shape)","metadata":{"execution":{"iopub.execute_input":"2022-11-06T10:48:49.372244Z","iopub.status.busy":"2022-11-06T10:48:49.371911Z","iopub.status.idle":"2022-11-06T10:49:03.298831Z","shell.execute_reply":"2022-11-06T10:49:03.297755Z"},"papermill":{"duration":13.942838,"end_time":"2022-11-06T10:49:03.301332","exception":false,"start_time":"2022-11-06T10:48:49.358494","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# splitting data into training and validation data\nx_sptrain, x_spval, y_sptrain, y_spval = train_test_split(\n    x_train, y_train, \n    test_size=0.10, \n    random_state=SEED\n)","metadata":{"execution":{"iopub.execute_input":"2022-11-06T10:49:03.327673Z","iopub.status.busy":"2022-11-06T10:49:03.327311Z","iopub.status.idle":"2022-11-06T10:49:03.733422Z","shell.execute_reply":"2022-11-06T10:49:03.732256Z"},"papermill":{"duration":0.423308,"end_time":"2022-11-06T10:49:03.736451","exception":false,"start_time":"2022-11-06T10:49:03.313143","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# importing inceptionv3 from tensorflow\nfrom tensorflow.keras.applications.inception_v3 import InceptionV3, preprocess_input","metadata":{"papermill":{"duration":0.021324,"end_time":"2022-11-06T10:49:03.769616","exception":false,"start_time":"2022-11-06T10:49:03.748292","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-11-06T19:59:31.178362Z","iopub.execute_input":"2022-11-06T19:59:31.178783Z","iopub.status.idle":"2022-11-06T19:59:31.184908Z","shell.execute_reply.started":"2022-11-06T19:59:31.178751Z","shell.execute_reply":"2022-11-06T19:59:31.183679Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"m= InceptionV3()\nm.summary()","metadata":{"execution":{"iopub.status.busy":"2022-11-06T20:00:41.902648Z","iopub.execute_input":"2022-11-06T20:00:41.903081Z","iopub.status.idle":"2022-11-06T20:00:44.857808Z","shell.execute_reply.started":"2022-11-06T20:00:41.903030Z","shell.execute_reply":"2022-11-06T20:00:44.856566Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import *\n\nimport tensorflow.keras.backend as K\n\ndef precision(y_true, y_pred):\n    true_positives = K.sum(K.round(K.clip(y_true * y_pred, 0, 1)))\n    predicted_positives = K.sum(K.round(K.clip(y_pred, 0, 1)))\n    precision = true_positives / (predicted_positives + K.epsilon())\n    return precision\n\ndef recall(y_true, y_pred):\n    true_positives = K.sum(K.round(K.clip(y_true * y_pred, 0, 1)))\n    possible_positives = K.sum(K.round(K.clip(y_true, 0, 1)))\n    recall = true_positives / (possible_positives + K.epsilon())\n    return recall\n\ndef fbeta_score(y_true, y_pred, beta=1):\n    if beta < 0:\n        raise ValueError('The lowest choosable beta is zero (only precision).')\n\n    # If there are no true positives, fix the F score at 0 like sklearn.\n    if K.sum(K.round(K.clip(y_true, 0, 1))) == 0:\n        return 0.0\n\n    p = precision(y_true, y_pred)\n    r = recall(y_true, y_pred)\n    bb = beta ** 2\n    fbeta_score = (1 + bb) * (p * r) / (bb * p + r + K.epsilon())\n    return fbeta_score\n\ndef fmeasure(y_true, y_pred):\n    return fbeta_score(y_true, y_pred, beta=1)\n\ndef mean_pred(y_true, y_pred):\n    return K.mean(y_pred)\n\ndef f1_score(y_true, y_pred):\n    p = precision(y_true, y_pred)\n    r = recall(y_true, y_pred)\n    return 2*(p*r) / (p+r+K.epsilon())","metadata":{"execution":{"iopub.execute_input":"2022-11-06T10:49:03.794850Z","iopub.status.busy":"2022-11-06T10:49:03.793633Z","iopub.status.idle":"2022-11-06T10:49:03.811584Z","shell.execute_reply":"2022-11-06T10:49:03.810610Z"},"papermill":{"duration":0.03307,"end_time":"2022-11-06T10:49:03.813942","exception":false,"start_time":"2022-11-06T10:49:03.780872","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# using InceptionV3 building a model for training\ninceptionv3 = InceptionV3(include_top=False, weights='imagenet', input_shape=INPUT_SHAPE)\ninceptionv3.trainable = False\n\ninputs = tf.keras.Input(shape=INPUT_SHAPE)\n\nx = preprocess_input(inputs)\nx = inceptionv3(x, training = False)\nx = GlobalAveragePooling2D()(x)\n\nx = Dense(100, activation = 'relu')(x)\nx = Dropout(0.3)(x)\n\noutputs = Dense(NUM_CLASSES, activation = 'softmax')(x)\n\nmodel = tf.keras.Model(inputs, outputs)\n\nmodel.compile(optimizer=tf.keras.optimizers.Adam(learning_rate=BASE_LR),\n              loss='categorical_crossentropy',\n              metrics=['categorical_accuracy', mean_pred, precision, recall, f1_score, fbeta_score]\n             )\nmodel.summary()","metadata":{"execution":{"iopub.execute_input":"2022-11-06T10:49:03.838302Z","iopub.status.busy":"2022-11-06T10:49:03.837984Z","iopub.status.idle":"2022-11-06T10:49:09.874975Z","shell.execute_reply":"2022-11-06T10:49:09.873762Z"},"papermill":{"duration":6.053038,"end_time":"2022-11-06T10:49:09.878254","exception":false,"start_time":"2022-11-06T10:49:03.825216","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# callbacks\nreduce_lr = tf.keras.callbacks.ReduceLROnPlateau(monitor='val_loss', factor=0.2, patience=8, verbose=1)\nearly_stopping = tf.keras.callbacks.EarlyStopping(monitor = 'val_loss', patience=24, verbose=1)\nckpt_path = './training/model.ckpt'\nmc = tf.keras.callbacks.ModelCheckpoint(ckpt_path, save_weights_only=True, monitor='val_categorical_accuracy', mode='max', verbose=1, save_best_only=True)\n\nhistory = model.fit(x_sptrain, y_sptrain, epochs=EPOCHS, validation_data=(x_spval, y_spval), callbacks = [reduce_lr, early_stopping, mc])","metadata":{"execution":{"iopub.execute_input":"2022-11-06T10:49:09.923636Z","iopub.status.busy":"2022-11-06T10:49:09.923136Z","iopub.status.idle":"2022-11-06T10:59:48.408661Z","shell.execute_reply":"2022-11-06T10:59:48.407739Z"},"papermill":{"duration":639.008152,"end_time":"2022-11-06T10:59:48.911091","exception":false,"start_time":"2022-11-06T10:49:09.902939","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# function to plot training results\ndef training_results(history):\n    acc = history.history['categorical_accuracy']\n    val_acc = history.history['val_categorical_accuracy']\n\n    loss = history.history['loss']\n    val_loss = history.history['val_loss']\n\n    epochs_range = range(len(loss))\n\n    plt.figure(figsize=(8, 8))\n    plt.subplot(211)\n    plt.plot(epochs_range, acc, label='Training Accuracy')\n    plt.plot(epochs_range, val_acc, label='Validation Accuracy')\n    plt.legend(loc='lower right')\n    plt.title('Training and Validation Accuracy')\n\n    plt.subplot(212)\n    plt.plot(epochs_range, loss, label='Training Loss')\n    plt.plot(epochs_range, val_loss, label='Validation Loss')\n    plt.legend(loc='upper right')\n    plt.title('Training and Validation Loss')\n    plt.show()","metadata":{"execution":{"iopub.execute_input":"2022-11-06T10:59:49.840748Z","iopub.status.busy":"2022-11-06T10:59:49.840298Z","iopub.status.idle":"2022-11-06T10:59:49.847845Z","shell.execute_reply":"2022-11-06T10:59:49.846891Z"},"papermill":{"duration":0.383148,"end_time":"2022-11-06T10:59:49.849833","exception":false,"start_time":"2022-11-06T10:59:49.466685","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# plotting training results\ntraining_results(history)","metadata":{"execution":{"iopub.execute_input":"2022-11-06T10:59:50.596369Z","iopub.status.busy":"2022-11-06T10:59:50.596004Z","iopub.status.idle":"2022-11-06T11:00:39.206606Z","shell.execute_reply":"2022-11-06T11:00:39.205638Z"},"papermill":{"duration":49.414583,"end_time":"2022-11-06T11:00:39.633074","exception":false,"start_time":"2022-11-06T10:59:50.218491","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history_df = pd.DataFrame(history.history)\nhistory_df.head(EPOCHS)","metadata":{"execution":{"iopub.execute_input":"2022-11-06T11:00:40.481595Z","iopub.status.busy":"2022-11-06T11:00:40.481150Z","iopub.status.idle":"2022-11-06T11:00:40.552239Z","shell.execute_reply":"2022-11-06T11:00:40.551226Z"},"papermill":{"duration":0.554903,"end_time":"2022-11-06T11:00:40.555264","exception":false,"start_time":"2022-11-06T11:00:40.000361","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_val_pred = model.predict(x_spval)","metadata":{"execution":{"iopub.execute_input":"2022-11-06T11:00:41.699816Z","iopub.status.busy":"2022-11-06T11:00:41.699414Z","iopub.status.idle":"2022-11-06T11:00:44.229385Z","shell.execute_reply":"2022-11-06T11:00:44.228326Z"},"papermill":{"duration":3.029219,"end_time":"2022-11-06T11:00:44.232435","exception":false,"start_time":"2022-11-06T11:00:41.203216","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# confusion matrix\ncm = confusion_matrix(np.argmax(y_spval, axis=1), np.argmax(y_val_pred, axis=1))\ndisp = ConfusionMatrixDisplay(confusion_matrix=cm)\ndisp.plot()\nplt.show()","metadata":{"execution":{"iopub.execute_input":"2022-11-06T11:00:45.035215Z","iopub.status.busy":"2022-11-06T11:00:45.034809Z","iopub.status.idle":"2022-11-06T11:00:45.302849Z","shell.execute_reply":"2022-11-06T11:00:45.301910Z"},"papermill":{"duration":0.643661,"end_time":"2022-11-06T11:00:45.304860","exception":false,"start_time":"2022-11-06T11:00:44.661199","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"papermill":{"duration":0.417214,"end_time":"2022-11-06T11:00:46.094178","exception":false,"start_time":"2022-11-06T11:00:45.676964","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]}]}