Available Functions#

The following functions are available for a conversion from Keras and PyTorch to AIfES. They are sorted by the used interface.

AIfES Express#

AIfES Express is an interface that allows easy and fast setup of FFNNs. It offers an intuitive interface and allows for changes of the model during runtime. We also offer the interface AIfES, see the AIfES description further down the page. The following type specific converter functions exist:

Float 32#

aifes.keras2aifes.convert_to_fnn_f32_express(keras_model: Model, output_path: str, with_weights=True)#

Converts the given Keras model to AIfES Express F32. Creates one header file with the model in the output_path that can be included to any AIfES project. If weights are extracted as well, a second header file is created, which contains the flattened weights of the Keras model.

Parameters:
  • keras_model – Keras model, which should be converted

  • output_path – File path, where the converted model should be stored. If the folder doesn’t exist, it will be created by this function.

  • with_weights – Extracts the weights and bias from the given model and creates a separate header file with flatten weights.

aifes.pytorch2aifes.convert_to_fnn_f32_express(pytorch_model: Module, output_path: str, with_weights=True)#

Converts the given PyTorch model to AIfES Express F32. Creates one header file with the model in the output_path that can be included to any AIfES project. If weights are extracted as well, a second header file is created, which contains the flattened weights of the PyTorch model.

Parameters:
  • pytorch_model – PyTorch model, which should be converted

  • output_path – File path, where the converted model should be stored. If the folder doesn’t exist, it will be created by this function.

  • with_weights – Extracts the weights and bias from the given model and creates a separate header file with flatten weights.

Quantized Q7#

aifes.keras2aifes.convert_to_fnn_q7_express(keras_model: Model, output_path: str, representative_data: ndarray, target_alignment: int, byteorder: str)#

Converts the given Keras model to AIfES Express Q7. Creates one header file with the model in the output_path that can be included to any AIfES project. A second header file is created, which contains the flattened weights of the Keras model. This function converts the given Keras model to Q7. It needs representative data to calculate the quantization parameters.

Parameters:
  • keras_model – Keras model, which should be converted

  • output_path – File path, where the converted model should be stored. If the folder doesn’t exist, it will be created by this function.

  • representative_data – Representative data of the input data of the given Keras model. The data is needed to calculate the quantization parameters for the hidden layers.

  • target_alignment – Alignment of the created flatbuffer depending on target architecture (1, 2, or 4 Bytes). E.g., for ARM Cortex M4 it is 4, which corresponds to 4 Bytes as it has a 32 Bit storage, for AVR Arduino it is 2, as the memory is organized as 16 Bit (2 Bytes)

  • byteorder – Byte order of target system, i.e., ‘little’ for little endian or ‘big’ for big endian.

aifes.pytorch2aifes.convert_to_fnn_q7_express(pytorch_model: Module, output_path: str, representative_data: ndarray, target_alignment: int, byteorder: str)#

Converts the given PyTorch model to AIfES Express Q7. Creates one header file with the model in the output_path that can be included to any AIfES project. A second header file is created, which contains the flattened weights of the PyTorch model. This function converts the given PyTorch model to Q7. It needs representative data to calculate the quantization parameters.

Parameters:
  • pytorch_model – PyTorch model, which should be converted

  • output_path – File path, where the converted model should be stored. If the folder doesn’t exist, it will be created by this function.

  • representative_data – Representative data of the input data of the given PyTorch model. The data is needed to calculate the quantization parameters for the hidden layers.

  • target_alignment – Alignment of the created flatbuffer depending on target architecture (1, 2, or 4 Bytes). E.g., for ARM Cortex M4 it is 4, which corresponds to 4 Bytes as it has a 32 Bit storage, for AVR Arduino it is 2, as the memory is organized as 16 Bit (2 Bytes)

  • byteorder – Byte order of target system, i.e., ‘little’ for little endian or ‘big’ for big endian.

AIfES#

AIfES offers an interface that is similar to other frameworks. With the following converter functions the FFNNs can be converted to the normal interface. It allows more control over the FFNNs by enabling optimized layer implementations (e.g., CMSIS implementations for ARM Cortex M controller) and finer control of layers. The following type specific converter functions exist:

Float 32#

Default Implementation#

For conversion to the default implementation of the layers use the following function:

aifes.keras2aifes.convert_to_fnn_f32(keras_model: Model, output_path: str, with_weights=True)#

Converts the given Keras model to AIfES F32 (non-express version). Creates one header file with the model in the output_path that can be included to any AIfES project. If weights are extracted as well, a second header file is created, which contains the flattened weights of the Keras model.

Parameters:
  • keras_model – Keras model, which should be converted

  • output_path – File path, where the converted model should be stored. If the folder doesn’t exist, it will be created by this function.

  • with_weights – Extracts the weights and bias from the given model and creates a separate header file with flattened weights.

aifes.pytorch2aifes.convert_to_fnn_f32(pytorch_model: Module, output_path: str, with_weights=True)#

Converts the given PyTorch model to AIfES F32 (non-express version). Creates one header file with the model in the output_path that can be included to any AIfES project. If weights are extracted as well, a second header file is created, which contains the flattened weights of the PyTorch model.

Parameters:
  • pytorch_model – PyTorch model, which should be converted

  • output_path – File path, where the converted model should be stored. If the folder doesn’t exist, it will be created by this function.

  • with_weights – Extracts the weights and bias from the given model and creates a separate header file with flattened weights.

CMSIS Implementation#

For the optimized implementation of the layers using CMSIS use the following function:

aifes.keras2aifes.convert_to_fnn_f32_cmsis(keras_model: Model, output_path: str, with_weights=True)#

Converts the given Keras model to AIfES F32 CMSIS implementation (non-express version). Creates one header file with the model in the output_path that can be included to any AIfES project. If weights are extracted as well, a second header file is created, which contains the flattened weights of the Keras model.

Parameters:
  • keras_model – Keras model, which should be converted

  • output_path – File path, where the converted model should be stored. If the folder doesn’t exist, it will be created by this function.

  • with_weights – Extracts the weights and bias from the given model and creates separate header file wih flatten weights.

aifes.pytorch2aifes.convert_to_fnn_f32_cmsis(pytorch_model: Module, output_path: str, with_weights=True)#

Converts the given PyTorch model to AIfES F32 CMSIS implementation (non-express version). Creates one header file with the model in the output_path that can be included to any AIfES project. If weights are extracted as well, a second header file is created, which contains the flattened weights of the PyTorch model.

Parameters:
  • pytorch_model – PyTorch model, which should be converted

  • output_path – File path, where the converted model should be stored. If the folder doesn’t exist, it will be created by this function.

  • with_weights – Extracts the weights and bias from the given model and creates separate header file wih flatten weights.

Quantized Q7#

Default Implementation#

For conversion to the default implementation of the layers and automatic quantization of your Model use the following function:

aifes.keras2aifes.convert_to_fnn_q7(keras_model: Model, output_path: str, representative_data: ndarray, target_alignment: int, byteorder: str, transpose=True)#

Converts the given Keras model to AIfES Q7 implementation (non-express version). Creates one header file with the model in the output_path that can be included to any AIfES project. A second header file is created, which contains the flattened weights of the Keras model. This function converts the given Keras model to Q7. It needs representative data to calculate the quantization parameters.

Parameters:
  • keras_model – Keras model, which should be converted

  • output_path – File path, where the converted model should be stored. If the folder doesn’t exist, it will be created by this function.

  • representative_data – Representative data of the input data of the given Keras model. The data is needed to calculate the quantization parameters for the hidden layers.

  • target_alignment – Alignment of the created flatbuffer depending on target architecture (1, 2, or 4 Bytes). E.g., for ARM Cortex M4 it is 4, which corresponds to 4 Bytes as it has a 32 Bit storage, for AVR Arduino it is 2, as the memory is organized as 16 Bit (2 Bytes)

  • byteorder – Byte order of target system, i.e., ‘little’ for little endian or ‘big’ for big endian.

  • transpose – When transpose=True the weights of the layers are transposed, so that the weights for each neuron are next to each other in memory. This can improve the performance of the ANN. Default is therefore ‘True’.

aifes.pytorch2aifes.convert_to_fnn_q7(pytorch_model: Module, output_path: str, representative_data: ndarray, target_alignment: int, byteorder: str, transpose=True)#

Converts the given PyTorch model to AIfES Q7 implementation (non-express version). Creates one header file with the model in the output_path that can be included to any AIfES project. A second header file is created, which contains the flattened weights of the PyTorch model. This function converts the given PyTorch model to Q7. It needs representative data to calculate the quantization parameters.

Parameters:
  • pytorch_model – PyTorch model, which should be converted

  • output_path – File path, where the converted model should be stored. If the folder doesn’t exist, it will be created by this function.

  • representative_data – Representative data of the input data of the given PyTorch model. The data is needed to calculate the quantization parameters for the hidden layers.

  • target_alignment – Alignment of the created flatbuffer depending on target architecture (1, 2, or 4 Bytes). E.g., for ARM Cortex M4 it is 4, which corresponds to 4 Bytes as it has a 32 Bit storage, for AVR Arduino it is 2, as the memory is organized as 16 Bit (2 Bytes)

  • byteorder – Byte order of target system, i.e., ‘little’ for little endian or ‘big’ for big endian.

  • transpose – When transpose=True the weights of the layers are transposed, so that the weights for each neuron are next to each other in memory. This can improve the performance of the ANN. Default is therefore ‘True’.

CMSIS Implementation#

For the optimized implementation of the layers using CMSIS and automatic quantization of your Model use the following function:

aifes.keras2aifes.convert_to_fnn_q7_cmsis(keras_model: Model, output_path: str, representative_data: ndarray, target_alignment: int, byteorder: str)#

Converts the given Keras model to AIfES Q7 implementation with CMSIS support (non-express version). Creates one header file with the model in the output_path that can be included to any AIfES project. A second header file is created, which contains the flattened weights of the Keras model. This function converts the given Keras model to Q7. It needs representative data to calculate the quantization parameters.

Parameters:
  • keras_model – Keras model, which should be converted

  • output_path – File path, where the converted model should be stored. If the folder doesn’t exist, it will be created by this function.

  • representative_data – Representative data of the input data of the given Keras model. Is needed to calculate the quantization parameters for the hidden layers.

  • target_alignment – Alignment of the created flatbuffer depending on target architecture (1, 2, or 4 Bytes). E.g., for ARM Cortex M4 it is 4, which corresponds to 4 Bytes as it has a 32 Bit storage, for AVR Arduino it is 2, as the memory is organized as 16 Bit (2 Bytes)

  • byteorder – Byte order of target system, i.e., ‘little’ for little endian or ‘big’ for big endian.

aifes.pytorch2aifes.convert_to_fnn_q7_cmsis(pytorch_model: Module, output_path: str, representative_data: ndarray, target_alignment: int, byteorder: str)#

Converts the given PyTorch model to AIfES Q7 implementation with CMSIS support (non-express version). Creates one header file with the model in the output_path that can be included to any AIfES project. A second header file is created, which contains the flattened weights of the PyTorch model. This function converts the given PyTorch model to Q7. It needs representative data to calculate the quantization parameters.

Parameters:
  • pytorch_model – PyTorch model, which should be converted

  • output_path – File path, where the converted model should be stored. If the folder doesn’t exist, it will be created by this function.

  • representative_data – Representative data of the input data of the given PyTorch model. Is needed to calculate the quantization parameters for the hidden layers.

  • target_alignment – Alignment of the created flatbuffer depending on target architecture (1, 2, or 4 Bytes). E.g., for ARM Cortex M4 it is 4, which corresponds to 4 Bytes as it has a 32 Bit storage, for AVR Arduino it is 2, as the memory is organized as 16 Bit (2 Bytes)

  • byteorder – Byte order of target system, i.e., ‘little’ for little endian or ‘big’ for big endian.