Google Play 服务运行时中的 TensorFlow Lite 允许您运行机器学习 (ML) 模型,而无需将 TensorFlow Lite 库静态捆绑到您的应用程序中。本指南提供有关如何使用 Google Play 服务的 C API 的说明。
在使用 Google Play 服务 C API 中的 TensorFlow Lite 之前,请确保您已安装 CMake 构建工具。
更新您的构建配置
将以下依赖项添加到您的应用程序项目代码中,以访问 TensorFlow Lite 的 Play 服务 API
implementation "com.google.android.gms:play-services-tflite-java:16.2.0-beta02"
然后,通过更新模块的 build.gradle 文件的 android 块,启用 预制 功能,以便从您的 CMake 脚本访问 C API
buildFeatures {
prefab = true
}
最后,您需要在您的 CMake 脚本中添加从 AAR 导入的包 tensorflowlite_jni_gms_client 作为依赖项
find_package(tensorflowlite_jni_gms_client REQUIRED CONFIG)
target_link_libraries(tflite-jni # your JNI lib target
tensorflowlite_jni_gms_client::tensorflowlite_jni_gms_client
android # other deps for your target
log)
# Also add -DTFLITE_IN_GMSCORE -DTFLITE_WITH_STABLE_ABI
# to the C/C++ compiler flags.
add_compile_definitions(TFLITE_IN_GMSCORE)
add_compile_definitions(TFLITE_WITH_STABLE_ABI)
初始化 TensorFlow Lite 运行时
在调用 TensorFlow Lite Native API 之前,您必须在您的 Java/Kotlin 代码中初始化 TfLiteNative 运行时。
Java
Task tfLiteInitializeTask = TfLiteNative.initialize(context);
Kotlin
val tfLiteInitializeTask: Task= TfLiteNative.initialize(context)
使用 Google Play 服务 Task API,TfLiteNative.initialize 会异步地将 TFLite 运行时从 Google Play 服务加载到您的应用程序的运行时进程中。使用 addOnSuccessListener() 确保 TfLite.initialize() 任务在执行访问 TensorFlow Lite API 的代码之前完成。任务成功完成后,您可以调用所有可用的 TFLite Native API。
本机代码实现
要将 Google Play 服务中的 TensorFlow Lite 与您的本机代码一起使用,您可以执行以下操作之一
- 声明新的 JNI 函数,以从您的 Java 代码调用本机函数
- 从您现有的本机 C 代码调用 TensorFlow Lite Native API。
JNI 函数
您可以声明一个新的 JNI 函数,以使在 Java/Kotlin 中声明的 TensorFlow Lite 运行时可供您的本机代码访问,如下所示
Java
package com.google.samples.gms.tflite.c;
public class TfLiteJni {
static {
System.loadLibrary("tflite-jni");
}
public TfLiteJni() { /**/ };
public native void loadModel(AssetManager assetManager, String assetName);
public native float[] runInference(float[] input);
}
Kotlin
package com.google.samples.gms.tflite.c
class TfLiteJni() {
companion object {
init {
System.loadLibrary("tflite-jni")
}
}
external fun loadModel(assetManager: AssetManager, assetName: String)
external fun runInference(input: FloatArray): FloatArray
}
匹配以下 loadModel 和 runInference 本机函数
#ifdef __cplusplus
extern "C" {
#endif
void Java_com_google_samples_gms_tflite_c_loadModel(
JNIEnv *env, jobject tflite_jni, jobject asset_manager, jstring asset_name){}
//...
}
jfloatArray Java_com_google_samples_gms_tflite_c_TfLiteJni_runInference(
JNIEnv* env, jobject tfliteJni, jfloatArray input) {
//...
}
#ifdef __cplusplus
} // extern "C".
#endif
然后,您可以从您的 Java/Kotlin 代码调用您的 C 函数
Java
tfLiteHandleTask.onSuccessTask(unused -> {
TfLiteJni jni = new TfLiteJni();
jni.loadModel(getAssets(), "add.bin");
//...
});
Kotlin
tfLiteHandleTask.onSuccessTask {
val jni = TfLiteJni()
jni.loadModel(assets, "add.bin")
// ...
}
C 代码中的 TensorFlow Lite
包含相应的 API 头文件,以包含带有 Google Play 服务 API 的 TfLite
#include "tensorflow/lite/c/c_api.h"
然后,您可以使用常规的 TensorFlow Lite C API
auto model = TfLiteModelCreate(model_asset, model_asset_length);
// ...
auto options = TfLiteInterpreterOptionsCreate();
// ...
auto interpreter = TfLiteInterpreterCreate(model, options);
带有 Google Play 服务的 TensorFlow Lite Native API 头文件提供与常规 TensorFlow Lite C API 相同的 API,但不包括已弃用或实验性的功能。目前,来自 c_api.h、c_api_types.h 和 common.h 头文件的函数和类型可用。请注意,来自 c_api_experimental.h 头文件的函数不受支持。文档可以在 网上 找到。
您可以通过包含 tflite.h 来使用特定于带有 Google Play 服务的 TensorFlow Lite 的函数。