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

LiteRT ในบริการ Google Play ยังสามารถเข้าถึงได้โดยใช้ Java API ใน นอกเหนือจาก Native API โดยเฉพาะอย่างยิ่ง LiteRT ใน Google Play พร้อมให้บริการผ่านล่าม LiteRT API

การใช้ Interpreter API

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

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

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

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

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

เริ่มต้นคอมโพเนนต์ LiteRT ของ API บริการ Google Play ก่อนใช้ LiteRT API

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

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

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

ผู้รับมอบสิทธิ์ GPU ให้บริการผ่านบริการ Google Play และจะโหลดแบบไดนามิก เช่นเดียวกับเวอร์ชันบริการ Google Play Interpreter 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 กับ Interpreter 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 ในตัวเลือกล่าม: ตั้งค่าโรงงานที่มอบสิทธิ์เป็น GpuDelegateFactory โดยการเรียกใช้ addDelegateFactory() withinInterpreterApi.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());
    

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

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

  1. ตรวจสอบส่วนข้อจำกัดในหน้านี้เพื่อให้แน่ใจว่า รองรับ Use Case
  2. ก่อนที่จะอัปเดตโค้ด ให้ตรวจสอบประสิทธิภาพและความแม่นยำสำหรับ โดยเฉพาะหากคุณใช้ LiteRT เวอร์ชันก่อนหน้านี้ กว่าเวอร์ชัน 2.1 คุณจึงมีเกณฑ์พื้นฐานที่จะเปรียบเทียบกับเวอร์ชันใหม่ การใช้งานของคุณ
  3. หากคุณย้ายโค้ดทั้งหมดไปใช้ API ของบริการ Google Play แล้ว LiteRT คุณควรนำ LiteRT รันไทม์ออก ทรัพยากร Dependency ของไลบรารี (รายการที่มี org.tensorflow:tensorflow-lite:*) จากbuild.gradle ของคุณ เพื่อลดขนาดของแอป
  4. ระบุรายการทั้งหมดของการสร้างออบเจ็กต์ new Interpreter ในโค้ด และแก้ไขแต่ละรายการเพื่อให้ใช้การเรียก InterpreterApi.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

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