借助 Google Play 服务运行时中的 TensorFlow Lite,您无需将 TensorFlow Lite 库静态绑定到您的应用中,即可运行机器学习 (ML) 模型。本指南介绍了如何使用适用于 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 块,启用 Prefab 功能以从 CMake 脚本访问 C API:
buildFeatures {
prefab = true
}
最后,您需要将从 AAR 导入的软件包 tensorflowlite_jni_gms_client
添加为 CMake 脚本的依赖项:
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 原生 API 之前,您必须在 Java/Kotlin 代码中初始化 TfLiteNative
运行时。
Java
Task tfLiteInitializeTask = TfLiteNative.initialize(context);
Kotlin
val tfLiteInitializeTask: Task= TfLiteNative.initialize(context)
使用 Google Play Services Task API,TfLiteNative.initialize
可将 TFLite 运行时从 Google Play 服务异步加载到应用的运行时进程中。在执行用于访问 TensorFlow Lite API 的代码之前,请使用 addOnSuccessListener()
确保 TfLite.initialize()
任务完成。成功完成任务后,您可以调用所有可用的 TFLite 原生 API。
原生代码实现
如需将 Google Play 服务中的 TensorFlow Lite 与原生代码搭配使用,您可以执行以下任一操作:
- 声明新的 JNI 函数,以从 Java 代码调用原生函数
- 从现有的原生 C 代码调用 TensorFlow Lite 原生 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 头文件,以便包含 TfLite with Google Play services API:
#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 服务原生 API 头文件的 TensorFlow Lite 提供与常规 TensorFlow Lite C API 相同的 API,但已弃用或实验性的功能除外。目前,c_api.h
、c_api_types.h
和 common.h
头文件中的函数和类型可用。请注意,不支持 c_api_experimental.h
头文件中的函数。
您可以通过添加 tflite.h
来将 TensorFlow Lite 专用函数与 Google Play 服务搭配使用。