braindecode 1.2.0.dev182094932__py3-none-any.whl → 1.3.0.dev168011974__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/augmentation/functional.py +154 -54
- braindecode/augmentation/transforms.py +2 -2
- braindecode/datasets/experimental.py +218 -0
- braindecode/datasets/sleep_physio_challe_18.py +2 -1
- braindecode/datautil/serialization.py +11 -6
- braindecode/models/__init__.py +6 -8
- braindecode/models/atcnet.py +156 -16
- braindecode/models/attentionbasenet.py +151 -26
- braindecode/models/{sleep_stager_eldele_2021.py → attn_sleep.py} +12 -2
- braindecode/models/biot.py +1 -1
- braindecode/models/ctnet.py +1 -1
- braindecode/models/deep4.py +6 -2
- braindecode/models/deepsleepnet.py +118 -5
- braindecode/models/eegconformer.py +114 -15
- braindecode/models/eeginception_erp.py +76 -7
- braindecode/models/eeginception_mi.py +2 -0
- braindecode/models/eegnet.py +27 -190
- braindecode/models/eegnex.py +113 -6
- braindecode/models/eegsimpleconv.py +2 -0
- braindecode/models/eegtcnet.py +1 -1
- braindecode/models/sccnet.py +81 -8
- braindecode/models/shallow_fbcsp.py +2 -0
- braindecode/models/sleep_stager_blanco_2020.py +2 -0
- braindecode/models/sleep_stager_chambon_2018.py +2 -0
- braindecode/models/sparcnet.py +2 -0
- braindecode/models/summary.csv +39 -41
- braindecode/models/tidnet.py +2 -0
- braindecode/models/tsinception.py +15 -3
- braindecode/models/usleep.py +103 -9
- braindecode/models/util.py +5 -5
- braindecode/preprocessing/preprocess.py +31 -28
- braindecode/version.py +1 -1
- {braindecode-1.2.0.dev182094932.dist-info → braindecode-1.3.0.dev168011974.dist-info}/METADATA +7 -2
- {braindecode-1.2.0.dev182094932.dist-info → braindecode-1.3.0.dev168011974.dist-info}/RECORD +38 -38
- braindecode/models/eegresnet.py +0 -362
- {braindecode-1.2.0.dev182094932.dist-info → braindecode-1.3.0.dev168011974.dist-info}/WHEEL +0 -0
- {braindecode-1.2.0.dev182094932.dist-info → braindecode-1.3.0.dev168011974.dist-info}/licenses/LICENSE.txt +0 -0
- {braindecode-1.2.0.dev182094932.dist-info → braindecode-1.3.0.dev168011974.dist-info}/licenses/NOTICE.txt +0 -0
- {braindecode-1.2.0.dev182094932.dist-info → braindecode-1.3.0.dev168011974.dist-info}/top_level.txt +0 -0
|
@@ -22,7 +22,6 @@ from mne.datasets.utils import _get_path
|
|
|
22
22
|
from mne.utils import warn
|
|
23
23
|
|
|
24
24
|
from braindecode.datasets import BaseConcatDataset, BaseDataset
|
|
25
|
-
from braindecode.preprocessing.preprocess import _preprocess
|
|
26
25
|
|
|
27
26
|
PC18_DIR = op.join(op.dirname(__file__), "data", "pc18")
|
|
28
27
|
PC18_RECORDS = op.join(PC18_DIR, "sleep_records.csv")
|
|
@@ -407,6 +406,8 @@ class SleepPhysionetChallenge2018(BaseConcatDataset):
|
|
|
407
406
|
base_dataset = BaseDataset(raw_file, desc)
|
|
408
407
|
|
|
409
408
|
if preproc is not None:
|
|
409
|
+
from braindecode.preprocessing.preprocess import _preprocess
|
|
410
|
+
|
|
410
411
|
_preprocess(base_dataset, None, preproc)
|
|
411
412
|
|
|
412
413
|
return base_dataset
|
|
@@ -138,12 +138,17 @@ def _load_signals(fif_file, preload, is_raw):
|
|
|
138
138
|
with open(pkl_file, "rb") as f:
|
|
139
139
|
signals = pickle.load(f)
|
|
140
140
|
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
141
|
+
if all(f.exists() for f in signals.filenames):
|
|
142
|
+
if preload:
|
|
143
|
+
signals.load_data()
|
|
144
|
+
return signals
|
|
145
|
+
else: # This may happen if the file has been moved together with the pickle file.
|
|
146
|
+
warnings.warn(
|
|
147
|
+
f"Pickle file {pkl_file} exists, but the referenced fif "
|
|
148
|
+
"file(s) do not exist. Will read the fif file(s) directly "
|
|
149
|
+
"and re-create the pickle file.",
|
|
150
|
+
UserWarning,
|
|
151
|
+
)
|
|
147
152
|
|
|
148
153
|
# If pickle didn't exist read via mne (likely slower) and save pkl after
|
|
149
154
|
if is_raw:
|
braindecode/models/__init__.py
CHANGED
|
@@ -4,6 +4,7 @@ Some predefined network architectures for EEG decoding.
|
|
|
4
4
|
|
|
5
5
|
from .atcnet import ATCNet
|
|
6
6
|
from .attentionbasenet import AttentionBaseNet
|
|
7
|
+
from .attn_sleep import AttnSleep
|
|
7
8
|
from .base import EEGModuleMixin
|
|
8
9
|
from .biot import BIOT
|
|
9
10
|
from .contrawr import ContraWR
|
|
@@ -15,9 +16,8 @@ from .eeginception_erp import EEGInceptionERP
|
|
|
15
16
|
from .eeginception_mi import EEGInceptionMI
|
|
16
17
|
from .eegitnet import EEGITNet
|
|
17
18
|
from .eegminer import EEGMiner
|
|
18
|
-
from .eegnet import
|
|
19
|
+
from .eegnet import EEGNet, EEGNetv4
|
|
19
20
|
from .eegnex import EEGNeX
|
|
20
|
-
from .eegresnet import EEGResNet
|
|
21
21
|
from .eegsimpleconv import EEGSimpleConv
|
|
22
22
|
from .eegtcnet import EEGTCNet
|
|
23
23
|
from .fbcnet import FBCNet
|
|
@@ -38,12 +38,11 @@ from .signal_jepa import (
|
|
|
38
38
|
from .sinc_shallow import SincShallowNet
|
|
39
39
|
from .sleep_stager_blanco_2020 import SleepStagerBlanco2020
|
|
40
40
|
from .sleep_stager_chambon_2018 import SleepStagerChambon2018
|
|
41
|
-
from .sleep_stager_eldele_2021 import SleepStagerEldele2021
|
|
42
41
|
from .sparcnet import SPARCNet
|
|
43
42
|
from .syncnet import SyncNet
|
|
44
43
|
from .tcn import BDTCN, TCN
|
|
45
44
|
from .tidnet import TIDNet
|
|
46
|
-
from .tsinception import
|
|
45
|
+
from .tsinception import TSception
|
|
47
46
|
from .usleep import USleep
|
|
48
47
|
from .util import _init_models_dict, models_mandatory_parameters
|
|
49
48
|
|
|
@@ -53,6 +52,7 @@ _init_models_dict()
|
|
|
53
52
|
|
|
54
53
|
__all__ = [
|
|
55
54
|
"ATCNet",
|
|
55
|
+
"AttnSleep",
|
|
56
56
|
"AttentionBaseNet",
|
|
57
57
|
"EEGModuleMixin",
|
|
58
58
|
"BIOT",
|
|
@@ -65,10 +65,9 @@ __all__ = [
|
|
|
65
65
|
"EEGInceptionMI",
|
|
66
66
|
"EEGITNet",
|
|
67
67
|
"EEGMiner",
|
|
68
|
-
"
|
|
68
|
+
"EEGNet",
|
|
69
69
|
"EEGNetv4",
|
|
70
70
|
"EEGNeX",
|
|
71
|
-
"EEGResNet",
|
|
72
71
|
"EEGSimpleConv",
|
|
73
72
|
"EEGTCNet",
|
|
74
73
|
"FBCNet",
|
|
@@ -87,13 +86,12 @@ __all__ = [
|
|
|
87
86
|
"SincShallowNet",
|
|
88
87
|
"SleepStagerBlanco2020",
|
|
89
88
|
"SleepStagerChambon2018",
|
|
90
|
-
"SleepStagerEldele2021",
|
|
91
89
|
"SPARCNet",
|
|
92
90
|
"SyncNet",
|
|
93
91
|
"BDTCN",
|
|
94
92
|
"TCN",
|
|
95
93
|
"TIDNet",
|
|
96
|
-
"
|
|
94
|
+
"TSception",
|
|
97
95
|
"USleep",
|
|
98
96
|
"_init_models_dict",
|
|
99
97
|
"models_mandatory_parameters",
|
braindecode/models/atcnet.py
CHANGED
|
@@ -13,13 +13,154 @@ from braindecode.modules import CausalConv1d, Ensure4d, MaxNormLinear
|
|
|
13
13
|
|
|
14
14
|
|
|
15
15
|
class ATCNet(EEGModuleMixin, nn.Module):
|
|
16
|
-
"""ATCNet
|
|
16
|
+
"""ATCNet from Altaheri et al. (2022) [1]_.
|
|
17
17
|
|
|
18
|
-
|
|
18
|
+
:bdg-success:`Convolution` :bdg-info:`Small Attention`
|
|
19
19
|
|
|
20
20
|
.. figure:: https://user-images.githubusercontent.com/25565236/185449791-e8539453-d4fa-41e1-865a-2cf7e91f60ef.png
|
|
21
|
-
|
|
22
|
-
|
|
21
|
+
:align: center
|
|
22
|
+
:alt: ATCNet Architecture
|
|
23
|
+
:width: 650px
|
|
24
|
+
|
|
25
|
+
.. rubric:: Architectural Overview
|
|
26
|
+
|
|
27
|
+
ATCNet is a *convolution-first* architecture augmented with a *lightweight attention–TCN*
|
|
28
|
+
sequence module. The end-to-end flow is:
|
|
29
|
+
|
|
30
|
+
- (i) :class:`_ConvBlock` learns temporal filter-banks and spatial projections (EEGNet-style),
|
|
31
|
+
downsampling time to a compact feature map;
|
|
32
|
+
|
|
33
|
+
- (ii) Sliding Windows carve overlapping temporal windows from this map;
|
|
34
|
+
|
|
35
|
+
- (iii) for each window, :class:`_AttentionBlock` applies small multi-head self-attention
|
|
36
|
+
over time, followed by a :class:`_TCNResidualBlock` stack (causal, dilated);
|
|
37
|
+
|
|
38
|
+
- (iv) window-level features are aggregated (mean of window logits or concatenation)
|
|
39
|
+
and mapped via a max-norm–constrained linear layer.
|
|
40
|
+
|
|
41
|
+
Relative to ViT, ATCNet replaces linear patch projection with learned *temporal–spatial*
|
|
42
|
+
convolutions; it processes *parallel* window encoders (attention→TCN) instead of a deep
|
|
43
|
+
stack; and swaps the MLP head for a TCN suited to 1-D EEG sequences.
|
|
44
|
+
|
|
45
|
+
.. rubric:: Macro Components
|
|
46
|
+
|
|
47
|
+
- :class:`_ConvBlock` **(Shallow conv stem → feature map)**
|
|
48
|
+
|
|
49
|
+
- *Operations.*
|
|
50
|
+
- **Temporal conv** (:class:`torch.nn.Conv2d`) with kernel ``(L_t, 1)`` builds a
|
|
51
|
+
FIR-like filter bank (``F1`` maps).
|
|
52
|
+
- **Depthwise spatial conv** (:class:`torch.nn.Conv2d`, ``groups=F1``) with kernel
|
|
53
|
+
``(1, n_chans)`` learns per-filter spatial projections (akin to EEGNet’s CSP-like step).
|
|
54
|
+
- **BN → ELU → AvgPool → Dropout** to stabilize and condense activations.
|
|
55
|
+
- **Refining temporal conv** (:class:`torch.nn.Conv2d`) with kernel ``(L_r, 1)`` +
|
|
56
|
+
**BN → ELU → AvgPool → Dropout**.
|
|
57
|
+
|
|
58
|
+
The output shape is ``(B, F2, T_c, 1)`` with ``F2 = F1·D`` and ``T_c = T/(P1·P2)``.
|
|
59
|
+
Temporal kernels behave as FIR filters; the depthwise-spatial conv yields frequency-specific
|
|
60
|
+
topographies. Pooling acts as a local integrator, reducing variance and imposing a
|
|
61
|
+
useful inductive bias on short EEG windows.
|
|
62
|
+
|
|
63
|
+
- **Sliding-Window Sequencer**
|
|
64
|
+
|
|
65
|
+
From the condensed time axis (length ``T_c``), ATCNet forms ``n`` overlapping windows
|
|
66
|
+
of width ``T_w = T_c - n + 1`` (one start per index). Each window produces a sequence
|
|
67
|
+
``(B, F2, T_w)`` forwarded to its own attention–TCN branch. This creates *parallel*
|
|
68
|
+
encoders over shifted contexts and is key to robustness on nonstationary EEG.
|
|
69
|
+
|
|
70
|
+
- :class:`_AttentionBlock` **(small MHA on temporal positions)**
|
|
71
|
+
|
|
72
|
+
- *Operations.*
|
|
73
|
+
- Rearrange to ``(B, T_w, F2)``,
|
|
74
|
+
- Normalization :class:`torch.nn.LayerNorm`
|
|
75
|
+
- Custom MultiHeadAttention :class:`_MHA` (``num_heads=H``, per-head dim ``d_h``) + residual add,
|
|
76
|
+
- Dropout :class:`torch.nn.Dropout`
|
|
77
|
+
- Rearrange back to ``(B, F2, T_w)``.
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
**Note**: Attention is *local to a window* and purely temporal.
|
|
81
|
+
|
|
82
|
+
*Role.* Re-weights evidence across the window, letting the model emphasize informative
|
|
83
|
+
segments (onsets, bursts) before causal convolutions aggregate history.
|
|
84
|
+
|
|
85
|
+
- :class:`_TCNResidualBlock` **(causal dilated temporal CNN)**
|
|
86
|
+
|
|
87
|
+
- *Operations.*
|
|
88
|
+
- Two :class:`braindecode.modules.CausalConv1d` layers per block with dilation ``1, 2, 4, …``
|
|
89
|
+
- Across blocks of `torch.nn.ELU` + `torch.nn.BatchNorm1d` + `torch.nn.Dropout`) +
|
|
90
|
+
a residual (identity or 1x1 mapping).
|
|
91
|
+
- The final feature used per window is the *last* causal step ``[..., -1]`` (forecast-style).
|
|
92
|
+
|
|
93
|
+
*Role.* Efficient long-range temporal integration with stable gradients; the dilated
|
|
94
|
+
receptive field complements attention’s soft selection.
|
|
95
|
+
|
|
96
|
+
- **Aggregation & Classifier**
|
|
97
|
+
|
|
98
|
+
- *Operations.*
|
|
99
|
+
- Either (a) map each window feature ``(B, F2)`` to logits via :class:`braindecode.modules.MaxNormLinear`
|
|
100
|
+
and **average** across windows (default, matching official code), or
|
|
101
|
+
- (b) **concatenate** all window features ``(B, n·F2)`` and apply a single :class:`MaxNormLinear`.
|
|
102
|
+
The max-norm constraint regularizes the readout.
|
|
103
|
+
|
|
104
|
+
.. rubric:: Convolutional Details
|
|
105
|
+
|
|
106
|
+
- **Temporal.** Temporal structure is learned in three places:
|
|
107
|
+
- (1) the stem’s wide ``(L_t, 1)`` conv (learned filter bank),
|
|
108
|
+
- (2) the refining ``(L_r, 1)`` conv after pooling (short-term dynamics), and
|
|
109
|
+
- (3) the TCN’s causal 1-D convolutions with exponentially increasing dilation
|
|
110
|
+
(long-range dependencies). The minimum sequence length required by the TCN stack is
|
|
111
|
+
``(K_t - 1)·2^{L-1} + 1``; the implementation *auto-scales* kernels/pools/windows
|
|
112
|
+
when inputs are shorter to preserve feasibility.
|
|
113
|
+
|
|
114
|
+
- **Spatial.** A depthwise spatial conv spans the **full montage** (kernel ``(1, n_chans)``),
|
|
115
|
+
producing *per-temporal-filter* spatial projections (no cross-filter mixing at this step).
|
|
116
|
+
This mirrors EEGNet’s interpretability: each temporal filter has its own spatial pattern.
|
|
117
|
+
|
|
118
|
+
|
|
119
|
+
.. rubric:: Attention / Sequential Modules
|
|
120
|
+
|
|
121
|
+
- **Type.** Multi-head self-attention with ``H`` heads and per-head dim ``d_h`` implemented
|
|
122
|
+
in :class:`_MHA`, allowing ``embed_dim = H·d_h`` independent of input and output dims.
|
|
123
|
+
- **Shapes.** ``(B, F2, T_w) → (B, T_w, F2) → (B, F2, T_w)``. Attention operates along
|
|
124
|
+
the **temporal** axis within a window; channels/features stay in the embedding dim ``F2``.
|
|
125
|
+
- **Role.** Highlights salient temporal positions prior to causal convolution; small attention
|
|
126
|
+
keeps compute modest while improving context modeling over pooled features.
|
|
127
|
+
|
|
128
|
+
.. rubric:: Additional Mechanisms
|
|
129
|
+
|
|
130
|
+
- **Parallel encoders over shifted windows.** Improves montage/phase robustness by
|
|
131
|
+
ensembling nearby contexts rather than committing to a single segmentation.
|
|
132
|
+
- **Max-norm classifier.** Enforces weight norm constraints at the readout, a common
|
|
133
|
+
stabilization trick in EEG decoding.
|
|
134
|
+
- **ViT vs. ATCNet (design choices).** Convolutional *nonlinear* projection rather than
|
|
135
|
+
linear patchification; attention followed by **TCN** (not MLP); *parallel* window
|
|
136
|
+
encoders rather than stacked encoders.
|
|
137
|
+
|
|
138
|
+
.. rubric:: Usage and Configuration
|
|
139
|
+
|
|
140
|
+
- ``conv_block_n_filters (F1)``, ``conv_block_depth_mult (D)`` → capacity of the stem
|
|
141
|
+
(with ``F2 = F1·D`` feeding attention/TCN), dimensions aligned to ``F2``, like :class:`EEGNet`.
|
|
142
|
+
- Pool sizes ``P1,P2`` trade temporal resolution for stability/compute; they set
|
|
143
|
+
``T_c = T/(P1·P2)`` and thus window width ``T_w``.
|
|
144
|
+
- ``n_windows`` controls the ensemble over shifts (compute ∝ windows).
|
|
145
|
+
- ``att_num_heads``, ``att_head_dim`` set attention capacity; keep ``H·d_h ≈ F2``.
|
|
146
|
+
- ``tcn_depth``, ``tcn_kernel_size`` govern receptive field; larger values demand
|
|
147
|
+
longer inputs (see minimum length above). The implementation warns and *rescales*
|
|
148
|
+
kernels/pools/windows if inputs are too short.
|
|
149
|
+
- **Aggregation choice.** ``concat=False`` (default, average of per-window logits) matches
|
|
150
|
+
the official code; ``concat=True`` mirrors the paper’s concatenation variant.
|
|
151
|
+
|
|
152
|
+
|
|
153
|
+
Notes
|
|
154
|
+
-----
|
|
155
|
+
- Inputs substantially shorter than the implied minimum length trigger **automatic
|
|
156
|
+
downscaling** of kernels, pools, windows, and TCN kernel size to maintain validity.
|
|
157
|
+
- The attention–TCN sequence operates **per window**; the last causal step is used as the
|
|
158
|
+
window feature, aligning the temporal semantics across windows.
|
|
159
|
+
|
|
160
|
+
.. versionadded:: 1.1
|
|
161
|
+
|
|
162
|
+
- More detailed documentation of the model.
|
|
163
|
+
|
|
23
164
|
|
|
24
165
|
Parameters
|
|
25
166
|
----------
|
|
@@ -85,15 +226,13 @@ class ATCNet(EEGModuleMixin, nn.Module):
|
|
|
85
226
|
Maximum L2-norm constraint imposed on weights of the last
|
|
86
227
|
fully-connected layer. Defaults to 0.25.
|
|
87
228
|
|
|
88
|
-
|
|
89
229
|
References
|
|
90
230
|
----------
|
|
91
|
-
.. [1] H. Altaheri, G. Muhammad
|
|
92
|
-
Physics-informed attention temporal convolutional network for EEG-based
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
https://github.com/Altaheri/EEG-ATCNet/blob/main/models.py
|
|
231
|
+
.. [1] H. Altaheri, G. Muhammad, M. Alsulaiman (2022).
|
|
232
|
+
*Physics-informed attention temporal convolutional network for EEG-based motor imagery classification.*
|
|
233
|
+
IEEE Transactions on Industrial Informatics. doi:10.1109/TII.2022.3197419.
|
|
234
|
+
.. [2] Official EEG-ATCNet implementation (TensorFlow):
|
|
235
|
+
https://github.com/Altaheri/EEG-ATCNet/blob/main/models.py
|
|
97
236
|
"""
|
|
98
237
|
|
|
99
238
|
def __init__(
|
|
@@ -231,7 +370,7 @@ class ATCNet(EEGModuleMixin, nn.Module):
|
|
|
231
370
|
nn.Sequential(
|
|
232
371
|
*[
|
|
233
372
|
_TCNResidualBlock(
|
|
234
|
-
in_channels=self.F2,
|
|
373
|
+
in_channels=self.F2 if i == 0 else self.tcn_n_filters,
|
|
235
374
|
kernel_size=self.tcn_kernel_size,
|
|
236
375
|
n_filters=self.tcn_n_filters,
|
|
237
376
|
dropout=self.tcn_dropout,
|
|
@@ -249,7 +388,7 @@ class ATCNet(EEGModuleMixin, nn.Module):
|
|
|
249
388
|
self.final_layer = nn.ModuleList(
|
|
250
389
|
[
|
|
251
390
|
MaxNormLinear(
|
|
252
|
-
in_features=self.
|
|
391
|
+
in_features=self.tcn_n_filters * self.n_windows,
|
|
253
392
|
out_features=self.n_outputs,
|
|
254
393
|
max_norm_val=self.max_norm_const,
|
|
255
394
|
)
|
|
@@ -259,7 +398,7 @@ class ATCNet(EEGModuleMixin, nn.Module):
|
|
|
259
398
|
self.final_layer = nn.ModuleList(
|
|
260
399
|
[
|
|
261
400
|
MaxNormLinear(
|
|
262
|
-
in_features=self.
|
|
401
|
+
in_features=self.tcn_n_filters,
|
|
263
402
|
out_features=self.n_outputs,
|
|
264
403
|
max_norm_val=self.max_norm_const,
|
|
265
404
|
)
|
|
@@ -556,7 +695,8 @@ class _TCNResidualBlock(nn.Module):
|
|
|
556
695
|
# Reshape the input for the residual connection when necessary
|
|
557
696
|
if in_channels != n_filters:
|
|
558
697
|
self.reshaping_conv = nn.Conv1d(
|
|
559
|
-
|
|
698
|
+
in_channels=in_channels, # Specify input channels
|
|
699
|
+
out_channels=n_filters, # Specify output channels
|
|
560
700
|
kernel_size=1,
|
|
561
701
|
padding="same",
|
|
562
702
|
)
|
|
@@ -576,7 +716,7 @@ class _TCNResidualBlock(nn.Module):
|
|
|
576
716
|
out = self.activation(out)
|
|
577
717
|
out = self.drop2(out)
|
|
578
718
|
|
|
579
|
-
|
|
719
|
+
X = self.reshaping_conv(X)
|
|
580
720
|
|
|
581
721
|
# ----- Residual connection -----
|
|
582
722
|
out = X + out
|
|
@@ -26,25 +26,150 @@ from braindecode.modules.attention import (
|
|
|
26
26
|
class AttentionBaseNet(EEGModuleMixin, nn.Module):
|
|
27
27
|
"""AttentionBaseNet from Wimpff M et al. (2023) [Martin2023]_.
|
|
28
28
|
|
|
29
|
+
:bdg-success:`Convolution` :bdg-info:`Small Attention`
|
|
30
|
+
|
|
29
31
|
.. figure:: https://content.cld.iop.org/journals/1741-2552/21/3/036020/revision2/jnead48b9f2_hr.jpg
|
|
30
|
-
|
|
31
|
-
|
|
32
|
+
:align: center
|
|
33
|
+
:alt: AttentionBaseNet Architecture
|
|
34
|
+
:width: 640px
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
.. rubric:: Architectural Overview
|
|
38
|
+
|
|
39
|
+
AttentionBaseNet is a *convolution-first* network with a *channel-attention* stage.
|
|
40
|
+
The end-to-end flow is:
|
|
41
|
+
|
|
42
|
+
- (i) :class:`_FeatureExtractor` learns a temporal filter bank and per-filter spatial
|
|
43
|
+
projections (depthwise across electrodes), then condenses time by pooling;
|
|
44
|
+
- (ii) **Channel Expansion** uses a ``1x1`` convolution to set the feature width;
|
|
45
|
+
- (iii) :class:`_ChannelAttentionBlock` refines features via depthwise–pointwise temporal
|
|
46
|
+
convs and an optional channel-attention module (SE/CBAM/ECA/…);
|
|
47
|
+
- (iv) **Classifier** flattens the sequence and applies a linear readout.
|
|
48
|
+
|
|
49
|
+
This design mirrors shallow CNN pipelines (EEGNet-style stem) but inserts a pluggable
|
|
50
|
+
attention unit that *re-weights channels* (and optionally temporal positions) before
|
|
51
|
+
classification.
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
.. rubric:: Macro Components
|
|
55
|
+
|
|
56
|
+
- :class:`_FeatureExtractor` **(Shallow conv stem → condensed feature map)**
|
|
57
|
+
|
|
58
|
+
- *Operations.*
|
|
59
|
+
- **Temporal conv** (:class:`torch.nn.Conv2d`) with kernel ``(1, L_t)`` creates a learned
|
|
60
|
+
FIR-like filter bank with ``n_temporal_filters`` maps.
|
|
61
|
+
- **Depthwise spatial conv** (:class:`torch.nn.Conv2d`, ``groups=n_temporal_filters``)
|
|
62
|
+
with kernel ``(n_chans, 1)`` learns per-filter spatial projections over the full montage.
|
|
63
|
+
- **BatchNorm → ELU → AvgPool → Dropout** stabilize and downsample time.
|
|
64
|
+
- Output shape: ``(B, F2, 1, T₁)`` with ``F2 = n_temporal_filters x spatial_expansion``.
|
|
65
|
+
|
|
66
|
+
*Interpretability/robustness.* Temporal kernels behave as analyzable FIR filters; the
|
|
67
|
+
depthwise spatial step yields rhythm-specific topographies. Pooling acts as a local
|
|
68
|
+
integrator that reduces variance on short EEG windows.
|
|
69
|
+
|
|
70
|
+
- **Channel Expansion**
|
|
71
|
+
|
|
72
|
+
- *Operations.*
|
|
73
|
+
- A ``1x1`` conv → BN → activation maps ``F2 → ch_dim`` without changing
|
|
74
|
+
the temporal length ``T₁`` (shape: ``(B, ch_dim, 1, T₁)``).
|
|
75
|
+
This sets the embedding width for the attention block.
|
|
76
|
+
|
|
77
|
+
- :class:`_ChannelAttentionBlock` **(temporal refinement + channel attention)**
|
|
78
|
+
|
|
79
|
+
- *Operations.*
|
|
80
|
+
- **Depthwise temporal conv** ``(1, L_a)`` (groups=``ch_dim``) + **pointwise ``1x1``**,
|
|
81
|
+
BN and activation → preserves shape ``(B, ch_dim, 1, T₁)`` while refining timing.
|
|
82
|
+
- **Optional attention module** (see *Additional Mechanisms*) applies channel reweighting
|
|
83
|
+
(some variants also apply temporal gating).
|
|
84
|
+
- **AvgPool (1, P₂)** with stride ``(1, S₂)`` and **Dropout** → outputs
|
|
85
|
+
``(B, ch_dim, 1, T₂)``.
|
|
86
|
+
|
|
87
|
+
*Role.* Emphasizes informative channels (and, in certain modes, salient time steps)
|
|
88
|
+
before the classifier; complements the convolutional priors with adaptive re-weighting.
|
|
89
|
+
|
|
90
|
+
- **Classifier (aggregation + readout)**
|
|
91
|
+
|
|
92
|
+
*Operations.* :class:`torch.nn.Flatten` → :class:`torch.nn.Linear` from
|
|
93
|
+
``(B, ch_dim·T₂)`` to classes.
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
.. rubric:: Convolutional Details
|
|
97
|
+
|
|
98
|
+
- **Temporal (where time-domain patterns are learned).**
|
|
99
|
+
Wide kernels in the stem (``(1, L_t)``) act as a learned filter bank for oscillatory
|
|
100
|
+
bands/transients; the attention block’s depthwise temporal conv (``(1, L_a)``) sharpens
|
|
101
|
+
short-term dynamics after downsampling. Pool sizes/strides (``P₁,S₁`` then ``P₂,S₂``)
|
|
102
|
+
set the token rate and effective temporal resolution.
|
|
103
|
+
|
|
104
|
+
- **Spatial (how electrodes are processed).**
|
|
105
|
+
A depthwise spatial conv with kernel ``(n_chans, 1)`` spans the full montage to
|
|
106
|
+
learn *per-temporal-filter* spatial projections (no cross-filter mixing at this step),
|
|
107
|
+
mirroring the interpretable spatial stage in shallow CNNs.
|
|
32
108
|
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
109
|
+
- **Spectral (how frequency content is captured).**
|
|
110
|
+
No explicit Fourier/wavelet transform is used in the stem—spectral selectivity
|
|
111
|
+
emerges from learned temporal kernels. When ``attention_mode="fca"``, a frequency
|
|
112
|
+
channel attention (DCT-based) summarizes frequencies to drive channel weights.
|
|
36
113
|
|
|
37
|
-
The paper and original code with more details about the methodological
|
|
38
|
-
choices are available at the [Martin2023]_ and [MartinCode]_.
|
|
39
114
|
|
|
40
|
-
|
|
41
|
-
- Input Block that performs a temporal convolution and a spatial
|
|
42
|
-
convolution.
|
|
43
|
-
- Channel Expansion that modifies the number of channels.
|
|
44
|
-
- An attention block that performs channel attention with several
|
|
45
|
-
options
|
|
46
|
-
- ClassificationHead
|
|
115
|
+
.. rubric:: Attention / Sequential Modules
|
|
47
116
|
|
|
117
|
+
- **Type.** Channel attention chosen by ``attention_mode`` (SE, ECA, CBAM, CAT, GSoP,
|
|
118
|
+
EncNet, GE, GCT, SRM, CATLite). Most operate purely on channels; CBAM/CAT additionally
|
|
119
|
+
include temporal attention.
|
|
120
|
+
|
|
121
|
+
- **Shapes.** Input/Output around attention: ``(B, ch_dim, 1, T₁)``. Re-arrangements
|
|
122
|
+
(if any) are internal to the module; the block returns the same shape before pooling.
|
|
123
|
+
|
|
124
|
+
- **Role.** Re-weights channels (and optionally time) to highlight informative sources
|
|
125
|
+
and suppress distractors, improving SNR ahead of the linear head.
|
|
126
|
+
|
|
127
|
+
|
|
128
|
+
.. rubric:: Additional Mechanisms
|
|
129
|
+
|
|
130
|
+
- **Attention variants at a glance.**
|
|
131
|
+
- ``"se"``: Squeeze-and-Excitation (global pooling → bottleneck → gates).
|
|
132
|
+
- ``"gsop"``: Global second-order pooling (covariance-aware channel weights).
|
|
133
|
+
- ``"fca"``: Frequency Channel Attention (DCT summary; uses ``seq_len`` and ``freq_idx``).
|
|
134
|
+
- ``"encnet"``: EncNet with learned codewords (uses ``n_codewords``).
|
|
135
|
+
- ``"eca"``: Efficient Channel Attention (local 1-D conv over channel descriptor; uses ``kernel_size``).
|
|
136
|
+
- ``"ge"``: Gather–Excite (context pooling with optional MLP; can use ``extra_params``).
|
|
137
|
+
- ``"gct"``: Gated Channel Transformation (global context normalization + gating).
|
|
138
|
+
- ``"srm"``: Style-based recalibration (mean–std descriptors; optional MLP).
|
|
139
|
+
- ``"cbam"``: Channel then temporal attention (uses ``kernel_size``).
|
|
140
|
+
- ``"cat"`` / ``"catlite"``: Collaborative (channel ± temporal) attention; *lite* omits temporal.
|
|
141
|
+
- **Auto-compatibility on short inputs.**
|
|
142
|
+
|
|
143
|
+
If the input duration is too short for the configured kernels/pools, the implementation
|
|
144
|
+
**automatically rescales** temporal lengths/strides downward (with a warning) to keep
|
|
145
|
+
shapes valid and preserve the pipeline semantics.
|
|
146
|
+
|
|
147
|
+
|
|
148
|
+
.. rubric:: Usage and Configuration
|
|
149
|
+
|
|
150
|
+
- ``n_temporal_filters``, ``temporal_filter_length`` and ``spatial_expansion``:
|
|
151
|
+
control the capacity and the number of spatial projections in the stem.
|
|
152
|
+
- ``pool_length_inp``, ``pool_stride_inp`` then ``pool_length``, ``pool_stride``:
|
|
153
|
+
trade temporal resolution for compute; they determine the final sequence length ``T₂``.
|
|
154
|
+
- ``ch_dim``: width after the ``1x1`` expansion and the effective embedding size for attention.
|
|
155
|
+
- ``attention_mode`` + its specific hyperparameters (``reduction_rate``,
|
|
156
|
+
``kernel_size``, ``seq_len``, ``freq_idx``, ``n_codewords``, ``use_mlp``):
|
|
157
|
+
select and tune the reweighting mechanism.
|
|
158
|
+
- ``drop_prob_inp`` and ``drop_prob_attn``: regularize stem and attention stages.
|
|
159
|
+
- **Training tips.**
|
|
160
|
+
|
|
161
|
+
Start with moderate pooling (e.g., ``P₁=75,S₁=15``) and ELU activations; enable attention
|
|
162
|
+
only after the stem learns stable filters. For small datasets, prefer simpler modes
|
|
163
|
+
(``"se"``, ``"eca"``) before heavier ones (``"gsop"``, ``"encnet"``).
|
|
164
|
+
|
|
165
|
+
Notes
|
|
166
|
+
-----
|
|
167
|
+
- Sequence length after each stage is computed internally; the final classifier expects
|
|
168
|
+
a flattened ``ch_dim x T₂`` vector.
|
|
169
|
+
- Attention operates on *channel* dimension by design; temporal gating exists only in
|
|
170
|
+
specific variants (CBAM/CAT).
|
|
171
|
+
- The paper and original code with more details about the methodological
|
|
172
|
+
choices are available at the [Martin2023]_ and [MartinCode]_.
|
|
48
173
|
.. versionadded:: 0.9
|
|
49
174
|
|
|
50
175
|
Parameters
|
|
@@ -73,18 +198,18 @@ class AttentionBaseNet(EEGModuleMixin, nn.Module):
|
|
|
73
198
|
the depth of the network after the initial layer. Default is 16.
|
|
74
199
|
attention_mode : str, optional
|
|
75
200
|
The type of attention mechanism to apply. If `None`, no attention is applied.
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
201
|
+
- "se" for Squeeze-and-excitation network
|
|
202
|
+
- "gsop" for Global Second-Order Pooling
|
|
203
|
+
- "fca" for Frequency Channel Attention Network
|
|
204
|
+
- "encnet" for context encoding module
|
|
205
|
+
- "eca" for Efficient channel attention for deep convolutional neural networks
|
|
206
|
+
- "ge" for Gather-Excite
|
|
207
|
+
- "gct" for Gated Channel Transformation
|
|
208
|
+
- "srm" for Style-based Recalibration Module
|
|
209
|
+
- "cbam" for Convolutional Block Attention Module
|
|
210
|
+
- "cat" for Learning to collaborate channel and temporal attention
|
|
211
|
+
from multi-information fusion
|
|
212
|
+
- "catlite" for Learning to collaborate channel attention
|
|
88
213
|
from multi-information fusion (lite version, cat w/o temporal attention)
|
|
89
214
|
pool_length : int, default=8
|
|
90
215
|
The length of the window for the average pooling operation.
|
|
@@ -8,18 +8,19 @@ from copy import deepcopy
|
|
|
8
8
|
|
|
9
9
|
import torch
|
|
10
10
|
import torch.nn.functional as F
|
|
11
|
+
from mne.utils import deprecated
|
|
11
12
|
from torch import nn
|
|
12
13
|
|
|
13
14
|
from braindecode.models.base import EEGModuleMixin
|
|
14
15
|
from braindecode.modules import CausalConv1d
|
|
15
16
|
|
|
16
17
|
|
|
17
|
-
class
|
|
18
|
+
class AttnSleep(EEGModuleMixin, nn.Module):
|
|
18
19
|
"""Sleep Staging Architecture from Eldele et al. (2021) [Eldele2021]_.
|
|
19
20
|
|
|
20
21
|
.. figure:: https://raw.githubusercontent.com/emadeldeen24/AttnSleep/refs/heads/main/imgs/AttnSleep.png
|
|
21
22
|
:align: center
|
|
22
|
-
:alt:
|
|
23
|
+
:alt: AttnSleep Architecture
|
|
23
24
|
|
|
24
25
|
Attention based Neural Net for sleep staging as described in [Eldele2021]_.
|
|
25
26
|
The code for the paper and this model is also available at [1]_.
|
|
@@ -533,3 +534,12 @@ class _PositionwiseFeedForward(nn.Module):
|
|
|
533
534
|
def forward(self, x: torch.Tensor) -> torch.Tensor:
|
|
534
535
|
"""Implements FFN equation."""
|
|
535
536
|
return self.w_2(self.dropout(self.activate(self.w_1(x))))
|
|
537
|
+
|
|
538
|
+
|
|
539
|
+
@deprecated(
|
|
540
|
+
"`SleepStagerEldele2021` was renamed to `AttnSleep` in v1.12 to follow original author's name; this alias will be removed in v1.14."
|
|
541
|
+
)
|
|
542
|
+
class SleepStagerEldele2021(AttnSleep):
|
|
543
|
+
"""Deprecated alias for SleepStagerEldele2021."""
|
|
544
|
+
|
|
545
|
+
pass
|
braindecode/models/biot.py
CHANGED
|
@@ -17,7 +17,7 @@ class BIOT(EEGModuleMixin, nn.Module):
|
|
|
17
17
|
|
|
18
18
|
BIOT: Cross-data Biosignal Learning in the Wild.
|
|
19
19
|
|
|
20
|
-
BIOT is a large
|
|
20
|
+
BIOT is a large brain model for biosignal classification. It is
|
|
21
21
|
a wrapper around the `BIOTEncoder` and `ClassificationHead` modules.
|
|
22
22
|
|
|
23
23
|
It is designed for N-dimensional biosignal data such as EEG, ECG, etc.
|
braindecode/models/ctnet.py
CHANGED
|
@@ -39,7 +39,7 @@ class CTNet(EEGModuleMixin, nn.Module):
|
|
|
39
39
|
The architecture consists of three main components:
|
|
40
40
|
|
|
41
41
|
1. **Convolutional Module**:
|
|
42
|
-
- Apply
|
|
42
|
+
- Apply :class:`EEGNet` to perform some feature extraction, denoted here as
|
|
43
43
|
_PatchEmbeddingEEGNet module.
|
|
44
44
|
|
|
45
45
|
2. **Transformer Encoder Module**:
|
braindecode/models/deep4.py
CHANGED
|
@@ -19,9 +19,13 @@ from braindecode.modules import (
|
|
|
19
19
|
class Deep4Net(EEGModuleMixin, nn.Sequential):
|
|
20
20
|
"""Deep ConvNet model from Schirrmeister et al (2017) [Schirrmeister2017]_.
|
|
21
21
|
|
|
22
|
-
|
|
22
|
+
:bdg-success:`Convolution`
|
|
23
|
+
|
|
24
|
+
.. figure:: https://onlinelibrary.wiley.com/cms/asset/fc200ccc-d8c4-45b4-8577-56ce4d15999a/hbm23730-fig-0001-m.jpg
|
|
23
25
|
:align: center
|
|
24
|
-
:alt:
|
|
26
|
+
:alt: Deep4Net Architecture
|
|
27
|
+
:width: 600px
|
|
28
|
+
|
|
25
29
|
|
|
26
30
|
Model described in [Schirrmeister2017]_.
|
|
27
31
|
|