braindecode 1.2.0.dev184328194__py3-none-any.whl → 1.3.0.dev171178473__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/augmentation/functional.py +154 -54
- braindecode/augmentation/transforms.py +2 -2
- braindecode/datasets/__init__.py +10 -2
- braindecode/datasets/base.py +116 -152
- braindecode/datasets/bcicomp.py +4 -4
- braindecode/datasets/bids.py +3 -3
- braindecode/datasets/experimental.py +218 -0
- 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 +4 -3
- braindecode/datasets/sleep_physionet.py +2 -2
- braindecode/datasets/tuh.py +2 -2
- braindecode/datasets/xy.py +2 -2
- braindecode/datautil/serialization.py +18 -13
- braindecode/eegneuralnet.py +2 -0
- braindecode/functional/functions.py +6 -2
- braindecode/functional/initialization.py +2 -3
- braindecode/models/__init__.py +12 -8
- braindecode/models/atcnet.py +156 -17
- braindecode/models/attentionbasenet.py +148 -16
- braindecode/models/{sleep_stager_eldele_2021.py → attn_sleep.py} +12 -2
- braindecode/models/base.py +280 -2
- braindecode/models/bendr.py +469 -0
- braindecode/models/biot.py +3 -1
- braindecode/models/ctnet.py +7 -4
- braindecode/models/deep4.py +6 -2
- braindecode/models/deepsleepnet.py +127 -5
- braindecode/models/eegconformer.py +114 -15
- braindecode/models/eeginception_erp.py +82 -7
- braindecode/models/eeginception_mi.py +2 -0
- braindecode/models/eegnet.py +64 -177
- braindecode/models/eegnex.py +113 -6
- braindecode/models/eegsimpleconv.py +2 -0
- braindecode/models/eegtcnet.py +1 -1
- braindecode/models/labram.py +188 -84
- braindecode/models/patchedtransformer.py +640 -0
- braindecode/models/sccnet.py +81 -8
- braindecode/models/shallow_fbcsp.py +2 -0
- braindecode/models/signal_jepa.py +109 -27
- braindecode/models/sinc_shallow.py +10 -9
- 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/sstdpn.py +869 -0
- braindecode/models/summary.csv +42 -41
- braindecode/models/tidnet.py +2 -0
- braindecode/models/tsinception.py +15 -3
- braindecode/models/usleep.py +108 -9
- braindecode/models/util.py +8 -5
- 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 +42 -39
- 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.2.0.dev184328194.dist-info → braindecode-1.3.0.dev171178473.dist-info}/METADATA +12 -3
- braindecode-1.3.0.dev171178473.dist-info/RECORD +106 -0
- braindecode/models/eegresnet.py +0 -362
- braindecode-1.2.0.dev184328194.dist-info/RECORD +0 -101
- {braindecode-1.2.0.dev184328194.dist-info → braindecode-1.3.0.dev171178473.dist-info}/WHEEL +0 -0
- {braindecode-1.2.0.dev184328194.dist-info → braindecode-1.3.0.dev171178473.dist-info}/licenses/LICENSE.txt +0 -0
- {braindecode-1.2.0.dev184328194.dist-info → braindecode-1.3.0.dev171178473.dist-info}/licenses/NOTICE.txt +0 -0
- {braindecode-1.2.0.dev184328194.dist-info → braindecode-1.3.0.dev171178473.dist-info}/top_level.txt +0 -0
|
@@ -26,24 +26,151 @@ 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
|
|
32
97
|
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
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.
|
|
36
103
|
|
|
37
|
-
|
|
38
|
-
|
|
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.
|
|
39
108
|
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
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.
|
|
113
|
+
|
|
114
|
+
|
|
115
|
+
.. rubric:: Attention / Sequential Modules
|
|
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
|
+
|
|
132
|
+
- ``"se"``: Squeeze-and-Excitation (global pooling → bottleneck → gates).
|
|
133
|
+
- ``"gsop"``: Global second-order pooling (covariance-aware channel weights).
|
|
134
|
+
- ``"fca"``: Frequency Channel Attention (DCT summary; uses ``seq_len`` and ``freq_idx``).
|
|
135
|
+
- ``"encnet"``: EncNet with learned codewords (uses ``n_codewords``).
|
|
136
|
+
- ``"eca"``: Efficient Channel Attention (local 1-D conv over channel descriptor; uses ``kernel_size``).
|
|
137
|
+
- ``"ge"``: Gather–Excite (context pooling with optional MLP; can use ``extra_params``).
|
|
138
|
+
- ``"gct"``: Gated Channel Transformation (global context normalization + gating).
|
|
139
|
+
- ``"srm"``: Style-based recalibration (mean–std descriptors; optional MLP).
|
|
140
|
+
- ``"cbam"``: Channel then temporal attention (uses ``kernel_size``).
|
|
141
|
+
- ``"cat"`` / ``"catlite"``: Collaborative (channel ± temporal) attention; *lite* omits temporal.
|
|
142
|
+
|
|
143
|
+
**Auto-compatibility on short inputs:**
|
|
144
|
+
|
|
145
|
+
If the input duration is too short for the configured kernels/pools, the implementation
|
|
146
|
+
**automatically rescales** temporal lengths/strides downward (with a warning) to keep
|
|
147
|
+
shapes valid and preserve the pipeline semantics.
|
|
148
|
+
|
|
149
|
+
.. rubric:: Usage and Configuration
|
|
150
|
+
|
|
151
|
+
- ``n_temporal_filters``, ``temporal_filter_length`` and ``spatial_expansion``:
|
|
152
|
+
control the capacity and the number of spatial projections in the stem.
|
|
153
|
+
- ``pool_length_inp``, ``pool_stride_inp`` then ``pool_length``, ``pool_stride``:
|
|
154
|
+
trade temporal resolution for compute; they determine the final sequence length ``T₂``.
|
|
155
|
+
- ``ch_dim``: width after the ``1x1`` expansion and the effective embedding size for attention.
|
|
156
|
+
- ``attention_mode`` + its specific hyperparameters (``reduction_rate``,
|
|
157
|
+
``kernel_size``, ``seq_len``, ``freq_idx``, ``n_codewords``, ``use_mlp``):
|
|
158
|
+
select and tune the reweighting mechanism.
|
|
159
|
+
- ``drop_prob_inp`` and ``drop_prob_attn``: regularize stem and attention stages.
|
|
160
|
+
- **Training tips.**
|
|
161
|
+
|
|
162
|
+
Start with moderate pooling (e.g., ``P₁=75,S₁=15``) and ELU activations; enable attention
|
|
163
|
+
only after the stem learns stable filters. For small datasets, prefer simpler modes
|
|
164
|
+
(``"se"``, ``"eca"``) before heavier ones (``"gsop"``, ``"encnet"``).
|
|
165
|
+
|
|
166
|
+
Notes
|
|
167
|
+
-----
|
|
168
|
+
- Sequence length after each stage is computed internally; the final classifier expects
|
|
169
|
+
a flattened ``ch_dim x T₂`` vector.
|
|
170
|
+
- Attention operates on *channel* dimension by design; temporal gating exists only in
|
|
171
|
+
specific variants (CBAM/CAT).
|
|
172
|
+
- The paper and original code with more details about the methodological
|
|
173
|
+
choices are available at the [Martin2023]_ and [MartinCode]_.
|
|
47
174
|
|
|
48
175
|
.. versionadded:: 0.9
|
|
49
176
|
|
|
@@ -73,6 +200,7 @@ class AttentionBaseNet(EEGModuleMixin, nn.Module):
|
|
|
73
200
|
the depth of the network after the initial layer. Default is 16.
|
|
74
201
|
attention_mode : str, optional
|
|
75
202
|
The type of attention mechanism to apply. If `None`, no attention is applied.
|
|
203
|
+
|
|
76
204
|
- "se" for Squeeze-and-excitation network
|
|
77
205
|
- "gsop" for Global Second-Order Pooling
|
|
78
206
|
- "fca" for Frequency Channel Attention Network
|
|
@@ -83,9 +211,10 @@ class AttentionBaseNet(EEGModuleMixin, nn.Module):
|
|
|
83
211
|
- "srm" for Style-based Recalibration Module
|
|
84
212
|
- "cbam" for Convolutional Block Attention Module
|
|
85
213
|
- "cat" for Learning to collaborate channel and temporal attention
|
|
86
|
-
|
|
214
|
+
from multi-information fusion
|
|
87
215
|
- "catlite" for Learning to collaborate channel attention
|
|
88
|
-
|
|
216
|
+
from multi-information fusion (lite version, cat w/o temporal attention)
|
|
217
|
+
|
|
89
218
|
pool_length : int, default=8
|
|
90
219
|
The length of the window for the average pooling operation.
|
|
91
220
|
pool_stride : int, default=8
|
|
@@ -256,6 +385,8 @@ class AttentionBaseNet(EEGModuleMixin, nn.Module):
|
|
|
256
385
|
for k, pl, ps in zip(kernel_lengths, pool_lengths, pool_strides):
|
|
257
386
|
out = math.floor(out + 2 * (k // 2) - k + 1)
|
|
258
387
|
out = math.floor((out - pl) / ps + 1)
|
|
388
|
+
# Ensure output is at least 1 to avoid zero-sized tensors
|
|
389
|
+
out = max(1, out)
|
|
259
390
|
seq_lengths.append(int(out))
|
|
260
391
|
return seq_lengths
|
|
261
392
|
|
|
@@ -372,6 +503,7 @@ class _ChannelAttentionBlock(nn.Module):
|
|
|
372
503
|
----------
|
|
373
504
|
attention_mode : str, optional
|
|
374
505
|
The type of attention mechanism to apply. If `None`, no attention is applied.
|
|
506
|
+
|
|
375
507
|
- "se" for Squeeze-and-excitation network
|
|
376
508
|
- "gsop" for Global Second-Order Pooling
|
|
377
509
|
- "fca" for Frequency Channel Attention Network
|
|
@@ -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/base.py
CHANGED
|
@@ -5,15 +5,35 @@
|
|
|
5
5
|
|
|
6
6
|
from __future__ import annotations
|
|
7
7
|
|
|
8
|
+
import json
|
|
8
9
|
import warnings
|
|
9
10
|
from collections import OrderedDict
|
|
10
|
-
from
|
|
11
|
+
from pathlib import Path
|
|
12
|
+
from typing import Dict, Iterable, Optional, Type, Union
|
|
11
13
|
|
|
12
14
|
import numpy as np
|
|
13
15
|
import torch
|
|
14
16
|
from docstring_inheritance import NumpyDocstringInheritanceInitMeta
|
|
17
|
+
from mne.utils import _soft_import
|
|
15
18
|
from torchinfo import ModelStatistics, summary
|
|
16
19
|
|
|
20
|
+
from braindecode.version import __version__
|
|
21
|
+
|
|
22
|
+
huggingface_hub = _soft_import(
|
|
23
|
+
"huggingface_hub", "Hugging Face Hub integration", strict=False
|
|
24
|
+
)
|
|
25
|
+
|
|
26
|
+
HAS_HF_HUB = huggingface_hub is not False
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
class _BaseHubMixin:
|
|
30
|
+
pass
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
# Define base class for hub mixin
|
|
34
|
+
if HAS_HF_HUB:
|
|
35
|
+
_BaseHubMixin: Type = huggingface_hub.PyTorchModelHubMixin # type: ignore
|
|
36
|
+
|
|
17
37
|
|
|
18
38
|
def deprecated_args(obj, *old_new_args):
|
|
19
39
|
out_args = []
|
|
@@ -32,10 +52,14 @@ def deprecated_args(obj, *old_new_args):
|
|
|
32
52
|
return out_args
|
|
33
53
|
|
|
34
54
|
|
|
35
|
-
class EEGModuleMixin(metaclass=NumpyDocstringInheritanceInitMeta):
|
|
55
|
+
class EEGModuleMixin(_BaseHubMixin, metaclass=NumpyDocstringInheritanceInitMeta):
|
|
36
56
|
"""
|
|
37
57
|
Mixin class for all EEG models in braindecode.
|
|
38
58
|
|
|
59
|
+
This class integrates with Hugging Face Hub when the ``huggingface_hub`` package
|
|
60
|
+
is installed, enabling models to be pushed to and loaded from the Hub using
|
|
61
|
+
:func:`push_to_hub()` and :func:`from_pretrained()` methods.
|
|
62
|
+
|
|
39
63
|
Parameters
|
|
40
64
|
----------
|
|
41
65
|
n_outputs : int
|
|
@@ -62,8 +86,87 @@ class EEGModuleMixin(metaclass=NumpyDocstringInheritanceInitMeta):
|
|
|
62
86
|
-----
|
|
63
87
|
If some input signal-related parameters are not specified,
|
|
64
88
|
there will be an attempt to infer them from the other parameters.
|
|
89
|
+
|
|
90
|
+
.. rubric:: Hugging Face Hub integration
|
|
91
|
+
|
|
92
|
+
When the optional ``huggingface_hub`` package is installed, all models
|
|
93
|
+
automatically gain the ability to be pushed to and loaded from the
|
|
94
|
+
Hugging Face Hub. Install with::
|
|
95
|
+
|
|
96
|
+
pip install braindecode[hug]
|
|
97
|
+
|
|
98
|
+
**Pushing a model to the Hub:**
|
|
99
|
+
|
|
100
|
+
.. code-block:: python
|
|
101
|
+
|
|
102
|
+
from braindecode.models import EEGNetv4
|
|
103
|
+
|
|
104
|
+
# Train your model
|
|
105
|
+
model = EEGNetv4(n_chans=22, n_outputs=4, n_times=1000)
|
|
106
|
+
# ... training code ...
|
|
107
|
+
|
|
108
|
+
# Push to the Hub
|
|
109
|
+
model.push_to_hub(
|
|
110
|
+
repo_id="username/my-eegnet-model", commit_message="Initial model upload"
|
|
111
|
+
)
|
|
112
|
+
|
|
113
|
+
**Loading a model from the Hub:**
|
|
114
|
+
|
|
115
|
+
.. code-block:: python
|
|
116
|
+
|
|
117
|
+
from braindecode.models import EEGNetv4
|
|
118
|
+
|
|
119
|
+
# Load pretrained model
|
|
120
|
+
model = EEGNetv4.from_pretrained("username/my-eegnet-model")
|
|
121
|
+
|
|
122
|
+
The integration automatically handles EEG-specific parameters (n_chans,
|
|
123
|
+
n_times, sfreq, chs_info, etc.) by saving them in a config file alongside
|
|
124
|
+
the model weights. This ensures that loaded models are correctly configured
|
|
125
|
+
for their original data specifications.
|
|
126
|
+
|
|
127
|
+
.. important::
|
|
128
|
+
Currently, only EEG-specific parameters (n_outputs, n_chans, n_times,
|
|
129
|
+
input_window_seconds, sfreq, chs_info) are saved to the Hub. Model-specific
|
|
130
|
+
parameters (e.g., dropout rates, activation functions, number of filters)
|
|
131
|
+
are not preserved and will use their default values when loading from the Hub.
|
|
132
|
+
|
|
133
|
+
To use non-default model parameters, specify them explicitly when calling
|
|
134
|
+
:func:`from_pretrained()`::
|
|
135
|
+
|
|
136
|
+
model = EEGNet.from_pretrained("user/model", dropout=0.3, activation='relu')
|
|
137
|
+
|
|
138
|
+
Full parameter serialization will be addressed in a future update.
|
|
65
139
|
"""
|
|
66
140
|
|
|
141
|
+
def __init_subclass__(cls, **kwargs):
|
|
142
|
+
if not HAS_HF_HUB:
|
|
143
|
+
super().__init_subclass__(**kwargs)
|
|
144
|
+
return
|
|
145
|
+
|
|
146
|
+
base_tags = ["braindecode", cls.__name__]
|
|
147
|
+
user_tags = kwargs.pop("tags", None)
|
|
148
|
+
tags = list(user_tags) if user_tags is not None else []
|
|
149
|
+
for tag in base_tags:
|
|
150
|
+
if tag not in tags:
|
|
151
|
+
tags.append(tag)
|
|
152
|
+
|
|
153
|
+
docs_url = kwargs.pop(
|
|
154
|
+
"docs_url",
|
|
155
|
+
f"https://braindecode.org/stable/generated/braindecode.models.{cls.__name__}.html",
|
|
156
|
+
)
|
|
157
|
+
repo_url = kwargs.pop("repo_url", "https://braindecode.org")
|
|
158
|
+
library_name = kwargs.pop("library_name", "braindecode")
|
|
159
|
+
license = kwargs.pop("license", "bsd-3-clause")
|
|
160
|
+
# TODO: model_card_template can be added in the future for custom model cards
|
|
161
|
+
super().__init_subclass__(
|
|
162
|
+
tags=tags,
|
|
163
|
+
docs_url=docs_url,
|
|
164
|
+
repo_url=repo_url,
|
|
165
|
+
library_name=library_name,
|
|
166
|
+
license=license,
|
|
167
|
+
**kwargs,
|
|
168
|
+
)
|
|
169
|
+
|
|
67
170
|
def __init__(
|
|
68
171
|
self,
|
|
69
172
|
n_outputs: Optional[int] = None, # type: ignore[assignment]
|
|
@@ -73,6 +176,16 @@ class EEGModuleMixin(metaclass=NumpyDocstringInheritanceInitMeta):
|
|
|
73
176
|
input_window_seconds: Optional[float] = None, # type: ignore[assignment]
|
|
74
177
|
sfreq: Optional[float] = None, # type: ignore[assignment]
|
|
75
178
|
):
|
|
179
|
+
# Deserialize chs_info if it comes as a list of dicts (from Hub)
|
|
180
|
+
if chs_info is not None and isinstance(chs_info, list):
|
|
181
|
+
if len(chs_info) > 0 and isinstance(chs_info[0], dict):
|
|
182
|
+
# Check if it needs deserialization (has 'loc' as list)
|
|
183
|
+
if "loc" in chs_info[0] and isinstance(chs_info[0]["loc"], list):
|
|
184
|
+
chs_info = self._deserialize_chs_info(chs_info)
|
|
185
|
+
warnings.warn(
|
|
186
|
+
"Modifying chs_info argument using the _deserialize_chs_info() method"
|
|
187
|
+
)
|
|
188
|
+
|
|
76
189
|
if n_chans is not None and chs_info is not None and len(chs_info) != n_chans:
|
|
77
190
|
raise ValueError(f"{n_chans=} different from {chs_info=} length")
|
|
78
191
|
if (
|
|
@@ -294,3 +407,168 @@ class EEGModuleMixin(metaclass=NumpyDocstringInheritanceInitMeta):
|
|
|
294
407
|
|
|
295
408
|
def __str__(self) -> str:
|
|
296
409
|
return str(self.get_torchinfo_statistics())
|
|
410
|
+
|
|
411
|
+
@staticmethod
|
|
412
|
+
def _serialize_chs_info(chs_info):
|
|
413
|
+
"""
|
|
414
|
+
Serialize MNE channel info to JSON-compatible format.
|
|
415
|
+
|
|
416
|
+
Parameters
|
|
417
|
+
----------
|
|
418
|
+
chs_info : list of dict or None
|
|
419
|
+
Channel information from MNE Info object.
|
|
420
|
+
|
|
421
|
+
Returns
|
|
422
|
+
-------
|
|
423
|
+
list of dict or None
|
|
424
|
+
Serialized channel information that can be saved to JSON.
|
|
425
|
+
"""
|
|
426
|
+
if chs_info is None:
|
|
427
|
+
return None
|
|
428
|
+
|
|
429
|
+
serialized = []
|
|
430
|
+
for ch in chs_info:
|
|
431
|
+
# Extract serializable fields from MNE channel info
|
|
432
|
+
ch_dict = {
|
|
433
|
+
"ch_name": ch.get("ch_name", ""),
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
# Handle kind field - can be either string or integer
|
|
437
|
+
kind_val = ch.get("kind")
|
|
438
|
+
if kind_val is not None:
|
|
439
|
+
ch_dict["kind"] = (
|
|
440
|
+
kind_val if isinstance(kind_val, str) else int(kind_val)
|
|
441
|
+
)
|
|
442
|
+
|
|
443
|
+
# Add numeric fields with safe conversion
|
|
444
|
+
coil_type = ch.get("coil_type")
|
|
445
|
+
if coil_type is not None:
|
|
446
|
+
ch_dict["coil_type"] = int(coil_type)
|
|
447
|
+
|
|
448
|
+
unit = ch.get("unit")
|
|
449
|
+
if unit is not None:
|
|
450
|
+
ch_dict["unit"] = int(unit)
|
|
451
|
+
|
|
452
|
+
cal = ch.get("cal")
|
|
453
|
+
if cal is not None:
|
|
454
|
+
ch_dict["cal"] = float(cal)
|
|
455
|
+
|
|
456
|
+
range_val = ch.get("range")
|
|
457
|
+
if range_val is not None:
|
|
458
|
+
ch_dict["range"] = float(range_val)
|
|
459
|
+
|
|
460
|
+
# Serialize location array if present
|
|
461
|
+
if "loc" in ch and ch["loc"] is not None:
|
|
462
|
+
ch_dict["loc"] = (
|
|
463
|
+
ch["loc"].tolist()
|
|
464
|
+
if hasattr(ch["loc"], "tolist")
|
|
465
|
+
else list(ch["loc"])
|
|
466
|
+
)
|
|
467
|
+
serialized.append(ch_dict)
|
|
468
|
+
|
|
469
|
+
return serialized
|
|
470
|
+
|
|
471
|
+
@staticmethod
|
|
472
|
+
def _deserialize_chs_info(chs_info_dict):
|
|
473
|
+
"""
|
|
474
|
+
Deserialize channel info from JSON-compatible format to MNE-like structure.
|
|
475
|
+
|
|
476
|
+
Parameters
|
|
477
|
+
----------
|
|
478
|
+
chs_info_dict : list of dict or None
|
|
479
|
+
Serialized channel information.
|
|
480
|
+
|
|
481
|
+
Returns
|
|
482
|
+
-------
|
|
483
|
+
list of dict or None
|
|
484
|
+
Deserialized channel information compatible with MNE.
|
|
485
|
+
"""
|
|
486
|
+
if chs_info_dict is None:
|
|
487
|
+
return None
|
|
488
|
+
|
|
489
|
+
deserialized = []
|
|
490
|
+
for ch_dict in chs_info_dict:
|
|
491
|
+
ch = ch_dict.copy()
|
|
492
|
+
# Convert location back to numpy array if present
|
|
493
|
+
if "loc" in ch and ch["loc"] is not None:
|
|
494
|
+
ch["loc"] = np.array(ch["loc"])
|
|
495
|
+
deserialized.append(ch)
|
|
496
|
+
|
|
497
|
+
return deserialized
|
|
498
|
+
|
|
499
|
+
def _save_pretrained(self, save_directory):
|
|
500
|
+
"""
|
|
501
|
+
Save model configuration and weights to the Hub.
|
|
502
|
+
|
|
503
|
+
This method is called by PyTorchModelHubMixin.push_to_hub() to save
|
|
504
|
+
model-specific configuration alongside the model weights.
|
|
505
|
+
|
|
506
|
+
Parameters
|
|
507
|
+
----------
|
|
508
|
+
save_directory : str or Path
|
|
509
|
+
Directory where the configuration should be saved.
|
|
510
|
+
"""
|
|
511
|
+
if not HAS_HF_HUB:
|
|
512
|
+
return
|
|
513
|
+
|
|
514
|
+
save_directory = Path(save_directory)
|
|
515
|
+
|
|
516
|
+
# Collect EEG-specific configuration
|
|
517
|
+
config = {
|
|
518
|
+
"n_outputs": self._n_outputs,
|
|
519
|
+
"n_chans": self._n_chans,
|
|
520
|
+
"n_times": self._n_times,
|
|
521
|
+
"input_window_seconds": self._input_window_seconds,
|
|
522
|
+
"sfreq": self._sfreq,
|
|
523
|
+
"chs_info": self._serialize_chs_info(self._chs_info),
|
|
524
|
+
"braindecode_version": __version__,
|
|
525
|
+
}
|
|
526
|
+
|
|
527
|
+
# Save to config.json
|
|
528
|
+
config_path = save_directory / "config.json"
|
|
529
|
+
with open(config_path, "w") as f:
|
|
530
|
+
json.dump(config, f, indent=2)
|
|
531
|
+
|
|
532
|
+
# Save model weights with standard Hub filename
|
|
533
|
+
weights_path = save_directory / "pytorch_model.bin"
|
|
534
|
+
torch.save(self.state_dict(), weights_path)
|
|
535
|
+
|
|
536
|
+
# Also save in safetensors format using parent's implementation
|
|
537
|
+
try:
|
|
538
|
+
super()._save_pretrained(save_directory)
|
|
539
|
+
except (ImportError, RuntimeError) as e:
|
|
540
|
+
# Fallback to pytorch_model.bin if safetensors saving fails
|
|
541
|
+
warnings.warn(
|
|
542
|
+
f"Could not save model in safetensors format: {e}. "
|
|
543
|
+
"Model weights saved in pytorch_model.bin instead.",
|
|
544
|
+
stacklevel=2,
|
|
545
|
+
)
|
|
546
|
+
|
|
547
|
+
if HAS_HF_HUB:
|
|
548
|
+
|
|
549
|
+
@classmethod
|
|
550
|
+
def _from_pretrained(
|
|
551
|
+
cls,
|
|
552
|
+
*,
|
|
553
|
+
model_id: str,
|
|
554
|
+
revision: Optional[str],
|
|
555
|
+
cache_dir: Optional[Union[str, Path]],
|
|
556
|
+
force_download: bool,
|
|
557
|
+
local_files_only: bool,
|
|
558
|
+
token: Union[str, bool, None],
|
|
559
|
+
map_location: str = "cpu",
|
|
560
|
+
strict: bool = False,
|
|
561
|
+
**model_kwargs,
|
|
562
|
+
):
|
|
563
|
+
model_kwargs.pop("braindecode_version", None)
|
|
564
|
+
return super()._from_pretrained( # type: ignore
|
|
565
|
+
model_id=model_id,
|
|
566
|
+
revision=revision,
|
|
567
|
+
cache_dir=cache_dir,
|
|
568
|
+
force_download=force_download,
|
|
569
|
+
local_files_only=local_files_only,
|
|
570
|
+
token=token,
|
|
571
|
+
map_location=map_location,
|
|
572
|
+
strict=strict,
|
|
573
|
+
**model_kwargs,
|
|
574
|
+
)
|