TensorFlow Lite ใน API บริการ Google Play (เบต้า)

TensorFlow Lite ในรันไทม์ของบริการ Google Play ช่วยให้คุณเรียกใช้โมเดลแมชชีนเลิร์นนิง (ML) ได้โดยไม่ต้องรวมไลบรารี TensorFlow Lite แบบคงที่เข้ากับแอป คู่มือนี้ให้คำแนะนำเกี่ยวกับวิธีใช้ C API สำหรับบริการ Google Play

ก่อนที่จะทำงานกับ TensorFlow Lite ใน C API ของบริการ Google Play คุณต้องติดตั้งเครื่องมือสร้าง CMake แล้ว

อัปเดตการกำหนดค่าบิลด์

เพิ่มทรัพยากร Dependency ต่อไปนี้ลงในโค้ดโปรเจ็กต์แอปเพื่อเข้าถึง Play services API สำหรับ TensorFlow Lite

implementation "com.google.android.gms:play-services-tflite-java:16.2.0-beta02"

จากนั้นเปิดใช้ฟีเจอร์ Prefab เพื่อเข้าถึง C API จากสคริปต์ CMake โดยอัปเดตบล็อก Android ของไฟล์ create.gradle ของโมดูล ดังนี้

buildFeatures {
  prefab = true
}

ขั้นตอนสุดท้ายคือคุณต้องเพิ่มแพ็กเกจ tensorflowlite_jni_gms_client ที่นำเข้าจาก AAR เป็นทรัพยากร Dependency ในสคริปต์ 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 Native API คุณต้องเริ่มต้นรันไทม์ TfLiteNative ในโค้ด Java/Kotlin

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 ทั้งหมดที่มี

การติดตั้งโค้ดแบบเนทีฟ

หากต้องการใช้ TensorFlow Lite ในบริการ Google Play ด้วยโค้ดแบบเนทีฟ คุณจะทำอย่างใดอย่างหนึ่งต่อไปนี้ได้

  • ประกาศฟังก์ชัน JNI ใหม่เพื่อเรียกฟังก์ชันเนทีฟจากโค้ด Java
  • เรียก TensorFlow Lite Native API จากโค้ด C แบบเนทีฟที่มีอยู่

ฟังก์ชัน JNI:

คุณประกาศฟังก์ชัน JNI ใหม่เพื่อทำให้โค้ดแบบเนทีฟเข้าถึงรันไทม์ของ 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

จากนั้นคุณจะเรียกใช้ฟังก์ชัน C จากโค้ด Java/Kotlin ได้ ดังนี้

Java

tfLiteHandleTask.onSuccessTask(unused -> {
    TfLiteJni jni = new TfLiteJni();
    jni.loadModel(getAssets(), "add.bin");
    //...
});
    

Kotlin

tfLiteHandleTask.onSuccessTask {
    val jni = TfLiteJni()
    jni.loadModel(assets, "add.bin")
    // ...
}
      

TensorFlow Lite ในโค้ด C

ใส่ไฟล์ส่วนหัว API ที่เหมาะสมเพื่อรวม TfLite ที่มี API ของบริการ Google Play

#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 ที่มีส่วนหัว Native API ของบริการ Google Play มี API เดียวกับ TensorFlow Lite C API ทั่วไป ยกเว้นฟีเจอร์ที่เลิกใช้งานแล้วหรืออยู่ระหว่างการทดสอบ ตอนนี้ฟังก์ชันและประเภทจากส่วนหัว c_api.h, c_api_types.h และ common.h พร้อมใช้งานแล้ว โปรดทราบว่าระบบไม่รองรับฟังก์ชันจากส่วนหัว c_api_experimental.h

คุณใช้ฟังก์ชันเฉพาะสำหรับ TensorFlow Lite กับบริการ Google Play ได้โดยรวมถึง tflite.h