braindecode.models.Deep4Net#

class braindecode.models.Deep4Net(n_chans=None, n_outputs=None, n_times=None, final_conv_length='auto', n_filters_time=25, n_filters_spat=25, filter_time_length=10, pool_time_length=3, pool_time_stride=3, n_filters_2=50, filter_length_2=10, n_filters_3=100, filter_length_3=10, n_filters_4=200, filter_length_4=10, activation_first_conv_nonlin=<class 'torch.nn.modules.activation.ELU'>, first_pool_mode='max', first_pool_nonlin=<class 'torch.nn.modules.linear.Identity'>, activation_later_conv_nonlin=<class 'torch.nn.modules.activation.ELU'>, later_pool_mode='max', later_pool_nonlin=<class 'torch.nn.modules.linear.Identity'>, drop_prob=0.5, split_first_layer=True, batch_norm=True, batch_norm_alpha=0.1, stride_before_pool=False, chs_info=None, input_window_seconds=None, sfreq=None)[source]#

Deep ConvNet model from Schirrmeister et al (2017) [Schirrmeister2017].

Convolution

Deep4Net Architecture

Model described in [Schirrmeister2017].

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.

  • final_conv_length (int | str) – Length of the final convolution layer. If set to “auto”, n_times must not be None. Default: “auto”.

  • n_filters_time (int) – Number of temporal filters.

  • n_filters_spat (int) – Number of spatial filters.

  • filter_time_length (int) – Length of the temporal filter in layer 1.

  • pool_time_length (int) – Length of temporal pooling filter.

  • pool_time_stride (int) – Length of stride between temporal pooling filters.

  • n_filters_2 (int) – Number of temporal filters in layer 2.

  • filter_length_2 (int) – Length of the temporal filter in layer 2.

  • n_filters_3 (int) – Number of temporal filters in layer 3.

  • filter_length_3 (int) – Length of the temporal filter in layer 3.

  • n_filters_4 (int) – Number of temporal filters in layer 4.

  • filter_length_4 (int) – Length of the temporal filter in layer 4.

  • activation_first_conv_nonlin (type[Module]) – Non-linear activation function to be used after convolution in layer 1.

  • first_pool_mode (str) – Pooling mode in layer 1. “max” or “mean”.

  • first_pool_nonlin (type[Module]) – Non-linear activation function to be used after pooling in layer 1.

  • activation_later_conv_nonlin (type[Module]) – Non-linear activation function to be used after convolution in later layers.

  • later_pool_mode (str) – Pooling mode in later layers. “max” or “mean”.

  • later_pool_nonlin (type[Module]) – Non-linear activation function to be used after pooling in later layers.

  • drop_prob (float) – Dropout probability.

  • split_first_layer (bool) – Split first layer into temporal and spatial layers (True) or just use temporal (False). There would be no non-linearity between the split layers.

  • batch_norm (bool) – Whether to use batch normalisation.

  • batch_norm_alpha (float) – Momentum for BatchNorm2d.

  • stride_before_pool (bool) – Stride before pooling.

  • chs_info (list of dict) – Information about each individual EEG channel. This should be filled with info["chs"]. Refer to mne.Info for more details.

  • input_window_seconds (float) – Length of the input window in seconds.

  • sfreq (float) – Sampling frequency of the EEG recordings.

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

[Schirrmeister2017] (1,2)

Schirrmeister, R. T., Springenberg, J. T., Fiederer, L. D. J., Glasstetter, M., Eggensperger, K., Tangermann, M., Hutter, F. & Ball, T. (2017). Deep learning with convolutional neural networks for EEG decoding and visualization. Human Brain Mapping , Aug. 2017. Online: http://dx.doi.org/10.1002/hbm.23730

Hugging Face Hub integration

When the optional huggingface_hub package 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 Deep4Net

# Train your model
model = Deep4Net(n_chans=22, n_outputs=4, n_times=1000)
# ... training code ...

# Push to the Hub
model.push_to_hub(
    repo_id="username/my-deep4net-model",
    commit_message="Initial model upload",
)

Loading a model from the Hub:

from braindecode.models import Deep4Net

# Load pretrained model
model = Deep4Net.from_pretrained("username/my-deep4net-model")

# Load with a different number of outputs (head is rebuilt automatically)
model = Deep4Net.from_pretrained("username/my-deep4net-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 = Deep4Net.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.

Examples using braindecode.models.Deep4Net#

Convolutional neural network regression model on fake data.

Convolutional neural network regression model on fake data.

Benchmarking eager and lazy loading

Benchmarking eager and lazy loading

Transfer learning across EEG pathology datasets

Transfer learning across EEG pathology datasets