This is an automated email from the ASF dual-hosted git repository.
jerpelea pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx-apps.git
The following commit(s) were added to refs/heads/master by this push:
new a81fe2f4d mlearning/tflite-micro: enable Makefile hello-world and
usable tflm
a81fe2f4d is described below
commit a81fe2f4d9201615cfd9be62056b3505d97e1262
Author: Abhishek Mishra <[email protected]>
AuthorDate: Wed Sep 9 07:33:19 2026 +0000
mlearning/tflite-micro: enable Makefile hello-world and usable tflm
Build tflm_hello from Makefile, allocate tensors in tflm, keep syslog
strings without debug, and honor CONFIG_DARKNET_YOLO_VER.
Assisted-by: Cursor:Grok-4.6
Signed-off-by: Abhishek Mishra <[email protected]>
---
mlearning/darknet/Makefile | 1 +
mlearning/tflite-micro/CMakeLists.txt | 4 +++-
mlearning/tflite-micro/Makefile | 44 ++++++++++++++++++++++++++++++----
mlearning/tflite-micro/tflm_tool.cc | 45 ++++++++++++++++++++++++-----------
4 files changed, 74 insertions(+), 20 deletions(-)
diff --git a/mlearning/darknet/Makefile b/mlearning/darknet/Makefile
index 627245fbd..94e80ba97 100644
--- a/mlearning/darknet/Makefile
+++ b/mlearning/darknet/Makefile
@@ -74,6 +74,7 @@ CSRCS +=$(SRC)/iseg_layer.c
CFLAGS += -Wno-shadow -Wno-strict-prototypes -Wno-unknown-pragmas
MODULE = $(CONFIG_DARKNET_YOLO)
+DARKNET_YOLO_VER = $(patsubst "%",%,$(strip $(CONFIG_DARKNET_YOLO_VER)))
darknet.zip:
$(Q) curl -L
https://github.com/pjreddie/darknet/archive/refs/heads/$(DARKNET_YOLO_VER).zip
-o darknet.zip
diff --git a/mlearning/tflite-micro/CMakeLists.txt
b/mlearning/tflite-micro/CMakeLists.txt
index a2756e7c9..919dcd146 100644
--- a/mlearning/tflite-micro/CMakeLists.txt
+++ b/mlearning/tflite-micro/CMakeLists.txt
@@ -76,7 +76,9 @@ if(CONFIG_TFLITEMICRO)
if(CONFIG_TFLITEMICRO_DEBUG)
list(APPEND COMMON_FLAGS -DTF_LITE_SHOW_MEMORY_USE)
list(APPEND COMMON_FLAGS -DTF_LITE_USE_CTIME)
- else()
+ endif()
+
+ if(NOT CONFIG_TFLITEMICRO_DEBUG AND NOT CONFIG_TFLITEMICRO_SYSLOG)
list(APPEND COMMON_FLAGS -DTF_LITE_STRIP_ERROR_STRINGS)
endif()
diff --git a/mlearning/tflite-micro/Makefile b/mlearning/tflite-micro/Makefile
index aba043805..179d0e9c3 100644
--- a/mlearning/tflite-micro/Makefile
+++ b/mlearning/tflite-micro/Makefile
@@ -38,6 +38,7 @@ tflite-micro.zip:
$(Q) patch -d $(TFLM_UNPACK) -p1 < 0001-dequantize-int8.patch
$(Q) patch -d $(TFLM_UNPACK) -p1 < 0002-quantize-int8.patch
$(Q) patch -d $(TFLM_UNPACK) -p1 < 0003-mean-int8.patch
+ $(Q) patch -d $(TFLM_UNPACK) -p1 <
0004-tflite-add-extern-C-to-main-function-to-avoid-c-mang.patch
# Download and unpack tarball if no git repo found
ifeq ($(wildcard $(TFLM_UNPACK)/.git),)
@@ -60,7 +61,9 @@ COMMON_FLAGS += -DTF_LITE_DISABLE_X86_NEON
ifneq ($(CONFIG_TFLITEMICRO_DEBUG),)
COMMON_FLAGS += -DTF_LITE_SHOW_MEMORY_USE
COMMON_FLAGS += -DTF_LITE_USE_CTIME
-else
+endif
+
+ifeq ($(CONFIG_TFLITEMICRO_DEBUG)$(CONFIG_TFLITEMICRO_SYSLOG),)
COMMON_FLAGS += -DTF_LITE_STRIP_ERROR_STRINGS
endif
@@ -106,11 +109,42 @@ endif
# extra hardware support.
-include $(TFLM_DIR)/tensorflow/lite/micro/nuttx/Makefile
+PROGNAME :=
+PRIORITY :=
+STACKSIZE :=
+MAINSRC :=
+
ifneq ($(CONFIG_TFLITEMICRO_TOOL),)
-MAINSRC = tflm_tool.cc
-PROGNAME = tflm
-PRIORITY = $(CONFIG_TFLITEMICRO_TOOL_PRIORITY)
-STACKSIZE = $(CONFIG_TFLITEMICRO_TOOL_STACKSIZE)
+PROGNAME += tflm
+PRIORITY += $(CONFIG_TFLITEMICRO_TOOL_PRIORITY)
+STACKSIZE += $(CONFIG_TFLITEMICRO_TOOL_STACKSIZE)
+MAINSRC += tflm_tool.cc
+endif
+
+ifneq ($(CONFIG_TFLITEMICRO_HELLOWORLD),)
+TFLM_HW_DIR := $(TFLM_UNPACK)/tensorflow/lite/micro/examples/hello_world
+TFLM_HW_MODELS := $(TFLM_HW_DIR)/models
+
+ifeq ($(wildcard $(TFLM_UNPACK)/.git),)
+$(TFLM_HW_MODELS)/hello_world_float_model_data.h \
+$(TFLM_HW_MODELS)/hello_world_int8_model_data.h: tflite-micro.zip
+endif
+
+$(TFLM_HW_MODELS)/hello_world_float_model_data.h:
+ $(Q) ( cd $(TFLM_HW_MODELS) && xxd -i hello_world_float.tflite ) | \
+ sed -e
's/hello_world_float_tflite/g_hello_world_float_model_data/g' > $@
+
+$(TFLM_HW_MODELS)/hello_world_int8_model_data.h:
+ $(Q) ( cd $(TFLM_HW_MODELS) && xxd -i hello_world_int8.tflite ) | \
+ sed -e
's/hello_world_int8_tflite/g_hello_world_int8_model_data/g' > $@
+
+context:: $(TFLM_HW_MODELS)/hello_world_float_model_data.h
+context:: $(TFLM_HW_MODELS)/hello_world_int8_model_data.h
+
+PROGNAME += tflm_hello
+PRIORITY += $(CONFIG_TFLITEMICRO_HELLOWORLD_PRIORITY)
+STACKSIZE += $(CONFIG_TFLITEMICRO_HELLOWORLD_STACKSIZE)
+MAINSRC += $(TFLM_HW_DIR)/hello_world_test.cc
endif
CFLAGS += ${COMMON_FLAGS}
diff --git a/mlearning/tflite-micro/tflm_tool.cc
b/mlearning/tflite-micro/tflm_tool.cc
index 200df5391..45560bab3 100644
--- a/mlearning/tflite-micro/tflm_tool.cc
+++ b/mlearning/tflite-micro/tflm_tool.cc
@@ -24,6 +24,8 @@
* Included Files
****************************************************************************/
+#include <stdio.h>
+#include <stdlib.h>
#include <unistd.h>
#include <cstdint>
@@ -44,7 +46,7 @@ static void usage(void)
"[ -C ] Compile tflite model into c++ codes.\n"
"[ -E ] Do once evaluation (for profiling).\n"
"[ -i <str> ] Readable model file path.\n"
- "[ -o <str> ] Writable c++ file path.\n"
+ "[ -o <str> ] Writable c++ file path (required with -C).\n"
"[ -p <str> ] Prefix of compiled code.\n"
"[ -a <int> ] Arena size (mempool).\n"
"[ -h ] Print this message.\n");
@@ -93,13 +95,19 @@ extern "C" int main(int argc, FAR char* argv[])
}
}
- if (!modelFileName || !codeFileName)
+ if (!modelFileName || (need_compile && !codeFileName))
{
usage();
return -1;
}
std::ifstream ifs(modelFileName, std::ios::binary);
+ if (!ifs)
+ {
+ printf("Failed to open model file: %s\n", modelFileName);
+ return -1;
+ }
+
ifs.seekg(0, std::ios::end);
size_t modelSize = ifs.tellg();
std::unique_ptr<uint8_t[]> pModel(new uint8_t[modelSize]);
@@ -108,18 +116,16 @@ extern "C" int main(int argc, FAR char* argv[])
ifs.read(reinterpret_cast<char*>(pModel.get()), modelSize);
ifs.close();
- /* HACK: can change operators here. */
-
tflite::MicroMutableOpResolver<9> resolver;
- resolver.AddConv2D(tflite::Register_CONV_2D_INT8());
- resolver.AddDepthwiseConv2D(tflite::Register_DEPTHWISE_CONV_2D_INT8());
- resolver.AddMaxPool2D(tflite::Register_MAX_POOL_2D_INT8());
- resolver.AddQuantize(tflite::Register_QUANTIZE_FLOAT32_INT8());
- resolver.AddDequantize(tflite::Register_DEQUANTIZE_INT8());
- resolver.AddMean(tflite::Register_MEAN_INT8());
+ resolver.AddConv2D();
+ resolver.AddDepthwiseConv2D();
+ resolver.AddMaxPool2D();
+ resolver.AddQuantize();
+ resolver.AddDequantize();
+ resolver.AddMean();
resolver.AddReshape();
- resolver.AddFullyConnected(tflite::Register_FULLY_CONNECTED_INT8());
- resolver.AddSoftmax(tflite::Register_SOFTMAX_INT8());
+ resolver.AddFullyConnected();
+ resolver.AddSoftmax();
std::unique_ptr<uint8_t[]> pArena(new uint8_t[arenaSize]);
@@ -128,11 +134,22 @@ extern "C" int main(int argc, FAR char* argv[])
resolver, pArena.get(), arenaSize, nullptr,
reinterpret_cast<tflite::MicroProfilerInterface*>(&profiler));
- /* HACK: can add testcases here. */
+ TfLiteStatus status = interpreter.AllocateTensors();
+ if (status != kTfLiteOk)
+ {
+ printf("AllocateTensors failed: %d\n", status);
+ return -1;
+ }
if (need_invoke)
{
- interpreter.Invoke();
+ status = interpreter.Invoke();
+ if (status != kTfLiteOk)
+ {
+ printf("Invoke failed: %d\n", status);
+ return -1;
+ }
+
profiler.LogCsv();
profiler.LogTicksPerTagCsv();
}