tensorflow / tflite-micro

Infrastructure to enable deployment of ML models to low-power resource-constrained embedded targets (including microcontrollers and digital signal processors).
Apache License 2.0
1.91k stars 823 forks source link

Linking the tflite micro library in PX4 #2748

Closed SindreMHegre closed 1 week ago

SindreMHegre commented 1 week ago

I'm trying to build tflite-micro as a submodule in PX4, what is the best way to do this? After two days of trying I'm still stuck at linker errors. Is there an easy way to utilize the Makefile with PX4's CMakeLists.txt?

https://github.com/SindreMHegre/PX4-Autopilot/tree/pr_tflite_micro_into_PX4/src/lib/tflite

SindreMHegre commented 1 week ago

I think I was able to link it properly. But when I run my module I get this error:

Didn't find op for builtin opcode 'ADD' Failed to get registration from op code ADD

ERROR [mc_nn_control] AllocateTensors() failed

And here is the code until that point: `namespace { using NNControlOpResolver = tflite::MicroMutableOpResolver<2>;

TfLiteStatus RegisterOps(NNControlOpResolver& op_resolver) { TF_LITE_ENSURE_STATUS(op_resolver.AddFullyConnected()); return kTfLiteOk; } } // namespace

extern "C" __EXPORT int mc_nn_control_main(int argc, char *argv[]) { PX4_INFO("Hello from mc_nn_control_main");

// Load the model
const tflite::Model* model = ::tflite::GetModel(simple_net_tflite);  // Replace with your model data variable
if (model->version() != TFLITE_SCHEMA_VERSION) {
    PX4_ERR("Model provided is schema version %d not equal to supported version %d.",
        model->version(), TFLITE_SCHEMA_VERSION);
    return -1;
}

// Set up the interpreter
static NNControlOpResolver resolver;
if (RegisterOps(resolver) != kTfLiteOk) {
    PX4_ERR("Failed to register ops");
    return -1;
}
constexpr int kTensorArenaSize = 10 * 1024;
static uint8_t tensor_arena[kTensorArenaSize];
tflite::MicroInterpreter interpreter(model, resolver, tensor_arena, kTensorArenaSize);

// Allocate memory for the model's tensors
TfLiteStatus allocate_status = interpreter.AllocateTensors();
if (allocate_status != kTfLiteOk) {
    PX4_ERR("AllocateTensors() failed");
    return -1;`
ddavis-2015 commented 1 week ago

@SindreMHegre

You will need to register all operators your model uses:

TF_LITE_ENSURE_STATUS(op_resolver.AddFullyConnected());
TF_LITE_ENSURE_STATUS(op_resolver.AddAdd());

This issue will now be closed. Please feel free to reopen the issue should you have additional comments.