Enabling tflite-micro in the nrf connect sdk (work for anyboard - tflite-micro/hello_world) - The correct steps that will work so you dont have to spend 500 hours trying to piece together a solution from 10000 different discussions on different forums.

There are 100s of solutions on the zephyr git and the nordic git. It took me a while to understand it all and it took me hours of debugging and frustration before I ended up guessing the right solution. 

In your prj.conf: Documentation says to use the below flags, but to really activate them we need to change the west.yml. 
CONFIG_TENSORFLOW_LITE_MICRO=y
CONFIG_TENSORFLOW_LITE_MICRO_CMSIS_NN_KERNELS=y
Without changing the west.yml file, vscode yellow underlines these flags and raises an error: "CONFIG_TENSORFLOW_LITE_MICRO" or "CONFIG_TENSORFLOW_LITE_MICRO_CMSIS_NN_KERNELS" is set to y but got n. 
When you build the project regardless of the warning you will get the below two errors resulting in build failure:
: fatal error: tensorflow/lite/micro/micro_mutable_op_resolver.h: No such file or directory
21 | #include <tensorflow/lite/micro/micro_mutable_op_resolver.h>
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
compilation terminated.
: fatal error: tensorflow/lite/c/common.h: No such file or directory
20 | #include <tensorflow/lite/c/common.h>
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
compilation terminated.
Solution:
/opt/nordic/ncs/vX.X.X/nrf/west.yml:
Under projects/-name: zephyr/import: name-allowlist 
Add the flag "- tflite-micro"
Run the below commands:
```
cd /opt/nordic/ncs/vX.X.X/
west config manifest.project-filter -- +tflite-micro
west config manifest.group-filter -- +optional
west update
```
When you do the update, tflite micro will be installed and then you will be able to use the CONFIG_TENSORFLOW_... flags and the import errors will be gone. 


  • Hi Darsh,

    You're correct in that you need to allow tflite-micro explicitly in `/ncs/vx.x.x/nrf/west.yml`. This has to do with tflite being a module, and NCS does not by default allow for it to be imported.

    The comment above the name-allowlist states (in west.yml)

    In addition to the zephyr repository itself, NCS also imports the contents of zephyr/west.yml at the above revision. Only the projects explicitly named in the following allowlist are imported.

    The documentation you linked also hints towards this "Add the tflite-micro module to your West manifest and pull it:".

    The reason for it not being there is that there is no official support for the `tflite-micro` in NCS, but because it's based on Zephyr it should most likely work with minimal effort.

    That being said, I do agree with you that it's not at all obvious that you need to add tflite-micro to the allow list, and it's also not obvious that it's not 100% supported. I'll forward this to the relevant team.

  • Thank you for your answer! I had created this answer for the people who might have a similar issue in the future and end up coming to the forum. Thank you for forwarding this to the relevant team! The community really appreciates this! 

    :) 

Related