Skip to content

Commit c6e9c15

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

17 files changed

+1325
-0
lines changed

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: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
2+
/*
3+
4+
SPDX-FileCopyrightText: 2023 Espressif Systems (Shanghai) CO LTD
5+
SPDX-License-Identifier: Apache-2.0
6+
7+
8+
Copyright 2023 The TensorFlow Authors. All Rights Reserved.
9+
10+
Licensed under the Apache License, Version 2.0 (the "License");
11+
you may not use this file except in compliance with the License.
12+
You may obtain a copy of the License at
13+
14+
http://www.apache.org/licenses/LICENSE-2.0
15+
16+
Unless required by applicable law or agreed to in writing, software
17+
distributed under the License is distributed on an "AS IS" BASIS,
18+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19+
See the License for the specific language governing permissions and
20+
limitations under the License.
21+
==============================================================================*/
22+
23+
#include "constants.h"
24+
25+
// This is a small number so that it's easy to read the logs
26+
const int kInferencesPerCycle = 20;

tools/hello_world/main/constants.h

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