-
Notifications
You must be signed in to change notification settings - Fork 7.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* create TFLite library * add TFLite hello_world example * add TFLite micro_speech example --------- Co-authored-by: Sanket Wadekar <[email protected]>
- Loading branch information
1 parent
ab6a25e
commit 1da0ca8
Showing
29 changed files
with
3,711 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# Hello World Example | ||
|
||
This example is designed to demonstrate the absolute basics of using [TensorFlow | ||
Lite for Microcontrollers](https://www.tensorflow.org/lite/microcontrollers). | ||
It includes the full end-to-end workflow of training a model, converting it for | ||
use with TensorFlow Lite for Microcontrollers for running inference on a | ||
microcontroller. | ||
|
||
The model is trained to replicate a `sine` function and generates a pattern of | ||
data to either blink LEDs or control an animation, depending on the capabilities | ||
of the device. | ||
|
||
## Deploy to ESP32 | ||
|
||
The sample has been tested on ESP-IDF version `release/v4.2` and `release/v4.4` with the following devices: | ||
- [ESP32-DevKitC](http://esp-idf.readthedocs.io/en/latest/get-started/get-started-devkitc.html) | ||
- [ESP32-S3-DevKitC](https://docs.espressif.com/projects/esp-idf/en/latest/esp32s3/hw-reference/esp32s3/user-guide-devkitc-1.html) | ||
- [ESP-EYE](https://github.com/espressif/esp-who/blob/master/docs/en/get-started/ESP-EYE_Getting_Started_Guide.md) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
/* Copyright 2019 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, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
==============================================================================*/ | ||
|
||
#include "constants.h" | ||
|
||
// This is a small number so that it's easy to read the logs | ||
const int kInferencesPerCycle = 20; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/* Copyright 2019 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, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
==============================================================================*/ | ||
|
||
#ifndef TENSORFLOW_LITE_MICRO_EXAMPLES_HELLO_WORLD_CONSTANTS_H_ | ||
#define TENSORFLOW_LITE_MICRO_EXAMPLES_HELLO_WORLD_CONSTANTS_H_ | ||
|
||
// This constant represents the range of x values our model was trained on, | ||
// which is from 0 to (2 * Pi). We approximate Pi to avoid requiring additional | ||
// libraries. | ||
const float kXrange = 2.f * 3.14159265359f; | ||
|
||
// This constant determines the number of inferences to perform across the range | ||
// of x values defined above. Since each inference takes time, the higher this | ||
// number, the more time it will take to run through the entire range. The value | ||
// of this constant can be tuned so that one full cycle takes a desired amount | ||
// of time. Since different devices take different amounts of time to perform | ||
// inference, this value should be defined per-device. | ||
extern const int kInferencesPerCycle; | ||
|
||
#endif // TENSORFLOW_LITE_MICRO_EXAMPLES_HELLO_WORLD_CONSTANTS_H_ |
111 changes: 111 additions & 0 deletions
111
libraries/TFLiteMicro/examples/hello_world/hello_world.ino
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
/* 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, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
==============================================================================*/ | ||
|
||
|
||
#include "tensorflow/lite/micro/micro_mutable_op_resolver.h" | ||
#include "tensorflow/lite/micro/micro_interpreter.h" | ||
#include "tensorflow/lite/micro/system_setup.h" | ||
#include "tensorflow/lite/schema/schema_generated.h" | ||
|
||
#include "model.h" | ||
#include "constants.h" | ||
#include "output_handler.h" | ||
|
||
// Globals, used for compatibility with Arduino-style sketches. | ||
namespace { | ||
const tflite::Model* model = nullptr; | ||
tflite::MicroInterpreter* interpreter = nullptr; | ||
TfLiteTensor* input = nullptr; | ||
TfLiteTensor* output = nullptr; | ||
int inference_count = 0; | ||
|
||
constexpr int kTensorArenaSize = 2000; | ||
uint8_t tensor_arena[kTensorArenaSize]; | ||
} // namespace | ||
|
||
// The name of this function is important for Arduino compatibility. | ||
void setup() { | ||
// Map the model into a usable data structure. This doesn't involve any | ||
// copying or parsing, it's a very lightweight operation. | ||
model = tflite::GetModel(g_model); | ||
if (model->version() != TFLITE_SCHEMA_VERSION) { | ||
MicroPrintf("Model provided is schema version %d not equal to supported " | ||
"version %d.", model->version(), TFLITE_SCHEMA_VERSION); | ||
return; | ||
} | ||
|
||
// Pull in only the operation implementations we need. | ||
static tflite::MicroMutableOpResolver<1> resolver; | ||
if (resolver.AddFullyConnected() != kTfLiteOk) { | ||
return; | ||
} | ||
|
||
// Build an interpreter to run the model with. | ||
static tflite::MicroInterpreter static_interpreter( | ||
model, resolver, tensor_arena, kTensorArenaSize); | ||
interpreter = &static_interpreter; | ||
|
||
// Allocate memory from the tensor_arena for the model's tensors. | ||
TfLiteStatus allocate_status = interpreter->AllocateTensors(); | ||
if (allocate_status != kTfLiteOk) { | ||
MicroPrintf("AllocateTensors() failed"); | ||
return; | ||
} | ||
|
||
// Obtain pointers to the model's input and output tensors. | ||
input = interpreter->input(0); | ||
output = interpreter->output(0); | ||
|
||
// Keep track of how many inferences we have performed. | ||
inference_count = 0; | ||
} | ||
|
||
// The name of this function is important for Arduino compatibility. | ||
void loop() { | ||
// Calculate an x value to feed into the model. We compare the current | ||
// inference_count to the number of inferences per cycle to determine | ||
// our position within the range of possible x values the model was | ||
// trained on, and use this to calculate a value. | ||
float position = static_cast<float>(inference_count) / | ||
static_cast<float>(kInferencesPerCycle); | ||
float x = position * kXrange; | ||
|
||
// Quantize the input from floating-point to integer | ||
int8_t x_quantized = x / input->params.scale + input->params.zero_point; | ||
// Place the quantized input in the model's input tensor | ||
input->data.int8[0] = x_quantized; | ||
|
||
// Run inference, and report any error | ||
TfLiteStatus invoke_status = interpreter->Invoke(); | ||
if (invoke_status != kTfLiteOk) { | ||
MicroPrintf("Invoke failed on x: %f\n", | ||
static_cast<double>(x)); | ||
return; | ||
} | ||
|
||
// Obtain the quantized output from model's output tensor | ||
int8_t y_quantized = output->data.int8[0]; | ||
// Dequantize the output from integer to floating-point | ||
float y = (y_quantized - output->params.zero_point) * output->params.scale; | ||
|
||
// Output the results. A custom HandleOutput function can be implemented | ||
// for each supported hardware target. | ||
HandleOutput(x, y); | ||
|
||
// Increment the inference_counter, and reset it if we have reached | ||
// the total number per cycle | ||
inference_count += 1; | ||
if (inference_count >= kInferencesPerCycle) inference_count = 0; | ||
} |
Oops, something went wrong.