Deep Learning

Understanding and using deep learning networks

Diabetic Retinopathy Detection

Post by Dr. Barath Narayanan, University of Dayton Research Institute (UDRI) with co-authors: Dr. Russell C. Hardie, University of Dayton (UD), Manawduge Supun De Silva, UD, and Nathaniel K. Kueterman, UD.


Diabetic Retinopathy (DR) is one of the leading cause for blindness, affecting over 93 million people across the world. DR is an eye disease associated with diabetes. Detection and grading DR at an early stage would help in preventing permanent vision loss. Automated detection and grading during the retinal screening process would help in providing a valuable second opinion. In this blog, we implement a simple transfer-learning based approach using a deep Convolutional Neural Network (CNN) to detect DR.
Please cite the following articles if you're using any part of the code for your research: The Kaggle blindness detection challenge dataset (APTOS 2019 Dataset) contains separate training and testing cases. In this blog, we solely utilize the training dataset to study and estimate the performance. These images were captured at the Aravind Eye Hospital, India. The training dataset contains 3662 images marked into different categories (Normal, Mild DR, Moderate DR, Severe DR, and Proliferative DR) by expert clinicians. Note that, in this blog, we solely focus on detecting DR, you could find more details about our grading architecture in our paper.

Grouping Data by Category

We extract the labels from excel sheet and segregate the images into 2-folders as 'no' or 'yes' as we're solely focused on detecting DR in this blog. The helper code for splitting the data into categories is at the end of this post.

Load the Database

Let's begin by loading the database using imageDatastore. It's a computationally efficient function to load the images along with its labels for analysis.
% Two-class Datapath
two_class_datapath = 'Train Dataset Two Classes';

% Image Datastore
imds=imageDatastore(two_class_datapath, ...
    'IncludeSubfolders',true, ...

% Determine the split up

Visualize the Images

Let's visualize the images and see how images differ for each class. It would also help us determine the type of classification technique that could be applied for distinguishing the two classes. Based on the images, we could identify preprocessing techniques that would assist our classification process. We could also determine the type of CNN architecture that could be utilized for the study based on the similarities within the class and differences across classes. In this article, we implement transfer learning using inception-v3 architecture. You can read our paper to see the performance of different preprocessing operations and other established architectures.
% Number of Images

% Visualize random 20 images
for idx=1:20

Training, Testing and Validation

Let’s split the dataset into training, validation and testing. At first, we are splitting the dataset into groups of 80% (training & validation) and 20% (testing). Make sure to split equal quantity of each class.
% Split the Training and Testing Dataset
% Split the Training and Validation
This leaves us with the following counts:  
Yes No
Training Set: 1337 1300
Validation Set: 144 149
Test Set: 361 371

Deep Learning Approach

Let’s adopt a transfer learning approach to classify retinal images. In this article, I’m utilizing Inception-v3 for classification, you could utilize other transfer learning approaches as mentioned in the paper or any other architecture that you think might be suited for this application. My MathWorks blogs on transfer learning using other established networks can be found here: AlexNet, ResNet


We will utilize validation patience of 3 as the stopping criteria. For starters, we use 'MaxEpochs' as 2 for our training, but we can tweak it further based on our training progress. Ideally, we want the validation performance to be high when training process is stopped. We choose a mini-batch size of 32 based on our hardware memory constraints, you could pick a bigger mini-batch size but make sure to change the other parameters accordingly.
% Converting images to 299 x 299 to suit the architecture
augimdsTrain = augmentedImageDatastore([299 299],imdsTrain);
augimdsValid = augmentedImageDatastore([299 299],imdsValid);

% Set the training options
options = trainingOptions('adam','MaxEpochs',2,'MiniBatchSize',32,...

netTransfer = trainNetwork(augimdsTrain,incepnet,options);

Testing and Performance Evaluation

% Reshape the test images match with the network 
augimdsTest = augmentedImageDatastore([299 299],imdsTest);

% Predict Test Labels
[predicted_labels,posterior] = classify(netTransfer,augimdsTest);

% Actual Labels
actual_labels = imdsTest.Labels;

% Confusion Matrix
title('Confusion Matrix: Inception v3');
% ROC Curve
[fp_rate,tp_rate,T,AUC] = perfcurve(test_labels,posterior(:,2),2);
plot(fp_rate,tp_rate,'b-');hold on;
grid on;
xlabel('False Positive Rate');
ylabel('Detection Rate');

Class Activation Mapping Results

We visualize the Class Activation Mapping (CAM) results for these networks for different DR cases using the code: This would help in providing insights behind the algorithm's decision to the doctors.
Here are the results obtained for various cases:


In this blog, we have presented a simple deep learning-based classification approach for CAD of DR in retinal images. The classification algorithm using Inception-v3 without any preprocessing performed relatively well with an overall accuracy of 98.0% and an AUC of 0.9947 (results may vary because of the random split). In the paper, we studied the performance of various established CNN architectures for the same set of training and testing cases under different preprocessing conditions. Combining the results of various architectures provides a boost in performance both in terms of AUC and overall accuracy. A comprehensive study of these algorithms, both in terms of computation (memory and time) and performance, allows the subject matter experts to make an informed choice. In addition, we have presented our novel architecture approaches in the paper for detection and grading of DR.

About the Author

Dr. Barath Narayanan graduated with MS and Ph.D. degree in Electrical Engineering from the University of Dayton (UD) in 2013 and 2017 respectively. He currently holds a joint appointment as a Research Scientist at UDRI's Software Systems Group and as an Adjunct Faculty for the ECE department at UD. His research interests include deep learning, machine learning, computer vision, and pattern recognition.

Helper Code

Code for grouping data by DR category (yes or no)

After downloading the ZIP files from the website and extracting them to a folder called "train_images". Make sure to download the excel sheet (train.csv - convert it to .xlsx for this code) containing the true labels by expert clinicians. We extract the labels from excel sheet and segregate the images into 2-folders as 'no' or 'yes' as we solely focus on detecting DR in this blog.
% Training Data path

% Two-class Data path
two_class_datapath='Train Dataset Two Classes\';

% Class Names

% Read the Excel Sheet with Labels

% Determine the Labels

% Merge all labels marked into Mild, Medium, Severe and Proliferative DR 
% into a single category 'Yes' 

% Rest of the dataset belongs to 'No' category

% Filename

% Now, write these images 2-folders 'Yes' or 'No' for us to develop a deep
% learning architecture utilizing Deep learning toolbox
% Determine the Files put them in separate folder
for idx=1:length(filename)
   % You could uncomment if you would like to see live progress
    %  fprintf('Processing %d among %d files:%s \n',idx,length(filename),filename{idx})[/%]
    % Read the image
    current_filename=strrep(filename{idx}, char(39), '');
    % Write the image in the respective folder
    clear img;



To leave a comment, please click here to sign in to your MathWorks Account or create a new one.