← Back to the chapter · Previous detail · Next detail →

Technical detail · TinyML on Microcontrollers

TensorFlow Lite Micro integration skeleton

11.6.1 TensorFlow Lite Micro Skeleton

This simplified structure shows the runtime pieces to review. Production code should add return-code checks, watchdog behavior, version reporting, and target-specific memory profiling.

#include "model_data.h"
#include "tensorflow/lite/micro/micro_interpreter.h"
#include "tensorflow/lite/micro/micro_mutable_op_resolver.h"
#include "tensorflow/lite/schema/schema_generated.h"

constexpr int kTensorArenaSize = 70 * 1024;
alignas(16) static uint8_t tensor_arena[kTensorArenaSize];

tflite::MicroMutableOpResolver<4> resolver;

void setup_inference() {
  resolver.AddConv2D();
  resolver.AddDepthwiseConv2D();
  resolver.AddFullyConnected();
  resolver.AddSoftmax();

  const tflite::Model* model = tflite::GetModel(g_model_data);
  static tflite::MicroInterpreter interpreter(
    model, resolver, tensor_arena, kTensorArenaSize);

  TfLiteStatus status = interpreter.AllocateTensors();
  if (status != kTfLiteOk) {
    report_fatal_error("tensor allocation failed");
  }
}

The tensor arena value is not a universal constant. Measure it for the exact converted model, operator resolver, compiler flags, and firmware build.

← Back to the chapter · Previous detail · Next detail →