This document explains how to train a model and run inference using a microcontroller.
The Hello World example
The Hello World example is designed to demonstrate the absolute basics of using LiteRT for Microcontrollers. We train and run a model that replicates a sine function, i.e., it takes a single number as its input, and outputs the number's sine value. When deployed to the microcontroller, its predictions are used to either blink LEDs or control an animation.
The end-to-end workflow involves the following steps:
- Train a model (in Python): A python file to train, convert and optimize a model for on-device use.
- Run inference (in C++ 17): An end-to-end unit test that runs inference on the model using the C++ library.
Get a supported device
The example application we'll be using has been tested on the following devices:
- Arduino Nano 33 BLE Sense (using Arduino IDE)
- SparkFun Edge (building directly from source)
- STM32F746 Discovery kit (using Mbed)
- Adafruit EdgeBadge (using Arduino IDE)
- Adafruit LiteRT for Microcontrollers Kit (using Arduino IDE)
- Adafruit Circuit Playground Bluefruit (using Arduino IDE)
- Espressif ESP32-DevKitC (using ESP IDF)
- Espressif ESP-EYE (using ESP IDF)
Learn more about supported platforms in LiteRT for Microcontrollers.
Train a model
Use train.py for hello world model training for sinwave recognition
Run: bazel build tensorflow/lite/micro/examples/hello_world:train
bazel-bin/tensorflow/lite/micro/examples/hello_world/train --save_tf_model
--save_dir=/tmp/model_created/
Run inference
To run the model on your device, we will walk through the instructions in the
README.md:
The following sections walk through the example's
evaluate_test.cc,
unit test which demonstrates how to run inference using LiteRT for
Microcontrollers. It loads the model and runs inference several times.
1. Include the library headers
To use the LiteRT for Microcontrollers library, we must include the following header files:
#include "tensorflow/lite/micro/micro_mutable_op_resolver.h"
#include "tensorflow/lite/micro/micro_error_reporter.h"
#include "tensorflow/lite/micro/micro_interpreter.h"
#include "tensorflow/lite/schema/schema_generated.h"
#include "tensorflow/lite/version.h"
micro_mutable_op_resolver.hprovides the operations used by the interpreter to run the model.micro_error_reporter.houtputs debug information.micro_interpreter.hcontains code to load and run models.schema_generated.hcontains the schema for the LiteRTFlatBuffermodel file format.version.hprovides versioning information for the LiteRT schema.
2. Include the model header
The LiteRT for Microcontrollers interpreter expects the model to be
provided as a C++ array. The model is defined in model.h and model.cc files.
The header is included with the following line:
#include "tensorflow/lite/micro/examples/hello_world/model.h"
3. Include the unit test framework header
In order to create a unit test, we include the LiteRT for Microcontrollers unit test framework by including the following line:
#include "tensorflow/lite/micro/testing/micro_test.h"
The test is defined using the following macros:
TF_LITE_MICRO_TESTS_BEGIN
TF_LITE_MICRO_TEST(LoadModelAndPerformInference) {
. // add code here
.
}
TF_LITE_MICRO_TESTS_END
We now discuss the code included in the macro above.
4. Set up logging
To set up logging, a tflite::ErrorReporter pointer is created using a pointer
to a