ResNet1D
- class selfeeg.models.zoo.ResNet1D(nb_classes: int, Chans: int, Samples: int, block: ~torch.nn.modules.module.Module = <class 'selfeeg.models.encoders.BasicBlock1'>, Layers: list of 4 int = [2, 2, 2, 2], inplane: int = 16, kernLength: int = 7, addConnection: bool = False, preBlock: ~torch.nn.modules.module.Module = None, postBlock: ~torch.nn.modules.module.Module = None, classifier: ~torch.nn.modules.module.Module = None, return_logits: bool = True, seed: int = None)[source]
Pytorch implementation of the Resnet model
This model adopts temporal convolutional layers, so conv2d layers with horizontal kernel. Implemented using as reference the following paper [res1] .
The expected input is a 3D tensor with size (Batch x Channels x Samples).
- Parameters:
nb_classes (int) – The number of classes. If less than 2, a binary classification problem is considered (output dimensions will be [batch, 1] in this case).
Chans (int) – The number of EEG channels.
Samples (int) – The sample length. It will be used to calculate the embedding size (for head initialization).
block (nn.Module, optional) –
An nn.Module defining the resnet block.
Default: selfeeg.models.BasicBlock1
Layers (list of 4 int, optional) –
A list of integers indicating the number of times the resnet block is repeated at each stage. It must be a list of length 4 with positive integers. Shorter lists are padded to 1 on the right. Only the first four elements of longer lists are considered. Zeros are changed to 1.
Default = [2, 2, 2, 2]
inplane (int, optional) –
The number of output filters.
Default = 16
kernLength (int, optional) –
The length of the temporal convolutional layer.
Default = 7
addConnection (bool, optional) –
Whether to add a connection from the start of the resnet part to the network head. If set to True the output of the following conv2d will be concatenate to the postblock output:
nn.Conv2d(inplane, 2, kernel_size=(Chans, kernLength), stride=(1, int(kernLength//2)), padding=(0, 0), bias=False)
Default = None
preBlock (nn.Module, optional) –
A custom nn.Module to pass before entering the sequence of resnet blocks. If none is left, the following sequence is used:
nn.conv2d(1, self.inplane, kernel_size=(1, kernLength), stride=(1, 2), padding=(0, kernLength//2), bias=False)
nn.BatchNorm2d()
nn.ReLU()
Default = None
postBlock (nn.Module, optional) –
A custom nn.Module to pass after the sequence of resnet blocks and before the network head. If none is left, the following sequence is used:
nn.conv2d(1, self.inplane, kernel_size=(1, kernLength), bias=False)
nn.BatchNorm2d()
nn.ReLU()
Default = None
classifier (nn.Module, optional) –
A custom nn.Module defining the network head. If none is left, a single dense layer is used.
Default = None
return_logits (bool, optional) –
Whether to return the output as logit or probability. It is suggested to not use False as the pytorch crossentropy applies the softmax internally.
Default = True
seed (int, optional) –
A custom seed for model initialization. It must be a nonnegative number. If None is passed, no custom seed will be set
Default = None
Note
The compatibility between each custom nn.Module given as argument has not been carefully checked. Errors may arise.
References
[res1]Zheng et al., Task-oriented Self-supervised Learning for Anomaly Detection in Electroencephalography
Example
>>> import selfeeg.models >>> import torch >>> x = torch.randn(4,8,512) >>> mdl = models.ResNet1D(4,8,512) >>> out = mdl(x) >>> print(out.shape) # shoud return torch.Size([4, 4]) >>> print(torch.isnan(out).sum()) # shoud return 0