<jupyter_start><jupyter_text>Data Pre-Processing and Augmentation using Keras ImageDataGenerator<jupyter_code>import tensorflow as tf 

datagen = tf.keras.preprocessing.image.ImageDataGenerator(rescale = 1./255., 
                                                          rotation_range = 8, 
                                                          width_shift_range = 0.1, 
                                                          height_shift_range = 0.1,
                                                          shear_range = 0.2, 
                                                          zoom_range = 0.2,
                                                          brightness_range = [0.5,1.5],
                                                          horizontal_flip = True,
                                                          fill_mode = 'nearest',
                                                          dtype = None,                                                      
                                                          validation_split = 0.075)<jupyter_output><empty_output><jupyter_text>Defining generators for training and validation set<jupyter_code>dire = '/content/gdrive/MyDrive/Eye Diseases'

train_generator = datagen.flow_from_directory(
    dire,
    subset="training",
    batch_size=32,
    class_mode='categorical',     
    shuffle=True,                      
    target_size=(200,200)
)

valid_generator = datagen.flow_from_directory(
    dire,
    subset="validation",
    shuffle=True,
    batch_size=32,
    class_mode='categorical',                           
    target_size=(200,200)
)
for image_batch, label_batch in train_generator:
  break
image_batch.shape, label_batch.shape<jupyter_output><empty_output><jupyter_text>Deducing and saving labels<jupyter_code>label_batch[0:5]
print(train_generator.class_indices)
labels = '\n'.join(sorted(train_generator.class_indices.keys()))
with open('labels.txt', 'w') as f:
 f.write(labels)
!cat labels.txt<jupyter_output>{'Arcus Senilis': 0, 'Cataracts': 1, 'Endophthalmitis': 2, 'Hemangioma': 3, 'Hyphema': 4}
Arcus Senilis
Cataracts
Endophthalmitis
Hemangioma
Hyphema<jupyter_text>Retrieving vgg16 pretrained vgg weights<jupyter_code>import os
import urllib.request

urllib.request.urlretrieve('https://github.com/fchollet/deep-learning-models/releases/download/v0.1/vgg16_weights_tf_dim_ordering_tf_kernels_notop.h5',
                           'vgg16_weights_tf_dim_ordering_tf_kernels_notop.h5')
os.listdir()<jupyter_output><empty_output><jupyter_text>Initializing vgg model with downloaded, pretrained weights and input shape of 200,200,3 because of the small image size in the dataset<jupyter_code>from tensorflow.keras import layers 
from tensorflow.keras import Model 

from tensorflow.keras.applications import ResNet50
from tensorflow.python.keras.models import Sequential
from tensorflow.python.keras.layers import Dense, Flatten, GlobalAveragePooling2D

from keras.applications.vgg16 import VGG16, preprocess_input

vgg16_weight_path = '/content/vgg16_weights_tf_dim_ordering_tf_kernels_notop.h5'

vgg = VGG16(
    weights = vgg16_weight_path,
    include_top = False, 
    input_shape = (200,200,3)
)<jupyter_output><empty_output><jupyter_text>Freezing previous layers<jupyter_code>for layer in vgg.layers:
    layer.trainable = False<jupyter_output><empty_output><jupyter_text>Adding layers for training best accuracy: 128 -> 32 -> 5 for final activation<jupyter_code>from keras import layers
from tensorflow.keras import Sequential
from tensorflow.keras.layers import Flatten, Dense
from tensorflow.keras import regularizers



model = Sequential()
model.add(vgg)
model.add(Flatten())
model.add(Dense(128, activation='relu'))
model.add(layers.Dropout(rate=0.5))
model.add(Dense(32, activation='relu'))
model.add(layers.Dropout(rate=0.2))
#model.add(Dense(16, activation='relu'))
#model.add(layers.Dropout(rate=0.1))
model.add(Dense(5,activation="softmax"))<jupyter_output><empty_output><jupyter_text>Slow learning rate with larger number of epochs has proven to give more accurate results without overfitting<jupyter_code>opt = tf.keras.optimizers.Adam(learning_rate=0.0001)

model.compile(optimizer=opt, loss="categorical_crossentropy", metrics=["accuracy"])

model.summary()<jupyter_output>Model: "sequential_2"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
vgg16 (Functional)           (None, 6, 6, 512)         14714688  
_________________________________________________________________
flatten_2 (Flatten)          (None, 18432)             0         
_________________________________________________________________
dense_7 (Dense)              (None, 128)               2359424   
_________________________________________________________________
dropout_5 (Dropout)          (None, 128)               0         
_________________________________________________________________
dense_8 (Dense)              (None, 32)                4128      
_________________________________________________________________
dropout_6 (Dropout)          (None, 32)                0         
______________________________________________________[...]<jupyter_text>Training for first 300 epochs<jupyter_code>hist= model.fit(train_generator, validation_data = valid_generator, epochs = 300)
final_loss, final_accuracy = model.evaluate(valid_generator)
print("Final loss: {:.2f}".format(final_loss))
print("Final accuracy: {:.2f}%".format(final_accuracy * 100))
hist= model.fit(train_generator, validation_data = valid_generator, epochs = 300)
final_loss, final_accuracy = model.evaluate(valid_generator)
print("Final loss: {:.2f}".format(final_loss))
print("Final Valid accuracy: {:.2f}%".format(final_accuracy * 100))

final_loss, final_accuracy = model.evaluate(train_generator)
print("Final loss: {:.2f}".format(final_loss))
print("Final Train accuracy: {:.2f}%".format(final_accuracy * 100))
model.save('/content/gdrive/MyDrive/Eye Diseases/eye_disease_detection.h5')
import numpy as np

val_image_batch, val_label_batch = next(iter(valid_generator))
true_label_ids = np.argmax(val_label_batch, axis=-1)
print("Validation batch shape:", val_image_batch.shape)
dataset_labels = sorted(train_generator.class_indices.items(), key=lambda pair:pair[1])
dataset_labels = np.array([key.title() for key, value in dataset_labels])
print(dataset_labels)<jupyter_output>['<NAME>' 'Cataracts' 'Endophthalmitis' 'Hemangioma' 'Hyphema']<jupyter_text>Predicting on validation set<jupyter_code>tf_model_predictions = model.predict(val_image_batch)
print("Prediction results shape:", tf_model_predictions.shape)
print(tf_model_predictions)
predicted_ids = np.argmax(tf_model_predictions, axis=-1)
predicted_labels = dataset_labels[predicted_ids]
print(predicted_labels)<jupyter_output>Prediction results shape: (32, 5)
[[5.30476719e-02 9.45869148e-01 7.01819663e-05 4.88160003e-05
  9.64164559e-04]
 [1.00943704e-04 9.99897122e-01 8.11278227e-08 1.24229132e-07
  1.70897761e-06]
 [1.02753507e-03 6.63300161e-05 9.81068671e-01 2.44020222e-04
  1.75935514e-02]
 [5.15712500e-02 7.63550282e-01 8.00942816e-03 4.22490615e-04
  1.76446497e-01]
 [1.75755355e-04 3.66787463e-05 7.14716196e-01 1.18185075e-04
  2.84953296e-01]
 [5.64435720e-01 1.77410850e-03 2.13300407e-01 6.23232918e-03
  2.14257389e-01]
 [1.40494012e-04 4.08467276e-05 1.41212333e-03 8.69257492e-04
  9.97537255e-01]
 [1.11508009e-03 5.07826335e-05 8.39429922e-05 9.98690069e-01
  6.01527354e-05]
 [1.15322627e-01 3.46851423e-02 6.20426536e-02 9.29986127e-03
  7.78649688e-01]
 [5.74010483e-04 4.90027560e-05 4.97739129e-05 5.78649851e-05
  9.99269307e-01]
 [9.83977854e-01 1.51552129e-04 1.57604776e-02 1.13339902e-05
  9.87565145e-05]
 [1.01009607e-02 9.84148264e-01 4.15319623e-03 8.09078992e-05
  1.51675765e-03]
 [2.75[...]<jupyter_text>Validation Set Prediction Results:<jupyter_code>import matplotlib.pyplot as plt
plt.figure(figsize=(10,9))
plt.subplots_adjust(hspace=0.5)
for n in range((len(predicted_labels)-2)):
  plt.subplot(6,5,n+1)
  plt.imshow(val_image_batch[n])
  color = "green" if predicted_ids[n] == true_label_ids[n] else "red"
  plt.title(predicted_labels[n].title(), color=color)
  plt.axis('off')
_ = plt.suptitle("Model predictions (green: correct, red: incorrect)")<jupyter_output><empty_output><jupyter_text>Model Testing<jupyter_code>from keras.preprocessing.image import load_img
 
image = load_img('/content/gdrive/MyDrive/Eye Diseases/Test Images/_185_capillaryhemangioma1.jpg', target_size=(200,200))

from keras.preprocessing.image import img_to_array
# convert the image pixels to a numpy array
image = img_to_array(image)
#image = np.repeat(image, 3, 2)
image = np.expand_dims(image,axis=0)
image = np.array(image)/255.0
print(image.shape)
pred = model.predict(image)
print(pred.shape)
print (pred)
predicted_id = np.argmax(pred, axis=-1)
predicted_labels = dataset_labels[predicted_id]
print(predicted_labels)<jupyter_output>['Hemangioma']
{'fraction_non_alphanumeric': 0.11286060339132349, 'fraction_numerical': 0.09689495705791676, 'mean_word_length': 3.730729166666667, 'pattern_counts': {'":': 0, '<': 45, '<?xml version=': 0, '>': 47, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 24, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '12', 'dir': 'jupyter-structured-clean-dedup', 'id': '97006', 'license': 'MIT', 'n_tokens_mistral': 3526, 'n_tokens_neox': 2948, 'n_words': 502, 'path': 'notebooks/Disease/Disease_Detection_Final.ipynb', 'repo_name': 'mfc2496/EyeSee-Server', 'size': '9089'}