Tensorflowlite移植ARM平台iMX6

一、LINUX环境下操作:

    1.安装交叉编译SDK (仅针对该型号:i.MX6,不同芯片需要对应的交叉编译SDK)

            编译方法参考:手动编译用于i.MX6系列的交叉编译SDK

     2.下载Tensorflow

       git clone https://github.com/tensorflow/tensorflow.git

       cd tensorflow

       git checkout r1.10

      Tensorflow与Bazel编译器(及CUDA,CUDNN)之间需要对应,否则会有兼容性问题。

          tensorflowr1.10     python 2.7,3.6   Bazel:0.18.0-0.19.2

          tensorflowr1.12     python 2.7,3.6   Bazel:0.18.0-0.19.2

          tensorflowr1.14     python 2.7,3.6   Bazel:0.24.0 - 0.25.2

      3、下载并安装编译工具Bazel

          安装依赖包:

                sudo apt-get install pkg-config zip g++ zilb1g-dev unzip

          下载Bazel包:

                wget https://github.com/bazelbuild/bazel/releases/download/0.18.1/bazel-0.181-installer-linux-86_64.sh

          安装Bazel:

                 chmod +x bazel-0.18.1-installer-linux-86_64.sh

                 ./bazel-0.18.1-installer-linux-86_64.sh --user

          设置环境变量:

                 sudo vi ~/.bashrc,在文件最后添加:export PATH=$PATH":~/bin"

                 source ~/.bashrc

    (如果仅仅是测试DEMO在ARM板上使用,可直接跳过4,5,6,7,8步,直接进行第9步)

    4、编译配置:

          在Tensorflow源码根目录运行:

             ./configure     (编译LINUX平台时使用默认设置:-march=native,编译ARM平台时需设置成相应值:-march=armv7-a)

     5、编译pip:

          bazel build --config=opt //tensorflow/tools/pip_package:build_pip_package

     7、编译包:

          ./bazel-bin/tensorflow/tools/pip__package/build_pip_package /tmp/tensorflow_pkg

     8、安装包:

          pip install /tmp/tensorflow_pkg/tensorflow-version-tags.whl

     9、下载依赖库:

         ./tensorflow/contrib/lite/tools/make/download_dependencies.sh(不同版本,位置略有不同,本文路径为r1.10版本)

     10、编译Tensorflow Lite:

         方法1:(参考网上的,但我调试了几天,并未生成该库,可行性待验证!)

                目前Tensorflow仅支持树莓派: ./tensorflow/contrib/lite/tools/make/build_rpi_lib.sh,该脚本的目标编译平台是ARMv7,即使目标平台是ARMv8,也不要更改。因为设置编译平台为ARMv7可以优化编译,提高运行速度。

                所以,需要针对iMX6,制作一份build_imx6_lib.sh

1 set -e 
2 
3 SCRIPT_DIR="$(cd "$(dirname "$(BASH_SOURCE[0]}")" && pwd)"
4 cd "$SCRIPT_DIR/../../.."
5 
6 CC_PREFIX=arm-poky-linux-gnueabi- make -j 3 -f tensorflow/contrib/lite/Makefile TARGET-imx6 TARGET_ARCH=armv7-a
View Code

                生成静态库位置为:

               ./tensorflow/contrib/lite/toos/make/gen/rpi_arm7l/lib/libtensorflow-lite.a静态库。

         方法2:(使用ARM(iMX6)交叉编译包,编译出来的包可以用于iMX6平台)

                1.打开Tensorflow/contrib/lite/kernels/internal/BUILD

 1 config_setting(
 2        name = "armv7a",
 3        values = {"cpu":"armv7a",},
 4 )
 5 
 6 "armv7a":[
 7     "-O3",
 8     "-mfpu=vfpv3",
 9     "-mfloat-abi=hard",
10 ],
11 
12 add code above to NEON_FLAGS_IF_APPLICABLE
13 
14 
15 ":armv6":[
16 
17     ":neon_tensor_utils",
18     ],
19 ":armv7a":[
20     ":neon_tensor_utils",
21 ],
22 
23 add code above to cc_library  tensor_utils select options
View Code

                2.在根目录下创建一个文件:build_armv7_tflite.sh

 1 #!/bin/bash
 2 
 3 bazel build --copt="-fPIC" --copt="-march=armv6" \
 4      --copt="-mfloat-abi=hard" \
 5      --copt="-mfpu=vfpv3" \
 6     --copt="-funsafe-math-optimizations" \
 7      --copt="-Wno-unused-function" \
 8      --copt="-Wno-sign-compare" \
 9      --copt="-ftree-vectorize" \
10      --copt="-fomit-frame-pointer" \
11      --cxxopt='--std=c++11' \
12      --cxxopt="-Wno-maybe-uninitialized" \
13      --cxxopt="-Wno-narrowing" \
14      --cxxopt="-Wno-unused" \
15      --cxxopt="-Wno-comment" \
16      --cxxopt="-Wno-unused-function" \
17      --cxxopt="-Wno-sign-compare" \
18      --cxxopt="-funsafe-math-optimizations" \
19      --linkopt="-lstdc++" \
20      --linkopt="-mfloat-abi=hard" \
21      --linkopt="-mfpu=vfpv3" \
22      --linkopt="-funsafe-math-optimizations" \
23      --verbose_failures \
24      --strip=always \
25      --crosstool_top=//armv6-compiler:toolchain --cpu=armv7 --config=opt \
26 33     tensorflow/contrib/lite/examples/label_image
View Code

                3.编译该文件build_armv7_tflite.sh,会碰到一个错误:.../.../read-ld:unrecognized options : --icf=all

                   解决方法:找到文件./build_def.bzl ,打开,去除所有--icf=all标识的信息即可。

                   编译后,会在bazel_bin下生成指定的文件label_image。

                4.生成头和库文件:      

                    libbuiltin_ops.a libframework.a libneon_tensor_utils.a libquantization_util.a libtensor_utils.a

                    libcontext.a libfarmhash.a libgemm_support.a libportable_tensor_utils.a  libstring_util.a

     11、编译模型:

          默认情况下label_image并未编译进去,需要修改Makefile,可参考minimal APK,主要修改以下三部分内容:

            LABELIMAGE_SRCS

            LABELIMAGE_BINARY

            LABEL_OBJS

  1 # Find where we're running from, so we can store generated files here.
  2 ifeq ($(origin MAKEFILE_DIR), undefined)
  3     MAKEFILE_DIR := $(shell dirname $(realpath $(lastword $(MAKEFILE_LIST))))
  4 endif
  5 
  6 # Try to figure out the host system
  7 HOST_OS :=
  8 ifeq ($(OS),Windows_NT)
  9     HOST_OS = WINDOWS
 10 else
 11     UNAME_S := $(shell uname -s)
 12     ifeq ($(UNAME_S),Linux)
 13             HOST_OS := LINUX
 14     endif
 15     ifeq ($(UNAME_S),Darwin)
 16         HOST_OS := OSX
 17     endif
 18 endif
 19 
 20 #HOST_ARCH := $(shell if [[ $(shell uname -m) =~ i[345678]86 ]]; then echo x86_32; else echo $(shell uname -m); fi)
 21 
 22 # Self-hosting
 23 TARGET_ARCH := ${HOST_ARCH}
 24 CROSS := imx6
 25 $(warning "CROSS :$(CROSS) HOST_ARCH:$(HOST_ARCH) TARGET_ARCH:$(TARGET_ARCH) TARGET_TOOLCHAIN_PREFIX:$(TARGET_TOOLCHAIN_PREFIX)")
 26 # Cross compiling
 27 ifeq ($(CROSS),imx6)
 28   TARGET_ARCH := armv7-a
 29   TARGET_TOOLCHAIN_PREFIX := arm-poky-linux-gnueabi-
 30 endif
 31 
 32 ifeq ($(CROSS),rpi)
 33   TARGET_ARCH := armv7l
 34   TARGET_TOOLCHAIN_PREFIX := arm-linux-gnueabihf-
 35 endif
 36 
 37 ifeq ($(CROSS),riscv)
 38   TARGET_ARCH := riscv
 39   TARGET_TOOLCHAIN_PREFIX := riscv32-unknown-elf-
 40 endif
 41 ifeq ($(CROSS),stm32f7)
 42   TARGET_ARCH := armf7
 43   TARGET_TOOLCHAIN_PREFIX := arm-none-eabi-
 44 endif
 45 ifeq ($(CROSS),stm32f1)
 46   TARGET_ARCH := armm1
 47   TARGET_TOOLCHAIN_PREFIX := arm-none-eabi-
 48 endif
 49 
 50 # Where compiled objects are stored.
 51 OBJDIR := $(MAKEFILE_DIR)/gen/obj/
 52 BINDIR := $(MAKEFILE_DIR)/gen/bin/
 53 LIBDIR := $(MAKEFILE_DIR)/gen/lib/
 54 GENDIR := $(MAKEFILE_DIR)/gen/obj/
 55 
 56 LIBS :=
 57 ifeq ($(TARGET_ARCH),x86_64)
 58         CXXFLAGS += -fPIC -DGEMMLOWP_ALLOW_SLOW_SCALAR_FALLBACK -pthread # -msse4.2
 59 endif
 60 
 61 
 62 ifeq ($(TARGET_ARCH),armv7-a)
 63         CXXFLAGS += -pthread -fPIC
 64     LIBS += -ldl
 65 endif
 66 
 67 ifeq ($(TARGET_ARCH),armv7l)
 68         CXXFLAGS += -mfpu=neon -pthread -fPIC
 69     LIBS += -ldl
 70 endif
 71 
 72 ifeq ($(TARGET_ARCH),riscv)
 73 #        CXXFLAGS += -march=gap8
 74         CXXFLAGS += -DTFLITE_MCU
 75     LIBS += -ldl
 76     BUILD_TYPE := micro
 77 endif
 78 
 79 ifeq ($(TARGET_ARCH),armf7)
 80         CXXFLAGS += -DGEMMLOWP_ALLOW_SLOW_SCALAR_FALLBACK -DTFLITE_MCU
 81         CXXFLAGS += -fno-rtti -fmessage-length=0 -fno-exceptions -fno-builtin -ffunction-sections -fdata-sections
 82         CXXFLAGS += -funsigned-char -MMD
 83         CXXFLAGS += -mcpu=cortex-m7 -mthumb -mfpu=fpv5-sp-d16 -mfloat-abi=softfp
 84         CXXFLAGS += '-std=gnu++11' '-fno-rtti' '-Wvla' '-c' '-Wall' '-Wextra' '-Wno-unused-parameter' '-Wno-missing-field-initializers' '-fmessage-length=0' '-fno-exceptions' '-fno-builtin' '-ffunction-sections' '-fdata-sections' '-funsigned-char' '-MMD' '-fno-delete-null-pointer-checks' '-fomit-frame-pointer' '-Os'
 85     LIBS += -ldl
 86     BUILD_TYPE := micro
 87 endif
 88 ifeq ($(TARGET_ARCH),armm1)
 89         CXXFLAGS += -DGEMMLOWP_ALLOW_SLOW_SCALAR_FALLBACK -mcpu=cortex-m1 -mthumb -DTFLITE_MCU
 90         CXXFLAGS += -fno-rtti -fmessage-length=0 -fno-exceptions -fno-builtin -ffunction-sections -fdata-sections
 91         CXXFLAGS += -funsigned-char -MMD
 92     LIBS += -ldl
 93 endif
 94 
 95 # Settings for the host compiler.
 96 #CXX := $(CC_PREFIX) ${TARGET_TOOLCHAIN_PREFIX}g++
 97 #CXX := ${TARGET_TOOLCHAIN_PREFIX}g++ -march=armv7-a -mfpu=neon  -mfloat-abi=hard -mcpu=cortex-a9 --sysroot=/opt/fsl-imx-x11/4.1.15-2.1.0/sysroots/x86_64-pokysdk-linux/usr/bin/arm-poky-linux-gnueabi 
 98 CXXFLAGS += -O3 -DNDEBUG
 99 CCFLAGS := ${CXXFLAGS}
100 #CXXFLAGS += --std=c++11
101 CXXFLAGS += --std=c++0x     #wly
102 #CC := ${TARGET_TOOLCHAIN_PREFIX}gcc
103 #AR := ${TARGET_TOOLCHAIN_PREFIX}ar
104 CFLAGS :=
105 LDOPTS :=
106 LDOPTS += -L/usr/local/lib
107 ARFLAGS := -r
108 
109 INCLUDES := \
110 -I. \
111 -I$(MAKEFILE_DIR)/../../../ \
112 -I$(MAKEFILE_DIR)/downloads/ \
113 -I$(MAKEFILE_DIR)/downloads/eigen \
114 -I$(MAKEFILE_DIR)/downloads/gemmlowp \
115 -I$(MAKEFILE_DIR)/downloads/neon_2_sse \
116 -I$(MAKEFILE_DIR)/downloads/farmhash/src \
117 -I$(MAKEFILE_DIR)/downloads/flatbuffers/include \
118 -I$(GENDIR)
119 # This is at the end so any globally-installed frameworks like protobuf don't
120 # override local versions in the source tree.
121 #INCLUDES += -I/usr/local/include
122 INCLUDES += -I/usr/include
123 LIBS += \
124 -lstdc++ \
125 -lpthread \
126 -lm \
127 -lz
128 
129 # If we're on Linux, also link in the dl library.
130 ifeq ($(HOST_OS),LINUX)
131     LIBS += -ldl
132 endif
133 
134 include $(MAKEFILE_DIR)/ios_makefile.inc
135 ifeq ($(CROSS),rpi)
136 #include $(MAKEFILE_DIR)/rpi_makefile.inc
137 endif
138 ifeq ($(CROSS),imx6)
139 include $(MAKEFILE_DIR)/imx6_makefile.inc
140 endif
141 # This library is the main target for this makefile. It will contain a minimal
142 # runtime that can be linked in to other programs.
143 LIB_NAME := libtensorflow-lite.a
144 LIB_PATH := $(LIBDIR)$(LIB_NAME)
145 
146 # A small example program that shows how to link against the library.
147 MINIMAL_PATH := $(BINDIR)minimal
148 LABEL_IMAGE_PATH :=$(BINDIR)label_image
149 
150 MINIMAL_SRCS := \
151 tensorflow/contrib/lite/examples/minimal/minimal.cc
152 MINIMAL_OBJS := $(addprefix $(OBJDIR), \
153 $(patsubst %.cc,%.o,$(patsubst %.c,%.o,$(MINIMAL_SRCS))))
154 
155 
156 LABEL_IMAGE_SRCS := \
157 tensorflow/contrib/lite/examples/label_image/label_image.cc \
158 tensorflow/contrib/lite/examples/label_image/bitmap_helpers.cc 
159 LABEL_IMAGE_OBJS := $(addprefix $(OBJDIR), \
160 $(patsubst %.cc,%.o,$(patsubst %.c,%.o,$(LABEL_IMAGE_SRCS))))
161 
162 # What sources we want to compile, must be kept in sync with the main Bazel
163 # build files.
164 
165 PROFILER_SRCS := \
166     tensorflow/contrib/lite/profiling/time.cc
167 PROFILE_SUMMARIZER_SRCS := \
168     tensorflow/contrib/lite/profiling/profile_summarizer.cc \
169     tensorflow/core/util/stats_calculator.cc
170 
171 CORE_CC_ALL_SRCS := \
172 $(wildcard tensorflow/contrib/lite/*.cc) \
173 $(wildcard tensorflow/contrib/lite/*.c)
174 ifneq ($(BUILD_TYPE),micro)
175 CORE_CC_ALL_SRCS += \
176 $(wildcard tensorflow/contrib/lite/kernels/*.cc) \
177 $(wildcard tensorflow/contrib/lite/kernels/internal/*.cc) \
178 $(wildcard tensorflow/contrib/lite/kernels/internal/optimized/*.cc) \
179 $(wildcard tensorflow/contrib/lite/kernels/internal/reference/*.cc) \
180 $(PROFILER_SRCS) \
181 $(wildcard tensorflow/contrib/lite/kernels/*.c) \
182 $(wildcard tensorflow/contrib/lite/kernels/internal/*.c) \
183 $(wildcard tensorflow/contrib/lite/kernels/internal/optimized/*.c) \
184 $(wildcard tensorflow/contrib/lite/kernels/internal/reference/*.c) \
185 $(wildcard tensorflow/contrib/lite/downloads/farmhash/src/farmhash.cc) \
186 $(wildcard tensorflow/contrib/lite/downloads/fft2d/fftsg.c)
187 endif
188 # Remove any duplicates.
189 CORE_CC_ALL_SRCS := $(sort $(CORE_CC_ALL_SRCS))
190 CORE_CC_EXCLUDE_SRCS := \
191 $(wildcard tensorflow/contrib/lite/*test.cc) \
192 $(wildcard tensorflow/contrib/lite/*/*test.cc) \
193 $(wildcard tensorflow/contrib/lite/*/*/*test.cc) \
194 $(wildcard tensorflow/contrib/lite/*/*/*/*test.cc) \
195 $(wildcard tensorflow/contrib/lite/kernels/test_util.cc) \
196 $(MINIMAL_SRCS) \
197 $(LABEL_IMAGE_SRCS)
198 ifeq ($(BUILD_TYPE),micro)
199 CORE_CC_EXCLUDE_SRCS += \
200 tensorflow/contrib/lite/model.cc \
201 tensorflow/contrib/lite/nnapi_delegate.cc
202 endif
203 # Filter out all the excluded files.
204 TF_LITE_CC_SRCS := $(filter-out $(CORE_CC_EXCLUDE_SRCS), $(CORE_CC_ALL_SRCS))
205 # File names of the intermediate files target compilation generates.
206 TF_LITE_CC_OBJS := $(addprefix $(OBJDIR), \
207 $(patsubst %.cc,%.o,$(patsubst %.c,%.o,$(TF_LITE_CC_SRCS))))
208 LIB_OBJS := $(TF_LITE_CC_OBJS)
209 
210 # Benchmark sources
211 BENCHMARK_SRCS_DIR := tensorflow/contrib/lite/tools/benchmark
212 BENCHMARK_ALL_SRCS := $(TFLITE_CC_SRCS) \
213     $(wildcard $(BENCHMARK_SRCS_DIR)/*.cc) \
214     $(PROFILE_SUMMARIZER_SRCS)
215 
216 BENCHMARK_SRCS := $(filter-out \
217     $(wildcard $(BENCHMARK_SRCS_DIR)/*_test.cc), \
218     $(BENCHMARK_ALL_SRCS))
219 
220 BENCHMARK_OBJS := $(addprefix $(OBJDIR), \
221 $(patsubst %.cc,%.o,$(patsubst %.c,%.o,$(BENCHMARK_SRCS))))
222 
223 # For normal manually-created TensorFlow C++ source files.
224 $(OBJDIR)%.o: %.cc
225     @mkdir -p $(dir $@)
226     $(CXX) $(CXXFLAGS) $(INCLUDES) -c $< -o $@
227 # For normal manually-created TensorFlow C++ source files.
228 $(OBJDIR)%.o: %.c
229     @mkdir -p $(dir $@)
230     $(CC) $(CCFLAGS) $(INCLUDES) -c $< -o $@
231 
232 # The target that's compiled if there's no command-line arguments.
233 all: $(LIB_PATH)  $(MINIMAL_PATH) $(LABEL_IMAGE_PATH) $(BENCHMARK_BINARY) 
234 
235 # The target that's compiled for micro-controllers
236 micro: $(LIB_PATH)
237 
238 # Gathers together all the objects we've compiled into a single '.a' archive.
239 $(LIB_PATH): $(LIB_OBJS)
240     @mkdir -p $(dir $@)
241     $(AR) $(ARFLAGS) $(LIB_PATH) $(LIB_OBJS)
242 
243 $(MINIMAL_PATH): $(MINIMAL_OBJS) $(LIB_PATH)
244     @mkdir -p $(dir $@)
245     $(CXX) $(CXXFLAGS) $(INCLUDES) \
246     -o $(MINIMAL_PATH) $(MINIMAL_OBJS) \
247     $(LIBFLAGS) $(LIB_PATH) $(LDFLAGS) $(LIBS)
248 
249 # $(LABEL_IMAGE_PATH): $(LABEL_IMAGE_OBJS) $(LIBS)
250 
251 $(LABEL_IMAGE_PATH) : $(LABEL_IMAGE_OBJS) $(LIB_PATH)
252     @mkdir -p $(dir $@)
253     $(CXX) $(CXXFLAGS) $(INCLUDES) \
254     -o $(LABEL_IMAGE_PATH) $(LABEL_IMAGE_OBJS)\
255     $(LIBFLAGS) $(LIB_PATH) $(LDFLAGS) $(LIBS)
256 
257 
258 $(BENCHMARK_LIB) : $(LIB_PATH) $(BENCHMARK_OBJS)
259     @mkdir -p $(dir $@)
260     $(AR) $(ARFLAGS) $(BENCHMARK_LIB) $(LIB_OBJS) $(BENCHMARK_OBJS)
261 
262 benchmark_lib: $(BENCHMARK_LIB)
263 $(info $(BENCHMARK_BINARY))
264 $(BENCHMARK_BINARY) : $(BENCHMARK_LIB)
265     @mkdir -p $(dir $@)
266     $(CXX) $(CXXFLAGS) $(INCLUDES) \
267     -o $(BENCHMARK_BINARY) \
268     $(LIBFLAGS) $(BENCHMARK_LIB) $(LDFLAGS) $(LIBS)
269 
270 benchmark: $(BENCHMARK_BINARY)
271 
272 # Gets rid of all generated files.
273 clean:
274     rm -rf $(MAKEFILE_DIR)/gen
275 
276 # Gets rid of target files only, leaving the host alone. Also leaves the lib
277 # directory untouched deliberately, so we can persist multiple architectures
278 # across builds for iOS and Android.
279 cleantarget:
280     rm -rf $(OBJDIR)
281     rm -rf $(BINDIR)
282 
283 $(DEPDIR)/%.d: ;
284 .PRECIOUS: $(DEPDIR)/%.d
285 
286 -include $(patsubst %,$(DEPDIR)/%.d,$(basename $(TF_CC_SRCS)))
View Code

          执行: ./tensorflow/contrib/lite/tools/make/build_imx6_lib.sh

          编译过程中可能会出现一些问题,依次解决即可!

          编译完成后,在./tensorflow/contrib/lite/tools/make/gen/rpi_arm7l/bin  目录下生成可执行文件label_img

          附上自己碰到的一些问题:

             1、a/tensorflow/contrib/lite/tools/benchmark/benchmark_params.h

 1 diff --git a/tensorflow/contrib/lite/profiling/profile_summarizer.cc b/tensorflow/contrib/lite/profiling/profile_summarizer.cc
 2 old mode 100644
 3 new mode 100755
 4 index c37a096..590cd21
 5 --- a/tensorflow/contrib/lite/profiling/profile_summarizer.cc
 6 +++ b/tensorflow/contrib/lite/profiling/profile_summarizer.cc
 7 @@ -83,7 +83,8 @@ OperatorDetails GetOperatorDetails(const tflite::Interpreter& interpreter,
 8    OperatorDetails details;
 9    details.name = op_name;
10    if (profiling_string) {
11 -    details.name += ":" + string(profiling_string);
12 +    //wly
13 +    details.name += ":" + std::string(profiling_string);
14    }
15    details.inputs = GetTensorNames(interpreter, inputs);
16    details.outputs = GetTensorNames(interpreter, outputs);
View Code

             2、./tensorflow/contrib/lite/profiling/profile_summarizer.cc

 1 diff --git a/tensorflow/contrib/lite/tools/benchmark/benchmark_params.h b/tensorflow/contrib/lite/tools/benchmark/benchmark_params.h
 2 old mode 100644
 3 new mode 100755
 4 index 33448dd..e7f63ff
 5 --- a/tensorflow/contrib/lite/tools/benchmark/benchmark_params.h
 6 +++ b/tensorflow/contrib/lite/tools/benchmark/benchmark_params.h
 7 @@ -47,7 +47,8 @@ class BenchmarkParam {
 8    virtual ~BenchmarkParam() {}
 9    BenchmarkParam(ParamType type) : type_(type) {}
10  
11 - private:
12 + //private:
13 + public:        //wly
14    static void AssertHasSameType(ParamType a, ParamType b);
15    template <typename T>
16    static ParamType GetValueType();
View Code

             3、./tensorflow/contrib/lite/kernels/internal/reference/reference_ops.h

 1 diff --git a/tensorflow/contrib/lite/kernels/internal/reference/reference_ops.h b/tensorflow/contrib/lite/kernels/internal/reference/reference_ops.h
 2 old mode 100644
 3 new mode 100755
 4 index 2d40f17..43c54dc
 5 --- a/tensorflow/contrib/lite/kernels/internal/reference/reference_ops.h
 6 +++ b/tensorflow/contrib/lite/kernels/internal/reference/reference_ops.h
 7 @@ -24,8 +24,9 @@ limitations under the License.
 8  #include <type_traits>
 9  
10 - #include "third_party/eigen3/Eigen/Core"
11 +//#include "third_party/eigen3/Eigen/Core"" 
12  #include "tensorflow/contrib/lite/kernels/internal/common.h"
13  #include "tensorflow/contrib/lite/kernels/internal/quantization_util.h"
14  #include "tensorflow/contrib/lite/kernels/internal/round.h"
View Code

             4、需要下载包:protobuf

             5、将c++11  改为c++0x

     12、在PC上测试label_image

          ./label_image -v 1 -m ./mobilenet_v1_1.0_224.tflite -i ./grace_hopper.jpg -l ./imagenet_slim_labels.txt

          报错信息: bash ./label_image: cannot execute binary file:Exec format error

          原因有两个:   

              一是GCC编译时多加了一个-C,生成了二进制文件;

                     解决方法:找到GCC编译处,去除-C选项。

              二就是编译环境不同(平台芯片不一致)导致

                     解决方法:需要在对应平台编译。

                            在tensorflow根目录执行:bazel build tensorflow/examples/label_image:label_image

                            如果是第一次编译,时间较久;

                            编译完成后,生成可执行文件:bazel_bin/tensorflow/examples/label_image/label_image

              本地测试该文件:

                      拷贝label_image和libtensorflow_framework.so到tensorflow/examples/label_image下

                      (第一次测试时,未拷贝libtensorflow_framework.so,直接提示:error while loading shared libraryies:libtensorflow_framework.so:cannot open shared object file:No such file or directory)

                      再次运行./tensorflow/examples/label_image/label_image

                       显示结果:military uniform(653):0.834306

                                         mortarboard(668):0.0218695

                                         academic gown(401):0.0103581

                                         pickelhaube(716):0.00800814

                                         bulletproof vest(466):0.00535084

                        说明测试OK!

      

以下操作在ARM板子上:

      1、拷贝生成的label_image到板子上(bazel_bin/tensorflow/contrib/lite/examples/label_image/)(1.7M)

            拷贝生成的label_image到板子上(bazel_bin/tensorflow/examples/label_image)(52.2M)

      2、拷贝图片./tensorflow/examples/label_image/data/grace_hopper.jpg到板子上  

      3、下载模型mobilenet_quant_v1_224.tflite

          (如果下载其它模型,可参考该文件:/tensorflow/contrib/lite/g3doc/models.md)

      4、下载模型所需文件:

             curl -L "https://storage.googleapis.com/download.tensorflow.org/models/inception_v3_2016_08_28_frozen.pb.tar.gz" | tar -C tensorflow/examples/label_image/data -xz

      5、拷贝libtensorflow_framework.so到板子上,文件位于目录bazel_bin/tensorflow/下(14M)

      4、运行label_image

        确保如下所需文件都已完成:

      (拷贝文件1:label_image)

      (拷贝文件2:grace_hopper.jpg)

      (拷贝文件3:mobilenet_quant_v1_224.tflite)

      (拷贝文件4:imagenet_slim_labels.txt)

      (拷贝文件5:libtensorflow_framework.so)

        ARM板上常用操作:

        mount /dev/sba1 /mnt/usb

        ls /mnt/usb

        cp /mnt/usb/   .

        执行脚本:

            ./label_image

           如果出现-sh: ./label_image: not found,可能是编译器不一致导致。

           尝试方法1:重定向:ln -s ld-linux.so.3 ld-linux-armhf.so.3

               新报错:./label_image:/lib/libm.so.6: version 'GLIBC_2.27' not found (required by ./label_image)

           出现以下信息,说明已经OK,恭喜你!

      

posted @ 2019-09-19 17:45  jimchen1218  阅读(3889)  评论(0编辑  收藏  举报