Failed to allocate TFLite arena (error code 1) on Portenta

The FOMO model runs perfectly fine on the Portenta, but when I try to light up the LED_BUILTIN, it gives the Failed to allocate TFLite arena error.

The example code gives the following solution:

 ** NOTE: If you run into TFLite arena allocation issue.
 **
 ** This may be due to may dynamic memory fragmentation.
 ** Try defining "-DEI_CLASSIFIER_ALLOCATION_STATIC" in boards.local.txt (create
 ** if it doesn't exist) and copy this file to
 ** `<ARDUINO_CORE_INSTALL_PATH>/arduino/hardware/<mbed_core>/<core_version>/`.
 **
 ** See
 ** (https://support.arduino.cc/hc/en-us/articles/360012076960-Where-are-the-installed-cores-located-)
 ** to find where Arduino installs cores on your machine.
 **

I was successful in finding where it is located (even though the link does not work), but I am not sure how to edit the file, because the file expects a key=value format.

Hello @ElectricDragon,

The Failed to allocate TFLite arena is a memory issue, you can try to use a smaller EI model (reduce your window size or your image size is usually a good start).

For the option Try defining "-DEI_CLASSIFIER_ALLOCATION_STATIC" in boards.local.txt. I’ll check with our Embedded team if they have a deeper explanation. I’ll add it to our documentation too.

Regards,

Louis

Okay thanks a lot! Let me know where to exactly add the flag in the boards.local.txt file

@ElectricDragon

Hi the URL looks to no longer be working. I’ve find the newer URL. You’d want to navigate to Board platforms and core and find the right path depending on your OS.

E.g. on Linux for the Portenta H7 using mbed core 2.8.0 and for the M7 you’d want to create boards.local.txt with the following path ~/.arduino15/packages/arduino/hardware/mbed_portenta/2.8.0/boards.local.txt.

boards.local.txt:

envie_m7.build.extra_flags= -DEI_CLASSIFIER_ALLOCATION_STATIC

(Optional) For an example on how you can further expand boards.local.txt take a look at our example for our firmware repo. for our firmware repo.

@ElectricDragon

The behavior you describe may be a memory fragmentation issue. Allocating the tensor arena may or may not solve the issue. It all depends on whether after allocating the tensor arena statically they’ll have enough memory for the rest of your application. Can you share the serial output when it fails and also the (led control) code snippet that causes the issue? And what’s the size of your model?

@rjames

This is the serial output


Starting inferencing in 2 seconds...
Taking photo...
Failed to allocate TFLite arena (error code 1)
ERR: Failed to run classifier (-6)

Following is the code for the led control:

if (bb.label == "bottle" && bb.value >= 0.90) {
   Serial.println("Bottle found!!!! with " + String(bb.value*100) + "% accuracy");
   digitalWrite(LED_BUILTIN, LOW); // further predictions failed after this line
}

I am not sure how to get the size of the model…can you tell me how to go about that?

@ElectricDragon,

you can get the size of the model from within the Deployment page of the Studio. A similar output:

.

@rjames,
Here is my model size

@ElectricDragon

Thank you. The model fits nicely in the Portenta. So I suspect your issue was due to memory fragmentation.
The combination of allocation/deallocation of the tensor arena (large area) with the alllocation/deallocation in your Serial.println("Bottle found!...") (small area) causses this. Did allocating the tensor arena statically solve your issue?

1 Like

@rjames

Yes it did…thanks a lot

1 Like