【问题标题】:Using Tensorflow-Lite GPU delegate in Android's Native environment with C-API在带有 C-API 的 Android 原生环境中使用 Tensorflow-Lite GPU 委托
【发布时间】:2020-12-05 19:58:50
【问题描述】:

信息

我通过 C-API(在 these instructions 之后)在 Android 的 Native 环境中使用 Tensorflow-Lite,但与通过 Java API(在 ART 上)的 GPU 委托相比,运行时间要长得多。

JNI AAR file (2.2) 提供 C 头文件和共享库,但似乎共享库不包含 GPU 委托,而仅包含用于配置委托的框架(TfLiteDelegate 对象和 TfLiteDelegateCreate() )。

** 例如,它不提供任何TfLiteGpuDelegateV2Create()tflite 命名空间访问权限。

试验

  • 我尝试使用 cmake 在项目中包含 libtensorflowlite_gpu_delegate.so,但尽管它似乎可以构建和链接,但无法通过本机代码访问该库。
  • 我尝试按照c_api.h 的委托使用示例进行操作,但我似乎无法配置 GPU 委托。
  • Docker 容器不包含工具链(尝试使用bazel build -c opt --config android_arm64 tensorflow/lite/delegates/gpu:libtensorflowlite_gpu_delegate.sotensorflow/tensorflow:latest-devel-gpu 中构建共享库的Tensorflow Docker 映像失败并出现cc_toolchain_suite '@local_config_cc//:toolchain' does not contain a toolchain for cpu 'arm64-v8a'

问题

如何使用 C-API 在 Android 的 Native 环境中通过 GPU 委托 运行推理?

【问题讨论】:

  • 好问题,我也有同样的问题,有人可以帮助我们吗?

标签: c android-ndk delegates gpu tensorflow-lite


【解决方案1】:

我设法做到了如下:

1。克隆并配置tensorflow

从 GitHub 克隆 tensorflow 存储库,cd 到其中并运行 ./configure。重要的是要回答 Would you like to interactively configure ./WORKSPACE for Android builds? [y/N] 使用y 并正确指定Android NDK 和SDK 目录。

2。使用bazel 构建libtensorflow-lite_gpu_delegate

我成功构建了 GPU 委托共享库

bazel build -c opt --cxxopt=--std=c++11 --config android_arm64 tensorflow/lite/delegates/gpu:libtensorflowlite_gpu_delegate.so

我针对 Android NDK 18.1.5063045 构建,最低 API 级别为 27。请注意,我只测试了 android_arm64 架构,我无法保证其他架构。

(我编译TensorFlow的时候HEAD指向提交0f8a27183657972c8ba2bce150e1364179ded6f9。)

3。更新CMakeLists.txt

相关行如下:

include_directories(
    /Users/<name>/tensorflow/tensorflow/lite/delegates/gpu # for Mac 
)

add_library(tensorflow-lite_gpu_delegate SHARED IMPORTED)
set_target_properties(tensorflow-lite_gpu_delegate PROPERTIES IMPORTED_LOCATION
    /private/var/tmp/_bazel_<name>/fe60511640322ef6962b77bab4b291e3/execroot/org_tensorflow/bazel-out/arm64-v8a-opt/bin/tensorflow/lite/delegates/gpu/libtensorflowlite_gpu_delegate.so) # I obtained this path pressing Cmd+Option+C on the libtensorflow-lite_gpu_delegate.so file on Mac, might be different on your OS

target_link_libraries(
    tensorflow-lite_gpu_delegate
    )

4。在代码中使用 GPU 委托

相关行如下:

#include <delegate.h>

auto *delegate = TfLiteGpuDelegateV2Create(/*default options=*/nullptr);

// Create the model and interpreter options.
TfLiteModel *model = TfLiteModelCreate(/* create as usual */);
TfLiteInterpreterOptions* options = TfLiteInterpreterOptionsCreate();
TfLiteInterpreterOptionsAddDelegate(options, delegate);

// Create the interpreter.
TfLiteInterpreter *interpreter = TfLiteInterpreterCreate(model, options);

注意:对我来说,GPU 委托并没有在推理速度方面产生很大的改进。这可能是由于我的模型使用了 GPU 委托不支持的操作(现在支持的操作集似乎是 quite small),因此必须在 CPU 上进行计算。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-08-10
    • 1970-01-01
    • 1970-01-01
    • 2018-09-24
    • 1970-01-01
    • 1970-01-01
    • 2018-10-15
    • 1970-01-01
    相关资源
    最近更新 更多