-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathEEGNet_like.py
More file actions
192 lines (169 loc) · 6.38 KB
/
EEGNet_like.py
File metadata and controls
192 lines (169 loc) · 6.38 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
'''
This is the EEGNet architecture used for CP experiments in the manuscript "Continuous Tracking using Deep Learning-based Decoding for Non-invasive Brain-Computer Interface".
This model is a lightly modified version of the EEGNetv4 model from the braindecode package [1]. The main change is to perform regression by taking the raw model output instead of performing a softmax on the final layer.
Authors:
Originally produced by the braindecode package [1] (https://braindecode.org/stable/generated/braindecode.models.EEGNetv4.html), based on the EEGNet architecture first published by Lawhern et. al [2].
Modifications done by Hao Zhu.
braindecode reference:
[1] R. T. Schirrmeister et al., “Deep learning with convolutional neural networks for EEG decoding and visualization,” Human Brain Mapping, vol. 38, no. 11, pp. 5391–5420, 2017, doi: 10.1002/hbm.23730.
Original EEGNet publication:
[2] V. J. Lawhern, A. J. Solon, N. R. Waytowich, S. M. Gordon, C. P. Hung, and B. J. Lance, “EEGNet: a compact convolutional neural network for EEG-based brain–computer interfaces,” J. Neural Eng., vol. 15, no. 5, p. 056013, Jul. 2018, doi: 10.1088/1741-2552/aace8c.
'''
import torch
from torch import nn
from torch.nn.functional import elu
from .modules import Expression, Ensure4d
from .functions import squeeze_final_output
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)
class EEGNetv4Like(nn.Sequential):
def __init__(
self,
in_chans,
n_classes,
input_window_samples=None,
final_conv_length="auto",
pool_mode="mean",
F1=8,
D=2,
F2=16,
kernel_length=64,
third_kernel_size=(8, 4),
drop_prob=0.25,
):
super().__init__()
if final_conv_length == "auto":
assert input_window_samples is not None
self.in_chans = in_chans
self.n_classes = n_classes
self.input_window_samples = input_window_samples
self.final_conv_length = final_conv_length
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())
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(
# nn.Conv2d(
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(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))
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(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))
out = self(
torch.ones(
(1, self.in_chans, self.input_window_samples, 1),
dtype=torch.float64
)
)
n_out_virtual_chans = out.cpu().data.numpy().shape[2]
if self.final_conv_length == "auto":
n_out_time = out.cpu().data.numpy().shape[3]
self.final_conv_length = n_out_time
self.add_module(
"conv_classifier",
nn.Conv2d(
self.F2,
self.n_classes,
(n_out_virtual_chans, self.final_conv_length),
bias=True,
),
)
self.add_module("permute_back", Expression(_transpose_1_0))
self.add_module("squeeze", Expression(squeeze_final_output))
_glorot_weight_zero_bias(self)
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)