Using CNNs to Diagnose Diabetic Retinopathy
By: Dev Patel
Tags: Artificial Intelligence, Machine Learning, Deep Learning, Pytorch, Disease

Computer vision has transformed the way we look at the world’s most challenging problems (see what I did there, look).

Despite having new algorithms and model architectures come up and change the playing field, we still have a long way to go in terms accessibility, efficiency, and accuracy of these models before we decide to have all of our doctors on the GPU.

For now though, let’s look at what can be done with some stable internet connection, a computer, and PyTorch. The result: a CNN that can accurately diagnose diabetic retinopathy.

I’m not going to focus all that much on the actual scientific cause of DR but what I will talk about is the benefits of ML in diagnosing diseases and the opportunity these models present:

Millions of datasets and accurate predictions: The number of MRI scans 1 doctor can analyze is futile compared to a computer and can rapidly improve speed and accuracy of diagnoses. Experience yields accuracy. Identifying trends in data over time: AI and ML algorithms can identify subtle trends in a given dataset with relation to a more expansive set of features and variables including time. Being able to detect disease progression at the right time and early on can save millions of lives. Quality access to those without doctors: Although farfetched, it is almost certain that doctors and even specialists are about to become obsolete in the next few decades due to advances in computer vision and AI. However, for those countries and communities where healthcare is already sparse, AI provides the opportunity to provide real-time care while eliminating factors like education, experience, and distance.

With that being said, disease prediction with computer vision is a difficult task and includes several issues of its own from collecting good data, fine-tuning the hyperparameters in the best fashion, and making sure patients aren’t misinformed about a disease they may not have.

Despite all of this, what you need to know is that AI is powerful and the fact that a 15 year-old kid can create a program through some code that performs similar to if not better than a trained doctor should blow your mind.

But what is Diabetic Retinopathy? A brief overview:

According to Fighting Blindness Canada, Diabetic retinopathy (DR) is the most common form of vision loss associated with diabetes. Affecting approximately 500,000 Canadians, it is the leading cause of blindness among working-age adults.

Most of these cases are due to the late onset of diabetes where patients are unable to control their blood-sugar levels can lead to blood vessel damage in the eye. As these vessels are unable to supply vital nutrients to the eye tissue, the body produces new, weaker vessels that can burst and damage the eye itself.

Credit: Magrabi Hospitals -> Comparison of the Anatomy of the Eye between a healthy and diabetic eye. Notice the spots, aneurysms, and congestion of smaller blood vessels all throughout the eye.

From here, nerve fibres can swell, micro aneurysms protrude from the vessel walls of the smaller vessels, and could lead to macular ischemia. This is a very common disease and treatments are available, but the most apparent problem stems from diagnosis and early onset of catching DR during its moderate stage.

Most diagnosis technology like fluorescein angiography, widefield imaging technology, and Optical Coherence Tomography (OCT) are available but do not provide a comprehensive view of the retina and its entirety, making it more difficult for doctors to give an accurate diagnosis in the process and, for that matter, early diagnosis.

According to the Byers Eye Institute at Stanford, 30% of DR patients are unaware that they have diabetic retinopathy until it reaches the severe state.

Credit: Modern Optometry -> Widefield image demonstrating severe non-proliferative diabetic retinopathy in a patient’s right eye (A) and proliferative diabetic retinopathy in a patient’s left eye (B).

CNNs have great potential in this problem and can significantly improve the early onset of diabetic retinopathy in seemingly healthy patients. In this part of a 2-part series, I’ll be going through the technicals of the code, the data, and the approach while giving you the basic knowledge to design your own custom CNN applications.

I want to emphasize here is that the results of the model came out to be very poor, and part 2 addresses those issues while also showing you how to improve the accuracy of the model from 23% to 98.95%.

The article shows a concrete method for developing CNNs that can be applied to any dataset as the issue was not the model, but the data itself.

If you don’t have the necessary prerequisites (PyTorch and Python, the basics of CNNs, and some basic library knowledge), I would recommend you read over these 2 articles and the second part of this series so that you’re able to gain a lot of value from this. Even still, you should be able to follow along.

The Dataset: Kaggle API and Preprocessing Dataset

To start, this is the dataset that I used for the model containing about 3662 images with 5 labels to classify each image -> No_DR: 0, Mild: 1, Moderate: 2, Severe: 3, Proliferate_DR: 4

To start, we need to import the dataset using the Kaggle API. You can use this article to guide you in the right direction, or you can figure it out and challenge yourself :) .

The commented line above needs to be used to download the dataset

Next, let’s look at the dataset in the project directory:

Notice that there is a train.csv file -> if you open it up, you’ll see that it contains the labels in integer format corresponding to the image key name. Open the /gaussian_filtered_images/gaussian_filtered_images/ and there will be 5 folders titled Mild, Moderate, No_DR, Proliferate_DR, and Severe containing their respective images.

Because we already know the corresponding integer labels with the given image labels, we need to combine all of the contents of the folder into the root folder.

To do this, just go into each folder, copy the images, and paste them in the /gaussian_filtered_images/gaussian_filtered_images/.

Then, delete the labelled folders. Make sure that the gaussian_filtered_images directory then contains 3662 images.

Now, I used this script to load in the data and preview some of the images with the corresponding output:

trainer_names_csv automatically produces a dataframe that can be indexed using iloc

This image should correspond to the image name in the directory

Here are some examples of images with their given labels for reference: