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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 |
+
# EEGNet
|
| 15 |
+
|
| 16 |
+
EEGNet model from Lawhern et al (2018) .
|
| 17 |
+
|
| 18 |
+
> **Architecture-only repository.** This repo documents the
|
| 19 |
+
> `braindecode.models.EEGNet` 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 EEGNet
|
| 32 |
+
|
| 33 |
+
model = EEGNet(
|
| 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.EEGNet.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/eegnet.py#L22>
|
| 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>EEGNet model from Lawhern et al (2018) [Lawhern2018]_.</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 |
+
|
| 62 |
+
|
| 63 |
+
.. figure:: https://content.cld.iop.org/journals/1741-2552/15/5/056013/revision2/jneaace8cf01_hr.jpg
|
| 64 |
+
:align: center
|
| 65 |
+
:alt: EEGNet Architecture
|
| 66 |
+
:width: 600px
|
| 67 |
+
|
| 68 |
+
.. rubric:: Architectural Overview
|
| 69 |
+
|
| 70 |
+
EEGNet is a compact convolutional network designed for EEG decoding with a pipeline that mirrors classical EEG processing:
|
| 71 |
+
- (i) learn temporal frequency-selective filters,
|
| 72 |
+
- (ii) learn spatial filters for those frequencies, and
|
| 73 |
+
- (iii) condense features with depthwise-separable convolutions before a lightweight classifier.
|
| 74 |
+
|
| 75 |
+
The architecture is deliberately small (temporal convolutional and spatial patterns) [Lawhern2018]_.
|
| 76 |
+
|
| 77 |
+
.. rubric:: Macro Components
|
| 78 |
+
|
| 79 |
+
- **Temporal convolution**
|
| 80 |
+
Temporal convolution applied per channel; learns ``F1`` kernels that act as data-driven band-pass filters.
|
| 81 |
+
- **Depthwise Spatial Filtering.**
|
| 82 |
+
Depthwise convolution spanning the channel dimension with ``groups = F1``,
|
| 83 |
+
yielding ``D`` spatial filters for each temporal filter (no cross-filter mixing).
|
| 84 |
+
- **Norm-Nonlinearity-Pooling (+ dropout).**
|
| 85 |
+
Batch normalization → ELU → temporal pooling, with dropout.
|
| 86 |
+
- **Depthwise-Separable Convolution Block.**
|
| 87 |
+
(a) depthwise temporal conv to refine temporal structure;
|
| 88 |
+
(b) pointwise 1x1 conv to mix feature maps into ``F2`` combinations.
|
| 89 |
+
- **Classifier Head.**
|
| 90 |
+
Lightweight 1x1 conv or dense layer (often with max-norm constraint).
|
| 91 |
+
|
| 92 |
+
.. rubric:: Convolutional Details
|
| 93 |
+
|
| 94 |
+
- **Temporal.** The initial temporal convs serve as a *learned filter bank*:
|
| 95 |
+
long 1-D kernels (implemented as 2-D with singleton spatial extent) emphasize oscillatory bands and transients.
|
| 96 |
+
Because this stage is linear prior to BN/ELU, kernels can be analyzed as FIR filters to reveal each feature's spectrum [Lawhern2018]_.
|
| 97 |
+
|
| 98 |
+
- **Spatial.** The depthwise spatial conv spans the full channel axis (kernel height = #electrodes; temporal size = 1).
|
| 99 |
+
With ``groups = F1``, each temporal filter learns its own set of ``D`` spatial projections—akin to CSP, learned end-to-end and
|
| 100 |
+
typically regularized with max-norm.
|
| 101 |
+
|
| 102 |
+
- **Spectral.** No explicit Fourier/wavelet transform is used. Frequency structure
|
| 103 |
+
is captured implicitly by the temporal filter bank; later depthwise temporal kernels act as short-time integrators/refiners.
|
| 104 |
+
|
| 105 |
+
.. rubric:: Additional Comments
|
| 106 |
+
|
| 107 |
+
- **Filter-bank structure:** Parallel temporal kernels (``F1``) emulate classical filter banks; pairing them with frequency-specific spatial filters
|
| 108 |
+
yields features mappable to rhythms and topographies.
|
| 109 |
+
- **Depthwise & separable convs:** Parameter-efficient decomposition (depthwise + pointwise) retains power while limiting overfitting
|
| 110 |
+
[Chollet2017]_ and keeps temporal vs. mixing steps interpretable.
|
| 111 |
+
- **Regularization:** Batch norm, dropout, pooling, and optional max-norm on spatial kernels aid stability on small EEG datasets.
|
| 112 |
+
- The v4 means the version 4 at the arxiv paper [Lawhern2018]_.
|
| 113 |
+
|
| 114 |
+
|
| 115 |
+
Parameters
|
| 116 |
+
----------
|
| 117 |
+
final_conv_length : int or "auto", default="auto"
|
| 118 |
+
Length of the final convolution layer. If "auto", it is set based on n_times.
|
| 119 |
+
pool_mode : {"mean", "max"}, default="mean"
|
| 120 |
+
Pooling method to use in pooling layers.
|
| 121 |
+
F1 : int, default=8
|
| 122 |
+
Number of temporal filters in the first convolutional layer.
|
| 123 |
+
D : int, default=2
|
| 124 |
+
Depth multiplier for the depthwise convolution.
|
| 125 |
+
F2 : int or None, default=None
|
| 126 |
+
Number of pointwise filters in the separable convolution. Usually set to ``F1 * D``.
|
| 127 |
+
depthwise_kernel_length : int, default=16
|
| 128 |
+
Length of the depthwise convolution kernel in the separable convolution.
|
| 129 |
+
pool1_kernel_size : int, default=4
|
| 130 |
+
Kernel size of the first pooling layer.
|
| 131 |
+
pool2_kernel_size : int, default=8
|
| 132 |
+
Kernel size of the second pooling layer.
|
| 133 |
+
kernel_length : int, default=64
|
| 134 |
+
Length of the temporal convolution kernel.
|
| 135 |
+
conv_spatial_max_norm : float, default=1
|
| 136 |
+
Maximum norm constraint for the spatial (depthwise) convolution.
|
| 137 |
+
activation : nn.Module, default=nn.ELU
|
| 138 |
+
Non-linear activation function to be used in the layers.
|
| 139 |
+
batch_norm_momentum : float, default=0.01
|
| 140 |
+
Momentum for instance normalization in batch norm layers.
|
| 141 |
+
batch_norm_affine : bool, default=True
|
| 142 |
+
If True, batch norm has learnable affine parameters.
|
| 143 |
+
batch_norm_eps : float, default=1e-3
|
| 144 |
+
Epsilon for numeric stability in batch norm layers.
|
| 145 |
+
drop_prob : float, default=0.25
|
| 146 |
+
Dropout probability.
|
| 147 |
+
final_layer_with_constraint : bool, default=False
|
| 148 |
+
If ``False``, uses a convolution-based classification layer. If ``True``,
|
| 149 |
+
apply a flattened linear layer with constraint on the weights norm as the final classification step.
|
| 150 |
+
norm_rate : float, default=0.25
|
| 151 |
+
Max-norm constraint value for the linear layer (used if ``final_layer_conv=False``).
|
| 152 |
+
|
| 153 |
+
References
|
| 154 |
+
----------
|
| 155 |
+
.. [Lawhern2018] Lawhern, V. J., Solon, A. J., Waytowich, N. R., Gordon, S. M.,
|
| 156 |
+
Hung, C. P., & Lance, B. J. (2018). EEGNet: a compact convolutional
|
| 157 |
+
neural network for EEG-based brain–computer interfaces. Journal of
|
| 158 |
+
neural engineering, 15(5), 056013.
|
| 159 |
+
.. [Chollet2017] Chollet, F., *Xception: Deep Learning with Depthwise Separable
|
| 160 |
+
Convolutions*, CVPR, 2017.
|
| 161 |
+
|
| 162 |
+
.. rubric:: Hugging Face Hub integration
|
| 163 |
+
|
| 164 |
+
When the optional ``huggingface_hub`` package is installed, all models
|
| 165 |
+
automatically gain the ability to be pushed to and loaded from the
|
| 166 |
+
Hugging Face Hub. Install with::
|
| 167 |
+
|
| 168 |
+
pip install braindecode[hub]
|
| 169 |
+
|
| 170 |
+
**Pushing a model to the Hub:**
|
| 171 |
+
|
| 172 |
+
.. code::
|
| 173 |
+
from braindecode.models import EEGNet
|
| 174 |
+
|
| 175 |
+
# Train your model
|
| 176 |
+
model = EEGNet(n_chans=22, n_outputs=4, n_times=1000)
|
| 177 |
+
# ... training code ...
|
| 178 |
+
|
| 179 |
+
# Push to the Hub
|
| 180 |
+
model.push_to_hub(
|
| 181 |
+
repo_id="username/my-eegnet-model",
|
| 182 |
+
commit_message="Initial model upload",
|
| 183 |
+
)
|
| 184 |
+
|
| 185 |
+
**Loading a model from the Hub:**
|
| 186 |
+
|
| 187 |
+
.. code::
|
| 188 |
+
from braindecode.models import EEGNet
|
| 189 |
+
|
| 190 |
+
# Load pretrained model
|
| 191 |
+
model = EEGNet.from_pretrained("username/my-eegnet-model")
|
| 192 |
+
|
| 193 |
+
# Load with a different number of outputs (head is rebuilt automatically)
|
| 194 |
+
model = EEGNet.from_pretrained("username/my-eegnet-model", n_outputs=4)
|
| 195 |
+
|
| 196 |
+
**Extracting features and replacing the head:**
|
| 197 |
+
|
| 198 |
+
.. code::
|
| 199 |
+
import torch
|
| 200 |
+
|
| 201 |
+
x = torch.randn(1, model.n_chans, model.n_times)
|
| 202 |
+
# Extract encoder features (consistent dict across all models)
|
| 203 |
+
out = model(x, return_features=True)
|
| 204 |
+
features = out["features"]
|
| 205 |
+
|
| 206 |
+
# Replace the classification head
|
| 207 |
+
model.reset_head(n_outputs=10)
|
| 208 |
+
|
| 209 |
+
**Saving and restoring full configuration:**
|
| 210 |
+
|
| 211 |
+
.. code::
|
| 212 |
+
import json
|
| 213 |
+
|
| 214 |
+
config = model.get_config() # all __init__ params
|
| 215 |
+
with open("config.json", "w") as f:
|
| 216 |
+
json.dump(config, f)
|
| 217 |
+
|
| 218 |
+
model2 = EEGNet.from_config(config) # reconstruct (no weights)
|
| 219 |
+
|
| 220 |
+
All model parameters (both EEG-specific and model-specific such as
|
| 221 |
+
dropout rates, activation functions, number of filters) are automatically
|
| 222 |
+
saved to the Hub and restored when loading.
|
| 223 |
+
|
| 224 |
+
See :ref:`load-pretrained-models` for a complete tutorial.</main>
|
| 225 |
+
</div>
|
| 226 |
+
|
| 227 |
+
## Citation
|
| 228 |
+
|
| 229 |
+
Please cite both the original paper for this architecture (see the
|
| 230 |
+
*References* section above) and braindecode:
|
| 231 |
+
|
| 232 |
+
```bibtex
|
| 233 |
+
@article{aristimunha2025braindecode,
|
| 234 |
+
title = {Braindecode: a deep learning library for raw electrophysiological data},
|
| 235 |
+
author = {Aristimunha, Bruno and others},
|
| 236 |
+
journal = {Zenodo},
|
| 237 |
+
year = {2025},
|
| 238 |
+
doi = {10.5281/zenodo.17699192},
|
| 239 |
+
}
|
| 240 |
+
```
|
| 241 |
+
|
| 242 |
+
## License
|
| 243 |
+
|
| 244 |
+
BSD-3-Clause for the model code (matching braindecode).
|
| 245 |
+
Pretraining-derived weights, if you fine-tune from a checkpoint,
|
| 246 |
+
inherit the licence of that checkpoint and its training corpus.
|