编译 tensorflow lite 最小示例

Compiling the tensorflow lite minimal example

我正在尝试在 android 上使用 tensorflow lite。我在使用 android studio 进行编译时遇到错误:

undefined reference to 'tflite::InterpreterBuilder::operator()(std::__ndk1::unique_ptr<tflite::Interpreter, std::__ndk1::default_delete<tflite::Interpreter> >*)'

我在尝试编译时设法获得了一个可重现的示例 this minimal example from the tf repo

获取错误的步骤

  1. 下载 android ndk(我尝试了 15c 和 16b,但都失败了)。调用 build/tools/make_standalone_toolchain.py --arch arm --api 21 --stl=libc++ --install-dir android-toolchain

  2. 构建 libtensorflow,这在我的构建文件中。

tensorflow/lite/BUILD

cc_binary(
    name = "libtensorflowlite.so",
    linkopts=[
        "-shared",
        "-Wl,-soname=libtensorflowlite.so",
    ],  
    linkshared = 1,
    copts = tflite_copts(),
    deps = [ 
        ":framework",
        "//tensorflow/lite/kernels:builtin_ops",
    ],  
)

我这样称呼它:

bazel build //tensorflow/lite:libtensorflowlite.so --crosstool_top=//external:android/crosstool --cpu=armeabi-v7a --host_crosstool_top=@bazel_tools//tools/cpp:toolchain --cxxopt="-std=c++11"
  1. 我克隆了 flatbuffer 存储库(一个依赖项)并编译它。

  2. 然后我尝试编译 minimal.cc.

我这样称呼它:

android-toolchain/bin/clang++  -std=c++11 -Igit/tensorflow-android -Igit/flatbuffers/include -Lgit/tensorflow-android -Lgit/flatbuffers/build minimal.cc -ltensorflowlite -lflatbuffers

(libtensorflow.sogit/tensorflow-android, libflatbuffers.agit/flatbuffers/build)

这是怎么回事?我不知道为什么要调用这个方法,我没有用两个参数调用对象。

错误是我没有在 ndk 18 上。我没有切换到它,因为编译 bazel 时特别告诉我使用 <=16!