1.. _tensorflow_hello_world:
2
3TensorFlow Lite Micro Hello World sample
4########################################
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
37The reference kernel application can be built and executed on QEMU as follows:
38
39.. zephyr-app-commands::
40   :zephyr-app: samples/modules/tflite-micro/hello_world
41   :host-os: unix
42   :board: qemu_x86
43   :goals: run
44   :compact:
45
46Exit QEMU by pressing :kbd:`CTRL+A` :kbd:`x`.
47
48The CMSIS-NN kernel application can be built and executed on any Arm(R) Cortex(R)-M core based platform,
49for example based on Arm Corstone(TM)-300 software. A reference implementation of Corstone-300
50can be downloaded either as a FPGA bitfile for the
51[MPS3 FPGA prototyping board](https://developer.arm.com/tools-and-software/development-boards/fpga-prototyping-boards/mps3),
52or as a
53[Fixed Virtual Platform](https://developer.arm.com/tools-and-software/open-source-software/arm-platforms-software/arm-ecosystem-fvps)
54that can be emulated on a host machine.
55
56Assuming that the Corstone-300 FVP has been downloaded, installed and added to
57the `PATH` variable, then building and testing can be done with following
58commands.
59
60```
61$ west build -p auto -b mps3_an547 samples/modules/tflite-micro/hello_world/ -T sample.tensorflow.helloworld.cmsis_nn
62$ FVP_Corstone_SSE-300_Ethos-U55 build/zephyr/zephyr.elf
63```
64
65Sample Output
66=============
67
68.. code-block:: console
69
70    ...
71
72    x_value: 1.0995567*2^1, y_value: 1.6951603*2^-1
73
74    x_value: 1.2566366*2^1, y_value: 1.1527088*2^-1
75
76    x_value: 1.4137159*2^1, y_value: 1.1527088*2^-2
77
78    x_value: 1.5707957*2^1, y_value: -1.0849024*2^-6
79
80    x_value: 1.7278753*2^1, y_value: -1.0509993*2^-2
81
82    ...
83
84The modified sample prints 10 generated-x-and-predicted-y pairs. To see
85the full period of the sine curve, increase the number of loops in :file:`main.c`.
86
87Modifying Sample for Your Own Project
88*************************************
89
90It is recommended that you copy and modify one of the two TensorFlow
91samples when creating your own TensorFlow project. To build with
92TensorFlow, you must enable the below Kconfig options in your :file:`prj.conf`:
93
94.. code-block:: kconfig
95
96    CONFIG_CPP=y
97    CONFIG_NEWLIB_LIBC=y
98    CONFIG_TENSORFLOW_LITE_MICRO=y
99
100Note that the CMSIS-NN kernel sample demonstrates how to use CMSIS-NN optimized kernels with
101TensorFlow Lite Micro, in that is sets below Kconfig option. Note also that this
102Kconfig option is only set for Arm Cortex-M cores, i.e. option CPU_CORTEX_M is set.
103
104.. code-block:: kconfig
105
106    CONFIG_TENSORFLOW_LITE_MICRO_CMSIS_NN_KERNELS=y
107
108Training
109********
110Follow the instructions in the :file:`train/` directory to train your
111own model for use in the sample.
112