TensorFlow Lite ใน Java API ของบริการ Google Play

TensorFlow Lite ในบริการ Google Play ยังเข้าถึงได้โดยใช้ Java API เสริมจาก Native API โดยเฉพาะอย่างยิ่ง TensorFlow Lite ในบริการต่างๆ ของ Google Play ที่พร้อมให้ใช้งานผ่าน TensorFlow Lite Translation API

การใช้ API ล่าม

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

1. เพิ่มทรัพยากร Dependency ของโปรเจ็กต์

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

dependencies {
...
    // Tensorflow Lite dependencies for Google Play services
    implementation 'com.google.android.gms:play-services-tflite-java:16.0.1'
    // Optional: include Tensorflow Lite Support Library
    implementation 'com.google.android.gms:play-services-tflite-support:16.0.1'
...
}

2. เพิ่มการเริ่มต้นของ TensorFlow Lite

เริ่มต้นคอมโพเนนต์ TensorFlow Lite ของ API บริการ Google Play ก่อนโดยใช้ API ของ TensorFlow Lite ดังนี้

Kotlin

val initializeTask: Task<Void> by lazy { TfLite.initialize(this) }

Java

Task<Void> initializeTask = TfLite.initialize(context);

3. สร้างล่ามและตั้งค่าตัวเลือกรันไทม์

สร้างล่ามโดยใช้ InterpreterApi.create() และกำหนดค่าให้ใช้รันไทม์ของบริการ Google Play โดยการเรียกใช้ InterpreterApi.Options.setRuntime() ดังที่แสดงในโค้ดตัวอย่างต่อไปนี้

Kotlin

import org.tensorflow.lite.InterpreterApi
import org.tensorflow.lite.InterpreterApi.Options.TfLiteRuntime
...
private lateinit var interpreter: InterpreterApi
...
initializeTask.addOnSuccessListener {
  val interpreterOption =
    InterpreterApi.Options().setRuntime(TfLiteRuntime.FROM_SYSTEM_ONLY)
  interpreter = InterpreterApi.create(
    modelBuffer,
    interpreterOption
  )}
  .addOnFailureListener { e ->
    Log.e("Interpreter", "Cannot initialize interpreter", e)
  }

Java

import org.tensorflow.lite.InterpreterApi
import org.tensorflow.lite.InterpreterApi.Options.TfLiteRuntime
...
private InterpreterApi interpreter;
...
initializeTask.addOnSuccessListener(a -> {
    interpreter = InterpreterApi.create(modelBuffer,
      new InterpreterApi.Options().setRuntime(TfLiteRuntime.FROM_SYSTEM_ONLY));
  })
  .addOnFailureListener(e -> {
    Log.e("Interpreter", String.format("Cannot initialize interpreter: %s",
          e.getMessage()));
  });

คุณควรใช้การติดตั้งใช้งานข้างต้นเนื่องจากหลีกเลี่ยงการบล็อกเทรดอินเทอร์เฟซผู้ใช้ Android หากจำเป็นต้องจัดการการดำเนินการของชุดข้อความให้ใกล้ชิดมากขึ้น คุณสามารถเพิ่มการเรียกใช้ Tasks.await() เพื่อสร้างอินเตอร์พรีเตอร์ได้ ดังนี้

Kotlin

import androidx.lifecycle.lifecycleScope
...
lifecycleScope.launchWhenStarted { // uses coroutine
  initializeTask.await()
}

Java

@BackgroundThread
InterpreterApi initializeInterpreter() {
    Tasks.await(initializeTask);
    return InterpreterApi.create(...);
}

4. เรียกใช้การอนุมาน

ใช้ออบเจ็กต์ interpreter ที่คุณสร้างขึ้นเพื่อเรียกเมธอด run() เพื่อสร้างการอนุมาน

Kotlin

interpreter.run(inputBuffer, outputBuffer)

Java

interpreter.run(inputBuffer, outputBuffer);

การเร่งฮาร์ดแวร์

TensorFlow Lite ช่วยให้คุณเร่งประสิทธิภาพของโมเดลได้โดยใช้ตัวประมวลผลฮาร์ดแวร์เฉพาะทาง เช่น หน่วยประมวลผลกราฟิก (GPU) คุณจะใช้ประโยชน์จากโปรเซสเซอร์พิเศษเหล่านี้ได้โดยใช้ไดรเวอร์ฮาร์ดแวร์ที่เรียกว่าการมอบสิทธิ์

GPU Delegate มีให้ผ่านบริการ Google Play และจะมีการโหลดแบบไดนามิก เช่นเดียวกับเวอร์ชันของบริการ Play สำหรับ ครั้งที่ Mediation API

กำลังตรวจสอบความเข้ากันได้ของอุปกรณ์

อุปกรณ์บางรุ่นไม่รองรับการเร่งฮาร์ดแวร์ GPU ด้วย TFLite ใช้เมธอด TfLiteGpu.isGpuDelegateAvailable เพื่อตรวจสอบว่าอุปกรณ์เข้ากันได้กับการมอบสิทธิ์ GPU หรือไม่ เพื่อลดข้อผิดพลาดและข้อขัดข้องที่อาจเกิดขึ้น

ใช้วิธีนี้เพื่อยืนยันว่าอุปกรณ์เข้ากันได้กับ GPU หรือไม่ และใช้ CPU เป็นข้อมูลสำรองเมื่อไม่รองรับ GPU

useGpuTask = TfLiteGpu.isGpuDelegateAvailable(context)

เมื่อมีตัวแปรอย่างเช่น useGpuTask แล้ว คุณจะใช้ตัวแปรดังกล่าวเพื่อระบุว่าอุปกรณ์ใช้การมอบสิทธิ์ GPU หรือไม่

Kotlin

val interpreterTask = useGpuTask.continueWith { task ->
  val interpreterOptions = InterpreterApi.Options()
      .setRuntime(TfLiteRuntime.FROM_SYSTEM_ONLY)
  if (task.result) {
      interpreterOptions.addDelegateFactory(GpuDelegateFactory())
  }
  InterpreterApi.create(FileUtil.loadMappedFile(context, MODEL_PATH), interpreterOptions)
}
    

Java

Task<InterpreterApi.Options> interpreterOptionsTask = useGpuTask.continueWith({ task ->
  InterpreterApi.Options options =
      new InterpreterApi.Options().setRuntime(TfLiteRuntime.FROM_SYSTEM_ONLY);
  if (task.getResult()) {
     options.addDelegateFactory(new GpuDelegateFactory());
  }
  return options;
});
    

GPU พร้อม API ของโหมดล่าม

วิธีใช้การมอบสิทธิ์ GPU กับ API ของล่าม ให้ทำดังนี้

  1. อัปเดตทรัพยากร Dependency ของโปรเจ็กต์เพื่อใช้การมอบสิทธิ์ GPU จากบริการ Play โดยทำดังนี้

    implementation 'com.google.android.gms:play-services-tflite-gpu:16.1.0'
    
  2. เปิดใช้ตัวเลือกการมอบสิทธิ์ GPU ในการเริ่มต้นของ TFlite

    Kotlin

        TfLite.initialize(context,
          TfLiteInitializationOptions.builder()
           .setEnableGpuDelegateSupport(true)
           .build())
        

    Java

        TfLite.initialize(context,
          TfLiteInitializationOptions.builder()
           .setEnableGpuDelegateSupport(true)
           .build());
        
  3. เปิดใช้การมอบสิทธิ์ GPU ในตัวเลือกล่าม: ตั้งค่าโรงงานมอบสิทธิ์เป็น GpuDelegateFound ด้วยการเรียกใช้ addDelegateFactory() withinก่อนจะตีความ.Options()`:

    Kotlin

    val interpreterOption = InterpreterApi.Options()
    .setRuntime(TfLiteRuntime.FROM_SYSTEM_ONLY)
    .addDelegateFactory(GpuDelegateFactory())
    

    Java

    Options interpreterOption = InterpreterApi.Options()
    .setRuntime(TfLiteRuntime.FROM_SYSTEM_ONLY) .addDelegateFactory(new
    GpuDelegateFactory());
    

การย้ายข้อมูลจาก TensorFlow Lite แบบสแตนด์อโลน

หากคุณกำลังวางแผนที่จะย้ายข้อมูลแอปจาก TensorFlow Lite แบบสแตนด์อโลนไปยัง API ของบริการ Play โปรดอ่านคำแนะนำเพิ่มเติมต่อไปนี้สำหรับการอัปเดตโค้ดโปรเจ็กต์แอปของคุณ

  1. ดูส่วนข้อจำกัดในหน้านี้เพื่อให้แน่ใจว่าระบบรองรับกรณีการใช้งานของคุณ
  2. ก่อนที่จะอัปเดตโค้ด ให้ตรวจสอบประสิทธิภาพและความแม่นยำสำหรับโมเดลของคุณ โดยเฉพาะอย่างยิ่งหากคุณใช้ TensorFlow Lite เวอร์ชันเก่ากว่าเวอร์ชัน 2.1 เพื่อให้มีพื้นฐานสำหรับเปรียบเทียบกับการใช้งานใหม่
  3. หากคุณย้ายข้อมูลโค้ดทั้งหมดเพื่อใช้ Play services API สำหรับ TensorFlow Lite แล้ว คุณควรนำทรัพยากร Dependency ของไลบรารีรันไทม์ ของ TensorFlow Lite (รายการที่มี org.tensorflow:tensorflow-lite:*) ออกจากไฟล์ Build.gradle ของคุณ เพื่อที่คุณจะได้ลดขนาดแอปได้
  4. ระบุการสร้างออบเจ็กต์ new Interpreter ทั้งหมดในโค้ดของคุณ และแก้ไขแต่ละรายการเพื่อให้ใช้การเรียกใช้ API ของ Mediation .create() TfLite.initialize ใหม่เป็นแบบไม่พร้อมกัน ซึ่งหมายความว่าในกรณีส่วนใหญ่ ฟีเจอร์ดังกล่าวไม่ใช่การแทนที่แบบดรอปอิน กล่าวคือ คุณต้องลงทะเบียน Listener เมื่อการโทรเสร็จสิ้น ดูข้อมูลโค้ดในโค้ดขั้นตอนที่ 3
  5. เพิ่ม import org.tensorflow.lite.InterpreterApi; และ import org.tensorflow.lite.InterpreterApi.Options.TfLiteRuntime; ลงในไฟล์ต้นฉบับโดยใช้คลาส org.tensorflow.lite.Interpreter หรือ org.tensorflow.lite.InterpreterApi
  6. หากการเรียกผลลัพธ์ไปยัง InterpreterApi.create() มีอาร์กิวเมนต์เดียว ให้เพิ่ม new InterpreterApi.Options() ต่อท้ายรายการอาร์กิวเมนต์
  7. เพิ่ม .setRuntime(TfLiteRuntime.FROM_SYSTEM_ONLY) ต่อท้ายอาร์กิวเมนต์สุดท้ายของการเรียกไปยัง InterpreterApi.create()
  8. แทนที่คลาส org.tensorflow.lite.Interpreter อื่นๆ ทั้งหมดด้วย org.tensorflow.lite.InterpreterApi

หากต้องการใช้ TensorFlow Lite แบบเดี่ยวและ API ของบริการ Play ควบคู่กัน คุณต้องใช้ TensorFlow Lite 2.9 (หรือใหม่กว่า) TensorFlow Lite 2.8 และเวอร์ชันก่อนหน้าจะใช้ไม่ได้กับเวอร์ชัน API ของบริการ Play