# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# + [markdown] id="ksuAn-q-hBJA"
# # Cataract detection 

# + [markdown] id="rH_FRQY7hBJJ"
# In this notebook I have attempted to detect cataracts in an human eye.

# + [markdown] id="0i2Zt9GThBJK"
# ## Necessary libraries

# + id="pxtqIhyahBJL"
import numpy as np
import cv2
import os 
import pandas as pd
from random import sample

import seaborn as sns
import matplotlib.pyplot as plt
from scikitplot.metrics import plot_confusion_matrix as plt_con_mat

from keras.utils.np_utils import to_categorical
from sklearn.model_selection import train_test_split

from tensorflow.keras.applications.vgg19 import VGG19
from tensorflow.keras.models import Model, Sequential
from tensorflow.keras.layers import Conv2D, Dense, Dropout, MaxPooling2D, Flatten
from keras.utils import plot_model

# + [markdown] id="ODhAuzZahBJL"
# ## Loading the data

# + papermill={"duration": 0.211849, "end_time": "2020-11-27T09:59:18.425572", "exception": false, "start_time": "2020-11-27T09:59:18.213723", "status": "completed"} tags=[] id="oWu5wspwhBJM" outputId="7049824f-00c7-47ca-9ef3-747366d86003"
path = "../input/ocular-disease-recognition-odir5k"
df = pd.read_csv(os.path.join(path, "full_df.csv"))
df.head()

# + papermill={"duration": 0.136531, "end_time": "2020-11-27T09:59:18.905712", "exception": false, "start_time": "2020-11-27T09:59:18.769181", "status": "completed"} tags=[] id="3MgJxh5jhBJN" outputId="18a6c699-377f-40d1-88d2-3aab7ced1fdc"
file_names = []
labels = []

for text, label, file_name in zip(df["Left-Diagnostic Keywords"], df["C"], df["Left-Fundus"]):
    
    if(("cataract" in text) and (label == 1)):
        file_names.append(file_name)
        labels.append(1)
    
    elif(("normal fundus" in text) and (label == 0)):
        file_names.append(file_name)
        labels.append(0)
        
for text, label, file_name in zip(df["Right-Diagnostic Keywords"], df["C"], df["Right-Fundus"]):
    
    if(("cataract" in text) and (label == 1)):
        file_names.append(file_name)
        labels.append(1)
    
    elif(("normal fundus" in text) and (label == 0)):
        file_names.append(file_name)
        labels.append(0)

print(len(file_names), len(labels))

# + id="p1ATmy4QhBJO" outputId="e25ccc61-04f9-488e-c1d0-2fc93318713c"
plt.bar([0,1], [len([i for i in labels if i == 1]), len([i for i in labels if i == 0])], color = ['b', 'g'])
plt.xticks([0, 1], ['Cataract', 'Normal'])
plt.show()

# + [markdown] id="lkoz_tQohBJO"
# ## Extracting the data into train and test sets.

# + id="zmUeBnB6hBJP"
ROW = 224
COL = 224

# + id="OPzu3OUghBJP" outputId="d439eb94-c100-4382-db5d-c43b05270191"
image_data = []
for idx, image_name in enumerate(file_names):
    
    img = cv2.imread(os.path.join(path,"preprocessed_images",image_name))
    try:
        img = cv2.resize(img, (ROW, COL))
        image_data.append(img)
    except:
        del labels[idx]
    
image_data = np.array(image_data)
print(image_data.shape)

# + papermill={"duration": 6.18404, "end_time": "2020-11-27T09:59:26.792667", "exception": false, "start_time": "2020-11-27T09:59:20.608627", "status": "completed"} tags=[] id="gbKC5PR4hBJP" outputId="2ea22e64-f433-4bb0-d184-33dc19cd4010"
temp = []
for idx, label in enumerate(labels):
    if label == 0:
        temp.append(idx)

temp = sample(temp, len([label for label in labels if label == 1]))

X_data = []
y_data = []

for idx in temp:
    X_data.append(image_data[idx])
    y_data.append(labels[idx])

temp = []
for idx, label in enumerate(labels):
    if label == 1:
        temp.append(idx)
        
for idx in temp:
    X_data.append(image_data[idx])
    y_data.append(labels[idx])

X_data = np.array(X_data)
    
y_data = np.array(y_data)
y_data = np.expand_dims(y_data, axis = -1)
y_data = to_categorical(y_data)

print(X_data.shape, y_data.shape)

# + id="-1d86tTuhBJQ" outputId="76736a8c-a8e0-4817-9f2d-768abba07e56"
X_train, X_test, y_train, y_test = train_test_split(X_data, y_data, test_size=0.2, shuffle = True, random_state = 1)

print(X_train.shape, y_train.shape)
print(X_test.shape, y_test.shape)

# + [markdown] id="S0h_wV6chBJR"
# ## CNN model using VGG19

# + [markdown] id="Wlrp1UUbhBJS"
# ### Transfer learning

# + papermill={"duration": 5.914392, "end_time": "2020-11-27T09:59:44.973888", "exception": false, "start_time": "2020-11-27T09:59:39.059496", "status": "completed"} tags=[] id="CgMSmiWIhBJS" outputId="f1c2c3e9-9b13-4412-ee10-dbf6425abe27"
vgg = VGG19(weights = "imagenet", include_top = False, input_shape=(ROW, COL, 3))
for layer in vgg.layers:
    layer.trainable = False

# + papermill={"duration": 0.318562, "end_time": "2020-11-27T09:59:46.085091", "exception": false, "start_time": "2020-11-27T09:59:45.766529", "status": "completed"} tags=[] id="Alt_fBMJhBJT" outputId="c318da4b-b232-4eff-ee59-c355792ce610"
model = Sequential()
model.add(vgg)
model.add(Flatten())
model.add(Dense(64, activation = 'relu'))
model.add(Dense(2,activation = "softmax"))

model.summary()

# + papermill={"duration": 0.247418, "end_time": "2020-11-27T09:59:46.547409", "exception": false, "start_time": "2020-11-27T09:59:46.299991", "status": "completed"} tags=[] id="NlsEFCSWhBJT" outputId="e3c63d9e-279f-402d-b2c7-d88c13be5bbe"
plot_model(model, show_shapes=True, show_layer_names=True)

# + [markdown] id="GipcxmjAhBJT"
# ## Training the CNN model

# + papermill={"duration": 0.236221, "end_time": "2020-11-27T09:59:46.982157", "exception": false, "start_time": "2020-11-27T09:59:46.745936", "status": "completed"} tags=[] id="oCJFEQsQhBJU" outputId="10c494b8-4728-4319-cf4e-1f258a93e708"
model.compile(optimizer = 'adam', 
              loss = 'categorical_crossentropy', 
              metrics=['accuracy', 'Precision', 'Recall'])

history = model.fit(X_train, y_train, 
                    validation_data = (X_test, y_test), 
                    epochs = 15,
                    batch_size = 64)

# + [markdown] id="b9XiSW97hBJU"
# ## Model training performance

# + papermill={"duration": 0.316185, "end_time": "2020-11-27T10:00:43.086059", "exception": false, "start_time": "2020-11-27T10:00:42.769874", "status": "completed"} tags=[] id="8SwRKlAthBJU" outputId="974735b6-d26c-4101-e27d-5e3bb32769b2"
sns.set()
fig = plt.figure(0, (12, 4))

ax = plt.subplot(1, 2, 1)
sns.lineplot(history.epoch, history.history['accuracy'], label = 'train')
sns.lineplot(history.epoch, history.history['val_accuracy'], label = 'validation')
plt.title('Accuracy')
plt.tight_layout()

ax = plt.subplot(1, 2, 2)
sns.lineplot(history.epoch, history.history['loss'], label = 'train')
sns.lineplot(history.epoch, history.history['val_loss'], label = 'validation')
plt.title('Loss')
plt.tight_layout()

#plt.savefig('epoch_history.png')
plt.show()

# + id="NTPHfa2OhBJV" outputId="cbb4da9e-cf4a-47e9-fba5-3031fa426940"
preds = model.predict_classes(X_test)
y_true = np.argmax(y_test, axis=1)

plt_con_mat(y_true, preds, figsize=(14,14))
plt.show()

# + id="CAonT78NhBJV"
import tensorflow as tf
tf.keras.models.save_model(model,'model.h5')

StarcoderdataJupyterScriptsDedupFiltered