美文网首页
(五)在安卓上运行Tensorflow C++可执行文件

(五)在安卓上运行Tensorflow C++可执行文件

作者: 半笔闪 | 来源:发表于2019-08-01 13:43 被阅读0次

如果不想使用封装好的接口,要使用tensorflow原始的C++或C API,则可以使用如下方法编译so库。

  1. 下载tensorflow源码
  2. 解压源码后,修改WORKSPACE,在最后面证据NDK,SDK路径信息:
android_sdk_repository(
    name = "androidsdk",
    api_level = 24,
    build_tools_version = "27.0.3",
    # Replace with path to Android SDK on your system
    path = "/mnt/e/crnncpp/android-sdk-linux"##你的sdk目录
)
##这里注意最好选ndk-r15c或ndk-r14b,这两个版本是经过作者实验的
##特别注意ndk-r16是坑
android_ndk_repository(
    name = "androidndk",
    path = "/mnt/e/crnncpp/android-ndk-r15c",##你的ndk目录
    api_level = 24
)
./configure

如下输入

You have bazel 0.19.1 installed.
Please specify the location of python. [Default is /usr/bin/python]:


Found possible Python library paths:
  /usr/local/lib/python3.6/dist-packages
  /usr/lib/python3/dist-packages
Please input the desired Python library path to use.  Default is [/usr/local/lib/python3.6/dist-packages]

Do you wish to build TensorFlow with jemalloc as malloc support? [Y/n]: n
No jemalloc as malloc support will be enabled for TensorFlow.

Do you wish to build TensorFlow with Google Cloud Platform support? [Y/n]: n
No Google Cloud Platform support will be enabled for TensorFlow.

Do you wish to build TensorFlow with Hadoop File System support? [Y/n]: n
No Hadoop File System support will be enabled for TensorFlow.

Do you wish to build TensorFlow with Amazon S3 File System support? [Y/n]: n
No Amazon S3 File System support will be enabled for TensorFlow.

Do you wish to build TensorFlow with Apache Kafka Platform support? [Y/n]: n
No Apache Kafka Platform support will be enabled for TensorFlow.

Do you wish to build TensorFlow with XLA JIT support? [y/N]: n
No XLA JIT support will be enabled for TensorFlow.

Do you wish to build TensorFlow with GDR support? [y/N]: n
No GDR support will be enabled for TensorFlow.

Do you wish to build TensorFlow with VERBS support? [y/N]: n
No VERBS support will be enabled for TensorFlow.

Do you wish to build TensorFlow with OpenCL SYCL support? [y/N]: n
No OpenCL SYCL support will be enabled for TensorFlow.

Do you wish to build TensorFlow with CUDA support? [y/N]: n
No CUDA support will be enabled for TensorFlow.

Do you wish to download a fresh release of clang? (Experimental) [y/N]: n
Clang will not be downloaded.

Do you wish to build TensorFlow with MPI support? [y/N]: n
No MPI support will be enabled for TensorFlow.

Please specify optimization flags to use during compilation when bazel option "--config=opt" is specified [Default is -march=native]: -march=armv7-a


The WORKSPACE file has at least one of ["android_sdk_repository", "android_ndk_repository"] already set. Will not ask to help configure the WORKSPACE. Please delete the existing rules to activate the helper.

Preconfigured Bazel build configs. You can use any of the below by adding "--config=<>" to your build command. See tools/bazel.rc for more details.
        --config=mkl            # Build with MKL support.
        --config=monolithic     # Config for mostly static monolithic build.
Configuration finished
  1. 修改tensorflow/tensorflow/contrib/android/路径下的BUILD文件:
package(default_visibility = ["//visibility:public"])

licenses(["notice"])  # Apache 2.0

load(
    "//tensorflow:tensorflow.bzl",
    "tf_copts",
    "tf_cc_test",
    "tf_cc_binary",
)

cc_library(
    name = "TensorflowInference_lib",
    testonly = 1,
    srcs = [],
    copts = tf_copts(),
    visibility = ["//visibility:public"],
    deps = select({
        "//tensorflow:android": [
            "//tensorflow/core:android_tensorflow_lib",
            "//tensorflow/core:android_tensorflow_test_lib",
        ],
        "//conditions:default": [
            "//tensorflow/core:core_cpu",
            "//tensorflow/core:lib",
            "//tensorflow/core:framework",
            "//tensorflow/core:framework_internal",
            "//tensorflow/core:framework_lite",
            "//tensorflow/core:protos_all_cc",
            "//tensorflow/core:tensorflow",
            "//tensorflow/core:test",
        ],
    }),
)
cc_binary(
    name = "libtensorflow_cc_inference.so",
    testonly = 1,
    srcs = [],
    copts = tf_copts(),
    linkopts = select({
        "//tensorflow:android": [
            "-shared",
            "-landroid",
            "-latomic",
            "-ljnigraphics",
            "-llog",
            "-lm",
            "-z defs",
            "-Wl,--allow-multiple-definition",
            "-Wl,-soname=libtensorflow_cc_inference.so",
            #特别注意这里,改为你第五步新建的version_script.lds的绝对路径
            "-Wl,--version-script=/mnt/e/tensorflow_compile/tensorflow/tensorflow/contrib/android/jni/version_script.lds",
            "-s",
        ],
        "//conditions:default": [],
    }),
    linkstatic = 1,
    linkshared = 1,
    deps = [":TensorflowInference_lib"],
)
  1. 修改tensorflow/tensorflow/contrib/android/jni下的version_script.lds文件(也可以在任意目录新建)
#需要C接口使用如下version_script.lds
VERS_1.0 {
  # Export JNI symbols.
  global:
    *TF_*;
    *TFE_*;
  # Hide everything else.
  local:
    *;
};
#需要C++接口使用如下version_script.lds
VERS_1.0 {
  # Export c symbols.
  global:
    *TF_*;
    *TFE_*;
    extern "C++" {
    tensorflow::*;   
    };#意思是以C++的形式暴露tensorflow接口
  # Hide everything else.
  local:
    *;
};

  1. 在tensorflow目录下执行如下命令:
bazel build --config=monolithic -c opt //tensorflow/contrib/android:libtensorflow_cc_inference.so --crosstool_top=//external:android/crosstool --host_crosstool_top=@bazel_tools//tools/cpp:toolchain --cpu=armeabi-v7a --cxxopt="-std=c++11" --verbose_failures

等待编译介绍,即可在tensorflow/bazel-bin/tensorflow/contrib/android/下得到可在android上运行的so库了。

相关文章

网友评论

      本文标题:(五)在安卓上运行Tensorflow C++可执行文件

      本文链接:https://www.haomeiwen.com/subject/bkuqdctx.html