-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathEEGNet.py
245 lines (229 loc) · 7.97 KB
/
EEGNet.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
"""EEGNet from https://doi.org/10.1088/1741-2552/aace8c.
Shallow and lightweight convolutional neural network proposed for a general decoding of single-trial EEG signals.
It was proposed for P300, error-related negativity, motor execution, motor imagery decoding.
Authors
* Davide Borra, 2021
"""
import torch
import speechbrain as sb
class EEGNet(torch.nn.Module):
"""EEGNet.
Arguments
---------
input_shape: tuple
The shape of the input.
cnn_temporal_kernels: int
Number of kernels in the 2d temporal convolution.
cnn_temporal_kernelsize: tuple
Kernel size of the 2d temporal convolution.
cnn_spatial_depth_multiplier: int
Depth multiplier of the 2d spatial depthwise convolution.
cnn_spatial_max_norm: float
Kernel max norm of the 2d spatial depthwise convolution.
cnn_spatial_pool: tuple
Pool size and stride after the 2d spatial depthwise convolution.
cnn_septemporal_depth_multiplier: int
Depth multiplier of the 2d temporal separable convolution.
cnn_septemporal_kernelsize: tuple
Kernel size of the 2d temporal separable convolution.
cnn_septemporal_pool: tuple
Pool size and stride after the 2d temporal separable convolution.
cnn_pool_type: string
Pooling type.
dropout: float
Dropout probability.
dense_max_norm: float
Weight max norm of the fully-connected layer.
dense_n_neurons: int
Number of output neurons.
activation_type: str
Activation function of the hidden layers.
Example
-------
#>>> inp_tensor = torch.rand([1, 200, 32, 1])
#>>> model = EEGNet(input_shape=inp_tensor.shape)
#>>> output = model(inp_tensor)
#>>> output.shape
#torch.Size([1,4])
"""
def __init__(
self,
input_shape=None, # (1, T, C, 1)
cnn_temporal_kernels=8,
cnn_temporal_kernelsize=(33, 1),
cnn_spatial_depth_multiplier=2,
cnn_spatial_max_norm=1.0,
cnn_spatial_pool=(4, 1),
cnn_septemporal_depth_multiplier=1,
cnn_septemporal_point_kernels=None,
cnn_septemporal_kernelsize=(17, 1),
cnn_septemporal_pool=(8, 1),
cnn_pool_type="avg",
dropout=0.5,
dense_max_norm=0.25,
dense_n_neurons=4,
activation_type="elu",
):
super().__init__()
if input_shape is None:
raise ValueError("Must specify input_shape")
if activation_type == "gelu":
activation = torch.nn.GELU()
elif activation_type == "elu":
activation = torch.nn.ELU()
elif activation_type == "relu":
activation = torch.nn.ReLU()
elif activation_type == "leaky_relu":
activation = torch.nn.LeakyReLU()
elif activation_type == "prelu":
activation = torch.nn.PReLU()
else:
raise ValueError("Wrong hidden activation function")
self.default_sf = 128 # sampling rate of the original publication (Hz)
# T = input_shape[1]
C = input_shape[2]
# CONVOLUTIONAL MODULE
self.conv_module = torch.nn.Sequential()
# Temporal convolution
self.conv_module.add_module(
"conv_0",
sb.nnet.CNN.Conv2d(
in_channels=1,
out_channels=cnn_temporal_kernels,
kernel_size=cnn_temporal_kernelsize,
padding="same",
padding_mode="constant",
bias=False,
swap=True,
),
)
self.conv_module.add_module(
"bnorm_0",
sb.nnet.normalization.BatchNorm2d(
input_size=cnn_temporal_kernels, momentum=0.01, affine=True,
),
)
# Spatial depthwise convolution
cnn_spatial_kernels = (
cnn_spatial_depth_multiplier * cnn_temporal_kernels
)
self.conv_module.add_module(
"conv_1",
sb.nnet.CNN.Conv2d(
in_channels=cnn_temporal_kernels,
out_channels=cnn_spatial_kernels,
kernel_size=(1, C),
groups=cnn_temporal_kernels,
padding="valid",
bias=False,
max_norm=cnn_spatial_max_norm,
swap=True,
),
)
self.conv_module.add_module(
"bnorm_1",
sb.nnet.normalization.BatchNorm2d(
input_size=cnn_spatial_kernels, momentum=0.01, affine=True,
),
)
self.conv_module.add_module("act_1", activation)
self.conv_module.add_module(
"pool_1",
sb.nnet.pooling.Pooling2d(
pool_type=cnn_pool_type,
kernel_size=cnn_spatial_pool,
stride=cnn_spatial_pool,
pool_axis=[1, 2],
),
)
self.conv_module.add_module("dropout_1", torch.nn.Dropout(p=dropout))
# Temporal separable convolution
cnn_septemporal_kernels = (
cnn_spatial_kernels * cnn_septemporal_depth_multiplier
)
self.conv_module.add_module(
"conv_2",
sb.nnet.CNN.Conv2d(
in_channels=cnn_spatial_kernels,
out_channels=cnn_septemporal_kernels,
kernel_size=cnn_septemporal_kernelsize,
groups=cnn_spatial_kernels,
padding="same",
padding_mode="constant",
bias=False,
swap=True,
),
)
if cnn_septemporal_point_kernels is None:
cnn_septemporal_point_kernels = cnn_septemporal_kernels
self.conv_module.add_module(
"conv_3",
sb.nnet.CNN.Conv2d(
in_channels=cnn_septemporal_kernels,
out_channels=cnn_septemporal_point_kernels,
kernel_size=(1, 1),
padding="valid",
bias=False,
swap=True,
),
)
self.conv_module.add_module(
"bnorm_3",
sb.nnet.normalization.BatchNorm2d(
input_size=cnn_septemporal_point_kernels,
momentum=0.01,
affine=True,
),
)
self.conv_module.add_module("act_3", activation)
self.conv_module.add_module(
"pool_3",
sb.nnet.pooling.Pooling2d(
pool_type=cnn_pool_type,
kernel_size=cnn_septemporal_pool,
stride=cnn_septemporal_pool,
pool_axis=[1, 2],
),
)
self.conv_module.add_module("dropout_3", torch.nn.Dropout(p=dropout))
# Shape of intermediate feature maps
out = self.conv_module(
torch.ones((1,) + tuple(input_shape[1:-1]) + (1,))
)
dense_input_size = self._num_flat_features(out)
# DENSE MODULE
self.dense_module = torch.nn.Sequential()
self.dense_module.add_module(
"flatten", torch.nn.Flatten(),
)
self.dense_module.add_module(
"fc_out",
sb.nnet.linear.Linear(
input_size=dense_input_size,
n_neurons=dense_n_neurons,
max_norm=dense_max_norm,
),
)
self.dense_module.add_module("act_out", torch.nn.LogSoftmax(dim=1))
def _num_flat_features(self, x):
"""Returns the number of flattened features from a tensor.
Arguments
---------
x : torch.Tensor
Input feature map.
"""
size = x.size()[1:] # all dimensions except the batch dimension
num_features = 1
for s in size:
num_features *= s
return num_features
def forward(self, x):
"""Returns the output of the model.
Arguments
---------
x : torch.Tensor (batch, time, EEG channel, channel)
Input to convolve. 4d tensors are expected.
"""
x = self.conv_module(x)
x = self.dense_module(x)
return x