qualia_plugin_snn.learningmodel.pytorch.SResNet module

Contains the template for a residual spiking neural network.

class qualia_plugin_snn.learningmodel.pytorch.SResNet.BasicBlockBuilder[source]

Bases: Protocol

Signature for basicblockbuilder.

Used to bind hyperparameters constant across all the ResNet blocks.

__call__(in_planes: int, planes: int, kernel_size: int, stride: int, padding: int) BasicBlock[source]

Build a BasicBlock.

Parameters:
  • in_planes (int) – Number of input channels

  • planes (int) – Number of filters (i.e., output channels) in the main branch Conv layers

  • kernel_size (int) – kernel_size for the main branch Conv layers

  • stride (int) – kernel_size for the MaxPool layers, no MaxPool layer added if 1

  • padding (int) – Padding for the main branch Conv layers

Returns:

A BasicBlock

Return type:

BasicBlock

__init__(*args, **kwargs)
class qualia_plugin_snn.learningmodel.pytorch.SResNet.BasicBlock[source]

Bases: Module

A single ResNetv1 block.

Structure is:

        |
      /   \
    |       |
   Conv     |
    |       |
BatchNorm   |
    |       |
 MaxPool   Conv
    |       |
    IF  BatchNorm
    |       |
   Conv  MaxPool
    |       |
BatchNorm   IF
    |       |
    IF      |
    |       |
      \   /
        |
       Add

Main (left) branch Conv use kernel_size=kernel_size, while residual (right) branch Conv use kernel_size=1.

BatchNorm layers will be absent if batch_norm == False

MaxPool layer will be absent if stride == 1.

Residual (right) branch Conv layer will be asbent if in_planes==planes, except if force_projection_with_stride==True and stride != 1.

__init__(sjlayers_t: ModuleType, in_planes: int, planes: int, kernel_size: int, stride: int, padding: int, batch_norm: bool, bn_momentum: float, force_projection_with_stride: bool, create_neuron: Callable[[], Module], step_mode: str) None[source]

Construct BasicBlock.

Parameters:
  • sjlayers_t (ModuleType) – Module containing the aliased layers to use (1D or 2D)

  • in_planes (int) – Number of input channels

  • planes (int) – Number of filters (i.e., output channels) in the main branch Conv layers

  • kernel_size (int) – kernel_size for the main branch Conv layers

  • stride (int) – kernel_size for the MaxPool layers, no MaxPool layer added if 1

  • padding (int) – Padding for the main branch Conv layers

  • batch_norm (bool) – If True, add BatchNorm layer after each Conv layer

  • bn_momentum (float) – BatchNorm layer momentum

  • force_projection_with_stride (bool) – If True, residual Conv layer is kept when stride != 1 even if in_planes == planes

  • create_neuron (Callable[[], Module]) – qualia_plugin_snn.learningmodel.pytorch.SNN.SNN.create_neuron() method to instantiate a spiking neuron

  • step_mode (str) – SpikingJelly step_mode from qualia_plugin_snn.learningmodel.pytorch.SNN.SNN.step_mode

Return type:

None

expansion: int = 1

Unused

forward(input: Tensor) Tensor[source]

Forward of ResNet block.

Parameters:

input (Tensor) – Input tensor

Returns:

Output tensor

Return type:

Tensor

class qualia_plugin_snn.learningmodel.pytorch.SResNet.SResNet[source]

Bases: SNN

Residual spiking neural network template.

Similar to qualia_core.learningmodel.pytorch.ResNet.ResNet but with spiking neuron activation layers (e.g., IF) instead of torch.nn.ReLU.

Example TOML configuration for a 2D ResNetv1-18 over 4 timesteps with soft-reset multi-step IF based on the SResNet template:

[[model]]
name = "SResNetv1-18"
params.filters      = [64, 64, 128, 256, 512]
params.kernel_sizes = [ 7,  3,   3,   3,   3]
params.paddings     = [ 3,  1,   1,   1,   1]
params.strides      = [ 2,  1,   1,   1,   1]
params.num_blocks   = [     2,   2,   2,   2]
params.prepool      = 1
params.postpool     = 'max'
params.batch_norm   = true
params.dims         = 2
params.timesteps    = 4
params.neuron.kind  = 'IFNode'
params.neuron.params.v_reset = false # Soft reset
params.neuron.params.v_threshold = 1.0
params.neuron.params.detach_reset = true
params.neuron.params.step_mode = 'm' # Multi-step mode, make sure to use SpikingJellyMultiStep learningframework
params.neuron.params.backend = 'torch'
__init__(input_shape: tuple[int, ...], output_shape: tuple[int, ...], filters: list[int], kernel_sizes: list[int], num_blocks: list[int], strides: list[int], paddings: list[int], prepool: int = 1, postpool: str = 'max', batch_norm: bool = False, bn_momentum: float = 0.1, force_projection_with_stride: bool = True, neuron: RecursiveConfigDict | None = None, timesteps: int = 2, dims: int = 1, basicblockbuilder: BasicBlockBuilder | None = None) None[source]

Construct SResNet.

Structure is:

  Input
    |
 AvgPool
    |
   Conv
    |
BatchNorm
    |
    IF
    |
BasicBlock
    |
    …
    |
BasicBlock
    |
GlobalPool
    |
 Flatten
    |
  Linear
Parameters:
  • input_shape (tuple[int, ...]) – Input shape

  • output_shape (tuple[int, ...]) – Output shape

  • filters (list[int]) – List of out_channels for Conv layers inside each BasicBlock group, must be of the same size as num_blocks, first element is for the first Conv layer at the beginning of the network

  • kernel_sizes (list[int]) – List of kernel_size for Conv layers inside each BasicBlock group, must of the same size as num_blocks, first element is for the first Conv layer at the beginning of the network

  • num_blocks (list[int]) – List of number of BasicBlock in each group, also defines the number of BasicBlock groups inside the network

  • strides (list[int]) – List of kernel_size for MaxPool layers inside each BasicBlock group, must of the same size as num_blocks, stride is applied only to the first BasicBlock of the group, next BasicBlock in the group use a stride of 1, first element is the stride of the first Conv layer at the beginning of the network

  • paddings (list[int]) – List of padding for Conv layer inside each BasicBlock group, must of the same size as num_blocks, first element is for the first Conv layer at the beginning of the network

  • prepool (int) – AvgPool layer kernel_size to add at the beginning of the network, no layer added if 0

  • postpool (str) – Global pooling layer type after all BasicBlock, either max for MaxPool or avg for AvgPool

  • batch_norm (bool) – If True, add a BatchNorm layer after each Conv layer, otherwise no layer added

  • bn_momentum (float) – BatchNorm momentum

  • force_projection_with_stride (bool) – If True, residual Conv layer is kept when stride != 1 even if in_planes == planes inside a BasicBlock

  • neuron (RecursiveConfigDict | None) – Spiking neuron configuration, see qualia_plugin_snn.learningmodel.pytorch.SNN.SNN.__init__()

  • timesteps (int) – Number of timesteps

  • dims (int) – Either 1 or 2 for 1D or 2D convolutional network.

  • basicblockbuilder (BasicBlockBuilder | None) – Optional function with BasicBlockBuilder.__call__() signature to build a basic block after binding constants common across all basic blocks

Return type:

None

forward(input: Tensor) Tensor[source]

Forward of residual spiking neural network.

Parameters:

input (Tensor) – Input tensor

Returns:

Output tensor

Return type:

Tensor