diff --git a/mlearning/darknet/Makefile b/mlearning/darknet/Makefile index 627245fbd1e..94e80ba9787 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 a2756e7c921..919dcd14660 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 aba043805e7..179d0e9c32b 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 8edcf45a49b..aa3a5934ab3 100644 --- a/mlearning/tflite-micro/tflm_tool.cc +++ b/mlearning/tflite-micro/tflm_tool.cc @@ -24,6 +24,8 @@ * Included Files ****************************************************************************/ +#include +#include #include #include @@ -44,7 +46,7 @@ static void usage(void) "[ -C ] Compile tflite model into c++ codes.\n" "[ -E ] Do once evaluation (for profiling).\n" "[ -i ] Readable model file path.\n" - "[ -o ] Writable c++ file path.\n" + "[ -o ] Writable c++ file path (required with -C).\n" "[ -p ] Prefix of compiled code.\n" "[ -a ] 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 pModel(new uint8_t[modelSize]); @@ -108,17 +116,15 @@ extern "C" int main(int argc, FAR char* argv[]) ifs.read(reinterpret_cast(pModel.get()), modelSize); ifs.close(); - /* HACK: can change operators here. */ - tflite::MicroMutableOpResolver<8> resolver; - resolver.AddConv2D(tflite::Register_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.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 pArena(new uint8_t[arenaSize]); @@ -127,11 +133,22 @@ extern "C" int main(int argc, FAR char* argv[]) resolver, pArena.get(), arenaSize, nullptr, reinterpret_cast(&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(); }