From b9d99cae6705ecb6026afcc3b386ceb016fdc308 Mon Sep 17 00:00:00 2001 From: 001ProMax <101964350+001ProMax@users.noreply.github.com> Date: Sat, 11 Jan 2025 12:41:52 +0800 Subject: [PATCH 1/3] Add files via upload --- tensorflow/lite/array.h | 123 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 123 insertions(+) create mode 100644 tensorflow/lite/array.h diff --git a/tensorflow/lite/array.h b/tensorflow/lite/array.h new file mode 100644 index 00000000..5a60784f --- /dev/null +++ b/tensorflow/lite/array.h @@ -0,0 +1,123 @@ +/* Copyright 2023 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_ARRAY_H_ +#define TENSORFLOW_LITE_ARRAY_H_ + +#include +#include +#include +#include +#include + +#include "tensorflow/lite/core/c/common.h" + +namespace tflite { + +/// TfLite*Array helpers + +namespace array_internal { + +// Function object used as a deleter for unique_ptr holding TFLite*Array +// objects. +struct TfLiteArrayDeleter { + void operator()(TfLiteIntArray* a); + void operator()(TfLiteFloatArray* a); +}; + +// Maps T to the corresponding TfLiteArray type. +template +struct TfLiteArrayInfo; + +template <> +struct TfLiteArrayInfo { + using Type = TfLiteIntArray; +}; + +template <> +struct TfLiteArrayInfo { + using Type = TfLiteFloatArray; +}; + +} // namespace array_internal + +template +using TfLiteArrayUniquePtr = + std::unique_ptr::Type, + array_internal::TfLiteArrayDeleter>; + +// `unique_ptr` wrapper for `TfLiteIntArray`s. +using IntArrayUniquePtr = TfLiteArrayUniquePtr; + +// `unique_ptr` wrapper for `TfLiteFloatArray`s. +using FloatArrayUniquePtr = TfLiteArrayUniquePtr; + +// Allocates a TfLiteArray of given size using malloc. +// +// This builds an int array by default as this is the overwhelming part of the +// use cases. +template +TfLiteArrayUniquePtr BuildTfLiteArray(int size); + +// Allocates a TfLiteIntArray of given size using malloc. +template <> +inline IntArrayUniquePtr BuildTfLiteArray(const int size) { + return IntArrayUniquePtr(TfLiteIntArrayCreate(size)); +} + +// Allocates a TfLiteFloatArray of given size using malloc. +template <> +inline FloatArrayUniquePtr BuildTfLiteArray(const int size) { + return FloatArrayUniquePtr(TfLiteFloatArrayCreate(size)); +} + +// Allocates a TFLiteArray of given size and initializes it. +// +// `values` is expected to holds `size` elements. +template +TfLiteArrayUniquePtr BuildTfLiteArray(const int size, + const T* const values) { + auto array = BuildTfLiteArray(size); + if (array) { + memcpy(array->data, values, size * sizeof(T)); + } + return array; +} + +// Allocates a TFLiteArray and initializes it with the given values. +template +TfLiteArrayUniquePtr BuildTfLiteArray(const std::vector& values) { + return BuildTfLiteArray(static_cast(values.size()), values.data()); +} + +// Allocates a TFLiteArray and initializes it with the given values. +template +TfLiteArrayUniquePtr BuildTfLiteArray( + const std::initializer_list& values) { + return BuildTfLiteArray(static_cast(values.size()), values.begin()); +} + +// Allocates a TFLiteArray and initializes it with the given array. +inline IntArrayUniquePtr BuildTfLiteArray(const TfLiteIntArray& other) { + return BuildTfLiteArray(other.size, other.data); +} + +// Allocates a TFLiteArray and initializes it with the given array. +inline FloatArrayUniquePtr BuildTfLiteArray(const TfLiteFloatArray& other) { + return BuildTfLiteArray(other.size, other.data); +} + +} // namespace tflite + +#endif // TENSORFLOW_LITE_ARRAY_H_ From cf55b461a91ef27f6e7b50183379246e8a9b291d Mon Sep 17 00:00:00 2001 From: 001ProMax <101964350+001ProMax@users.noreply.github.com> Date: Sat, 11 Jan 2025 12:57:52 +0800 Subject: [PATCH 2/3] Update README.md Support PlatformIO --- README.md | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/README.md b/README.md index 00c91eeb..b9fe92aa 100644 --- a/README.md +++ b/README.md @@ -18,6 +18,8 @@ We keep track with the ESP-IDF's support period policy mentioned [here](https:// Currently ESP-IDF versions `release/v4.4` and above are supported by this project. +## Method 1: Using ESP IDF + ### Install the ESP IDF Follow the instructions of the @@ -29,6 +31,27 @@ The next steps assume that this installation is successful and the * the `IDF_PATH` environment variable is set * the `idf.py` and Xtensa-esp32 tools (e.g., `xtensa-esp32-elf-gcc`) are in `$PATH` +## Method 2: Using PlatformIO + +### Configuration + +Add the following configuration to your `platformio.ini` file: + +```ini +[env:your_env_name] +build_flags = + -I ${PROJECT_LIBDEPS_DIR}/${PIOENV}/esp-tflite-micro/third_party/flatbuffers/include + -I ${PROJECT_LIBDEPS_DIR}/${PIOENV}/esp-tflite-micro/third_party/gemmlowp + -I ${PROJECT_LIBDEPS_DIR}/${PIOENV}/esp-tflite-micro/third_party/kissfft + -I ${PROJECT_LIBDEPS_DIR}/${PIOENV}/esp-tflite-micro/third_party/ruy + -Ofast + +lib_deps = + https://github.com/espressif/esp-tflite-micro.git + +; src_dir = .pio/libdeps/esp32-c6-devkitc-1/esp-tflite-micro/examples/hello_world # build hello_world example +``` + ## Using the component Run the following command in your ESP-IDF project to install this component: From a58644ffd9a12f9196aaf9216e1c3e1665f38534 Mon Sep 17 00:00:00 2001 From: 001ProMax <101964350+001ProMax@users.noreply.github.com> Date: Sat, 11 Jan 2025 12:59:25 +0800 Subject: [PATCH 3/3] Update README.md --- README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index b9fe92aa..e4c19013 100644 --- a/README.md +++ b/README.md @@ -49,7 +49,8 @@ build_flags = lib_deps = https://github.com/espressif/esp-tflite-micro.git -; src_dir = .pio/libdeps/esp32-c6-devkitc-1/esp-tflite-micro/examples/hello_world # build hello_world example +; src_dir = + -I ${PROJECT_LIBDEPS_DIR}/${PIOENV}/esp-tflite-micro/examples/hello_world ``` ## Using the component