c - 在带有 C-API 的 Android native 环境中使用 Tensorflow-Lite GPU 委托(delegate)

标签 c android-ndk delegates gpu tensorflow-lite

信息

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

JNI AAR file (2.2)提供 C header 和共享库,但似乎共享库不包含 GPU 委托(delegate),而只包含一个框架来配置委托(delegate)(TfLiteDelegate 对象和 TfLiteDelegateCreate( )).

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

试验

  • 我尝试使用 cmake 在项目中包含一个 libtensorflowlite_gpu_delegate.so,但尽管它似乎构建和链接正常 - 该库无法通过 Native 访问代码。
  • 我尝试关注 c_api.h的委托(delegate)使用示例,但我似乎无法配置 GPU 委托(delegate)。
  • 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' 不包含 cpu 'arm64-v8a' 的工具链)

问题

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

最佳答案

我设法做到了,如下所示:

1。克隆并配置 tensorflow

从 GitHub 克隆 tensorflow repo,cd 进入它并运行 ./configure。重要的是要回答 您想为 Android 构建交互式配置 ./WORKSPACE 吗? [是/否] 使用 y 并正确指定 Android NDK 和 SDK 目录。

2。使用 bazel

构建 libtensorflow-lite_gpu_delegate

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

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 委托(delegate)

相关行如下:

#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 委托(delegate)不支持的操作(支持的操作集现在似乎是 quite small),因此必须在 CPU 上计算。

关于c - 在带有 C-API 的 Android native 环境中使用 Tensorflow-Lite GPU 委托(delegate),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63438024/

相关文章:

android - C++ - 将结构转换为字符

c++ - OpenMAX 和 NDK : Where I can get the surface?

c# - 在.NET 中,委托(delegate)的内部实现是什么?

c# - 多次附加事件处理程序

c - 使用 c/GTK 截取特定窗口的屏幕截图

c# - 生成 P/Invoke 代码的最简单方法?

android - 学习 ffmpeg 的一些链接和教程

ios - 委托(delegate) ivar 解释

c - 如何在 C 中保存/检索数据?

C变量实例化速度