Skip to content

Commit 3c734fa

Browse files
committed
Create TFLITE Code Generator
1 parent 3e88521 commit 3c734fa

18 files changed

+1271
-0
lines changed

tools/.gitignore

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
.output
2+
map.txt
3+
main/include/user_config.local.h
4+
bin
5+
build
6+
*.old
7+
*.swp
8+
sdkconfig
9+
config.local.h
10+
coredump.txt
11+
.settings
12+
.cproject
13+
.project
14+
config.local.h
15+
out.txt
16+
idf_modify_log.txt
17+
*.orig
18+
*.elf
19+
*.s
20+
.vscode/
21+
sdkconfig
22+
23+
## Mac OS files
24+
.DS_Store
25+
26+
## virtual environment
27+
env/

tools/README.md

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
# TFlite-Micro Tools for Espressif Chipsets
2+
3+
This repository offers a code generation tool designed to facilitate the creation of essential C++ code files necessary for deploying TensorFlow Lite models on Espressif micro-controllers. It caters specifically to the requirements of TensorFlow Lite Micro, enabling seamless integration of machine learning models into embedded systems.
4+
5+
By utilizing this tool, developers can generate C++ code that efficiently executes TensorFlow Lite models. This tool serves as a valuable asset for developers seeking to leverage the power of TensorFlow Lite for developing IoT applications through Espressif Chipsets.
6+
7+
8+
# Features
9+
10+
- Converts TensorFlow Lite model files (.tflite) into C++ unsigned integer arrays storing hex values of .tflite models for efficient deployment on Espressif Chipsets.
11+
12+
- Generates a micro mutable op resolver based on the model, which extracts all the operations associated with the model.
13+
14+
- Creates template code for the main function, simplifying the integration of TensorFlow Lite models into Espressif based real time microcontroller applications.
15+
16+
# Setup Environment
17+
- Clone the [tflite-micro](https://github.com/tensorflow/tflite-micro/) repository locally on your machine.
18+
- Set paths for TFLITE_PATH and PYTHONPATH necessary for running the python scripts associated with generating array for model and micro mutable op resolver which extracts the model's operations.
19+
```
20+
export TFLITE_PATH=path/to/cloned/repository
21+
export PYTHONPATH=path/to/directory/of/cloned/repository
22+
```
23+
- Once the paths have been set, create a python virtual environment by following the steps below.
24+
```
25+
# create virtual environment - env
26+
python3 -m venv env
27+
28+
# activate the environment
29+
source env/bin/activate
30+
31+
# install requirements.txt
32+
pip install -r requirements.txt
33+
```
34+
35+
# Usage
36+
37+
This script can convert any tflite model to example template which then can be integrated with any project.
38+
39+
```
40+
python main.py model.tflite
41+
```
42+
This command performs the following:
43+
44+
- Convert the tflite model to a C++ unsigned integer array representation using generate_cc_arrays.py.
45+
46+
- Generate a micro mutable operation resolver based on the model using generate_micro_mutable_op_resolver_from_model.py.
47+
48+
- Generate templates for main functions using generate_main_templates.py.
49+
50+
- Extract relevant information from the generated files and create C++ files required for the application.
51+
52+
53+
# Building the project
54+
55+
The command idf.py build is typically associated with the ESP-IDF (Espressif IoT Development Framework), which is the official development framework for the Espressif microcontrollers.
56+
57+
```
58+
idf.py build
59+
```
60+
61+
After executing `idf.py build`, it initiates the build process for the current project. This command compiles the source code files, resolves dependencies, and generates the firmware binary that can be flashed onto the Espressif microcontroller. Developers can further use this image for real time applications and use cases.
62+
63+
# Customization
64+
65+
You can modify the templates in the templates.py file to customize the generated code according to your project requirements.
66+
67+
Adjust the code and templates as needed to suit your specific use case.
68+
69+
70+
# Resources
71+
72+
- [Tensorflow Lite for Microcontrollers](https://github.com/tensorflow/tflite-micro)
73+
74+
- [TensorFlow Lite Micro for Espressif Chipsets](https://github.com/espressif/tflite-micro-esp-examples)
75+
76+
- [Espressif IoT Development Framework](https://github.com/espressif/esp-idf)
77+
78+
- [CMake Documentation](https://cmake.org/documentation/)
79+
80+
 

tools/hello_world.tflite

2.45 KB
Binary file not shown.

tools/hello_world/CMakeLists.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
2+
# The following lines of boilerplate have to be in your project's
3+
# CMakeLists in this exact order for cmake to work correctly
4+
cmake_minimum_required(VERSION 3.5)
5+
set(EXTRA_COMPONENT_DIRS ../../components)
6+
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
7+
project(test)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
2+
idf_component_register(SRCS main_functions.cc main.cc hello_world_model_data.cc output_handler.cc constants.cc
3+
INCLUDE_DIRS "")
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
2+
/* Copyright 2019 The TensorFlow Authors. All Rights Reserved.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
==============================================================================*/
16+
17+
#include "constants.h"
18+
19+
// This is a small number so that it's easy to read the logs
20+
const int kInferencesPerCycle = 20;

tools/hello_world/main/constants.h

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
2+
/* Copyright 2019 The TensorFlow Authors. All Rights Reserved.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
==============================================================================*/
16+
17+
#ifndef TENSORFLOW_LITE_MICRO_EXAMPLES_HELLO_WORLD_CONSTANTS_H_
18+
#define TENSORFLOW_LITE_MICRO_EXAMPLES_HELLO_WORLD_CONSTANTS_H_
19+
20+
// This constant represents the range of x values our model was trained on,
21+
// which is from 0 to (2 * Pi). We approximate Pi to avoid requiring additional
22+
// libraries.
23+
const float kXrange = 2.f * 3.14159265359f;
24+
25+
// This constant determines the number of inferences to perform across the range
26+
// of x values defined above. Since each inference takes time, the higher this
27+
// number, the more time it will take to run through the entire range. The value
28+
// of this constant can be tuned so that one full cycle takes a desired amount
29+
// of time. Since different devices take different amounts of time to perform
30+
// inference, this value should be defined per-device.
31+
extern const int kInferencesPerCycle;
32+
33+
#endif
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/* Copyright 2023 The TensorFlow Authors. All Rights Reserved.
2+
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
7+
http://www.apache.org/licenses/LICENSE-2.0
8+
9+
Unless required by applicable law or agreed to in writing, software
10+
distributed under the License is distributed on an "AS IS" BASIS,
11+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
See the License for the specific language governing permissions and
13+
limitations under the License.
14+
==============================================================================*/
15+
16+
// Generated based on hello_world.tflite.
17+
18+
#pragma once
19+
20+
#include "tensorflow/lite/micro/micro_mutable_op_resolver.h"
21+
22+
constexpr int kNumberOperators = 3;
23+
24+
inline tflite::MicroMutableOpResolver<kNumberOperators> get_resolver()
25+
{
26+
tflite::MicroMutableOpResolver<kNumberOperators> micro_op_resolver;
27+
28+
micro_op_resolver.AddDequantize();
29+
micro_op_resolver.AddFullyConnected();
30+
micro_op_resolver.AddQuantize();
31+
32+
return micro_op_resolver;
33+
}

0 commit comments

Comments
 (0)