1.. zephyr:code-sample:: tflite-hello-world 2 :name: Hello World 3 4 Replicate a sine wave using TensorFlow Lite for Microcontrollers. 5 6Overview 7******** 8 9This sample TensorFlow application replicates a sine wave and 10demonstrates the absolute basics of using TensorFlow Lite Micro. 11 12The model included with the sample is trained to replicate a 13sine function and generates x values to print alongside the 14y values predicted by the model. The x values iterate from 0 to 15an approximation of 2π. 16 17The sample also includes a full end-to-end workflow of training 18a model and converting it for use with TensorFlow Lite Micro for 19running inference on a microcontroller. 20 21The sample comes in two flavors. One with TensorFlow Lite Micro 22reference kernels and one with CMSIS-NN optimized kernels. 23 24.. Note:: 25 This README and sample have been modified from 26 `the TensorFlow Hello World sample for Zephyr`_. 27 28.. _the TensorFlow Hello World sample for Zephyr: 29 https://github.com/tensorflow/tflite-micro/tree/main/tensorflow/lite/micro/examples/hello_world 30 31Building and Running 32******************** 33 34The sample should work on most boards since it does not rely 35on any sensors. 36 37Add the tflite-micro module to your West manifest and pull it: 38 39.. code-block:: console 40 41 west config manifest.project-filter -- +tflite-micro 42 west update 43 44The reference kernel application can be built and executed on QEMU as follows: 45 46.. zephyr-app-commands:: 47 :zephyr-app: samples/modules/tflite-micro/hello_world 48 :host-os: unix 49 :board: qemu_x86 50 :goals: run 51 :compact: 52 53Exit QEMU by pressing :kbd:`CTRL+A` :kbd:`x`. 54 55The CMSIS-NN kernel application can be built and executed on any Arm(R) Cortex(R)-M core based platform, 56for example based on Arm Corstone(TM)-300 software. A reference implementation of Corstone-300 57can be downloaded either as a FPGA bitfile for the 58[MPS3 FPGA prototyping board](https://developer.arm.com/tools-and-software/development-boards/fpga-prototyping-boards/mps3), 59or as a 60[Fixed Virtual Platform](https://developer.arm.com/tools-and-software/open-source-software/arm-platforms-software/arm-ecosystem-fvps) 61that can be emulated on a host machine. 62 63Assuming that the Corstone-300 FVP has been downloaded, installed and added to 64the :envvar:`PATH` variable, then building and testing can be done with following 65commands. 66 67``` 68$ west build -p auto -b mps3/corstone300/an547 samples/modules/tflite-micro/hello_world/ -T sample.tensorflow.helloworld.cmsis_nn 69$ FVP_Corstone_SSE-300_Ethos-U55 build/zephyr/zephyr.elf 70``` 71 72Sample Output 73============= 74 75.. code-block:: console 76 77 ... 78 79 x_value: 1.0995567*2^1, y_value: 1.6951603*2^-1 80 81 x_value: 1.2566366*2^1, y_value: 1.1527088*2^-1 82 83 x_value: 1.4137159*2^1, y_value: 1.1527088*2^-2 84 85 x_value: 1.5707957*2^1, y_value: -1.0849024*2^-6 86 87 x_value: 1.7278753*2^1, y_value: -1.0509993*2^-2 88 89 ... 90 91The modified sample prints 10 generated-x-and-predicted-y pairs. To see 92the full period of the sine curve, increase the number of loops in :file:`main.c`. 93 94Modifying Sample for Your Own Project 95************************************* 96 97It is recommended that you copy and modify one of the two TensorFlow 98samples when creating your own TensorFlow project. To build with 99TensorFlow, you must enable the below Kconfig options in your :file:`prj.conf`: 100 101.. code-block:: cfg 102 103 CONFIG_CPP=y 104 CONFIG_REQUIRES_FULL_LIBC=y 105 CONFIG_TENSORFLOW_LITE_MICRO=y 106 107Note that the CMSIS-NN kernel sample demonstrates how to use CMSIS-NN optimized kernels with 108TensorFlow Lite Micro, in that is sets below Kconfig option. Note also that this 109Kconfig option is only set for Arm Cortex-M cores, i.e. option CPU_CORTEX_M is set. 110 111.. code-block:: cfg 112 113 CONFIG_TENSORFLOW_LITE_MICRO_CMSIS_NN_KERNELS=y 114 115Training 116******** 117Follow the instructions in the :file:`train/` directory to train your 118own model for use in the sample. 119