utils#

build_from_list(layers: List[Module], in_shape, discretize=True, dvs_input=False) Sequential[source]#

Build a sequential model of DVSLayer and DynapcnnLayer(s) given a list of layers comprising a spiking CNN.

Parameters:
  • layers (sequence of layer objects) –

  • in_shape (tuple of integers) – Shape of the input to the first layer in layers. Convention: (channels, height, width)

  • discretize (bool) – Discretize weights and thresholds if True

  • dvs_input (bool) – Whether model should receive DVS input. If True, the returned model will begin with a DVSLayer with disable_pixel_array set to False. Otherwise, the model starts with a DVSLayer only if the first element in layers is a pooling, cropping or flipping layer.

Returns:

nn.Sequential

construct_dvs_layer(layers: List[Module], input_shape: Tuple[int, int, int], idx_start: int = 0, dvs_input: bool = False) Tuple[DVSLayer | None, int, float][source]#

Generate a DVSLayer given a list of layers. If layers does not start with a pooling, cropping or flipping layer and dvs_input is False, will return None instead of a DVSLayer. NOTE: The number of channels is implicitly assumed to be 2 because of DVS

Parameters:
  • layers – List of layers

  • input_shape – Shape of input (channels, height, width)

  • idx_start – Starting index to scan the list. Default 0

Returns:

  • dvs_layer – None or DVSLayer

  • idx_next (int or None) – Index of first layer after this layer is constructed

  • rescale_factor (float) – Rescaling factor needed when turning AvgPool to SumPool. May differ from the pooling kernel in certain cases.

  • dvs_input (bool) – Whether DVSLayer should have pixel array activated.

construct_next_dynapcnn_layer(layers: List[Module], idx_start: int, in_shape: Tuple[int, int, int], discretize: bool, rescale_factor: float = 1) Tuple[DynapcnnLayer, int, float][source]#

Generate a DynapcnnLayer from a Conv2d layer and its subsequent spiking and pooling layers.

Parameters:
  • layers (sequence of layer objects) – First object must be Conv2d, next must be an IAF layer. All pooling layers that follow immediately are consolidated. Layers after this will be ignored.

  • idx_start – Layer index to start construction from

  • in_shape (tuple of integers) – Shape of the input to the first layer in layers. Convention: (input features, height, width)

  • discretize (bool) – Discretize weights and thresholds if True

  • rescale_factor (float) – Weights of Conv2d layer are scaled down by this factor. Can be used to account for preceding average pooling that gets converted to sum pooling.

Returns:

  • dynapcnn_layer (DynapcnnLayer) – DynapcnnLayer

  • layer_idx_next (int) – Index of the next layer after this layer is constructed

  • rescale_factor (float) – rescaling factor to account for average pooling

construct_next_pooling_layer(layers: List[Module], idx_start: int) Tuple[SumPool2d | None, int, float][source]#

Consolidate the first AvgPool2d objects in layers until the first object of different type.

Parameters:
  • layers (Sequence of layer objects) – Contains AvgPool2d and other objects.

  • idx_start (int) – Layer index to start construction from

Returns:

  • lyr_pool (int or tuple of ints) – Consolidated pooling size.

  • idx_next (int) – Index of first object in layers that is not a AvgPool2d,

  • rescale_factor (float) – Rescaling factor needed when turning AvgPool to SumPool. May differ from the pooling kernel in certain cases.

convert_cropping2dlayer_to_crop2d(layer: Cropping2dLayer, input_shape: Tuple[int, int]) Crop2d[source]#

Convert a sinabs layer of type Cropping2dLayer to Crop2d layer

Parameters:
  • layer – Cropping2dLayer

  • input_shape – (height, width) input dimensions

Returns:

Equivalent Crop2d layer

convert_model_to_layer_list(model: Sequential | Network) List[Module][source]#

Convert a model to a list of layers.

Parameters:

model (nn.Sequential or sinabs.Network) –

Returns:

List[nn.Module]

extend_readout_layer(model: DynapcnnNetwork) DynapcnnNetwork[source]#

Return a copied and extended model with the readout layer extended to 4 times the number of output channels. For Speck 2E and 2F, to get readout with correct output index, we need to extend the final layer to 4 times the number of output.

Parameters:

model (DynapcnnNetwork) – the model to be extended

Returns:

DynapcnnNetwork – the extended model

get_device_id(device_type: str, index: int) str[source]#

Generate a device id string given a device type and its index

Parameters:
  • device_type (str) – Device type

  • index (int) – Device index

Returns:

str – A string of the form device_type:index

infer_input_shape(layers: List[Module], input_shape: Tuple[int, int, int] | None = None) Tuple[int, int, int][source]#

Checks if the input_shape is specified. If either of them are specified, then it checks if the information is consistent and returns the input shape.

Parameters:
  • layers – List of modules

  • input_shape – (channels, height, width)

Returns:

Output shape – (channels, height, width)

merge_conv_bn(conv, bn)[source]#

Merge a convolutional layer with subsequent batch normalization

Parameters:
  • conv (torch.nn.Conv2d) – Convolutional layer

  • bn (torch.nn.Batchnorm2d) – Batch normalization

Returns:

torch.nn.Conv2d (Convolutional layer including batch normalization)

parse_device_id(device_id: str) Tuple[str, int][source]#

Parse device id into device type and device index

Parameters:

device_id (str) – Device id typically of the form device_type:index. In case no index is specified, the default index of zero is returned.

Returns:

Tuple[str, int] – (device_type, index) Returns a tuple with the index and device type.

standardize_device_id(device_id: str) str[source]#

Standardize device id string

Parameters:

device_id (str) – Device id string. Could be of the form device_type or device_type:index

Returns:

str – Returns a sanitized device id of the form device_type:index