Machine Learning with Arduino and TensorFlow

Machine Learning allows computer algorithms to automatically improve through increased experience. Now you can even deploy the resulting learned model to microcontroller devices so they can implement the same algortihms!

This allows you to distill vast amounts of data to create predictive models, deployable to microcontrollers, as well as higher power machines using TensorFlow.


Get Going...

Arduino: Getting Started with Machine Learning

Learn how to setup Arduino with Tensorflow and vMicro, and see your board predict the fade value (sin()), quite accurately!

If you are using the ESP32 then the ESP32 variant of the Tensorflow Library should be used, and the examples which come with it.


Data Modelling

You can now move on to building your own model for hello world from scratch, so you can see how much goes into this accurate prediction mechanism.

In the hello_world example, the model allows the value of sin(x) to be prediced from the model, given a value for x.

Google Collab

This is an online service, which allows for tensor flow model compilation on a cloud machine, along with useful notebook features.

Very useful for learning with, and many examples for Tensorflow run in this environment.

 

We reccommend for the first run to follow this through in their brilliant tutorial below:

Google Colaboratory: train_hello_world_model.ipynb

Local Modelling

You can install Python and the dependant modules below on your machine, to allow the models to be built locally.

#Python Modules to Build TensorFlow Locally
pip install numpy
pip install tensorflow
pip install matplotlib
                        

 

Now you can clone the code from Colab to a *.py script on your machine, and run it locally.

Our local version of hello_world is available here, and shown in the video at the bottom of this page.

NOTE:

Note IconNote:

Depending on the specification of your machine, and GPU acceleration available, it may be considerably slower locally, compared to Colab.


Data Gathering

As all of the Arduino boards which can harness the ML model, also have the power to detect a whole host of other data via sensors, there is a good chance doing both is also useful!

e.g. A basic example:

A cooling setup whose aim is to keep cooling a warm environment to xx degrees, whereby the predictions are used to try and anticipate any actions to take, and the data is logged back to show what actions had to be taken.

The control system on the Arduino would run with the predictive data unless there were other alarms triggered which override its behaivour.

This can vastly reduce the time it takes to gather the data needed to make a reliable model.

 

In the example we use here we are essentially just breaking up the hello_world example, so that the ESP32 will send the data back to the Model builder, for the sin() function (with noise added).

It will also be using the previous upload of the model to flash its red LED at the predicted brightness (we eventually get a smooth curve as in the example test model).

(Note: there is also additional code present for OTA Uploads from the Examples section)

Download ESP32EdgeDevice Example Sketch for ML


Cloud Building

We have a great Instructable on using the vMicro CLI on a machine, and triggering it from Azure Dev Ops, allowing for custom build processes, and even deployments from a Cloud Work + Version Control System.


"In a team or commercial setting it can be useful to have the code automatically build on a regular basis within your cloud management solution, often as part of continuous integration.
Here we can leverage the Visual Micro Command Line Interface (CLI) on our Build server to build the code, for a variety of boards or debug levels for example."

Distributed Machine Learning

With all the previous tabs considered, its interesting to realise with what you now know you can have a completely distributed team of developers, along with testers, modellers, and data gatherers too.

The devices can use the model learned to date for a purpose, whilst sending back all data needed to improve the model for the future, as well as receive updated models...... its continuous integration for a distributed machine learning system...

This can be greatly useful, given the devices are likely small enough to ship anywhere, and can be updated via a variety of means with new models when available

It also allows all of the Arduino nodes to learn from each others data with each update, making it a truly distributed machine learning system!

This Video here touches on all topics needed to be able to build a self learning basic system, using Arduino's for the data collection, and harnessing the model.

The ESP32 Edge Device Sketch Example for this can be downloaded from here.