在 Android 中使用 Tensorflow C++ 和 CMake

hho*_*hov 5 c++ android cmake tensorflow

我有 Android 应用程序,其 C++ 代码与 CMake 链接。我需要在 C++ 端加载并运行张量流图。我使用 ./tensorflow/contrib/makefile/build_all_android.sh 脚本构建了 Tensorflow。我通过以下方式将tensorflow头文件和库添加到CMake文件中:

# other libraries here
add_library(lib_tf STATIC IMPORTED )
set_target_properties(lib_tf PROPERTIES IMPORTED_LOCATION
    /home/hhov/github/tensorflow/tensorflow/contrib/makefile/gen/lib/android_armeabi-v7a/libtensorflow-core.a)

add_library(lib_proto STATIC IMPORTED )
set_target_properties(lib_proto PROPERTIES IMPORTED_LOCATION
    /home/hhov/github/tensorflow/tensorflow/contrib/makefile/gen/protobuf_android/armeabi-v7a/lib/libprotobuf.a)

add_library(lib_proto_lite STATIC IMPORTED )
set_target_properties(lib_proto_lite PROPERTIES IMPORTED_LOCATION
    /home/hhov/github/tensorflow/tensorflow/contrib/makefile/gen/protobuf_android/armeabi-v7a/lib/libprotobuf-lite.a)

add_library(lib_protoc STATIC IMPORTED )
set_target_properties(lib_protoc PROPERTIES IMPORTED_LOCATION
    /home/hhov/github/tensorflow/tensorflow/contrib/makefile/gen/protobuf_android/armeabi-v7a/lib/libprotoc.a)

target_include_directories(pitools PRIVATE
                          # Other header paths here
                           ../../../../../../tensorflow/
                           ../../../../../../tensorflow/tensorflow/contrib/makefile/downloads
                           ../../../../../../tensorflow/tensorflow/contrib/makefile/downloads/eigen
                           ../../../../../../tensorflow/tensorflow/contrib/makefile/downloads/protobuf/src
                           ../../../../../../tensorflow/contrib/makefile/gen/proto
                           ../../../../../../tensorflow/tensorflow/contrib/makefile/downloads/nsync/public/
                           ../../../../../../tensorflow/tensorflow/contrib/makefile/gen/proto/
)

target_link_libraries( # Specifies the target library.
                       #other libs here
                       lib_tf
                       lib_proto
                       lib_proto_lite
                       lib_protoc
)
Run Code Online (Sandbox Code Playgroud)

目前我只是尝试创建 NewSession 并加载图表。目前代码很简单:包含此标头。

#include "tensorflow/core/framework/tensor.h"
#include "tensorflow/core/public/session.h"
#include "tensorflow/core/util/stat_summarizer.h"
Run Code Online (Sandbox Code Playgroud)

并像这样创建了会话:

tensorflow::SessionOptions options;
options.config.set_inter_op_parallelism_threads(0);
options.config.set_use_per_session_threads(false);
options.config.set_intra_op_parallelism_threads(0);
options.config.set_allow_soft_placement(false);
options.config.set_log_device_placement(false);
tensorflow::Status status = tensorflow::NewSession(options, &_session);
Run Code Online (Sandbox Code Playgroud)

当我尝试在 Android Studio 中创建项目时,它给我链接器错误,如下所示:

/home/hhov/github/tensorflow/tensorflow/contrib/makefile/gen/lib/android_armeabi-v7a/libtensorflow-core.a(config.pb.o):config.pb.cc:function std::string::_Rep::_M_dispose(std::allocator<char> const&) [clone .part.1]: error: undefined reference to 'std::string::_Rep::_M_destroy(std::allocator<char> const&)'
/home/hhov/github/tensorflow/tensorflow/contrib/makefile/gen/lib/android_armeabi-v7a/libtensorflow-core.a(config.pb.o):config.pb.cc:function void google::protobuf::internal::RepeatedPtrFieldBase::Clear<google::protobuf::RepeatedPtrField<std::string>::TypeHandler>() [clone .isra.161]: error: undefined reference to 'std::string::_M_mutate(unsigned int, unsigned int, unsigned int)'
/home/hhov/github/tensorflow/tensorflow/contrib/makefile/gen/lib/android_armeabi-v7a/libtensorflow-core.a(config.pb.o):config.pb.cc:function void google::protobuf::internal::RepeatedPtrFieldBase::Destroy<google::protobuf::RepeatedPtrField<std::string>::TypeHandler>() [clone .isra.160]: error: undefined reference to 'std::string::_Rep::_M_destroy(std::allocator<char> const&)'
/home/hhov/github/tensorflow/tensorflow/contrib/makefile/gen/lib/android_armeabi-v7a/libtensorflow-core.a(config.pb.o):config.pb.cc:function void google::protobuf::internal::RepeatedPtrFieldBase::Destroy<google::protobuf::RepeatedPtrField<std::string>::TypeHandler>() [clone .isra.160]: error: undefined reference to 'std::string::_Rep::_S_empty_rep_storage'
/home/hhov/github/tensorflow/tensorflow/contrib/makefile/gen/lib/android_armeabi-v7a/libtensorflow-core.a(config.pb.o):config.pb.cc:function protobuf_tensorflow_2fcore_2fprotobuf_2fconfig_2eproto::protobuf_AssignDescriptors(): error: undefined reference to 'std::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string(char const*, std::allocator<char> const&)'
Run Code Online (Sandbox Code Playgroud)

在下面的谷歌驱动器中找到输出的完整版本为 build_output.txt 我还尝试构建静态 libtensorflow_inference.so 并将其链接到项目,但是当尝试使用以下命令构建它时出现错误(构建输出也在驱动器中如tensorflow_build_inference_output.txt):

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
--verbose_failures
Run Code Online (Sandbox Code Playgroud)

上述命令的错误是:

ERROR: /home/hhov/.cache/bazel/_bazel_hhov/7cd939709f31bed648236b7a888c061b/external/double_conversion/BUILD.bazel:7:1: C++ compilation of rule '@double_conversion//:double-conversion' failed (Exit 1): false failed: error executing command 
  (cd /home/hhov/.cache/bazel/_bazel_hhov/7cd939709f31bed648236b7a888c061b/execroot/org_tensorflow && \
  exec env - \
    PWD=/proc/self/cwd \
    PYTHON_BIN_PATH=/usr/bin/python \
    PYTHON_LIB_PATH=/usr/local/lib/python2.7/dist-packages \
    TF_DOWNLOAD_CLANG=0 \
    TF_NEED_CUDA=0 \
    TF_NEED_OPENCL_SYCL=0 \
  /bin/false -MD -MF bazel-out/armeabi-v7a-opt/bin/external/double_conversion/_objs/double-conversion/external/double_conversion/double-conversion/diy-fp.pic.d '-frandom-seed=bazel-out/armeabi-v7a-opt/bin/external/double_conversion/_objs/double-conversion/external/double_conversion/double-conversion/diy-fp.pic.o' -fPIC -iquote external/double_conversion -iquote bazel-out/armeabi-v7a-opt/genfiles/external/double_conversion -iquote external/bazel_tools -iquote bazel-out/armeabi-v7a-opt/genfiles/external/bazel_tools -isystem external/double_conversion -isystem bazel-out/armeabi-v7a-opt/genfiles/external/double_conversion -isystem bazel-out/armeabi-v7a-opt/bin/external/double_conversion -c external/double_conversion/double-conversion/diy-fp.cc -o bazel-out/armeabi-v7a-opt/bin/external/double_conversion/_objs/double-conversion/external/double_conversion/double-conversion/diy-fp.pic.o)
Target //tensorflow/contrib/android:libtensorflow_inference.so failed to build
Run Code Online (Sandbox Code Playgroud)

完整错误消息的 Google 云端硬盘链接: https://drive.google.com/drive/folders/13XYQveBX1cB_jH88XWlpoycEtgTswOcA ?usp=sharing

您能说一下我做错了什么吗?或者如果这不是正确的方法,那么您能否提供一些步骤来做到这一点?

谢谢,呵呵