Skip to content

Instantly share code, notes, and snippets.

TensorFlow Lite for Microcontrollers

This an experimental port of TensorFlow Lite aimed at micro controllers and other devices with only kilobytes of memory. It doesn't require any operating system support, any standard C or C++ libraries, or dynamic memory allocation, so it's designed to be portable even to 'bare metal' systems. The core runtime fits in 16KB on a Cortex M3, and with enough operators to run a speech keyword detection model, takes up a total of 22KB.

Table of Contents

TensorFlow Lite for Microcontrollers

This an experimental port of TensorFlow Lite aimed at micro controllers and other devices with only kilobytes of memory. It doesn't require any operating system support, any standard C or C++ libraries, or dynamic memory allocation, so it's designed to be portable even to 'bare metal' systems. The core runtime fits in 16KB on a Cortex M3, and with enough operators to run a speech keyword detection model, takes up a total of 22KB.

Table of Contents

@petewarden
petewarden / Makefile
Created January 21, 2019 21:34
Call `make -f Makefile` on this gives the error `/tmp/Makefile:9: *** multiple target patterns. Stop.`. Pasting the content directly into the file instead of calling doesn't! Any ideas anyone?
define call_test
%: %
echo "foo"
foo/%: %
echo "foo"
endef
$(call call_test)
# To convert from .ogg files to .wavs:
# find . -iname "*.ogg" -print0 | xargs -0 basename -s .ogg | xargs -I {} ffmpeg -i {}.ogg -ar 16000 ../converted_early_wavs/{}.wav
# Then run the extract_loudest xcode project to get one-second clips.
import glob
import os
import re
import shutil
data_index = {}
@petewarden
petewarden / strip_names.py
Created January 29, 2020 20:35
Example of using the new Flatbuffers Python API to modify a TensorFlow Lite file
def StripTfliteFile(tflite_input, tflite_output):
"""Given 'tflite_input`, write stripped version, 'tflite_output'."""
if not os.path.exists(tflite_input):
raise RuntimeError("Invalid filename %r" % tflite_input)
with open(tflite_input, "rb") as file_handle:
file_data = bytearray(file_handle.read())
model_obj = schema_fb.Model.GetRootAsModel(file_data, 0)
model = schema_fb.ModelT.InitFromObj(model_obj)
model.description = ""
for subgraph in model.subgraphs:
@petewarden
petewarden / magic_wand_gesture.ino
Created February 17, 2020 22:07
TensorFlow Lite Micro sketch for Arduino that captures gesture data for training magic wand gestures
/* Copyright 2020 The TensorFlow Authors. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
@petewarden
petewarden / convert_cc_to_tflite.py
Created February 27, 2020 23:55
Example of converting a .cc TensorFlow Lite C data array file back into a binary flatbuffer on disk
import re
output_data = bytearray()
with open('tensorflow/tensorflow/lite/micro/examples/magic_wand/magic_wand_model_data.cc', 'r') as file:
for line in file:
values_match = re.match(r"\W*(0x[0-9a-fA-F,x ]+).*", line)
if values_match:
list_text = values_match.group(1)
values_text = filter(None, list_text.split(","))
values = [int(x, base=16) for x in values_text]
output_data.extend(values)
# ==============================================================================
"""LSTM quantization with python."""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import pathlib
from absl import app
@petewarden
petewarden / bashrc.sh
Created January 31, 2022 23:41
Bash history settings to tweak for infinite history, instant appending of commands, and shared history across multiple terminals.
# By Pete Warden, @petewarden
# To use this, open ~/.bashrc in your editor of choice,
# and place these settings at the end.
# Based on https://unix.stackexchange.com/a/48113 and
# https://blog.sanctum.geek.nz/better-bash-history/
# Ignore both duplicate commands, and those that start with a space.
HISTCONTROL=ignoreboth
# Append to the history file, don't overwrite it.
@petewarden
petewarden / trace.h
Created February 15, 2022 19:12
Header file for simple variable tracing macros
#ifndef INCLUDE_TRACE_H
#define INCLUDE_TRACE_H
#include <stdio.h>
#include <stdint.h>
#define TRACE_STR(variable) do { fprintf(stderr, __FILE__":%d "#variable"=%s\n", __LINE__, variable); } while (0)
#define TRACE_INT(variable) do { fprintf(stderr, __FILE__":%d "#variable"=%d\n", __LINE__, variable); } while (0)
#define TRACE_PTR(variable) do { fprintf(stderr, __FILE__":%d "#variable"=0x%016lx\n", __LINE__, (uint64_t)(variable)); } while (0)
#define TRACE_SIZ(variable) do { fprintf(stderr, __FILE__":%d "#variable"=%zu\n", __LINE__, variable); } while (0)