braindecode 1.3.0.dev180851780__py3-none-any.whl → 1.3.0.dev183667303__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/base.py +1 -1
- braindecode/datasets/__init__.py +10 -2
- braindecode/datasets/base.py +115 -151
- braindecode/datasets/bcicomp.py +4 -4
- braindecode/datasets/bids.py +3 -3
- braindecode/datasets/experimental.py +2 -2
- braindecode/datasets/mne.py +3 -5
- braindecode/datasets/moabb.py +2 -2
- braindecode/datasets/nmt.py +2 -2
- braindecode/datasets/sleep_physio_challe_18.py +2 -2
- braindecode/datasets/sleep_physionet.py +2 -2
- braindecode/datasets/tuh.py +2 -2
- braindecode/datasets/xy.py +2 -2
- braindecode/datautil/serialization.py +7 -7
- braindecode/eegneuralnet.py +2 -0
- braindecode/functional/functions.py +6 -2
- braindecode/functional/initialization.py +2 -3
- braindecode/models/__init__.py +2 -0
- braindecode/models/atcnet.py +25 -26
- braindecode/models/attentionbasenet.py +39 -32
- braindecode/models/base.py +280 -2
- braindecode/models/bendr.py +469 -0
- braindecode/models/biot.py +2 -0
- braindecode/models/ctnet.py +6 -3
- braindecode/models/deepsleepnet.py +27 -18
- braindecode/models/eegconformer.py +2 -2
- braindecode/models/eeginception_erp.py +31 -25
- braindecode/models/eegnet.py +1 -1
- braindecode/models/labram.py +193 -87
- braindecode/models/signal_jepa.py +109 -27
- braindecode/models/sinc_shallow.py +10 -9
- braindecode/models/sstdpn.py +11 -11
- braindecode/models/summary.csv +1 -0
- braindecode/models/usleep.py +26 -21
- braindecode/models/util.py +1 -0
- braindecode/modules/attention.py +10 -10
- braindecode/modules/blocks.py +3 -3
- braindecode/modules/filter.py +2 -3
- braindecode/modules/layers.py +18 -17
- braindecode/preprocessing/__init__.py +24 -0
- braindecode/preprocessing/eegprep_preprocess.py +1202 -0
- braindecode/preprocessing/preprocess.py +12 -12
- braindecode/preprocessing/util.py +166 -0
- braindecode/preprocessing/windowers.py +24 -19
- braindecode/samplers/base.py +8 -8
- braindecode/version.py +1 -1
- {braindecode-1.3.0.dev180851780.dist-info → braindecode-1.3.0.dev183667303.dist-info}/METADATA +6 -2
- {braindecode-1.3.0.dev180851780.dist-info → braindecode-1.3.0.dev183667303.dist-info}/RECORD +52 -49
- {braindecode-1.3.0.dev180851780.dist-info → braindecode-1.3.0.dev183667303.dist-info}/WHEEL +0 -0
- {braindecode-1.3.0.dev180851780.dist-info → braindecode-1.3.0.dev183667303.dist-info}/licenses/LICENSE.txt +0 -0
- {braindecode-1.3.0.dev180851780.dist-info → braindecode-1.3.0.dev183667303.dist-info}/licenses/NOTICE.txt +0 -0
- {braindecode-1.3.0.dev180851780.dist-info → braindecode-1.3.0.dev183667303.dist-info}/top_level.txt +0 -0
|
@@ -5,7 +5,8 @@ from __future__ import annotations
|
|
|
5
5
|
|
|
6
6
|
import math
|
|
7
7
|
from copy import deepcopy
|
|
8
|
-
from
|
|
8
|
+
from pathlib import Path
|
|
9
|
+
from typing import Any, Optional, Sequence
|
|
9
10
|
|
|
10
11
|
import torch
|
|
11
12
|
from einops.layers.torch import Rearrange
|
|
@@ -145,6 +146,8 @@ class _BaseSignalJEPA(EEGModuleMixin, nn.Module):
|
|
|
145
146
|
class SignalJEPA(_BaseSignalJEPA):
|
|
146
147
|
"""Architecture introduced in signal-JEPA for self-supervised pre-training, Guetschel, P et al (2024) [1]_
|
|
147
148
|
|
|
149
|
+
:bdg-danger:`Large Brain Model`
|
|
150
|
+
|
|
148
151
|
This model is not meant for classification but for SSL pre-training.
|
|
149
152
|
Its output shape depends on the input shape.
|
|
150
153
|
For classification purposes, three variants of this model are available:
|
|
@@ -231,6 +234,8 @@ class SignalJEPA(_BaseSignalJEPA):
|
|
|
231
234
|
class SignalJEPA_Contextual(_BaseSignalJEPA):
|
|
232
235
|
"""Contextual downstream architecture introduced in signal-JEPA Guetschel, P et al (2024) [1]_.
|
|
233
236
|
|
|
237
|
+
:bdg-danger:`Large Brain Model`
|
|
238
|
+
|
|
234
239
|
This architecture is one of the variants of :class:`SignalJEPA`
|
|
235
240
|
that can be used for classification purposes.
|
|
236
241
|
|
|
@@ -319,25 +324,50 @@ class SignalJEPA_Contextual(_BaseSignalJEPA):
|
|
|
319
324
|
@classmethod
|
|
320
325
|
def from_pretrained(
|
|
321
326
|
cls,
|
|
322
|
-
model: SignalJEPA,
|
|
323
|
-
n_outputs: int,
|
|
327
|
+
model: Optional[SignalJEPA | str | Path] = None, # type: ignore
|
|
328
|
+
n_outputs: Optional[int] = None, # type: ignore
|
|
324
329
|
n_spat_filters: int = 4,
|
|
325
|
-
chs_info: list[dict[str, Any]]
|
|
330
|
+
chs_info: Optional[list[dict[str, Any]]] = None, # type: ignore
|
|
331
|
+
**kwargs,
|
|
326
332
|
):
|
|
327
|
-
"""Instantiate a new model from a pre-trained :class:`SignalJEPA` model.
|
|
333
|
+
"""Instantiate a new model from a pre-trained :class:`SignalJEPA` model or from Hub.
|
|
328
334
|
|
|
329
335
|
Parameters
|
|
330
336
|
----------
|
|
331
|
-
model: SignalJEPA
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
337
|
+
model: SignalJEPA, str, Path, or None
|
|
338
|
+
Either a pre-trained :class:`SignalJEPA` model, a string/Path to a local directory
|
|
339
|
+
(for Hub-style loading), or None (for Hub loading via kwargs).
|
|
340
|
+
n_outputs: int or None
|
|
341
|
+
Number of classes for the new model. Required when loading from a SignalJEPA model,
|
|
342
|
+
optional when loading from Hub (will be read from config).
|
|
335
343
|
n_spat_filters: int
|
|
336
344
|
Number of spatial filters.
|
|
337
345
|
chs_info: list of dict | None
|
|
338
346
|
Information about each individual EEG channel. This should be filled with
|
|
339
347
|
``info["chs"]``. Refer to :class:`mne.Info` for more details.
|
|
348
|
+
**kwargs
|
|
349
|
+
Additional keyword arguments passed to the parent class for Hub loading.
|
|
340
350
|
"""
|
|
351
|
+
# Check if this is a Hub-style load (from a directory path)
|
|
352
|
+
if isinstance(model, (str, Path)) or (model is None and kwargs):
|
|
353
|
+
# This is a Hub load, delegate to parent class
|
|
354
|
+
if isinstance(model, (str, Path)):
|
|
355
|
+
# model is actually the repo_id or directory path
|
|
356
|
+
return super().from_pretrained(model, **kwargs)
|
|
357
|
+
else:
|
|
358
|
+
# model is None, treat as hub-style load
|
|
359
|
+
return super().from_pretrained(**kwargs)
|
|
360
|
+
|
|
361
|
+
# This is the original SignalJEPA transfer learning case
|
|
362
|
+
if not isinstance(model, SignalJEPA):
|
|
363
|
+
raise TypeError(
|
|
364
|
+
f"model must be a SignalJEPA instance, a path string, or Path object, got {type(model)}"
|
|
365
|
+
)
|
|
366
|
+
if n_outputs is None:
|
|
367
|
+
raise ValueError(
|
|
368
|
+
"n_outputs must be provided when loading from a SignalJEPA model"
|
|
369
|
+
)
|
|
370
|
+
|
|
341
371
|
feature_encoder = model.feature_encoder
|
|
342
372
|
pos_encoder = model.pos_encoder
|
|
343
373
|
transformer = model.transformer
|
|
@@ -377,6 +407,8 @@ class SignalJEPA_Contextual(_BaseSignalJEPA):
|
|
|
377
407
|
class SignalJEPA_PostLocal(_BaseSignalJEPA):
|
|
378
408
|
"""Post-local downstream architecture introduced in signal-JEPA Guetschel, P et al (2024) [1]_.
|
|
379
409
|
|
|
410
|
+
:bdg-danger:`Large Brain Model`
|
|
411
|
+
|
|
380
412
|
This architecture is one of the variants of :class:`SignalJEPA`
|
|
381
413
|
that can be used for classification purposes.
|
|
382
414
|
|
|
@@ -463,22 +495,47 @@ class SignalJEPA_PostLocal(_BaseSignalJEPA):
|
|
|
463
495
|
|
|
464
496
|
@classmethod
|
|
465
497
|
def from_pretrained(
|
|
466
|
-
cls,
|
|
498
|
+
cls,
|
|
499
|
+
model: SignalJEPA | str | Path = None, # type: ignore
|
|
500
|
+
n_outputs: int = None, # type: ignore
|
|
501
|
+
n_spat_filters: int = 4,
|
|
502
|
+
**kwargs,
|
|
467
503
|
):
|
|
468
|
-
"""Instantiate a new model from a pre-trained :class:`SignalJEPA` model.
|
|
504
|
+
"""Instantiate a new model from a pre-trained :class:`SignalJEPA` model or from Hub.
|
|
469
505
|
|
|
470
506
|
Parameters
|
|
471
507
|
----------
|
|
472
|
-
model: SignalJEPA
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
508
|
+
model: SignalJEPA, str, Path, or None
|
|
509
|
+
Either a pre-trained :class:`SignalJEPA` model, a string/Path to a local directory
|
|
510
|
+
(for Hub-style loading), or None (for Hub loading via kwargs).
|
|
511
|
+
n_outputs: int or None
|
|
512
|
+
Number of classes for the new model. Required when loading from a SignalJEPA model,
|
|
513
|
+
optional when loading from Hub (will be read from config).
|
|
476
514
|
n_spat_filters: int
|
|
477
515
|
Number of spatial filters.
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
``info["chs"]``. Refer to :class:`mne.Info` for more details.
|
|
516
|
+
**kwargs
|
|
517
|
+
Additional keyword arguments passed to the parent class for Hub loading.
|
|
481
518
|
"""
|
|
519
|
+
# Check if this is a Hub-style load (from a directory path)
|
|
520
|
+
if isinstance(model, (str, Path)) or (model is None and kwargs):
|
|
521
|
+
# This is a Hub load, delegate to parent class
|
|
522
|
+
if isinstance(model, (str, Path)):
|
|
523
|
+
# model is actually the repo_id or directory path
|
|
524
|
+
return super().from_pretrained(model, **kwargs)
|
|
525
|
+
else:
|
|
526
|
+
# model is None, treat as hub-style load
|
|
527
|
+
return super().from_pretrained(**kwargs)
|
|
528
|
+
|
|
529
|
+
# This is the original SignalJEPA transfer learning case
|
|
530
|
+
if not isinstance(model, SignalJEPA):
|
|
531
|
+
raise TypeError(
|
|
532
|
+
f"model must be a SignalJEPA instance, a path string, or Path object, got {type(model)}"
|
|
533
|
+
)
|
|
534
|
+
if n_outputs is None:
|
|
535
|
+
raise ValueError(
|
|
536
|
+
"n_outputs must be provided when loading from a SignalJEPA model"
|
|
537
|
+
)
|
|
538
|
+
|
|
482
539
|
feature_encoder = model.feature_encoder
|
|
483
540
|
assert feature_encoder is not None
|
|
484
541
|
new_model = cls(
|
|
@@ -597,22 +654,47 @@ class SignalJEPA_PreLocal(_BaseSignalJEPA):
|
|
|
597
654
|
|
|
598
655
|
@classmethod
|
|
599
656
|
def from_pretrained(
|
|
600
|
-
cls,
|
|
657
|
+
cls,
|
|
658
|
+
model: SignalJEPA | str | Path = None, # type: ignore
|
|
659
|
+
n_outputs: int = None, # type: ignore
|
|
660
|
+
n_spat_filters: int = 4,
|
|
661
|
+
**kwargs,
|
|
601
662
|
):
|
|
602
|
-
"""Instantiate a new model from a pre-trained :class:`SignalJEPA` model.
|
|
663
|
+
"""Instantiate a new model from a pre-trained :class:`SignalJEPA` model or from Hub.
|
|
603
664
|
|
|
604
665
|
Parameters
|
|
605
666
|
----------
|
|
606
|
-
model: SignalJEPA
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
667
|
+
model: SignalJEPA, str, Path, or None
|
|
668
|
+
Either a pre-trained :class:`SignalJEPA` model, a string/Path to a local directory
|
|
669
|
+
(for Hub-style loading), or None (for Hub loading via kwargs).
|
|
670
|
+
n_outputs: int or None
|
|
671
|
+
Number of classes for the new model. Required when loading from a SignalJEPA model,
|
|
672
|
+
optional when loading from Hub (will be read from config).
|
|
610
673
|
n_spat_filters: int
|
|
611
674
|
Number of spatial filters.
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
``info["chs"]``. Refer to :class:`mne.Info` for more details.
|
|
675
|
+
**kwargs
|
|
676
|
+
Additional keyword arguments passed to the parent class for Hub loading.
|
|
615
677
|
"""
|
|
678
|
+
# Check if this is a Hub-style load (from a directory path)
|
|
679
|
+
if isinstance(model, (str, Path)) or (model is None and kwargs):
|
|
680
|
+
# This is a Hub load, delegate to parent class
|
|
681
|
+
if isinstance(model, (str, Path)):
|
|
682
|
+
# model is actually the repo_id or directory path
|
|
683
|
+
return super().from_pretrained(model, **kwargs)
|
|
684
|
+
else:
|
|
685
|
+
# model is None, treat as hub-style load
|
|
686
|
+
return super().from_pretrained(**kwargs)
|
|
687
|
+
|
|
688
|
+
# This is the original SignalJEPA transfer learning case
|
|
689
|
+
if not isinstance(model, SignalJEPA):
|
|
690
|
+
raise TypeError(
|
|
691
|
+
f"model must be a SignalJEPA instance, a path string, or Path object, got {type(model)}"
|
|
692
|
+
)
|
|
693
|
+
if n_outputs is None:
|
|
694
|
+
raise ValueError(
|
|
695
|
+
"n_outputs must be provided when loading from a SignalJEPA model"
|
|
696
|
+
)
|
|
697
|
+
|
|
616
698
|
feature_encoder = model.feature_encoder
|
|
617
699
|
assert feature_encoder is not None
|
|
618
700
|
new_model = cls(
|
|
@@ -19,23 +19,24 @@ class SincShallowNet(EEGModuleMixin, nn.Module):
|
|
|
19
19
|
The Sinc-ShallowNet architecture has these fundamental blocks:
|
|
20
20
|
|
|
21
21
|
1. **Block 1: Spectral and Spatial Feature Extraction**
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
- *Batch Normalization*
|
|
22
|
+
|
|
23
|
+
- *Temporal Sinc-Convolutional Layer*: Uses parametrized sinc functions to learn band-pass filters,
|
|
24
|
+
significantly reducing the number of trainable parameters by only
|
|
25
|
+
learning the lower and upper cutoff frequencies for each filter.
|
|
26
|
+
- *Spatial Depthwise Convolutional Layer*: Applies depthwise convolutions to learn spatial filters for
|
|
27
|
+
each temporal feature map independently, further reducing
|
|
28
|
+
parameters and enhancing interpretability.
|
|
29
|
+
- *Batch Normalization*
|
|
31
30
|
|
|
32
31
|
2. **Block 2: Temporal Aggregation**
|
|
32
|
+
|
|
33
33
|
- *Activation Function*: ELU
|
|
34
34
|
- *Average Pooling Layer*: Aggregation by averaging spatial dim
|
|
35
35
|
- *Dropout Layer*
|
|
36
36
|
- *Flatten Layer*
|
|
37
37
|
|
|
38
38
|
3. **Block 3: Classification**
|
|
39
|
+
|
|
39
40
|
- *Fully Connected Layer*: Maps the feature vector to n_outputs.
|
|
40
41
|
|
|
41
42
|
**Implementation Notes:**
|
braindecode/models/sstdpn.py
CHANGED
|
@@ -24,7 +24,7 @@ class SSTDPN(EEGModuleMixin, nn.Module):
|
|
|
24
24
|
:alt: SSTDPN Architecture
|
|
25
25
|
:width: 1000px
|
|
26
26
|
|
|
27
|
-
The **Spatial
|
|
27
|
+
The **Spatial-Spectral** and **Temporal - Dual Prototype Network** (SST-DPN)
|
|
28
28
|
is an end-to-end 1D convolutional architecture designed for motor imagery (MI) EEG decoding,
|
|
29
29
|
aiming to address challenges related to discriminative feature extraction and
|
|
30
30
|
small-sample sizes [Han2025]_.
|
|
@@ -37,9 +37,9 @@ class SSTDPN(EEGModuleMixin, nn.Module):
|
|
|
37
37
|
SST-DPN consists of a feature extractor (_SSTEncoder, comprising Adaptive Spatial-Spectral
|
|
38
38
|
Fusion and Multi-scale Variance Pooling) followed by Dual Prototype Learning classification [Han2025]_.
|
|
39
39
|
|
|
40
|
-
1. **Adaptive Spatial
|
|
41
|
-
multi-channel spatial
|
|
42
|
-
(Spatial-Spectral Attention) to model relationships and highlight key spatial
|
|
40
|
+
1. **Adaptive Spatial-Spectral Fusion (ASSF)**: Uses :class:`_DepthwiseTemporalConv1d` to generate a
|
|
41
|
+
multi-channel spatial-spectral representation, followed by :class:`_SpatSpectralAttn`
|
|
42
|
+
(Spatial-Spectral Attention) to model relationships and highlight key spatial-spectral
|
|
43
43
|
channels [Han2025]_.
|
|
44
44
|
|
|
45
45
|
2. **Multi-scale Variance Pooling (MVP)**: Applies :class:`_MultiScaleVarPooler` with variance pooling
|
|
@@ -57,7 +57,7 @@ class SSTDPN(EEGModuleMixin, nn.Module):
|
|
|
57
57
|
|
|
58
58
|
- `SSTDPN.encoder` **(Feature Extractor)**
|
|
59
59
|
|
|
60
|
-
- *Operations.* Combines Adaptive Spatial
|
|
60
|
+
- *Operations.* Combines Adaptive Spatial-Spectral Fusion and Multi-scale Variance Pooling
|
|
61
61
|
via an internal :class:`_SSTEncoder`.
|
|
62
62
|
- *Role.* Maps the raw MI-EEG trial :math:`X_i \in \mathbb{R}^{C \times T}` to the
|
|
63
63
|
feature space :math:`z_i \in \mathbb{R}^d`.
|
|
@@ -69,11 +69,11 @@ class SSTDPN(EEGModuleMixin, nn.Module):
|
|
|
69
69
|
depth multiplier `n_spectral_filters_temporal` (equivalent to :math:`F_1` in the paper).
|
|
70
70
|
- *Role.* Extracts multiple distinct spectral bands from each EEG channel independently.
|
|
71
71
|
|
|
72
|
-
- `_SSTEncoder.spt_attn` **(Spatial
|
|
72
|
+
- `_SSTEncoder.spt_attn` **(Spatial-Spectral Attention for Channel Gating)**
|
|
73
73
|
|
|
74
74
|
- *Operations.* Internal :class:`_SpatSpectralAttn` module using Global Context Embedding
|
|
75
75
|
via variance-based pooling, followed by adaptive channel normalization and gating.
|
|
76
|
-
- *Role.* Reweights channels in the spatial
|
|
76
|
+
- *Role.* Reweights channels in the spatial-spectral dimension to extract efficient and
|
|
77
77
|
discriminative features by emphasizing task-relevant regions and frequency bands.
|
|
78
78
|
|
|
79
79
|
- `_SSTEncoder.chan_conv` **(Pointwise Fusion across Channels)**
|
|
@@ -81,7 +81,7 @@ class SSTDPN(EEGModuleMixin, nn.Module):
|
|
|
81
81
|
- *Operations.* A 1D pointwise convolution with `n_fused_filters` output channels
|
|
82
82
|
(equivalent to :math:`F_2` in the paper), followed by BatchNorm and the specified
|
|
83
83
|
`activation` function (default: ELU).
|
|
84
|
-
- *Role.* Fuses the weighted spatial
|
|
84
|
+
- *Role.* Fuses the weighted spatial-spectral features across all electrodes to produce
|
|
85
85
|
a fused representation :math:`X_{fused} \in \mathbb{R}^{F_2 \times T}`.
|
|
86
86
|
|
|
87
87
|
- `_SSTEncoder.mvp` **(Multi-scale Variance Pooling for Temporal Extraction)**
|
|
@@ -109,11 +109,11 @@ class SSTDPN(EEGModuleMixin, nn.Module):
|
|
|
109
109
|
* **Spatial.**
|
|
110
110
|
The initial convolution at the classes :class:`_DepthwiseTemporalConv1d` groups parameter :math:`h=1`,
|
|
111
111
|
meaning :math:`F_1` temporal filters are shared across channels. The Spatial-Spectral Attention
|
|
112
|
-
mechanism explicitly models the relationships among these channels in the spatial
|
|
112
|
+
mechanism explicitly models the relationships among these channels in the spatial-spectral
|
|
113
113
|
dimension, allowing for finer-grained spatial feature modeling compared to conventional
|
|
114
114
|
GCNs according to the authors [Han2025]_.
|
|
115
115
|
In other words, all electrode channels share :math:`F_1` temporal filters
|
|
116
|
-
independently to produce the spatial
|
|
116
|
+
independently to produce the spatial-spectral representation.
|
|
117
117
|
|
|
118
118
|
* **Spectral.**
|
|
119
119
|
Spectral information is implicitly extracted via the :math:`F_1` filters in :class:`_DepthwiseTemporalConv1d`.
|
|
@@ -123,7 +123,7 @@ class SSTDPN(EEGModuleMixin, nn.Module):
|
|
|
123
123
|
|
|
124
124
|
.. rubric:: Additional Mechanisms
|
|
125
125
|
|
|
126
|
-
- **Attention.** A lightweight Spatial-Spectral Attention mechanism models spatial
|
|
126
|
+
- **Attention.** A lightweight Spatial-Spectral Attention mechanism models spatial-spectral relationships
|
|
127
127
|
at the channel level, distinct from applying attention to deep feature dimensions,
|
|
128
128
|
which is common in comparison methods like :class:`ATCNet`.
|
|
129
129
|
- **Regularization.** Dual Prototype Learning acts as a regularization technique
|
braindecode/models/summary.csv
CHANGED
|
@@ -39,3 +39,4 @@ FBLightConvNet,Motor Imagery,Classification,250,"n_chans, n_outputs, n_times, sf
|
|
|
39
39
|
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"
|
|
40
40
|
PBT,General,Classification,250,"n_chans, n_outputs, n_times",818948,"PBT(n_chans=22, n_outputs=4, n_times=1000, sfreq=250)","Large Brain Model"
|
|
41
41
|
SSTDPN,Motor Imagery,Classification,250,"n_chans, n_outputs, n_times",19502,"SSTDPN(n_chans=22, n_outputs=4, n_times=1000)","Convolution,Small Attention"
|
|
42
|
+
BENDR,General,"Classification,Embedding",250,"n_chans, n_times, n_outputs",157141049,"BENDR(n_chans=22, n_outputs=4, n_times=1000)","Large Brain Model,Convolution"
|
braindecode/models/usleep.py
CHANGED
|
@@ -62,43 +62,48 @@ class USleep(EEGModuleMixin, nn.Module):
|
|
|
62
62
|
- Decoder :class:`_DecoderBlock` **(progressive upsampling + skip fusion to high-frequency map, 12 blocks; upsampling x2 per block)**
|
|
63
63
|
|
|
64
64
|
- *Operations.*
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
65
|
+
|
|
66
|
+
- **Nearest-neighbor upsample**, :class:`nn.Upsample` (x2)
|
|
67
|
+
- **Convolution2d** (k=2), :class:`torch.nn.Conv2d`
|
|
68
|
+
- ELU, :class:`torch.nn.ELU`
|
|
69
|
+
- Batch Norm, :class:`torch.nn.BatchNorm2d`
|
|
70
|
+
- **Concatenate** with the encoder skip at the same temporal scale, ``torch.cat``
|
|
71
|
+
- **Convolution**, :class:`torch.nn.Conv2d`
|
|
72
|
+
- ELU, :class:`torch.nn.ELU`
|
|
73
|
+
- Batch Norm, :class:`torch.nn.BatchNorm2d`.
|
|
73
74
|
|
|
74
75
|
**Output**: A multi-class, **high-frequency** per-sample representation aligned to the input rate (128 Hz).
|
|
75
76
|
|
|
76
77
|
- **Segment Classifier incorporate into :class:`braindecode.models.USleep` (aggregation to fixed epochs)**
|
|
77
78
|
|
|
78
79
|
- *Operations.*
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
80
|
+
|
|
81
|
+
- **Mean-pool**, :class:`torch.nn.AvgPool2d` per class with kernel = epoch length *i* and stride *i*
|
|
82
|
+
- **1x1 conv**, :class:`torch.nn.Conv2d`
|
|
83
|
+
- ELU, :class:`torch.nn.ELU`
|
|
84
|
+
- **1x1 conv**, :class:`torch.nn.Conv2d` with ``(T, K)`` (epochs x stages).
|
|
83
85
|
|
|
84
86
|
**Role**: Learns a **non-linear** weighted combination over each 30-s window (unlike U-Time's linear combiner).
|
|
85
87
|
|
|
86
88
|
.. rubric:: Convolutional Details
|
|
87
89
|
|
|
88
90
|
- **Temporal (where time-domain patterns are learned).**
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
91
|
+
|
|
92
|
+
All convolutions are **1-D along time**; depth (12 levels) plus pooling yields an extensive receptive field
|
|
93
|
+
(reported sensitivity to ±6.75 min around each epoch; theoretical field ≈ 9.6 min at the deepest layer).
|
|
94
|
+
The decoder restores sample-level resolution before epoch aggregation.
|
|
92
95
|
|
|
93
96
|
- **Spatial (how channels are processed).**
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
+
|
|
98
|
+
Convolutions mix across the *channel* dimension jointly with time (no separate spatial operator). The system
|
|
99
|
+
is **montage-agnostic** (any reasonable EEG/EOG pair) and was trained across diverse cohorts/protocols,
|
|
100
|
+
supporting robustness to channel placement and hardware differences.
|
|
97
101
|
|
|
98
102
|
- **Spectral (how frequency content is captured).**
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
103
|
+
|
|
104
|
+
No explicit Fourier/wavelet transform is used; the **stack of temporal convolutions** acts as a learned
|
|
105
|
+
filter bank whose effective bandwidth grows with depth. The high-frequency decoder output (128 Hz)
|
|
106
|
+
retains fine temporal detail for the segment classifier.
|
|
102
107
|
|
|
103
108
|
|
|
104
109
|
.. rubric:: Attention / Sequential Modules
|
braindecode/models/util.py
CHANGED
|
@@ -97,6 +97,7 @@ models_mandatory_parameters = [
|
|
|
97
97
|
("IFNet", ["n_chans", "n_outputs", "n_times", "sfreq"], dict(sfreq=200.0)),
|
|
98
98
|
("PBT", ["n_chans", "n_outputs", "n_times"], None),
|
|
99
99
|
("SSTDPN", ["n_chans", "n_outputs", "n_times", "sfreq"], None),
|
|
100
|
+
("BENDR", ["n_chans", "n_outputs", "n_times"], None),
|
|
100
101
|
]
|
|
101
102
|
|
|
102
103
|
################################################################
|
braindecode/modules/attention.py
CHANGED
|
@@ -38,7 +38,7 @@ class SqueezeAndExcitation(nn.Module):
|
|
|
38
38
|
References
|
|
39
39
|
----------
|
|
40
40
|
.. [Hu2018] Hu, J., Albanie, S., Sun, G., Wu, E., 2018.
|
|
41
|
-
|
|
41
|
+
Squeeze-and-Excitation Networks. CVPR 2018.
|
|
42
42
|
"""
|
|
43
43
|
|
|
44
44
|
def __init__(self, in_channels: int, reduction_rate: int, bias: bool = False):
|
|
@@ -93,7 +93,7 @@ class GSoP(nn.Module):
|
|
|
93
93
|
References
|
|
94
94
|
----------
|
|
95
95
|
.. [Gao2018] Gao, Z., Jiangtao, X., Wang, Q., Li, P., 2018.
|
|
96
|
-
|
|
96
|
+
Global Second-order Pooling Convolutional Networks. CVPR 2018.
|
|
97
97
|
"""
|
|
98
98
|
|
|
99
99
|
def __init__(self, in_channels: int, reduction_rate: int, bias: bool = True):
|
|
@@ -149,7 +149,7 @@ class FCA(nn.Module):
|
|
|
149
149
|
References
|
|
150
150
|
----------
|
|
151
151
|
.. [Qin2021] Qin, Z., Zhang, P., Wu, F., Li, X., 2021.
|
|
152
|
-
|
|
152
|
+
FcaNet: Frequency Channel Attention Networks. ICCV 2021.
|
|
153
153
|
"""
|
|
154
154
|
|
|
155
155
|
def __init__(
|
|
@@ -233,7 +233,7 @@ class EncNet(nn.Module):
|
|
|
233
233
|
References
|
|
234
234
|
----------
|
|
235
235
|
.. [Zhang2018] Zhang, H. et al. 2018.
|
|
236
|
-
|
|
236
|
+
Context Encoding for Semantic Segmentation. CVPR 2018.
|
|
237
237
|
"""
|
|
238
238
|
|
|
239
239
|
def __init__(self, in_channels: int, n_codewords: int):
|
|
@@ -290,7 +290,7 @@ class ECA(nn.Module):
|
|
|
290
290
|
References
|
|
291
291
|
----------
|
|
292
292
|
.. [Wang2021] Wang, Q. et al., 2021. ECA-Net: Efficient Channel Attention
|
|
293
|
-
|
|
293
|
+
for Deep Convolutional Neural Networks. CVPR 2021.
|
|
294
294
|
"""
|
|
295
295
|
|
|
296
296
|
def __init__(self, in_channels: int, kernel_size: int):
|
|
@@ -341,8 +341,8 @@ class GatherExcite(nn.Module):
|
|
|
341
341
|
References
|
|
342
342
|
----------
|
|
343
343
|
.. [Hu2018b] Hu, J., Albanie, S., Sun, G., Vedaldi, A., 2018.
|
|
344
|
-
|
|
345
|
-
|
|
344
|
+
Gather-Excite: Exploiting Feature Context in Convolutional Neural Networks.
|
|
345
|
+
NeurIPS 2018.
|
|
346
346
|
"""
|
|
347
347
|
|
|
348
348
|
def __init__(
|
|
@@ -410,7 +410,7 @@ class GCT(nn.Module):
|
|
|
410
410
|
References
|
|
411
411
|
----------
|
|
412
412
|
.. [Yang2020] Yang, Z. Linchao, Z., Wu, Y., Yang, Y., 2020.
|
|
413
|
-
|
|
413
|
+
Gated Channel Transformation for Visual Recognition. CVPR 2020.
|
|
414
414
|
"""
|
|
415
415
|
|
|
416
416
|
def __init__(self, in_channels: int):
|
|
@@ -455,7 +455,7 @@ class SRM(nn.Module):
|
|
|
455
455
|
References
|
|
456
456
|
----------
|
|
457
457
|
.. [Lee2019] Lee, H., Kim, H., Nam, H., 2019. SRM: A Style-based
|
|
458
|
-
|
|
458
|
+
Recalibration Module for Convolutional Neural Networks. ICCV 2019.
|
|
459
459
|
"""
|
|
460
460
|
|
|
461
461
|
def __init__(
|
|
@@ -520,7 +520,7 @@ class CBAM(nn.Module):
|
|
|
520
520
|
References
|
|
521
521
|
----------
|
|
522
522
|
.. [Woo2018] Woo, S., Park, J., Lee, J., Kweon, I., 2018.
|
|
523
|
-
|
|
523
|
+
CBAM: Convolutional Block Attention Module. ECCV 2018.
|
|
524
524
|
"""
|
|
525
525
|
|
|
526
526
|
def __init__(self, in_channels: int, reduction_rate: int, kernel_size: int):
|
braindecode/modules/blocks.py
CHANGED
|
@@ -37,8 +37,8 @@ class MLP(nn.Sequential):
|
|
|
37
37
|
:math:`a_i` are called activation functions. The trainable parameters of an
|
|
38
38
|
MLP are its weights and biases :math:`\\phi = \{W_i, b_i | i = 1, \dots, L\}`.
|
|
39
39
|
|
|
40
|
-
Parameters
|
|
41
|
-
|
|
40
|
+
Parameters
|
|
41
|
+
----------
|
|
42
42
|
in_features: int
|
|
43
43
|
Number of input features.
|
|
44
44
|
hidden_features: Sequential[int] (default=None)
|
|
@@ -49,7 +49,7 @@ class MLP(nn.Sequential):
|
|
|
49
49
|
out_features: int (default=None)
|
|
50
50
|
Number of output features, if None, set to in_features.
|
|
51
51
|
act_layer: nn.GELU (default)
|
|
52
|
-
The activation function constructor. If
|
|
52
|
+
The activation function constructor. If ``None``, use
|
|
53
53
|
:class:`torch.nn.GELU` instead.
|
|
54
54
|
drop: float (default=0.0)
|
|
55
55
|
Dropout rate.
|
braindecode/modules/filter.py
CHANGED
|
@@ -17,9 +17,8 @@ class FilterBankLayer(nn.Module):
|
|
|
17
17
|
It uses MNE's `create_filter` function to create the band-specific filters and
|
|
18
18
|
applies them to multi-channel time-series data. Each filter in the bank corresponds to a
|
|
19
19
|
specific frequency band and is applied to all channels of the input data. The filtering is
|
|
20
|
-
performed using FFT-based convolution via the
|
|
21
|
-
|
|
22
|
-
:func:`torchaudio.functional if the method is IIR.
|
|
20
|
+
performed using FFT-based convolution via the ``torchaudio.functional`` if the method is FIR,
|
|
21
|
+
and ``torchaudio.functional`` if the method is IIR.
|
|
23
22
|
|
|
24
23
|
The default configuration creates 9 non-overlapping frequency bands with a 4 Hz bandwidth,
|
|
25
24
|
spanning from 4 Hz to 40 Hz (i.e., 4-8 Hz, 8-12 Hz, ..., 36-40 Hz). This setup is based on the
|
braindecode/modules/layers.py
CHANGED
|
@@ -70,26 +70,27 @@ class TimeDistributed(nn.Module):
|
|
|
70
70
|
class DropPath(nn.Module):
|
|
71
71
|
"""Drop paths, also known as Stochastic Depth, per sample.
|
|
72
72
|
|
|
73
|
-
|
|
73
|
+
When applied in main path of residual blocks.
|
|
74
74
|
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
75
|
+
Parameters
|
|
76
|
+
----------
|
|
77
|
+
drop_prob: float (default=None)
|
|
78
|
+
Drop path probability (should be in range 0-1).
|
|
79
79
|
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
80
|
+
Notes
|
|
81
|
+
-----
|
|
82
|
+
Code copied and modified from VISSL facebookresearch:
|
|
83
83
|
https://github.com/facebookresearch/vissl/blob/0b5d6a94437bc00baed112ca90c9d78c6ccfbafb/vissl/models/model_helpers.py#L676
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
84
|
+
|
|
85
|
+
All rights reserved.
|
|
86
|
+
|
|
87
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
88
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
89
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
90
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
91
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
92
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
93
|
+
SOFTWARE.
|
|
93
94
|
"""
|
|
94
95
|
|
|
95
96
|
def __init__(self, drop_prob=None):
|
|
@@ -1,3 +1,16 @@
|
|
|
1
|
+
from .eegprep_preprocess import (
|
|
2
|
+
EEGPrep,
|
|
3
|
+
ReinterpolateRemovedChannels,
|
|
4
|
+
RemoveBadChannels,
|
|
5
|
+
RemoveBadChannelsNoLocs,
|
|
6
|
+
RemoveBadWindows,
|
|
7
|
+
RemoveBursts,
|
|
8
|
+
RemoveCommonAverageReference,
|
|
9
|
+
RemoveDCOffset,
|
|
10
|
+
RemoveDrifts,
|
|
11
|
+
RemoveFlatChannels,
|
|
12
|
+
Resampling,
|
|
13
|
+
)
|
|
1
14
|
from .mne_preprocess import ( # type: ignore[attr-defined]
|
|
2
15
|
Crop,
|
|
3
16
|
DropChannels,
|
|
@@ -31,6 +44,17 @@ __all__ = [
|
|
|
31
44
|
"Filter",
|
|
32
45
|
"Pick",
|
|
33
46
|
"Crop",
|
|
47
|
+
"EEGPrep",
|
|
48
|
+
"RemoveDCOffset",
|
|
49
|
+
"Resampling",
|
|
50
|
+
"RemoveFlatChannels",
|
|
51
|
+
"RemoveDrifts",
|
|
52
|
+
"RemoveBadChannels",
|
|
53
|
+
"RemoveBadChannelsNoLocs",
|
|
54
|
+
"RemoveBursts",
|
|
55
|
+
"RemoveBadWindows",
|
|
56
|
+
"ReinterpolateRemovedChannels",
|
|
57
|
+
"RemoveCommonAverageReference",
|
|
34
58
|
"create_windows_from_events",
|
|
35
59
|
"create_fixed_length_windows",
|
|
36
60
|
"create_windows_from_target_channels",
|