-
Notifications
You must be signed in to change notification settings - Fork 17
/
auto_diagnosis.py
389 lines (358 loc) · 15.9 KB
/
auto_diagnosis.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
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
import logging
import time
from copy import copy
import sys
import numpy as np
from numpy.random import RandomState
import resampy
from torch import optim
import torch.nn.functional as F
import torch as th
from torch.nn.functional import elu
from torch import nn
from braindecode.datautil.signal_target import SignalAndTarget
from braindecode.torch_ext.util import np_to_var
from braindecode.torch_ext.util import set_random_seeds
from braindecode.torch_ext.modules import Expression
from braindecode.experiments.experiment import Experiment
from braindecode.datautil.iterators import CropsFromTrialsIterator
from braindecode.experiments.monitors import (RuntimeMonitor, LossMonitor,
MisclassMonitor)
from braindecode.experiments.stopcriteria import MaxEpochs
from braindecode.models.shallow_fbcsp import ShallowFBCSPNet
from braindecode.models.deep4 import Deep4Net
from braindecode.models.util import to_dense_prediction_model
from braindecode.datautil.iterators import get_balanced_batches
from braindecode.torch_ext.constraints import MaxNormDefaultConstraint
from braindecode.torch_ext.util import var_to_np
from braindecode.torch_ext.functions import identity
from dataset import DiagnosisSet
from monitors import compute_preds_per_trial, CroppedDiagnosisMonitor
log = logging.getLogger(__name__)
log.setLevel('DEBUG')
def create_set(X, y, inds):
"""
X list and y nparray
:return:
"""
new_X = []
for i in inds:
new_X.append(X[i])
new_y = y[inds]
return SignalAndTarget(new_X, new_y)
class TrainValidTestSplitter(object):
def __init__(self, n_folds, i_test_fold, shuffle):
self.n_folds = n_folds
self.i_test_fold = i_test_fold
self.rng = RandomState(39483948)
self.shuffle = shuffle
def split(self, X, y,):
if len(X) < self.n_folds:
raise ValueError("Less Trials: {:d} than folds: {:d}".format(
len(X), self.n_folds
))
folds = get_balanced_batches(len(X), self.rng, self.shuffle,
n_batches=self.n_folds)
test_inds = folds[self.i_test_fold]
valid_inds = folds[self.i_test_fold - 1]
all_inds = list(range(len(X)))
train_inds = np.setdiff1d(all_inds, np.union1d(test_inds, valid_inds))
assert np.intersect1d(train_inds, valid_inds).size == 0
assert np.intersect1d(train_inds, test_inds).size == 0
assert np.intersect1d(valid_inds, test_inds).size == 0
assert np.array_equal(np.sort(
np.union1d(train_inds, np.union1d(valid_inds, test_inds))),
all_inds)
train_set = create_set(X, y, train_inds)
valid_set = create_set(X, y, valid_inds)
test_set = create_set(X, y, test_inds)
return train_set, valid_set, test_set
class TrainValidSplitter(object):
def __init__(self, n_folds, i_valid_fold, shuffle):
self.n_folds = n_folds
self.i_valid_fold = i_valid_fold
self.rng = RandomState(39483948)
self.shuffle = shuffle
def split(self, X, y):
if len(X) < self.n_folds:
raise ValueError("Less Trials: {:d} than folds: {:d}".format(
len(X), self.n_folds
))
folds = get_balanced_batches(len(X), self.rng, self.shuffle,
n_batches=self.n_folds)
valid_inds = folds[self.i_valid_fold]
all_inds = list(range(len(X)))
train_inds = np.setdiff1d(all_inds, valid_inds)
assert np.intersect1d(train_inds, valid_inds).size == 0
assert np.array_equal(np.sort(np.union1d(train_inds, valid_inds)),
all_inds)
train_set = create_set(X, y, train_inds)
valid_set = create_set(X, y, valid_inds)
return train_set, valid_set
def run_exp(data_folders,
n_recordings,
sensor_types,
n_chans,
max_recording_mins,
sec_to_cut, duration_recording_mins,
test_recording_mins,
max_abs_val,
sampling_freq,
divisor,
test_on_eval,
n_folds, i_test_fold,
shuffle,
model_name,
n_start_chans, n_chan_factor,
input_time_length, final_conv_length,
model_constraint,
init_lr,
batch_size, max_epochs,cuda,):
import torch.backends.cudnn as cudnn
cudnn.benchmark = True
preproc_functions = []
preproc_functions.append(
lambda data, fs: (data[:, int(sec_to_cut * fs):-int(
sec_to_cut * fs)], fs))
preproc_functions.append(
lambda data, fs: (data[:, :int(duration_recording_mins * 60 * fs)], fs))
if max_abs_val is not None:
preproc_functions.append(lambda data, fs:
(np.clip(data, -max_abs_val, max_abs_val), fs))
preproc_functions.append(lambda data, fs: (resampy.resample(data, fs,
sampling_freq,
axis=1,
filter='kaiser_fast'),
sampling_freq))
if divisor is not None:
preproc_functions.append(lambda data, fs: (data / divisor, fs))
dataset = DiagnosisSet(n_recordings=n_recordings,
max_recording_mins=max_recording_mins,
preproc_functions=preproc_functions,
data_folders=data_folders,
train_or_eval='train',
sensor_types=sensor_types)
if test_on_eval:
if test_recording_mins is None:
test_recording_mins = duration_recording_mins
test_preproc_functions = copy(preproc_functions)
test_preproc_functions[1] = lambda data, fs: (
data[:, :int(test_recording_mins * 60 * fs)], fs)
test_dataset = DiagnosisSet(n_recordings=n_recordings,
max_recording_mins=None,
preproc_functions=test_preproc_functions,
data_folders=data_folders,
train_or_eval='eval',
sensor_types=sensor_types)
X,y = dataset.load()
max_shape = np.max([list(x.shape) for x in X],
axis=0)
assert max_shape[1] == int(duration_recording_mins *
sampling_freq * 60)
if test_on_eval:
test_X, test_y = test_dataset.load()
max_shape = np.max([list(x.shape) for x in test_X],
axis=0)
assert max_shape[1] == int(test_recording_mins *
sampling_freq * 60)
if not test_on_eval:
splitter = TrainValidTestSplitter(n_folds, i_test_fold,
shuffle=shuffle)
train_set, valid_set, test_set = splitter.split(X, y)
else:
splitter = TrainValidSplitter(n_folds, i_valid_fold=i_test_fold,
shuffle=shuffle)
train_set, valid_set = splitter.split(X, y)
test_set = SignalAndTarget(test_X, test_y)
del test_X, test_y
del X,y # shouldn't be necessary, but just to make sure
set_random_seeds(seed=20170629, cuda=cuda)
n_classes = 2
if model_name == 'shallow':
model = ShallowFBCSPNet(in_chans=n_chans, n_classes=n_classes,
n_filters_time=n_start_chans,
n_filters_spat=n_start_chans,
input_time_length=input_time_length,
final_conv_length=final_conv_length).create_network()
elif model_name == 'deep':
model = Deep4Net(n_chans, n_classes,
n_filters_time=n_start_chans,
n_filters_spat=n_start_chans,
input_time_length=input_time_length,
n_filters_2 = int(n_start_chans * n_chan_factor),
n_filters_3 = int(n_start_chans * (n_chan_factor ** 2.0)),
n_filters_4 = int(n_start_chans * (n_chan_factor ** 3.0)),
final_conv_length=final_conv_length,
stride_before_pool=True).create_network()
elif (model_name == 'deep_smac'):
if model_name == 'deep_smac':
do_batch_norm = False
else:
assert model_name == 'deep_smac_bnorm'
do_batch_norm = True
double_time_convs = False
drop_prob = 0.244445
filter_length_2 = 12
filter_length_3 = 14
filter_length_4 = 12
filter_time_length = 21
final_conv_length = 1
first_nonlin = elu
first_pool_mode = 'mean'
first_pool_nonlin = identity
later_nonlin = elu
later_pool_mode = 'mean'
later_pool_nonlin = identity
n_filters_factor = 1.679066
n_filters_start = 32
pool_time_length = 1
pool_time_stride = 2
split_first_layer = True
n_chan_factor = n_filters_factor
n_start_chans = n_filters_start
model = Deep4Net(n_chans, n_classes,
n_filters_time=n_start_chans,
n_filters_spat=n_start_chans,
input_time_length=input_time_length,
n_filters_2=int(n_start_chans * n_chan_factor),
n_filters_3=int(n_start_chans * (n_chan_factor ** 2.0)),
n_filters_4=int(n_start_chans * (n_chan_factor ** 3.0)),
final_conv_length=final_conv_length,
batch_norm=do_batch_norm,
double_time_convs=double_time_convs,
drop_prob=drop_prob,
filter_length_2=filter_length_2,
filter_length_3=filter_length_3,
filter_length_4=filter_length_4,
filter_time_length=filter_time_length,
first_nonlin=first_nonlin,
first_pool_mode=first_pool_mode,
first_pool_nonlin=first_pool_nonlin,
later_nonlin=later_nonlin,
later_pool_mode=later_pool_mode,
later_pool_nonlin=later_pool_nonlin,
pool_time_length=pool_time_length,
pool_time_stride=pool_time_stride,
split_first_layer=split_first_layer,
stride_before_pool=True).create_network()
elif model_name == 'shallow_smac':
conv_nonlin = identity
do_batch_norm = True
drop_prob = 0.328794
filter_time_length = 56
final_conv_length = 22
n_filters_spat = 73
n_filters_time = 24
pool_mode = 'max'
pool_nonlin = identity
pool_time_length = 84
pool_time_stride = 3
split_first_layer = True
model = ShallowFBCSPNet(in_chans=n_chans, n_classes=n_classes,
n_filters_time=n_filters_time,
n_filters_spat=n_filters_spat,
input_time_length=input_time_length,
final_conv_length=final_conv_length,
conv_nonlin=conv_nonlin,
batch_norm=do_batch_norm,
drop_prob=drop_prob,
filter_time_length=filter_time_length,
pool_mode=pool_mode,
pool_nonlin=pool_nonlin,
pool_time_length=pool_time_length,
pool_time_stride=pool_time_stride,
split_first_layer=split_first_layer,
).create_network()
elif model_name == 'linear':
model = nn.Sequential()
model.add_module("conv_classifier",
nn.Conv2d(n_chans, n_classes, (600,1)))
model.add_module('softmax', nn.LogSoftmax())
model.add_module('squeeze', Expression(lambda x: x.squeeze(3)))
else:
assert False, "unknown model name {:s}".format(model_name)
to_dense_prediction_model(model)
log.info("Model:\n{:s}".format(str(model)))
if cuda:
model.cuda()
# determine output size
test_input = np_to_var(
np.ones((2, n_chans, input_time_length, 1), dtype=np.float32))
if cuda:
test_input = test_input.cuda()
log.info("In shape: {:s}".format(str(test_input.cpu().data.numpy().shape)))
out = model(test_input)
log.info("Out shape: {:s}".format(str(out.cpu().data.numpy().shape)))
n_preds_per_input = out.cpu().data.numpy().shape[2]
log.info("{:d} predictions per input/trial".format(n_preds_per_input))
iterator = CropsFromTrialsIterator(batch_size=batch_size,
input_time_length=input_time_length,
n_preds_per_input=n_preds_per_input)
optimizer = optim.Adam(model.parameters(), lr=init_lr)
loss_function = lambda preds, targets: F.nll_loss(
th.mean(preds, dim=2, keepdim=False), targets)
if model_constraint is not None:
assert model_constraint == 'defaultnorm'
model_constraint = MaxNormDefaultConstraint()
monitors = [LossMonitor(), MisclassMonitor(col_suffix='sample_misclass'),
CroppedDiagnosisMonitor(input_time_length, n_preds_per_input),
RuntimeMonitor(),]
stop_criterion = MaxEpochs(max_epochs)
batch_modifier = None
run_after_early_stop = True
exp = Experiment(model, train_set, valid_set, test_set, iterator,
loss_function, optimizer, model_constraint,
monitors, stop_criterion,
remember_best_column='valid_misclass',
run_after_early_stop=run_after_early_stop,
batch_modifier=batch_modifier,
cuda=cuda)
exp.run()
return exp
if __name__ == "__main__":
import config
start_time = time.time()
logging.basicConfig(format='%(asctime)s %(levelname)s : %(message)s',
level=logging.DEBUG, stream=sys.stdout)
exp = run_exp(
config.data_folders,
config.n_recordings,
config.sensor_types,
config.n_chans,
config.max_recording_mins,
config.sec_to_cut, config.duration_recording_mins,
config.test_recording_mins,
config.max_abs_val,
config.sampling_freq,
config.divisor,
config.test_on_eval,
config.n_folds, config.i_test_fold,
config.shuffle,
config.model_name,
config.n_start_chans, config.n_chan_factor,
config.input_time_length, config.final_conv_length,
config.model_constraint,
config.init_lr,
config.batch_size, config.max_epochs,config.cuda,)
end_time = time.time()
run_time = end_time - start_time
log.info("Experiment runtime: {:.2f} sec".format(run_time))
# In case you want to recompute predictions for further analysis:
exp.model.eval()
for setname in ('train', 'valid', 'test'):
log.info("Compute predictions for {:s}...".format(
setname))
dataset = exp.datasets[setname]
if config.cuda:
preds_per_batch = [var_to_np(exp.model(np_to_var(b[0]).cuda()))
for b in exp.iterator.get_batches(dataset, shuffle=False)]
else:
preds_per_batch = [var_to_np(exp.model(np_to_var(b[0])))
for b in exp.iterator.get_batches(dataset, shuffle=False)]
preds_per_trial = compute_preds_per_trial(
preds_per_batch, dataset,
input_time_length=exp.iterator.input_time_length,
n_stride=exp.iterator.n_preds_per_input)
mean_preds_per_trial = [np.mean(preds, axis=(0, 2)) for preds in
preds_per_trial]
mean_preds_per_trial = np.array(mean_preds_per_trial)