ResNet1DEncoder

class selfeeg.models.encoders.ResNet1DEncoder(Chans: int, block: ~torch.nn.modules.module.Module = <class 'selfeeg.models.encoders.BasicBlock1'>, Layers: list of 4 ints = [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, seed: int = None)[source]

Pytorch implementation of the Resnet Encoder

This version uses temporal convolutional layers (so conv2d with horizontal kernel). See ResNet for the reference paper which inspired this implementation.

The expected input is a 3D tensor with size (Batch x Channels x Samples).

Parameters:
  • Chans (int) – The number of EEG channels.

  • block (nn.Module, optional) – An nn.Module defining the resnet block.

  • 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.

  • kernLength (int, optional) –

    The length of the temporal convolutional layer.

    Default = 25

  • 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:

    1. 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:

    1. nn.conv2d(1, self.inplane, kernel_size=(1, kernLength), stride=(1, 2), padding=(0, kernLength//2), bias=False)

    2. nn.BatchNorm2d()

    3. 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:

    1. nn.conv2d(1, self.inplane, kernel_size=(1, kernLength), bias=False)

    2. nn.BatchNorm2d()

    3. nn.ReLU()

    Default = None

  • 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 beend checked. Design the network carefully.

Example

>>> import selfeeg.models
>>> import torch
>>> x = torch.randn(4,8,512)
>>> mdl = models.ResNet1DEncoder(8)
>>> out = mdl(x)
>>> print(out.shape) # shoud return torch.Size([4, 296])
>>> print(torch.isnan(out).sum()) # shoud return 0