Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions mlearning/darknet/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion mlearning/tflite-micro/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
44 changes: 39 additions & 5 deletions mlearning/tflite-micro/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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),)
Expand All @@ -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

Expand Down Expand Up @@ -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}
Expand Down
43 changes: 30 additions & 13 deletions mlearning/tflite-micro/tflm_tool.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
* Included Files
****************************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

#include <cstdint>
Expand All @@ -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");
Expand Down Expand Up @@ -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]);
Expand All @@ -108,17 +116,15 @@ 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<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<uint8_t[]> pArena(new uint8_t[arenaSize]);

Expand All @@ -127,11 +133,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();
}
Expand Down
Loading