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.
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.
The tool writes its output into multiple files in a single output directory.
The tool always emits a pair of files
.h, which declares the variables as follows
const uint8_t sl_tflite_model_arraycontaining the full contents of the
const uint32_t sl_tflite_model_lencontaining the length of the model array
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.
.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
SLC Project Configuration Integration
The Flatbuffer Converter Tool integrates with the SLC project configuration tools in Simplicty 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.
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 SLC Project Generator
SLC-CLI supports running a single project generation tool by passing the
-tools to the
slc generate command.
The Flatbuffer Converter step of the project generation process can be run standalone by running the following command:
slc generate -p <my_project.slcp> -tools tflite
As Standalone SLC-CLI Command
The flatbuffer converter is also available as a standalone command in SLC-CLI for use outside of the context of a project. Execute the following command using absolute paths for both input and output directories.
slc tflite generate -contentFolder [/path/to/config/tflite] -generationOutput [/path/to/autogen]