braindecode.models.SPARCNet#
- class braindecode.models.SPARCNet(n_chans=None, n_times=None, n_outputs=None, block_layers=4, growth_rate=16, bottleneck_size=16, drop_prob=0.5, conv_bias=True, batch_norm=True, activation=<class 'torch.nn.modules.activation.ELU'>, kernel_size_conv0=7, kernel_size_conv1=1, kernel_size_conv2=3, kernel_size_pool=3, stride_pool=2, stride_conv0=2, stride_conv1=1, stride_conv2=1, padding_pool=1, padding_conv0=3, padding_conv2=1, kernel_size_trans=2, stride_trans=2, chs_info=None, input_window_seconds=None, sfreq=None)[source]#
Seizures, Periodic and Rhythmic pattern Continuum Neural Network (SPaRCNet) from Jing et al (2023) [jing2023].
Convolution
This is a temporal CNN model for biosignal classification based on the DenseNet architecture.
The model is based on the unofficial implementation [Code2023].
Added in version 0.9.
- Parameters:
n_chans (int) – Number of EEG channels.
n_times (int) – Number of time samples of the input window.
n_outputs (int) – Number of outputs of the model. This is the number of classes in the case of classification.
block_layers (
int) – Number of layers per dense block. Default is 4.growth_rate (
int) – Growth rate of the DenseNet. Default is 16.bottleneck_size (
int) – The description is missing.drop_prob (
float) – Dropout rate. Default is 0.5.conv_bias (
bool) – Whether to use bias in convolutional layers. Default is True.batch_norm (
bool) – Whether to use batch normalization. Default is True.activation (
type[Module]) – Activation function class to apply. Should be a PyTorch activation module class likenn.ReLUornn.ELU. Default isnn.ELU.kernel_size_conv0 (
int) – The description is missing.kernel_size_conv1 (
int) – The description is missing.kernel_size_conv2 (
int) – The description is missing.kernel_size_pool (
int) – The description is missing.stride_pool (
int) – The description is missing.stride_conv0 (
int) – The description is missing.stride_conv1 (
int) – The description is missing.stride_conv2 (
int) – The description is missing.padding_pool (
int) – The description is missing.padding_conv0 (
int) – The description is missing.padding_conv2 (
int) – The description is missing.kernel_size_trans (
int) – The description is missing.stride_trans (
int) – The description is missing.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.
- Raises:
ValueError – If some input signal-related parameters are not specified: and can not be inferred.
Notes
This implementation is not guaranteed to be correct, has not been checked by original authors.
References
[jing2023]Jing, J., Ge, W., Hong, S., Fernandes, M. B., Lin, Z., Yang, C., … & Westover, M. B. (2023). Development of expert-level classification of seizures and rhythmic and periodic patterns during eeg interpretation. Neurology, 100(17), e1750-e1762.
[Code2023]Yang, C., Westover, M.B. and Sun, J., 2023. BIOT Biosignal Transformer for Cross-data Learning in the Wild. GitHub https://github.com/ycq091044/BIOT (accessed 2024-02-13)
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 SPARCNet # Train your model model = SPARCNet(n_chans=22, n_outputs=4, n_times=1000) # ... training code ... # Push to the Hub model.push_to_hub( repo_id="username/my-sparcnet-model", commit_message="Initial model upload", )
Loading a model from the Hub:
from braindecode.models import SPARCNet # Load pretrained model model = SPARCNet.from_pretrained("username/my-sparcnet-model") # Load with a different number of outputs (head is rebuilt automatically) model = SPARCNet.from_pretrained("username/my-sparcnet-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 = SPARCNet.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