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

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

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

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

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

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

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

buildFeatures {
  prefab = true
}

ในที่สุดคุณต้องเพิ่มแพ็คเกจ tensorflowlite_jni_gms_client ที่นำเข้าจาก AAR เป็นการพึ่งพาในสคริปต์ 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 ของคุณ

ชวา

Task tfLiteInitializeTask = TfLiteNative.initialize(context);
      

คอตลิน

val tfLiteInitializeTask: Task = TfLiteNative.initialize(context)
        

การใช้ Task API ของบริการ Google Play 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/Kotlin เข้าถึงได้ด้วยโค้ดเนทีฟของคุณดังนี้:

ชวา

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

คอตลิน

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:

ชวา

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

คอตลิน

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

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