espressif / esp-tflite-micro

TensorFlow Lite Micro for Espressif Chipsets
Apache License 2.0
394 stars 83 forks source link

Documentation/example when using large model that overflows flash memory (TFMIC-39) #96

Closed kirianguiller closed 2 months ago

kirianguiller commented 2 months ago

Checklist

Issue or Suggestion Description

Hello,

I'm trying to use an esp32-S3 to do sound classifiaction. My issue is that the tflite model is 4MB big and it overflows the flash memory of my microcontroller.

I have the .tflite model that I converted to .cc file using xdd.

I have checked all issues (the issuer of this issue achieve such behavior) and searched through the documentation (here the official espressif doc for external ram) to try to understand how I should store and load the model (storing it in spiffs ? And loading it in PSRAM ?), but I don't succeed to make the changes to one of the three esp-tflite-micro examples to make a working solution.

Does someone has such a working example that I could take inspiration from? Or is there somewhere a more expressive documentation that would help me?

Thanks in advance!

vikramdattu commented 2 months ago

Hi @kirianguiller the model data pointer can be passed to getModel function and the rest of the code stays as is. Do refer main_functions.cc file for this. https://github.com/espressif/esp-tflite-micro/blob/1355a9afab6c2c8cd2a22e4f253126b753b63f10/examples/hello_world/main/main_functions.cc#L43

kirianguiller commented 2 months ago

Thank you, I will try the options 2a and 2b and will report back my research

kirianguiller commented 2 months ago

I did as you said in 2b :

* If you have an option, you should explore:

  * Either increase the partition size, which will automatically put the static array model in flash as rodata. With this approach, you do not change the code.

Here is how I proceeded:

I changed the partitions.csv to the following (0x500000 is equal to 5MB):

# Name,   Type, SubType, Offset,  Size,      Flags
nvs,      data, nvs,     0x9000,  0x5000,
phy_init, data, phy,     0xe000,  0x1000,
factory,  app,  factory, 0x10000, 0x500000,

And I got sure that the model data was assigned as a constant. Otherwise the model will not go to PSRAM (when I converted the .tflite to .cc with xdd, the const was missing and thus was making the compilation fails.

Thanks! I can close the issue as everything works (other than the fact that the model I use itself, Yamnet, has some layers that are not supported by TFLM)