Small model, small problem
TinyML does not replace a large language or vision model. It excels at targeted tasks with few classes: recognising a few gestures, a keyword, an abnormal sound, a posture.
What is TinyML?
Understand what TinyML is: the difference between a written program and a trained model, training and inference, and the constraints of a microcontroller.
Prerequisites:
For a console to detect a joystick pushed to the right, you write a rule: if (x > 2500). But how do you write the rule that recognises a circle drawn with the joystick, a spoken word or a machine that starts to vibrate abnormally? The signals vary from one person to another, and from one time to the next: no series of if statements will do.
Machine learning turns the problem around: instead of writing the rule, you show the program labelled examples (“this is a circle”, “this is a back-and-forth”), and an algorithm works out by itself a model able to classify new examples.
TinyML (tiny machine learning) means running this model on a microcontroller: a few hundred kilobytes of memory, a few milliwatts, and no Internet connection.
| Written program | Trained model | |
|---|---|---|
| Who sets the rule? | The developer, line by line | An algorithm, from examples |
| What you have to provide | Code | Labelled data |
| When it is the right choice | The rule is simple and known | The rule is fuzzy or too complex to write |
| How you check it | You reread the code | You measure the rate of correct answers on examples never seen before |
The two combine: a model recognises the gesture, and ordinary code decides what to do with it (turn on an LED, send a message).
A model lives through two very different moments:
flowchart LR
A["1. Collect<br/>data"] --> B["2. Train<br/>the model"]
B --> C["3. Shrink<br/>the model"]
C --> D["4. Deploy<br/>on the board"]
D --> E["5. Infer<br/>continuously"]
subgraph PC["On the computer"]
B
C
end
subgraph MCU["On the microcontroller"]
A
D
E
end
A model must fit in the memory of the board and answer fast enough:
| Laptop | XIAO ESP32S3 | |
|---|---|---|
| RAM | 8 to 32 GB | 512 KB (and 8 MB of external PSRAM) |
| Program storage | hundreds of GB | 8 MB of Flash |
| Processor | several GHz, several cores | 240 MHz, 2 cores |
| Power consumption | tens of watts | a few hundred milliwatts when computing |
To fit, the model is quantised: its parameters, computed as 32-bit floating-point numbers (float), are converted into 8-bit integers (int8). The model becomes about four times smaller and faster, at the cost of a slight loss of accuracy, which is measured.
Sending every measurement to a server for it to classify is possible, but:
| Application | Sensor | What the model recognises |
|---|---|---|
| Voice assistant | Microphone | The word that wakes the device (“Ok…”) |
| Smartwatch | Accelerometer | A fall, a type of activity |
| Machine maintenance | Accelerometer, microphone | An abnormal vibration, before the breakdown |
| Agriculture | Camera | A diseased leaf, an insect |
1. Which stage is done on the computer, and which on the microcontroller?
2. What do you gain by quantising a model to int8?
3. Give one reason to classify the data on the board rather than in the cloud.