Skip to content

Commit

Permalink
Add meta-tensorflow-lite
Browse files Browse the repository at this point in the history
This commit adds TensorFlow Lite support to the iwg20m as
a new Yocto/OpenEmbedded meta-layer delivered within the
scope of meta-renesas-ai.

Signed-off-by: Jianming Qiao <[email protected]>
Signed-off-by: Fabrizio Castro <[email protected]>
Signed-off-by: Chris Paterson <[email protected]>
  • Loading branch information
Jianming Qiao authored and patersonc committed May 22, 2018
1 parent 2c68558 commit 0f7b57a
Show file tree
Hide file tree
Showing 11 changed files with 836 additions and 4 deletions.
15 changes: 11 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,22 +27,29 @@ the configuration must be inspected and further customized according to the
project requirements.


At this point in time the only AI tool supported is *TensorFlow* and the only
platform supported is *iwg20m*.
Supported AI tools:
- TensorFlow v1.2.0
- TensorFlow Lite v1.7.0


This project is licensed under the terms of the MIT license (please see file
*COPYING.MIT* in this directory for further details).
Supported Platforms:
- Renesas RZ/G1M iwg20m


---

**Note**
If working behind a proxy, make sure the environment of the shell you are
running bitbake from contains *HTTP\_PROXY* and *HTTPS\_PROXY* environment
variables, set according to your proxy configuration.

---


This project is licensed under the terms of the MIT license (please see file
*COPYING.MIT* in this directory for further details).


Send pull requests, patches, comments or questions to:
[[email protected]](mailto:[email protected]).

Expand Down
56 changes: 56 additions & 0 deletions meta-tensorflow-lite/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# meta-tensorflow-lite

TensorFlow Lite is TensorFlow’s lightweight solution for mobile and embedded
devices. It enables on-device machine learning inference with low latency and
a small binary size. TensorFlow Lite uses many techniques for achieving low
latency such as optimizing the kernels for mobile apps, pre-fused activations,
and quantized kernels that allow smaller and faster (fixed-point math) models.


The official website is:
**https://www.tensorflow.org/mobile/tflite/**


This Yocto/OpenEmbedded meta-layer provides TensorFlow Lite support for the
RZ/G1 family of System on Chips.


In order to add TensorFlow Lite support to your project, make sure
*tensorflow-lite* is listed as a dependency to your recipe/package.
Listing *tensorflow-lite-staticdev* and *tensorflow-dev* in *IMAGE\_INSTALL* variable could be
beneficial when you just want to populate an SDK for developing an application
based on TensorFlow Lie.


After the build is complete the static C++ TensorFlow Lite library
(*libtensorflow-lite.a*) will be generated.


The library can be verified with the TensorFlow Lite image classification sample
application named *label_image* which is included in the build (included by
package *tensorflow-lite-examples*). The sample application is installed under
*/usr/bin/tensorflow-lite/examples/*.


To use *label_image*:
1. Download model data:
`wget https://storage.googleapis.com/download.tensorflow.org/models/tflite/mobilenet_v1_224_android_quant_2017_11_08.zip`
2. Extract to */usr/bin/tensorflow-lite/examples/*.
3. Execute *label_image* by running the following commands:
```
cd /usr/bin/tensorflow-lite/examples/
./label_image
```


The output of a healthy execution should look like the following:
> Neon Support
> Loaded model ./mobilenet_quant_v1_224.tflite
> resolved reporter
> invoked
> average time: 317.484 ms
> 0.666667: 458 bow tie
> 0.290196: 653 military uniform
> 0.0117647: 835 suit
> 0.00784314: 611 jersey
> 0.00392157: 922 book jacket
13 changes: 13 additions & 0 deletions meta-tensorflow-lite/conf/layer.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#We have a conf and classes directory, append to BBPATH
BBPATH .= ":${LAYERDIR}"

# We have a recipes directory, add to BBFILES
BBFILES += " \
${LAYERDIR}/recipes-*/*/*.bb \
${LAYERDIR}/recipes-*/*/*.bbappend \
"

BBFILE_COLLECTIONS += "tensorflow-lite-layer"
BBFILE_PATTERN_tensorflow-lite-layer := "^${LAYERDIR}/"
BBFILE_PRIORITY_tensorflow-lite-layer = "7"

Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
From f8f782093a41da2fab1c770e4bd5ce0ffa5aea55 Mon Sep 17 00:00:00 2001
From: Jianming Qiao <[email protected]>
Date: Mon, 21 May 2018 13:49:43 +0100
Subject: [PATCH 1/5] Get TensorFlow lite to cross compile

This patch adds cross compiler support to TensorFlow lite.

The content of file tensorflow/contrib/lite/Makefile.internal has
been inspired from the content of file:
tensorflow/contrib/lite/Makefile

from repository:
https://github.com/tensorflow/tensorflow.git

tag:
v1.7.0 (024aecf414941e11eb643e29ceed3e1c47a115ad)

Signed-off-by: Jianming Qiao <[email protected]>
---
Makefile | 1 +
tensorflow/contrib/lite/Makefile.internal | 112 ++++++++++++++++++++++++++++++
2 files changed, 113 insertions(+)
create mode 100644 Makefile
create mode 100644 tensorflow/contrib/lite/Makefile.internal

diff --git a/Makefile b/Makefile
new file mode 100644
index 0000000..eb10ead
--- /dev/null
+++ b/Makefile
@@ -0,0 +1 @@
+include ./tensorflow/contrib/lite/Makefile.internal
diff --git a/tensorflow/contrib/lite/Makefile.internal b/tensorflow/contrib/lite/Makefile.internal
new file mode 100644
index 0000000..cd70948
--- /dev/null
+++ b/tensorflow/contrib/lite/Makefile.internal
@@ -0,0 +1,112 @@
+# Find where we're running from, so we can store generated files here.
+ifeq ($(origin MAKEFILE_DIR), undefined)
+ MAKEFILE_DIR := $(shell dirname $(realpath $(lastword $(MAKEFILE_LIST))))
+endif
+
+# Where compiled objects are stored.
+OBJDIR := $(MAKEFILE_DIR)/gen/obj/
+BINDIR := $(MAKEFILE_DIR)/gen/bin/
+LIBDIR := $(MAKEFILE_DIR)/gen/lib/
+GENDIR := $(MAKEFILE_DIR)/gen/obj/
+
+# Settings for the host compiler.
+CXX ?= $(TOOLCHAIN_PATH)/bin/$(CC_PREFIX)-gcc
+CXXFLAGS += --std=c++11 -O3 -DNDEBUG
+CC ?= $(TOOLCHAIN_PATH)/bin/$(CC_PREFIX)-gcc
+CFLAGS ?= -O3 -DNDEBUG
+ARFLAGS ?= -r
+AR ?= $(TOOLCHAIN_PATH)/bin/$(CC_PREFIX)-ar
+
+INCLUDES := \
+-I. \
+-I$(MAKEFILE_DIR)/../../../ \
+-I$(MAKEFILE_DIR)/downloads/ \
+-I$(MAKEFILE_DIR)/downloads/eigen \
+-I$(MAKEFILE_DIR)/downloads/gemmlowp \
+-I$(MAKEFILE_DIR)/downloads/neon_2_sse \
+-I$(MAKEFILE_DIR)/downloads/farmhash/src \
+-I$(MAKEFILE_DIR)/downloads/flatbuffers/include \
+-I$(GENDIR)
+
+LIBS := \
+-lstdc++ \
+-lpthread \
+-lm
+
+LIBS += -ldl
+
+# This library is the main target for this makefile. It will contain a minimal
+# runtime that can be linked in to other programs.
+LIB_NAME := libtensorflow-lite.a
+LIB_PATH := $(LIBDIR)$(LIB_NAME)
+
+# What sources we want to compile, must be kept in sync with the main Bazel
+# build files.
+
+CORE_CC_ALL_SRCS := \
+$(wildcard tensorflow/contrib/lite/*.cc) \
+$(wildcard tensorflow/contrib/lite/kernels/*.cc) \
+$(wildcard tensorflow/contrib/lite/kernels/internal/*.cc) \
+$(wildcard tensorflow/contrib/lite/kernels/internal/optimized/*.cc) \
+$(wildcard tensorflow/contrib/lite/kernels/internal/reference/*.cc) \
+$(wildcard tensorflow/contrib/lite/*.c) \
+$(wildcard tensorflow/contrib/lite/kernels/*.c) \
+$(wildcard tensorflow/contrib/lite/kernels/internal/*.c) \
+$(wildcard tensorflow/contrib/lite/kernels/internal/optimized/*.c) \
+$(wildcard tensorflow/contrib/lite/kernels/internal/reference/*.c) \
+$(wildcard tensorflow/contrib/lite/downloads/farmhash/src/farmhash.cc)
+# Remove any duplicates.
+CORE_CC_ALL_SRCS := $(sort $(CORE_CC_ALL_SRCS))
+CORE_CC_EXCLUDE_SRCS := \
+$(wildcard tensorflow/contrib/lite/*test.cc) \
+$(wildcard tensorflow/contrib/lite/*/*test.cc) \
+$(wildcard tensorflow/contrib/lite/*/*/*test.cc) \
+$(wildcard tensorflow/contrib/lite/*/*/*/*test.cc) \
+$(wildcard tensorflow/contrib/lite/kernels/test_util.cc)
+
+$(info $$CORE_CC_ALL_SRCS is [${CORE_CC_ALL_SRCS}])
+$(info $$CORE_CC_EXCLUDE_SRCS is [${CORE_CC_EXCLUDE_SRCS}])
+
+# Filter out all the excluded files.
+TF_LITE_CC_SRCS := $(filter-out $(CORE_CC_EXCLUDE_SRCS), $(CORE_CC_ALL_SRCS))
+# File names of the intermediate files target compilation generates.
+TF_LITE_CC_OBJS := $(addprefix $(OBJDIR), \
+$(patsubst %.cc,%.o,$(patsubst %.c,%.o,$(TF_LITE_CC_SRCS))))
+LIB_OBJS := $(TF_LITE_CC_OBJS)
+
+$(info $$TF_LITE_CC_SRCS is [${TF_LITE_CC_SRCS}])
+$(info $$TF_LITE_CC_OBJS is [${TF_LITE_CC_OBJS}])
+
+# For normal manually-created TensorFlow C++ source files.
+$(OBJDIR)%.o: %.cc
+ @mkdir -p $(dir $@)
+ $(CXX) $(CXXFLAGS) $(INCLUDES) -c $< -o $@
+
+# For normal manually-created TensorFlow C++ source files.
+$(OBJDIR)%.o: %.c
+ @mkdir -p $(dir $@)
+ $(CC) $(CCFLAGS) $(INCLUDES) -c $< -o $@
+
+# The target that's compiled if there's no command-line arguments.
+all: $(LIB_PATH)
+
+# Gathers together all the objects we've compiled into a single '.a' archive.
+$(LIB_PATH): $(LIB_OBJS)
+ @mkdir -p $(dir $@)
+ $(AR) $(ARFLAGS) $(LIB_PATH) $(LIB_OBJS)
+
+# Gets rid of all generated files.
+clean:
+ rm -rf $(MAKEFILE_DIR)/gen
+
+# Gets rid of target files only, leaving the host alone. Also leaves the lib
+# directory untouched deliberately, so we can persist multiple architectures
+# across builds for iOS and Android.
+cleantarget:
+ rm -rf $(OBJDIR)
+ rm -rf $(BINDIR)
+
+$(DEPDIR)/%.d: ;
+.PRECIOUS: $(DEPDIR)/%.d
+
+-include $(patsubst %,$(DEPDIR)/%.d,$(basename $(TF_CC_SRCS)))
--
2.7.4

Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
From 744b6e7864c2c4346bd44b1311921adab556c830 Mon Sep 17 00:00:00 2001
From: Jianming Qiao <[email protected]>
Date: Mon, 21 May 2018 13:52:15 +0100
Subject: [PATCH 2/5] Compile label_image

This patch adds label_image example to list of objects to be
compiled.

Signed-off-by: Jianming Qiao <[email protected]>
---
tensorflow/contrib/lite/Makefile.internal | 20 ++++++++++++++++++--
1 file changed, 18 insertions(+), 2 deletions(-)

diff --git a/tensorflow/contrib/lite/Makefile.internal b/tensorflow/contrib/lite/Makefile.internal
index cd70948..24afa75 100644
--- a/tensorflow/contrib/lite/Makefile.internal
+++ b/tensorflow/contrib/lite/Makefile.internal
@@ -40,6 +40,15 @@ LIBS += -ldl
LIB_NAME := libtensorflow-lite.a
LIB_PATH := $(LIBDIR)$(LIB_NAME)

+# A small example program that shows how to link against the library.
+LABELIMAGE_PATH := $(BINDIR)label_image
+
+LABELIMAGE_SRCS := \
+tensorflow/contrib/lite/examples/label_image/bitmap_helpers.cc \
+tensorflow/contrib/lite/examples/label_image/label_image.cc
+LABELIMAGE_OBJS := $(addprefix $(OBJDIR), \
+$(patsubst %.cc,%.o,$(patsubst %.c,%.o,$(LABELIMAGE_SRCS))))
+
# What sources we want to compile, must be kept in sync with the main Bazel
# build files.

@@ -62,7 +71,8 @@ $(wildcard tensorflow/contrib/lite/*test.cc) \
$(wildcard tensorflow/contrib/lite/*/*test.cc) \
$(wildcard tensorflow/contrib/lite/*/*/*test.cc) \
$(wildcard tensorflow/contrib/lite/*/*/*/*test.cc) \
-$(wildcard tensorflow/contrib/lite/kernels/test_util.cc)
+$(wildcard tensorflow/contrib/lite/kernels/test_util.cc) \
+$(LABELIMAGE_SRCS)

$(info $$CORE_CC_ALL_SRCS is [${CORE_CC_ALL_SRCS}])
$(info $$CORE_CC_EXCLUDE_SRCS is [${CORE_CC_EXCLUDE_SRCS}])
@@ -88,13 +98,19 @@ $(OBJDIR)%.o: %.c
$(CC) $(CCFLAGS) $(INCLUDES) -c $< -o $@

# The target that's compiled if there's no command-line arguments.
-all: $(LIB_PATH)
+all: $(LIB_PATH) $(LABELIMAGE_PATH)

# Gathers together all the objects we've compiled into a single '.a' archive.
$(LIB_PATH): $(LIB_OBJS)
@mkdir -p $(dir $@)
$(AR) $(ARFLAGS) $(LIB_PATH) $(LIB_OBJS)

+$(LABELIMAGE_PATH): $(LABELIMAGE_OBJS) $(LIB_PATH)
+ @mkdir -p $(dir $@)
+ $(CXX) $(CXXFLAGS) $(INCLUDES) \
+ -o $(LABELIMAGE_PATH) $(LABELIMAGE_OBJS) \
+ $(LIBFLAGS) $(LIB_PATH) $(LDFLAGS) $(LIBS)
+
# Gets rid of all generated files.
clean:
rm -rf $(MAKEFILE_DIR)/gen
--
2.7.4

Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
From ca5efe0e03ac39a4ba76152ae389b18f611c465b Mon Sep 17 00:00:00 2001
From: Jianming Qiao <[email protected]>
Date: Wed, 16 May 2018 16:22:53 +0100
Subject: [PATCH 3/5] Fix compile time error

Include assert.h to fix compile time error.

Signed-off-by: Jianming Qiao <[email protected]>
---
tensorflow/contrib/lite/kernels/internal/spectrogram.cc | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/tensorflow/contrib/lite/kernels/internal/spectrogram.cc b/tensorflow/contrib/lite/kernels/internal/spectrogram.cc
index 66ca694..5700d31 100644
--- a/tensorflow/contrib/lite/kernels/internal/spectrogram.cc
+++ b/tensorflow/contrib/lite/kernels/internal/spectrogram.cc
@@ -16,7 +16,7 @@ limitations under the License.
#include "tensorflow/contrib/lite/kernels/internal/spectrogram.h"

#include <math.h>
-
+#include <assert.h>
#include "third_party/fft2d/fft.h"

namespace tflite {
--
2.7.4

Loading

0 comments on commit 0f7b57a

Please sign in to comment.