braindecode.models.EEGTCNet#
- class braindecode.models.EEGTCNet(n_chans=None, n_outputs=None, n_times=None, chs_info=None, input_window_seconds=None, sfreq=None, activation=<class 'torch.nn.modules.activation.ELU'>, depth_multiplier=2, filter_1=8, kern_length=64, drop_prob=0.5, depth=2, kernel_size=4, filters=12, max_norm_const=0.25, drop_prob_eeg=None, drop_prob_tcn=None, tcn_batch_norm=True, act_after_temporal_bn=False)[source]#
EEGTCNet model from Ingolfsson et al (2020) [ingolfsson2020].
Convolution Recurrent
Combining EEGNet and TCN blocks.
- Parameters:
n_chans (int) – Number of EEG channels.
n_outputs (int) – Number of outputs of the model. This is the number of classes in the case of classification.
n_times (int) – Number of time samples of the input window.
chs_info (list of dict) – Information about each individual EEG channel. This should be filled with
info["chs"]. Refer tomne.Infofor more details.input_window_seconds (float) – Length of the input window in seconds.
sfreq (float) – Sampling frequency of the EEG recordings.
activation (
type[Module]) – Activation function to use. Default is nn.ELU().depth_multiplier (
int) – Depth multiplier for the depthwise convolution. Default is 2.filter_1 (
int) – Number of temporal filters in the first convolutional layer. Default is 8.kern_length (
int) – Length of the temporal kernel in the first convolutional layer. Default is 64.drop_prob (
float) – Default dropout rate, used for both the EEGNet front-end and the TCN block unless overridden bydrop_prob_eeg/drop_prob_tcn. Default is 0.5.depth (
int) – Number of residual blocks in the TCN. Default is 2.kernel_size (
int) – Size of the temporal convolutional kernel in the TCN. Default is 4.filters (
int) – Number of filters in the TCN convolutional layers. Default is 12.max_norm_const (
float) – Maximum L2-norm constraint imposed on weights of the last fully-connected layer. Defaults to 0.25.drop_prob_eeg (
float|None) – Dropout rate for the EEGNet front-end. IfNone(default), falls back todrop_prob. The source/paper uses0.2for BCI IV 2a.drop_prob_tcn (
float|None) – Dropout rate for the TCN block. IfNone(default), falls back todrop_prob. The source/paper uses0.3for BCI IV 2a.tcn_batch_norm (
bool) –If
True(default), insert aBatchNorm1dafter each TCN convolution and use a bias in the residual1x1downsample, matching the original Keras source. Set toFalseto get the TCN block of braindecode before 1.6.1, for example to load checkpoints trained with it. To also get the outputs of that version, setact_after_temporal_bn=True. Default isTrue.Added in version 1.6.1.
act_after_temporal_bn (
bool) –If
True, applyactivationbetween the temporal batch norm and the depthwise convolution, as braindecode did before 2.1. The authors’ code has no activation there. Default isFalse.Added in version 2.1.
- Raises:
ValueError – If some input signal-related parameters are not specified: and can not be inferred.
Notes
If some input signal-related parameters are not specified, there will be an attempt to infer them from the other parameters.
References
[ingolfsson2020]Ingolfsson, T. M., Hersche, M., Wang, X., Kobayashi, N., Cavigelli, L., & Benini, L. (2020). EEG-TCNet: An accurate temporal convolutional network for embedded motor-imagery brain–machine interfaces. https://doi.org/10.48550/arXiv.2006.00622
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 EEGTCNet # Train your model model = EEGTCNet(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-eegtcnet-model", commit_message="Initial model upload", model_card_kwargs={"datasets": ["BNCI2014_001"]}, )
Loading a model from the Hub:
from braindecode.models import EEGTCNet # Load pretrained model model = EEGTCNet.from_pretrained("username/my-eegtcnet-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 = EEGTCNet.from_pretrained("username/my-eegtcnet-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 = EEGTCNet.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