Flatbuffer Converter Tool#

The Flatbuffer Converter Tool helps to convert a .tflite file into a C array that can be compiled into a binary for an embedded system. This array can be used with the TensorFlow Lite for Microcontrollers API, which takes a void pointer to a buffer containing the model as an argument to its tflite::GetModel() init function.

In addition to converting the flatbuffer into a C array, the tool supports emitting model parameters embedded as metadata in the .tflite file as C preprocessor macros.

Input#

The tool takes a directory containing one or more .tflite files as input. If the directory consists of multiple files, only the first file in alphabetical order is converted.

Tip: If you have multiple files in the directory, but the one you want to convert isn't the first file in alphabetical order, you can rename the other files to add a .bak extension or rename the target file accordingly.

Output#

The tool writes its output into multiple files in a single output directory.

Model Array#

The tool always emits a pair of files sl_tflite_micro_model.c/.h, which declares the variables as follows.

  • const uint8_t sl_tflite_model_array[] containing the full contents of the .tflite file

  • const uint32_t sl_tflite_model_len containing the length of the model array

Opcode Resolver#

A header file sl_tflite_micro_opcode_resolver.h is also emitted. This file declares a C preprocessor macro SL_TFLITE_MICRO_OPCODE_RESOLVER(opcode_resolver, error_reporter) that instantiates a tflite::MicroMutableOpResolver object and automatically registers the set of operators required to parse the model.

This macro can be used as part of an initialization sequence to automatically initializing the optimal opcode resolver.

Model Parameters#

If the .tflite file contains model parameters in its metadata section, a third header file sl_tflite_micro_model_parameters.h is emitted.

For every model parameter key-value pair, a C preprocessor macro SL_TFLITE_MODEL_<key> is created with the relevant value.

See the MLTK documentation to learn more about embedding model parameters in the .tflite file.

SLC Project Configuration Integration#

The Flatbuffer Converter Tool integrates with the SLC project configuration tools in Simplicity Studio and on the command line using SLC-CLI. When using these tools, the Flatbuffer Converter is automatically run with the config/tflite/ directory of the project as input, and the autogen/ directory as output.

In other words, any file with the .tflite extension in the config/tflite/ directory in the project will be automatically converted when the project is generated. If using Simplicity Studio, a directory watcher ensures that the project is automatically generated if a .tflite file is added or removed. This means that it is sufficient to drag-and-drop a .tflite file into the project, and it is automatically converted into C code.

Manual Usage#

If conversion is desired outside of a full SLC project generation cycle, the flatbuffer converter can be invoked manually. This is generally not necessary, but is an option for advanced users. This can only be done using the SLC-CLI, users of Simplicity Studio are recommended to regenerate the full project.

As Standalone Conversion Command#

The Flatbuffer Converter Tool runs as a standalone Python script outside of a project generation flow. Execute the following command using absolute paths for both input and output directories.

  • NumPy must be installed. If not install it using:

pip install numpy
  • The output directory must already exist before running the tool. Create one in any location you prefer:

mkdir -p /path/to/output/folder
  • Identify your TFLite model folder where .tflite model files are stored. You may also use your own custom .tflite model files by supplying their absolute path to -i.

  • Run slt where aiml to locate the AIML package installation path, and then go to the tool/tflite directory where tflite.py is located.

  • Navigate to the folder containing flatbuffer_converter.py:

cd "$(slt where aiml)/tool/flatbuffer-converter/flatbuffer_converter.py"
  • Run the Flatbuffer Converter Tool using:

python flatbuffer_converter.py generate </path/to/tflite/model/folder> </path/to/output/folder> <part_number>

Tool Help#

You can run the help command at any time to view the latest usage instructions for this tool:

python flatbuffer_converter.py generate --help
usage: flatbuffer_converter.py generate [-h] input_dir output_dir part_number

Runs when project is generated using SLC-CLI or Studio.

positional arguments:
  input_dir    Input directory containing .tflite files
  output_dir   Output directory to populate with serialized content.
  part_number  Part Number

options:
  -h, --help   show this help message and exit