utils#

build_from_list(layers: List[torch.nn.modules.module.Module], in_shape, discretize=True) torch.nn.modules.container.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

Returns

nn.Sequential

construct_dvs_layer(layers: List[torch.nn.modules.module.Module], input_shape: Tuple[int, int, int], idx_start=0) Tuple[Optional[sinabs.backend.dynapcnn.dvs_layer.DVSLayer], int, float][source]#

Generate a DVSLayer given a list of layers 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.

construct_next_dynapcnn_layer(layers: List[torch.nn.modules.module.Module], idx_start: int, in_shape: Tuple[int, int, int], discretize: bool, rescale_factor: float = 1) Tuple[sinabs.backend.dynapcnn.dynapcnn_layer.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[torch.nn.modules.module.Module], idx_start: int) Tuple[Optional[sinabs.layers.pool2d.SumPool2d], 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: sinabs.layers.crop2d.Cropping2dLayer, input_shape: Tuple[int, int]) sinabs.backend.dynapcnn.crop2d.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: Union[torch.nn.modules.container.Sequential, sinabs.network.Network]) List[torch.nn.modules.module.Module][source]#

Convert a model to a list of layers.

Parameters

model (nn.Sequential or sinabs.Network) –

Returns

List[nn.Module]

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[torch.nn.modules.module.Module], input_shape: Optional[Tuple[int, int, int]] = 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