Tensorflow编译android平台的so库和jar包

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/zhangpengzp/article/details/86220384

tensorflow自己编译so库和android jar包,遇到了一些困难,这里简单叙述下注意的问题和基本流程。

环境:ubuntu16.04

tensorflow1.5 下载地址:https://github.com/tensorflow/tensorflow

根据下图,tags不同选择不同tensorflow版本也可以。

1、bazel安装

参考官网,:第一步大多已经安装好了,自己检测一下。主要是step2 bazel下载,版本的抉择,之前由于版本错误,一直导致编译通不过。主要方法在于:tensorflow-1.5.0/tensorflow目录下的workspace.bzl 下找下面段话,其中`check_version("0.5.4")`最好下这个版本的bazel,这样保证了bazel的准确性,更容易编过。(tensorflow-1.5.0 为根目录

def tf_workspace(path_prefix="", tf_repo_name=""):

# We must check the bazel version before trying to parse any other BUILD

# files, in case the parsing of those build files depends on the bazel

# version we require here.

check_version("0.5.4")

cuda_configure(name="local_config_cuda")

sycl_configure(name="local_config_sycl")

python_configure(name="local_config_python")

官网内容:

Step 1: Install required packages

First, install the prerequisites: pkg-configzipg++zlib1g-devunzip, and python.

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

Step 2: Download Bazel

Next, download the Bazel binary installer named bazel-<version>-installer-linux-x86_64.sh from the Bazel releases page on GitHub.

Step 3: Run the installer

Run the Bazel installer as follows:

chmod +x bazel-<version>-installer-linux-x86_64.sh
./bazel-<version>-installer-linux-x86_64.sh --user

The --user flag installs Bazel to the $HOME/bin directory on your system and sets the .bazelrc path to $HOME/.bazelrc. Use the --help command to see additional installation options.

Step 4: Set up your environment

If you ran the Bazel installer with the --user flag as above, the Bazel executable is installed in your $HOME/bin directory. It’s a good idea to add this directory to your default paths, as follows:

export PATH="$PATH:$HOME/bin"

You can also add this command to your ~/.bashrc file.

2、下载 android ndk

之前下载的版本过新16,不适合tensorflow,在执行./conficure的时候,通过提示知道了支持10、11、12、13、14、15版本的。因此下载了15的,其实不用走弯路,打开目录`tensorflow-1.5.0`下configure.py文件搜索:“_SUPPORTED_ANDROID_NDK_VERSIONS”,如下:

_SUPPORTED_ANDROID_NDK_VERSIONS = [10, 11, 12, 13, 14, 15]

3、编译so和jar包

进入tensorflow根目录

1、配置路径

./configure

会让你配置一些文件的路径。如:python 、gcc++、ndk和是否用支持显卡编译、等这些都需要你认真配好,大多会读取环境变量中默认的安装好的地址,你回车即可,其他的不知道的直接回车即可。

2、编译so

bazel build -c opt //tensorflow/contrib/android:libtensorflow_inference.so  --crosstool_top=//external:android/crosstool  --host_crosstool_top=@bazel_tools//tools/cpp:toolchain  --cpu=armeabi-v7a

编译好的地址在:根目录/tensorflow/bazel-bin/tensorflow/contrib/android

1、编译过程中会提示一些找不到的依赖库,pip install 安装即可。

2、由于之前就用bazel编译过tensorflow,不知道咋了,结果bazel时就报错:

ERROR: /home/gezp/.cache/bazel/_bazel_gezp/2e4f7705435d0bd99b2c7f0d4e7595e7/external/protobuf_archive/BUILD:93:1: undeclared inclusion(s) in rule '@protobuf_archive//:protobuf_lite':

this rule is missing dependency declarations for the following files included by 'external/protobuf_archive/src/google/protobuf/stubs/int128.cc'

解决方法 :删除bazel cache,重新编译。

rm -rf /home/gezp/.cache/bazel #选择自己的cache目录

3、编译jar包

bazel build //tensorflow/contrib/android:android_tensorflow_inference_java

编译好的位置:/tensorflow/bazel-bin/tensorflow/contrib/android

编译jar包后 so不见了,编译so后jar不见了,因此编一个后得备份出来。

这里实现了一个android studio上实现手写字体的识别预测demo,欢迎下载参考。

猜你喜欢

转载自blog.csdn.net/zhangpengzp/article/details/86220384