如何在(Android Studio)NDK(C / C++ API中运行Tensorflow-Lite推理



信息

  • 我从Keras构建了一个Tensorflow(TF)模型,并将其转换为Tensorflow-Lite(TFL)。
  • 我在Android Studio中构建了一个Android应用程序,并使用Java API运行TFL模型。
  • 在Java应用程序中,我使用了TFL支持库(见这里)和JCenter的TensorFlow Lite AAR,将implementation 'org.tensorflow:tensorflow-lite:+'包含在我的build.gradle依赖项下

推理时间不是那么长,所以现在我想在Android的NDK中使用TFL。

所以我在Android Studio的NDK中构建了Java应用程序的精确副本,现在我正在尝试将TFL库包含在项目中。我遵循了 TensorFlow-Lite 的 Android 指南,在本地构建了 TFL 库(并获得了一个 AAR 文件),并将该库包含在 Android Studio 的 NDK 项目中。

现在,我尝试在C++文件中使用 TFL 库,方法是尝试在代码中#include它,但我收到一条错误消息:cannot find tensorflow(或我尝试使用的任何其他名称,根据我在CMakeLists.txt文件中给它的名称)。

文件

Appbuild.gradle

apply plugin: 'com.android.application'
android {
compileSdkVersion 29
buildToolsVersion "29.0.3"
defaultConfig {
applicationId "com.ndk.tflite"
minSdkVersion 28
targetSdkVersion 29
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
externalNativeBuild {
cmake {
cppFlags ""
}
}
ndk {
abiFilters 'arm64-v8a'
}
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
// tf lite
aaptOptions {
noCompress "tflite"
}
externalNativeBuild {
cmake {
path "src/main/cpp/CMakeLists.txt"
version "3.10.2"
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
// tflite build
compile(name:'tensorflow-lite', ext:'aar')
}

Projectbuild.gradle

buildscript {
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.6.2'
}
}
allprojects {
repositories {
google()
jcenter()
// native tflite
flatDir {
dirs 'libs'
}
}
}

task clean(type: Delete) {
delete rootProject.buildDir
}

CMakeLists.txt

cmake_minimum_required(VERSION 3.4.1)
add_library( # Sets the name of the library.
native-lib
# Sets the library as a shared library.
SHARED
# Provides a relative path to your source file(s).
native-lib.cpp )
add_library( # Sets the name of the library.
tensorflow-lite
# Sets the library as a shared library.
SHARED
# Provides a relative path to your source file(s).
native-lib.cpp )
find_library( # Sets the name of the path variable.
log-lib
# Specifies the name of the NDK library that
# you want CMake to locate.
log )

target_link_libraries( # Specifies the target library.
native-lib tensorflow-lite
# Links the target library to the log library
# included in the NDK.
${log-lib} )

native-lib.cpp

#include <jni.h>
#include <string>
#include "tensorflow"
extern "C" JNIEXPORT jstring JNICALL
Java_com_xvu_f32c_1jni_MainActivity_stringFromJNI(
JNIEnv* env,
jobject /* this */) {
std::string hello = "Hello from C++";
return env->NewStringUTF(hello.c_str());
}
class FlatBufferModel {
// Build a model based on a file. Return a nullptr in case of failure.
static std::unique_ptr<FlatBufferModel> BuildFromFile(
const char* filename,
ErrorReporter* error_reporter);
// Build a model based on a pre-loaded flatbuffer. The caller retains
// ownership of the buffer and should keep it alive until the returned object
// is destroyed. Return a nullptr in case of failure.
static std::unique_ptr<FlatBufferModel> BuildFromBuffer(
const char* buffer,
size_t buffer_size,
ErrorReporter* error_reporter);
};

进展

我也试图遵循这些:

  • 在 Android Studio Project 中使用 tensorflow lite C++ API 的问题
  • Android C++ NDK:某些共享库拒绝在运行时链接
  • 如何将 TensorFlow Lite 构建为静态库,并从单独的 (CMake) 项目链接到它?
  • 如何设置 Tensorflow Lite C++的输入
  • 如何仅从源代码构建 TensorFlow lite 而不是所有 TensorFlow?

但就我而言,我使用 Bazel 来构建 TFL 库。

尝试构建 (label_image) 的分类演示,我设法构建它并adb push到我的设备,但在尝试运行时出现以下错误:

ERROR: Could not open './mobilenet_quant_v1_224.tflite'.
Failed to mmap model ./mobilenet_quant_v1_224.tflite
  • 我关注了子梦柳的帖子:试图在WORKSPACE中设置android_sdk_repository/android_ndk_repository,得到了一个错误:WORKSPACE:149:1: Cannot redefine repository after any load statement in the WORKSPACE file (for repository 'androidsdk'),将这些语句放在不同的地方会导致同样的错误。
  • 我删除了这些更改WORKSPACE,并继续阅读子梦柳的帖子:我编译了libtensorflowLite.so,并编辑了CMakeLists.txt,以便引用libtensorflowLite.so文件,但省略了FlatBuffer部分。Android 项目编译成功,但没有明显的变化,我仍然无法包含任何 TFLite 库。

在尝试编译 TFL 时,我在tensorflow/tensorflow/lite/BUILD中添加了一个cc_binary(按照label_image示例):

cc_binary(
name = "native-lib",
srcs = [
"native-lib.cpp",
],
linkopts = tflite_experimental_runtime_linkopts() + select({
"//tensorflow:android": [
"-pie",
"-lm",
],
"//conditions:default": [],
}),
deps = [
"//tensorflow/lite/c:common",
"//tensorflow/lite:framework",
"//tensorflow/lite:string_util",
"//tensorflow/lite/delegates/nnapi:nnapi_delegate",
"//tensorflow/lite/kernels:builtin_ops",
"//tensorflow/lite/profiling:profiler",
"//tensorflow/lite/tools/evaluation:utils",
] + select({
"//tensorflow:android": [
"//tensorflow/lite/delegates/gpu:delegate",
],
"//tensorflow:android_arm64": [
"//tensorflow/lite/delegates/gpu:delegate",
],
"//conditions:default": [],
}),
)

并尝试为x86_64构建它,arm64-v8a出现错误:cc_toolchain_suite rule @local_config_cc//:toolchain: cc_toolchain_suite '@local_config_cc//:toolchain' does not contain a toolchain for cpu 'x86_64'.

检查第 47 行中的external/local_config_cc/BUILD(提供错误):

cc_toolchain_suite(
name = "toolchain",
toolchains = {
"k8|compiler": ":cc-compiler-k8",
"k8": ":cc-compiler-k8",
"armeabi-v7a|compiler": ":cc-compiler-armeabi-v7a",
"armeabi-v7a": ":cc-compiler-armeabi-v7a",
},
)

这些是找到的唯一 2 个cc_toolchain。在存储库中搜索"cc-compiler-",我只找到了">aarch64",我假设它是针对 64 位 ARM,但没有"x86_64"。不过,有"x64_windows"——我在 Linux 上。

尝试使用 aarch64 构建如下:

bazel build -c opt --fat_apk_cpu=aarch64 --cpu=aarch64 --host_crosstool_top=@bazel_tools//tools/cpp:toolchain //tensorflow/lite/java:tensorflow-lite

导致错误:

ERROR: /.../external/local_config_cc/BUILD:47:1: in cc_toolchain_suite rule @local_config_cc//:toolchain: cc_toolchain_suite '@local_config_cc//:toolchain' does not contain a toolchain for cpu 'aarch64'

使用 Android Studio 中的库:

我能够通过更改构建配置中的soname并使用CMakeLists.txt中的完整路径来构建x86_64架构库。这导致了一个.so共享库。另外 - 通过调整aarch64_makefile.inc文件,我能够使用 TFLite Docker 容器构建arm64-v8a库,但我没有更改任何构建选项,而是让build_aarch64_lib.sh它构建的任何内容。这导致了一个.a静态库。

所以现在我有两个 TFLite 库,但我仍然无法使用它们(例如,我无法#include "..."任何东西)。

尝试构建项目时,仅使用x86_64可以正常工作,但尝试包含arm64-v8a库会导致 ninja 错误:'.../libtensorflow-lite.a', needed by '.../app/build/intermediates/cmake/debug/obj/armeabi-v7a/libnative-lib.so', missing and no known rule to make it

不同的方法 - 使用 Gradle 构建/编译源文件:

  1. 我在 Android Studio 中创建了一个原生C++项目
  2. 我从 Tensorflow 的lite目录中获取了基本的 C/C++ 源文件和标头,并在app/src/main/cpp中创建了一个类似的结构,其中我包含 (A) 张量流、(B) absl 和 (C) flatbuffers 文件
  3. 我将 tensorflow 的所有头文件中的#include "tensorflow/...行更改为相对路径,以便编译器可以找到它们。
  4. 在应用程序的build.gradle中,我为.tflite文件添加了一个无压缩行:aaptOptions { noCompress "tflite" }
  5. 我向应用程序添加了assets目录
  6. native-lib.cpp,我从TFLite网站添加了一些示例代码
  7. 尝试使用包含的源文件构建项目(构建目标为arm64-v8a)。

我收到一个错误:

/path/to/Android/Sdk/ndk/20.0.5594570/toolchains/llvm/prebuilt/linux-x86_64/sysroot/usr/include/c++/v1/memory:2339: error: undefined reference to 'tflite::impl::Interpreter::~Interpreter()'

<memory>中,2339行是"delete __ptr;"行:

_LIBCPP_INLINE_VISIBILITY void operator()(_Tp* __ptr) const _NOEXCEPT {
static_assert(sizeof(_Tp) > 0,
"default_delete can not delete incomplete type");
static_assert(!is_void<_Tp>::value,
"default_delete can not delete incomplete type");
delete __ptr;
}

问题

如何在 Android Studio 中包含 TFLite 库,以便从 NDK 运行 TFL 推理?

或者 - 如何使用 gradle(目前使用cmake)来构建和编译源文件?

我通过以下方式将本机TFL与C-API一起使用:

设置:

  1. 下载最新版本的 TensorFlow Lite AAR 文件
  2. 将下载的.arr文件的文件类型更改为.zip并解压缩文件以获取共享库(.so文件)
  3. 从 TFL 存储库中的c目录中下载所有头文件
  4. 在 Android Studio 中创建 Android C++ 应用
  5. app/src/main中创建一个jni目录(New->Folder->JNI Folder),并在其中创建架构子目录(例如arm64-v8ax86_64)
  6. 将所有头文件放在jni目录中(在架构目录旁边),并将共享库放在架构目录中
  7. 打开CMakeLists.txt文件并包含 TFL 库的add_library节、set_target_properties节中共享库的路径以及include_directories节中的标头(请参阅下面的"注释"部分)
  8. 同步格拉德尔

用法:

native-lib.cpp中包含标头,例如:

#include "../jni/c_api.h"
#include "../jni/common.h"
#include "../jni/builtin_ops.h"

可以直接调用 TFL 函数,例如:

TfLiteModel * model = TfLiteModelCreateFromFile(full_path);
TfLiteInterpreter * interpreter = TfLiteInterpreterCreate(model);
TfLiteInterpreterAllocateTensors(interpreter);
TfLiteTensor * input_tensor =
TfLiteInterpreterGetInputTensor(interpreter, 0);
const TfLiteTensor * output_tensor =
TfLiteInterpreterGetOutputTensor(interpreter, 0);
TfLiteStatus from_status = TfLiteTensorCopyFromBuffer(
input_tensor,
input_data,
TfLiteTensorByteSize(input_tensor));
TfLiteStatus interpreter_invoke_status = TfLiteInterpreterInvoke(interpreter);
TfLiteStatus to_status = TfLiteTensorCopyToBuffer(
output_tensor,
output_data,
TfLiteTensorByteSize(output_tensor));

笔记:

  • 在此设置中,使用了 SDK 版本 29
  • cmake环境还包括cppFlags "-frtti -fexceptions"

CMakeLists.txt示例:

set(JNI_DIR ${CMAKE_CURRENT_SOURCE_DIR}/../jni)
add_library(tflite-lib SHARED IMPORTED)
set_target_properties(tflite-lib
PROPERTIES IMPORTED_LOCATION
${JNI_DIR}/${ANDROID_ABI}/libtfl.so)
include_directories( ${JNI_DIR} )
target_link_libraries(
native-lib
tflite-lib
...)

我也一直在为Android构建TF Lite C++ API。幸运的是,我设法让它工作。

问题是我们需要在运行bazel build ...命令之前配置 Bazel 构建过程。TF Lite Android快速入门指南没有提到它。

分步指南(https://github.com/cuongvng/TF-Lite-Cpp-API-for-Android):

  • 第 1 步:安装 Bazel

  • 第 2 步:克隆 TensorFlow 存储库

git clone https://github.com/tensorflow/tensorflow
cd ./tensorflow/
  • 步骤 3:配置 Android构建 在运行bazel build ...命令之前,您需要配置构建过程。通过执行来执行此操作
./configure

configure文件位于 tensorflow 目录的根目录中,您可以在步骤 2 中cd该目录。 现在你必须在命令行上输入一些配置:

$ ./configure
You have bazel 3.7.2-homebrew installed.
Please specify the location of python. [Default is /Library/Developer/CommandLineTools/usr/bin/python3]: /Users/cuongvng/opt/miniconda3/envs/style-transfer-tf-lite/bin/python

首先是python的位置,因为./configure执行.configure.py文件。 选择安装了 Numpy 的位置,否则以后的版本将失败。 在这里,我将其指向conda环境的python可执行文件。

下一个

Found possible Python library paths:
/Users/cuongvng/opt/miniconda3/envs/style-transfer-tf-lite/lib/python3.7/site-packages
Please input the desired Python library path to use.  Default is [/Users/cuongvng/opt/miniconda3/envs/style-transfer-tf-lite/lib/python3.7/site-packages]

我按Enter使用默认站点包,其中包含构建 TF 所需的库。

下一个

Do you wish to build TensorFlow with ROCm support? [y/N]: N
No ROCm support will be enabled for TensorFlow.
Do you wish to build TensorFlow with CUDA support? [y/N]: N
No CUDA support will be enabled for TensorFlow.
Do you wish to download a fresh release of clang? (Experimental) [y/N]: N
Clang will not be downloaded.
Please specify optimization flags to use during compilation when bazel option "--config=opt" is specified [Default is -Wno-sign-compare]: 

如上所示键入,在最后一行键入Enter。 然后它会询问您是否为 Android 版本配置 ./WORKSPACE,键入y以添加配置。

Would you like to interactively configure ./WORKSPACE for Android builds? [y/N]: y
Searching for NDK and SDK installations.
Please specify the home path of the Android NDK to use. [Default is /Users/cuongvng/library/Android/Sdk/ndk-bundle]: /Users/cuongvng/Library/Android/sdk/ndk/21.1.6352462

这是我本地计算机上Android NDK(版本21.1.6352462)的主路径。 请注意,当您ls路径时,它必须包含platforms,例如:

$ ls /Users/cuongvng/Library/Android/sdk/ndk/21.1.6352462
CHANGELOG.md      build             ndk-stack         prebuilt          source.properties wrap.sh
NOTICE            meta              ndk-which         python-packages   sources
NOTICE.toolchain  ndk-build         package.xml       shader-tools      sysroot
README.md         ndk-gdb           platforms         simpleperf        toolchains

现在,我忽略生成的警告,然后选择最低 NDK API 级别

WARNING: The NDK version in /Users/cuongvng/Library/Android/sdk/ndk/21.1.6352462 is 21, which is not supported by Bazel (officially supported versions: [10, 11, 12, 13, 14, 15, 16, 17, 18]). Please use another version. Compiling Android targets may result in confusing errors.
Please specify the (min) Android NDK API level to use. [Available levels: ['16', '17', '18', '19', '21', '22', '23', '24', '26', '27', '28', '29']] [Default is 21]: 29

下一个

Please specify the home path of the Android SDK to use. [Default is /Users/cuongvng/library/Android/Sdk]: /Users/cuongvng/Library/Android/sdk
Please specify the Android SDK API level to use. [Available levels: ['28', '29', '30']] [Default is 30]: 30
Please specify an Android build tools version to use. [Available versions: ['29.0.2', '29.0.3', '30.0.3', '31.0.0-rc1']] [Default is 31.0.0-rc1]: 30.0.3

这就是Android构建配置的全部内容。为稍后出现的所有问题选择N

  • 步骤 4:构建共享库 (.so) 现在,您可以运行 bazel build 命令为目标架构生成库:
bazel build -c opt --config=android_arm //tensorflow/lite:libtensorflowlite.so
# or
bazel build -c opt --config=android_arm64 //tensorflow/lite:libtensorflowlite.so

它应该可以正常工作。 生成的库将保存在./bazel-bin/tensorflow/lite/libtensorflowlite.so.

最新更新