-
Notifications
You must be signed in to change notification settings - Fork 1
/
convnets.py
311 lines (259 loc) · 13.4 KB
/
convnets.py
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
#!/usr/bin/env python
from __future__ import print_function
import sys
import gc
import os
import time
import yaml
import traceback
import numpy as np
import theano
import theano.tensor as T
import lasagne
from data_utils import MRIDataIterator
def build_cnn(input_var=None, batch_size=5, meta_data_input_var=None):
"""number_of_buckets: Is the number of histogram buckets we have created.
We treat these like layers for the convolution,
filling in the missing layers with 0s. We also throw
out slices that are probably from the same location
"""
# Input layer, as usual:
# (number of frames in cardiac cycle x number_of_buckets x image_width x image_height)
# (30 x 10 x 64 x 64)
network = lasagne.layers.InputLayer(shape=(None, 1, 64, 64),
input_var=input_var)
# metadata = gender, age (in years), lower_bound, mean, upper_bound for age group
meta_data_network = lasagne.layers.InputLayer(shape=(None, 8), input_var=meta_data_input_var)
# This time we do not apply input dropout, as it tends to work less well
# for convolutional layers.
# Convolutional layer with 32 kernels of size 5x5. Strided and padded
# convolutions are supported as well; see the docstring.
network = lasagne.layers.Conv2DLayer(
network, num_filters=64, filter_size=(3, 3),
nonlinearity=lasagne.nonlinearities.rectify,
W=lasagne.init.GlorotUniform())
network = lasagne.layers.MaxPool2DLayer(network, pool_size=(2, 2))
network = lasagne.layers.DropoutLayer(network, p=.25)
# Another convolution with 32 5x5 kernels, and another 2x2 pooling:
network = lasagne.layers.Conv2DLayer(
network, num_filters=96, filter_size=(3, 3),
nonlinearity=lasagne.nonlinearities.rectify)
network = lasagne.layers.MaxPool2DLayer(network, pool_size=(2, 2))
network = lasagne.layers.DropoutLayer(network, p=.25)
# Another convolution with 32 5x5 kernels, and another 2x2 pooling:
network = lasagne.layers.Conv2DLayer(
network, num_filters=128, filter_size=(2, 2),
nonlinearity=lasagne.nonlinearities.rectify)
network = lasagne.layers.MaxPool2DLayer(network, pool_size=(2, 2))
network = lasagne.layers.DropoutLayer(network, p=.25)
#should now be (30 x (64*64*10))
network = lasagne.layers.Conv2DLayer(
network, num_filters=256, filter_size=(2, 2),
nonlinearity=lasagne.nonlinearities.rectify)
network = lasagne.layers.MaxPool2DLayer(network, pool_size=(2, 2))
network = lasagne.layers.DropoutLayer(network, p=.25)
network = lasagne.layers.FlattenLayer(network, 2)
print("After flatter, dims: {}".format(network.output_shape))
# need to get it to (1 x 30 x (64*64*10))
# With batching need to somehow reshape from (30*batch_size, 1, 64, 64) to
# (batch_size, 30, 1024)
network = lasagne.layers.ReshapeLayer(network, (-1, 30, [1]))
#print("After reshape, dims: {}".format(network.output_shape))
network = lasagne.layers.LSTMLayer(
network, 1024, grad_clipping=100,
nonlinearity=lasagne.nonlinearities.tanh)
#print("After lstm, dims: {}".format(network.output_shape))
# The l_forward layer creates an output of dimension (batch_size, SEQ_LENGTH, N_HIDDEN)
# Since we are only interested in the final prediction, we isolate that quantity and feed it to the next layer.
# The output of the sliced layer will then be of size (batch_size, N_HIDDEN)
# network = lasagne.layers.SliceLayer(network, -1, 1)
# print("After slice, dims: {}".format(network.output_shape))
# A fully-connected layer of 1024 units with 50% dropout on its inputs:
network = lasagne.layers.DenseLayer(
lasagne.layers.dropout(network, p=.5),
num_units=712,
nonlinearity=lasagne.nonlinearities.tanh)
meta_data_network = lasagne.layers.DenseLayer(
lasagne.layers.dropout(meta_data_network, p=.5),
num_units=256,
nonlinearity=lasagne.nonlinearities.tanh)
meta_data_network = lasagne.layers.DenseLayer(
lasagne.layers.dropout(meta_data_network, p=.5),
num_units=512,
nonlinearity=lasagne.nonlinearities.tanh)
meta_data_network = lasagne.layers.DenseLayer(
lasagne.layers.dropout(meta_data_network, p=.5),
num_units=768,
nonlinearity=lasagne.nonlinearities.tanh)
# And, finally, the 600-unit output layer with 50% dropout on its inputs:
network = lasagne.layers.DenseLayer(
lasagne.layers.dropout(network, p=.5),
num_units=768,
nonlinearity=lasagne.nonlinearities.tanh)
network = lasagne.layers.ElemwiseSumLayer([network, meta_data_network])
network = lasagne.layers.DenseLayer(
lasagne.layers.dropout(network, p=.5),
num_units=1024,
nonlinearity=lasagne.nonlinearities.softmax)
network = lasagne.layers.DenseLayer(
lasagne.layers.dropout(network, p=.25),
num_units=600,
nonlinearity=lasagne.nonlinearities.softmax)
return network
def compose_functions(scope="default", batch_size=5):
# Prepare Theano variables for inputs and targets
input_var = T.tensor4(scope + 'inputs')
meta_data_input_var = T.matrix(scope + 'metadata_inputs')
target_var = T.ivector(scope + 'targets')
#x_printed = theano.printing.Print('this is a very important value')(target_var)
network = build_cnn(input_var, batch_size, meta_data_input_var)
# Create a loss expression for training, i.e., a scalar objective we want
# to minimize (for our multi-class problem, it is the cross-entropy loss):
prediction = lasagne.layers.get_output(network)
#prediction_printed = theano.printing.Print('this is a very important prediction')(prediction)
loss = lasagne.objectives.categorical_crossentropy(prediction, target_var).mean()
# loss = loss.mean()
# We could add some weight decay as well here, see lasagne.regularization.
# Create update expressions for training, i.e., how to modify the
# parameters at each training step. Here, we'll use Stochastic Gradient
# Descent (SGD) with Nesterov momentum, but Lasagne offers plenty more.
params = lasagne.layers.get_all_params(network, trainable=True)
updates = lasagne.updates.nesterov_momentum(
loss, params, learning_rate=0.01, momentum=0.9)
# Create a loss expression for validation/testing. The crucial difference
# here is that we do a deterministic forward pass through the network,
# disabling dropout layers.
#TODO: this should actually be CPRS
test_prediction = lasagne.layers.get_output(network, deterministic=True)
test_loss = lasagne.objectives.categorical_crossentropy(test_prediction,
target_var)
test_loss = test_loss.mean()
# As a bonus, also create an expression for the classification accuracy:
# test_acc = T.mean(T.eq(T.argmax(test_prediction, axis=1), target_var),
# dtype=theano.config.floatX)
# print(len(test_prediction))
# print(test_prediction.shape)
# v = np.array(range(test_prediction.shape[0]))
# h = (v >= V_m) * -1.
# sq_dists = (test_prediction - h)**2
# test_acc = T.sqr(T.sum(p, h))
# Compile a function performing a training step on a mini-batch (by giving
# the updates dictionary) and returning the corresponding training loss:
train_fn = theano.function([input_var, target_var, meta_data_input_var], loss, updates=updates)
# Compile a second function computing the validation loss and accuracy:
val_fn = theano.function([input_var, target_var, meta_data_input_var], [test_loss, test_prediction])
return network, train_fn, val_fn
# ############################## Main program ################################
# Everything else will be handled in our main program now. We could pull out
# more functions to better separate the code, but it wouldn't make it any
# easier to read.
def main(num_epochs=30):
# Load the dataset
print("Creating data iterator...")
with open("config.yml", 'r') as ymlfile:
cfg = yaml.load(ymlfile)
train_dir = cfg['dataset_paths']['train_data']
train_labels = cfg['dataset_paths']['train_labels']
batch_size = 5
mriIter = MRIDataIterator(train_dir, train_labels)
network, train_fn, val_fn = compose_functions("systole", batch_size) #systole
network_dia, train_fn_dia, val_fn_dia = compose_functions("diastole", batch_size)
if os.path.exists('model-sys.npz'):
with np.load('model-sys.npz') as f:
param_values = [f['arr_%d' % i] for i in range(len(f.files))]
lasagne.layers.set_all_param_values(network, param_values)
if os.path.exists('model-dia.npz'):
with np.load('model-dia.npz') as f:
param_values = [f['arr_%d' % i] for i in range(len(f.files))]
lasagne.layers.set_all_param_values(network_dia, param_values)
# Finally, launch the training loop.
print("Starting training...")
# We iterate over epochs:
for epoch in range(num_epochs):
# In each epoch, we do a full pass over the training data:
train_err_sys = 0
train_err_dia = 0
train_batches = 0
training_index = 1
validation_index = mriIter.last_training_index + 1
start_time = time.time()
while mriIter.has_more_training_data(training_index + batch_size):
gc.collect()
print("Training index %s" % training_index)
inputs, systole, diastole, metadata = mriIter.get_median_bucket_data(training_index, batch_size, return_gender_age=True)
train_err_sys += train_fn(inputs, systole, metadata)
train_err_dia += train_fn_dia(inputs, diastole, metadata)
train_batches += batch_size
training_index += batch_size
augmented_training_index = training_index
while (augmented_training_index < 500):
gc.collect()
print("Augmented training index: %s" % augmented_training_index)
inputs, systole, diastole, metadata= mriIter.get_augmented_data(augmented_training_index, training_index - batch_size, return_gender_age=True)
train_err_sys += train_fn(inputs, systole, metadata)
train_err_dia += train_fn_dia(inputs, diastole, metadata)
augmented_training_index += batch_size
# And a full pass over the validation data:
val_err_sys = 0
val_acc_sys = 0
val_err_dia = 0
val_acc_dia = 0
val_batches = 0
while mriIter.has_more_data(validation_index):
gc.collect()
print("Validation index %s" % validation_index)
inputs, systole, diastole, metadata= mriIter.get_median_bucket_data(validation_index, batch_size, return_gender_age=True)
# systole, diastole = targets
err, prediction = val_fn(inputs, systole, metadata)
y = 0
for prob_set in prediction:
prob_dist = np.cumsum(prob_set)
v = np.array(range(prediction.shape[1]))
heavy = (v >= systole[y])
sq_dists = (prob_dist - heavy)**2
# print(prediction.shape)
val_err_sys += err
val_acc_sys += (sum(sq_dists) / 600.)
y += 1
err, prediction = val_fn_dia(inputs, systole, metadata)
y = 0
for prob_set in prediction:
prob_dist = np.cumsum(prob_set)
v = np.array(range(prediction.shape[1]))
heavy = (v >= diastole[y])
sq_dists = (prob_dist - heavy)**2
val_err_dia += err
val_acc_dia += (sum(sq_dists) / 600.)
y += 1
val_batches += batch_size
validation_index += batch_size
# Then we print the results for this epoch:
print("Epoch {} of {} took {:.3f}s".format(
epoch + 1, num_epochs, time.time() - start_time))
# print(" training loss:\t\t{:.6f}".format(train_err / train_batches))
# print(" validation loss:\t\t{:.6f}".format(val_err / val_batches))
print("Validation Sum Sqrts Systolic: {}".format(val_acc_sys))
print("Validation Sum Sqrts Diastolic: {}".format(val_acc_dia))
print("Train Err Systole: {}".format(train_err_sys))
print("Train Err Diastole: {}".format(train_err_dia))
print("CRPS:\t\t{:.6f} %".format(
(val_acc_sys + val_acc_dia) / (val_batches) * .5))
# Optionally, you could now dump the network weights to a file like this:
np.savez('model-sys.npz', *lasagne.layers.get_all_param_values(network))
np.savez('model-dia.npz', *lasagne.layers.get_all_param_values(network_dia))
#
# And load them again later on like this:
# with np.load('model.npz') as f:
# param_values = [f['arr_%d' % i] for i in range(len(f.files))]
# lasagne.layers.set_all_param_values(network, param_values)
if __name__ == '__main__':
if ('--help' in sys.argv) or ('-h' in sys.argv):
print("Trains a neural network on MNIST using Lasagne.")
print("Usage: %s [MODEL [EPOCHS]]" % sys.argv[0])
print()
print("EPOCHS: number of training epochs to perform (default: 1)")
else:
kwargs = {}
if len(sys.argv) > 1:
kwargs['num_epochs'] = int(sys.argv[1])
main(**kwargs)