mirror of
https://github.com/apache/nuttx-apps.git
synced 2026-09-10 18:56:33 +00:00
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 <mishra.abhishek2808@gmail.com>
This commit is contained in:
parent
46b8774f7f
commit
a81fe2f4d9
4 changed files with 74 additions and 20 deletions
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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()
|
||||
|
||||
|
|
|
|||
|
|
@ -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}
|
||||
|
|
|
|||
|
|
@ -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();
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue