This guide explains how to download and set up ZephyrOS with the latest TFLM (TensorFlow Lite for Microcontrollers) on your system. Follow the steps below:
-
Download Zephyr by following the instructions provided here -- I am working with Zephyr Branch 3.6
-
Once downloaded, run the Blinky example to ensure everything is working correctly.
- Install
gmake
using Homebrew:brew install gmake
- Clone the TFLM repository:
git clone https://www.github.com/tensorflow/tflite-micro.git
(you can also delete the already exsisting tflite-micro in zephyr and clone latest tflite-micro in that same zephyr location -- make sure you cmakelists.txt has the correct path for tflite-micro. However, I didnt do that here, I have a seperate folder for tflite-micro) currently I am atcommit 9245002844166fc3d0c41917b9b6e1678e8dfad1
- Navigate to the TFLM directory:
cd tflite-micro
- Build TFLM using the provided Makefile:
gmake -f tensorflow/lite/micro/tools/make/Makefile
If you want to build TFLM for the Cortex-M4 processor:
-
Build TFLM with the specified target and target architecture:
gmake -f tensorflow/lite/micro/tools/make/Makefile TARGET=cortex_m_generic TARGET_ARCH=cortex-m4 third_party_downloads
-
gmake -f tensorflow/lite/micro/tools/make/Makefile TARGET=cortex_m_generic TARGET_ARCH=cortex-m4 OPTIMIZED_KERNEL_DIR=cmsis_nn microlite
replace m4 by m33 if you want to build for nrf53
-
Copy the
hello_world
folder fromhello_world_1.1
and place it in the following directory:zephyrproject/zephyr/samples/basic
. -
Build the project using the following commands:
cd ~/zephyrproject
west build -p always -b nrf5340dk_nrf5340_cpuapp zephyr/samples/basic/hello_world/
-
Make sure to edit the path to your latest TFLM in the
CMakeLists.txt
file and check thetarget_arch
(for nRF5340 or nRF52, it should becortex-m33
orcortex-m4
). -
Deploy the built project using the following command:
west flash
Once the above steps are completed successfully, follow these additional instructions to replace the TFLM image with the latest version:
-
Copy the latest TFLM image and replace the existing one in the following directory:
zephyrproject/modules/lib/tflite-micro
. -
Copy the
hello_world_1.2
file and place it in the following directory:zephyrproject/zephyr/samples/basic
. -
Build the updated project using the following command:
west build -p always -b nrf5340dk_nrf5340_cpuapp zephyr/samples/basic/hello_world_1.2/
-
You may see an error for fixedpoint.h:
- copy fixedpoint folder and detect_platform.h from tflite-micro/tensorflow/lite/micro/tools/make/downloads/gemmlowp/internal and copy it to tflite-micro/tensorflow/lite/kernels/internal
- now open the fixedpoint/fixedpoint.h and change the path of detect_platform.h from ../internal/detect_platform.h to ../detect_platform.h
- You may see an error for DWT not defined:
- something is wrong witht he gmake, clean the tflite-micro build and repeat the following commands:
gmake -f tensorflow/lite/micro/tools/make/Makefile
gmake -f tensorflow/lite/micro/tools/make/Makefile TARGET=cortex_m_generic TARGET_ARCH=cortex-m4 third_party_downloads
gmake -f tensorflow/lite/micro/tools/make/Makefile TARGET=cortex_m_generic TARGET_ARCH=cortex-m4 OPTIMIZED_KERNEL_DIR=cmsis_nn microlite
-
Make sure to edit the path to your latest TFLM in the
CMakeLists.txt
file and check thetarget_arch
(for nRF5340 or nRF52, it should becortex-m4
). -
Deploy the updated project using the following command:
west flash
To view the output, you can use the picocom
tool:
- Install
picocom
using Homebrew:brew install picocom
- Run
picocom
with the appropriate settings:picocom -fh -b 115200 --imap lfcrlf /dev/tty…
If you want to build TFLM for the Cortex-M7 processor:
-
Navigate to the TFLM directory:
cd ~
cd tflite-micro
-
Clean the previous build (if necessary):
gmake -f tensorflow/lite/micro/tools/make/Makefile clean
-
Build TFLM with the specified target, target architecture, and CMSIS-NN enabled:
gmake -f tensorflow/lite/micro/tools/make/Makefile TARGET=cortex_m_generic OPTIMIZED_KERNEL_DIR=cmsis_nn TARGET_ARCH=cortex-m7 microlite
Note: If using the latest TFLM and building without CMSIS-NN, make sure to comment out the line OPTIMIZED_KERNEL_DIR=cmsis_nn
in CMakeLists.txt