First Review PDF
First Review PDF
USING
CONVOLUTIONAL NEURAL NETWORK
Submitted by
HRITIK RAO-RA1611003010365
PRANJAY BAJAJ-RA1611003011005
PROJECT GUIDE:
Ms. P. SARANYA - 102076
Department
ABSTRACT
There has been a noticeable increase in the number of people diagnosed as blind in
today’s day and age. Many patients who are diagnosed as blind were patients who
had prolonged diabetes . Hence, people with long lasting diabetes condition can have
an eye disease called as diabetic retinopathy. This generally occurs when high blood
sugar levels cause damage to blood vessels in the retina. These blood vessels could
swell and leak. All these factors could lead to blindness. DR is estimated to have
affected approx 93 million worldwide. DR generally affects 40% of the people
associated with long-term diabetes. There are basically four categories of diabetic
retinopathy namely (no DR, mild , non-proliferative DR and proliferative DR ). The
blindness caused due to prolonged diabetes can be stopped by early detection of DR,
this can be achieved if we detect DR at an early stage hence we require technologies
to be available free of cost and be present everywhere. Hence a deep learning model
using tensor flow and keras will be used ,so people will be able to access it on a
global front. The efficiency of early detection of DR will improve as this project
develops.
TABLE OF CONTENTS
ABSTRACT
1 INTRODUCTION 1
1.1 BACKGROUND 1
1.2 PREVIOUS SYSTEM 1
1.3 PROPOSED SYSTEM 2
1.4 CONVOLUTIONAL NEURAL NETWORK 2
2 LITERATURE SURVEY 3
2.1 COMPARISION TABLE 7
2.2 INFERENCE FROM THE SURVEY 10
2.3 ARCHITECTURE 12
3 MODULES 13
3.1 FUNDUS CAMERA 13
3.2 DATASET 14
3.3 DATA PRE-PROCESSING 14
3.4 CONVOLUTIONAL NEURAL NETWORK 15
3.5 WEBAPP 17
5. APPENDIX 18
APPENDIX A: CODE 18
APPENDIX B: OUTPUT 28
6. REFERENCES 32
LIST OF TABLES AND FIGURES
BACKGROUND:
Diabetes has always been a very widespread disease. People suffering from prolonged diabetes get an
eye disease called as Diabetic Retinopathy. Severe case of Diabetic Retinopathy can cause blindness.
Diabetic Retinopathy has affected approximately 93 million people across the world. It has therefore
become one of the leading causes of blindness.The probability of developing diabetic retinopathy is
directly related to the duration of the disease. In people suffering from Type 2 diabetes, DR has a very
deceitful onset and hence a majority of times go unnoticed most of the time. Type 1 diabetics, on the
other hand, are diagnosed early hence patients suffer from DR only after years of diagnosis that had
been made. Generally speaking the risk of developing retinopathy increases after the patient has hit
puberty. Nearly after twenty years of diabetes, almost 80% of the type 2 diabetics and all type 1
diabetics patients show some form of retinopathy.
PREVIOUS SYSTEM
The existing systems present in detecting DR are time-consuming and manual in nature. The current
system requires trained medical personnel to examine and hence evaluate the fundus image of the
retina. Hence there are delays in the result, causes miscommunication, and delayed treatment.
Clinicians are able to identify DR only with the presence of lesions along with vascular abnormalities
caused by the disease. Even though this procedure is effective but the major drawback is that the
resource demands are high. The expertise and the resources required in detecting DR are often lacking
in certain areas specifically where the number of patients suffering from diabetes is more. As there is
an increase in the number of individuals suffering from diabetes the infrastructure needs to be improved
and the methods need to be modernized and made accurate.
1
PROPOSED SYSTEM
The main idea of this project is to bring a change in the way Diabetic Retinopathy is being detected
so that each person who wants their eyes to be analyzed need not wait and his/her eye can be
analyzed accurately. Here the main requirement for the detection of Diabetic Retinopathy is the
Fundus Image. This Fundus Image is captured using OCT (Optic Coherence Tomography). OCT
is an imaging technique that is used so that the Blood Vessels, Lesions, Micro aneurysms and other
important features of the eye are displayed successfully. This hence holds the primary dataset for
the method to come into picture. The fundus image is hence then undergoes the preprocessing
stage where the images are augmented using the Keras Image Generator. The essential features
that are required for the detection of DR are extracted. This then is used feed the pre-processed
data into Convolutional Neural Network. The convolutional neural network then uses various
weights to assign to different features to examine which of the features play an important role in
detecting DR. We have used CNN due its previous success in various image recognition
techniques.
Convolutional Neural Network like Artificial Neural Network consists of some neurons. Each
neuron receives several inputs takes. Each neuron is assigned different weights these neurons help
in finding which feature holds a greater importance. Hence, from all the feature all the essential
features are extracted. Therefore, these features with different weights help in getting the desired
result through training set. There are various hidden layers which are not necessarily connected to
other neurons. There is an important function in CNN called the pooling effect. The pooling effect
help reduce the spatial size of representation and therefore it reduces the number of parameters
and hence there are less number of computations. There are two types of pooling which can be
used max pooling and min pooling.
2
LITERATURE SURVEY
The dataset used by Yuchen Wu and Ze Hu was derived from the Diabetic Retinopathy Detection
competition that was held on Kaggle Platform. The images that were derived were basically
classified into 5 categories namely No DR, Mild DR, Moderate DR, Severe DR and Proliferative
DR. It uses the Migration Learning Approach which is one of the new machine learning
approaches. There are four different approaches to implement this method they have used Feature
based Transfer Learning. The pre-training model they used is based on ImageNet. Since there was
a requirement of data augmentation which involved the usage of imageDataGenerator which is a
class present in Keras .Hence keras was primarily used.They used the pre-training model for
feature extraction that was required to develop a final model to detect DR. The accuracy that was
obtained using ResNet50 was 0.50. This paper adopts the method of migration learning, using
Keras' built-in pre-training model to fine-tune the new dataset to achieve the classification based
on the degree of diabetic retinopathy.
Shuang Yu, Di Xiao and Yogesan Kanagasingam : Exudate Detection for Diabetic
Retinopathy With Convolutional Neural Networks.
Shuang Yu, Di Xiao and Yogesan Kanagasingam have tried to use the CNN model (Convolutional
Neural Network) to detect the Diabetic Retinopathy. It is used to achieve pixel-wise exudate image
patches. The accuracy that was obtained is off 91.92% . The Framework for exudate detection
with deep learning includes three main procedures before the image is sent to the DR model that
was established using CNN. The three major steps are Removal of Optic Disc Detection, Removal
of Retinal Vessels,Ultimate Opening. The images are entered in form off 64*64 patches. The
database used is E-Ophtha EX, which is a newly published datasource for exudate segmentation
with pixel level annotations.
3
Mamta Arora and Mrinal Pandey of Manav Rachna University : Deep Neural
Network for Diabetic Retinopathy Detection
The database used was borrowed from the dataset provided by kaggle during the competition to
detect Diabetic Retinopathy Detection. They basically followed a two step process which includes
step 1 :data preprocessing and augmentation and step2: convolution layer. The convolutional layer
was further divided into a 5 step process which involved convolutional layer, The basic building
block of Convolutional neural extracting weights, Pooling Layer , connecting layer and logistic
classifier (the values it output represent the probability of each class). The accuracy that was
obtained was 74%.
Enrique V. Carrera, Andres Gonz ´ alez and Ricardo Carrera : Automated detection
of diabetic retinopathy using SVM
This paper uses Support Vector Machine to have an automated detection of Diabetic Retinopathy.
The accuracy that was obtained was 94%. The database that was used is off Messidor database
which consists of 1200 eye fundus images. Out of these 1200 images, 800 images are of pupil
dilation and 400 images are taken without dilation. There were basically eight features that were
essentially used in detection of DR are Standard deviation of the red component, Standard
deviation of the green component, Standard deviation of the blue component, Blood vessel density,
Possible number of micro aneurysms, Actual number of micro aneurysms, Density of hard
exudates, Green component entropy. The accuracy was found to be 85.1%.
D. V. Jadhav, S.D. Shirbahadurkar and V. M. Mane: A Modern Screening Approach
for Detection of Diabetic Retinopathy
The database that was used is of DIARETDB1. This classification algorithm used to classify the
fundus images is based on decision trees. This algorithm is able to classify the fundus image into
norvarious DR. Hence this algorithm is holoentropy enabled. The above method uses activities
like grey scaling(converts image into a pixel driven image that a computer understands), optic disc
segmentation and blood vessel segmentation to detect DR . The following steps include feature
extraction and hence a feature extraction vector to which appropriate weights are assigned to
different features which help in detecting DR.. The performance obtained by the model was
97.75%.
4
Anaswara Chandran, Prof. Nisha K K and Dr. Vineetha S : Computer Aided Approach
for Proliferative Diabetic Retinopathy Detection in Color Retinal Images
This uses STARE and MESSIDOR databases. MESSIDOR consists of 1200 fundus images and
STARE consists of 400 images. This uses Random forest classifier. Random Forest classifier is
used for the classification process because it has the ability to handle higher dimensional feature
sets. Different patches of dataset are used to train various decision trees. The patches are extracted
from the image sets. The patches undergo two distinct extraction processes namely Texture Feature
Extraction and Vessel Feature Extraction. The decision trees after giving various decisions it’s
output is fed forward to random forest classifier which uses a rule based approach to classify the
images to reach to a certain result. The accuracy obtained was 89% for MESSIDOR and 88% for
STARE
Ankita Guptaa, Rita Chhikarab, “Diabetic Retinopathy: Present and Past”:
This paper reviews the various techniques used for detecting DR based on the features like blood
vessels, microaneurysms, haemorrhages etc. In most of the experiments retinal fundus images were
used in which images of retina were captured by fundus camera. This review bifurcates the
detection of DR into two approaches; Blood vessels segmentation and Identification of lesions.
This paper compares the experimental results of various machine learning techniques based on
parameters like sensitivity, specificity, Area Under Curve (AUC), Accuracy. The results are also
compared with the deep neural networks and analysis of best technique has been provided.
Sensitivity of 0.934s and 0.93.
Handayani Tjandrasa, Ricky Eka Putra, Arya Yudhi Wijaya, Isye Arieshanti,
“Classification of Non-Proliferative Diabetic Retinopathy Based on Hard Exudates Using
Soft Margin SVM ”,
This study proposes a classification system of NPDR based on exudate features. The main
processes in this study are exudate segmentation, feature extraction and the classification of NPDR
severity level. The exudate segmentation performs the exudate extraction and transforms the
segmented exudates into a binary image. Finally, the extracted features are trained and tested using
soft margin SVM as a classification model. SVM model achieves high accuracy (90.54 %) when
5
classify retinal fundus images as moderate NPDR or severe NPDR
Jayant Yadav, Manish Sharma, Vikas Saxena, “Diabetic Retinopathy Detection using
feedforward Neural Network”,
DR detection is being done by detecting two major parts of it, namely Exudates and Dot
Hemorrhages. First, we will do the preprocessing (1. Extracting Optic Disk 2. Extracting Blood
Vessels 3. Detection of Exudates 4. Detection of Dot hemorrhages ) which is followed by training
the dataset [8] using machine learning, which is by applying neural networks. 75% accuracy.
6
INFERENCE FROM THE SURVEY
The analysis of the literature survey on the methods of Diabetic Retinopathy Detection using
Convolutional Neural Network led to various conclusions, which should be used in further works in
the future. Regarding the pre-processing phase, we can conclude that Optic disc detection, Red Lesion
detection, Blood Vessel and micro aneurisms play a major role in determining DR. this helps in
increasing the quality of the photos. The quantity of the photos is also improved by using Keras Image
Generator. The fact that these previous systems use manual resources hence these resources are
sometimes not available or exhausted. Owing to the fact that manual procedure of these tasks is a
complex and time-consuming task, it would be more prudent to use the proposed system of Diabetic
Retinopathy Detection. Automation tools and procedures like using CNN for detection of DR enables
patients to get their retinas detected in a more accurate and efficient manner. The early detection of DR
is also skipped out by ophthalmologists hence making this model one the more efficient methods. Thus
enables CNN in saving time and improving efficiency, accuracy, and consistency while avoiding
human errors and reducing human efforts.
10
ARCHITECTURE
FUNDUS CAMERA
LABELLING BY
DATABASE
OPHTHALMOLOGIST
DATA PREPROCESSING
MODEL.H5
OUTPUT GENERATION
11
MODULES
A fundus camera is a specialized low power microscope that has an attached camera. An angle of
30deg is considered the normal angle of view, creates the film image two times(approx) larger
than life. Wide-angle cameras capture images that are between 40 deg to 140deg and provide
proportionately less retinal magnification.
Light is reflected through a set of filters. This filtered light is then incident onto a mirror. The mirror
is angled such that the incident ray of light is directed to the pupil. This ray of light is then reflected
back by the retina. The light continues to travel through the central aperture of the previously
described mirror through the astigmatic correction devices. This reflected light is then incident onto
a single lens photographic camera, which captures the fundus image of the eye.
The fundus is the bottom or base of anything. We define fundus imaging as the process whereby
reflected light is used to obtain a two-dimensional (2D) representation of the 3D, semitransparent,
retinal tissues projected on to the imaging plane. Since the retina is normally not illuminated
internally, both external illumination projected into the eye as well as the retinal image projected
out of the eye must traverse the pupillary plane.
12
MODULE 2: DATASET
The dataset consists of the fundus image of the eye. These datasets were obtained through various
medical healthcare facilities. For the development of algorithm, macula-centered fundus images
were obtained from EYEPACS in the U.S and three other hospitals in India. There were basically
two sets of datasets that were acquired from a kaagle dataset which consisted of about 35,000
images with 5-class labels and Messidor-1 dataset of 1200 color fundus image with 4 class labels
(normal, mild moderate, severe). Both the datasets consists of images with different heights, widths
and different contraction.
Since including all the images consisting of various unrequired features would lead to overfit of
the model hence will lead to bad modelling of the CNN. Therefore, to achieve efficient modelling,
we progressed to a more smaller but ideal dataset that would help in learning different features.
The dataset consisted of fundus image from varied patients who had extremely varied levels of
lightening and hence are being able to be labeled in a consistent mannner.
The lightening effects the value of the pixels in the fundus images and therefore is labelled in a
very inconsistent manner. To assess the suitability of transfer learning, we considered several
available CNN architecture available to train and deploy a different version of Google-Net using
the fundus photography. Our final model was implemented using keras and Tensor flow and was
influenced by the results obtained from a Deep Learning GPU interactive training system
(DIGITS).Therefore, this enabled rapid prototype training, performance monitoring and real time
visualization.
Data pre-processing is an integral step in Machine Learning as the quality of data and the useful
information that can be derived from it directly affects the ability of our model to learn, thus it
is one of the most major steps to pre-process the data before feeding it to the model. The major
steps required for pre-processing can be categorized as follows:
13
Image Pre-processing:
Image processing is the first and the most important part of data processing as the performance
of the Machine learning model depends solely on the data it is trained with. Techniques such as
segmentation, Fourier Transforms, Wavelet Transforms, and Image Augmentation are employed
to build more robust and accurate models when training data is limited. Few of the image pre-
processing algorithms include:
A fuzzy vessel track algo for retina pictures that used the concept of fuzzy clusters was developed by
Toliaset.al.in21. The group of Zhou et.al.in20 presented the detecting & quantifying of the DR disease
using digital angiogram, which produced excellent results. The algorithm worked on the DR phases
using the retinal fundus image using different computer algos 3536& used it for detection, but the
drawback was the severity of the cases could not be detected
The extent picture developed from the 3 coloured parts of the R-G-B combination is improved
utilizing a multi-scale morphology channelling scheme, thus protecting the colour vector's
directional features. Their strategy enhanced the differentiation of vessels in the coloured retinal
pictures &furthermore the R-G-B channel pictures of the colouring images of the retinas. Another
new algorithm had been introduced in their study to give the correct identification of the structure
of the eye's vascular network.
It’s not taking the original data, randomly transforming it, and then return both the original data
and transformed data.
14
Handling Categorical variables:
Categorical variables are variables that have discrete points rather than continuous points.
Categorical variables are further divided into two points:
Ordinal Categorical variables: these variables can be ordered. Ex: shoe size
Nominal Categorical Variables: these variables cannot be ordered. Ex: the color of t-shirts. We
cannot use the color of a t-shirt as one of our features in the algorithm.
One-Hot Encoding:
So in One-Hot Encoding what we essentially do is that we create ’n’ columns where n is the
number of unique values that the nominal variable can take.
Example: if there are three different types of shirts in our database that we need to examine we
need to create three different columns so as to differentiate them.
A Convolutional Neural Network (ConvNet/CNN) could be a Deep Learning algorithm that has
the ability or the potential to take in an input image, assign importance (learnable weights and
biases) to varied aspects/objects within the image and be able to differentiate one from the other.
The pre-processing required in a ConvNet is much lower as compared to other classification
algorithms. While in primitive strategies filters square measures hand engineered, with enough
training, ConvNets have the ability to learn these filters/characteristics.
The design of a ConvNet is thereto analogous of the property pattern of Neurons within the Human
Brain and was impressed by the organization of the cortical area. Individual neurons answer
stimuli solely in a very restricted region of the field of regard called the Receptive Field. A
collection of such fields overlap to hide the complete cortical region.
The primary role that ConvNet plays is that to reduce the images into a form that is much easier to
process, without losing features which are critical for getting good perception and hence the
result.During network training, the filter weights are adjusted, so as to improve the classification
performance of the network. This can be done using a method called backpropagation, where the
gradient of an error function is computed with respect to all network weights, going all the way to
15
the input connections of the network. Network weights are updated by the following equation
relating the step size to the gradient and the learning rate denoted η.
Wnew = W − η dE dW
An error (or "scoring") function can be expressed as a sum of squared differences between the
network's output and the correct output, overall discrete points in the output. This sort of scoring
function works for cases where the network output is a vector, matrix, or tensor of continuous real
values
KerasNet
Keras is one of the most user-friendly network library that is generally used in developing a neural
network for deep learning. Keras is generally used while developing high-level neural networks
API, which can be run on top of Tensor flow, CNTKor Theano. Keras is generally used if you
need easy and fast prototyping, support both conventional and recurrent neural networks and runs
seamlessly on CPU and GPU.keras application are deep learning models that are used along with
pre-trained weights. Keras also helps in prediction, fine-tuning and feature extraction.
ResNet-50
ResNet also is known as the residual network is a classical neural network used as a backbone for
many computer vision tasks. ResNet allows us to train extremely deep neural networks. ResNet uses
skip connection to add the output from an earlier layer to a later layer. This allows the model to learn
and identify the necessary functions that would ensure that the higher layer performs equally good as
the lower layer. This helps mitigate the vanishing gradient problem. ResNets sum the output feature
maps of the layer with the incoming feature maps but concatenate them.
DenseNet-121
A densely connected neural network is the next step on the way to increase the depth of
convolutional neural networks. The main advantage of DenseNet is that they require fewer
parameters than a traditional CNN. Hence, the redundant feature maps are not omitted by dense
nets. Some variations of ResNetshave proven that many layers do not contribute significantly
towards the development hence these are dropped off by DenseNet. The denseNet has also added
16
a small set of the new-feature map. In denseNets each and every layer has direct access to the
gradients present in the loss function and the original image.
MODULE 5: WEBSITE
Having created a deep learning algorithm that is used for Diabetic Retinopathy Detection, we want
it to be publically available to others hence this deep learning model is deployed onto a website.
This requires traditionally the following things:
Flask:
Flask is a web framework, therefore, flask provides with tools and libraries that help us build web
applications. Flask is a micro-framework, meaning they have no or very little dependencies. Flask
is able to incorporate deep learning and machine learning models onto a web application. Flask
being a light weight framework provide little or no dependencies debug.
17
APPENDIX
APPENDIX A: CODE
IMPORT LIBRARIES
import numpy as np
import pandas as pd
import os
import cv2
import PIL
import gc
import psutil
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from tensorflow import set_random_seed from
tqdm import tqdm
from math import ceil
import math
import sys
import gc
import keras
from keras.preprocessing.image import ImageDataGenerator
from keras.preprocessing.image import load_img from
keras.preprocessing.image import array_to_img
from keras.preprocessing.image import img_to_array
from keras.applications.resnet50 import ResNet50
from keras.applications.resnet50 import preprocess_input
from keras.models import Model
from keras.models import Sequential
from keras.layers.convolutional import Conv2D
from keras.layers.convolutional import MaxPooling2D
from keras.layers.pooling import
GlobalAveragePooling2D from keras.layers import Input
from keras.layers.core import Dropout
from keras.layers.core import Flatten
from keras.layers.core import Dense
from keras.callbacks import ModelCheckpoint
from keras.callbacks import ReduceLROnPlateau
from keras.callbacks import EarlyStopping
18
from keras.activations import softmax
from keras.activations import elu
from keras.activations import relu
from keras.optimizers import Adam
from keras.optimizers import RMSprop
from keras.optimizers import SGD
from keras.layers.normalization import
BatchNormalization from tqdm import tqdm
gc.enable()
print(os.listdir("../input/"))
Loading Data
Data Disribution
Data Visualization
SEED = 7
np.random.seed(SEED)
set_random_seed(SEED)
dir_path = "../input/aptos2019-blindness-detection/"
IMG_DIM = 299 # 224 399 #
BATCH_SIZE = 12
CHANNEL_SIZE = 3
NUM_EPOCHS = 60
TRAIN_DIR = 'train_images'
TEST_DIR = 'test_images'
FREEZE_LAYERS = 2 # freeze the first this many layers for training
CLASSS = {0: "No DR", 1: "Mild", 2: "Moderate", 3: "Severe", 4: "Proliferative DR"}
Loading Data
df_train = pd.read_csv(os.path.join(dir_path, "train.csv"))
df_test = pd.read_csv(os.path.join(dir_path, "test.csv"))
NUM_CLASSES = df_train['diagnosis'].nunique()
print("Training set has {} samples and {} classes.".format(df_train.shape[0], df_train.shape[1]))
print("Testing set has {} samples and {} classes.".format(df_test.shape[0], df_test.shape[1]))
19
Data Visualization
chat_data = df_train.diagnosis.value_counts()
chat_data.plot(kind='bar');
plt.title('Sample Per Class');
plt.show()
plt.pie(chat_data, autopct='%1.1f%%', shadow=True, labels=["No DR", "Mild", "Moderate",
"Severe", "Proliferative DR"])
plt.title('Per class sample Percentage');
plt.show()
Split DataSet
x_train, x_test, y_train, y_test = train_test_split(df_train.id_code, df_train.diagnosis,
test_size=0.2,
random_state=SEED, stratify=df_train.diagnosis)
def draw_img(imgs, target_dir, class_label='0'):
fig, axis = plt.subplots(2, 6, figsize=(15, 6))
for idnx, (idx, row) in enumerate(imgs.iterrows()):
imgPath = os.path.join(dir_path, f"{target_dir}/{row['id_code']}.png")
img = cv2.imread(imgPath)
row = idnx // 6
col = idnx % 6
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
axis[row, col].imshow(img)
plt.suptitle(class_label)
plt.show()
CLASS_ID = 0
20
draw_img(df_train[df_train.diagnosis == CLASS_ID].head(12), 'train_images',
CLASSS[CLASS_ID])
CLASS_ID = 1
draw_img(df_train[df_train.diagnosis == CLASS_ID].head(12), 'train_images',
CLASSS[CLASS_ID])
CLASS_ID = 2
draw_img(df_train[df_train.diagnosis == CLASS_ID].head(12), 'train_images',
CLASSS[CLASS_ID])
CLASS_ID = 3
draw_img(df_train[df_train.diagnosis == CLASS_ID].head(12), 'train_images',
CLASSS[CLASS_ID])
CLASS_ID = 4
draw_img(df_train[df_train.diagnosis == CLASS_ID].head(12), 'train_images',
CLASSS[CLASS_ID])
CLASS_ID = 'Test DataSet'
draw_img(df_test.sample(12, random_state=SEED), 'test_images', CLASS_ID)
GrayScale Images
# Display some random images from Data Set with class categories ing gray
figure = plt.figure(figsize=(20, 16))
for target_class in (y_train.unique()):
for i, (idx, row) in enumerate(
df_train.loc[df_train.diagnosis ==
target_class].sample(5, random_state=SEED).iterrows()):
ax = figure.add_subplot(5, 5, target_class * 5 + i + 1)
21
imagefile = f"../input/aptos2019-blindness-
detection/train_images/{row['id_code']}.png" img = cv2.imread(imagefile)
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
img = cv2.resize(img, (IMG_DIM, IMG_DIM))
plt.imshow(img, cmap='gray')
ax.set_title(CLASSS[target_class])
Image Cropping
# Image Croping
def crop_image1(img,tol=7):
# img is image data
# tol is tolerance
mask = img>tol
return img[np.ix_(mask.any(1),mask.any(0))]
def crop_image_from_gray(img,tol=7):
if img.ndim== 2:
mask=img>tol
elif img.ndim==3:
gray_img=cv2.cvtColor(img,cv2.COLOR_RGB2GRAY)
mask=gray_img>tol
22
check_shape = img[:,:,0][np.ix_(mask.any(1),mask.any(0))].shape[0]
# check_shape = img[:,:,0][np.ix_(mask.any(1),mask.any(0))].shape[0]
if check_shape ==0: # Image was full dark and may be cropout everything.
return img # Return original Image
else:
img1=img[:,:,0][np.ix_(mask.any(1),mask.any(0))]
img2=img[:,:,1][np.ix_(mask.any(1),mask.any(0))]
img3=img[:,:,2][np.ix_(mask.any(1),mask.any(0))]
print(img1.shape,img2.shape,img3.shape)
img=np.stack([img1,img2,img3],axis=1)
print(img.shape)
return img
def load_ben_color(path, sigmaX=10):
image = cv2.imread(path)
# image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)S
image = crop_image_from_gray(image)
image = cv2.resize(image, (IMG_DIM, IMG_DIM))
image=cv2.addWeighted ( image,4, cv2.GaussianBlur( image , (0,0) , sigmaX) ,-4 ,128)
return image
%%time
NUM_SAMP=7
fig = plt.figure(figsize=(25, 16))
for class_id in sorted(y_train.unique()):
for i, (idx, row) in enumerate(df_train.loc[df_train['diagnosis'] ==
class_id].sample(NUM_SAMP, random_state=SEED).iterrows()):
ax = fig.add_subplot(5, NUM_SAMP, class_id * NUM_SAMP + i + 1, xticks=[],
yticks=[]) path=f"../input/aptos2019-blindness-
detection/train_images/{row['id_code']}.png" image = load_ben_color(path,sigmaX=30)
plt.imshow(image)
ax.set_title('%d-%d-%s' % (class_id, idx, row['id_code']) )
def crop_image(img,tol=7):
w, h = img.shape[1],img.shape[0]
gray_img = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
gray_img = cv2.blur(gray_img,(5,5))
shape = gray_img.shape
gray_img = gray_img.reshape(-1,1)
quant = quantile_transform(gray_img, n_quantiles=256, random_state=0, copy=True)
quant = (quant*256).astype(int)
gray_img = quant.reshape(shape)
xp = (gray_img.mean(axis=0)>tol)
24
yp = (gray_img.mean(axis=1)>tol)
x1, x2 = np.argmax(xp), w-np.argmax(np.flip(xp))
y1, y2 = np.argmax(yp), h-np.argmax(np.flip(yp))
if x1 >= x2 or y1 >= y2 : # something wrong with the
crop return img # return original image
else:
img1=img[y1:y2,x1:x2,0]
img2=img[y1:y2,x1:x2,1]
img3=img[y1:y2,x1:x2,2]
img = np.stack([img1,img2,img3],axis=-1)
return img
# Display some random images from Data Set with class categories. showig Gray image
removing other channel and adding lighting to image.
figure = plt.figure(figsize=(20, 16))
for target_class in (y_train.unique()):
# print(CLASSS[target_class],target_class)
for i, (idx, row) in enumerate(
df_train.loc[df_train.diagnosis == target_class].sample(5,
random_state=SEED).iterrows()):
ax = figure.add_subplot(5, 5, target_class * 5 + i + 1)
imagefile = f"../input/aptos2019-blindness-
detection/train_images/{row['id_code']}.png" img = cv2.imread(imagefile)
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
img = cv2.resize(img, (IMG_DIM, IMG_DIM))
img = cv2.addWeighted(img, 4, cv2.GaussianBlur(img, (0, 0), IMG_DIM / 10), -4, 128)
plt.imshow(img, cmap='gray')
ax.set_title('%s-%d-%s' % (CLASSS[target_class], idx, row['id_code']))
# print(row['id_code'])
# plt.show()
23
Data Pre-Processing
imgPath = f"../input/aptos2019-blindness-detection/train_images/cd54d022e37d.png"
img = cv2.imread(imgPath)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
_, thresh = cv2.threshold(gray, 1, 255, cv2.THRESH_BINARY)
contours, hierarchy = cv2.findContours(thresh, cv2.RETR_EXTERNAL,
cv2.CHAIN_APPROX_SIMPLE)
cnt = contours[0]
x, y, w, h = cv2.boundingRect(cnt)
img = img[y:y + h, x:x + w]
plt.imshow(img)
# print("available RAM:",
psutil.virtual_memory()) gc.collect()
# print("available RAM:", psutil.virtual_memory())
24
Image Data Generator
# Creating the imageDatagenerator Instance
datagenerator=ImageDataGenerator(#rescale=1./255,
# validation_split=0.15,
horizontal_flip=True,
vertical_flip=True,
rotation_range=40,
zoom_range=0.2,
shear_range=0.1,
fill_mode='nearest')
imgPath = f"../input/aptos2019-blindness-detection/train_images/cd54d022e37d.png"
# Loading image
img = load_img(imgPath)
data = img_to_array(img)
samples =np.expand_dims(data, 0)
i=5
it=datagenerator.flow(samples , batch_size=1)
for i in range(5):
plt.subplot(230 + 1 + i)
batch = it.next()
image = batch[0].astype('uint8')
plt.imshow(image)
plt.show()
train_generator = train_datagen.flow_from_dataframe(dataframe=df_train,
directory="../input/aptos2019-blindness-
detection/train_images/",
x_col="id_code",
y_col="diagnosis",
batch_size=BATCH_SIZE,
class_mode="categorical",
target_size=(IMG_DIM, IMG_DIM),
subset='training',
shuffle=True,
seed=SEED,
)
valid_generator = train_datagen.flow_from_dataframe(dataframe=df_train,
directory="../input/aptos2019-blindness-
detection/train_images/",
x_col="id_code",
y_col="diagnosis",
batch_size=BATCH_SIZE,
class_mode="categorical",
target_size=(IMG_DIM, IMG_DIM),
25
subset='validation',
shuffle=True,
seed=SEED
)
del x_train
# # del x_test
del y_train
# del y_test
gc.collect()
# color_mode= "grayscale",
model = design_model()
# model.summary()
26
validation_data=valid_generator,
steps_per_epoch=259,
validation_steps=35,
verbose=1,
use_multiprocessing=True,
workers=3,
shuffle=True,
max_queue_size=16,
epochs=10)
NUB_TRAIN_STEPS, NUB_VALID_STEPS
model.save("best.h5")
27
APPENDIX B: OUTPUT
28
Figure 3 DETSILS OOF TRAINING AND TESTING
Figure 4 NO DR DATA
29
Figure 6 MODERATE DR DATA
Figure 7 SEVERE DR
30
Figure 8 PROLIFERATIVE DR
Figure 10 RESULT/ACCURACY
31
REFERENCES
Ankita Guptaa, Rita Chhikarab, “Diabetic Retinopathy: Present and Past”,
International Conference on Computational Intelligence and Data Science (ICCIDS
2018)
Handayani Tjandrasa, Ricky Eka Putra, Arya Yudhi Wijaya, Isye Arieshanti,
“Classification of Non-Proliferative Diabetic Retinopathy Based on Hard Exudates
Using Soft Margin SVM ”, 2013 IEEE International Conference on Control System,
Computing and Engineering, 29 Nov. - 1 Dec. 2013, Penang, Malaysia
Jayant Yadav, Manish Sharma, Vikas Saxena, “Diabetic Retinopathy Detection using
feedforward Neural Network”, Proceedings of 2017 Tenth International Conference on
Contemporary Computing ( IC3), 10-12 August 2017, Noida, India
Shuang Yu, Di Xiao and Yogesan Kanagasingam “ Exudate Detection for Diabetic
Retinopathy With Convolutional Neural Networks.”
Mamta Arora and Mrinal Pandey of Manav Rachna University “Deep Neural Network for
Diabetic Retinopathy Detection”, 2019 International Conference on Machine Learning, Big
Data, Cloud and Parallel Computing (Com-IT-Con), India, 14th -16th Feb 2019
32