braindecode 0.8__py3-none-any.whl → 1.0.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of braindecode might be problematic. Click here for more details.
- braindecode/__init__.py +1 -2
- braindecode/augmentation/__init__.py +50 -0
- braindecode/augmentation/base.py +222 -0
- braindecode/augmentation/functional.py +1096 -0
- braindecode/augmentation/transforms.py +1274 -0
- braindecode/classifier.py +26 -24
- braindecode/datasets/__init__.py +34 -0
- braindecode/datasets/base.py +840 -0
- braindecode/datasets/bbci.py +694 -0
- braindecode/datasets/bcicomp.py +194 -0
- braindecode/datasets/bids.py +245 -0
- braindecode/datasets/mne.py +172 -0
- braindecode/datasets/moabb.py +209 -0
- braindecode/datasets/nmt.py +311 -0
- braindecode/datasets/sleep_physio_challe_18.py +412 -0
- braindecode/datasets/sleep_physionet.py +125 -0
- braindecode/datasets/tuh.py +588 -0
- braindecode/datasets/xy.py +95 -0
- braindecode/datautil/__init__.py +49 -0
- braindecode/datautil/serialization.py +342 -0
- braindecode/datautil/util.py +41 -0
- braindecode/eegneuralnet.py +63 -47
- braindecode/functional/__init__.py +10 -0
- braindecode/functional/functions.py +251 -0
- braindecode/functional/initialization.py +47 -0
- braindecode/models/__init__.py +52 -0
- braindecode/models/atcnet.py +652 -0
- braindecode/models/attentionbasenet.py +550 -0
- braindecode/models/base.py +296 -0
- braindecode/models/biot.py +483 -0
- braindecode/models/contrawr.py +296 -0
- braindecode/models/ctnet.py +450 -0
- braindecode/models/deep4.py +322 -0
- braindecode/models/deepsleepnet.py +295 -0
- braindecode/models/eegconformer.py +372 -0
- braindecode/models/eeginception_erp.py +304 -0
- braindecode/models/eeginception_mi.py +371 -0
- braindecode/models/eegitnet.py +301 -0
- braindecode/models/eegminer.py +255 -0
- braindecode/models/eegnet.py +473 -0
- braindecode/models/eegnex.py +247 -0
- braindecode/models/eegresnet.py +362 -0
- braindecode/models/eegsimpleconv.py +199 -0
- braindecode/models/eegtcnet.py +335 -0
- braindecode/models/fbcnet.py +221 -0
- braindecode/models/fblightconvnet.py +313 -0
- braindecode/models/fbmsnet.py +325 -0
- braindecode/models/hybrid.py +126 -0
- braindecode/models/ifnet.py +441 -0
- braindecode/models/labram.py +1166 -0
- braindecode/models/msvtnet.py +375 -0
- braindecode/models/sccnet.py +182 -0
- braindecode/models/shallow_fbcsp.py +208 -0
- braindecode/models/signal_jepa.py +1012 -0
- braindecode/models/sinc_shallow.py +337 -0
- braindecode/models/sleep_stager_blanco_2020.py +167 -0
- braindecode/models/sleep_stager_chambon_2018.py +157 -0
- braindecode/models/sleep_stager_eldele_2021.py +536 -0
- braindecode/models/sparcnet.py +378 -0
- braindecode/models/summary.csv +41 -0
- braindecode/models/syncnet.py +232 -0
- braindecode/models/tcn.py +273 -0
- braindecode/models/tidnet.py +395 -0
- braindecode/models/tsinception.py +258 -0
- braindecode/models/usleep.py +340 -0
- braindecode/models/util.py +133 -0
- braindecode/modules/__init__.py +38 -0
- braindecode/modules/activation.py +60 -0
- braindecode/modules/attention.py +757 -0
- braindecode/modules/blocks.py +108 -0
- braindecode/modules/convolution.py +274 -0
- braindecode/modules/filter.py +632 -0
- braindecode/modules/layers.py +133 -0
- braindecode/modules/linear.py +50 -0
- braindecode/modules/parametrization.py +38 -0
- braindecode/modules/stats.py +77 -0
- braindecode/modules/util.py +77 -0
- braindecode/modules/wrapper.py +75 -0
- braindecode/preprocessing/__init__.py +37 -0
- braindecode/preprocessing/mne_preprocess.py +77 -0
- braindecode/preprocessing/preprocess.py +478 -0
- braindecode/preprocessing/windowers.py +1031 -0
- braindecode/regressor.py +23 -12
- braindecode/samplers/__init__.py +18 -0
- braindecode/samplers/base.py +401 -0
- braindecode/samplers/ssl.py +263 -0
- braindecode/training/__init__.py +23 -0
- braindecode/training/callbacks.py +23 -0
- braindecode/training/losses.py +105 -0
- braindecode/training/scoring.py +483 -0
- braindecode/util.py +55 -59
- braindecode/version.py +1 -1
- braindecode/visualization/__init__.py +8 -0
- braindecode/visualization/confusion_matrices.py +289 -0
- braindecode/visualization/gradients.py +57 -0
- {braindecode-0.8.dist-info → braindecode-1.0.0.dist-info}/METADATA +39 -55
- braindecode-1.0.0.dist-info/RECORD +101 -0
- {braindecode-0.8.dist-info → braindecode-1.0.0.dist-info}/WHEEL +1 -1
- {braindecode-0.8.dist-info → braindecode-1.0.0.dist-info/licenses}/LICENSE.txt +1 -1
- braindecode-1.0.0.dist-info/licenses/NOTICE.txt +20 -0
- braindecode-0.8.dist-info/RECORD +0 -11
- {braindecode-0.8.dist-info → braindecode-1.0.0.dist-info}/top_level.txt +0 -0
|
@@ -0,0 +1,258 @@
|
|
|
1
|
+
# Authors: Bruno Aristimunha <b.aristimunha>
|
|
2
|
+
#
|
|
3
|
+
# License: BSD (3-clause)
|
|
4
|
+
|
|
5
|
+
from __future__ import annotations
|
|
6
|
+
|
|
7
|
+
import torch
|
|
8
|
+
import torch.nn as nn
|
|
9
|
+
from einops.layers.torch import Rearrange
|
|
10
|
+
|
|
11
|
+
from braindecode.models.base import EEGModuleMixin
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
class TSceptionV1(EEGModuleMixin, nn.Module):
|
|
15
|
+
"""TSception model from Ding et al. (2020) from [ding2020]_.
|
|
16
|
+
|
|
17
|
+
TSception: A deep learning framework for emotion detection using EEG.
|
|
18
|
+
|
|
19
|
+
.. figure:: https://user-images.githubusercontent.com/58539144/74716976-80415e00-526a-11ea-9433-02ab2b753f6b.PNG
|
|
20
|
+
:align: center
|
|
21
|
+
:alt: TSceptionV1 Architecture
|
|
22
|
+
|
|
23
|
+
The model consists of temporal and spatial convolutional layers
|
|
24
|
+
(Tception and Sception) designed to learn temporal and spatial features
|
|
25
|
+
from EEG data.
|
|
26
|
+
|
|
27
|
+
Parameters
|
|
28
|
+
----------
|
|
29
|
+
number_filter_temp : int
|
|
30
|
+
Number of temporal convolutional filters.
|
|
31
|
+
number_filter_spat : int
|
|
32
|
+
Number of spatial convolutional filters.
|
|
33
|
+
hidden_size : int
|
|
34
|
+
Number of units in the hidden fully connected layer.
|
|
35
|
+
drop_prob : float
|
|
36
|
+
Dropout rate applied after the hidden layer.
|
|
37
|
+
activation : nn.Module, optional
|
|
38
|
+
Activation function class to apply. Should be a PyTorch activation
|
|
39
|
+
module like ``nn.ReLU`` or ``nn.LeakyReLU``. Default is ``nn.LeakyReLU``.
|
|
40
|
+
pool_size : int, optional
|
|
41
|
+
Pooling size for the average pooling layers. Default is 8.
|
|
42
|
+
inception_windows : list[float], optional
|
|
43
|
+
List of window sizes (in seconds) for the inception modules.
|
|
44
|
+
Default is [0.5, 0.25, 0.125].
|
|
45
|
+
|
|
46
|
+
Notes
|
|
47
|
+
-----
|
|
48
|
+
This implementation is not guaranteed to be correct, has not been checked
|
|
49
|
+
by original authors. The modifications are minimal and the model is expected
|
|
50
|
+
to work as intended. the original code from [code2020]_.
|
|
51
|
+
|
|
52
|
+
References
|
|
53
|
+
----------
|
|
54
|
+
.. [ding2020] Ding, Y., Robinson, N., Zeng, Q., Chen, D., Wai, A. A. P.,
|
|
55
|
+
Lee, T. S., & Guan, C. (2020, July). Tsception: a deep learning framework
|
|
56
|
+
for emotion detection using EEG. In 2020 international joint conference
|
|
57
|
+
on neural networks (IJCNN) (pp. 1-7). IEEE.
|
|
58
|
+
.. [code2020] Ding, Y., Robinson, N., Zeng, Q., Chen, D., Wai, A. A. P.,
|
|
59
|
+
Lee, T. S., & Guan, C. (2020, July). Tsception: a deep learning framework
|
|
60
|
+
for emotion detection using EEG.
|
|
61
|
+
https://github.com/deepBrains/TSception/blob/master/Models.py
|
|
62
|
+
"""
|
|
63
|
+
|
|
64
|
+
def __init__(
|
|
65
|
+
self,
|
|
66
|
+
# Braindecode parameters
|
|
67
|
+
n_chans=None,
|
|
68
|
+
n_outputs=None,
|
|
69
|
+
input_window_seconds=None,
|
|
70
|
+
chs_info=None,
|
|
71
|
+
n_times=None,
|
|
72
|
+
sfreq=None,
|
|
73
|
+
# Model parameters
|
|
74
|
+
number_filter_temp: int = 9,
|
|
75
|
+
number_filter_spat: int = 6,
|
|
76
|
+
hidden_size: int = 128,
|
|
77
|
+
drop_prob: float = 0.5,
|
|
78
|
+
activation: nn.Module = nn.LeakyReLU,
|
|
79
|
+
pool_size: int = 8,
|
|
80
|
+
inception_windows: tuple[float, float, float] = (0.5, 0.25, 0.125),
|
|
81
|
+
):
|
|
82
|
+
super().__init__(
|
|
83
|
+
n_outputs=n_outputs,
|
|
84
|
+
n_chans=n_chans,
|
|
85
|
+
chs_info=chs_info,
|
|
86
|
+
n_times=n_times,
|
|
87
|
+
input_window_seconds=input_window_seconds,
|
|
88
|
+
sfreq=sfreq,
|
|
89
|
+
)
|
|
90
|
+
del n_outputs, n_chans, chs_info, n_times, input_window_seconds, sfreq
|
|
91
|
+
|
|
92
|
+
self.activation = activation
|
|
93
|
+
self.pool_size = pool_size
|
|
94
|
+
self.inception_windows = inception_windows
|
|
95
|
+
self.number_filter_spat = number_filter_spat
|
|
96
|
+
self.number_filter_temp = number_filter_temp
|
|
97
|
+
self.drop_prob = drop_prob
|
|
98
|
+
|
|
99
|
+
### Layers
|
|
100
|
+
self.ensuredim = Rearrange("batch nchans time -> batch 1 nchans time")
|
|
101
|
+
# Define temporal convolutional layers (Tception)
|
|
102
|
+
self.temporal_blocks = nn.ModuleList(
|
|
103
|
+
[
|
|
104
|
+
self._conv_block(
|
|
105
|
+
in_channels=1,
|
|
106
|
+
out_channels=number_filter_temp,
|
|
107
|
+
kernel_size=(1, int(window * self.sfreq)),
|
|
108
|
+
stride=1,
|
|
109
|
+
pool_size=self.pool_size,
|
|
110
|
+
activation=self.activation,
|
|
111
|
+
)
|
|
112
|
+
for window in self.inception_windows
|
|
113
|
+
]
|
|
114
|
+
)
|
|
115
|
+
self.batch_temporal_lay = nn.BatchNorm2d(self.number_filter_temp)
|
|
116
|
+
|
|
117
|
+
# Define spatial convolutional layers (Sception)
|
|
118
|
+
|
|
119
|
+
pool_size_spat = self.pool_size // 4
|
|
120
|
+
|
|
121
|
+
self.spatial_block_1 = self._conv_block(
|
|
122
|
+
in_channels=self.number_filter_temp,
|
|
123
|
+
out_channels=self.number_filter_spat,
|
|
124
|
+
kernel_size=(self.n_chans, 1),
|
|
125
|
+
stride=1,
|
|
126
|
+
pool_size=pool_size_spat,
|
|
127
|
+
activation=self.activation,
|
|
128
|
+
)
|
|
129
|
+
|
|
130
|
+
kernel_size_spat_2 = (max(1, self.n_chans // 2), 1)
|
|
131
|
+
|
|
132
|
+
self.spatial_block_2 = self._conv_block(
|
|
133
|
+
in_channels=self.number_filter_temp,
|
|
134
|
+
out_channels=self.number_filter_spat,
|
|
135
|
+
kernel_size=kernel_size_spat_2,
|
|
136
|
+
stride=kernel_size_spat_2,
|
|
137
|
+
pool_size=pool_size_spat,
|
|
138
|
+
activation=self.activation,
|
|
139
|
+
)
|
|
140
|
+
self.batch_spatial_lay = nn.BatchNorm2d(self.number_filter_spat)
|
|
141
|
+
|
|
142
|
+
# Calculate the size of the features after convolution and pooling layers
|
|
143
|
+
self.feature_size = self._calculate_feature_size()
|
|
144
|
+
# self.feature_size = self.number_filter_spat *
|
|
145
|
+
# Define the final classification layers
|
|
146
|
+
|
|
147
|
+
self.dense_layer = nn.Sequential(
|
|
148
|
+
nn.Linear(self.feature_size, hidden_size),
|
|
149
|
+
self.activation(),
|
|
150
|
+
nn.Dropout(self.drop_prob),
|
|
151
|
+
)
|
|
152
|
+
|
|
153
|
+
self.final_layer = nn.Linear(hidden_size, self.n_outputs)
|
|
154
|
+
|
|
155
|
+
def forward(self, x: torch.Tensor) -> torch.Tensor:
|
|
156
|
+
"""
|
|
157
|
+
Forward pass of the TSception model.
|
|
158
|
+
|
|
159
|
+
Parameters
|
|
160
|
+
----------
|
|
161
|
+
x : torch.Tensor
|
|
162
|
+
Input tensor of shape (batch_size, n_channels, n_times).
|
|
163
|
+
|
|
164
|
+
Returns
|
|
165
|
+
-------
|
|
166
|
+
torch.Tensor
|
|
167
|
+
Output tensor of shape (batch_size, n_classes).
|
|
168
|
+
"""
|
|
169
|
+
# Temporal Convolution
|
|
170
|
+
# shape: (batch_size, n_channels, n_times)
|
|
171
|
+
x = self.ensuredim(x)
|
|
172
|
+
# shape: (batch_size, 1, n_channels, n_times)
|
|
173
|
+
|
|
174
|
+
t_features = [layer(x) for layer in self.temporal_blocks]
|
|
175
|
+
# shape: (batch_size, number_filter_temp, n_channels,
|
|
176
|
+
#
|
|
177
|
+
t_out = torch.cat(t_features, dim=-1)
|
|
178
|
+
|
|
179
|
+
t_out = self.batch_temporal_lay(t_out)
|
|
180
|
+
|
|
181
|
+
# Spatial Convolution
|
|
182
|
+
s_out1 = self.spatial_block_1(t_out)
|
|
183
|
+
s_out2 = self.spatial_block_2(t_out)
|
|
184
|
+
s_out = torch.cat((s_out1, s_out2), dim=2)
|
|
185
|
+
s_out = self.batch_spatial_lay(s_out)
|
|
186
|
+
|
|
187
|
+
# Flatten and apply final layers
|
|
188
|
+
s_out = s_out.view(s_out.size(0), -1)
|
|
189
|
+
output = self.dense_layer(s_out)
|
|
190
|
+
output = self.final_layer(output)
|
|
191
|
+
return output
|
|
192
|
+
|
|
193
|
+
def _calculate_feature_size(self) -> int:
|
|
194
|
+
"""
|
|
195
|
+
Calculates the size of the features after convolution and pooling layers.
|
|
196
|
+
|
|
197
|
+
Returns
|
|
198
|
+
-------
|
|
199
|
+
int
|
|
200
|
+
Flattened size of the features after convolution and pooling layers.
|
|
201
|
+
"""
|
|
202
|
+
with torch.no_grad():
|
|
203
|
+
dummy_input = torch.ones(1, 1, self.n_chans, self.n_times)
|
|
204
|
+
t_features = [layer(dummy_input) for layer in self.temporal_blocks]
|
|
205
|
+
t_out = torch.cat(t_features, dim=-1)
|
|
206
|
+
t_out = self.batch_temporal_lay(t_out)
|
|
207
|
+
|
|
208
|
+
s_out1 = self.spatial_block_1(t_out)
|
|
209
|
+
s_out2 = self.spatial_block_2(t_out)
|
|
210
|
+
s_out = torch.cat((s_out1, s_out2), dim=2)
|
|
211
|
+
s_out = self.batch_spatial_lay(s_out)
|
|
212
|
+
|
|
213
|
+
feature_size = s_out.view(1, -1).size(1)
|
|
214
|
+
return feature_size
|
|
215
|
+
|
|
216
|
+
@staticmethod
|
|
217
|
+
def _conv_block(
|
|
218
|
+
in_channels: int,
|
|
219
|
+
out_channels: int,
|
|
220
|
+
kernel_size: tuple,
|
|
221
|
+
stride: tuple[int, int] | int,
|
|
222
|
+
pool_size: int,
|
|
223
|
+
activation: nn.Module,
|
|
224
|
+
) -> nn.Sequential:
|
|
225
|
+
"""
|
|
226
|
+
Creates a convolutional block with Conv2d, activation, and AvgPool2d layers.
|
|
227
|
+
|
|
228
|
+
Parameters
|
|
229
|
+
----------
|
|
230
|
+
in_channels : int
|
|
231
|
+
Number of input channels.
|
|
232
|
+
out_channels : int
|
|
233
|
+
Number of output channels.
|
|
234
|
+
kernel_size : tuple
|
|
235
|
+
Size of the convolutional kernel.
|
|
236
|
+
stride : int
|
|
237
|
+
Stride of the convolution.
|
|
238
|
+
pool_size : int
|
|
239
|
+
Size of the pooling kernel.
|
|
240
|
+
activation : nn.Module
|
|
241
|
+
Activation function class.
|
|
242
|
+
|
|
243
|
+
Returns
|
|
244
|
+
-------
|
|
245
|
+
nn.Sequential
|
|
246
|
+
A sequential container of the convolutional block.
|
|
247
|
+
"""
|
|
248
|
+
return nn.Sequential(
|
|
249
|
+
nn.Conv2d(
|
|
250
|
+
in_channels=in_channels,
|
|
251
|
+
out_channels=out_channels,
|
|
252
|
+
kernel_size=kernel_size,
|
|
253
|
+
stride=stride,
|
|
254
|
+
padding=0,
|
|
255
|
+
),
|
|
256
|
+
activation(),
|
|
257
|
+
nn.AvgPool2d(kernel_size=(1, pool_size), stride=(1, pool_size)),
|
|
258
|
+
)
|
|
@@ -0,0 +1,340 @@
|
|
|
1
|
+
# Authors: Theo Gnassounou <theo.gnassounou@inria.fr>
|
|
2
|
+
# Omar Chehab <l-emir-omar.chehab@inria.fr>
|
|
3
|
+
#
|
|
4
|
+
# License: BSD (3-clause)
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
import numpy as np
|
|
8
|
+
import torch
|
|
9
|
+
from torch import nn
|
|
10
|
+
|
|
11
|
+
from braindecode.models.base import EEGModuleMixin
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
class USleep(EEGModuleMixin, nn.Module):
|
|
15
|
+
"""
|
|
16
|
+
Sleep staging architecture from Perslev et al. (2021) [1]_.
|
|
17
|
+
|
|
18
|
+
.. figure:: https://media.springernature.com/full/springer-static/image/art%3A10.1038%2Fs41746-021-00440-5/MediaObjects/41746_2021_440_Fig2_HTML.png
|
|
19
|
+
:align: center
|
|
20
|
+
:alt: USleep Architecture
|
|
21
|
+
|
|
22
|
+
U-Net (autoencoder with skip connections) feature-extractor for sleep
|
|
23
|
+
staging described in [1]_.
|
|
24
|
+
|
|
25
|
+
For the encoder ('down'):
|
|
26
|
+
- the temporal dimension shrinks (via maxpooling in the time-domain)
|
|
27
|
+
- the spatial dimension expands (via more conv1d filters in the time-domain)
|
|
28
|
+
|
|
29
|
+
For the decoder ('up'):
|
|
30
|
+
- the temporal dimension expands (via upsampling in the time-domain)
|
|
31
|
+
- the spatial dimension shrinks (via fewer conv1d filters in the time-domain)
|
|
32
|
+
|
|
33
|
+
Both do so at exponential rates.
|
|
34
|
+
|
|
35
|
+
Parameters
|
|
36
|
+
----------
|
|
37
|
+
n_chans : int
|
|
38
|
+
Number of EEG or EOG channels. Set to 2 in [1]_ (1 EEG, 1 EOG).
|
|
39
|
+
sfreq : float
|
|
40
|
+
EEG sampling frequency. Set to 128 in [1]_.
|
|
41
|
+
depth : int
|
|
42
|
+
Number of conv blocks in encoding layer (number of 2x2 max pools).
|
|
43
|
+
Note: each block halves the spatial dimensions of the features.
|
|
44
|
+
n_time_filters : int
|
|
45
|
+
Initial number of convolutional filters. Set to 5 in [1]_.
|
|
46
|
+
complexity_factor : float
|
|
47
|
+
Multiplicative factor for the number of channels at each layer of the U-Net.
|
|
48
|
+
Set to 2 in [1]_.
|
|
49
|
+
with_skip_connection : bool
|
|
50
|
+
If True, use skip connections in decoder blocks.
|
|
51
|
+
n_outputs : int
|
|
52
|
+
Number of outputs/classes. Set to 5.
|
|
53
|
+
input_window_seconds : float
|
|
54
|
+
Size of the input, in seconds. Set to 30 in [1]_.
|
|
55
|
+
time_conv_size_s : float
|
|
56
|
+
Size of the temporal convolution kernel, in seconds. Set to 9 / 128 in
|
|
57
|
+
[1]_.
|
|
58
|
+
ensure_odd_conv_size : bool
|
|
59
|
+
If True and the size of the convolutional kernel is an even number, one
|
|
60
|
+
will be added to it to ensure it is odd, so that the decoder blocks can
|
|
61
|
+
work. This can be useful when using different sampling rates from 128
|
|
62
|
+
or 100 Hz.
|
|
63
|
+
activation : nn.Module, default=nn.ELU
|
|
64
|
+
Activation function class to apply. Should be a PyTorch activation
|
|
65
|
+
module class like ``nn.ReLU`` or ``nn.ELU``. Default is ``nn.ELU``.
|
|
66
|
+
|
|
67
|
+
References
|
|
68
|
+
----------
|
|
69
|
+
.. [1] Perslev M, Darkner S, Kempfner L, Nikolic M, Jennum PJ, Igel C.
|
|
70
|
+
U-Sleep: resilient high-frequency sleep staging. *npj Digit. Med.* 4, 72 (2021).
|
|
71
|
+
https://github.com/perslev/U-Time/blob/master/utime/models/usleep.py
|
|
72
|
+
"""
|
|
73
|
+
|
|
74
|
+
def __init__(
|
|
75
|
+
self,
|
|
76
|
+
n_chans=None,
|
|
77
|
+
sfreq=None,
|
|
78
|
+
depth=12,
|
|
79
|
+
n_time_filters=5,
|
|
80
|
+
complexity_factor=1.67,
|
|
81
|
+
with_skip_connection=True,
|
|
82
|
+
n_outputs=5,
|
|
83
|
+
input_window_seconds=None,
|
|
84
|
+
time_conv_size_s=9 / 128,
|
|
85
|
+
ensure_odd_conv_size=False,
|
|
86
|
+
activation: nn.Module = nn.ELU,
|
|
87
|
+
chs_info=None,
|
|
88
|
+
n_times=None,
|
|
89
|
+
):
|
|
90
|
+
super().__init__(
|
|
91
|
+
n_outputs=n_outputs,
|
|
92
|
+
n_chans=n_chans,
|
|
93
|
+
chs_info=chs_info,
|
|
94
|
+
n_times=n_times,
|
|
95
|
+
input_window_seconds=input_window_seconds,
|
|
96
|
+
sfreq=sfreq,
|
|
97
|
+
)
|
|
98
|
+
del n_outputs, n_chans, chs_info, n_times, input_window_seconds, sfreq
|
|
99
|
+
|
|
100
|
+
self.mapping = {
|
|
101
|
+
"clf.3.weight": "final_layer.0.weight",
|
|
102
|
+
"clf.3.bias": "final_layer.0.bias",
|
|
103
|
+
"clf.5.weight": "final_layer.2.weight",
|
|
104
|
+
"clf.5.bias": "final_layer.2.bias",
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
max_pool_size = 2 # Hardcoded to avoid dimensional errors
|
|
108
|
+
time_conv_size = int(np.round(time_conv_size_s * self.sfreq))
|
|
109
|
+
if time_conv_size % 2 == 0:
|
|
110
|
+
if ensure_odd_conv_size:
|
|
111
|
+
time_conv_size += 1
|
|
112
|
+
else:
|
|
113
|
+
raise ValueError(
|
|
114
|
+
"time_conv_size must be an odd number to accommodate the "
|
|
115
|
+
"upsampling step in the decoder blocks."
|
|
116
|
+
)
|
|
117
|
+
|
|
118
|
+
channels = [self.n_chans]
|
|
119
|
+
n_filters = n_time_filters
|
|
120
|
+
for _ in range(depth + 1):
|
|
121
|
+
channels.append(int(n_filters * np.sqrt(complexity_factor)))
|
|
122
|
+
n_filters = int(n_filters * np.sqrt(2))
|
|
123
|
+
self.channels = channels
|
|
124
|
+
|
|
125
|
+
# Instantiate encoder
|
|
126
|
+
self.encoder_blocks = nn.ModuleList(
|
|
127
|
+
_EncoderBlock(
|
|
128
|
+
in_channels=channels[idx],
|
|
129
|
+
out_channels=channels[idx + 1],
|
|
130
|
+
kernel_size=time_conv_size,
|
|
131
|
+
downsample=max_pool_size,
|
|
132
|
+
activation=activation,
|
|
133
|
+
)
|
|
134
|
+
for idx in range(depth)
|
|
135
|
+
)
|
|
136
|
+
|
|
137
|
+
# Instantiate bottom (channels increase, temporal dim stays the same)
|
|
138
|
+
self.bottom = nn.Sequential(
|
|
139
|
+
nn.Conv1d(
|
|
140
|
+
in_channels=channels[-2],
|
|
141
|
+
out_channels=channels[-1],
|
|
142
|
+
kernel_size=time_conv_size,
|
|
143
|
+
padding=(time_conv_size - 1) // 2,
|
|
144
|
+
), # preserves dimension
|
|
145
|
+
activation(),
|
|
146
|
+
nn.BatchNorm1d(num_features=channels[-1]),
|
|
147
|
+
)
|
|
148
|
+
|
|
149
|
+
# Instantiate decoder
|
|
150
|
+
channels_reverse = channels[::-1]
|
|
151
|
+
self.decoder_blocks = nn.ModuleList(
|
|
152
|
+
_DecoderBlock(
|
|
153
|
+
in_channels=channels_reverse[idx],
|
|
154
|
+
out_channels=channels_reverse[idx + 1],
|
|
155
|
+
kernel_size=time_conv_size,
|
|
156
|
+
upsample=max_pool_size,
|
|
157
|
+
with_skip_connection=with_skip_connection,
|
|
158
|
+
activation=activation,
|
|
159
|
+
)
|
|
160
|
+
for idx in range(depth)
|
|
161
|
+
)
|
|
162
|
+
|
|
163
|
+
# The temporal dimension remains unchanged
|
|
164
|
+
# (except through the AvgPooling which collapses it to 1)
|
|
165
|
+
# The spatial dimension is preserved from the end of the UNet, and is mapped to n_classes
|
|
166
|
+
|
|
167
|
+
self.clf = nn.Sequential(
|
|
168
|
+
nn.Conv1d(
|
|
169
|
+
in_channels=channels[1],
|
|
170
|
+
out_channels=channels[1],
|
|
171
|
+
kernel_size=1,
|
|
172
|
+
stride=1,
|
|
173
|
+
padding=0,
|
|
174
|
+
), # output is (B, C, 1, S * T)
|
|
175
|
+
nn.Tanh(),
|
|
176
|
+
nn.AvgPool1d(self.n_times), # output is (B, C, S)
|
|
177
|
+
)
|
|
178
|
+
|
|
179
|
+
self.final_layer = nn.Sequential(
|
|
180
|
+
nn.Conv1d(
|
|
181
|
+
in_channels=channels[1],
|
|
182
|
+
out_channels=self.n_outputs,
|
|
183
|
+
kernel_size=1,
|
|
184
|
+
stride=1,
|
|
185
|
+
padding=0,
|
|
186
|
+
), # output is (B, n_classes, S)
|
|
187
|
+
activation(),
|
|
188
|
+
nn.Conv1d(
|
|
189
|
+
in_channels=self.n_outputs,
|
|
190
|
+
out_channels=self.n_outputs,
|
|
191
|
+
kernel_size=1,
|
|
192
|
+
stride=1,
|
|
193
|
+
padding=0,
|
|
194
|
+
),
|
|
195
|
+
nn.Identity(),
|
|
196
|
+
# output is (B, n_classes, S)
|
|
197
|
+
)
|
|
198
|
+
|
|
199
|
+
def forward(self, x: torch.Tensor) -> torch.Tensor:
|
|
200
|
+
"""If input x has shape (B, S, C, T), return y_pred of shape (B, n_classes, S).
|
|
201
|
+
If input x has shape (B, C, T), return y_pred of shape (B, n_classes).
|
|
202
|
+
"""
|
|
203
|
+
# reshape input
|
|
204
|
+
if x.ndim == 4: # input x has shape (B, S, C, T)
|
|
205
|
+
x = x.permute(0, 2, 1, 3) # (B, C, S, T)
|
|
206
|
+
x = x.flatten(start_dim=2) # (B, C, S * T)
|
|
207
|
+
|
|
208
|
+
# encoder
|
|
209
|
+
residuals = []
|
|
210
|
+
for down in self.encoder_blocks:
|
|
211
|
+
x, res = down(x)
|
|
212
|
+
residuals.append(res)
|
|
213
|
+
|
|
214
|
+
# bottom
|
|
215
|
+
x = self.bottom(x)
|
|
216
|
+
|
|
217
|
+
# decoder
|
|
218
|
+
num_blocks = len(self.decoder_blocks) # statically known
|
|
219
|
+
for idx, dec in enumerate(self.decoder_blocks):
|
|
220
|
+
# pick the matching residual in reverse order
|
|
221
|
+
res = residuals[num_blocks - 1 - idx]
|
|
222
|
+
x = dec(x, res)
|
|
223
|
+
|
|
224
|
+
# classifier
|
|
225
|
+
x = self.clf(x)
|
|
226
|
+
y_pred = self.final_layer(x) # (B, n_classes, seq_length)
|
|
227
|
+
|
|
228
|
+
if y_pred.shape[-1] == 1: # seq_length of 1
|
|
229
|
+
y_pred = y_pred[:, :, 0]
|
|
230
|
+
|
|
231
|
+
return y_pred
|
|
232
|
+
|
|
233
|
+
|
|
234
|
+
class _EncoderBlock(nn.Module):
|
|
235
|
+
"""Encoding block for a timeseries x of shape (B, C, T)."""
|
|
236
|
+
|
|
237
|
+
def __init__(
|
|
238
|
+
self,
|
|
239
|
+
in_channels=2,
|
|
240
|
+
out_channels=2,
|
|
241
|
+
kernel_size=9,
|
|
242
|
+
downsample=2,
|
|
243
|
+
activation: nn.Module = nn.ELU,
|
|
244
|
+
):
|
|
245
|
+
super().__init__()
|
|
246
|
+
self.in_channels = in_channels
|
|
247
|
+
self.out_channels = out_channels
|
|
248
|
+
self.kernel_size = kernel_size
|
|
249
|
+
self.downsample = downsample
|
|
250
|
+
|
|
251
|
+
self.block_prepool = nn.Sequential(
|
|
252
|
+
nn.Conv1d(
|
|
253
|
+
in_channels=in_channels,
|
|
254
|
+
out_channels=out_channels,
|
|
255
|
+
kernel_size=kernel_size,
|
|
256
|
+
padding="same",
|
|
257
|
+
),
|
|
258
|
+
activation(),
|
|
259
|
+
nn.BatchNorm1d(num_features=out_channels),
|
|
260
|
+
)
|
|
261
|
+
|
|
262
|
+
self.pad = nn.ConstantPad1d(padding=1, value=0.0)
|
|
263
|
+
self.maxpool = nn.MaxPool1d(kernel_size=self.downsample, stride=self.downsample)
|
|
264
|
+
|
|
265
|
+
def forward(self, x: torch.Tensor) -> tuple[torch.Tensor, torch.Tensor]:
|
|
266
|
+
x = self.block_prepool(x)
|
|
267
|
+
residual = x
|
|
268
|
+
if x.shape[-1] % 2:
|
|
269
|
+
x = self.pad(x)
|
|
270
|
+
x = self.maxpool(x)
|
|
271
|
+
return x, residual
|
|
272
|
+
|
|
273
|
+
|
|
274
|
+
class _DecoderBlock(nn.Module):
|
|
275
|
+
"""Decoding block for a timeseries x of shape (B, C, T)."""
|
|
276
|
+
|
|
277
|
+
def __init__(
|
|
278
|
+
self,
|
|
279
|
+
in_channels=2,
|
|
280
|
+
out_channels=2,
|
|
281
|
+
kernel_size=9,
|
|
282
|
+
upsample=2,
|
|
283
|
+
with_skip_connection=True,
|
|
284
|
+
activation: nn.Module = nn.ELU,
|
|
285
|
+
):
|
|
286
|
+
super().__init__()
|
|
287
|
+
self.in_channels = in_channels
|
|
288
|
+
self.out_channels = out_channels
|
|
289
|
+
self.kernel_size = kernel_size
|
|
290
|
+
self.upsample = upsample
|
|
291
|
+
self.with_skip_connection = with_skip_connection
|
|
292
|
+
|
|
293
|
+
self.block_preskip = nn.Sequential(
|
|
294
|
+
nn.Upsample(scale_factor=upsample),
|
|
295
|
+
nn.Conv1d(
|
|
296
|
+
in_channels=in_channels,
|
|
297
|
+
out_channels=out_channels,
|
|
298
|
+
kernel_size=2,
|
|
299
|
+
padding="same",
|
|
300
|
+
),
|
|
301
|
+
activation(),
|
|
302
|
+
nn.BatchNorm1d(num_features=out_channels),
|
|
303
|
+
)
|
|
304
|
+
self.block_postskip = nn.Sequential(
|
|
305
|
+
nn.Conv1d(
|
|
306
|
+
in_channels=(
|
|
307
|
+
2 * out_channels if with_skip_connection else out_channels
|
|
308
|
+
),
|
|
309
|
+
out_channels=out_channels,
|
|
310
|
+
kernel_size=kernel_size,
|
|
311
|
+
padding="same",
|
|
312
|
+
),
|
|
313
|
+
activation(),
|
|
314
|
+
nn.BatchNorm1d(num_features=out_channels),
|
|
315
|
+
)
|
|
316
|
+
|
|
317
|
+
def forward(self, x: torch.Tensor, residual: torch.Tensor) -> torch.Tensor:
|
|
318
|
+
x = self.block_preskip(x)
|
|
319
|
+
if self.with_skip_connection:
|
|
320
|
+
x, residual = self._crop_tensors_to_match(
|
|
321
|
+
x, residual, axis=-1
|
|
322
|
+
) # in case of mismatch
|
|
323
|
+
x = torch.cat([x, residual], dim=1) # (B, 2 * C, T)
|
|
324
|
+
x = self.block_postskip(x)
|
|
325
|
+
return x
|
|
326
|
+
|
|
327
|
+
@staticmethod
|
|
328
|
+
def _crop_tensors_to_match(
|
|
329
|
+
x1: torch.Tensor, x2: torch.Tensor, axis: int = -1
|
|
330
|
+
) -> tuple[torch.Tensor, torch.Tensor]:
|
|
331
|
+
"""Crops two tensors to their lowest-common-dimension along an axis."""
|
|
332
|
+
dim_cropped = min(x1.shape[axis], x2.shape[axis])
|
|
333
|
+
|
|
334
|
+
x1_cropped = torch.index_select(
|
|
335
|
+
x1, dim=axis, index=torch.arange(dim_cropped).to(device=x1.device)
|
|
336
|
+
)
|
|
337
|
+
x2_cropped = torch.index_select(
|
|
338
|
+
x2, dim=axis, index=torch.arange(dim_cropped).to(device=x1.device)
|
|
339
|
+
)
|
|
340
|
+
return x1_cropped, x2_cropped
|