[302dd3]: / deeplearn-approach / train_model.py

Download this file

419 lines (371 with data), 15.7 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
'''
This function function used for training and cross-validating model using. The database is not
included in this repo, please download the CinC Challenge database and truncate/pad data into a
NxM matrix array, being N the number of recordings and M the window accepted by the network (i.e.
30 seconds).
For more information visit: https://github.com/fernandoandreotti/cinc-challenge2017
Referencing this work
Andreotti, F., Carr, O., Pimentel, M.A.F., Mahdi, A., & De Vos, M. (2017). Comparing Feature Based
Classifiers and Convolutional Neural Networks to Detect Arrhythmia from Short Segments of ECG. In
Computing in Cardiology. Rennes (France).
--
cinc-challenge2017, version 1.0, Sept 2017
Last updated : 27-09-2017
Released under the GNU General Public License
Copyright (C) 2017 Fernando Andreotti, Oliver Carr, Marco A.F. Pimentel, Adam Mahdi, Maarten De Vos
University of Oxford, Department of Engineering Science, Institute of Biomedical Engineering
fernando.andreotti@eng.ox.ac.uk
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
'''
import matplotlib.pyplot as plt
import tensorflow as tf
import numpy as np
import scipy.io
import gc
import itertools
from sklearn.metrics import confusion_matrix
import sys
sys.path.insert(0, './preparation')
# Keras imports
import keras
from keras.models import Model
from keras.layers import Input, Conv1D, Dense, Flatten, Dropout,MaxPooling1D, Activation, BatchNormalization
from keras.callbacks import EarlyStopping, ModelCheckpoint
from keras.utils import plot_model
from keras import backend as K
from keras.callbacks import Callback,warnings
###################################################################
### Callback method for reducing learning rate during training ###
###################################################################
class AdvancedLearnignRateScheduler(Callback):
'''
# Arguments
monitor: quantity to be monitored.
patience: number of epochs with no improvement
after which training will be stopped.
verbose: verbosity mode.
mode: one of {auto, min, max}. In 'min' mode,
training will stop when the quantity
monitored has stopped decreasing; in 'max'
mode it will stop when the quantity
monitored has stopped increasing.
'''
def __init__(self, monitor='val_loss', patience=0,verbose=0, mode='auto', decayRatio=0.1):
super(Callback, self).__init__()
self.monitor = monitor
self.patience = patience
self.verbose = verbose
self.wait = 0
self.decayRatio = decayRatio
if mode not in ['auto', 'min', 'max']:
warnings.warn('Mode %s is unknown, '
'fallback to auto mode.'
% (self.mode), RuntimeWarning)
mode = 'auto'
if mode == 'min':
self.monitor_op = np.less
self.best = np.Inf
elif mode == 'max':
self.monitor_op = np.greater
self.best = -np.Inf
else:
if 'acc' in self.monitor:
self.monitor_op = np.greater
self.best = -np.Inf
else:
self.monitor_op = np.less
self.best = np.Inf
def on_epoch_end(self, epoch, logs={}):
current = logs.get(self.monitor)
current_lr = K.get_value(self.model.optimizer.lr)
print("\nLearning rate:", current_lr)
if current is None:
warnings.warn('AdvancedLearnignRateScheduler'
' requires %s available!' %
(self.monitor), RuntimeWarning)
if self.monitor_op(current, self.best):
self.best = current
self.wait = 0
else:
if self.wait >= self.patience:
if self.verbose > 0:
print('\nEpoch %05d: reducing learning rate' % (epoch))
assert hasattr(self.model.optimizer, 'lr'), \
'Optimizer must have a "lr" attribute.'
current_lr = K.get_value(self.model.optimizer.lr)
new_lr = current_lr * self.decayRatio
K.set_value(self.model.optimizer.lr, new_lr)
self.wait = 0
self.wait += 1
###########################################
## Function to plot confusion matrices ##
#########################################
def plot_confusion_matrix(cm, classes,
normalize=False,
title='Confusion matrix',
cmap=plt.cm.Blues):
"""
This function prints and plots the confusion matrix.
Normalization can be applied by setting `normalize=True`.
"""
if normalize:
cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis]
print("Normalized confusion matrix")
else:
print('Confusion matrix, without normalization')
cm = np.around(cm, decimals=3)
print(cm)
thresh = cm.max() / 2.
for i, j in itertools.product(range(cm.shape[0]), range(cm.shape[1])):
plt.text(j, i, cm[i, j],
horizontalalignment="center",
color="white" if cm[i, j] > thresh else "black")
plt.imshow(cm, interpolation='nearest', cmap=cmap)
plt.title(title)
plt.colorbar()
tick_marks = np.arange(len(classes))
plt.xticks(tick_marks, classes, rotation=45)
plt.yticks(tick_marks, classes)
plt.tight_layout()
plt.ylabel('True label')
plt.xlabel('Predicted label')
plt.savefig('confusion.eps', format='eps', dpi=1000)
#####################################
## Model definition ##
## ResNet based on Rajpurkar ##
##################################
def ResNet_model(WINDOW_SIZE):
# Add CNN layers left branch (higher frequencies)
# Parameters from paper
INPUT_FEAT = 1
OUTPUT_CLASS = 4 # output classes
k = 1 # increment every 4th residual block
p = True # pool toggle every other residual block (end with 2^8)
convfilt = 64
convstr = 1
ksize = 16
poolsize = 2
poolstr = 2
drop = 0.5
# Modelling with Functional API
#input1 = Input(shape=(None,1), name='input')
input1 = Input(shape=(WINDOW_SIZE,INPUT_FEAT), name='input')
## First convolutional block (conv,BN, relu)
x = Conv1D(filters=convfilt,
kernel_size=ksize,
padding='same',
strides=convstr,
kernel_initializer='he_normal')(input1)
x = BatchNormalization()(x)
x = Activation('relu')(x)
## Second convolutional block (conv, BN, relu, dropout, conv) with residual net
# Left branch (convolutions)
x1 = Conv1D(filters=convfilt,
kernel_size=ksize,
padding='same',
strides=convstr,
kernel_initializer='he_normal')(x)
x1 = BatchNormalization()(x1)
x1 = Activation('relu')(x1)
x1 = Dropout(drop)(x1)
x1 = Conv1D(filters=convfilt,
kernel_size=ksize,
padding='same',
strides=convstr,
kernel_initializer='he_normal')(x1)
x1 = MaxPooling1D(pool_size=poolsize,
strides=poolstr)(x1)
# Right branch, shortcut branch pooling
x2 = MaxPooling1D(pool_size=poolsize,
strides=poolstr)(x)
# Merge both branches
x = keras.layers.add([x1, x2])
del x1,x2
## Main loop
p = not p
for l in range(15):
if (l%4 == 0) and (l>0): # increment k on every fourth residual block
k += 1
# increase depth by 1x1 Convolution case dimension shall change
xshort = Conv1D(filters=convfilt*k,kernel_size=1)(x)
else:
xshort = x
# Left branch (convolutions)
# notice the ordering of the operations has changed
x1 = BatchNormalization()(x)
x1 = Activation('relu')(x1)
x1 = Dropout(drop)(x1)
x1 = Conv1D(filters=convfilt*k,
kernel_size=ksize,
padding='same',
strides=convstr,
kernel_initializer='he_normal')(x1)
x1 = BatchNormalization()(x1)
x1 = Activation('relu')(x1)
x1 = Dropout(drop)(x1)
x1 = Conv1D(filters=convfilt*k,
kernel_size=ksize,
padding='same',
strides=convstr,
kernel_initializer='he_normal')(x1)
if p:
x1 = MaxPooling1D(pool_size=poolsize,strides=poolstr)(x1)
# Right branch: shortcut connection
if p:
x2 = MaxPooling1D(pool_size=poolsize,strides=poolstr)(xshort)
else:
x2 = xshort # pool or identity
# Merging branches
x = keras.layers.add([x1, x2])
# change parameters
p = not p # toggle pooling
# Final bit
x = BatchNormalization()(x)
x = Activation('relu')(x)
x = Flatten()(x)
#x = Dense(1000)(x)
#x = Dense(1000)(x)
out = Dense(OUTPUT_CLASS, activation='softmax')(x)
model = Model(inputs=input1, outputs=out)
model.compile(optimizer='adam',
loss='categorical_crossentropy',
metrics=['accuracy'])
#model.summary()
#sequential_model_to_ascii_printout(model)
plot_model(model, to_file='model.png')
return model
###########################################################
## Function to perform K-fold Crossvalidation on model ##
##########################################################
def model_eval(X,y):
batch =64
epochs = 20
rep = 1 # K fold procedure can be repeated multiple times
Kfold = 5
Ntrain = 8528 # number of recordings on training set
Nsamp = int(Ntrain/Kfold) # number of recordings to take as validation
# Need to add dimension for training
X = np.expand_dims(X, axis=2)
classes = ['A', 'N', 'O', '~']
Nclass = len(classes)
cvconfusion = np.zeros((Nclass,Nclass,Kfold*rep))
cvscores = []
counter = 0
# repetitions of cross validation
for r in range(rep):
print("Rep %d"%(r+1))
# cross validation loop
for k in range(Kfold):
print("Cross-validation run %d"%(k+1))
# Callbacks definition
callbacks = [
# Early stopping definition
EarlyStopping(monitor='val_loss', patience=3, verbose=1),
# Decrease learning rate by 0.1 factor
AdvancedLearnignRateScheduler(monitor='val_loss', patience=1,verbose=1, mode='auto', decayRatio=0.1),
# Saving best model
ModelCheckpoint('weights-best_k{}_r{}.hdf5'.format(k,r), monitor='val_loss', save_best_only=True, verbose=1),
]
# Load model
model = ResNet_model(WINDOW_SIZE)
# split train and validation sets
idxval = np.random.choice(Ntrain, Nsamp,replace=False)
idxtrain = np.invert(np.in1d(range(X_train.shape[0]),idxval))
ytrain = y[np.asarray(idxtrain),:]
Xtrain = X[np.asarray(idxtrain),:,:]
Xval = X[np.asarray(idxval),:,:]
yval = y[np.asarray(idxval),:]
# Train model
model.fit(Xtrain, ytrain,
validation_data=(Xval, yval),
epochs=epochs, batch_size=batch,callbacks=callbacks)
# Evaluate best trained model
model.load_weights('weights-best_k{}_r{}.hdf5'.format(k,r))
ypred = model.predict(Xval)
ypred = np.argmax(ypred,axis=1)
ytrue = np.argmax(yval,axis=1)
cvconfusion[:,:,counter] = confusion_matrix(ytrue, ypred)
F1 = np.zeros((4,1))
for i in range(4):
F1[i]=2*cvconfusion[i,i,counter]/(np.sum(cvconfusion[i,:,counter])+np.sum(cvconfusion[:,i,counter]))
print("F1 measure for {} rhythm: {:1.4f}".format(classes[i],F1[i,0]))
cvscores.append(np.mean(F1)* 100)
print("Overall F1 measure: {:1.4f}".format(np.mean(F1)))
K.clear_session()
gc.collect()
config = tf.ConfigProto()
config.gpu_options.allow_growth=True
sess = tf.Session(config=config)
K.set_session(sess)
counter += 1
# Saving cross validation results
scipy.io.savemat('xval_results.mat',mdict={'cvconfusion': cvconfusion.tolist()})
return model
###########################
## Function to load data ##
###########################
def loaddata(WINDOW_SIZE):
'''
Load training/test data into workspace
This function assumes you have downloaded and padded/truncated the
training set into a local file named "trainingset.mat". This file should
contain the following structures:
- trainset: NxM matrix of N ECG segments with length M
- traintarget: Nx4 matrix of coded labels where each column contains
one in case it matches ['A', 'N', 'O', '~'].
'''
print("Loading data training set")
matfile = scipy.io.loadmat('trainingset.mat')
X = matfile['trainset']
y = matfile['traintarget']
# Merging datasets
# Case other sets are available, load them then concatenate
#y = np.concatenate((traintarget,augtarget),axis=0)
#X = np.concatenate((trainset,augset),axis=0)
X = X[:,0:WINDOW_SIZE]
return (X, y)
#####################
# Main function ##
###################
config = tf.ConfigProto(allow_soft_placement=True)
config.gpu_options.allow_growth = True
sess = tf.Session(config=config)
seed = 7
np.random.seed(seed)
# Parameters
FS = 300
WINDOW_SIZE = 30*FS # padding window for CNN
# Loading data
(X_train,y_train) = loaddata(WINDOW_SIZE)
# Training model
model = model_eval(X_train,y_train)
# Outputing results of cross validation
matfile = scipy.io.loadmat('xval_results.mat')
cv = matfile['cvconfusion']
F1mean = np.zeros(cv.shape[2])
for j in range(cv.shape[2]):
classes = ['A', 'N', 'O', '~']
F1 = np.zeros((4,1))
for i in range(4):
F1[i]=2*cv[i,i,j]/(np.sum(cv[i,:,j])+np.sum(cv[:,i,j]))
print("F1 measure for {} rhythm: {:1.4f}".format(classes[i],F1[i,0]))
F1mean[j] = np.mean(F1)
print("mean F1 measure for: {:1.4f}".format(F1mean[j]))
print("Overall F1 : {:1.4f}".format(np.mean(F1mean)))
# Plotting confusion matrix
cvsum = np.sum(cv,axis=2)
for i in range(4):
F1[i]=2*cvsum[i,i]/(np.sum(cvsum[i,:])+np.sum(cvsum[:,i]))
print("F1 measure for {} rhythm: {:1.4f}".format(classes[i],F1[i,0]))
F1mean = np.mean(F1)
print("mean F1 measure for: {:1.4f}".format(F1mean))
plot_confusion_matrix(cvsum, classes,normalize=True,title='Confusion matrix')