IMG_WIDTH = 4752
IMG_HEIGHT = 3168
batch_size = 32
num_classes = 5
num_epochs = 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
import keras as K
from keras.utils import Sequence
from skimage.io import imread
from skimage.transform import resize
import numpy as np
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)
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)
trainDRSequence = DRSequence(X_train_fnames, y_train, batch_size)
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
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'))
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'])
model.fit_generator(trainDRSequence,
steps_per_epoch=len(trainDRSequence),
epochs=num_epochs,
workers=8,
use_multiprocessing=True,
callbacks=[tb_callback],
verbose=1)