编译Tensorflow Lite 静态库

简介

最近需要编译Tensorflow Lite的静态库,但是网上资料不是很多,要么就是编译出问题

本来是根据官网来进行编译的

Build TensorFlow Lite with CMake

但是编译的时候会出现以下提示:

CMake Warning:
 Manually-specified variables were not used by the project:
  ANDROID_ABI

ANDROID_ABI似乎并没有起到作用

不过我又找到另一种方式,并且实测是可行的

防止以后踩坑,在此记录一下,希望能帮助需要的人

PS:以下内容我写在了我的Github上面,所以内容基本是一样的

构建Tensorflow Lite 静态库

准备

Tensorflow版本:2.4.1

NDK版本:r18b

git clone https://github.com/rianlu/build_tflite_static_library.git

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

仓库里已经有编译好的Tensorflow Lite 2.4.1的静态库了,可以直接使用

生成工具链

生成对应架构的工具链

$ANDROID_NDK/build/tools/make_standalone_toolchain.py --arch arm --api 21 --install-dir arm-android-toolchain

$ANDROID_NDK/build/tools/make_standalone_toolchain.py --arch arm64 --api 21 --install-dir arm64-android-toolchain

$ANDROID_NDK/build/tools/make_standalone_toolchain.py --arch x86 --api 21 --install-dir x86-android-toolchain

$ANDROID_NDK/build/tools/make_standalone_toolchain.py --arch x86_64 --api 21 --install-dir x86_64-android-toolchain

修改脚本

下载对应的Tensorflow源码,把仓库里的tensorflow/lite/tools/make/.sh放到Tensorflow源码的对应位置,这里额外添加了x86以及x86_64的脚本

然后把仓库里的tensorflow/lite/tools/make/targets/android_makefile.inc放到Tensorflow源码的对应位置,这里已经对android_makefile.inc作了修改,可以支持arm64-v8aarmeabi-v7ax86x86_64四种架构

然后修改tensorflow/lite/tools/make/.sh里的ndk_standalone_root,使其对应各个架构的工具链

修改Makefile.android

如果使用的是Tensorflow 2.4.1可以直接使用仓库里的Makefile.android,原仓库里的Makefile.android对应的版本是Tensorflow 2.3.0,如果是更高版本,作如下修改:

进入到${tensorflow_root}/tensorflow/lite/tools/make目录,将Makefile改为Makefile.android(可以先备份一下)

# 注释下面两行

# TARGET_TOOLCHAIN_PREFIX :=
# CC_PREFIX :=

# 原
PROFILER_SRCS := \
    tensorflow/lite/profiling/memory_info.cc \
    tensorflow/lite/profiling/platform_profiler.cc \
    tensorflow/lite/profiling/time.cc
    
# 修改后
PROFILER_SRCS := \
    tensorflow/lite/profiling/memory_info.cc \
    tensorflow/lite/profiling/platform_profiler.cc \
    tensorflow/lite/profiling/atrace_profiler.cc \
    tensorflow/lite/profiling/time.cc
    
# 原
ifeq ($(TARGET_ARCH),aarch64)

# 修改后
ifeq ($(findstring $(TARGET_ARCH),  android_armv8a, android_armv7a),)

执行脚本

执行./download_dependencies.sh,下载Tensorflow Lite依赖

执行chmod +x build_android_xx.sh以及./build_android_xx.sh

可能会出现以下错误:

fatal error: -f/--auxiliary may not be used without -shared
clang70++: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [/Users/enjoymusic/Development/tensorflow-repository/tensorflow-2.4.1/tensorflow/lite/tools/make/gen/android_armv7a/bin/benchmark_model] Error 1

这个对生成的benchmark-lib.a可能有影响,但是对libtensorflow-lite.a无影响

到此就完成了Tensorflow Lite静态库的编译,如果有什么疑问或建议,一起讨论~

你可能感兴趣的:(编译Tensorflow Lite 静态库)