braindecode.models.MIRepNet#
- class braindecode.models.MIRepNet(n_outputs=None, n_chans=None, chs_info=None, n_times=None, input_window_seconds=None, sfreq=None, *, embed_dim=256, n_filters_time=64, n_filters_spat=128, filter_time_length=25, pool_time_length=75, pool_time_stride=15, num_layers=6, num_heads=8, feedforward_expansion=4, activation=<class 'torch.nn.modules.activation.ELU'>, activation_trans=<class 'torch.nn.modules.activation.GELU'>, drop_prob=0.5, att_drop_prob=0.5, feedforward_drop_prob=0.5, attention_scale=None, return_features=False, channel_strategy='native', channel_strategy_kwargs=None)[source]#
MIRepNet from Liu et al. (2026) [liu2026mirepnet].
Convolution Attention/Transformer Foundation Model
Original MIRepNet overview. Braindecode implements only the downstream embedding, Transformer encoder, pooling, and classifier shown at right.#
Architecture Overview
Temporal and spatial convolutions embed the input EEG.
Pre-normalized Transformer blocks contextualize the resulting tokens.
Token averaging and a linear head produce class logits.
Macro Components
MIRepNet.embeddingOperations: A 25-sample temporal convolution, a spatial convolution across all channels, batch normalization, ELU, 75-sample average pooling with stride 15, dropout, and a 1x1 projection transform
(batch, n_chans, n_times)into(batch, n_tokens, embed_dim).Role: Extract local temporal-spatial EEG features and form tokens.
MIRepNet.transformerOperations: Pre-normalized residual attention and feed-forward branches from the released implementation [mirepnetcode] process the token sequence.
Role: Contextualize each token using the full embedded sequence.
MIRepNet.final_layerOperations: Mean pooling produces
(batch, embed_dim)features, then a linear projection produces(batch, n_outputs)logits.Role: Convert the learned representation into class scores.
Temporal, Spatial, and Spectral Encoding
Time: temporal convolution, temporal pooling, and attention encode local and global temporal structure.
Channels/space: one convolution spans all input channels.
Frequency: no explicit spectral layer is used; spectral content is learned from the time-domain input.
Additional Mechanisms
Attention scores use the released source scale
embed_dim ** -0.5. Withreturn_features=True, mean-pooled tokens are returned in the unified feature dictionary instead of being classified.This class does not perform the paper’s 8–30 Hz filter, 250 Hz resampling, channel preparation, or Euclidean alignment. Users are responsible for applying that preprocessing before calling the model.
Pre-trained weights
The released downstream checkpoint is re-hosted in Braindecode format on the Hugging Face Hub and can be loaded with:
model = MIRepNet.from_pretrained("braindecode/mirepnet-pretrained")
It was trained with 45 channels, 1,000 samples at 250 Hz, and three output classes. Pass
n_outputsto replace its classification head.Added in version 2.0.
- Parameters:
n_outputs (int) – Number of outputs of the model. This is the number of classes in the case of classification.
n_chans (int) – Number of EEG channels.
chs_info (list of dict) – Information about each individual EEG channel. This should be filled with
info["chs"]. Refer tomne.Infofor more details.n_times (int) – Number of time samples of the input window.
input_window_seconds (float) – Length of the input window in seconds.
sfreq (float) – Sampling frequency of the EEG recordings.
embed_dim (
int) – Transformer embedding dimension.n_filters_time (
int) – Number of temporal convolution filters.n_filters_spat (
int) – Number of spatial convolution filters.filter_time_length (
int) – Length of the temporal convolution kernel, in samples.pool_time_length (
int) – Length of the temporal average-pooling kernel, in samples.pool_time_stride (
int) – Stride of the temporal average pooling, in samples.num_layers (
int) – Number of Transformer encoder blocks.num_heads (
int) – Number of attention heads.feedforward_expansion (
int) – Transformer feed-forward hidden-width multiplier.activation (
type[Module]) – Activation used in the convolutional patch embedding.activation_trans (
type[Module]) – Activation used in Transformer feed-forward blocks.drop_prob (
float) – Dropout probability in the patch embedding.att_drop_prob (
float) – Dropout probability in attention and Transformer residual branches.feedforward_drop_prob (
float) – Dropout probability inside Transformer feed-forward blocks.attention_scale (
float|None) – Attention-score multiplier.Noneuses the releasedembed_dim ** -0.5scale.return_features (
bool) – Whetherforwardreturns the unified feature dictionary by default.channel_strategy (
str) – How any montage reaches the backbone (pretrained models only; see Channel strategies: any montage in)."native"keeps the model as it is."exact","zero","nearest","idw","spline","field","source","region","wiener"(callmodel.channel_layer.fitfirst) or"latent"map the montage ofchs_info(or of thechs_infogiven toforward) onto the backbone’s channels with aChannelLayer. Saved in the config.model(x)andmodel.forward(x)both apply the layer.channel_strategy_kwargs (
dict|None) – Options of the strategy (e.g.{"reg": 1e-2}for"spline").
- Raises:
ValueError – If some input signal-related parameters are not specified: and can not be inferred.
Notes
Input shape:
(batch, n_chans, n_times).Output shape: by default,
(batch, n_outputs)logits. Withreturn_features=True,{"features": (batch, embed_dim), "cls_token": None}.References
[liu2026mirepnet]Liu et al. (2026). MIRepNet: A pipeline and pre-trained model for EEG-based motor imagery classification. Knowledge-Based Systems, 343, 115966. https://doi.org/10.1016/j.knosys.2026.115966
[mirepnetcode]Released implementation: staraink/MIRepNet
Hugging Face Hub integration
When the optional
huggingface_hubpackage is installed, all models automatically gain the ability to be pushed to and loaded from the Hugging Face Hub. Install with:pip install braindecode[hub]
Pushing a model to the Hub:
from braindecode.models import MIRepNet # Train your model model = MIRepNet(n_chans=22, n_outputs=4, n_times=1000) # ... training code ... # Push to the Hub; ``datasets`` and ``base_model`` go to the card model.push_to_hub( repo_id="username/my-mirepnet-model", commit_message="Initial model upload", model_card_kwargs={"datasets": ["BNCI2014_001"]}, )
Loading a model from the Hub:
from braindecode.models import MIRepNet # Load pretrained model model = MIRepNet.from_pretrained("username/my-mirepnet-model") # Data the weights were trained on, from the Hub model card print(model.card_data.datasets if model.card_data else None) # Load with a different number of outputs (head is rebuilt automatically) model = MIRepNet.from_pretrained("username/my-mirepnet-model", n_outputs=4)
Extracting features and replacing the head:
import torch x = torch.randn(1, model.n_chans, model.n_times) # Extract encoder features (consistent dict across all models) out = model(x, return_features=True) features = out["features"] # Replace the classification head model.reset_head(n_outputs=10)
Saving and restoring full configuration:
import json config = model.get_config() # all __init__ params with open("config.json", "w") as f: json.dump(config, f) model2 = MIRepNet.from_config(config) # reconstruct (no weights)
All model parameters (both EEG-specific and model-specific such as dropout rates, activation functions, number of filters) are automatically saved to the Hub and restored when loading.
See Loading and Adapting Pretrained Foundation Models for a complete tutorial.
Methods
- forward(x, return_features=None)[source]#
Define the computation performed at every call.
Should be overridden by all subclasses.
Note
Although the recipe for forward pass needs to be defined within this function, one should call the
Moduleinstance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.
- reset_head(n_outputs)[source]#
Replace the classification head for a new number of outputs.
This is called automatically by
from_pretrained()when the user passes ann_outputsthat differs from the saved config. Override in subclasses that need a model-specific head structure. Implementations keep changed constructor arguments in sync withself._update_init_kwargs, so that a saved model can be loaded back. Implementations requiring positive outputs can also useself._set_n_outputsto validate and record the new value.Examples
>>> from braindecode.models import BENDR >>> model = BENDR(n_chans=22, n_times=1000, n_outputs=4) >>> model.reset_head(10) >>> model.n_outputs 10
Added in version 1.4.