GithubHelp home page GithubHelp logo

istvanzk / tflm-arduino Goto Github PK

View Code? Open in Web Editor NEW
0.0 1.0 0.0 1.93 MB

This experimental fork is based on the old official TensorFlow Lite Micro Library for Arduino Examples, with modifications to work with the newer (2024+) official TensorFlow Lite for Microcontrollers implementation.

License: Apache License 2.0

C 4.00% C++ 96.00%

tflm-arduino's Introduction

TFLM implementation for Arduino

Exp Lic Py TensorFlow 2.15 Ver HWD

This experimental fork is based on the old official TensorFlow Lite Micro Library for Arduino Examples with modifications to work with the newer (2024+) official TensorFlow Lite for Microcontrollers implementation.

To avoid compatibility issues with the old official TensorFlow Lite Micro Library for Arduino Examples, this Arduino library should be installed as TFLM_Arduino and the ino code uses #include <TensorFlowLiteMicro.h>. Its version number has been reseted to indicate a new fork of the library.

The included micro_speech_pico implementation is using the modifications introduced in MICRO_SPEECH implementation for PICO-TFLMICRO and requires the Raspberry Pi Pico Arduino core, for all RP2040 boards by Earle F. Philhower, to be installed and used in Arduino IDE instead of the Arduino Mbed OS RP2040 Boards.

Table of contents

How to Install

GitHub

To avoid compatibility issues with the old official TensorFlow Lite Micro Library for Arduino Examples, this Arduino library should be installed as TFLM_Arduino and the ino code uses #include <TensorFlowLiteMicro.h>.

To install this experimental version of the library, requires you to clone the repo into the folder that holds libraries for the Arduino IDE. The location for this folder varies by operating system, but typically it's in ~/Arduino/libraries on Linux, ~/Documents/Arduino/libraries/ on MacOS, and My Documents\Arduino\Libraries on Windows.

Once you're in that folder in the terminal, you can then grab the code using the git command line tool:

git clone https://github.com/istvanzk/tflm-arduino TFLM_Arduino

To update your clone of the repository to the latest code, use the following terminal commands:

cd TFLM_Arduino
git pull

Please note that the micro_speech_pico example requires the Raspberry Pi Pico Arduino core, for all RP2040 boards by Earle F. Philhower, to be installed and used (follow therin provided installation guidelines) instead of the Arduino Mbed OS RP2040 Boards.

Checking your Installation

Once the library has been installed, you should then start the Arduino IDE. You will now see an TFLM_Arduino entry in the File -> Examples menu of the Arduino IDE. This submenu contains the hello_world and micro_speech_pico sample projects you can try out.

Updates

The code here is created through a manual project generation process and may differ from the source of truth, since it's cross-platform and needs to be modified to work within the Arduino IDE. For now, the code cannot be synced to official TensorFlow Lite for Microcontrollers nor to the old TensorFlow Lite Micro Library for Arduino Examples.

The follwing code provided in the old official TensorFlow Lite Micro Library for Arduino Examples, has been removed in this fork:

  • The scripts folder and its content
  • The docs folder and its content
  • The src/tensorflow/lite/experimental/microfrontend/lib folder and its content (not used by the included micro_speech_pico example)
  • The examples/magic_wand, examples/person_detection examples

The follwing code provided in the old official TensorFlow Lite Micro Library for Arduino Examples, has been modified in this fork:

  • The src/tensorflow/lite/micro/system_setup.cpp (see below for details)
  • The src/peripherals (see below for details)
  • The hello_world.ino example is modified to pull in only the operation implementations needed, instead of all available TFLM operators
  • The included micro_speech_pico example is modified to work specifically with Raspberry Pi Pico (RP2040) compatible boards, hence its new name

Compatibility

This TFLM framework code for running machine learning models should be compatible with most Arm Cortex M-based boards, such as the Raspberry Pi Pico or Nano 33 BLE Sense.

However, please note that the original code in src/peripherals from TensorFlow Lite Micro Library for Arduino Examples, meant to be used to access peripherals like microphones, cameras, and accelerometers, has been modified in this fork, as described below.

The modified src/peripherals includes a bare minimum implementation, for only three peripherals:

See the /src/peripherals/README for details. These adaptations were required to make the library code work with the latest official Arduino and Tensorflow Lite Micro libraries.

The included micro_speech_pico example code has been modified to work specifically with Raspberry Pi Pico (RP2040) compatible boards.

The peripherals.h implementation uses:

#if defined(ARDUINO_SPARKFUN_MICROMOD_RP2040)
#define RP2040_BOARD 
...

and the system_setup.cpp implementation uses:

#if defined(ARDUINO) && !defined(ARDUINO_SPARKFUN_MICROMOD_RP2040)
#define ARDUINO_EXCLUDE_CODE
#endif  // defined(ARDUINO) && !defined(ARDUINO_SPARKFUN_MICROMOD_RP2040)

#ifndef ARDUINO_EXCLUDE_CODE
...

Therefore, if you want to experiment with and adapt the code for other RP2040 based boards, you'll need to edit these two files accordingly by changing the ARDUINO_SPARKFUN_MICROMOD_RP2040 to another board preprocessing definition name. The board preprocessor definition format in Arduino IDE is: ARDUINO_<PROCESSOR-DESCRIPTOR>_<BOARDNAME>. The <PROCESSOR-DESCRIPTOR>_<BOARDNAME> strings for the installed boards can be listed by running in the base Arduino directory, the following command:

grep board= `find . -name boards.txt` | cut -f2 -d= | sort -u

NOTE: The system_setup.cpp implementation fixes at least two issues encoutered with the older version of the library:

  1. The error of missing/wrong definition for DebugLog() function, see e.g. undefined reference to 'DebugLog' in micro_error_reporter.cpp. The tensorflow/lite/micro/micro_error_reporter.h is not used anymore, and is replaced by the use of tensorflow/lite/micro/micro_log.h: MicroPrintf() uses DebugLog() and DebugLog() uses the Arduino Serial.printf()

  2. The missing correct DebugVsnprintf() function, which allows the use of the MicroVsnprintf() to output printf like formated strings to the serial output/console. DebugVsnprintf() uses vsnprintf().

  3. Most of the new system_setup.cpp implementation is pulled in only if !defined(TF_LITE_STRIP_ERROR_STRINGS) and TF_LITE_STRIP_ERROR_STRINGS can be undefined e.g. in micro_speech_pico.ino. Else, a simple DebugLog() function without printf like formating is compiled, to reduce the code size.

License

This code is made available under the Apache 2 license.

Contributing

Forks of this library are welcome and encouraged.

tflm-arduino's People

Contributors

istvanzk avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.