snntoolbox.parsing
¶
On the input side of the SNN conversion toolbox, models from the following neural network libraries can be parsed:
snntoolbox.parsing.model_libs.keras_input_lib |
Keras model parser. |
snntoolbox.parsing.model_libs.pytorch_input_lib |
PyTorch model parser. |
snntoolbox.parsing.model_libs.lasagne_input_lib |
|
snntoolbox.parsing.model_libs.caffe_input_lib |
These parsers inherit from AbstractModelParser
in
snntoolbox.parsing.utils |
Functions common to input model parsers. |
See Extending the toolbox on how to extend the toolbox by another input model library.
snntoolbox.parsing.model_libs
¶
keras_input_lib
¶
Keras model parser.
@author: rbodo
-
class
snntoolbox.parsing.model_libs.keras_input_lib.
ModelParser
(input_model, config)[source]¶ Bases:
snntoolbox.parsing.utils.AbstractModelParser
-
get_layer_iterable
()[source]¶ Get an iterable over the layers of the network.
Returns: layers Return type: list
-
get_type
(layer)[source]¶ Get layer class name.
Returns: layer_type – Layer class name. Return type: str
-
get_batchnorm_parameters
(layer)[source]¶ Get the parameters of a batch-normalization layer.
Returns: mean, var_eps_sqrt_inv, gamma, beta, axis Return type: tuple
-
get_inbound_layers
(layer)[source]¶ Get inbound layers of
layer
.Returns: inbound Return type: Sequence
-
layers_to_skip
¶ Return a list of layer names that should be skipped during conversion to a spiking network.
Returns: self._layers_to_skip Return type: List[str]
-
initialize_attributes
(layer=None)[source]¶ Return a dictionary that will be used to collect all attributes of a layer. This dictionary can then be used to instantiate a new parsed layer.
-
get_input_shape
()[source]¶ Get the input shape of a network, not including batch size.
Returns: input_shape – Input shape. Return type: tuple
-
get_output_shape
(layer)[source]¶ Get output shape of a
layer
.Parameters: layer – Layer. Returns: output_shape – Output shape of layer
.Return type: Sized
-
parse_dense
(layer, attributes)[source]¶ Parse a fully-connected layer.
Parameters: - layer – Layer.
- attributes (dict) – The layer attributes as key-value pairs in a dict.
-
parse_convolution
(layer, attributes)[source]¶ Parse a convolutional layer.
Parameters: - layer – Layer.
- attributes (dict) – The layer attributes as key-value pairs in a dict.
-
parse_depthwiseconvolution
(layer, attributes)[source]¶ Parse a depthwise convolution layer.
Parameters: - layer – Layer.
- attributes (dict) – The layer attributes as key-value pairs in a dict.
-
parse_pooling
(layer, attributes)[source]¶ Parse a pooling layer.
Parameters: - layer – Layer.
- attributes (dict) – The layer attributes as key-value pairs in a dict.
-
get_activation
(layer)[source]¶ Get the activation string of an activation
layer
.Parameters: layer – Layer Returns: activation – String indicating the activation of the layer
.Return type: str
-
-
snntoolbox.parsing.model_libs.keras_input_lib.
load
(path, filename, **kwargs)[source]¶ Load network from file.
Parameters: Returns: A dictionary of objects that constitute the input model. It must contain the following two keys:
- ’model’: keras.models.Sequential
- Keras model instance of the network.
- ’val_fn’: function
- Function that allows evaluating the original model.
Return type:
-
snntoolbox.parsing.model_libs.keras_input_lib.
evaluate
(val_fn, batch_size, num_to_test, x_test=None, y_test=None, dataflow=None)[source]¶ Evaluate the original ANN.
Can use either numpy arrays
x_test, y_test
containing the test samples, or generate them with a dataflow (Keras.ImageDataGenerator.flow_from_directory
object).Parameters:
pytorch_input_lib
¶
PyTorch model parser.
@author: rbodo
-
class
snntoolbox.parsing.model_libs.pytorch_input_lib.
ModelParser
(input_model, config)[source]¶ Bases:
snntoolbox.parsing.model_libs.keras_input_lib.ModelParser
-
snntoolbox.parsing.model_libs.pytorch_input_lib.
load
(path, filename)[source]¶ Load network from file.
Parameters: Returns: A dictionary of objects that constitute the input model. It must contain the following two keys:
- ’model’: keras.models.Sequential
- Keras model instance of the network.
- ’val_fn’: function
- Function that allows evaluating the original model.
Return type:
lasagne_input_lib
¶
caffe_input_lib
¶
snntoolbox.parsing.utils
¶
Functions common to input model parsers.
The core of this module is an abstract base class extracts an input model written in some neural network library and prepares it for further processing in the SNN toolbox.
AbstractModelParser |
Abstract base class for neural network model parsers. |
The idea is to make all further steps in the conversion/simulation pipeline independent of the original model format.
Other functions help navigate through the network in order to explore network connectivity and layer attributes:
get_type |
Get type of Keras layer. |
has_weights |
Return True if layer has weights. |
get_fanin |
Return fan-in of a neuron in layer . |
get_fanout |
Return fan-out of a neuron in layer . |
get_inbound_layers |
Return inbound layers. |
get_inbound_layers_with_params |
Iterate until inbound layers are found that have parameters. |
get_inbound_layers_without_params |
Return inbound layers. |
get_outbound_layers |
Return outbound layers. |
get_outbound_activation |
Iterate over 2 outbound layers to find an activation layer. |
@author: rbodo
-
class
snntoolbox.parsing.utils.
AbstractModelParser
(input_model, config)[source]¶ Bases:
object
Abstract base class for neural network model parsers.
Parameters: - input_model – The input network object.
- config (configparser.Configparser) – Contains the toolbox configuration for a particular experiment.
-
config
¶ Contains the toolbox configuration for a particular experiment.
Type: configparser.Configparser
-
_layer_list
¶ A list where each entry is a dictionary containing layer specifications. Obtained by calling
parse
. Used to build new, parsed Keras model.Type: list[dict]
-
_layer_dict
¶ Maps the layer names of the specific input model library to our standard names (currently Keras).
Type: dict
-
parsed_model
¶ The parsed model.
Type: keras.models.Model
-
parse
()[source]¶ Extract the essential information about a neural network.
This method serves to abstract the conversion process of a network from the language the input model was built in (e.g. Keras or Lasagne).
The methods iterates over all layers of the input model and writes the layer specifications and parameters into
_layer_list
. The keys are chosen in accordance with Keras layer attributes to facilitate instantiation of a new, parsed Keras model (done in a later step bybuild_parsed_model
).This function applies several simplifications and adaptations to prepare the model for conversion to spiking. These modifications include:
- Removing layers only used during training (Dropout, BatchNormalization, …)
- Absorbing the parameters of BatchNormalization layers into the parameters of the preceeding layer. This does not affect performance because batch-norm-parameters are constant at inference time.
- Removing ReLU activation layers, because their function is inherent
to the spike generation mechanism. The information which nonlinearity
was used in the original model is preserved in the
activation
key in_layer_list
. If the output layer employs the softmax function, a spiking version is used when testing the SNN in INIsim or MegaSim simulators. - Inserting a Flatten layer between Conv and FC layers, if the input model did not explicitly include one.
-
get_layer_iterable
()[source]¶ Get an iterable over the layers of the network.
Returns: layers Return type: list
-
get_type
(layer)[source]¶ Get layer class name.
Returns: layer_type – Layer class name. Return type: str
-
get_batchnorm_parameters
(layer)[source]¶ Get the parameters of a batch-normalization layer.
Returns: mean, var_eps_sqrt_inv, gamma, beta, axis Return type: tuple
-
get_inbound_layers_with_parameters
(layer)[source]¶ Iterate until inbound layers are found that have parameters.
Parameters: layer – Layer Returns: List of inbound layers. Return type: list
-
get_inbound_names
(layer, name_map)[source]¶ Get names of inbound layers.
Parameters: Returns: The names of inbound layers.
Return type:
-
get_inbound_layers
(layer)[source]¶ Get inbound layers of
layer
.Returns: inbound Return type: Sequence
-
layers_to_skip
¶ Return a list of layer names that should be skipped during conversion to a spiking network.
Returns: self._layers_to_skip Return type: List[str]
-
initialize_attributes
(layer=None)[source]¶ Return a dictionary that will be used to collect all attributes of a layer. This dictionary can then be used to instantiate a new parsed layer.
-
get_input_shape
()[source]¶ Get the input shape of a network, not including batch size.
Returns: input_shape – Input shape. Return type: tuple
-
get_batch_input_shape
()[source]¶ Get the input shape of a network, including batch size.
Returns: batch_input_shape – Batch input shape. Return type: tuple
-
get_name
(layer, idx, layer_type=None)[source]¶ Create a name for a
layer
.The format is <layer_num><layer_type>_<layer_shape>.
>>> # Name of first convolution layer with 32 feature maps and >>> # dimension 64x64: "00Conv2D_32x64x64" >>> # Name of final dense layer with 100 units: "06Dense_100"
Parameters: Returns: name – Layer name.
Return type:
-
format_layer_idx
(idx)[source]¶ Pad the layer index with the appropriate amount of zeros.
The number of zeros used for padding is determined by the maximum index (i.e. the number of layers in the network).
Parameters: idx (int) – Layer index. Returns: num_str – Zero-padded layer index. Return type: str
-
get_output_shape
(layer)[source]¶ Get output shape of a
layer
.Parameters: layer – Layer. Returns: output_shape – Output shape of layer
.Return type: Sized
-
parse_dense
(layer, attributes)[source]¶ Parse a fully-connected layer.
Parameters: - layer – Layer.
- attributes (dict) – The layer attributes as key-value pairs in a dict.
-
parse_convolution
(layer, attributes)[source]¶ Parse a convolutional layer.
Parameters: - layer – Layer.
- attributes (dict) – The layer attributes as key-value pairs in a dict.
-
parse_depthwiseconvolution
(layer, attributes)[source]¶ Parse a depthwise convolution layer.
Parameters: - layer – Layer.
- attributes (dict) – The layer attributes as key-value pairs in a dict.
-
parse_pooling
(layer, attributes)[source]¶ Parse a pooling layer.
Parameters: - layer – Layer.
- attributes (dict) – The layer attributes as key-value pairs in a dict.
-
absorb_activation
(layer, attributes)[source]¶ Detect what activation is used by the layer.
Sometimes the Dense or Conv layer specifies its activation directly, sometimes it is followed by a dedicated Activation layer (possibly with BatchNormalization in between). Here we try to find such an activation layer, and add this information to the Dense/Conv layer itself. The separate Activation layer can then be removed.
Parameters: - layer – Layer.
- attributes (dict) – The layer attributes as key-value pairs in a dict.
-
get_activation
(layer)[source]¶ Get the activation string of an activation
layer
.Parameters: layer – Layer Returns: activation – String indicating the activation of the layer
.Return type: str
-
get_outbound_layers
(layer)[source]¶ Get outbound layers of
layer
.Parameters: layer – Layer. Returns: outbound – Outbound layers of layer
.Return type: list
-
parse_concatenate
(layer, attributes)[source]¶ Parse a concatenation layer.
Parameters: - layer – Layer.
- attributes (dict) – The layer attributes as key-value pairs in a dict.
-
build_parsed_model
()[source]¶ Create a Keras model suitable for conversion to SNN.
This method uses the specifications in
_layer_list
to build a Keras model. The resulting model contains all essential information about the original network, independently of the model library in which the original network was built (e.g. Caffe).Returns: parsed_model – A Keras model, functionally equivalent to input_model
.Return type: keras.models.Model
-
snntoolbox.parsing.utils.
absorb_bn_parameters
(weight, bias, mean, var_eps_sqrt_inv, gamma, beta, axis, image_data_format, is_depthwise=False)[source]¶ Absorb the parameters of a batch-normalization layer into the previous layer.
-
snntoolbox.parsing.utils.
padding_string
(pad, pool_size)[source]¶ Get string defining the border mode.
Parameters: Returns: padding – Border mode identifier.
Return type:
-
snntoolbox.parsing.utils.
load_parameters
(filepath)[source]¶ Load all layer parameters from an HDF5 file.
-
snntoolbox.parsing.utils.
save_parameters
(params, filepath, fileformat='h5')[source]¶ Save all layer parameters to an HDF5 file.
-
snntoolbox.parsing.utils.
has_weights
(layer)[source]¶ Return
True
if layer has weights.Parameters: layer (keras.layers.Layer) – Keras layer Returns: True
if layer has weights.Return type: bool
-
snntoolbox.parsing.utils.
get_inbound_layers_with_params
(layer)[source]¶ Iterate until inbound layers are found that have parameters.
Parameters: layer (keras.layers.Layer) – Layer Returns: List of inbound layers. Return type: list
-
snntoolbox.parsing.utils.
get_inbound_layers_without_params
(layer)[source]¶ Return inbound layers.
Parameters: layer (Keras.layers) – A Keras layer. Returns: List of inbound layers. Return type: list[Keras.layers]
-
snntoolbox.parsing.utils.
get_inbound_layers
(layer)[source]¶ Return inbound layers.
Parameters: layer (Keras.layers) – A Keras layer. Returns: List of inbound layers. Return type: list[Keras.layers]
-
snntoolbox.parsing.utils.
get_outbound_layers
(layer)[source]¶ Return outbound layers.
Parameters: layer (Keras.layers) – A Keras layer. Returns: List of outbound layers. Return type: list[Keras.layers]
-
snntoolbox.parsing.utils.
get_outbound_activation
(layer)[source]¶ Iterate over 2 outbound layers to find an activation layer. If there is no activation layer, take the activation of the current layer.
Parameters: layer (Union[keras.layers.Conv2D, keras.layers.Dense]) – Layer Returns: activation – Name of outbound activation type. Return type: str
-
snntoolbox.parsing.utils.
get_fanin
(layer)[source]¶ Return fan-in of a neuron in
layer
.Parameters: layer (Subclass[keras.layers.Layer]) – Layer. Returns: fanin – Fan-in. Return type: int
-
snntoolbox.parsing.utils.
get_fanout
(layer, config)[source]¶ Return fan-out of a neuron in
layer
.Parameters: - layer (Subclass[keras.layers.Layer]) – Layer.
- config (configparser.ConfigParser) – Settings.
Returns: fanout – Fan-out. The fan-out of a neuron projecting onto a convolution layer varies between neurons in a feature map if the stride of the convolution layer is greater than unity. In this case, return an array of the same shape as the layer.
Return type: Union[int, ndarray]
-
snntoolbox.parsing.utils.
has_stride_unity
(layer)[source]¶ Return
True
if the strides in all dimensions of alayer
are 1.
-
snntoolbox.parsing.utils.
get_fanout_array
(layer_pre, layer_post, is_depthwise_conv=False)[source]¶ Return an array of the same shape as
layer_pre
, where each entry gives the number of outgoing connections of a neuron. In convolution layers where the post-synaptic layer has stride > 1, the fan-out varies between neurons.
-
snntoolbox.parsing.utils.
get_type
(layer)[source]¶ Get type of Keras layer.
Parameters: layer (Keras.layers.Layer) – Keras layer. Returns: Layer type. Return type: str
-
snntoolbox.parsing.utils.
get_quantized_activation_function_from_string
(activation_str)[source]¶ Parse a string describing the activation of a layer, and return the corresponding activation function.
Parameters: activation_str (str) – Describes activation. Returns: activation – Activation function. Return type: functools.partial Examples
>>> f = get_quantized_activation_function_from_string('relu_Q1.15') >>> f functools.partial(<function reduce_precision at 0x7f919af92b70>, f='15', m='1') >>> print(f.__name__) relu_Q1.15
-
snntoolbox.parsing.utils.
get_custom_activation
(activation_str)[source]¶ If
activation_str
describes a custom activation function, import this function fromsnntoolbox.utils.utils
and return it. If custom activation function is not found or implemented, return theactivation_str
in place of the activation function.Parameters: activation_str (str) – Describes activation. Returns: - activation – Activation function.
- activation_str (str) – Describes activation.
-
snntoolbox.parsing.utils.
get_custom_layers_dict
(filepath=None)[source]¶ Import all implemented custom layers so they can be used when loading a Keras model.
Parameters: filepath (Optional[str]) – Path to json file containing additional custom objects.
-
snntoolbox.parsing.utils.
get_custom_activations_dict
(filepath=None)[source]¶ Import all implemented custom activation functions so they can be used when loading a Keras model.
Parameters: filepath (Optional[str]) – Path to json file containing additional custom objects.
-
snntoolbox.parsing.utils.
check_for_custom_activations
(layer_attributes)[source]¶ Check if the layer contains a custom activation function, and deal with it appropriately.
Parameters: layer_attributes (dict) – A dictionary containing the attributes of the layer.