CNN2SNN Toolkit API¶
CNN2SNN toolkit to quantize and convert Keras models into Akidacompatible models.
Tool functions¶
quantize¶

cnn2snn.
quantize
(model, weight_quantization=0, activ_quantization=0, input_weight_quantization=None, fold_BN=True)[source]¶ 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)[source]¶ Quantizes a specific layer with the given bitwidth.
This function returns a Keras model where the target layer is quantized. All other layers are preserved. If the target layer is a native Keras layer (Conv2D, SeparableConv2D, Dense, ReLU), it is replaced by a CNN2SNN quantized layer (QuantizedConv2D, QuantizedSeparableConv2D, QuantizedDense, ActivationDiscreteRelu). If the target layer is an already quantized layer, only the bitwidth is modified.
Examples
>>> # Quantize a layer of a native Keras model >>> model = tf.keras.Sequential([ ... tf.keras.layers.Dense(5, input_shape=(3,)), ... tf.keras.layers.Softmax()]) >>> model_quantized = cnn2snn.quantize_layer(model, ... target_layer=0, ... bitwidth=4) >>> assert isinstance(model_quantized.layers[0], cnn2snn.QuantizedDense) >>> print(model_quantized.layers[0].quantizer.bitwidth) 4
>>> # Quantize a layer of an an already quantized layer >>> model_quantized = cnn2snn.quantize_layer(model_quantized, ... target_layer=0, bitwidth=2) >>> print(model_quantized.layers[0].quantizer.bitwidth) 2
 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
 Raises
ValueError – In case of invalid target layer
ValueError – If bitwidth is not greater than zero
convert¶

cnn2snn.
convert
(model, file_path=None, input_scaling=None, input_is_image=True)[source]¶ Converts a Keras quantized model to an Akida one.
After quantizing a Keras model with
cnn2snn.quantize()
, it can be converted to an Akida model. By default, the conversion expects that the Akida model takes 8bit images as inputs.input_scaling
defines how the images have been rescaled to be fed into the Keras model (see note below).If inputs are spikes, you can set
input_is_image=False
. In this case, Akida inputs are then expected to be integers between 0 and 15.Note
The relationship between Keras and Akida inputs is defined as:
input_akida = input_scaling[0] * input_keras + input_scaling[1].
If a
tf.keras.layers.experimental.preprocessing.Rescaling
layer is present as first layer of the model,input_scaling
must be None: theRescaling
parameters will be used to compute the input scaling.Examples
>>> # Convert a quantized Keras model with Keras inputs as images >>> # rescaled between 1 and 1 >>> inputs_akida = images.astype('uint8') >>> inputs_keras = (images.astype('float32')  128) / 128 >>> model_akida = cnn2snn.convert(model_keras, input_scaling=(128, 128)) >>> model_akida.evaluate(inputs_akida)
>>> # Convert a quantized Keras model with Keras inputs as spikes and >>> # input scaling of (2.5, 0). Akida spikes must be integers between >>> # 0 and 15 >>> inputs_akida = spikes.astype('uint8') >>> inputs_keras = spikes.astype('float32') / 2.5 >>> model_akida = cnn2snn.convert(model_keras, input_scaling=(2.5, 0)) >>> model_akida.evaluate(inputs_akida)
>>> # Convert and directly save the Akida model to fbz file. >>> cnn2snn.convert(model_keras, 'model_akida.fbz')
 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 = None)
input_is_image (bool, optional) – True if input is an image (3D 8bit input with 1 or 3 channels) followed by QuantizedConv2D. Akida model input will be InputConvolutional. If False, Akida model input will be InputData. (Default value = True)
 Returns
an Akida model.
 Return type
 Raises
ValueError – If
input_scaling[0]
is null or negative.ValueError – If a
Rescaling
layer is present andinput_scaling
is not None.SystemError – If Tensorflow is not run in eager mode.
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_image=True)[source]¶ 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_image (bool, optional) – True if input is an image (8bit input with 1 or 3 channels) followed by QuantizedConv2D. Akida model input will be InputConvolutional. If False, Akida model input will be InputData. (Default value = True)
load_quantized_model¶

cnn2snn.
load_quantized_model
(filepath, custom_objects=None, compile_model=True)[source]¶ 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.
custom_objects (dict) – optional dictionary mapping names (strings) to custom classes or functions to be considered during deserialization.
compile_model (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)[source]¶ 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)[source]¶ Bases:
tensorflow.python.keras.engine.base_layer.Layer
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.
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 (int) – the quantization bitwidth.
Attributes:
Returns the bitwidth of the quantizer
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.

property
bitwidth
¶ Returns the bitwidth of the quantizer

get_config
()[source]¶ 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)[source]¶ 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)[source]¶ 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)[source]¶ Bases:
cnn2snn.quantization_ops.WeightQuantizer
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.
Methods:
quantize
(w)Linearly quantizes the input weights on a symmetric uniform grid based on the scale factor.
scale_factor
(w)Evaluates the scale factor for the specified weights tf.Tensor.

quantize
(w)[source]¶ 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.

scale_factor
(w)[source]¶ 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

StdWeightQuantizer¶

class
cnn2snn.
StdWeightQuantizer
(*args, **kwargs)[source]¶ Bases:
cnn2snn.quantization_ops.LinearWeightQuantizer
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.
 Parameters
threshold (int) – the standard deviation multiplier used to exclude outliers.
bitwidth (int) – 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 the range on which the weights are quantized.

get_config
()[source]¶ 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)[source]¶ 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)[source]¶ Bases:
cnn2snn.quantization_ops.LinearWeightQuantizer
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.
 Parameters
threshold (int) – the initial value of the standard deviation multiplier used to exclude outliers.
bitwidth (int) – 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 the range on which the weights are quantized.

get_config
()[source]¶ 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)[source]¶ 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)[source]¶ Bases:
cnn2snn.quantization_ops.LinearWeightQuantizer
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.
 Parameters
bitwidth (int) – the quantizer bitwidth defining the number of quantized values.
Methods:
max_range_
(w)Get the range on which the weights are quantized.
scale_factor
(w)Evaluates the scale factor for the specified weights tf.Tensor.

static
max_range_
(w)[source]¶ Get the range on which the weights are quantized. This quantizer discretizes weights in the range:
[max(weights) ; max(weights)]

scale_factor
(w)[source]¶ 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)[source]¶ Bases:
cnn2snn.quantization_ops.MaxQuantizer
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.
Methods:
max_range_
(w)Get the range on which the weights are quantized.
Quantized layers¶
QuantizedConv2D¶

class
cnn2snn.
QuantizedConv2D
(*args, **kwargs)[source]¶ Bases:
tensorflow.python.keras.layers.convolutional.Conv2D
A quantizationaware Keras convolutional layer.
Inherits from Keras Conv2D layer, applying a quantization on weights during the forward pass.
 Parameters
filters (int) – the number of filters.
kernel_size (tuple of integer) – the kernel spatial dimensions.
quantizer (
cnn2snn.WeightQuantizer
) – the quantizer to apply during the forward pass.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.(str (activity_regularizer) – optional): regularization applied to the output of the layer.
:param or a
tf.keras.regularizer
: optional): regularization applied to the output of the layer. :param : optional): regularization applied to the output of the layer. :param kernel_constraint: constraint applied to the weights. :type kernel_constraint: str, or atf.keras.constraint
, optional :param bias_constraint: constraint applied to the bias. :type bias_constraint: str, or atf.keras.constraint
, optionalMethods:
call
(inputs)Evaluates input Tensor.
Returns the config of the layer.

call
(inputs)[source]¶ 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
()[source]¶ 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)[source]¶ Bases:
tensorflow.python.keras.layers.convolutional.DepthwiseConv2D
A quantizationaware Keras depthwise convolutional layer.
Inherits from Keras DepthwiseConv2D layer, applying a quantization on weights during the forward pass.
 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.
quantizer (
cnn2snn.WeightQuantizer
) – the quantizer to apply during the forward pass.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.(str (activity_regularizer) – optional): regularization applied to the output of the layer.
:param or a
tf.keras.regularizer
: optional): regularization applied to the output of the layer. :param : optional): regularization applied to the output of the layer. :param depthwise_constraint: constraint applied to the weights. :type depthwise_constraint: str, or atf.keras.initializer
, optional :param bias_constraint: constraint applied to the bias. :type bias_constraint: str, or atf.keras.initializer
, optionalMethods:
call
(inputs)Evaluates input Tensor.
Returns the config of the layer.

call
(inputs)[source]¶ 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
()[source]¶ 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)[source]¶ Bases:
tensorflow.python.keras.layers.core.Dense
A quantizationaware Keras dense layer.
Inherits from Keras Dense layer, applying a quantization on weights during the forward pass.
 Parameters
units (int) – the number of neurons.
use_bias (boolean, optional) – whether the layer uses a bias vector.
quantizer (
cnn2snn.WeightQuantizer
) – the quantizer to apply during the forward pass.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.(str (activity_regularizer) – optional): regularization applied to the output of the layer.
:param or a
tf.keras.regularizer
: optional): regularization applied to the output of the layer. :param : optional): regularization applied to the output of the layer. :param kernel_constraint: constraint applied to the weights. :type kernel_constraint: str, or atf.keras.constraint
, optional :param bias_constraint: constraint applied to the bias. :type bias_constraint: str, or atf.keras.constraint
, optionalMethods:
call
(inputs)Evaluates input Tensor.
Returns the config of the layer.

call
(inputs)[source]¶ 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
()[source]¶ 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)[source]¶ Bases:
tensorflow.python.keras.layers.convolutional.SeparableConv2D
A quantizationaware Keras separable convolutional layer.
Inherits from Keras SeparableConv2D layer, applying a quantization on weights during the forward pass.
Creates a quantizationaware separable convolutional layer.
 Parameters
filters (int) – the number of filters.
kernel_size (tuple of integer) – the kernel spatial dimensions.
quantizer (
cnn2snn.WeightQuantizer
) – the quantizer to apply during the forward pass.quantizer_dw (
cnn2snn.WeightQuantizer
, optional) – the depthwise quantizer to apply during the forward pass.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.(str (activity_regularizer) – optional): regularization applied to the output of the layer.
:param or a
tf.keras.regularizer
: optional): regularization applied to the output of the layer. :param : optional): regularization applied to the output of the layer. :param depthwise_constraint: constraint applied to the depthwise kernel. :type depthwise_constraint: str, or atf.keras.constraint
, optional :param pointwise_constraint: constraint applied to the pointwise kernel. :type pointwise_constraint: str, or atf.keras.constraint
, optional :param bias_constraint: constraint applied to the bias. :type bias_constraint: str, or atf.keras.constraint
, optionalMethods:
call
(inputs)Evaluates input Tensor.
Returns the config of the layer.

call
(inputs)[source]¶ 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
()[source]¶ 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)[source]¶ Bases:
tensorflow.python.keras.engine.base_layer.Layer
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.
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 (int) – the quantization bitwidth
Attributes:
Returns the bitwidth of the quantized activation
Returns the step height of the quantized activation
Returns the step width of the quantized activation
Returns the activation threshold
Methods:
call
(inputs, **kwargs)This is where the layer’s logic lives.
Returns the config of the layer.
Evaluates the quantized activations for the specified input Tensor.

property
bitwidth
¶ Returns the bitwidth of the quantized activation

call
(inputs, **kwargs)[source]¶ This is where the layer’s logic lives.
Note here that call() method in tf.keras is little bit different from keras API. In keras API, you can pass support masking for layers as additional arguments. Whereas tf.keras has compute_mask() method to support masking.
 Parameters
inputs – Input tensor, or list/tuple of input tensors.
**kwargs – Additional keyword arguments. Currently unused.
 Returns
A tensor or list/tuple of tensors.

get_config
()[source]¶ 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)[source]¶ 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.

property
step_height
¶ Returns the step height of the quantized activation

property
step_width
¶ Returns the step width of the quantized activation

property
threshold
¶ Returns the activation threshold
ActivationDiscreteRelu¶

class
cnn2snn.
ActivationDiscreteRelu
(*args, **kwargs)[source]¶ Bases:
cnn2snn.quantization_layers.QuantizedActivation
A discrete ReLU Keras Activation.
Activations will be quantized and will have 2^bitwidth values in the range [0,6].
 Parameters
bitwidth (int) – the activation bitwidth.
Attributes:
Returns the step height of the quantized activation
Returns the step width of the quantized activation
Returns the activation threshold

property
step_height
¶ Returns the step height of the quantized activation

property
step_width
¶ Returns the step width of the quantized activation

property
threshold
¶ Returns the activation threshold
QuantizedReLU¶

class
cnn2snn.
QuantizedReLU
(*args, **kwargs)[source]¶ Bases:
cnn2snn.quantization_layers.QuantizedActivation
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
 Parameters
bitwidth (int) – the activation bitwidth.
Attributes:
Returns the step height of the quantized activation
Returns the step width of the quantized activation
Returns the activation threshold

property
step_height
¶ Returns the step height of the quantized activation

property
step_width
¶ Returns the step width of the quantized activation

property
threshold
¶ Returns the activation threshold