Skip to content

A much simpler approach to the basics of implementing machine learning on microcontrollers, specifically Arduino

License

Notifications You must be signed in to change notification settings

TSprech/Easy-Tensorflow-Lite

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Tensorflow GitHub

Easy Tensorflow Lite

A much simpler approach to the basics of implementing machine learning on microcontrollers, specifically Arduino. This strips away most of the extra information when training and running a model in a microcontroller environment. It is also meant to simplify machine learning down to a basic example: number mapping.

Tested boards

  • SparkFun RedBoard ATP
  • Adafruit Grand Central M4

Both of these boards use the same or similar MCUs which have been tested by Tensorflow to be compatible with the Tensorflow library. More information about Tensorflow tested boards can be found here.

Circuit

Fritzing diagram for wiring up 3 push buttons and 3 LEDs to the SparkFun Artemis

This circuit is one of the simplest examples of machine learning. It takes in a button input and outputs to a corresponding LED. This model is designed such that 0 corresponds to 1, 1 to 2, and 2 to 0. While this could be accomplished by much simpler methods, it is intended to be an easier start into training, exporting, loading, and debugging a custom Tensorflow model.

This is not intended to be a full delve into Tensorflow Lite or TinyML in general. This is meant to be a simple, straightforward example of Tensorflow with no extras.

Usage

Programming Knowledge

While full understanding is not required, it is highly recommended to have some knowledge on these topics

Python

  • Library knowledge
    • Tensorflow
      • Setting up and training a Keras model
      • Utilizing the docs - What is shown here does not scratch the surface of Tensorflow's capabilities, the docs have almost all the info you could need
    • Pandas
      • Data manipulation
    • Numpy
      • Focus especially on arrays
    • Matplot
      • While not used much in the model here to maintain simplicity, this is utilized heavily in machine learning
  • Other Knowledge
    • Google Colab
      • Colab is the primary platform to train models on
      • Comfort navigating and working with importing and exporting files
    • Importing and installing libraries

C++

Required once the model is created to allow it to run on microcontrollers

  • Libraries Knowledge
    • Tensorflow
      • Working with loading, allotting, instantiating, and debugging
    • Arduino
      • General board IO
  • Other Knowledge
    • C++ Classes - It is important to understand how they work and how to implement them

Training the Model

Upload Easy Tensorflow Lite Colab.ipynb to your Google Drive and open it. From there you either view the original output or run it to create a new model.

Transferring to Project

Copying the output bytes and the length of the buffer to simple_model_data.cc in the corresponding locations will allow you to run your own model.

PlatformIO Setup

To get the SparkFun Artemis boards installed into PlatformIO, follow the instructions on this repository. Note that on Windows you cannot use the platformio command. You must copy the path to the PlatformIO exe file and use that instead

*Notes as of writing this:

  1. The path to the project must not contain any spaces to compile correctly
  2. Projects must be created from the terminal to generate the proper files
  3. The condition upload_speed = must be added to the project INI file, 921600 works
  4. To compile without a connected board, add upload_port = to the project INI file
  5. A model can convert without errors but not work on a microcontroller due to incorrect conversion parameters; this will often result in a large number that will often overflow

Other Examples

I have also included an example that uses only the serial monitor in the event the breadboard supplies above are not available. It allows for input from the serial monitor and the microcontroller echos back the prediction. It can be found under /Other Examples.

Credits

Libraries/Examples

  • Condensed Tensorflow Lite library and code references from wezleysherman
  • As stated above, SparkFun Artemis PlatformIO support from nigelb
  • Reference for converting models to TFLite models from tensorflow

Additional Resources

License

GPL-3.0

This software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

About

A much simpler approach to the basics of implementing machine learning on microcontrollers, specifically Arduino

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published