CNN2SNN Toolkit API¶
Tool functions¶
quantize¶

cnn2snn.
quantize
(model, weight_quantization=0, activ_quantization=0, input_weight_quantization=None, fold_BN=True)¶ Converts a standard sequential Keras model to a CNN2SNN Keras quantized model, compatible for Akida conversion.
This function returns a Keras model where the standard neural layers (Conv2D, SeparableConv2D, Dense) and the ReLU activations are replaced with CNN2SNN quantized layers (QuantizedConv2D, QuantizedSeparableConv2D, QuantizedDense, ActivationDiscreteRelu).
Several transformations are applied to the model:  the order of MaxPool and BatchNormalization layers are inverted so that BatchNormalization always happens first,  the batch normalization layers are folded into the previous layers.
This new model can be either directly converted to akida, or first retrained for a few epochs to recover any accuracy loss.
 Parameters
model (tf.keras.Model) – a standard Keras model
weight_quantization (int) –
sets all weights in the model to have a particular quantization bitwidth except for the weights in the first layer.
’0’ implements floating point 32bit weights.
’2’ through ‘8’ implements nbit weights where n is from 28 bits.
activ_quantization (int) –
sets all activations in the model to have a particular activation quantization bitwidth.
’0’ implements floating point 32bit activations.
’1’ through ‘8’ implements nbit weights where n is from 18 bits.
input_weight_quantization (int) –
sets weight quantization in the first layer. Defaults to weight_quantization value.
’None’ implements the same bitwidth as the other weights.
’0’ implements floating point 32bit weights.
’2’ through ‘8’ implements nbit weights where n is from 28 bits.
fold_BN (bool) – enable folding batch normalization layers with their corresponding neural layer.
 Returns
a quantized Keras model
 Return type
tf.keras.Model
quantize_layer¶

cnn2snn.
quantize_layer
(model, target_layer, bitwidth)¶ Converts a specific layer to a quantized version with the given bitwidth.
This function returns a Keras model where the target layer is converted to an equivalent quantized layer. All other layers are preserved.
 Parameters
model (tf.keras.Model) – a standard Keras model
target_layer – a standard or quantized Keras layer to be converted, or the index or name of the target layer.
bitwidth (int) – the desired quantization bitwidth. If zero, no quantization will be applied.
 Returns
a quantized Keras model
 Return type
tf.keras.Model
convert¶

cnn2snn.
convert
(model, file_path=None, input_scaling=1.0, 0, input_is_sparse=False)¶ Simple function to convert a Keras model to an Akida one.
These steps are performed:
Merge the depthwise+conv layers into a separable_conv one.
Generate an Akida model based on that model.
Convert weights from the Keras model to Akida.
Note
The relationship between Keras and Akida inputs is: input_akida = alpha * input_keras + beta, optional
 Parameters
model (
tf.keras.Model
) – a tf.keras modelfile_path (str, optional) – destination for the akida model. (Default value = None)
input_scaling (2 elements tuple, optional) – value of the input scaling. (Default value = (1.0,0))
input_is_sparse (bool, optional) – if True, input will be an InputData layer, otherwise it will be InputConvolutional. (Default value = False)
 Returns
an Akida model.
A detailed description of the input_scaling parameter is given in the user guide.
check_model_compatibility¶

cnn2snn.
check_model_compatibility
(model_keras, input_is_sparse=False)¶ Checks if a Keras model is compatible for cnn2snn conversion.
This function doesn’t convert the Keras model to an Akida model but only checks if the model design is compatible. The checks are performed at two different levels:
Some checks are done when the Keras model is scanned, during the generation of the model map.
Other checks are then done based on the model map.
Note that this function doesn’t check if the quantization bitwidths (weights or activations) are supported by the Akida Execution Engine or by the Akida NSoC.
1. How to build a compatible Keras quantized model?
The following lines give details and constraints on how to build a Keras model compatible for the conversion to an Akida model.
2. General information about layers
An Akida layer must be seen as a block of Keras layers starting with a processing layer (Conv2D, SeparableConv2D, Dense). All blocks of Keras layers except the last block must have exactly one activation layer (ReLU or ActivationDiscreteRelu). Other optional layers can be present in a block such as a pooling layer or a batch normalization layer. Here are all the supported Keras layers for an Akidacompatible model:
Processing layers:
tf.keras Conv2D/SeparableConv2D/Dense
cnn2snn QuantizedConv2D/QuantizedSeparableConv2D/QuantizedDense
Activation layers:
tf.keras ReLU
cnn2snn ActivationDiscreteRelu
any increasing activation function (only for the last block of layers) such as softmax, sigmoid set as last layer. This layer must derive from tf.keras.layers.Activation, and it will be removed during conversion to an Akida model.
Pooling layers:
MaxPool2D
GlobalAvgPool2D
BatchNormalization
Dropout
Flatten
Input
Reshape
Example of a block of Keras layers:
  Conv2D    \/   BatchNormalization    \/   MaxPool2D    \/   ActivationDiscreteRelu  
3. Constraints about inputs
An Akida model can accept two types of inputs: sparse events or 8bit images. Whatever the input type, the Keras inputs must respect the following relation:
input_akida = scale * input_keras + shift
where the Akida inputs must be positive integers, the input scale must be a float value and the input shift must be an integer. In other words, scale * input_keras must be integers.
Depending on the input type:
if the inputs are events (sparse), the first layer of the Keras model can be any processing layer. The input shift must be zero.
if the inputs are images, the first layer must be a Conv2D layer.
4. Constraints about layers’ parameters
To be Akidacompatible, the Keras layers must observe the following rules:
all layers with the ‘data_format’ parameter must be ‘channels_last’
all processing quantized layers and ActivationDiscreteRelu must have a valid quantization bitwidth
a Dense layer must have an input shape of (N,) or (1, 1, N)
a BatchNormalization layer must have ‘axis’ set to 1 (default)
a BatchNormalization layer cannot have negative gammas
Reshape layers can only be used to transform a tensor of shape (N,) to a tensor of shape (1, 1, N), and viceversa
only one pooling layer can be used in each block
a MaxPool2D layer must have the same ‘padding’ as the corresponding processing quantized layer
5. Constraints about the order of layers
To be Akidacompatible, the order of Keras layers must observe the following rules:
a block of Keras layers must start with a processing quantized layer
where present, a BatchNormalization/GlobalAvgPool2D layer must be placed before the activation
a Flatten layer can only be used before a Dense layer
an Activation layer other than ActivationDiscreteRelu can only be used in the last layer
 Parameters
model (tf.keras model) – the model to parse.
input_is_sparse (bool) – if True, input will be an inputData layer, otherwise it will be inputConvolutional.
load_quantized_model¶

cnn2snn.
load_quantized_model
(filepath, compile=True)¶ Loads a quantized model saved in TF or HDF5 format.
If the model was compiled and trained before saving, its training state will be loaded as well. This function is a wrapper of tf.keras.models.load_model.
 Parameters
filepath (string) – path to the saved model.
compile (bool) – whether to compile the model after loading.
 Returns
a Keras model instance.
 Return type
tensorflow.keras.Model
load_partial_weights¶

cnn2snn.
load_partial_weights
(dest_model, src_model)¶ Loads a subset of weights from one Keras model to another
This goes through each layers of the source model, looking for a matching layer in the destination model. If a layer with the same name is found, then this method assumes that one of the two layer has the same set of weights as the other plus some extra weights at the end, and loads only the first common weights into the destination layer.
 Parameters
dest_model (
tensorflow.keras.Model
) – the destination Modelsrc_model (
tensorflow.keras.Model
) – the source Model
Quantizers¶
WeightQuantizer¶

class
cnn2snn.quantization_ops.
WeightQuantizer
(*args, **kwargs)¶ The base class for all weight quantizers.
This base class must be overloaded as well as the two functions quantize and scale_factor.
Quantizers derived from this class must be symmetric uniform midtread quantizers, in order to be compatible with the conversion into an Akida model. Quantization is usually done in two steps:
The weights must be first quantized on integer values in the range imposed by the bitwidth, e.g. from 7 to 7 for a 4bit quantization.
These integer weights are then reconstructed to float discretized values, in the range of the original weights. For example, 4bit integer weights are reconstructed on a grid from 7*qstep to 7*qstep, where qstep is the quantization step size between two levels of the uniform grid.
For a full explanation about midtread uniform quantization, one can take a look at the Wikipedia page.
The quantize function takes as inputs the original weights and must return the reconstructed float values after quantization. The scale_factor function must return the factor used to transform the float reconstructed weights into the integer values obtained after step 1. In other words, given a set of float weights “w”:
quantize(w) * scale_factor(w) is a set of integer weights.

__init__
(bitwidth)¶ Creates a WeightQuantizer for the specified bitwidth.
The bitwidth defines the number of quantization levels on which the weights will be quantized. For instance, a 4bit quantization gives integer values between 7 and 7. More generally, for a nbit quantization, values are ranged from kmax to kmax where kmax is (2^(n1)  1).
 Parameters
bitwidth (integer) – the quantization bitwidth
Methods:
Returns the config of the layer.
quantize
(w)Quantizes the specified weights Tensor.
scale_factor
(w)Evaluates the scale factor for the specified weights tf.Tensor.

get_config
()¶ Returns the config of the layer.
A layer config is a Python dictionary (serializable) containing the configuration of a layer. The same layer can be reinstantiated later (without its trained weights) from this configuration.
The config of a layer does not include connectivity information, nor the layer class name. These are handled by Network (one layer of abstraction above).
 Returns
Python dictionary.

quantize
(w)¶ Quantizes the specified weights Tensor.
This function must return a tf.Tensor containing float weights discretized on a uniform grid based on the scale factor “sf”. In other words, the discretized weights must be values among: kmax/sf, …, 2/sf, 1/sf, 0, 1/sf, 2/sf, …, kmax/sf
 Parameters
w (
tensorflow.Tensor
) – the weights Tensor to quantize. Returns
a Tensor of quantized weights.
 Return type
tensorflow.Tensor

scale_factor
(w)¶ Evaluates the scale factor for the specified weights tf.Tensor.
This function returns the scale factor to get the quantized integer weights from the reconstructed float weights. It is equal to the inverse of the quantization step size.
The scale factor can be a scalar that is applied on the whole tensor of weights. It can also be a vector of length the number of filters, where each value applies to the weights of the corresponding output filter. This is called a peraxis quantization, as opposed to a pertensor quantization. The number of filters is usually the last dimension of the weights tensor. More details are given here
Note that the quantizer_dw of a depthwise convolution in a QuantizedSeparableConv2D layer must imperatively return a scalar scale factor.
 Parameters
w (
tensorflow.Tensor
) – the weights Tensor to quantize. Returns
a Tensor containing a single scalar value.
 Return type
tensorflow.Tensor
LinearWeightQuantizer¶

class
cnn2snn.quantization_ops.
LinearWeightQuantizer
(*args, **kwargs)¶ An abstract linear weight quantizer
This abstract class proposes a linear symmetric and uniform quantization function. The “linear” term here means that there is no nonlinear transformation of the weights before the uniform quantization.
The scale_factor function must be overloaded.

__init__
(bitwidth)¶ Creates a WeightQuantizer for the specified bitwidth.
The bitwidth defines the number of quantization levels on which the weights will be quantized. For instance, a 4bit quantization gives integer values between 7 and 7. More generally, for a nbit quantization, values are ranged from kmax to kmax where kmax is (2^(n1)  1).
 Parameters
bitwidth (integer) – the quantization bitwidth
Methods:
quantize
(w)Linearly quantizes the input weights on a symmetric uniform grid based on the scale factor.

quantize
(w)¶ Linearly quantizes the input weights on a symmetric uniform grid based on the scale factor.
The input weights are directly rounded to the closest discretized value, without any transformation on the input weights.
The gradient is estimated using the StraightThrough Estimator (STE), i.e. the gradient is computed as if there were no quantization.

StdWeightQuantizer¶

class
cnn2snn.
StdWeightQuantizer
(*args, **kwargs)¶ A uniform quantizer.
Quantizes the specified weights into 2^bitwidth1 values centered on zero. E.g. with bitwidth = 4, 15 quantization levels: from 7 * qstep to 7 * qstep with qstep being the quantization step. The quantization step is defined by:
qstep = threshold * std(W) / max_value
with max_value being 2^(bitwidth1)  1. E.g with bitwidth = 4, max_value = 7.
All values below or above threshold * std(W) are automatically assigned to the min (resp max) value.

__init__
(threshold=3, bitwidth=4)¶ Creates a Weights quantizer for the specified bitwidth.
 Parameters
threshold (integer) – the standard deviation multiplier used to exclude outliers.
bitwidth (integer) – the quantizer bitwidth defining the number of quantized values.
Methods:
Returns the config of the layer.
scale_factor
(w)Evaluates the scale factor for the specified weights tf.Tensor.

get_config
()¶ Returns the config of the layer.
A layer config is a Python dictionary (serializable) containing the configuration of a layer. The same layer can be reinstantiated later (without its trained weights) from this configuration.
The config of a layer does not include connectivity information, nor the layer class name. These are handled by Network (one layer of abstraction above).
 Returns
Python dictionary.

scale_factor
(w)¶ Evaluates the scale factor for the specified weights tf.Tensor.
This function returns the scale factor to get the quantized integer weights from the reconstructed float weights. It is equal to the inverse of the quantization step size.
The scale factor can be a scalar that is applied on the whole tensor of weights. It can also be a vector of length the number of filters, where each value applies to the weights of the corresponding output filter. This is called a peraxis quantization, as opposed to a pertensor quantization. The number of filters is usually the last dimension of the weights tensor. More details are given here
Note that the quantizer_dw of a depthwise convolution in a QuantizedSeparableConv2D layer must imperatively return a scalar scale factor.
 Parameters
w (
tensorflow.Tensor
) – the weights Tensor to quantize. Returns
a Tensor containing a single scalar value.
 Return type
tensorflow.Tensor

TrainableStdWeightQuantizer¶

class
cnn2snn.
TrainableStdWeightQuantizer
(*args, **kwargs)¶ A trainable weight quantizer.
Quantizes the specified weights into 2^bitwidth1 values centered on zero. E.g. with bitwidth = 4, 15 quantization levels: from 7 * qstep to 7 * qstep with qstep being the quantization step. The quantization step is defined by:
qstep = threshold * std(W) / max_value
with:
max_value being 2^(bitwidth1)  1. E.g with bitwidth = 4, max_value = 7.
threshold a trainable parameter whose initial value can be specified.
All values below or above threshold * std(W) are automatically assigned to the min (resp max) value.
This is the trainable version of the StdWeightQuantizer class.

__init__
(threshold=3, bitwidth=4, **kwargs)¶ Creates a trainable weights quantizer for the specified bitwidth.
 Parameters
threshold (integer) – the initial value of the standard deviation multiplier used to exclude outliers.
bitwidth (integer) – the quantizer bitwidth defining the number of quantized values.
Methods:
Returns the config of the layer.
scale_factor
(w)Evaluates the scale factor for the specified weights tf.Tensor.

get_config
()¶ Returns the config of the layer.
A layer config is a Python dictionary (serializable) containing the configuration of a layer. The same layer can be reinstantiated later (without its trained weights) from this configuration.
The config of a layer does not include connectivity information, nor the layer class name. These are handled by Network (one layer of abstraction above).
 Returns
Python dictionary.

scale_factor
(w)¶ Evaluates the scale factor for the specified weights tf.Tensor.
This function returns the scale factor to get the quantized integer weights from the reconstructed float weights. It is equal to the inverse of the quantization step size.
The scale factor can be a scalar that is applied on the whole tensor of weights. It can also be a vector of length the number of filters, where each value applies to the weights of the corresponding output filter. This is called a peraxis quantization, as opposed to a pertensor quantization. The number of filters is usually the last dimension of the weights tensor. More details are given here
Note that the quantizer_dw of a depthwise convolution in a QuantizedSeparableConv2D layer must imperatively return a scalar scale factor.
 Parameters
w (
tensorflow.Tensor
) – the weights Tensor to quantize. Returns
a Tensor containing a single scalar value.
 Return type
tensorflow.Tensor
MaxQuantizer¶

class
cnn2snn.
MaxQuantizer
(*args, **kwargs)¶ A quantizer that relies on maximum range.
Quantizes the specified weights into 2^bitwidth1 values centered on zero. E.g. with bitwidth = 4, 15 quantization levels: from 7 * qstep to 7 * qstep with qstep being the quantization step. The quantization step is defined by:
qstep = max_range / max_value
with:
max_range = max(abs(W))
max_value = 2^(bitwidth1)  1. E.g with bitwidth = 4, max_value = 7.

__init__
(bitwidth=4)¶ Creates a Max quantizer for the specified bitwidth.
 Parameters
bitwidth (integer) – the quantizer bitwidth defining the number of quantized values.
Methods:
scale_factor
(w)Evaluates the scale factor for the specified weights tf.Tensor.

scale_factor
(w)¶ Evaluates the scale factor for the specified weights tf.Tensor.
This function returns the scale factor to get the quantized integer weights from the reconstructed float weights. It is equal to the inverse of the quantization step size.
The scale factor can be a scalar that is applied on the whole tensor of weights. It can also be a vector of length the number of filters, where each value applies to the weights of the corresponding output filter. This is called a peraxis quantization, as opposed to a pertensor quantization. The number of filters is usually the last dimension of the weights tensor. More details are given here
Note that the quantizer_dw of a depthwise convolution in a QuantizedSeparableConv2D layer must imperatively return a scalar scale factor.
 Parameters
w (
tensorflow.Tensor
) – the weights Tensor to quantize. Returns
a Tensor containing a single scalar value.
 Return type
tensorflow.Tensor
MaxPerAxisQuantizer¶

class
cnn2snn.
MaxPerAxisQuantizer
(*args, **kwargs)¶ A quantizer that relies on maximum range per axis.
Quantizes the specified weights into 2^bitwidth1 values centered on zero. E.g. with bitwidth = 4, 15 quantization levels: from 7 * qstep to 7 * qstep with qstep being the quantization step. The quantization step is defined by:
qstep = max_range / max_value
with:
max_range = max(abs(W))
max_value = 2^(bitwidth1)  1. E.g with bitwidth = 4, max_value = 7.
This is an evolution of the MaxQuantizer that defines the max_range per axis.
The last dimension is used as axis, meaning that the scaling factor is a vector with as many values as “filters”, or “neurons”.
Note: for a DepthwiseConv2D layer that has a single filter, this quantizer is strictly equivalent to the MaxQuantizer.

__init__
(bitwidth=4)¶ Creates a Max quantizer for the specified bitwidth.
 Parameters
bitwidth (integer) – the quantizer bitwidth defining the number of quantized values.
WeightFloat¶

class
cnn2snn.
WeightFloat
(*args, **kwargs)¶ This quantizer actually does not perform any quantization, and it might be used for training.

__init__
()¶ Creates a WeightQuantizer for the specified bitwidth.
The bitwidth defines the number of quantization levels on which the weights will be quantized. For instance, a 4bit quantization gives integer values between 7 and 7. More generally, for a nbit quantization, values are ranged from kmax to kmax where kmax is (2^(n1)  1).
 Parameters
bitwidth (integer) – the quantization bitwidth
Methods:
Returns the config of the layer.
quantize
(w)Quantizes the specified weights Tensor.
scale_factor
(w)Evaluates the scale factor for the specified weights tf.Tensor.

get_config
()¶ Returns the config of the layer.
A layer config is a Python dictionary (serializable) containing the configuration of a layer. The same layer can be reinstantiated later (without its trained weights) from this configuration.
The config of a layer does not include connectivity information, nor the layer class name. These are handled by Network (one layer of abstraction above).
 Returns
Python dictionary.

quantize
(w)¶ Quantizes the specified weights Tensor.
This function must return a tf.Tensor containing float weights discretized on a uniform grid based on the scale factor “sf”. In other words, the discretized weights must be values among: kmax/sf, …, 2/sf, 1/sf, 0, 1/sf, 2/sf, …, kmax/sf
 Parameters
w (
tensorflow.Tensor
) – the weights Tensor to quantize. Returns
a Tensor of quantized weights.
 Return type
tensorflow.Tensor

scale_factor
(w)¶ Evaluates the scale factor for the specified weights tf.Tensor.
This function returns the scale factor to get the quantized integer weights from the reconstructed float weights. It is equal to the inverse of the quantization step size.
The scale factor can be a scalar that is applied on the whole tensor of weights. It can also be a vector of length the number of filters, where each value applies to the weights of the corresponding output filter. This is called a peraxis quantization, as opposed to a pertensor quantization. The number of filters is usually the last dimension of the weights tensor. More details are given here
Note that the quantizer_dw of a depthwise convolution in a QuantizedSeparableConv2D layer must imperatively return a scalar scale factor.
 Parameters
w (
tensorflow.Tensor
) – the weights Tensor to quantize. Returns
a Tensor containing a single scalar value.
 Return type
tensorflow.Tensor

Quantized layers¶
QuantizedConv2D¶

class
cnn2snn.
QuantizedConv2D
(*args, **kwargs)¶ A quantizationaware Keras convolutional layer.
Inherits from Keras Conv2D layer, applying a quantization on weights during the forward pass.

__init__
(filters, kernel_size, strides=(1, 1), padding='valid', use_bias=True, kernel_initializer='glorot_uniform', bias_initializer='zeros', kernel_regularizer=None, bias_regularizer=None, kernel_constraint=None, bias_constraint=None, quantizer=<cnn2snn.quantization_ops.WeightFloat object>, **kwargs)¶ Creates a quantizationaware convolutional layer.
 Parameters
filters (integer) – the number of filters.
kernel_size (tuple of integer) – the kernel spatial dimensions.
strides (integer, or tuple of integers, optional) – strides of the convolution along height and width.
padding (str, optional) – one of ‘valid’ or ‘same’.
use_bias (boolean, optional) – whether the layer uses a bias vector.
kernel_initializer (str, or a
tf.keras.initializer
, optional) – initializer for the weights matrix.bias_initializer (str, or a
tf.keras.initializer
, optional) – initializer for the bias vector.kernel_regularizer (str, or a
tf.keras.regularizer
, optional) – regularization applied to the weights.bias_regularizer (str, or a
tf.keras.regularizer
, optional) – regularization applied to the bias.kernel_constraint (str, or a
tf.keras.constraint
, optional) – constraint applied to the weights.bias_constraint (str, or a
tf.keras.constraint
, optional) – constraint applied to the bias.quantizer (
cnn2snn.WeightQuantizer
, optional) – the quantizer to apply during the forward pass.
Methods:
call
(inputs)Evaluates input Tensor.
Returns the config of the layer.

call
(inputs)¶ Evaluates input Tensor.
This applies the quantization on weights, then evaluates the input Tensor and produces the output Tensor.
 Parameters
inputs (
tensorflow.Tensor
) – input Tensor. Returns
output Tensor.
 Return type
tensorflow.Tensor

get_config
()¶ Returns the config of the layer.
A layer config is a Python dictionary (serializable) containing the configuration of a layer. The same layer can be reinstantiated later (without its trained weights) from this configuration.
The config of a layer does not include connectivity information, nor the layer class name. These are handled by Network (one layer of abstraction above).
 Returns
Python dictionary.

QuantizedDepthwiseConv2D¶

class
cnn2snn.
QuantizedDepthwiseConv2D
(*args, **kwargs)¶ A quantizationaware Keras depthwise convolutional layer.
Inherits from Keras DepthwiseConv2D layer, applying a quantization on weights during the forward pass.

__init__
(kernel_size, strides=(1, 1), padding='valid', use_bias=True, depthwise_initializer='glorot_uniform', bias_initializer='zeros', depthwise_regularizer=None, bias_regularizer=None, depthwise_constraint=None, bias_constraint=None, quantizer=<cnn2snn.quantization_ops.WeightFloat object>, **kwargs)¶ Creates a quantizationaware depthwise convolutional layer.
 Parameters
kernel_size (a tuple of integer) – the kernel spatial dimensions.
strides (integer, or tuple of integers, optional) – strides of the convolution along height and width.
padding (str, optional) – One of ‘valid’ or ‘same’.
use_bias (boolean, optional) – whether the layer uses a bias vector.
depthwise_initializer (str, or a
tf.keras.initializer
, optional) – initializer for the weights matrix.bias_initializer (str, or a
tf.keras.initializer
, optional) – initializer for the bias vector.depthwise_regularizer (str, or a
tf.keras.initializer
, optional) – regularization applied to the weights.bias_regularizer (str, or a
tf.keras.initializer
, optional) – regularization applied to the bias.depthwise_constraint (str, or a
tf.keras.initializer
, optional) – constraint applied to the weights.bias_constraint (str, or a
tf.keras.initializer
, optional) – constraint applied to the bias.quantizer (
cnn2snn.WeightQuantizer
, optional) – the quantizer to apply during the forward pass.
Methods:
call
(inputs)Evaluates input Tensor.
Returns the config of the layer.

call
(inputs)¶ Evaluates input Tensor.
This applies the quantization on weights, then evaluates the input Tensor and produces the output Tensor.
 Parameters
inputs (
tensorflow.Tensor
) – input Tensor. Returns
output Tensor.
 Return type
tensorflow.Tensor

get_config
()¶ Returns the config of the layer.
A layer config is a Python dictionary (serializable) containing the configuration of a layer. The same layer can be reinstantiated later (without its trained weights) from this configuration.
The config of a layer does not include connectivity information, nor the layer class name. These are handled by Network (one layer of abstraction above).
 Returns
Python dictionary.

QuantizedDense¶

class
cnn2snn.
QuantizedDense
(*args, **kwargs)¶ A quantizationaware Keras dense layer.
Inherits from Keras Dense layer, applying a quantization on weights during the forward pass.

__init__
(units, use_bias=True, kernel_initializer='glorot_uniform', bias_initializer='zeros', kernel_regularizer=None, bias_regularizer=None, kernel_constraint=None, bias_constraint=None, quantizer=<cnn2snn.quantization_ops.WeightFloat object>, **kwargs)¶ Creates a quantizationaware dense layer.
 Parameters
units (integer) – the number of neurons.
use_bias (boolean, optional) – whether the layer uses a bias vector.
kernel_initializer (str, or a
tf.keras.initializer
, optional) – initializer for the weights matrix.bias_initializer (str, or a
tf.keras.initializer
, optional) – initializer for the bias vector.kernel_regularizer (str, or a
tf.keras.regularizer
, optional) – regularization applied to the weights.bias_regularizer (str, or a
tf.keras.regularizer
, optional) – regularization applied to the bias.kernel_constraint (str, or a
tf.keras.constraint
, optional) – constraint applied to the weights.bias_constraint (str, or a
tf.keras.constraint
, optional) – constraint applied to the bias.quantizer (
cnn2snn.WeightQuantizer
, optional) – the quantizer to apply during the forward pass.
Methods:
call
(inputs)Evaluates input Tensor.
Returns the config of the layer.

call
(inputs)¶ Evaluates input Tensor.
This applies the quantization on weights, then evaluates the input Tensor and produces the output Tensor.
 Parameters
inputs (
tensorflow.Tensor
) – input Tensor. Returns
output Tensor.
 Return type
tensorflow.Tensor

get_config
()¶ Returns the config of the layer.
A layer config is a Python dictionary (serializable) containing the configuration of a layer. The same layer can be reinstantiated later (without its trained weights) from this configuration.
The config of a layer does not include connectivity information, nor the layer class name. These are handled by Network (one layer of abstraction above).
 Returns
Python dictionary.

QuantizedSeparableConv2D¶

class
cnn2snn.
QuantizedSeparableConv2D
(*args, **kwargs)¶ A quantizationaware Keras separable convolutional layer.
Inherits from Keras SeparableConv2D layer, applying a quantization on weights during the forward pass.

__init__
(filters, kernel_size, strides=(1, 1), padding='valid', use_bias=True, depthwise_initializer='glorot_uniform', pointwise_initializer='glorot_uniform', bias_initializer='zeros', depthwise_regularizer=None, pointwise_regularizer=None, bias_regularizer=None, depthwise_constraint=None, pointwise_constraint=None, bias_constraint=None, quantizer=<cnn2snn.quantization_ops.WeightFloat object>, quantizer_dw=None, **kwargs)¶ Creates a quantizationaware separable convolutional layer.
 Parameters
filters (integer) – the number of filters.
kernel_size (tuple of integer) – the kernel spatial dimensions.
strides (integer, or tuple of integers, optional) – strides of the convolution along height and width.
padding (str, optional) – One of ‘valid’ or ‘same’.
use_bias (boolean, optional) – Whether the layer uses a bias vector.
depthwise_initializer (str, or a
tf.keras.initializer
, optional) – initializer for the depthwise kernel.pointwise_initializer (str, or a
tf.keras.initializer
, optional) – initializer for the pointwise kernel.bias_initializer (str, or a
tf.keras.initializer
, optional) – initializer for the bias vector.depthwise_regularizer (str, or a
tf.keras.regularizer
, optional) – regularization applied to the depthwise kernel.pointwise_regularizer (str, or a
tf.keras.regularizer
, optional) – regularization applied to the pointwise kernel.bias_regularizer (str, or a
tf.keras.regularizer
, optional) – regularization applied to the bias.depthwise_constraint (str, or a
tf.keras.constraint
, optional) – constraint applied to the depthwise kernel.pointwise_constraint (str, or a
tf.keras.constraint
, optional) – constraint applied to the pointwise kernel.bias_constraint (str, or a
tf.keras.constraint
, optional) – constraint applied to the bias.quantizer (
cnn2snn.WeightQuantizer
) – the quantizer to apply during the forward pass.
Methods:
call
(inputs)Evaluates input Tensor.
Returns the config of the layer.

call
(inputs)¶ Evaluates input Tensor.
This applies the quantization on weights, then evaluates the input Tensor and produces the output Tensor.
 Parameters
inputs (
tensorflow.Tensor
) – input Tensor. Returns
a Tensor.
 Return type
tensorflow.Tensor

get_config
()¶ Returns the config of the layer.
A layer config is a Python dictionary (serializable) containing the configuration of a layer. The same layer can be reinstantiated later (without its trained weights) from this configuration.
The config of a layer does not include connectivity information, nor the layer class name. These are handled by Network (one layer of abstraction above).
 Returns
Python dictionary.

QuantizedActivation¶

class
cnn2snn.
QuantizedActivation
(*args, **kwargs)¶ Base class for quantized activation layers.
This base class must be overloaded as well as the three @property functions:
threshold
step_height
step_width
These @property functions must return TensorFlow objects (e.g. tf.Tensor or tf.Variable) of scalar values. The .numpy() method must be callable on them. They can be fixed at initialization or can be trainable variables.
The CNN2SNN toolkit only support linear quantized activation as defined in the quantized_activation function.

__init__
(bitwidth, **kwargs)¶ Creates a QuantizedActivation for the specified bitwidth.
The bitwidth defines the number of quantization levels on which the activation will be quantized. For instance, a 4bit quantization gives 15 activation levels. More generally, a nbit quantization gives 2^n1 levels.
 Parameters
bitwidth (integer) – the quantization bitwidth
Methods:
call
(inputs)This is where the layer’s logic lives.
Returns the config of the layer.
Evaluates the quantized activations for the specified input Tensor.

call
(inputs)¶ This is where the layer’s logic lives.
 Parameters
inputs – Input tensor, or list/tuple of input tensors.
**kwargs – Additional keyword arguments.
 Returns
A tensor or list/tuple of tensors.

get_config
()¶ Returns the config of the layer.
A layer config is a Python dictionary (serializable) containing the configuration of a layer. The same layer can be reinstantiated later (without its trained weights) from this configuration.
The config of a layer does not include connectivity information, nor the layer class name. These are handled by Network (one layer of abstraction above).
 Returns
Python dictionary.

quantized_activation
(x)¶ Evaluates the quantized activations for the specified input Tensor.
The quantization is defined by three parameters:
the activation threshold, ‘threshold’
the quantization step size, ‘step_height’
the step width, ‘step_width’
For any potential x, the activation output is as follows:
if x <= threshold, activation is zero
if threshold < x <= threshold + step_width, activation is step_height
if threshold + step_width < x <= threshold + 2*step_width, activation is 2*step_height
and so on…
if x > threshold + levels*step_width, activation is levels*step_height
 Parameters
x (
tensorflow.Tensor
) – the input values.
ActivationDiscreteRelu¶

class
cnn2snn.
ActivationDiscreteRelu
(*args, **kwargs)¶ A discrete ReLU Keras Activation.
Activations will be quantized and will have 2^bitwidth values in the range [0,6].

__init__
(bitwidth=1, **kwargs)¶ Creates a discrete ReLU for the specified bitwidth.
 Parameters
bitwidth (int) – the activation bitwidth.

QuantizedReLU¶

class
cnn2snn.
QuantizedReLU
(*args, **kwargs)¶ A Trainable Quantized ReLU Keras Activation.
Activations will be clipped to a trainable range, and quantized to a number of values defined by the bitwidth: N = (2^bitwidth  1) values plus zero
More specifically, this class uses two trainable variables:
threshold represents the lower bound of the activation range,
step_height represents the step between two quantized activation values.
The activation range is therefore [threshold, tN_k], with:
tN_k = threshold + N * step_height = (2^bitwidth  1) * step_height
In other words:
inputs below threshold will result in no activation
inputs between threshold and threshold + tN_k will be ceiled to the nearest threshold + n * step_height, and result in a activation of n * step_height
inputs above threshold + tN_k will result in a activation of N * step_height

__init__
(bitwidth=1, **kwargs)¶ Creates a QuantizedReLU for the specified bitwidth.
 Parameters
bitwidth (int) – the activation bitwidth.