...

Build a CNN Model for Retinal Image Diagnosis

Tensorflow retinal eye

👁️ CNN Image Classification for Retinal Health Diagnosis with TensorFlow and Keras! 👁️

How to gather and preprocess a dataset of over 80,000 retinal images, build a CNN model , and train it that can accurately distinguish between these health categories.

What You’ll Learn:

🔹 Data Collection and Preprocessing: Discover how to acquire and prepare retinal images for optimal model training.

🔹 CNN Architecture Design: Create a customized architecture tailored to retinal image classification.

🔹 Training Process: Explore the intricacies of model training, including parameter tuning and validation techniques.

🔹 Model Evaluation: Learn how to assess the performance of your trained CNN on a separate test dataset.

Check out our tutorial here : https://www.youtube.com/watch?v=PVKI_fXNS1E

Link for the full code : https://ko-fi.com/s/2ee538397a

Link for my blog : https://eranfeit.net/blog/

You can find more tutorials, and join my newsletter here : https://eranfeit.net/

Here is the code for the CNN Model :

Link for the dataset https://www.kaggle.com/datasets/paultimothymooney/kermany2018

Step 1 : Build a CNN model

This code is designed for multi-class classification of retinal OCT (Optical Coherence Tomography) images.

It utilizes a Convolutional Neural Network (CNN) model to classify retinal OCT images into multiple categories (4 classes in this case). The dataset is organized in directories for training and testing images.

The model is trained on grayscale images of size 224×224 pixels. After training, the model’s performance is evaluated using training and validation accuracy and loss. The model is saved to a file for future use and also visualizes training metrics (accuracy and loss) over epochs.

# Import necessary libraries import numpy as np from keras.models import Sequential  # Sequential model to build a linear stack of layers from keras.layers import Conv2D, MaxPooling2D, Flatten, Dense  # CNN layers from keras.preprocessing.image import ImageDataGenerator  # For data augmentation from keras.callbacks import EarlyStopping  # To stop training early if validation performance doesn't improve  # Define paths to the training and testing data directories trainPath = "E:/Data-sets/Retinal OCT Images/train" testPath = "E:/Data-sets/Retinal OCT Images/test" batchSize = 32  # Number of samples per gradient update  # Build the CNN model model = Sequential()  # First convolutional layer: 16 filters, 3x3 kernel, ReLU activation, input shape for grayscale images (224x224x1) model.add(Conv2D(filters=16, kernel_size=3, activation='relu', input_shape=(224,224,1)))  model.add(MaxPooling2D())  # MaxPooling layer to down-sample the feature maps  # Second convolutional layer: 32 filters, 3x3 kernel, ReLU activation model.add(Conv2D(filters=32, kernel_size=3, activation='relu')) model.add(MaxPooling2D())  # MaxPooling layer to reduce dimensionality  # Flatten the feature map to a 1D vector before feeding it into a fully connected layer model.add(Flatten())  # Fully connected layer with 4 output neurons, softmax activation to predict class probabilities model.add(Dense(4, activation='softmax'))  # Print a summary of the model architecture print(model.summary())  # Compile the model with categorical cross-entropy loss and Adam optimizer model.compile(loss="categorical_crossentropy", optimizer="Adam", metrics = ["accuracy"])  # Prepare the data using ImageDataGenerator for data augmentation datagen = ImageDataGenerator(rescale=1./255)  # Rescale pixel values to [0,1]  # Load training data from the directory, applying transformations (resizing, grayscale, etc.) trainData = datagen.flow_from_directory(trainPath,                                         target_size=(224,224),  # Resize images to 224x224                                         batch_size=batchSize,  # Set batch size                                         color_mode='grayscale',  # Grayscale images                                         class_mode='categorical',  # Multi-class classification                                         shuffle=True)  # Shuffle the training data  # Load testing data from the directory (no shuffling for testing) testData = datagen.flow_from_directory(testPath,                                         target_size=(224,224),                                         batch_size=batchSize,                                         color_mode='grayscale',                                         class_mode='categorical',                                          shuffle=False)  # No shuffle for testing  # Calculate the number of steps per epoch and validation steps stepsPerEpoch = np.ceil(trainData.samples / batchSize) validationSteps = np.ceil(testData.samples / batchSize)  # Set up early stopping to prevent overfitting (stop training if validation accuracy doesn't improve for 5 epochs) stopEarly = EarlyStopping(monitor='val_accuracy', patience=5)  # Train the model with the prepared data, validation data, and early stopping callback history = model.fit(trainData,                     steps_per_epoch=stepsPerEpoch,                     epochs=50,                     validation_steps=validationSteps,                     validation_data=testData,                     callbacks=[stopEarly])  # Early stopping callback to monitor validation accuracy  # Save the trained model to a file for later use model.save("e:/temp/retinalOCT.h5")  # Import Matplotlib to visualize training and validation metrics import matplotlib.pyplot as plt  # Get the training and validation accuracy, loss values from the training history acc = history.history['accuracy'] val_acc = history.history['val_accuracy'] loss = history.history['loss'] val_loss = history.history['val_loss']  # Create a range of epochs for plotting epochsForPlot = range(len(acc))  # Plot the training and validation accuracy over epochs plt.plot(epochsForPlot, acc, 'r', label='Train Accuracy')  # Red line for training accuracy plt.plot(epochsForPlot, val_acc, 'b', label='Validation Accuracy')  # Blue line for validation accuracy plt.xlabel('Epochs') plt.ylabel('Accuracy') plt.title('Training and Validation Accuracy') plt.legend(loc='lower right') plt.show()  # Plot the training and validation loss over epochs plt.plot(epochsForPlot, loss, 'r', label='Train Loss')  # Red line for training loss plt.plot(epochsForPlot, val_loss, 'b', label='Validation Loss')  # Blue line for validation loss plt.xlabel('Epochs') plt.ylabel('Loss') plt.title('Training and Validation Loss') plt.legend(loc='upper right') plt.show()

Link for the full code : https://ko-fi.com/s/2ee538397a


Step 2 : Test the model

# Import necessary libraries import tensorflow as tf  # TensorFlow for deep learning tasks import os  # OS module to interact with file system from keras.utils import img_to_array, load_img  # For loading and converting images import numpy as np  # For numerical operations import cv2  # OpenCV for image processing and displaying  # Load the pre-trained model model = tf.keras.models.load_model("e:/temp/retinalOCT.h5")  # Load the trained model from file  # Load categories (class labels) source_folder = "E:/Data-sets/Retinal OCT Images/val"  # Path to validation dataset folder categories = os.listdir(source_folder)  # List all categories (subfolders) categories.sort()  # Sort the categories alphabetically print(categories)  # Print the sorted categories  numOfClasses = len(categories)  # Number of different classes (categories) print(numOfClasses)  # Print the number of classes  # Function to preprocess the input image for prediction def prepareImage(pathForImage):     # Load the image, resize it to 224x224 and convert it to grayscale     image = load_img(pathForImage, target_size=(224,224), color_mode='grayscale')     imgResult = img_to_array(image)  # Convert the image to an array     imgResult = np.expand_dims(imgResult, axis=0)  # Add a batch dimension     imgResult = imgResult / 255.  # Normalize the image pixel values to [0,1]     return imgResult  # Return the preprocessed image  # Prediction for a specific test image #testImagePath = "E:/Data-sets/Retinal OCT Images/val/CNV/CNV-6851127-1.jpeg"  # Example image path (commented out) testImagePath = "E:/Data-sets/Retinal OCT Images/val/DRUSEN/DRUSEN-9894035-1.jpeg"  # Path to test image  # Prepare the image for prediction imgForModel = prepareImage(testImagePath)  # Predict the class probabilities for the image using the trained model resultArray = model.predict(imgForModel, verbose=1)  # Get the model prediction answers = np.argmax(resultArray, axis=1)  # Get the index of the highest probability class  print(answers)  # Print the index of the predicted class  # Get the corresponding class label text = categories[answers[0]]  # Map the predicted class index to the category name print("Predicted : " + text)  # Print the predicted category  # Display the image with the predicted label img = cv2.imread(testImagePath)  # Read the image using OpenCV font = cv2.FONT_HERSHEY_COMPLEX  # Define the font for the text overlay  # Overlay the predicted label on the image at position (0, 50) with size 2 and color (209,19,77) cv2.putText(img, text, (0,50), font, 2, (209,19,77), 2)  # Show the image in a window cv2.imshow('img', img)  # Display the image with the text overlay cv2.waitKey(0)  # Wait for a key press before closing the window  cv2.destroyAllWindows()  # Close all OpenCV windows

Link for the full code : https://ko-fi.com/s/2ee538397a


Connect :

☕ Buy me a coffee — https://ko-fi.com/eranfeit

🖥️ Email : feitgemel@gmail.com

🌐 https://eranfeit.net

🤝 Fiverr : https://www.fiverr.com/s/mB3Pbb

Enjoy,

Eran

error: Content is protected !!
Eran Feit