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"
然後,啟用 Prefab 功能,以從您的 CMake 指令碼存取 C API,方法是更新模組 build.gradle 檔案的 android 區塊
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 服務 Native API 標頭的 TensorFlow Lite 提供與一般 TensorFlow Lite C API 相同的 API,但不包括已淘汰或實驗性的功能。目前,c_api.h
、c_api_types.h
和 common.h
標頭中的函式和類型皆可使用。請注意,不支援 c_api_experimental.h
標頭中的函式。文件可在線上找到。
您可以透過納入 tflite.h
來使用 Google Play 服務專用的 TensorFlow Lite 函式。