![](/img/trans.png)
[英]Undefined references when trying to use Tensorflow-Lite C api built for ARM64, with Android - ndk standalone toolchain for ARM
[英]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 ,但盡管它似乎可以構建和鏈接,但無法通過本機代碼訪問該庫。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 委托運行推理?
我設法做到了如下:
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 目錄。
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
。)
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
)
相關行如下:
#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.