braindecode 1.2.0.dev182094932__py3-none-any.whl → 1.3.0.dev173691341__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 +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/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 +20 -26
- braindecode/version.py +1 -1
- {braindecode-1.2.0.dev182094932.dist-info → braindecode-1.3.0.dev173691341.dist-info}/METADATA +7 -2
- {braindecode-1.2.0.dev182094932.dist-info → braindecode-1.3.0.dev173691341.dist-info}/RECORD +33 -33
- braindecode/models/eegresnet.py +0 -362
- {braindecode-1.2.0.dev182094932.dist-info → braindecode-1.3.0.dev173691341.dist-info}/WHEEL +0 -0
- {braindecode-1.2.0.dev182094932.dist-info → braindecode-1.3.0.dev173691341.dist-info}/licenses/LICENSE.txt +0 -0
- {braindecode-1.2.0.dev182094932.dist-info → braindecode-1.3.0.dev173691341.dist-info}/licenses/NOTICE.txt +0 -0
- {braindecode-1.2.0.dev182094932.dist-info → braindecode-1.3.0.dev173691341.dist-info}/top_level.txt +0 -0
|
@@ -13,6 +13,8 @@ from braindecode.modules import Ensure4d
|
|
|
13
13
|
class EEGInceptionMI(EEGModuleMixin, nn.Module):
|
|
14
14
|
"""EEG Inception for Motor Imagery, as proposed in Zhang et al. (2021) [1]_
|
|
15
15
|
|
|
16
|
+
:bdg-success:`Convolution`
|
|
17
|
+
|
|
16
18
|
.. figure:: https://content.cld.iop.org/journals/1741-2552/18/4/046014/revision3/jneabed81f1_hr.jpg
|
|
17
19
|
:align: center
|
|
18
20
|
:alt: EEGInceptionMI Architecture
|
braindecode/models/eegnet.py
CHANGED
|
@@ -6,7 +6,7 @@ from __future__ import annotations
|
|
|
6
6
|
from typing import Dict, Optional
|
|
7
7
|
|
|
8
8
|
from einops.layers.torch import Rearrange
|
|
9
|
-
from mne.utils import warn
|
|
9
|
+
from mne.utils import deprecated, warn
|
|
10
10
|
from torch import nn
|
|
11
11
|
|
|
12
12
|
from braindecode.functional import glorot_weight_zero_bias
|
|
@@ -19,23 +19,22 @@ from braindecode.modules import (
|
|
|
19
19
|
)
|
|
20
20
|
|
|
21
21
|
|
|
22
|
-
class
|
|
23
|
-
"""EEGNet
|
|
22
|
+
class EEGNet(EEGModuleMixin, nn.Sequential):
|
|
23
|
+
"""EEGNet model from Lawhern et al. (2018) [Lawhern2018]_.
|
|
24
24
|
|
|
25
|
-
:bdg-success:`Convolution`
|
|
25
|
+
:bdg-success:`Convolution`
|
|
26
26
|
|
|
27
27
|
.. figure:: https://content.cld.iop.org/journals/1741-2552/15/5/056013/revision2/jneaace8cf01_hr.jpg
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
28
|
+
:align: center
|
|
29
|
+
:alt: EEGNet Architecture
|
|
30
|
+
:width: 600px
|
|
31
31
|
|
|
32
32
|
.. rubric:: Architectural Overview
|
|
33
33
|
|
|
34
|
-
|
|
35
|
-
pipeline that mirrors classical EEG processing:
|
|
34
|
+
EEGNet is a compact convolutional network designed for EEG decoding with a pipeline that mirrors classical EEG processing:
|
|
36
35
|
- (i) learn temporal frequency-selective filters,
|
|
37
36
|
- (ii) learn spatial filters for those frequencies, and
|
|
38
|
-
- (iii) condense features with depthwise
|
|
37
|
+
- (iii) condense features with depthwise-separable convolutions before a lightweight classifier.
|
|
39
38
|
|
|
40
39
|
The architecture is deliberately small (temporal convolutional and spatial patterns) [Lawhern2018]_.
|
|
41
40
|
|
|
@@ -46,9 +45,9 @@ class EEGNetv4(EEGModuleMixin, nn.Sequential):
|
|
|
46
45
|
- **Depthwise Spatial Filtering.**
|
|
47
46
|
Depthwise convolution spanning the channel dimension with ``groups = F1``,
|
|
48
47
|
yielding ``D`` spatial filters for each temporal filter (no cross-filter mixing).
|
|
49
|
-
- **Norm
|
|
48
|
+
- **Norm-Nonlinearity-Pooling (+ dropout).**
|
|
50
49
|
Batch normalization → ELU → temporal pooling, with dropout.
|
|
51
|
-
- **Depthwise
|
|
50
|
+
- **Depthwise-Separable Convolution Block.**
|
|
52
51
|
(a) depthwise temporal conv to refine temporal structure;
|
|
53
52
|
(b) pointwise 1x1 conv to mix feature maps into ``F2`` combinations.
|
|
54
53
|
- **Classifier Head.**
|
|
@@ -56,16 +55,16 @@ class EEGNetv4(EEGModuleMixin, nn.Sequential):
|
|
|
56
55
|
|
|
57
56
|
.. rubric:: Convolutional Details
|
|
58
57
|
|
|
59
|
-
**Temporal.** The initial temporal convs serve as a *learned filter bank*:
|
|
60
|
-
|
|
61
|
-
|
|
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]_.
|
|
62
61
|
|
|
63
|
-
**Spatial.** The depthwise spatial conv spans the full channel axis (kernel height = #electrodes; temporal size = 1).
|
|
64
|
-
|
|
65
|
-
|
|
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.
|
|
66
65
|
|
|
67
|
-
**Spectral.** No explicit Fourier/wavelet transform is used. Frequency structure
|
|
68
|
-
|
|
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.
|
|
69
68
|
|
|
70
69
|
.. rubric:: Additional Comments
|
|
71
70
|
|
|
@@ -74,6 +73,8 @@ class EEGNetv4(EEGModuleMixin, nn.Sequential):
|
|
|
74
73
|
- **Depthwise & separable convs:** Parameter-efficient decomposition (depthwise + pointwise) retains power while limiting overfitting
|
|
75
74
|
[Chollet2017]_ and keeps temporal vs. mixing steps interpretable.
|
|
76
75
|
- **Regularization:** Batch norm, dropout, pooling, and optional max-norm on spatial kernels aid stability on small EEG datasets.
|
|
76
|
+
- The v4 means the version 4 at the arxiv paper [Lawhern2018]_.
|
|
77
|
+
|
|
77
78
|
|
|
78
79
|
Parameters
|
|
79
80
|
----------
|
|
@@ -349,174 +350,10 @@ class EEGNetv4(EEGModuleMixin, nn.Sequential):
|
|
|
349
350
|
glorot_weight_zero_bias(self)
|
|
350
351
|
|
|
351
352
|
|
|
352
|
-
|
|
353
|
-
"
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
Parameters
|
|
358
|
-
----------
|
|
359
|
-
in_chans :
|
|
360
|
-
Alias for n_chans.
|
|
361
|
-
n_classes:
|
|
362
|
-
Alias for n_outputs.
|
|
363
|
-
input_window_samples :
|
|
364
|
-
Alias for n_times.
|
|
365
|
-
activation: nn.Module, default=nn.ELU
|
|
366
|
-
Activation function class to apply. Should be a PyTorch activation
|
|
367
|
-
module class like ``nn.ReLU`` or ``nn.ELU``. Default is ``nn.ELU``.
|
|
368
|
-
|
|
369
|
-
Notes
|
|
370
|
-
-----
|
|
371
|
-
This implementation is not guaranteed to be correct, has not been checked
|
|
372
|
-
by original authors, only reimplemented from the paper description.
|
|
373
|
-
|
|
374
|
-
References
|
|
375
|
-
----------
|
|
376
|
-
.. [EEGNet] Lawhern, V. J., Solon, A. J., Waytowich, N. R., Gordon,
|
|
377
|
-
S. M., Hung, C. P., & Lance, B. J. (2016).
|
|
378
|
-
EEGNet: A Compact Convolutional Network for EEG-based
|
|
379
|
-
Brain-Computer Interfaces.
|
|
380
|
-
arXiv preprint arXiv:1611.08024.
|
|
381
|
-
"""
|
|
382
|
-
|
|
383
|
-
def __init__(
|
|
384
|
-
self,
|
|
385
|
-
n_chans=None,
|
|
386
|
-
n_outputs=None,
|
|
387
|
-
n_times=None,
|
|
388
|
-
final_conv_length="auto",
|
|
389
|
-
pool_mode="max",
|
|
390
|
-
second_kernel_size=(2, 32),
|
|
391
|
-
third_kernel_size=(8, 4),
|
|
392
|
-
drop_prob=0.25,
|
|
393
|
-
activation: nn.Module = nn.ELU,
|
|
394
|
-
chs_info=None,
|
|
395
|
-
input_window_seconds=None,
|
|
396
|
-
sfreq=None,
|
|
397
|
-
):
|
|
398
|
-
super().__init__(
|
|
399
|
-
n_outputs=n_outputs,
|
|
400
|
-
n_chans=n_chans,
|
|
401
|
-
chs_info=chs_info,
|
|
402
|
-
n_times=n_times,
|
|
403
|
-
input_window_seconds=input_window_seconds,
|
|
404
|
-
sfreq=sfreq,
|
|
405
|
-
)
|
|
406
|
-
del n_outputs, n_chans, chs_info, n_times, input_window_seconds, sfreq
|
|
407
|
-
warn(
|
|
408
|
-
"The class EEGNetv1 is deprecated and will be removed in the "
|
|
409
|
-
"release 1.0 of braindecode. Please use "
|
|
410
|
-
"braindecode.models.EEGNetv4 instead in the future.",
|
|
411
|
-
DeprecationWarning,
|
|
412
|
-
)
|
|
413
|
-
if final_conv_length == "auto":
|
|
414
|
-
assert self.n_times is not None
|
|
415
|
-
self.final_conv_length = final_conv_length
|
|
416
|
-
self.pool_mode = pool_mode
|
|
417
|
-
self.second_kernel_size = second_kernel_size
|
|
418
|
-
self.third_kernel_size = third_kernel_size
|
|
419
|
-
self.drop_prob = drop_prob
|
|
420
|
-
# For the load_state_dict
|
|
421
|
-
# When padronize all layers,
|
|
422
|
-
# add the old's parameters here
|
|
423
|
-
self.mapping = {
|
|
424
|
-
"conv_classifier.weight": "final_layer.conv_classifier.weight",
|
|
425
|
-
"conv_classifier.bias": "final_layer.conv_classifier.bias",
|
|
426
|
-
}
|
|
427
|
-
|
|
428
|
-
pool_class = dict(max=nn.MaxPool2d, mean=nn.AvgPool2d)[self.pool_mode]
|
|
429
|
-
self.add_module("ensuredims", Ensure4d())
|
|
430
|
-
n_filters_1 = 16
|
|
431
|
-
self.add_module(
|
|
432
|
-
"conv_1",
|
|
433
|
-
nn.Conv2d(self.n_chans, n_filters_1, (1, 1), stride=1, bias=True),
|
|
434
|
-
)
|
|
435
|
-
self.add_module(
|
|
436
|
-
"bnorm_1",
|
|
437
|
-
nn.BatchNorm2d(n_filters_1, momentum=0.01, affine=True, eps=1e-3),
|
|
438
|
-
)
|
|
439
|
-
self.add_module("elu_1", activation())
|
|
440
|
-
# transpose to examples x 1 x (virtual, not EEG) channels x time
|
|
441
|
-
self.add_module("permute_1", Rearrange("batch x y z -> batch z x y"))
|
|
442
|
-
|
|
443
|
-
self.add_module("drop_1", nn.Dropout(p=self.drop_prob))
|
|
444
|
-
|
|
445
|
-
n_filters_2 = 4
|
|
446
|
-
# keras pads unequal padding more in front, so padding
|
|
447
|
-
# too large should be ok.
|
|
448
|
-
# Not padding in time so that cropped training makes sense
|
|
449
|
-
# https://stackoverflow.com/questions/43994604/padding-with-even-kernel-size-in-a-convolutional-layer-in-keras-theano
|
|
450
|
-
|
|
451
|
-
self.add_module(
|
|
452
|
-
"conv_2",
|
|
453
|
-
nn.Conv2d(
|
|
454
|
-
1,
|
|
455
|
-
n_filters_2,
|
|
456
|
-
self.second_kernel_size,
|
|
457
|
-
stride=1,
|
|
458
|
-
padding=(self.second_kernel_size[0] // 2, 0),
|
|
459
|
-
bias=True,
|
|
460
|
-
),
|
|
461
|
-
)
|
|
462
|
-
self.add_module(
|
|
463
|
-
"bnorm_2",
|
|
464
|
-
nn.BatchNorm2d(n_filters_2, momentum=0.01, affine=True, eps=1e-3),
|
|
465
|
-
)
|
|
466
|
-
self.add_module("elu_2", activation())
|
|
467
|
-
self.add_module("pool_2", pool_class(kernel_size=(2, 4), stride=(2, 4)))
|
|
468
|
-
self.add_module("drop_2", nn.Dropout(p=self.drop_prob))
|
|
469
|
-
|
|
470
|
-
n_filters_3 = 4
|
|
471
|
-
self.add_module(
|
|
472
|
-
"conv_3",
|
|
473
|
-
nn.Conv2d(
|
|
474
|
-
n_filters_2,
|
|
475
|
-
n_filters_3,
|
|
476
|
-
self.third_kernel_size,
|
|
477
|
-
stride=1,
|
|
478
|
-
padding=(self.third_kernel_size[0] // 2, 0),
|
|
479
|
-
bias=True,
|
|
480
|
-
),
|
|
481
|
-
)
|
|
482
|
-
self.add_module(
|
|
483
|
-
"bnorm_3",
|
|
484
|
-
nn.BatchNorm2d(n_filters_3, momentum=0.01, affine=True, eps=1e-3),
|
|
485
|
-
)
|
|
486
|
-
self.add_module("elu_3", activation())
|
|
487
|
-
self.add_module("pool_3", pool_class(kernel_size=(2, 4), stride=(2, 4)))
|
|
488
|
-
self.add_module("drop_3", nn.Dropout(p=self.drop_prob))
|
|
489
|
-
|
|
490
|
-
output_shape = self.get_output_shape()
|
|
491
|
-
n_out_virtual_chans = output_shape[2]
|
|
492
|
-
|
|
493
|
-
if self.final_conv_length == "auto":
|
|
494
|
-
n_out_time = output_shape[3]
|
|
495
|
-
self.final_conv_length = n_out_time
|
|
496
|
-
|
|
497
|
-
# Incorporating classification module and subsequent ones in one final layer
|
|
498
|
-
module = nn.Sequential()
|
|
499
|
-
|
|
500
|
-
module.add_module(
|
|
501
|
-
"conv_classifier",
|
|
502
|
-
nn.Conv2d(
|
|
503
|
-
n_filters_3,
|
|
504
|
-
self.n_outputs,
|
|
505
|
-
(n_out_virtual_chans, self.final_conv_length),
|
|
506
|
-
bias=True,
|
|
507
|
-
),
|
|
508
|
-
)
|
|
509
|
-
|
|
510
|
-
# Transpose back to the logic of braindecode,
|
|
511
|
-
|
|
512
|
-
# so time in third dimension (axis=2)
|
|
513
|
-
module.add_module(
|
|
514
|
-
"permute_2",
|
|
515
|
-
Rearrange("batch x y z -> batch x z y"),
|
|
516
|
-
)
|
|
517
|
-
|
|
518
|
-
module.add_module("squeeze", SqueezeFinalOutput())
|
|
519
|
-
|
|
520
|
-
self.add_module("final_layer", module)
|
|
353
|
+
@deprecated(
|
|
354
|
+
"`EEGNetv4` was renamed to `EEGNet` in v1.12; this alias will be removed in v1.14."
|
|
355
|
+
)
|
|
356
|
+
class EEGNetv4(EEGNet):
|
|
357
|
+
"""Deprecated alias for EEGNet."""
|
|
521
358
|
|
|
522
|
-
|
|
359
|
+
pass
|
braindecode/models/eegnex.py
CHANGED
|
@@ -16,9 +16,122 @@ 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
|
+
- The braindecode implementation follows the paper's conv-only design with five blocks
|
|
129
|
+
and reproduces the depthwise spatial step and dilated temporal stack. See the class
|
|
130
|
+
reference for exact kernel sizes, dilations, and pooling defaults. You can check the
|
|
131
|
+
original implementation at [EEGNexCode]_.
|
|
132
|
+
|
|
133
|
+
.. versionadded:: 1.1
|
|
134
|
+
|
|
22
135
|
|
|
23
136
|
Parameters
|
|
24
137
|
----------
|
|
@@ -45,12 +158,6 @@ class EEGNeX(EEGModuleMixin, nn.Module):
|
|
|
45
158
|
avg_pool_block5 : tuple[int, int], optional
|
|
46
159
|
Pooling size for block 5. Default is (1, 8).
|
|
47
160
|
|
|
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
161
|
References
|
|
55
162
|
----------
|
|
56
163
|
.. [eegnex] Chen, X., Teng, X., Chen, H., Pan, Y., & Geyer, P. (2024).
|
|
@@ -21,6 +21,8 @@ from braindecode.models.base import EEGModuleMixin
|
|
|
21
21
|
class EEGSimpleConv(EEGModuleMixin, torch.nn.Module):
|
|
22
22
|
"""EEGSimpleConv from Ouahidi, YE et al. (2023) [Yassine2023]_.
|
|
23
23
|
|
|
24
|
+
:bdg-success:`Convolution`
|
|
25
|
+
|
|
24
26
|
.. figure:: https://raw.githubusercontent.com/elouayas/EEGSimpleConv/refs/heads/main/architecture.png
|
|
25
27
|
:align: center
|
|
26
28
|
:alt: EEGSimpleConv Architecture
|
braindecode/models/eegtcnet.py
CHANGED
|
@@ -157,7 +157,7 @@ class EEGTCNet(EEGModuleMixin, nn.Module):
|
|
|
157
157
|
class _EEGNetTC(nn.Module):
|
|
158
158
|
"""EEGNet Temporal Convolutional Network (TCN) block.
|
|
159
159
|
|
|
160
|
-
The main difference from our
|
|
160
|
+
The main difference from our :class:`EEGNet` (braindecode) implementation is the
|
|
161
161
|
kernel and dimensional order. Because of this, we decided to keep this
|
|
162
162
|
implementation in a future issue; we will re-evaluate if it is necessary
|
|
163
163
|
to maintain this separate implementation.
|
braindecode/models/sccnet.py
CHANGED
|
@@ -17,13 +17,21 @@ from braindecode.modules import LogActivation
|
|
|
17
17
|
class SCCNet(EEGModuleMixin, nn.Module):
|
|
18
18
|
"""SCCNet from Wei, C S (2019) [sccnet]_.
|
|
19
19
|
|
|
20
|
+
:bdg-success:`Convolution`
|
|
21
|
+
|
|
20
22
|
Spatial component-wise convolutional network (SCCNet) for motor-imagery EEG
|
|
21
23
|
classification.
|
|
22
24
|
|
|
23
25
|
.. figure:: https://dt5vp8kor0orz.cloudfront.net/6e3ec5d729cd51fe8acc5a978db27d02a5df9e05/2-Figure1-1.png
|
|
24
26
|
:align: center
|
|
25
27
|
:alt: Spatial component-wise convolutional network
|
|
28
|
+
:width: 680px
|
|
29
|
+
|
|
30
|
+
.. rubric:: Architectural Overview
|
|
26
31
|
|
|
32
|
+
SCCNet is a spatial-first convolutional layer that fixes temporal kernels in seconds
|
|
33
|
+
to make its filters correspond to neurophysiologically aligned windows. The model
|
|
34
|
+
comprises four stages:
|
|
27
35
|
|
|
28
36
|
1. **Spatial Component Analysis**: Performs convolution spatial filtering
|
|
29
37
|
across all EEG channels to extract spatial components, effectively
|
|
@@ -35,11 +43,83 @@ class SCCNet(EEGModuleMixin, nn.Module):
|
|
|
35
43
|
4. **Classification**: Flattens the features and applies a fully connected
|
|
36
44
|
layer.
|
|
37
45
|
|
|
46
|
+
.. rubric:: Macro Components
|
|
47
|
+
|
|
48
|
+
- `SCCNet.spatial_conv` **(spatial component analysis)**
|
|
49
|
+
|
|
50
|
+
- *Operations.*
|
|
51
|
+
- :class:`~torch.nn.Conv2d` with kernel `(n_chans, N_t)` and stride `(1, 1)` on an input reshaped to `(B, 1, n_chans, T)`; typical choice `N_t=1` yields a pure across-channel projection (montage-wide linear spatial filter).
|
|
52
|
+
- Zero padding to preserve time, :class:`~torch.nn.BatchNorm2d`; output has `N_u` component signals shaped `(B, 1, N_u, T)` after a permute step.
|
|
53
|
+
|
|
54
|
+
*Interpretability/robustness.* Mimics CSP-like spatial filtering: each learned filter is a channel-weighted component, easing inspection and reducing channel noise.
|
|
55
|
+
|
|
56
|
+
- `SCCNet.spatial_filt_conv` **(spatio-temporal filtering)**
|
|
57
|
+
|
|
58
|
+
- *Operations.*
|
|
59
|
+
- :class:`~torch.nn.Conv2d` with kernel `(N_u, 12)` over components and time (12 samples ~ 0.1 s at 125 Hz),
|
|
60
|
+
- :class:`~torch.nn.BatchNorm2d`;
|
|
61
|
+
- Nonlinearity is **power-like**: the original paper uses **square** like :class:`~braindecode.models.ShallowFBCSPNet` with the class :class:`~braindecode.modules.LogActivation` as default.
|
|
62
|
+
- :class:`~torch.nn.Dropout` with rate `p=0.5`.
|
|
63
|
+
|
|
64
|
+
- *Role.* Learns frequency-selective energy features and inter-component interactions within a 0.1 s context (beta/alpha cycle scale).
|
|
65
|
+
|
|
66
|
+
- `SCCNet.temporal_smoothing` **(aggregation + readout)**
|
|
67
|
+
|
|
68
|
+
- *Operations.*
|
|
69
|
+
- :class:`~torch.nn.AvgPool2d` with size `(1, 62)` (~ 0.5 s) for temporal smoothing and downsampling
|
|
70
|
+
- :class:`~torch.nn.Flatten`
|
|
71
|
+
- :class:`~torch.nn.Linear` to `n_outputs`.
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
.. rubric:: Convolutional Details
|
|
75
|
+
|
|
76
|
+
* **Temporal (where time-domain patterns are learned).**
|
|
77
|
+
The second block's kernel length is fixed to 12 samples (≈ 100 ms) and slides with
|
|
78
|
+
stride 1; average pooling `(1, 62)` (≈ 500 ms) integrates power over longer spans.
|
|
79
|
+
These choices bake in short-cycle detection followed by half-second trend smoothing.
|
|
80
|
+
|
|
81
|
+
* **Spatial (how electrodes are processed).**
|
|
82
|
+
The first block's kernel spans **all electrodes** `(n_chans, N_t)`. With `N_t=1`,
|
|
83
|
+
it reduces to a montage-wide linear projection, mapping channels → `N_u` components.
|
|
84
|
+
The second block mixes **across components** via kernel height `N_u`.
|
|
85
|
+
|
|
86
|
+
* **Spectral (how frequency information is captured).**
|
|
87
|
+
No explicit transform is used; learned **temporal kernels** serve as bandpass-like
|
|
88
|
+
filters, and the **square/log power** nonlinearity plus 0.5 s averaging approximate
|
|
89
|
+
band-power estimation (ERD/ERS-style features).
|
|
90
|
+
|
|
91
|
+
.. rubric:: Attention / Sequential Modules
|
|
92
|
+
|
|
93
|
+
This model contains **no attention** and **no recurrent units**.
|
|
94
|
+
|
|
95
|
+
.. rubric:: Additional Mechanisms
|
|
96
|
+
|
|
97
|
+
- :class:`~torch.nn.BatchNorm2d` and zero-padding are applied to both convolutions;
|
|
98
|
+
L2 weight decay was used in the original paper; dropout `p=0.5` combats overfitting.
|
|
99
|
+
- Contrasting with other compact neural network, in EEGNet performs a temporal depthwise conv
|
|
100
|
+
followed by a **depthwise spatial** conv (separable), learning temporal filters first.
|
|
101
|
+
SCCNet inverts this order: it performs a **full spatial projection first** (CSP-like),
|
|
102
|
+
then a short **spatio-temporal** conv with an explicit 0.1 s kernel, followed by
|
|
103
|
+
**power-like** nonlinearity and longer temporal averaging. EEGNet's ELU and
|
|
104
|
+
separable design favor parameter efficiency; SCCNet's second-scale kernels and
|
|
105
|
+
square/log emphasize interpretable **band-power** features.
|
|
106
|
+
|
|
107
|
+
- Reference implementation: see [sccnetcode]_.
|
|
108
|
+
|
|
109
|
+
.. rubric:: Usage and Configuration
|
|
110
|
+
|
|
111
|
+
* **Training from the original authors.**
|
|
112
|
+
|
|
113
|
+
* Match window length so that `T` is comfortably larger than pooling length
|
|
114
|
+
(e.g., > 1.5-2 s for MI).
|
|
115
|
+
* Start with standard MI augmentations (channel dropout/shuffle, time reverse)
|
|
116
|
+
and tune `n_spatial_filters` before deeper changes.
|
|
38
117
|
|
|
39
118
|
Parameters
|
|
40
119
|
----------
|
|
41
120
|
n_spatial_filters : int, optional
|
|
42
|
-
Number of spatial filters in the first convolutional layer
|
|
121
|
+
Number of spatial filters in the first convolutional layer, variable `N_u` from the
|
|
122
|
+
original paper. Default is 22.
|
|
43
123
|
n_spatial_filters_smooth : int, optional
|
|
44
124
|
Number of spatial filters used as filter in the second convolutional
|
|
45
125
|
layer. Default is 20.
|
|
@@ -49,13 +129,6 @@ class SCCNet(EEGModuleMixin, nn.Module):
|
|
|
49
129
|
Activation function after the second convolutional layer. Default is
|
|
50
130
|
logarithm activation.
|
|
51
131
|
|
|
52
|
-
Notes
|
|
53
|
-
-----
|
|
54
|
-
This implementation is not guaranteed to be correct, has not been checked
|
|
55
|
-
by original authors, only reimplemented from the paper description and
|
|
56
|
-
the source that have not been tested [sccnetcode]_.
|
|
57
|
-
|
|
58
|
-
|
|
59
132
|
References
|
|
60
133
|
----------
|
|
61
134
|
.. [sccnet] Wei, C. S., Koike-Akino, T., & Wang, Y. (2019, March). Spatial
|
|
@@ -20,6 +20,8 @@ from braindecode.modules import (
|
|
|
20
20
|
class ShallowFBCSPNet(EEGModuleMixin, nn.Sequential):
|
|
21
21
|
"""Shallow ConvNet model from Schirrmeister et al (2017) [Schirrmeister2017]_.
|
|
22
22
|
|
|
23
|
+
:bdg-success:`Convolution`
|
|
24
|
+
|
|
23
25
|
.. figure:: https://onlinelibrary.wiley.com/cms/asset/221ea375-6701-40d3-ab3f-e411aad62d9e/hbm23730-fig-0002-m.jpg
|
|
24
26
|
:align: center
|
|
25
27
|
:alt: ShallowNet Architecture
|
|
@@ -11,6 +11,8 @@ from braindecode.models.base import EEGModuleMixin
|
|
|
11
11
|
class SleepStagerBlanco2020(EEGModuleMixin, nn.Module):
|
|
12
12
|
"""Sleep staging architecture from Blanco et al. (2020) from [Blanco2020]_
|
|
13
13
|
|
|
14
|
+
:bdg-success:`Convolution`
|
|
15
|
+
|
|
14
16
|
.. figure:: https://media.springernature.com/full/springer-static/image/art%3A10.1007%2Fs00500-019-04174-1/MediaObjects/500_2019_4174_Fig2_HTML.png
|
|
15
17
|
:align: center
|
|
16
18
|
:alt: SleepStagerBlanco2020 Architecture
|
|
@@ -13,6 +13,8 @@ from braindecode.models.base import EEGModuleMixin
|
|
|
13
13
|
class SleepStagerChambon2018(EEGModuleMixin, nn.Module):
|
|
14
14
|
"""Sleep staging architecture from Chambon et al. (2018) [Chambon2018]_.
|
|
15
15
|
|
|
16
|
+
:bdg-success:`Convolution`
|
|
17
|
+
|
|
16
18
|
.. figure:: https://braindecode.org/dev/_static/model/SleepStagerChambon2018.jpg
|
|
17
19
|
:align: center
|
|
18
20
|
:alt: SleepStagerChambon2018 Architecture
|
braindecode/models/sparcnet.py
CHANGED
|
@@ -13,6 +13,8 @@ from braindecode.models.base import EEGModuleMixin
|
|
|
13
13
|
class SPARCNet(EEGModuleMixin, nn.Module):
|
|
14
14
|
"""Seizures, Periodic and Rhythmic pattern Continuum Neural Network (SPaRCNet) from Jing et al. (2023) [jing2023]_.
|
|
15
15
|
|
|
16
|
+
:bdg-success:`Convolution`
|
|
17
|
+
|
|
16
18
|
This is a temporal CNN model for biosignal classification based on the DenseNet
|
|
17
19
|
architecture.
|
|
18
20
|
|