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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
license: bsd-3-clause
|
| 3 |
+
library_name: braindecode
|
| 4 |
+
pipeline_tag: feature-extraction
|
| 5 |
+
tags:
|
| 6 |
+
- eeg
|
| 7 |
+
- biosignal
|
| 8 |
+
- pytorch
|
| 9 |
+
- neuroscience
|
| 10 |
+
- braindecode
|
| 11 |
+
- convolutional
|
| 12 |
+
---
|
| 13 |
+
|
| 14 |
+
# EEGSym
|
| 15 |
+
|
| 16 |
+
EEGSym from Pérez-Velasco et al (2022) .
|
| 17 |
+
|
| 18 |
+
> **Architecture-only repository.** This repo documents the
|
| 19 |
+
> `braindecode.models.EEGSym` class. **No pretrained weights are
|
| 20 |
+
> distributed here** — instantiate the model and train it on your own
|
| 21 |
+
> data, or fine-tune from a published foundation-model checkpoint
|
| 22 |
+
> separately.
|
| 23 |
+
|
| 24 |
+
## Quick start
|
| 25 |
+
|
| 26 |
+
```bash
|
| 27 |
+
pip install braindecode
|
| 28 |
+
```
|
| 29 |
+
|
| 30 |
+
```python
|
| 31 |
+
from braindecode.models import EEGSym
|
| 32 |
+
|
| 33 |
+
model = EEGSym(
|
| 34 |
+
n_chans=22,
|
| 35 |
+
sfreq=250,
|
| 36 |
+
input_window_seconds=4.0,
|
| 37 |
+
n_outputs=4,
|
| 38 |
+
)
|
| 39 |
+
```
|
| 40 |
+
|
| 41 |
+
The signal-shape arguments above are example defaults — adjust them
|
| 42 |
+
to match your recording.
|
| 43 |
+
|
| 44 |
+
## Documentation
|
| 45 |
+
|
| 46 |
+
- Full API reference (parameters, references, architecture figure):
|
| 47 |
+
<https://braindecode.org/stable/generated/braindecode.models.EEGSym.html>
|
| 48 |
+
- Interactive browser with live instantiation:
|
| 49 |
+
<https://huggingface.co/spaces/braindecode/model-explorer>
|
| 50 |
+
- Source on GitHub: <https://github.com/braindecode/braindecode/blob/master/braindecode/models/eegsym.py#L16>
|
| 51 |
+
|
| 52 |
+
## Architecture description
|
| 53 |
+
|
| 54 |
+
The block below is the rendered class docstring (parameters,
|
| 55 |
+
references, architecture figure where available).
|
| 56 |
+
|
| 57 |
+
<div class='bd-doc'><main>
|
| 58 |
+
<p>EEGSym from Pérez-Velasco et al (2022) [eegsym2022]_.</p>
|
| 59 |
+
<span style="display:inline-block;padding:2px 8px;border-radius:4px;background:#5cb85c;color:white;font-size:11px;font-weight:600;margin-right:4px;">Convolution</span>
|
| 60 |
+
|
| 61 |
+
:bdg-dark-line:`Channel`
|
| 62 |
+
|
| 63 |
+
.. figure:: ../../docs/_static/model/eegsym.png
|
| 64 |
+
:align: center
|
| 65 |
+
:alt: EEGSym Architecture
|
| 66 |
+
|
| 67 |
+
|
| 68 |
+
The **EEGSym** is a novel Convolutional Neural Network (CNN) architecture designed for
|
| 69 |
+
Motor Imagery (MI) based Brain-Computer Interfaces (BCIs), primarily aimed at
|
| 70 |
+
**overcoming inter-subject variability** and significantly **reducing BCI inefficiency**
|
| 71 |
+
[eegsym2022]_.
|
| 72 |
+
|
| 73 |
+
The architecture integrates advances from Deep Learning (DL), complemented by
|
| 74 |
+
Transfer Learning (TL) techniques and Data Augmentation (DA), to achieve strong
|
| 75 |
+
performance in inter-subject MI classification [eegsym2022]_.
|
| 76 |
+
|
| 77 |
+
.. rubric:: Architectural Overview
|
| 78 |
+
|
| 79 |
+
EEGSym systematically incorporates three core features:
|
| 80 |
+
|
| 81 |
+
#. **Inception Modules** for multi-scale temporal analysis [eegsym2022]_.
|
| 82 |
+
#. **Residual Connections** maintain spatio-temporal signal structure and
|
| 83 |
+
enable deeper feature extraction [eegsym2022]_.
|
| 84 |
+
#. A **Siamese-network design** exploits the inherent symmetry of the brain
|
| 85 |
+
across the mid-sagittal plane [eegsym2022]_.
|
| 86 |
+
|
| 87 |
+
.. rubric:: Macro Components
|
| 88 |
+
|
| 89 |
+
- `EEGSym.symmetric_division` **(Input Processing)**
|
| 90 |
+
- *Operations.* The input is virtually split into left, right, and middle channels.
|
| 91 |
+
Middle (central) channels are duplicated and concatenated to both left
|
| 92 |
+
and right lateralized electrodes to form the two hemisphere inputs [eegsym2022]_.
|
| 93 |
+
- *Role.* Prepares the data for the siamese-network approach,
|
| 94 |
+
reducing the number of parameters in the spatial filters
|
| 95 |
+
for the tempospatial analysis stage [eegsym2022]_.
|
| 96 |
+
|
| 97 |
+
- `EEGSym.inception_block` **(Tempospatial Analysis - Temporal Feature Extraction)**
|
| 98 |
+
- *Operations.* Uses :class:`_InceptionBlock` modules, which apply parallel
|
| 99 |
+
temporal convolutions with different kernel sizes (scales) [eegsym2022]_.
|
| 100 |
+
This is followed by concatenation, residual connections, and average
|
| 101 |
+
pooling for temporal dimensionality reduction [eegsym2022]_.
|
| 102 |
+
- *Role.* Captures detailed temporal relationships in the architecture,
|
| 103 |
+
similarly to :class:`~braindecode.models.eeginception_mi.EEGInceptionMI`
|
| 104 |
+
[eeginception2020]_. The first block uses large temporal kernels
|
| 105 |
+
(e.g., 500 ms, 250 ms, 125 ms) [eegsym2022]_.
|
| 106 |
+
|
| 107 |
+
- `EEGSym.residual_blocks` **(Tempospatial Analysis - Spatial Feature Extraction)**
|
| 108 |
+
- *Operations.* Composed of multiple :class:`_ResidualBlock` modules (typically three instances)
|
| 109 |
+
[eegsym2022]_. Each block applies temporal convolution, pooling, and a spatial analysis layer
|
| 110 |
+
(convolution or grouped convolution) [eegsym2022]_.
|
| 111 |
+
- *Role.* Enhances spatial feature extraction by incorporating residual
|
| 112 |
+
connections across all CNN stages, which helps maintain the spatio-temporal
|
| 113 |
+
structure of the signal through deeper layers [eegsym2022]_.
|
| 114 |
+
|
| 115 |
+
- `EEGSym.channel_merging` **(Hemisphere Merging)**
|
| 116 |
+
- *Operations.* The :class:`_ChannelMergingBlock` reduces the spatial dimensionality
|
| 117 |
+
(Z and C) to 1, performing two residual convolutions followed by a final grouped
|
| 118 |
+
convolution that merges the feature information from the two hemispheres [eegsym2022]_.
|
| 119 |
+
- *Role.* Extracts complex relationships between channels of both hemispheres as part of the
|
| 120 |
+
symmetry exploitation [eegsym2022]_.
|
| 121 |
+
|
| 122 |
+
- `EEGSym.temporal_merging` **(Temporal Collapse)**
|
| 123 |
+
- *Operations.* The :class:`_TemporalMergingBlock` uses residual convolution
|
| 124 |
+
followed by grouped convolution to reduce the temporal dimension (S) to 1 [eegsym2022]_.
|
| 125 |
+
- *Role.* Final step of temporal aggregation before the output module [eegsym2022]_.
|
| 126 |
+
|
| 127 |
+
- `EEGSym.output_blocks` **(Output Processing)**
|
| 128 |
+
- *Operations.* The :class:`_OutputBlock` applies four residual convolution iterations
|
| 129 |
+
(1x1x1 convolutions) followed by flattening [eegsym2022]_.
|
| 130 |
+
- *Role.* Final feature refinement through residual connections before the
|
| 131 |
+
fully connected classification layer [eegsym2022]_.
|
| 132 |
+
|
| 133 |
+
.. rubric:: How the information is encoded temporally, spatially, and spectrally
|
| 134 |
+
|
| 135 |
+
* **Temporal.**
|
| 136 |
+
Temporal features are extracted across multiple scales in the inception modules
|
| 137 |
+
using different temporal convolution kernel sizes (e.g., corresponding to
|
| 138 |
+
500 ms, 250 ms, and 125 ms windows for a 128 Hz sampling rate), very similar to [eeginception2020]_.
|
| 139 |
+
Subsequent pooling operations and residual blocks continue to reduce the temporal dimension
|
| 140 |
+
[eegsym2022]_.
|
| 141 |
+
|
| 142 |
+
* **Spatial.**
|
| 143 |
+
|
| 144 |
+
Spatial features are extracted via two main mechanisms:
|
| 145 |
+
|
| 146 |
+
- (1) The **siamese-network design** implicitly introduces brain symmetry by treating the two hemispheres
|
| 147 |
+
equally during feature extraction [eegsym2022]_.
|
| 148 |
+
- (2) **Residual connections** are utilized in the Tempospatial Analysis stage to enhance the extraction of
|
| 149 |
+
spatial correlations between electrodes [eegsym2022]_.
|
| 150 |
+
|
| 151 |
+
* **Spectral.**
|
| 152 |
+
Spectral information is implicitly captured by the varying kernel sizes of the temporal convolutions
|
| 153 |
+
in the inception modules [eegsym2022]_. These kernels filter the signal across different temporal windows,
|
| 154 |
+
corresponding to different frequency characteristics.
|
| 155 |
+
|
| 156 |
+
Notes
|
| 157 |
+
-----
|
| 158 |
+
* EEGSym achieved competitive accuracies across five large MI datasets [eegsym2022]_.
|
| 159 |
+
* The model maintained high accuracy using a reduced set of electrodes (8 or 16 channels)
|
| 160 |
+
[eegsym2022]_.
|
| 161 |
+
* This is PyTorch implementation of the EEGSym model of the TensorFlow original [eegsym2022code]_.
|
| 162 |
+
|
| 163 |
+
Parameters
|
| 164 |
+
----------
|
| 165 |
+
filters_per_branch : int, optional
|
| 166 |
+
Number of filters in each inception branch. Should be a multiple of 8.
|
| 167 |
+
Default is 12 [eegsym2022]_.
|
| 168 |
+
scales_time : tuple of int, optional
|
| 169 |
+
Temporal scales (in milliseconds) for the temporal convolutions in the first
|
| 170 |
+
inception module. Default is (500, 250, 125) [eegsym2022]_.
|
| 171 |
+
drop_prob : float, optional
|
| 172 |
+
Dropout probability. Default is 0.25 [eegsym2022]_.
|
| 173 |
+
activation : type[nn.Module], optional
|
| 174 |
+
Activation function class to use. Default is :class:`nn.ELU` [eegsym2022]_.
|
| 175 |
+
spatial_resnet_repetitions : int, optional
|
| 176 |
+
Number of repetitions of the spatial analysis operations at each step.
|
| 177 |
+
Default is 5 [eegsym2022]_.
|
| 178 |
+
left_right_chs : list of tuple of str, optional
|
| 179 |
+
List of tuples pairing left and right hemisphere channel names,
|
| 180 |
+
e.g., ``[('C3', 'C4'), ('FC5', 'FC6')]``. If not provided, channels
|
| 181 |
+
are automatically split into left/right hemispheres using
|
| 182 |
+
:func:`~braindecode.datautil.channel_utils.division_channels_idx` and
|
| 183 |
+
:func:`~braindecode.datautil.channel_utils.match_hemisphere_chans`.
|
| 184 |
+
Must be provided together with ``middle_chs`` [eegsym2022]_.
|
| 185 |
+
middle_chs : list of str, optional
|
| 186 |
+
List of midline (central) channel names that lie on the mid-sagittal plane,
|
| 187 |
+
e.g., ``['FZ', 'CZ', 'PZ']``. These channels are duplicated and concatenated
|
| 188 |
+
to both hemispheres. If not provided, channels are automatically identified
|
| 189 |
+
using :func:`~braindecode.datautil.channel_utils.division_channels_idx`.
|
| 190 |
+
Must be provided together with ``left_right_chs`` [eegsym2022]_.
|
| 191 |
+
|
| 192 |
+
References
|
| 193 |
+
----------
|
| 194 |
+
.. [eegsym2022] Pérez-Velasco, S., Santamaría-Vázquez, E., Martínez-Cagigal, V.,
|
| 195 |
+
Marcos-Martínez, D., & Hornero, R. (2022). EEGSym: Overcoming inter-subject
|
| 196 |
+
variability in motor imagery based BCIs with deep learning. IEEE Transactions
|
| 197 |
+
on Neural Systems and Rehabilitation Engineering, 30, 1766-1775.
|
| 198 |
+
.. [eegsym2022code] Pérez-Velasco, S., EEGSym source code.
|
| 199 |
+
https://github.com/Serpeve/EEGSym
|
| 200 |
+
.. [eeginception2020] Santamaría-Vázquez, E., Martínez-Cagigal, V.,
|
| 201 |
+
Vaquerizo-Villar, F., & Hornero, R. (2020). EEG-Inception: A novel deep
|
| 202 |
+
convolutional neural network for assistive ERP-based brain-computer interfaces.
|
| 203 |
+
IEEE Transactions on Neural Systems and Rehabilitation Engineering, 28, 2773-2782.
|
| 204 |
+
|
| 205 |
+
.. rubric:: Hugging Face Hub integration
|
| 206 |
+
|
| 207 |
+
When the optional ``huggingface_hub`` package is installed, all models
|
| 208 |
+
automatically gain the ability to be pushed to and loaded from the
|
| 209 |
+
Hugging Face Hub. Install with::
|
| 210 |
+
|
| 211 |
+
pip install braindecode[hub]
|
| 212 |
+
|
| 213 |
+
**Pushing a model to the Hub:**
|
| 214 |
+
|
| 215 |
+
.. code::
|
| 216 |
+
from braindecode.models import EEGSym
|
| 217 |
+
|
| 218 |
+
# Train your model
|
| 219 |
+
model = EEGSym(n_chans=22, n_outputs=4, n_times=1000)
|
| 220 |
+
# ... training code ...
|
| 221 |
+
|
| 222 |
+
# Push to the Hub
|
| 223 |
+
model.push_to_hub(
|
| 224 |
+
repo_id="username/my-eegsym-model",
|
| 225 |
+
commit_message="Initial model upload",
|
| 226 |
+
)
|
| 227 |
+
|
| 228 |
+
**Loading a model from the Hub:**
|
| 229 |
+
|
| 230 |
+
.. code::
|
| 231 |
+
from braindecode.models import EEGSym
|
| 232 |
+
|
| 233 |
+
# Load pretrained model
|
| 234 |
+
model = EEGSym.from_pretrained("username/my-eegsym-model")
|
| 235 |
+
|
| 236 |
+
# Load with a different number of outputs (head is rebuilt automatically)
|
| 237 |
+
model = EEGSym.from_pretrained("username/my-eegsym-model", n_outputs=4)
|
| 238 |
+
|
| 239 |
+
**Extracting features and replacing the head:**
|
| 240 |
+
|
| 241 |
+
.. code::
|
| 242 |
+
import torch
|
| 243 |
+
|
| 244 |
+
x = torch.randn(1, model.n_chans, model.n_times)
|
| 245 |
+
# Extract encoder features (consistent dict across all models)
|
| 246 |
+
out = model(x, return_features=True)
|
| 247 |
+
features = out["features"]
|
| 248 |
+
|
| 249 |
+
# Replace the classification head
|
| 250 |
+
model.reset_head(n_outputs=10)
|
| 251 |
+
|
| 252 |
+
**Saving and restoring full configuration:**
|
| 253 |
+
|
| 254 |
+
.. code::
|
| 255 |
+
import json
|
| 256 |
+
|
| 257 |
+
config = model.get_config() # all __init__ params
|
| 258 |
+
with open("config.json", "w") as f:
|
| 259 |
+
json.dump(config, f)
|
| 260 |
+
|
| 261 |
+
model2 = EEGSym.from_config(config) # reconstruct (no weights)
|
| 262 |
+
|
| 263 |
+
All model parameters (both EEG-specific and model-specific such as
|
| 264 |
+
dropout rates, activation functions, number of filters) are automatically
|
| 265 |
+
saved to the Hub and restored when loading.
|
| 266 |
+
|
| 267 |
+
See :ref:`load-pretrained-models` for a complete tutorial.</main>
|
| 268 |
+
</div>
|
| 269 |
+
|
| 270 |
+
## Citation
|
| 271 |
+
|
| 272 |
+
Please cite both the original paper for this architecture (see the
|
| 273 |
+
*References* section above) and braindecode:
|
| 274 |
+
|
| 275 |
+
```bibtex
|
| 276 |
+
@article{aristimunha2025braindecode,
|
| 277 |
+
title = {Braindecode: a deep learning library for raw electrophysiological data},
|
| 278 |
+
author = {Aristimunha, Bruno and others},
|
| 279 |
+
journal = {Zenodo},
|
| 280 |
+
year = {2025},
|
| 281 |
+
doi = {10.5281/zenodo.17699192},
|
| 282 |
+
}
|
| 283 |
+
```
|
| 284 |
+
|
| 285 |
+
## License
|
| 286 |
+
|
| 287 |
+
BSD-3-Clause for the model code (matching braindecode).
|
| 288 |
+
Pretraining-derived weights, if you fine-tune from a checkpoint,
|
| 289 |
+
inherit the licence of that checkpoint and its training corpus.
|