-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodels.py
351 lines (297 loc) · 10 KB
/
models.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
'''
Implementation obtained from braindecode
'''
import torch
from torch import nn
import torch.nn.functional as F
import pytorch_lightning as pl
import torchmetrics
################################################################
# functions
################################################################
def square(x):
return x * x
def safe_log(x, eps=1e-6):
""" Prevents :math:`log(0)` by using :math:`log(max(x, eps))`."""
return torch.log(torch.clamp(x, min=eps))
def squeeze_final_output(x):
"""Removes empty dimension at end and potentially removes empty time
dimension. It does not just use squeeze as we never want to remove
first dimension.
Returns
-------
x: torch.Tensor
squeezed tensor
"""
assert x.size()[3] == 1
x = x[:, :, :, 0]
if x.size()[2] == 1:
x = x[:, :, 0]
return x
def transpose_time_to_spat(x):
"""Swap time and spatial dimensions.
Returns
-------
x: torch.Tensor
tensor in which last and first dimensions are swapped
"""
return x.permute(0, 3, 2, 1)
################################################################
# modules
################################################################
class Ensure4d(torch.nn.Module):
def forward(self, x):
while len(x.shape) < 4:
x = x.unsqueeze(-1)
return x
class Expression(torch.nn.Module):
"""Compute given expression on forward pass.
Parameters
----------
expression_fn : callable
Should accept variable number of objects of type
`torch.autograd.Variable` to compute its output.
"""
def __init__(self, expression_fn):
super(Expression, self).__init__()
self.expression_fn = expression_fn
def forward(self, *x):
return self.expression_fn(*x)
def __repr__(self):
if hasattr(self.expression_fn, "func") and hasattr(
self.expression_fn, "kwargs"
):
expression_str = "{:s} {:s}".format(
self.expression_fn.func.__name__, str(self.expression_fn.kwargs)
)
elif hasattr(self.expression_fn, "__name__"):
expression_str = self.expression_fn.__name__
else:
expression_str = repr(self.expression_fn)
return (
self.__class__.__name__ +
"(expression=%s) " % expression_str
)
class Conv2dWithConstraint(nn.Conv2d):
def __init__(self, *args, max_norm=1, **kwargs):
self.max_norm = max_norm
super(Conv2dWithConstraint, self).__init__(*args, **kwargs)
def forward(self, x):
self.weight.data = torch.renorm(
self.weight.data, p=2, dim=0, maxnorm=self.max_norm
)
return super(Conv2dWithConstraint, self).forward(x)
################################################################
# models
################################################################
class _EEGNetv4Embedding(nn.Sequential):
def __init__(
self,
in_chans,
pool_mode="mean",
F1=8,
D=2,
F2=16, # usually set to F1*D (?)
kernel_length=64,
third_kernel_size=(8, 4),
drop_prob=0.25,
):
super().__init__()
self.in_chans = in_chans
self.pool_mode = pool_mode
self.F1 = F1
self.D = D
self.F2 = F2
self.kernel_length = kernel_length
self.third_kernel_size = third_kernel_size
self.drop_prob = drop_prob
pool_class = dict(max=nn.MaxPool2d, mean=nn.AvgPool2d)[self.pool_mode]
self.add_module("ensuredims", Ensure4d())
# b c 0 1
# now to b 1 0 c
self.add_module("dimshuffle", Expression(_transpose_to_b_1_c_0))
self.add_module(
"conv_temporal",
nn.Conv2d(
1,
self.F1,
(1, self.kernel_length),
stride=1,
bias=False,
padding=(0, self.kernel_length // 2),
),
)
self.add_module(
"bnorm_temporal",
nn.BatchNorm2d(self.F1, momentum=0.01, affine=True, eps=1e-3),
)
self.add_module(
"conv_spatial",
Conv2dWithConstraint(
self.F1,
self.F1 * self.D,
(self.in_chans, 1),
max_norm=1,
stride=1,
bias=False,
groups=self.F1,
padding=(0, 0),
),
)
self.add_module(
"bnorm_1",
nn.BatchNorm2d(
self.F1 * self.D, momentum=0.01, affine=True, eps=1e-3
),
)
self.add_module("elu_1", Expression(F.elu))
self.add_module("pool_1", pool_class(kernel_size=(1, 4), stride=(1, 4)))
self.add_module("drop_1", nn.Dropout(p=self.drop_prob))
# https://discuss.pytorch.org/t/how-to-modify-a-conv2d-to-depthwise-separable-convolution/15843/7
self.add_module(
"conv_separable_depth",
nn.Conv2d(
self.F1 * self.D,
self.F1 * self.D,
(1, 16),
stride=1,
bias=False,
groups=self.F1 * self.D,
padding=(0, 16 // 2),
),
)
self.add_module(
"conv_separable_point",
nn.Conv2d(
self.F1 * self.D,
self.F2,
(1, 1),
stride=1,
bias=False,
padding=(0, 0),
),
)
self.add_module(
"bnorm_2",
nn.BatchNorm2d(self.F2, momentum=0.01, affine=True, eps=1e-3),
)
self.add_module("elu_2", Expression(F.elu))
self.add_module("pool_2", pool_class(kernel_size=(1, 8), stride=(1, 8)))
self.add_module("drop_2", nn.Dropout(p=self.drop_prob))
self.add_module('flatten', nn.Flatten())
_glorot_weight_zero_bias(self)
class EEGNetv4(pl.LightningModule):
"""
EEGNet v4 model from [EEGNet4]_.
Notes
-----
This implementation is not guaranteed to be correct, has not been checked
by original authors, only reimplemented from the paper description.
References
----------
.. [EEGNet4] Lawhern, V. J., Solon, A. J., Waytowich, N. R., Gordon,
S. M., Hung, C. P., & Lance, B. J. (2018).
EEGNet: A Compact Convolutional Network for EEG-based
Brain-Computer Interfaces.
arXiv preprint arXiv:1611.08024.
"""
def __init__(
self,
in_chans,
n_classes,
input_window_samples,
pool_mode="mean",
F1=8,
D=2,
F2=16, # usually set to F1*D (?)
kernel_length=64,
third_kernel_size=(8, 4),
drop_prob=0.25,
lr=0.001,
max_lr=0.1,
):
super().__init__()
self.save_hyperparameters()
self.embedding = _EEGNetv4Embedding(
in_chans=self.hparams.in_chans,
pool_mode=self.hparams.pool_mode,
F1=self.hparams.F1,
D=self.hparams.D,
F2=self.hparams.F2,
kernel_length=self.hparams.kernel_length,
third_kernel_size=self.hparams.third_kernel_size,
drop_prob=self.hparams.drop_prob,
)
out = self.embedding(
torch.ones(
(1, self.hparams.in_chans, self.hparams.input_window_samples, 1),
dtype=torch.float32
)
)
self.embedding_size = out.cpu().data.numpy().shape[1]
self.classifier = nn.Linear(self.embedding_size, self.hparams.n_classes)
self.train_acc = torchmetrics.Accuracy()
self.val_acc = torchmetrics.Accuracy()
self.test_acc = torchmetrics.Accuracy()
def forward(self, x):
return self.classifier(self.embedding(x))
def training_step(self, batch, batch_idx):
x, y = batch
y_hat = self(x)
loss = F.cross_entropy(y_hat, y)
self.train_acc(y_hat, y)
self.log('train_acc', self.train_acc, on_epoch=True)
self.log("train_loss", loss, on_epoch=True)
return loss
def validation_step(self, batch, batch_idx):
x, y = batch
y_hat = self(x)
loss = F.cross_entropy(y_hat, y)
self.val_acc(y_hat, y)
self.log('val_acc', self.val_acc, on_epoch=True)
self.log("val_loss", loss, on_epoch=True)
return loss
def test_step(self, batch, batch_idx):
x, y = batch
y_hat = self(x)
loss = F.cross_entropy(y_hat, y)
self.test_acc(y_hat, y)
self.log('test_acc', self.test_acc, on_epoch=True)
self.log("test_loss", loss, on_epoch=True)
return loss
def configure_optimizers(self):
optimizer = torch.optim.SGD(self.parameters(), lr=self.hparams.lr)
lr_scheduler = torch.optim.lr_scheduler.OneCycleLR(
optimizer,
max_lr=self.hparams.max_lr,
total_steps=self.trainer.estimated_stepping_batches
)
return dict(
optimizer=optimizer,
lr_scheduler=dict(
scheduler=lr_scheduler,
interval='step',
)
)
def _transpose_to_b_1_c_0(x):
return x.permute(0, 3, 1, 2)
def _transpose_1_0(x):
return x.permute(0, 1, 3, 2)
def _glorot_weight_zero_bias(model):
"""Initalize parameters of all modules by initializing weights with
glorot
uniform/xavier initialization, and setting biases to zero. Weights from
batch norm layers are set to 1.
Parameters
----------
model: Module
"""
for module in model.modules():
if hasattr(module, "weight"):
if not ("BatchNorm" in module.__class__.__name__):
nn.init.xavier_uniform_(module.weight, gain=1)
else:
nn.init.constant_(module.weight, 1)
if hasattr(module, "bias"):
if module.bias is not None:
nn.init.constant_(module.bias, 0)