utils#
- sinabs.backend.dynapcnn.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.
- Return type:
nn.Sequential
- sinabs.backend.dynapcnn.utils.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[Module]) – List of layers
input_shape (Tuple[int, int, int]) – Shape of input (channels, height, width)
idx_start (int) – Starting index to scan the list. Default 0
dvs_input (bool)
- 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.
- Return type:
Tuple[DVSLayer | None, int, float]
- sinabs.backend.dynapcnn.utils.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 (int) – 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
- Return type:
Tuple[DynapcnnLayer, int, float]
- sinabs.backend.dynapcnn.utils.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.
- Return type:
Tuple[SumPool2d | None, int, float]
- sinabs.backend.dynapcnn.utils.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) – Cropping2dLayer
input_shape (Tuple[int, int]) – (height, width) input dimensions
- Return type:
Equivalent Crop2d layer
- sinabs.backend.dynapcnn.utils.convert_model_to_layer_list(model: Sequential | Network, ignore: Type | Tuple[Type, ...] = ()) List[Module] [source]#
Convert a model to a list of layers.
- Parameters:
model (nn.Sequential or sinabs.Network)
ignore (type or tuple of types of modules to be ignored)
- Return type:
List[nn.Module]
- sinabs.backend.dynapcnn.utils.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:
the extended model
- Return type:
DynapcnnNetwork
- sinabs.backend.dynapcnn.utils.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:
A string of the form device_type:index
- Return type:
str
- sinabs.backend.dynapcnn.utils.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[Module]) – List of modules
input_shape (Tuple[int, int, int] | None) – (channels, height, width)
- Returns:
(channels, height, width)
- Return type:
Output shape
- sinabs.backend.dynapcnn.utils.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
- Return type:
Convolutional layer including batch normalization
- sinabs.backend.dynapcnn.utils.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:
(device_type, index) Returns a tuple with the index and device type.
- Return type:
Tuple[str, int]