braindecode 1.2.0.dev169062562__py3-none-any.whl → 1.2.0.dev176281713__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/datasets/experimental.py +218 -0
- braindecode/models/__init__.py +1 -2
- braindecode/models/atcnet.py +152 -12
- braindecode/models/attentionbasenet.py +151 -26
- braindecode/models/eegconformer.py +114 -15
- braindecode/models/eegnet.py +53 -4
- braindecode/models/eegnex.py +115 -6
- braindecode/models/summary.csv +40 -41
- braindecode/models/util.py +0 -1
- braindecode/modules/attention.py +6 -3
- braindecode/modules/convolution.py +10 -7
- braindecode/modules/filter.py +8 -6
- braindecode/training/scoring.py +2 -8
- braindecode/version.py +1 -1
- braindecode/visualization/gradients.py +6 -1
- {braindecode-1.2.0.dev169062562.dist-info → braindecode-1.2.0.dev176281713.dist-info}/METADATA +5 -1
- {braindecode-1.2.0.dev169062562.dist-info → braindecode-1.2.0.dev176281713.dist-info}/RECORD +21 -20
- {braindecode-1.2.0.dev169062562.dist-info → braindecode-1.2.0.dev176281713.dist-info}/WHEEL +0 -0
- {braindecode-1.2.0.dev169062562.dist-info → braindecode-1.2.0.dev176281713.dist-info}/licenses/LICENSE.txt +0 -0
- {braindecode-1.2.0.dev169062562.dist-info → braindecode-1.2.0.dev176281713.dist-info}/licenses/NOTICE.txt +0 -0
- {braindecode-1.2.0.dev169062562.dist-info → braindecode-1.2.0.dev176281713.dist-info}/top_level.txt +0 -0
|
@@ -12,33 +12,129 @@ from braindecode.modules import FeedForwardBlock, MultiHeadAttention
|
|
|
12
12
|
|
|
13
13
|
|
|
14
14
|
class EEGConformer(EEGModuleMixin, nn.Module):
|
|
15
|
-
"""EEG Conformer from Song et al. (2022)
|
|
15
|
+
"""EEG Conformer from Song et al. (2022) [song2022]_.
|
|
16
16
|
|
|
17
|
-
|
|
17
|
+
:bdg-success:`Convolution` :bdg-info:`Small Attention`
|
|
18
|
+
|
|
19
|
+
.. figure:: https://raw.githubusercontent.com/eeyhsong/EEG-Conformer/refs/heads/main/visualization/Fig1.png
|
|
18
20
|
:align: center
|
|
19
21
|
:alt: EEGConformer Architecture
|
|
22
|
+
:width: 600px
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
.. rubric:: Architectural Overview
|
|
26
|
+
|
|
27
|
+
EEG-Conformer is a *convolution-first* model augmented with a *lightweight transformer
|
|
28
|
+
encoder*. The end-to-end flow is:
|
|
29
|
+
|
|
30
|
+
- (i) :class:`_PatchEmbedding` converts the continuous EEG into a compact sequence of tokens via a
|
|
31
|
+
:class:`ShallowFBCSPNet` temporal–spatial conv stem and temporal pooling;
|
|
32
|
+
- (ii) :class:`_TransformerEncoder` applies small multi-head self-attention to integrate
|
|
33
|
+
longer-range temporal context across tokens;
|
|
34
|
+
- (iii) :class:`_ClassificationHead` aggregates the sequence and performs a linear readout.
|
|
35
|
+
This preserves the strong inductive biases of shallow CNN filter banks while adding
|
|
36
|
+
just enough attention to capture dependencies beyond the pooling horizon [song2022]_.
|
|
37
|
+
|
|
38
|
+
.. rubric:: Macro Components
|
|
39
|
+
|
|
40
|
+
- :class:`_PatchEmbedding` **(Shallow conv stem → tokens)**
|
|
41
|
+
|
|
42
|
+
- *Operations.*
|
|
43
|
+
- A temporal convolution (`:class:torch.nn.Conv2d`) ``(1 x L_t)`` forms a data-driven "filter bank";
|
|
44
|
+
- A spatial convolution (`:class:torch.nn.Conv2d`) (n_chans x 1)`` projects across electrodes,
|
|
45
|
+
collapsing the channel axis into a virtual channel.
|
|
46
|
+
- **Normalization function** :class:`torch.nn.BatchNorm`
|
|
47
|
+
- **Activation function** :class:`torch.nn.ELU`
|
|
48
|
+
- **Average Pooling** :class:`torch.nn.AvgPool` along time (kernel ``(1, P)`` with stride ``(1, S)``)
|
|
49
|
+
- final ``1x1`` :class:`torch.nn.Linear` projection.
|
|
50
|
+
|
|
51
|
+
The result is rearranged to a token sequence ``(B, S_tokens, D)``, where ``D = n_filters_time``.
|
|
52
|
+
|
|
53
|
+
*Interpretability/robustness.* Temporal kernels can be inspected as FIR filters;
|
|
54
|
+
the spatial conv yields channel projections analogous to :class:`ShallowFBCSPNet`’s learned
|
|
55
|
+
spatial filters. Temporal pooling stabilizes statistics and reduces sequence length.
|
|
56
|
+
|
|
57
|
+
- :class:`_TransformerEncoder` **(context over temporal tokens)**
|
|
58
|
+
|
|
59
|
+
- *Operations.*
|
|
60
|
+
- A stack of ``att_depth`` encoder blocks. :class:`_TransformerEncoderBlock`
|
|
61
|
+
- Each block applies LayerNorm :class:`torch.nn.LayerNorm`
|
|
62
|
+
- Multi-Head Self-Attention (``att_heads``) with dropout + residual :class:`MultiHeadAttention` (:class:`torch.nn.Dropout`)
|
|
63
|
+
- LayerNorm :class:`torch.nn.LayerNorm`
|
|
64
|
+
- 2-layer feed-forward (≈4x expansion, :class:`torch.nn.GELU`) with dropout + residual.
|
|
65
|
+
|
|
66
|
+
Shapes remain ``(B, S_tokens, D)`` throughout.
|
|
20
67
|
|
|
21
|
-
|
|
68
|
+
*Role.* Small attention focuses on interactions among *temporal patches* (not channels),
|
|
69
|
+
extending effective receptive fields at modest cost.
|
|
22
70
|
|
|
23
|
-
|
|
24
|
-
choices are available at the [song2022]_ and [ConformerCode]_.
|
|
71
|
+
- :class:`ClassificationHead` **(aggregation + readout)**
|
|
25
72
|
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
73
|
+
- *Operations*.
|
|
74
|
+
- Flatten, :class:`torch.nn.Flatten` the sequence ``(B, S_tokens·D)`` -
|
|
75
|
+
- MLP (:class:`torch.nn.Linear` → activation (default: :class:`torch.nn.ELU`) → :class:`torch.nn.Dropout` → :class:`torch.nn.Linear`)
|
|
76
|
+
- final Linear to classes.
|
|
29
77
|
|
|
30
|
-
|
|
78
|
+
With ``return_features=True``, features before the last Linear can be exported for
|
|
79
|
+
linear probing or downstream tasks.
|
|
31
80
|
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
81
|
+
.. rubric:: Convolutional Details
|
|
82
|
+
|
|
83
|
+
- **Temporal (where time-domain patterns are learned).**
|
|
84
|
+
The initial ``(1 x L_t)`` conv per channel acts as a *learned filter bank* for oscillatory
|
|
85
|
+
bands and transients. Subsequent **AvgPool** along time performs local integration,
|
|
86
|
+
converting activations into “patches” (tokens). Pool length/stride control the
|
|
87
|
+
token rate and set the lower bound on temporal context within each token.
|
|
88
|
+
|
|
89
|
+
- **Spatial (how electrodes are processed).**
|
|
90
|
+
A single conv with kernel ``(n_chans x 1)`` spans the full montage to learn spatial
|
|
91
|
+
projections for each temporal feature map, collapsing the channel axis into a
|
|
92
|
+
virtual channel before tokenization. This mirrors the shallow spatial step in
|
|
93
|
+
:class:`ShallowFBCSPNet` (temporal filters → spatial projection → temporal condensation).
|
|
94
|
+
|
|
95
|
+
- **Spectral (how frequency content is captured).**
|
|
96
|
+
No explicit Fourier/wavelet stage is used. Spectral selectivity emerges implicitly
|
|
97
|
+
from the learned temporal kernels; pooling further smooths high-frequency noise.
|
|
98
|
+
The effective spectral resolution is thus governed by ``L_t`` and the pooling
|
|
99
|
+
configuration.
|
|
100
|
+
|
|
101
|
+
.. rubric:: Attention / Sequential Modules
|
|
102
|
+
|
|
103
|
+
- **Type.** Standard multi-head self-attention (MHA) with ``att_heads`` heads over the token sequence.
|
|
104
|
+
- **Shapes.** Input/Output: ``(B, S_tokens, D)``; attention operates along the ``S_tokens`` axis.
|
|
105
|
+
- **Role.** Re-weights and integrates evidence across pooled windows, capturing dependencies
|
|
106
|
+
longer than any single token while leaving channel relationships to the convolutional stem.
|
|
107
|
+
The design is intentionally *small*—attention refines rather than replaces convolutional feature extraction.
|
|
108
|
+
|
|
109
|
+
.. rubric:: Additional Mechanisms
|
|
110
|
+
|
|
111
|
+
- **Parallel with ShallowFBCSPNet.** Both begin with a learned temporal filter bank,
|
|
112
|
+
spatial projection across electrodes, and early temporal condensation.
|
|
113
|
+
:class:`ShallowFBCSPNet` then computes band-power (via squaring/log-variance), whereas
|
|
114
|
+
EEG-Conformer applies BN/ELU and **continues with attention** over tokens to
|
|
115
|
+
refine temporal context before classification.
|
|
116
|
+
|
|
117
|
+
- **Tokenization knob.** ``pool_time_length`` and especially ``pool_time_stride`` set
|
|
118
|
+
the number of tokens ``S_tokens``. Smaller strides → more tokens and higher attention
|
|
119
|
+
capacity (but higher compute); larger strides → fewer tokens and stronger inductive bias.
|
|
120
|
+
|
|
121
|
+
- **Embedding dimension = filters.** ``n_filters_time`` serves double duty as both the
|
|
122
|
+
number of temporal filters in the stem and the transformer’s embedding size ``D``,
|
|
123
|
+
simplifying dimensional alignment.
|
|
124
|
+
|
|
125
|
+
.. rubric:: Usage and Configuration
|
|
126
|
+
|
|
127
|
+
- **Instantiation.** Choose ``n_filters_time`` (embedding size ``D``) and
|
|
128
|
+
``filter_time_length`` to match the rhythms of interest. Tune
|
|
129
|
+
``pool_time_length/stride`` to trade temporal resolution for sequence length.
|
|
130
|
+
Keep ``att_depth`` modest (e.g., 4–6) and set ``att_heads`` to divide ``D``.
|
|
131
|
+
``final_fc_length="auto"`` infers the flattened size from PatchEmbedding.
|
|
36
132
|
|
|
37
133
|
Notes
|
|
38
134
|
-----
|
|
39
135
|
The authors recommend using data augmentation before using Conformer,
|
|
40
136
|
e.g. segmentation and recombination,
|
|
41
|
-
Please refer to the original paper and code for more details.
|
|
137
|
+
Please refer to the original paper and code for more details [ConformerCode]_.
|
|
42
138
|
|
|
43
139
|
The model was initially tuned on 4 seconds of 250 Hz data.
|
|
44
140
|
Please adjust the scale of the temporal convolutional layer,
|
|
@@ -47,7 +143,10 @@ class EEGConformer(EEGModuleMixin, nn.Module):
|
|
|
47
143
|
.. versionadded:: 0.8
|
|
48
144
|
|
|
49
145
|
We aggregate the parameters based on the parts of the models, or
|
|
50
|
-
when the parameters were used first, e.g. n_filters_time
|
|
146
|
+
when the parameters were used first, e.g. ``n_filters_time``.
|
|
147
|
+
|
|
148
|
+
.. versionadded:: 1.1
|
|
149
|
+
|
|
51
150
|
|
|
52
151
|
Parameters
|
|
53
152
|
----------
|
braindecode/models/eegnet.py
CHANGED
|
@@ -20,13 +20,59 @@ from braindecode.modules import (
|
|
|
20
20
|
|
|
21
21
|
|
|
22
22
|
class EEGNetv4(EEGModuleMixin, nn.Sequential):
|
|
23
|
-
"""EEGNet v4 model from Lawhern et al. (2018) [
|
|
23
|
+
"""EEGNet v4 model from Lawhern et al. (2018) [Lawhern2018]_.
|
|
24
|
+
|
|
25
|
+
:bdg-success:`Convolution` :bdg-secondary:`Depthwise–Separable`
|
|
24
26
|
|
|
25
27
|
.. figure:: https://content.cld.iop.org/journals/1741-2552/15/5/056013/revision2/jneaace8cf01_hr.jpg
|
|
26
28
|
:align: center
|
|
27
|
-
:alt:
|
|
29
|
+
:alt: EEGNetv4 Architecture
|
|
30
|
+
:width: 600px
|
|
31
|
+
|
|
32
|
+
.. rubric:: Architectural Overview
|
|
33
|
+
|
|
34
|
+
EEGNetv4 is a compact convolutional network designed for EEG decoding with a pipeline that mirrors classical EEG processing:
|
|
35
|
+
- (i) learn temporal frequency-selective filters,
|
|
36
|
+
- (ii) learn spatial filters for those frequencies, and
|
|
37
|
+
- (iii) condense features with depthwise–separable convolutions before a lightweight classifier.
|
|
38
|
+
|
|
39
|
+
The architecture is deliberately small (temporal convolutional and spatial patterns) [Lawhern2018]_.
|
|
40
|
+
|
|
41
|
+
.. rubric:: Macro Components
|
|
42
|
+
|
|
43
|
+
- **Temporal convolution**
|
|
44
|
+
Temporal convolution applied per channel; learns ``F1`` kernels that act as data-driven band-pass filters.
|
|
45
|
+
- **Depthwise Spatial Filtering.**
|
|
46
|
+
Depthwise convolution spanning the channel dimension with ``groups = F1``,
|
|
47
|
+
yielding ``D`` spatial filters for each temporal filter (no cross-filter mixing).
|
|
48
|
+
- **Norm–Nonlinearity–Pooling (+ dropout).**
|
|
49
|
+
Batch normalization → ELU → temporal pooling, with dropout.
|
|
50
|
+
- **Depthwise–Separable Convolution Block.**
|
|
51
|
+
(a) depthwise temporal conv to refine temporal structure;
|
|
52
|
+
(b) pointwise 1x1 conv to mix feature maps into ``F2`` combinations.
|
|
53
|
+
- **Classifier Head.**
|
|
54
|
+
Lightweight 1x1 conv or dense layer (often with max-norm constraint).
|
|
28
55
|
|
|
29
|
-
|
|
56
|
+
.. rubric:: Convolutional Details
|
|
57
|
+
|
|
58
|
+
- **Temporal.** The initial temporal convs serve as a *learned filter bank*:
|
|
59
|
+
long 1-D kernels (implemented as 2-D with singleton spatial extent) emphasize oscillatory bands and transients.
|
|
60
|
+
Because this stage is linear prior to BN/ELU, kernels can be analyzed as FIR filters to reveal each feature’s spectrum [Lawhern2018]_.
|
|
61
|
+
|
|
62
|
+
- **Spatial.** The depthwise spatial conv spans the full channel axis (kernel height = #electrodes; temporal size = 1).
|
|
63
|
+
With ``groups = F1``, each temporal filter learns its own set of ``D`` spatial projections—akin to CSP, learned end-to-end and
|
|
64
|
+
typically regularized with max-norm.
|
|
65
|
+
|
|
66
|
+
- **Spectral.** No explicit Fourier/wavelet transform is used. Frequency structure
|
|
67
|
+
is captured implicitly by the temporal filter bank; later depthwise temporal kernels act as short-time integrators/refiners.
|
|
68
|
+
|
|
69
|
+
.. rubric:: Additional Comments
|
|
70
|
+
|
|
71
|
+
- **Filter-bank structure:** Parallel temporal kernels (``F1``) emulate classical filter banks; pairing them with frequency-specific spatial filters
|
|
72
|
+
yields features mappable to rhythms and topographies.
|
|
73
|
+
- **Depthwise & separable convs:** Parameter-efficient decomposition (depthwise + pointwise) retains power while limiting overfitting
|
|
74
|
+
[Chollet2017]_ and keeps temporal vs. mixing steps interpretable.
|
|
75
|
+
- **Regularization:** Batch norm, dropout, pooling, and optional max-norm on spatial kernels aid stability on small EEG datasets.
|
|
30
76
|
|
|
31
77
|
Parameters
|
|
32
78
|
----------
|
|
@@ -68,10 +114,13 @@ class EEGNetv4(EEGModuleMixin, nn.Sequential):
|
|
|
68
114
|
|
|
69
115
|
References
|
|
70
116
|
----------
|
|
71
|
-
.. [
|
|
117
|
+
.. [Lawhern2018] Lawhern, V. J., Solon, A. J., Waytowich, N. R., Gordon, S. M.,
|
|
72
118
|
Hung, C. P., & Lance, B. J. (2018). EEGNet: a compact convolutional
|
|
73
119
|
neural network for EEG-based brain–computer interfaces. Journal of
|
|
74
120
|
neural engineering, 15(5), 056013.
|
|
121
|
+
.. [Chollet2017] Chollet, F., *Xception: Deep Learning with Depthwise Separable
|
|
122
|
+
Convolutions*, CVPR, 2017.
|
|
123
|
+
|
|
75
124
|
"""
|
|
76
125
|
|
|
77
126
|
def __init__(
|
braindecode/models/eegnex.py
CHANGED
|
@@ -16,9 +16,124 @@ from braindecode.modules import Conv2dWithConstraint, LinearWithConstraint
|
|
|
16
16
|
class EEGNeX(EEGModuleMixin, nn.Module):
|
|
17
17
|
"""EEGNeX model from Chen et al. (2024) [eegnex]_.
|
|
18
18
|
|
|
19
|
+
:bdg-success:`Convolution`
|
|
20
|
+
|
|
19
21
|
.. figure:: https://braindecode.org/dev/_static/model/eegnex.jpg
|
|
20
22
|
:align: center
|
|
21
23
|
:alt: EEGNeX Architecture
|
|
24
|
+
:width: 620px
|
|
25
|
+
|
|
26
|
+
.. rubric:: Architectural Overview
|
|
27
|
+
|
|
28
|
+
EEGNeX is a **purely convolutional** architecture that refines the EEGNet-style stem
|
|
29
|
+
and deepens the temporal stack with **dilated temporal convolutions**. The end-to-end
|
|
30
|
+
flow is:
|
|
31
|
+
|
|
32
|
+
- (i) **Block-1/2**: two temporal convolutions ``(1 x L)`` with BN refine a
|
|
33
|
+
learned FIR-like *temporal filter bank* (no pooling yet);
|
|
34
|
+
- (ii) **Block-3**: depthwise **spatial** convolution across electrodes
|
|
35
|
+
``(n_chans x 1)`` with max-norm constraint, followed by ELU → AvgPool (time) → Dropout;
|
|
36
|
+
- (iii) **Block-4/5**: two additional **temporal** convolutions with increasing **dilation**
|
|
37
|
+
to expand the receptive field; the last block applies ELU → AvgPool → Dropout → Flatten;
|
|
38
|
+
- (iv) **Classifier**: a max-norm–constrained linear layer.
|
|
39
|
+
|
|
40
|
+
The published work positions EEGNeX as a compact, conv-only alternative that consistently
|
|
41
|
+
outperforms prior baselines across MOABB-style benchmarks, with the popular
|
|
42
|
+
“EEGNeX-8,32” shorthand denoting *8 temporal filters* and *kernel length 32*.
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
.. rubric:: Macro Components
|
|
46
|
+
|
|
47
|
+
- **Block-1 / Block-2 — Temporal filter (learned).**
|
|
48
|
+
|
|
49
|
+
- *Operations.*
|
|
50
|
+
- :class:`torch.nn.Conv2d` with kernels ``(1, L)``
|
|
51
|
+
- :class:`torch.nn.BatchNorm2d` (no nonlinearity until Block-3, mirroring a linear FIR analysis stage).
|
|
52
|
+
These layers set up frequency-selective detectors before spatial mixing.
|
|
53
|
+
|
|
54
|
+
- *Interpretability.* Kernels can be inspected as FIR filters; two stacked temporal
|
|
55
|
+
convs allow longer effective kernels without parameter blow-up.
|
|
56
|
+
|
|
57
|
+
- **Block-3 — Spatial projection + condensation.**
|
|
58
|
+
|
|
59
|
+
- *Operations.*
|
|
60
|
+
- :class:`braindecode.modules.Conv2dWithConstraint` with kernel``(n_chans, 1)``
|
|
61
|
+
and ``groups = filter_2`` (depthwise across filters)
|
|
62
|
+
- :class:`torch.nn.BatchNorm2d`
|
|
63
|
+
- :class:`torch.nn.ELU`
|
|
64
|
+
- :class:`torch.nn.AvgPool2d` (time)
|
|
65
|
+
- :class:`torch.nn.Dropout`.
|
|
66
|
+
|
|
67
|
+
*Role.* Learns per-filter spatial patterns over the **full montage** while temporal
|
|
68
|
+
pooling stabilizes and compresses features; max-norm encourages well-behaved spatial
|
|
69
|
+
weights similar to EEGNet practice.
|
|
70
|
+
|
|
71
|
+
- **Block-4 / Block-5 — Dilated temporal integration.**
|
|
72
|
+
|
|
73
|
+
- *Operations.*
|
|
74
|
+
- :class:`torch.nn.Conv2d` with kernels ``(1, k)`` and **dilations**
|
|
75
|
+
(e.g., 2 then 4);
|
|
76
|
+
- :class:`torch.nn.BatchNorm2d`
|
|
77
|
+
- :class:`torch.nn.ELU`
|
|
78
|
+
- :class:`torch.nn.AvgPool2d` (time)
|
|
79
|
+
- :class:`torch.nn.Dropout`
|
|
80
|
+
- :class:`torch.nn.Flatten`.
|
|
81
|
+
|
|
82
|
+
*Role.* Expands the temporal receptive field efficiently to capture rhythms and
|
|
83
|
+
long-range context after condensation.
|
|
84
|
+
|
|
85
|
+
- **Final Classifier — Max-norm linear.**
|
|
86
|
+
|
|
87
|
+
- *Operations.*
|
|
88
|
+
- :class:`braindecode.modules.LinearWithConstraint` maps the flattened
|
|
89
|
+
vector to the target classes; the max-norm constraint regularizes the readout.
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
.. rubric:: Convolutional Details
|
|
93
|
+
|
|
94
|
+
- **Temporal (where time-domain patterns are learned).**
|
|
95
|
+
Blocks 1-2 learn the primary filter bank (oscillations/transients), while Blocks 4-5
|
|
96
|
+
use **dilation** to integrate over longer horizons without extra pooling. The final
|
|
97
|
+
AvgPool in Block-5 sets the output token rate and helps noise suppression.
|
|
98
|
+
|
|
99
|
+
- **Spatial (how electrodes are processed).**
|
|
100
|
+
A *single* depthwise spatial conv (Block-3) spans the entire electrode set
|
|
101
|
+
(kernel ``(n_chans, 1)``), producing per-temporal-filter topographies; no cross-filter
|
|
102
|
+
mixing occurs at this stage, aiding interpretability.
|
|
103
|
+
|
|
104
|
+
- **Spectral (how frequency content is captured).**
|
|
105
|
+
Frequency selectivity emerges from the learned temporal kernels; dilation broadens effective
|
|
106
|
+
bandwidth coverage by composing multiple scales.
|
|
107
|
+
|
|
108
|
+
.. rubric:: Additional Mechanisms
|
|
109
|
+
|
|
110
|
+
- **EEGNeX-8,32 naming.** “8,32” indicates *8 temporal filters* and *kernel length 32*,
|
|
111
|
+
reflecting the paper's ablation path from EEGNet-8,2 toward thicker temporal kernels
|
|
112
|
+
and a deeper conv stack.
|
|
113
|
+
- **Max-norm constraints.** Spatial (Block-3) and final linear layers use max-norm
|
|
114
|
+
regularization—standard in EEG CNNs—to reduce overfitting and encourage stable spatial
|
|
115
|
+
patterns.
|
|
116
|
+
|
|
117
|
+
.. rubric:: Usage and Configuration
|
|
118
|
+
|
|
119
|
+
- **Kernel schedule.** Start with the canonical **EEGNeX-8,32** (``filter_1=8``,
|
|
120
|
+
``kernel_block_1_2=32``) and keep **Block-3** depth multiplier modest (e.g., 2) to match
|
|
121
|
+
the paper's “pure conv” profile.
|
|
122
|
+
- **Pooling vs. dilation.** Use pooling in Blocks 3 and 5 to control compute and variance;
|
|
123
|
+
increase dilations (Blocks 4-5) to widen temporal context when windows are short.
|
|
124
|
+
- **Regularization.** Combine dropout (Blocks 3 & 5) with max-norm on spatial and
|
|
125
|
+
classifier layers; prefer ELU activations for stable training on small EEG datasets.
|
|
126
|
+
|
|
127
|
+
|
|
128
|
+
Notes
|
|
129
|
+
-----
|
|
130
|
+
- The braindecode implementation follows the paper's conv-only design with five blocks
|
|
131
|
+
and reproduces the depthwise spatial step and dilated temporal stack. See the class
|
|
132
|
+
reference for exact kernel sizes, dilations, and pooling defaults. You can check the
|
|
133
|
+
original implementation at [EEGNexCode]_.
|
|
134
|
+
|
|
135
|
+
.. versionadded:: 1.1
|
|
136
|
+
|
|
22
137
|
|
|
23
138
|
Parameters
|
|
24
139
|
----------
|
|
@@ -45,12 +160,6 @@ class EEGNeX(EEGModuleMixin, nn.Module):
|
|
|
45
160
|
avg_pool_block5 : tuple[int, int], optional
|
|
46
161
|
Pooling size for block 5. Default is (1, 8).
|
|
47
162
|
|
|
48
|
-
Notes
|
|
49
|
-
-----
|
|
50
|
-
This implementation is not guaranteed to be correct, has not been checked
|
|
51
|
-
by original authors, only reimplemented from the paper description and
|
|
52
|
-
source code in tensorflow [EEGNexCode]_.
|
|
53
|
-
|
|
54
163
|
References
|
|
55
164
|
----------
|
|
56
165
|
.. [eegnex] Chen, X., Teng, X., Chen, H., Pan, Y., & Geyer, P. (2024).
|
braindecode/models/summary.csv
CHANGED
|
@@ -1,41 +1,40 @@
|
|
|
1
|
-
Model,Paradigm,Type,Freq(Hz),Hyperparameters,#Parameters,get_#Parameters
|
|
2
|
-
ATCNet,General,Classification,250,"n_chans, n_outputs, n_times",113732,"ATCNet(n_chans=22, n_outputs=4, n_times=1000)"
|
|
3
|
-
AttentionBaseNet,Motor Imagery,Classification,250,"n_chans, n_outputs, n_times",3692,"AttentionBaseNet(n_chans=22, n_outputs=4, n_times=1000)"
|
|
4
|
-
BDTCN,Normal
|
|
5
|
-
BIOT,"Sleep Staging, Epilepsy",Classification,200,"n_chans, n_outputs",3183879,"BIOT(n_chans=2, n_outputs=5, n_times=6000)"
|
|
6
|
-
ContraWR,Sleep Staging,"Classification, Embedding",125,"n_chans, n_outputs, sfreq",1160165,"ContraWR(n_chans=2, n_outputs=5, n_times=3750, emb_size=256, sfreq=125)"
|
|
7
|
-
CTNet,Motor Imagery,Classification,250,"n_chans, n_outputs, n_times",26900,"CTNet(n_chans=22, n_outputs=4, n_times=1000, n_filters_time=8, kernel_size=16, heads=2, emb_size=16)"
|
|
8
|
-
Deep4Net,Motor Imagery,Classification,250,"n_chans, n_outputs, n_times",282879,"Deep4Net(n_chans=22, n_outputs=4, n_times=1000)"
|
|
9
|
-
DeepSleepNet,Sleep Staging,Classification,256,"n_chans, n_outputs",24744837,"DeepSleepNet(n_chans=1, n_outputs=5, n_times=7680, sfreq=256)"
|
|
10
|
-
EEGConformer,General,Classification,250,"n_chans, n_outputs, n_times",789572,"EEGConformer(n_chans=22, n_outputs=4, n_times=1000)
|
|
11
|
-
EEGInceptionERP,"ERP, SSVEP",Classification,128,"n_chans, n_outputs",14926,"EEGInceptionERP(n_chans=8, n_outputs=2, n_times=128, sfreq=128)"
|
|
12
|
-
EEGInceptionMI,Motor Imagery,Classification,250,"n_chans, n_outputs, n_times",558028,"EEGInceptionMI(n_chans=22, n_outputs=4, n_times=1000, n_convs=5, n_filters=12)"
|
|
13
|
-
EEGITNet,Motor Imagery,Classification,125,"n_chans, n_outputs, n_times",5212,"EEGITNet(n_chans=22, n_outputs=4, n_times=500)"
|
|
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
|
-
IFNet,Motor Imagery,Classification,250,"n_chans, n_outputs, n_times, sfreq",9860,"IFNet(n_chans=22, n_outputs=4, n_times=1000, sfreq=250)"
|
|
1
|
+
Model,Paradigm,Type,Freq(Hz),Hyperparameters,#Parameters,get_#Parameters,Categorization
|
|
2
|
+
ATCNet,General,Classification,250,"n_chans, n_outputs, n_times",113732,"ATCNet(n_chans=22, n_outputs=4, n_times=1000)","Convolution,Recurrent,Small Attention"
|
|
3
|
+
AttentionBaseNet,Motor Imagery,Classification,250,"n_chans, n_outputs, n_times",3692,"AttentionBaseNet(n_chans=22, n_outputs=4, n_times=1000)","Convolution,Small Attention"
|
|
4
|
+
BDTCN,Normal Abnormal,Classification,100,"n_chans, n_outputs, n_times",456502,"BDTCN(n_chans=21, n_outputs=2, n_times=6000, n_blocks=5, n_filters=55, kernel_size=16)","Convolution,Recurrent"
|
|
5
|
+
BIOT,"Sleep Staging, Epilepsy",Classification,200,"n_chans, n_outputs",3183879,"BIOT(n_chans=2, n_outputs=5, n_times=6000)","Large Language Model"
|
|
6
|
+
ContraWR,Sleep Staging,"Classification, Embedding",125,"n_chans, n_outputs, sfreq",1160165,"ContraWR(n_chans=2, n_outputs=5, n_times=3750, emb_size=256, sfreq=125)",Convolution
|
|
7
|
+
CTNet,Motor Imagery,Classification,250,"n_chans, n_outputs, n_times",26900,"CTNet(n_chans=22, n_outputs=4, n_times=1000, n_filters_time=8, kernel_size=16, heads=2, emb_size=16)","Convolution,Small Attention"
|
|
8
|
+
Deep4Net,Motor Imagery,Classification,250,"n_chans, n_outputs, n_times",282879,"Deep4Net(n_chans=22, n_outputs=4, n_times=1000)","Convolution"
|
|
9
|
+
DeepSleepNet,Sleep Staging,Classification,256,"n_chans, n_outputs",24744837,"DeepSleepNet(n_chans=1, n_outputs=5, n_times=7680, sfreq=256)","Convolution"
|
|
10
|
+
EEGConformer,General,Classification,250,"n_chans, n_outputs, n_times",789572,"EEGConformer(n_chans=22, n_outputs=4, n_times=1000)","Convolution,Small Attention"
|
|
11
|
+
EEGInceptionERP,"ERP, SSVEP",Classification,128,"n_chans, n_outputs",14926,"EEGInceptionERP(n_chans=8, n_outputs=2, n_times=128, sfreq=128)","Convolution"
|
|
12
|
+
EEGInceptionMI,Motor Imagery,Classification,250,"n_chans, n_outputs, n_times",558028,"EEGInceptionMI(n_chans=22, n_outputs=4, n_times=1000, n_convs=5, n_filters=12)","Convolution"
|
|
13
|
+
EEGITNet,Motor Imagery,Classification,125,"n_chans, n_outputs, n_times",5212,"EEGITNet(n_chans=22, n_outputs=4, n_times=500)","Convolution,Recurrent"
|
|
14
|
+
EEGNetv4,General,Classification,128,"n_chans, n_outputs, n_times",2484,"EEGNetv4(n_chans=22, n_outputs=4, n_times=512)","Convolution"
|
|
15
|
+
EEGNeX,Motor Imagery,Classification,125,"n_chans, n_outputs, n_times",55940,"EEGNeX(n_chans=22, n_outputs=4, n_times=500)","Convolution"
|
|
16
|
+
EEGMiner,Emotion Recognition,Classification,128,"n_chans, n_outputs, n_times, sfreq",7572,"EEGMiner(n_chans=62, n_outputs=2, n_times=2560, sfreq=128)","Convolution"
|
|
17
|
+
EEGResNet,General,Classification,250,"n_chans, n_outputs, n_times",247484,"EEGResNet(n_chans=22, n_outputs=4, n_times=1000)","Interpretability"
|
|
18
|
+
EEGSimpleConv,Motor Imagery,Classification,80,"n_chans, n_outputs, sfreq",730404,"EEGSimpleConv(n_chans=22, n_outputs=4, n_times=320, sfreq=80)","Convolution"
|
|
19
|
+
EEGTCNet,Motor Imagery,Classification,250,"n_chans, n_outputs",4516,"EEGTCNet(n_chans=22, n_outputs=4, n_times=1000, kern_length=32)","Convolution,Recurrent"
|
|
20
|
+
Labram,General,"Classification, Embedding",200,"n_chans, n_outputs, n_times",5866180,"Labram(n_chans=22, n_outputs=4, n_times=1000, sfreq=250)","Convolution,Large Language Model"
|
|
21
|
+
MSVTNet,Motor Imagery,Classification,250,"n_chans, n_outputs, n_times",75494," MSVTNet(n_chans=22, n_outputs=4, n_times=1000)","Convolution,Recurrent,Small Attention"
|
|
22
|
+
SCCNet,Motor Imagery,Classification,125,"n_chans, n_outputs, n_times, sfreq",12070,"SCCNet(n_chans=22, n_outputs=4, n_times=1000, sfreq=125)","Convolution"
|
|
23
|
+
SignalJEPA,"Motor Imagery, ERP, SSVEP",Embedding,128,"n_times, chs_info",3456882,"SignalJEPA(n_times=512, chs_info=Lee2019_MI().get_data(subjects=[1])[1]['0']['1train'].info[""chs""][:62])","Convolution,Channel,Large Language Model"
|
|
24
|
+
SignalJEPA_Contextual,"Motor Imagery, ERP, SSVEP",Classification,128,"n_outputs, n_times, chs_info",3459184,"SignalJEPA_Contextual(n_outputs=2, input_window_seconds=4.19, sfreq=128, chs_info=Lee2019_MI().get_data(subjects=[1])[1]['0']['1train'].info[""chs""][:62])","Convolution,Channel,Large Language Model"
|
|
25
|
+
SignalJEPA_PostLocal,"Motor Imagery, ERP, SSVEP",Classification,128,"n_chans, n_outputs, n_times",16142,"SignalJEPA_PostLocal(n_chans=62, n_outputs=2, input_window_seconds=4.19, sfreq=128)","Convolution,Channel,Large Language Model"
|
|
26
|
+
SignalJEPA_PreLocal,"Motor Imagery, ERP, SSVEP",Classification,128,"n_outputs, n_times, chs_info",16142,"SignalJEPA_PreLocal(n_chans=62, n_outputs=2, input_window_seconds=4.19, sfreq=128)","Convolution,Channel,Large Language Model"
|
|
27
|
+
SincShallowNet,Motor Imagery,Classification,250,"n_chans, n_outputs, n_times, sfreq",21892,"SincShallowNet(n_chans=22, n_outputs=4, n_times=1000, sfreq=250)","Convolution,Interpretability"
|
|
28
|
+
ShallowFBCSPNet,Motor Imagery,Classification,250,"n_chans, n_outputs, n_times",46084,"ShallowFBCSPNet(n_chans=22, n_outputs=4, n_times=1000, sfreq=250)","Convolution"
|
|
29
|
+
SleepStagerBlanco2020,Sleep Staging,Classification,100,"n_chans, n_outputs, n_times",2845,"SleepStagerBlanco2020(n_chans=2, n_outputs=5, n_times=3000, sfreq=100)","Convolution"
|
|
30
|
+
SleepStagerChambon2018,Sleep Staging,Classification,128,"n_chans, n_outputs, n_times, sfreq",5835,"SleepStagerChambon2018(n_chans=2, n_outputs=5, n_times=3840, sfreq=128)","Convolution"
|
|
31
|
+
SleepStagerEldele2021,Sleep Staging,Classification,100,"n_chans, n_outputs, n_times, sfreq",719925,"SleepStagerEldele2021(n_chans=2, n_outputs=5, n_times=3000, sfreq=100)","Convolution, Small Attention"
|
|
32
|
+
SPARCNet,Epilepsy,Classification,200,"n_chans, n_outputs, n_times",1141921,"SPARCNet(n_chans=16, n_outputs=6, n_times=2000, sfreq=200)","Convolution"
|
|
33
|
+
SyncNet,"Emotion Recognition, Alcoholism",Classification,256,"n_chans, n_outputs, n_times",554,"SyncNet(n_chans=62, n_outputs=3, n_times=5120, sfreq=256)","Interpretability"
|
|
34
|
+
TSceptionV1,Emotion Recognition,Classification,256,"n_chans, n_outputs, n_times, sfreq",2187206,"TSceptionV1(n_chans=62, n_outputs=3, n_times=5120, sfreq=256)","Convolution"
|
|
35
|
+
TIDNet,General,Classification,250,"n_chans, n_outputs, n_times",240404,"TIDNet(n_chans=22, n_outputs=4, n_times=1000)","Convolution"
|
|
36
|
+
USleep,Sleep Staging,Classification,128,"n_chans, n_outputs, n_times, sfreq",2482011,"USleep(n_chans=2, n_outputs=5, n_times=3000, sfreq=100)","Convolution"
|
|
37
|
+
FBCNet,Motor Imagery,Classification,250,"n_chans, n_outputs, n_times, sfreq",11812,"FCNet(n_chans=22, n_outputs=4, n_times=1000, sfreq=250)","Convolution,FilterBank"
|
|
38
|
+
FBMSNet,Motor Imagery,Classification,250,"n_chans, n_outputs, n_times, sfreq",16231,"FBMSNet(n_chans=22, n_outputs=4, n_times=1000, sfreq=250)","Convolution,FilterBank"
|
|
39
|
+
FBLightConvNet,Motor Imagery,Classification,250,"n_chans, n_outputs, n_times, sfreq",6596,"FBLightConvNet(n_chans=22, n_outputs=4, n_times=1000, sfreq=250)","Convolution,FilterBank"
|
|
40
|
+
IFNet,Motor Imagery,Classification,250,"n_chans, n_outputs, n_times, sfreq",9860,"IFNet(n_chans=22, n_outputs=4, n_times=1000, sfreq=250)","Convolution,FilterBank"
|
braindecode/models/util.py
CHANGED
|
@@ -55,7 +55,6 @@ models_mandatory_parameters = [
|
|
|
55
55
|
("EEGInceptionERP", ["n_chans", "n_outputs", "n_times", "sfreq"], None),
|
|
56
56
|
("EEGInceptionMI", ["n_chans", "n_outputs", "n_times", "sfreq"], None),
|
|
57
57
|
("EEGITNet", ["n_chans", "n_outputs", "n_times"], None),
|
|
58
|
-
("EEGNetv1", ["n_chans", "n_outputs", "n_times"], None),
|
|
59
58
|
("EEGNetv4", ["n_chans", "n_outputs", "n_times"], None),
|
|
60
59
|
("EEGResNet", ["n_chans", "n_outputs", "n_times"], None),
|
|
61
60
|
("ShallowFBCSPNet", ["n_chans", "n_outputs", "n_times"], None),
|
braindecode/modules/attention.py
CHANGED
|
@@ -157,7 +157,8 @@ class FCA(nn.Module):
|
|
|
157
157
|
):
|
|
158
158
|
super(FCA, self).__init__()
|
|
159
159
|
mapper_y = [freq_idx]
|
|
160
|
-
|
|
160
|
+
if in_channels % len(mapper_y) != 0:
|
|
161
|
+
raise ValueError("in_channels must be divisible by number of DCT filters")
|
|
161
162
|
|
|
162
163
|
self.weight = nn.Parameter(
|
|
163
164
|
self.get_dct_filter(seq_len, mapper_y, in_channels), requires_grad=False
|
|
@@ -295,7 +296,8 @@ class ECA(nn.Module):
|
|
|
295
296
|
def __init__(self, in_channels: int, kernel_size: int):
|
|
296
297
|
super(ECA, self).__init__()
|
|
297
298
|
self.gap = nn.AdaptiveAvgPool2d(1)
|
|
298
|
-
|
|
299
|
+
if kernel_size % 2 != 1:
|
|
300
|
+
raise ValueError("kernel size must be odd for same padding")
|
|
299
301
|
self.conv = nn.Conv1d(
|
|
300
302
|
1, 1, kernel_size=kernel_size, padding=kernel_size // 2, bias=False
|
|
301
303
|
)
|
|
@@ -530,7 +532,8 @@ class CBAM(nn.Module):
|
|
|
530
532
|
nn.ReLU(),
|
|
531
533
|
nn.Conv2d(in_channels // reduction_rate, in_channels, 1, bias=False),
|
|
532
534
|
)
|
|
533
|
-
|
|
535
|
+
if kernel_size % 2 != 1:
|
|
536
|
+
raise ValueError("kernel size must be odd for same padding")
|
|
534
537
|
self.conv = nn.Conv2d(2, 1, (1, kernel_size), padding=(0, kernel_size // 2))
|
|
535
538
|
|
|
536
539
|
def forward(self, x):
|
|
@@ -136,7 +136,8 @@ class CombinedConv(nn.Module):
|
|
|
136
136
|
# Calculate bias terms
|
|
137
137
|
if self.bias_time:
|
|
138
138
|
time_bias = self.conv_time.bias
|
|
139
|
-
|
|
139
|
+
if time_bias is None:
|
|
140
|
+
raise RuntimeError("conv_time.bias is None despite bias_time=True")
|
|
140
141
|
calculated_bias = (
|
|
141
142
|
self.conv_spat.weight.squeeze()
|
|
142
143
|
.sum(-1)
|
|
@@ -145,7 +146,8 @@ class CombinedConv(nn.Module):
|
|
|
145
146
|
)
|
|
146
147
|
if self.bias_spat:
|
|
147
148
|
spat_bias = self.conv_spat.bias
|
|
148
|
-
|
|
149
|
+
if spat_bias is None:
|
|
150
|
+
raise RuntimeError("conv_spat.bias is None despite bias_spat=True")
|
|
149
151
|
if calculated_bias is None:
|
|
150
152
|
calculated_bias = spat_bias
|
|
151
153
|
else:
|
|
@@ -190,11 +192,12 @@ class CausalConv1d(nn.Conv1d):
|
|
|
190
192
|
dilation=1,
|
|
191
193
|
**kwargs,
|
|
192
194
|
):
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
195
|
+
if "padding" in kwargs:
|
|
196
|
+
raise ValueError(
|
|
197
|
+
"The padding parameter is controlled internally by "
|
|
198
|
+
f"{type(self).__name__} class. You should not try to override this"
|
|
199
|
+
" parameter."
|
|
200
|
+
)
|
|
198
201
|
|
|
199
202
|
super().__init__(
|
|
200
203
|
in_channels=in_channels,
|
braindecode/modules/filter.py
CHANGED
|
@@ -452,12 +452,14 @@ class GeneralizedGaussianFilter(nn.Module):
|
|
|
452
452
|
self.inverse_fourier = inverse_fourier
|
|
453
453
|
self.affine_group_delay = affine_group_delay
|
|
454
454
|
self.clamp_f_mean = clamp_f_mean
|
|
455
|
-
|
|
456
|
-
"out_channels has to be multiple of in_channels"
|
|
457
|
-
)
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
455
|
+
if out_channels % in_channels != 0:
|
|
456
|
+
raise ValueError("out_channels has to be multiple of in_channels")
|
|
457
|
+
if len(f_mean) * in_channels != out_channels:
|
|
458
|
+
raise ValueError("len(f_mean) * in_channels must equal out_channels")
|
|
459
|
+
if len(bandwidth) * in_channels != out_channels:
|
|
460
|
+
raise ValueError("len(bandwidth) * in_channels must equal out_channels")
|
|
461
|
+
if len(shape) * in_channels != out_channels:
|
|
462
|
+
raise ValueError("len(shape) * in_channels must equal out_channels")
|
|
461
463
|
|
|
462
464
|
# Range from 0 to half sample rate, normalized
|
|
463
465
|
self.n_range = nn.Parameter(
|
braindecode/training/scoring.py
CHANGED
|
@@ -11,7 +11,6 @@ from contextlib import contextmanager
|
|
|
11
11
|
|
|
12
12
|
import numpy as np
|
|
13
13
|
import torch
|
|
14
|
-
from mne.utils.check import check_version
|
|
15
14
|
from skorch.callbacks.scoring import EpochScoring
|
|
16
15
|
from skorch.dataset import unpack_data
|
|
17
16
|
from skorch.utils import to_numpy
|
|
@@ -370,13 +369,8 @@ class PostEpochTrainScoring(EpochScoring):
|
|
|
370
369
|
y_preds = []
|
|
371
370
|
y_test = []
|
|
372
371
|
for batch in iterator:
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
if not check_version("skorch", min_version="0.10.1"):
|
|
376
|
-
yp = net.evaluation_step(batch_X, training=False)
|
|
377
|
-
# X, y unpacking has been pushed downstream in skorch 0.10
|
|
378
|
-
else:
|
|
379
|
-
yp = net.evaluation_step(batch, training=False)
|
|
372
|
+
_, batch_y = unpack_data(batch)
|
|
373
|
+
yp = net.evaluation_step(batch, training=False)
|
|
380
374
|
yp = yp.to(device="cpu")
|
|
381
375
|
y_test.append(self.target_extractor(batch_y))
|
|
382
376
|
y_preds.append(yp)
|
braindecode/version.py
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
__version__ = "1.2.0.
|
|
1
|
+
__version__ = "1.2.0.dev176281713"
|
|
@@ -6,8 +6,13 @@ import numpy as np
|
|
|
6
6
|
import torch
|
|
7
7
|
from skorch.utils import to_numpy, to_tensor
|
|
8
8
|
|
|
9
|
+
from braindecode.util import set_random_seeds
|
|
9
10
|
|
|
10
|
-
|
|
11
|
+
|
|
12
|
+
def compute_amplitude_gradients(model, dataset, batch_size, seed=20240205):
|
|
13
|
+
"""Compute amplitude gradients after seeding for reproducibility."""
|
|
14
|
+
cuda = next(model.parameters()).is_cuda
|
|
15
|
+
set_random_seeds(seed=seed, cuda=cuda)
|
|
11
16
|
loader = torch.utils.data.DataLoader(
|
|
12
17
|
dataset, batch_size=batch_size, drop_last=False, shuffle=False
|
|
13
18
|
)
|