# Introduction
Source: TinyML (Machine Learning with TensorFlow Lite on Arduino and Ultra-Low-Power Microcontrollers) by Pete Warden & Daniel Situnayake
# TinyML Hello World Goal
We want to calculate the sine of x and use its output to control the timing of a flashing LED on our microcontroller. Instead of using mathematical functions to calculate the sine of x we can train a model that takes x (going from 0 to 2π) and outputs the corresponding sine value (which lays between -1 and 1).
# Prerequisites
We are going to use the command line to start a Jupyter-Notebook. Open a terminal by pressing the terminal symbol in the taskbar at the top of your window. Note: Don't use the mbed-cli terminal icon on the desktop as this will not run the notebook with the correct requirements.
Open a Jupyter-Notebook instance using the command line and typing:
jupyter-notebook --no-browser
If this doesn't work, try a reboot or typing source ~/.profile
and try again.
The console should give you a URL with a token attached to it where the notebook is located. Open this URL with Chromium by pressing ctrl
and left-clicking the link. Open the file train_hello_world_model.ipynb
which should be located in the folder /home/pi/workshop-ai-edge/hello-world-jupyter-notebook
. If this is not the case, download train_hello_world_model.ipynb
(opens new window) from the Tensorflow Lite Micro examples, save or copy/paste the file to the directory where Jupyter-notebook is running. The file should now be visible in the Jupyter notebook.
Loading the notebook
It can take some time for Chromium to load the notebook, please be patient and press Wait when the browser prompts a warning.
Installing dependencies
In the notebook section Setup Environment the tensorflow dependency tensorflow=2.4.0
is installed. This step might take a long time to complete. If you are following the workshop on a prepared system, this dependency is already installed for you, and you can skip this step by commenting it out by prepending a #
character at the beginning of the line.
# ! pip install tensorflow==2.4.0
The section should look like this:
# Steps
An overview is given of the steps that are taken to create the dataset, train the model and export the model parameters. A more in-depth explanation of the code and the functions is embedded in the Jupyter Notebook.
One can run the whole notebook by using the symbol, or by going through it step by step by pressing ctrl+Enter
.
- Obtain a simple dataset
- Generate data
- Add noise
- Split the data
- Train a small model
- Design the neural network
- Train the neural network
- Plot and observe the metrics
- Train a larger model
- Design the neural network
- Train the neural network
- Plot and observe the metrics
- Generate a Tensorflow Lite model
- Generate model without quantisation
- Generate model with quantisation
- Compare the model performance
- Generate a Tensorflow Lite for Microcontrollers model
- Deploy to a Microcontroller
The training dataset can be adjusted to your liking, but keep in mind that the program running on the microcontroller will have to be adjusted accordingly!
# Next steps
Running the notebook will have created a new folder named models
. In that folder you can find model.cc
which is a C++ file that contains an array with the quantised weights of the model you created and trained. In the next steps we are going to use that file to infer the model on the Sensortile microcontroller.