tflite提供rpi/ios编译脚本(编译方法参看tensorflow/contrib/lite/g3doc),先运行download_dependencies.sh如需编译armv8版本,先利用NDKmake_standalone_toolchain,添加环境变量。在tensorflow/contrib/lite中新建build_ndk_lib.sh,其内容为
1 #!/bin/bash -x
2 set -e
3 SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
4 cd "$SCRIPT_DIR/../../.."
5 CC_PREFIX= make -j 3 -f tensorflow/contrib/lite/Makefile TARGET=armv8 TARGE T_ARCH=armv8 CROSS=armv8
修改makefile文件标红,
# Self-hosting
TARGET_ARCH := ${HOST_ARCH}
# Cross compiling
ifeq ($(CROSS),rpi)
TARGET_ARCH := armv7l
TARGET_TOOLCHAIN_PREFIX := arm-linux-gnueabihf-
endif
ifeq ($(CROSS),armv8)
TARGET_ARCH := armv8
TARGET_TOOLCHAIN_PREFIX := aarch64-linux-android-
endif
ifeq ($(CROSS),riscv)
TARGET_ARCH := riscv
TARGET_TOOLCHAIN_PREFIX := riscv32-unknown-elf-
endif
...
...
ifeq ($(TARGET_ARCH),armv7l)
CXXFLAGS += -mfpu=neon -pthread -fPIC
LIBS += -ldl
endif
ifeq ($(TARGET_ARCH),armv8)
CXXFLAGS += -fPIC
LIBS += -ldl
endif
ifeq ($(TARGET_ARCH),riscv)
# CXXFLAGS += -march=gap8
CXXFLAGS += -DTFLITE_MCU
LIBS += -ldl
BUILD_TYPE := micro
endif
保存后运行build_ndk_lib.sh
报错如下:
1../tensorflow/contrib/lite/tools/benchmark/benchmark_params.h:62:39: error: ‘static tflite::benchmark::BenchmarkParam::ParamType tflite::benchmark::BenchmarkParam::GetValueType() [with T = std::__cxx11::basic_string<char>]’ is private within this context
: BenchmarkParam(GetValueType<T>()), value_(value) {}
~~~~~~~~~~~~~~~^~
解决方法:
修改该文档中标红
virtual ~BenchmarkParam() {}
BenchmarkParam(ParamType type) : type_(type) {}
protected:
template <typename T>
static ParamType GetValueType();
private:
static void AssertHasSameType(ParamType a, ParamType b);
const ParamType type_;
2./tensorflow/contrib/lite/kernels/internal/optimized/cpu_check.h:21:58: fatal error: ndk/sources/android/cpufeatures/cpu-features.h: No such file or directory
#include "ndk/sources/android/cpufeatures/cpu-features.h"
解决方法将#include注释掉
3. /media/wangyingjun/Work/Use_tflite/tensorflow/tensorflow/contrib/lite/gen/lib/libtensorflow-lite.a -ldl -lstdc++ -lpthread -lm -lz -ldl
/opt/arch64-linux-android-4.9/bin/../lib/gcc/aarch64-linux-android/4.9.x/../../../../aarch64-linux-android/bin/ld: cannot find -lpthread
解决方法将makefile中ipthread删除
LIBS += \
-lstdc++ \-lpthread \
-lm \
-lz
4.出现未定义stderr。NDK从16开始默认Using Unified Headers,将NDK降为14,或者在NDK 15中make_standalone_toolchain时android-ndk-r15c/build/tools/make_standalone_toolchain.py --arch=arm64 --install-dir=/opt/arch-linux-android-4.9-r15c/aarch64-linux-android-4.9 --deprecated-headers(deprecated-headers与Unified Headers互斥,只需定义deprecated-headers)
编译 tensorflow 转 tflite 时用到的几个工具,freeze、toco、summarize_graph(参看https://blog.csdn.net/sinat_34022298/article/details/81569769、https://blog.csdn.net/gubenpeiyuan/article/details/79652227)