簡體   English   中英

在帶有 C-API 的 Android 原生環境中使用 Tensorflow-Lite GPU 委托

[英]Using Tensorflow-Lite GPU delegate in Android's Native environment with C-API

信息

我正在通過 C-API(按照 這些說明)在 Android 的本機環境中使用 Tensorflow-Lite,但與通過 Java ZDB9742387108CA8DE63ZA7ACE14 的 GPU 委托相比,運行時間要長得多。

JNI AAR 文件 (2.2)提供 C-headers 和共享庫,但似乎共享庫不包含 GPU 委托,而僅包含配置委托的框架 ( TfLiteDelegate object 和TfLiteDelegateCreate() )

** 例如,它不提供任何TfLiteGpuDelegateV2Create()tflite命名空間訪問。

試驗

  • 我嘗試使用libtensorflowlite_gpu_delegate.so在項目中包含libtensorflowlite_gpu_delegate.so ,但盡管它似乎可以構建和鏈接,但無法通過本機代碼訪問該庫。
  • 我嘗試按照c_api.h的委托使用示例,但我似乎無法配置 GPU 委托。
  • Docker container doesn't include toolchain (trying to build shared library in tensorflow/tensorflow:latest-devel-gpu Tensorflow Docker image with bazel build -c opt --config android_arm64 tensorflow/lite/delegates/gpu:libtensorflowlite_gpu_delegate.so fails with cc_toolchain_suite '@local_config_cc//:toolchain' does not contain a toolchain for cpu 'arm64-v8a'

問題

如何使用C-API在 Android 的本環境中使用GPU 委托運行推理?

我設法做到了如下:

1.克隆並配置tensorflow

從 GitHub 克隆tensorflow庫, cd到其中並運行./configure 重要的是要回答Would you like to interactively configure./WORKSPACE for Android builds? [y/N] Would you like to interactively configure./WORKSPACE for Android builds? [y/N]y並正確指定 Android NDK 和 SDK 目錄。

2. 使用 bazel 構建libtensorflow-lite_gpu_delegate bazel

我成功構建了 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 代表在推理速度方面並沒有很大的提高。 這可能是由於我的 model 使用了 GPU 委托不支持的操作(現在支持的操作集似乎很小),因此必須在 CPU 上計算。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM