Google Play 服務 C API 中的 TensorFlow Lite (Beta 版)

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 區塊,讓 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 服務 Task API,TfLiteNative.initialize 會以非同步方式將 TFLite 執行階段從 Google Play 服務載入至應用程式的執行階段程序。在執行存取 TensorFlow Lite API 的程式碼之前,請使用 addOnSuccessListener() 來確保 TfLite.initialize() 工作已完成。工作順利完成後,您可以叫用所有可用的 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
}
        

比對下列 loadModelrunInference 原生函式:

#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);

TensorFlow Lite 搭配 Google Play 服務 Native API 標頭提供與一般 TensorFlow Lite C API 相同的 API,但不含已淘汰或實驗性功能。目前,您可以使用 c_api.hc_api_types.hcommon.h 標頭中的函式和類型。請注意,系統不支援 c_api_experimental.h 標頭中的函式。

您可以加入 tflite.h,藉此搭配 Google Play 服務使用 TensorFlow Lite 專屬的函式。