braindecode 1.2.0.dev168908090__py3-none-any.whl → 1.2.0.dev175267687__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 +1 -1
- 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 +15 -12
- braindecode/models/eeginception_erp.py +76 -7
- braindecode/models/eeginception_mi.py +2 -0
- braindecode/models/eegnet.py +25 -189
- braindecode/models/eegnex.py +113 -6
- braindecode/models/eegsimpleconv.py +2 -0
- braindecode/models/eegtcnet.py +1 -1
- braindecode/models/sccnet.py +79 -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/version.py +1 -1
- {braindecode-1.2.0.dev168908090.dist-info → braindecode-1.2.0.dev175267687.dist-info}/METADATA +7 -2
- {braindecode-1.2.0.dev168908090.dist-info → braindecode-1.2.0.dev175267687.dist-info}/RECORD +32 -32
- braindecode/models/eegresnet.py +0 -362
- {braindecode-1.2.0.dev168908090.dist-info → braindecode-1.2.0.dev175267687.dist-info}/WHEEL +0 -0
- {braindecode-1.2.0.dev168908090.dist-info → braindecode-1.2.0.dev175267687.dist-info}/licenses/LICENSE.txt +0 -0
- {braindecode-1.2.0.dev168908090.dist-info → braindecode-1.2.0.dev175267687.dist-info}/licenses/NOTICE.txt +0 -0
- {braindecode-1.2.0.dev168908090.dist-info → braindecode-1.2.0.dev175267687.dist-info}/top_level.txt +0 -0
|
@@ -0,0 +1,218 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import random
|
|
4
|
+
from pathlib import Path
|
|
5
|
+
from typing import Callable, Sequence
|
|
6
|
+
|
|
7
|
+
import mne_bids
|
|
8
|
+
from torch.utils.data import IterableDataset, get_worker_info
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
class BIDSIterableDataset(IterableDataset):
|
|
12
|
+
"""Dataset for loading BIDS.
|
|
13
|
+
|
|
14
|
+
.. warning::
|
|
15
|
+
This class is experimental and may change in the future.
|
|
16
|
+
|
|
17
|
+
.. warning::
|
|
18
|
+
This dataset is not consistent with the Braindecode API.
|
|
19
|
+
|
|
20
|
+
This class has the same parameters as the :func:`mne_bids.find_matching_paths` function
|
|
21
|
+
as it will be used to find the files to load. The default ``extensions`` parameter was changed.
|
|
22
|
+
|
|
23
|
+
More information on BIDS (Brain Imaging Data Structure)
|
|
24
|
+
can be found at https://bids.neuroimaging.io
|
|
25
|
+
|
|
26
|
+
Examples
|
|
27
|
+
--------
|
|
28
|
+
>>> from braindecode.datasets import BaseDataset, BaseConcatDataset
|
|
29
|
+
>>> from braindecode.datasets.bids import BIDSIterableDataset, _description_from_bids_path
|
|
30
|
+
>>> from braindecode.preprocessing import create_fixed_length_windows
|
|
31
|
+
>>>
|
|
32
|
+
>>> def my_reader_fn(path):
|
|
33
|
+
... raw = mne_bids.read_raw_bids(path)
|
|
34
|
+
... desc = _description_from_bids_path(path)
|
|
35
|
+
... ds = BaseDataset(raw, description=desc)
|
|
36
|
+
... windows_ds = create_fixed_length_windows(
|
|
37
|
+
... BaseConcatDataset([ds]),
|
|
38
|
+
... window_size_samples=400,
|
|
39
|
+
... window_stride_samples=200,
|
|
40
|
+
... )
|
|
41
|
+
... return windows_ds
|
|
42
|
+
>>>
|
|
43
|
+
>>> dataset = BIDSIterableDataset(
|
|
44
|
+
... reader_fn=my_reader_fn,
|
|
45
|
+
... root="root/of/my/bids/dataset/",
|
|
46
|
+
... )
|
|
47
|
+
|
|
48
|
+
Parameters
|
|
49
|
+
----------
|
|
50
|
+
reader_fn : Callable[[mne_bids.BIDSPath], Sequence]
|
|
51
|
+
A function that takes a BIDSPath and returns a dataset.
|
|
52
|
+
pool_size : int
|
|
53
|
+
The number of recordings to read and sample from.
|
|
54
|
+
bids_paths : list[mne_bids.BIDSPath] | None
|
|
55
|
+
A list of BIDSPaths to load. If None, will use the paths found by
|
|
56
|
+
:func:`mne_bids.find_matching_paths` and the arguments below.
|
|
57
|
+
root : pathlib.Path | str
|
|
58
|
+
The root of the BIDS path.
|
|
59
|
+
subjects : str | array-like of str | None
|
|
60
|
+
The subject ID. Corresponds to "sub".
|
|
61
|
+
sessions : str | array-like of str | None
|
|
62
|
+
The acquisition session. Corresponds to "ses".
|
|
63
|
+
tasks : str | array-like of str | None
|
|
64
|
+
The experimental task. Corresponds to "task".
|
|
65
|
+
acquisitions: str | array-like of str | None
|
|
66
|
+
The acquisition parameters. Corresponds to "acq".
|
|
67
|
+
runs : str | array-like of str | None
|
|
68
|
+
The run number. Corresponds to "run".
|
|
69
|
+
processings : str | array-like of str | None
|
|
70
|
+
The processing label. Corresponds to "proc".
|
|
71
|
+
recordings : str | array-like of str | None
|
|
72
|
+
The recording name. Corresponds to "rec".
|
|
73
|
+
spaces : str | array-like of str | None
|
|
74
|
+
The coordinate space for anatomical and sensor location
|
|
75
|
+
files (e.g., ``*_electrodes.tsv``, ``*_markers.mrk``).
|
|
76
|
+
Corresponds to "space".
|
|
77
|
+
Note that valid values for ``space`` must come from a list
|
|
78
|
+
of BIDS keywords as described in the BIDS specification.
|
|
79
|
+
splits : str | array-like of str | None
|
|
80
|
+
The split of the continuous recording file for ``.fif`` data.
|
|
81
|
+
Corresponds to "split".
|
|
82
|
+
descriptions : str | array-like of str | None
|
|
83
|
+
This corresponds to the BIDS entity ``desc``. It is used to provide
|
|
84
|
+
additional information for derivative data, e.g., preprocessed data
|
|
85
|
+
may be assigned ``description='cleaned'``.
|
|
86
|
+
suffixes : str | array-like of str | None
|
|
87
|
+
The filename suffix. This is the entity after the
|
|
88
|
+
last ``_`` before the extension. E.g., ``'channels'``.
|
|
89
|
+
The following filename suffix's are accepted:
|
|
90
|
+
'meg', 'markers', 'eeg', 'ieeg', 'T1w',
|
|
91
|
+
'participants', 'scans', 'electrodes', 'coordsystem',
|
|
92
|
+
'channels', 'events', 'headshape', 'digitizer',
|
|
93
|
+
'beh', 'physio', 'stim'
|
|
94
|
+
extensions : str | array-like of str | None
|
|
95
|
+
The extension of the filename. E.g., ``'.json'``.
|
|
96
|
+
By default, uses the ones accepted by :func:`mne_bids.read_raw_bids`.
|
|
97
|
+
datatypes : str | array-like of str | None
|
|
98
|
+
The BIDS data type, e.g., ``'anat'``, ``'func'``, ``'eeg'``, ``'meg'``,
|
|
99
|
+
``'ieeg'``.
|
|
100
|
+
check : bool
|
|
101
|
+
If ``True``, only returns paths that conform to BIDS. If ``False``
|
|
102
|
+
(default), the ``.check`` attribute of the returned
|
|
103
|
+
:class:`mne_bids.BIDSPath` object will be set to ``True`` for paths that
|
|
104
|
+
do conform to BIDS, and to ``False`` for those that don't.
|
|
105
|
+
preload : bool
|
|
106
|
+
If True, preload the data. Defaults to False.
|
|
107
|
+
n_jobs : int
|
|
108
|
+
Number of jobs to run in parallel. Defaults to 1.
|
|
109
|
+
"""
|
|
110
|
+
|
|
111
|
+
def __init__(
|
|
112
|
+
self,
|
|
113
|
+
reader_fn: Callable[[mne_bids.BIDSPath], Sequence],
|
|
114
|
+
pool_size: int = 4,
|
|
115
|
+
bids_paths: list[mne_bids.BIDSPath] | None = None,
|
|
116
|
+
root: Path | str | None = None,
|
|
117
|
+
subjects: str | list[str] | None = None,
|
|
118
|
+
sessions: str | list[str] | None = None,
|
|
119
|
+
tasks: str | list[str] | None = None,
|
|
120
|
+
acquisitions: str | list[str] | None = None,
|
|
121
|
+
runs: str | list[str] | None = None,
|
|
122
|
+
processings: str | list[str] | None = None,
|
|
123
|
+
recordings: str | list[str] | None = None,
|
|
124
|
+
spaces: str | list[str] | None = None,
|
|
125
|
+
splits: str | list[str] | None = None,
|
|
126
|
+
descriptions: str | list[str] | None = None,
|
|
127
|
+
suffixes: str | list[str] | None = None,
|
|
128
|
+
extensions: str | list[str] | None = [
|
|
129
|
+
".con",
|
|
130
|
+
".sqd",
|
|
131
|
+
".pdf",
|
|
132
|
+
".fif",
|
|
133
|
+
".ds",
|
|
134
|
+
".vhdr",
|
|
135
|
+
".set",
|
|
136
|
+
".edf",
|
|
137
|
+
".bdf",
|
|
138
|
+
".EDF",
|
|
139
|
+
".snirf",
|
|
140
|
+
".cdt",
|
|
141
|
+
".mef",
|
|
142
|
+
".nwb",
|
|
143
|
+
],
|
|
144
|
+
datatypes: str | list[str] | None = None,
|
|
145
|
+
check: bool = False,
|
|
146
|
+
):
|
|
147
|
+
if bids_paths is None:
|
|
148
|
+
bids_paths = mne_bids.find_matching_paths(
|
|
149
|
+
root=root,
|
|
150
|
+
subjects=subjects,
|
|
151
|
+
sessions=sessions,
|
|
152
|
+
tasks=tasks,
|
|
153
|
+
acquisitions=acquisitions,
|
|
154
|
+
runs=runs,
|
|
155
|
+
processings=processings,
|
|
156
|
+
recordings=recordings,
|
|
157
|
+
spaces=spaces,
|
|
158
|
+
splits=splits,
|
|
159
|
+
descriptions=descriptions,
|
|
160
|
+
suffixes=suffixes,
|
|
161
|
+
extensions=extensions,
|
|
162
|
+
datatypes=datatypes,
|
|
163
|
+
check=check,
|
|
164
|
+
ignore_json=True,
|
|
165
|
+
)
|
|
166
|
+
# Filter out _epo.fif files:
|
|
167
|
+
bids_paths = [
|
|
168
|
+
bids_path
|
|
169
|
+
for bids_path in bids_paths
|
|
170
|
+
if not (bids_path.suffix == "epo" and bids_path.extension == ".fif")
|
|
171
|
+
]
|
|
172
|
+
self.bids_paths = bids_paths
|
|
173
|
+
self.reader_fn = reader_fn
|
|
174
|
+
self.pool_size = pool_size
|
|
175
|
+
|
|
176
|
+
def __add__(self, other):
|
|
177
|
+
assert isinstance(other, BIDSIterableDataset)
|
|
178
|
+
return BIDSIterableDataset(
|
|
179
|
+
reader_fn=self.reader_fn,
|
|
180
|
+
bids_paths=self.bids_paths + other.bids_paths,
|
|
181
|
+
pool_size=self.pool_size,
|
|
182
|
+
)
|
|
183
|
+
|
|
184
|
+
def __iadd__(self, other):
|
|
185
|
+
assert isinstance(other, BIDSIterableDataset)
|
|
186
|
+
self.bids_paths += other.bids_paths
|
|
187
|
+
return self
|
|
188
|
+
|
|
189
|
+
def __iter__(self):
|
|
190
|
+
worker_info = get_worker_info()
|
|
191
|
+
if worker_info is None: # single-process data loading, return the full iterator
|
|
192
|
+
bids_paths = self.bids_paths
|
|
193
|
+
else: # in a worker process
|
|
194
|
+
# split workload
|
|
195
|
+
bids_paths = self.bids_paths[worker_info.id :: worker_info.num_workers]
|
|
196
|
+
|
|
197
|
+
pool = []
|
|
198
|
+
end = False
|
|
199
|
+
paths_it = iter(random.sample(bids_paths, k=len(bids_paths)))
|
|
200
|
+
while not (end and len(pool) == 0):
|
|
201
|
+
while not end and len(pool) < self.pool_size:
|
|
202
|
+
try:
|
|
203
|
+
bids_path = next(paths_it)
|
|
204
|
+
ds = self.reader_fn(bids_path)
|
|
205
|
+
if ds is None:
|
|
206
|
+
print(f"Skipping {bids_path} as it is too short.")
|
|
207
|
+
continue
|
|
208
|
+
idx = iter(random.sample(range(len(ds)), k=len(ds)))
|
|
209
|
+
pool.append((ds, idx))
|
|
210
|
+
except StopIteration:
|
|
211
|
+
end = True
|
|
212
|
+
i_pool = random.randint(0, len(pool) - 1)
|
|
213
|
+
ds, idx = pool[i_pool]
|
|
214
|
+
try:
|
|
215
|
+
i_ds = next(idx)
|
|
216
|
+
yield ds[i_ds]
|
|
217
|
+
except StopIteration:
|
|
218
|
+
pool.pop(i_pool)
|
braindecode/models/__init__.py
CHANGED
|
@@ -4,6 +4,7 @@ Some predefined network architectures for EEG decoding.
|
|
|
4
4
|
|
|
5
5
|
from .atcnet import ATCNet
|
|
6
6
|
from .attentionbasenet import AttentionBaseNet
|
|
7
|
+
from .attn_sleep import AttnSleep
|
|
7
8
|
from .base import EEGModuleMixin
|
|
8
9
|
from .biot import BIOT
|
|
9
10
|
from .contrawr import ContraWR
|
|
@@ -15,9 +16,8 @@ from .eeginception_erp import EEGInceptionERP
|
|
|
15
16
|
from .eeginception_mi import EEGInceptionMI
|
|
16
17
|
from .eegitnet import EEGITNet
|
|
17
18
|
from .eegminer import EEGMiner
|
|
18
|
-
from .eegnet import
|
|
19
|
+
from .eegnet import EEGNet, EEGNetv4
|
|
19
20
|
from .eegnex import EEGNeX
|
|
20
|
-
from .eegresnet import EEGResNet
|
|
21
21
|
from .eegsimpleconv import EEGSimpleConv
|
|
22
22
|
from .eegtcnet import EEGTCNet
|
|
23
23
|
from .fbcnet import FBCNet
|
|
@@ -38,12 +38,11 @@ from .signal_jepa import (
|
|
|
38
38
|
from .sinc_shallow import SincShallowNet
|
|
39
39
|
from .sleep_stager_blanco_2020 import SleepStagerBlanco2020
|
|
40
40
|
from .sleep_stager_chambon_2018 import SleepStagerChambon2018
|
|
41
|
-
from .sleep_stager_eldele_2021 import SleepStagerEldele2021
|
|
42
41
|
from .sparcnet import SPARCNet
|
|
43
42
|
from .syncnet import SyncNet
|
|
44
43
|
from .tcn import BDTCN, TCN
|
|
45
44
|
from .tidnet import TIDNet
|
|
46
|
-
from .tsinception import
|
|
45
|
+
from .tsinception import TSception
|
|
47
46
|
from .usleep import USleep
|
|
48
47
|
from .util import _init_models_dict, models_mandatory_parameters
|
|
49
48
|
|
|
@@ -53,6 +52,7 @@ _init_models_dict()
|
|
|
53
52
|
|
|
54
53
|
__all__ = [
|
|
55
54
|
"ATCNet",
|
|
55
|
+
"AttnSleep",
|
|
56
56
|
"AttentionBaseNet",
|
|
57
57
|
"EEGModuleMixin",
|
|
58
58
|
"BIOT",
|
|
@@ -65,10 +65,9 @@ __all__ = [
|
|
|
65
65
|
"EEGInceptionMI",
|
|
66
66
|
"EEGITNet",
|
|
67
67
|
"EEGMiner",
|
|
68
|
-
"
|
|
68
|
+
"EEGNet",
|
|
69
69
|
"EEGNetv4",
|
|
70
70
|
"EEGNeX",
|
|
71
|
-
"EEGResNet",
|
|
72
71
|
"EEGSimpleConv",
|
|
73
72
|
"EEGTCNet",
|
|
74
73
|
"FBCNet",
|
|
@@ -87,13 +86,12 @@ __all__ = [
|
|
|
87
86
|
"SincShallowNet",
|
|
88
87
|
"SleepStagerBlanco2020",
|
|
89
88
|
"SleepStagerChambon2018",
|
|
90
|
-
"SleepStagerEldele2021",
|
|
91
89
|
"SPARCNet",
|
|
92
90
|
"SyncNet",
|
|
93
91
|
"BDTCN",
|
|
94
92
|
"TCN",
|
|
95
93
|
"TIDNet",
|
|
96
|
-
"
|
|
94
|
+
"TSception",
|
|
97
95
|
"USleep",
|
|
98
96
|
"_init_models_dict",
|
|
99
97
|
"models_mandatory_parameters",
|
braindecode/models/atcnet.py
CHANGED
|
@@ -138,7 +138,7 @@ class ATCNet(EEGModuleMixin, nn.Module):
|
|
|
138
138
|
.. rubric:: Usage and Configuration
|
|
139
139
|
|
|
140
140
|
- ``conv_block_n_filters (F1)``, ``conv_block_depth_mult (D)`` → capacity of the stem
|
|
141
|
-
(with ``F2 = F1·D`` feeding attention/TCN), dimensions aligned to ``F2``, like
|
|
141
|
+
(with ``F2 = F1·D`` feeding attention/TCN), dimensions aligned to ``F2``, like :class:`EEGNet`.
|
|
142
142
|
- Pool sizes ``P1,P2`` trade temporal resolution for stability/compute; they set
|
|
143
143
|
``T_c = T/(P1·P2)`` and thus window width ``T_w``.
|
|
144
144
|
- ``n_windows`` controls the ensemble over shifts (compute ∝ windows).
|
|
@@ -26,25 +26,150 @@ from braindecode.modules.attention import (
|
|
|
26
26
|
class AttentionBaseNet(EEGModuleMixin, nn.Module):
|
|
27
27
|
"""AttentionBaseNet from Wimpff M et al. (2023) [Martin2023]_.
|
|
28
28
|
|
|
29
|
+
:bdg-success:`Convolution` :bdg-info:`Small Attention`
|
|
30
|
+
|
|
29
31
|
.. figure:: https://content.cld.iop.org/journals/1741-2552/21/3/036020/revision2/jnead48b9f2_hr.jpg
|
|
30
|
-
|
|
31
|
-
|
|
32
|
+
:align: center
|
|
33
|
+
:alt: AttentionBaseNet Architecture
|
|
34
|
+
:width: 640px
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
.. rubric:: Architectural Overview
|
|
38
|
+
|
|
39
|
+
AttentionBaseNet is a *convolution-first* network with a *channel-attention* stage.
|
|
40
|
+
The end-to-end flow is:
|
|
41
|
+
|
|
42
|
+
- (i) :class:`_FeatureExtractor` learns a temporal filter bank and per-filter spatial
|
|
43
|
+
projections (depthwise across electrodes), then condenses time by pooling;
|
|
44
|
+
- (ii) **Channel Expansion** uses a ``1x1`` convolution to set the feature width;
|
|
45
|
+
- (iii) :class:`_ChannelAttentionBlock` refines features via depthwise–pointwise temporal
|
|
46
|
+
convs and an optional channel-attention module (SE/CBAM/ECA/…);
|
|
47
|
+
- (iv) **Classifier** flattens the sequence and applies a linear readout.
|
|
48
|
+
|
|
49
|
+
This design mirrors shallow CNN pipelines (EEGNet-style stem) but inserts a pluggable
|
|
50
|
+
attention unit that *re-weights channels* (and optionally temporal positions) before
|
|
51
|
+
classification.
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
.. rubric:: Macro Components
|
|
55
|
+
|
|
56
|
+
- :class:`_FeatureExtractor` **(Shallow conv stem → condensed feature map)**
|
|
57
|
+
|
|
58
|
+
- *Operations.*
|
|
59
|
+
- **Temporal conv** (:class:`torch.nn.Conv2d`) with kernel ``(1, L_t)`` creates a learned
|
|
60
|
+
FIR-like filter bank with ``n_temporal_filters`` maps.
|
|
61
|
+
- **Depthwise spatial conv** (:class:`torch.nn.Conv2d`, ``groups=n_temporal_filters``)
|
|
62
|
+
with kernel ``(n_chans, 1)`` learns per-filter spatial projections over the full montage.
|
|
63
|
+
- **BatchNorm → ELU → AvgPool → Dropout** stabilize and downsample time.
|
|
64
|
+
- Output shape: ``(B, F2, 1, T₁)`` with ``F2 = n_temporal_filters x spatial_expansion``.
|
|
65
|
+
|
|
66
|
+
*Interpretability/robustness.* Temporal kernels behave as analyzable FIR filters; the
|
|
67
|
+
depthwise spatial step yields rhythm-specific topographies. Pooling acts as a local
|
|
68
|
+
integrator that reduces variance on short EEG windows.
|
|
69
|
+
|
|
70
|
+
- **Channel Expansion**
|
|
71
|
+
|
|
72
|
+
- *Operations.*
|
|
73
|
+
- A ``1x1`` conv → BN → activation maps ``F2 → ch_dim`` without changing
|
|
74
|
+
the temporal length ``T₁`` (shape: ``(B, ch_dim, 1, T₁)``).
|
|
75
|
+
This sets the embedding width for the attention block.
|
|
76
|
+
|
|
77
|
+
- :class:`_ChannelAttentionBlock` **(temporal refinement + channel attention)**
|
|
78
|
+
|
|
79
|
+
- *Operations.*
|
|
80
|
+
- **Depthwise temporal conv** ``(1, L_a)`` (groups=``ch_dim``) + **pointwise ``1x1``**,
|
|
81
|
+
BN and activation → preserves shape ``(B, ch_dim, 1, T₁)`` while refining timing.
|
|
82
|
+
- **Optional attention module** (see *Additional Mechanisms*) applies channel reweighting
|
|
83
|
+
(some variants also apply temporal gating).
|
|
84
|
+
- **AvgPool (1, P₂)** with stride ``(1, S₂)`` and **Dropout** → outputs
|
|
85
|
+
``(B, ch_dim, 1, T₂)``.
|
|
86
|
+
|
|
87
|
+
*Role.* Emphasizes informative channels (and, in certain modes, salient time steps)
|
|
88
|
+
before the classifier; complements the convolutional priors with adaptive re-weighting.
|
|
89
|
+
|
|
90
|
+
- **Classifier (aggregation + readout)**
|
|
91
|
+
|
|
92
|
+
*Operations.* :class:`torch.nn.Flatten` → :class:`torch.nn.Linear` from
|
|
93
|
+
``(B, ch_dim·T₂)`` to classes.
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
.. rubric:: Convolutional Details
|
|
97
|
+
|
|
98
|
+
- **Temporal (where time-domain patterns are learned).**
|
|
99
|
+
Wide kernels in the stem (``(1, L_t)``) act as a learned filter bank for oscillatory
|
|
100
|
+
bands/transients; the attention block’s depthwise temporal conv (``(1, L_a)``) sharpens
|
|
101
|
+
short-term dynamics after downsampling. Pool sizes/strides (``P₁,S₁`` then ``P₂,S₂``)
|
|
102
|
+
set the token rate and effective temporal resolution.
|
|
103
|
+
|
|
104
|
+
- **Spatial (how electrodes are processed).**
|
|
105
|
+
A depthwise spatial conv with kernel ``(n_chans, 1)`` spans the full montage to
|
|
106
|
+
learn *per-temporal-filter* spatial projections (no cross-filter mixing at this step),
|
|
107
|
+
mirroring the interpretable spatial stage in shallow CNNs.
|
|
32
108
|
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
109
|
+
- **Spectral (how frequency content is captured).**
|
|
110
|
+
No explicit Fourier/wavelet transform is used in the stem—spectral selectivity
|
|
111
|
+
emerges from learned temporal kernels. When ``attention_mode="fca"``, a frequency
|
|
112
|
+
channel attention (DCT-based) summarizes frequencies to drive channel weights.
|
|
36
113
|
|
|
37
|
-
The paper and original code with more details about the methodological
|
|
38
|
-
choices are available at the [Martin2023]_ and [MartinCode]_.
|
|
39
114
|
|
|
40
|
-
|
|
41
|
-
- Input Block that performs a temporal convolution and a spatial
|
|
42
|
-
convolution.
|
|
43
|
-
- Channel Expansion that modifies the number of channels.
|
|
44
|
-
- An attention block that performs channel attention with several
|
|
45
|
-
options
|
|
46
|
-
- ClassificationHead
|
|
115
|
+
.. rubric:: Attention / Sequential Modules
|
|
47
116
|
|
|
117
|
+
- **Type.** Channel attention chosen by ``attention_mode`` (SE, ECA, CBAM, CAT, GSoP,
|
|
118
|
+
EncNet, GE, GCT, SRM, CATLite). Most operate purely on channels; CBAM/CAT additionally
|
|
119
|
+
include temporal attention.
|
|
120
|
+
|
|
121
|
+
- **Shapes.** Input/Output around attention: ``(B, ch_dim, 1, T₁)``. Re-arrangements
|
|
122
|
+
(if any) are internal to the module; the block returns the same shape before pooling.
|
|
123
|
+
|
|
124
|
+
- **Role.** Re-weights channels (and optionally time) to highlight informative sources
|
|
125
|
+
and suppress distractors, improving SNR ahead of the linear head.
|
|
126
|
+
|
|
127
|
+
|
|
128
|
+
.. rubric:: Additional Mechanisms
|
|
129
|
+
|
|
130
|
+
- **Attention variants at a glance.**
|
|
131
|
+
- ``"se"``: Squeeze-and-Excitation (global pooling → bottleneck → gates).
|
|
132
|
+
- ``"gsop"``: Global second-order pooling (covariance-aware channel weights).
|
|
133
|
+
- ``"fca"``: Frequency Channel Attention (DCT summary; uses ``seq_len`` and ``freq_idx``).
|
|
134
|
+
- ``"encnet"``: EncNet with learned codewords (uses ``n_codewords``).
|
|
135
|
+
- ``"eca"``: Efficient Channel Attention (local 1-D conv over channel descriptor; uses ``kernel_size``).
|
|
136
|
+
- ``"ge"``: Gather–Excite (context pooling with optional MLP; can use ``extra_params``).
|
|
137
|
+
- ``"gct"``: Gated Channel Transformation (global context normalization + gating).
|
|
138
|
+
- ``"srm"``: Style-based recalibration (mean–std descriptors; optional MLP).
|
|
139
|
+
- ``"cbam"``: Channel then temporal attention (uses ``kernel_size``).
|
|
140
|
+
- ``"cat"`` / ``"catlite"``: Collaborative (channel ± temporal) attention; *lite* omits temporal.
|
|
141
|
+
- **Auto-compatibility on short inputs.**
|
|
142
|
+
|
|
143
|
+
If the input duration is too short for the configured kernels/pools, the implementation
|
|
144
|
+
**automatically rescales** temporal lengths/strides downward (with a warning) to keep
|
|
145
|
+
shapes valid and preserve the pipeline semantics.
|
|
146
|
+
|
|
147
|
+
|
|
148
|
+
.. rubric:: Usage and Configuration
|
|
149
|
+
|
|
150
|
+
- ``n_temporal_filters``, ``temporal_filter_length`` and ``spatial_expansion``:
|
|
151
|
+
control the capacity and the number of spatial projections in the stem.
|
|
152
|
+
- ``pool_length_inp``, ``pool_stride_inp`` then ``pool_length``, ``pool_stride``:
|
|
153
|
+
trade temporal resolution for compute; they determine the final sequence length ``T₂``.
|
|
154
|
+
- ``ch_dim``: width after the ``1x1`` expansion and the effective embedding size for attention.
|
|
155
|
+
- ``attention_mode`` + its specific hyperparameters (``reduction_rate``,
|
|
156
|
+
``kernel_size``, ``seq_len``, ``freq_idx``, ``n_codewords``, ``use_mlp``):
|
|
157
|
+
select and tune the reweighting mechanism.
|
|
158
|
+
- ``drop_prob_inp`` and ``drop_prob_attn``: regularize stem and attention stages.
|
|
159
|
+
- **Training tips.**
|
|
160
|
+
|
|
161
|
+
Start with moderate pooling (e.g., ``P₁=75,S₁=15``) and ELU activations; enable attention
|
|
162
|
+
only after the stem learns stable filters. For small datasets, prefer simpler modes
|
|
163
|
+
(``"se"``, ``"eca"``) before heavier ones (``"gsop"``, ``"encnet"``).
|
|
164
|
+
|
|
165
|
+
Notes
|
|
166
|
+
-----
|
|
167
|
+
- Sequence length after each stage is computed internally; the final classifier expects
|
|
168
|
+
a flattened ``ch_dim x T₂`` vector.
|
|
169
|
+
- Attention operates on *channel* dimension by design; temporal gating exists only in
|
|
170
|
+
specific variants (CBAM/CAT).
|
|
171
|
+
- The paper and original code with more details about the methodological
|
|
172
|
+
choices are available at the [Martin2023]_ and [MartinCode]_.
|
|
48
173
|
.. versionadded:: 0.9
|
|
49
174
|
|
|
50
175
|
Parameters
|
|
@@ -73,18 +198,18 @@ class AttentionBaseNet(EEGModuleMixin, nn.Module):
|
|
|
73
198
|
the depth of the network after the initial layer. Default is 16.
|
|
74
199
|
attention_mode : str, optional
|
|
75
200
|
The type of attention mechanism to apply. If `None`, no attention is applied.
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
201
|
+
- "se" for Squeeze-and-excitation network
|
|
202
|
+
- "gsop" for Global Second-Order Pooling
|
|
203
|
+
- "fca" for Frequency Channel Attention Network
|
|
204
|
+
- "encnet" for context encoding module
|
|
205
|
+
- "eca" for Efficient channel attention for deep convolutional neural networks
|
|
206
|
+
- "ge" for Gather-Excite
|
|
207
|
+
- "gct" for Gated Channel Transformation
|
|
208
|
+
- "srm" for Style-based Recalibration Module
|
|
209
|
+
- "cbam" for Convolutional Block Attention Module
|
|
210
|
+
- "cat" for Learning to collaborate channel and temporal attention
|
|
211
|
+
from multi-information fusion
|
|
212
|
+
- "catlite" for Learning to collaborate channel attention
|
|
88
213
|
from multi-information fusion (lite version, cat w/o temporal attention)
|
|
89
214
|
pool_length : int, default=8
|
|
90
215
|
The length of the window for the average pooling operation.
|
|
@@ -8,18 +8,19 @@ from copy import deepcopy
|
|
|
8
8
|
|
|
9
9
|
import torch
|
|
10
10
|
import torch.nn.functional as F
|
|
11
|
+
from mne.utils import deprecated
|
|
11
12
|
from torch import nn
|
|
12
13
|
|
|
13
14
|
from braindecode.models.base import EEGModuleMixin
|
|
14
15
|
from braindecode.modules import CausalConv1d
|
|
15
16
|
|
|
16
17
|
|
|
17
|
-
class
|
|
18
|
+
class AttnSleep(EEGModuleMixin, nn.Module):
|
|
18
19
|
"""Sleep Staging Architecture from Eldele et al. (2021) [Eldele2021]_.
|
|
19
20
|
|
|
20
21
|
.. figure:: https://raw.githubusercontent.com/emadeldeen24/AttnSleep/refs/heads/main/imgs/AttnSleep.png
|
|
21
22
|
:align: center
|
|
22
|
-
:alt:
|
|
23
|
+
:alt: AttnSleep Architecture
|
|
23
24
|
|
|
24
25
|
Attention based Neural Net for sleep staging as described in [Eldele2021]_.
|
|
25
26
|
The code for the paper and this model is also available at [1]_.
|
|
@@ -533,3 +534,12 @@ class _PositionwiseFeedForward(nn.Module):
|
|
|
533
534
|
def forward(self, x: torch.Tensor) -> torch.Tensor:
|
|
534
535
|
"""Implements FFN equation."""
|
|
535
536
|
return self.w_2(self.dropout(self.activate(self.w_1(x))))
|
|
537
|
+
|
|
538
|
+
|
|
539
|
+
@deprecated(
|
|
540
|
+
"`SleepStagerEldele2021` was renamed to `AttnSleep` in v1.12 to follow original author's name; this alias will be removed in v1.14."
|
|
541
|
+
)
|
|
542
|
+
class SleepStagerEldele2021(AttnSleep):
|
|
543
|
+
"""Deprecated alias for SleepStagerEldele2021."""
|
|
544
|
+
|
|
545
|
+
pass
|
braindecode/models/ctnet.py
CHANGED
|
@@ -39,7 +39,7 @@ class CTNet(EEGModuleMixin, nn.Module):
|
|
|
39
39
|
The architecture consists of three main components:
|
|
40
40
|
|
|
41
41
|
1. **Convolutional Module**:
|
|
42
|
-
- Apply
|
|
42
|
+
- Apply :class:`EEGNet` to perform some feature extraction, denoted here as
|
|
43
43
|
_PatchEmbeddingEEGNet module.
|
|
44
44
|
|
|
45
45
|
2. **Transformer Encoder Module**:
|
braindecode/models/deep4.py
CHANGED
|
@@ -19,9 +19,13 @@ from braindecode.modules import (
|
|
|
19
19
|
class Deep4Net(EEGModuleMixin, nn.Sequential):
|
|
20
20
|
"""Deep ConvNet model from Schirrmeister et al (2017) [Schirrmeister2017]_.
|
|
21
21
|
|
|
22
|
-
|
|
22
|
+
:bdg-success:`Convolution`
|
|
23
|
+
|
|
24
|
+
.. figure:: https://onlinelibrary.wiley.com/cms/asset/fc200ccc-d8c4-45b4-8577-56ce4d15999a/hbm23730-fig-0001-m.jpg
|
|
23
25
|
:align: center
|
|
24
|
-
:alt:
|
|
26
|
+
:alt: Deep4Net Architecture
|
|
27
|
+
:width: 600px
|
|
28
|
+
|
|
25
29
|
|
|
26
30
|
Model described in [Schirrmeister2017]_.
|
|
27
31
|
|