Constants¶

In [2]:
IMG_WIDTH = 4752
IMG_HEIGHT = 3168
batch_size = 32
num_classes = 5
num_epochs = 3

Load label mapping¶

In [3]:
def load_label_mapping(path='/srv/scratch/rgupta/data/DR/trainLabels.csv'):
    fname_to_label = dict()
    with open(path) as f:
        next(f)
        for line in f:
            fname, label = line.split(',')
            fname = '/srv/scratch/rgupta/data/DR/train/{0}.jpeg'.format(fname)
            fname_to_label[fname] = float(label)
    return fname_to_label

DRSequence object¶

In [4]:
import keras as K
from keras.utils import Sequence
from skimage.io import imread
from skimage.transform import resize
import numpy as np
Using TensorFlow backend.
In [5]:
class DRSequence(Sequence):
    def __init__(self, x_set, y_set, batch_size):
        self.X, self.y = x_set, y_set
        self.batch_size = batch_size
        
    
    def __len__(self):
        return len(self.X) // self.batch_size
    
    
    def __getitem__(self, idx):
        batch_x = self.X[idx*self.batch_size:(idx+1)*self.batch_size]
        batch_y = self.y[idx*self.batch_size:(idx+1)*self.batch_size]
        
        return np.array([
            resize(imread(file_name), (396, 264, 3))
            for file_name in batch_x]), np.array(batch_y)
In [6]:
train_fnames_labels = load_label_mapping()
X_train_fnames, y_train = zip(*train_fnames_labels.items())
y_train = K.utils.to_categorical(y_train, num_classes)
In [7]:
trainDRSequence = DRSequence(X_train_fnames, y_train, batch_size)

Create model¶

In [8]:
from keras.models import Sequential
from keras.layers import Dense, Activation, Flatten, BatchNormalization
from keras.layers import Conv2D, MaxPooling2D
from keras.layers import noise
from keras.callbacks import TensorBoard
In [9]:
model = Sequential()

model.add(Conv2D(32, (2, 2), padding='same', input_shape=(396, 264, 3)))
model.add(Activation('selu'))
model.add(BatchNormalization())
model.add(noise.GaussianDropout(0.4))
model.add(MaxPooling2D(pool_size=(2, 2)))

model.add(Conv2D(64, (3, 3), padding='same'))
model.add(Activation('selu'))
model.add(BatchNormalization())
model.add(noise.GaussianDropout(0.4))
model.add(MaxPooling2D(pool_size=(2, 2)))

model.add(Flatten())
model.add(Dense(256))
model.add(Activation('selu'))
model.add(BatchNormalization())
model.add(Dense(num_classes))
model.add(Activation('softmax'))
In [ ]:
opt = K.optimizers.rmsprop(lr=0.001, decay=1e-5)

tb_callback = TensorBoard(log_dir='./logs', batch_size=batch_size, write_graph=True, write_grads=True)

model.compile(loss='categorical_crossentropy',
             optimizer=opt,
             metrics=['categorical_accuracy'])
In [ ]:
model.fit_generator(trainDRSequence,
                   steps_per_epoch=len(trainDRSequence),
                   epochs=num_epochs,
                   workers=8,
                   use_multiprocessing=True,
                   callbacks=[tb_callback],
                   verbose=1)
/opt/anaconda3/lib/python3.6/site-packages/skimage/transform/_warps.py:84: UserWarning: The default mode, 'constant', will be changed to 'reflect' in skimage 0.15.
  warn("The default mode, 'constant', will be changed to 'reflect' in "
/opt/anaconda3/lib/python3.6/site-packages/skimage/transform/_warps.py:84: UserWarning: The default mode, 'constant', will be changed to 'reflect' in skimage 0.15.
  warn("The default mode, 'constant', will be changed to 'reflect' in "
/opt/anaconda3/lib/python3.6/site-packages/skimage/transform/_warps.py:84: UserWarning: The default mode, 'constant', will be changed to 'reflect' in skimage 0.15.
  warn("The default mode, 'constant', will be changed to 'reflect' in "
/opt/anaconda3/lib/python3.6/site-packages/skimage/transform/_warps.py:84: UserWarning: The default mode, 'constant', will be changed to 'reflect' in skimage 0.15.
  warn("The default mode, 'constant', will be changed to 'reflect' in "
/opt/anaconda3/lib/python3.6/site-packages/skimage/transform/_warps.py:84: UserWarning: The default mode, 'constant', will be changed to 'reflect' in skimage 0.15.
  warn("The default mode, 'constant', will be changed to 'reflect' in "
Epoch 1/3
/opt/anaconda3/lib/python3.6/site-packages/skimage/transform/_warps.py:84: UserWarning: The default mode, 'constant', will be changed to 'reflect' in skimage 0.15.
  warn("The default mode, 'constant', will be changed to 'reflect' in "
/opt/anaconda3/lib/python3.6/site-packages/skimage/transform/_warps.py:84: UserWarning: The default mode, 'constant', will be changed to 'reflect' in skimage 0.15.
  warn("The default mode, 'constant', will be changed to 'reflect' in "
/opt/anaconda3/lib/python3.6/site-packages/skimage/transform/_warps.py:84: UserWarning: The default mode, 'constant', will be changed to 'reflect' in skimage 0.15.
  warn("The default mode, 'constant', will be changed to 'reflect' in "
1097/1097 [==============================] - 849s - loss: 0.9288 - categorical_accuracy: 0.7192   
Epoch 2/3
 278/1097 [======>.......................] - ETA: 626s - loss: 0.8810 - categorical_accuracy: 0.7262
In [ ]: