นอกจากนี้ยังเข้าถึง LiteRT ในบริการ Google Play ได้โดยใช้ Java API ซึ่งใช้ได้จากโค้ด Java หรือ Kotlin นอกเหนือจาก Native API โดยเฉพาะ LiteRT ในบริการของ Google Play พร้อมให้บริการผ่าน LiteRT Interpreter API
การใช้ Interpreter API
LiteRT Interpreter API ซึ่งจัดทำโดยรันไทม์ TensorFlow มีอินเทอร์เฟซอเนกประสงค์สำหรับการสร้างและเรียกใช้โมเดล ML ทำตามขั้นตอนต่อไปนี้เพื่อเรียกใช้การอนุมานด้วย Interpreter API โดยใช้ TensorFlow Lite ในรันไทม์ของบริการ Google Play
1. เพิ่มการพึ่งพาโปรเจ็กต์
เพิ่มทรัพยากร Dependency ต่อไปนี้ลงในโค้ดโปรเจ็กต์แอปเพื่อเข้าถึง API ของบริการ Google Play สำหรับ LiteRT
dependencies {
...
// LiteRT dependencies for Google Play services
implementation 'com.google.android.gms:play-services-tflite-java:16.1.0'
// Optional: include LiteRT Support Library
implementation 'com.google.android.gms:play-services-tflite-support:16.1.0'
...
}
2. เพิ่มการเริ่มต้น LiteRT
เริ่มต้นคอมโพเนนต์ LiteRT ของ Google Play Services API ก่อนใช้ LiteRT API โดยทำดังนี้
Kotlin
val initializeTask: Task<Void> by lazy { TfLite.initialize(this) }
Java
Task<Void> initializeTask = TfLite.initialize(context);
3. สร้าง Interpreter และตั้งค่าตัวเลือกเวลาเรียกใช้
สร้างอินเทอร์พรีเตอร์โดยใช้ 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) คุณ ใช้ประโยชน์จากโปรเซสเซอร์เฉพาะทางเหล่านี้ได้โดยใช้ไดรเวอร์ฮาร์ดแวร์ที่เรียกว่า Delegate
ตัวแทน GPU จะให้บริการผ่านบริการ Google Play และจะโหลดแบบไดนามิกเช่นเดียวกับ API ของ Interpreter เวอร์ชันบริการ Play
การตรวจสอบความเข้ากันได้ของอุปกรณ์
อุปกรณ์บางรุ่นไม่รองรับการเร่งฮาร์ดแวร์ 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 ที่มี Interpreter API
วิธีใช้ตัวแทน GPU กับ Interpreter API
อัปเดตทรัพยากร Dependency ของโปรเจ็กต์เพื่อใช้ตัวแทน GPU จากบริการ Play ดังนี้
implementation 'com.google.android.gms:play-services-tflite-gpu:16.2.0'
เปิดใช้ตัวเลือกผู้แทน GPU ในการเริ่มต้น TFlite
Kotlin
TfLite.initialize(context, TfLiteInitializationOptions.builder() .setEnableGpuDelegateSupport(true) .build())
Java
TfLite.initialize(context, TfLiteInitializationOptions.builder() .setEnableGpuDelegateSupport(true) .build());
เปิดใช้ตัวแทน GPU ในตัวเลือก Interpreter โดยตั้งค่าโรงงานตัวแทนเป็น GpuDelegateFactory โดยเรียกใช้
addDelegateFactory() within
InterpreterApi.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 แบบสแตนด์อโลนไปยัง Play Services API โปรดอ่านคำแนะนำเพิ่มเติมต่อไปนี้สำหรับการอัปเดตโค้ดโปรเจ็กต์แอป
- โปรดอ่านส่วนข้อจำกัด เพื่อให้แน่ใจว่าระบบรองรับกรณีการใช้งานของคุณ
- ก่อนที่จะอัปเดตโค้ด เราขอแนะนำให้ตรวจสอบประสิทธิภาพและความแม่นยำของโมเดล โดยเฉพาะอย่างยิ่งหากคุณใช้ LiteRT (TF Lite) เวอร์ชันก่อนหน้า 2.1 เพื่อให้มีพื้นฐานในการเปรียบเทียบกับการติดตั้งใช้งานใหม่
- หากย้ายข้อมูลโค้ดทั้งหมดเพื่อใช้ Play Services API สำหรับ LiteRT แล้ว คุณควรนำทรัพยากร Dependency ของไลบรารี
รันไทม์ LiteRT ที่มีอยู่ (รายการที่มี
org.tensorflow:tensorflow-lite:*
) ออกจากไฟล์ build.gradle เพื่อลดขนาดแอป - ระบุการสร้างออบเจ็กต์
new Interpreter
ทั้งหมดในโค้ด และแก้ไขแต่ละรายการเพื่อให้ใช้การเรียกInterpreterApi.create()
TfLite.initialize
ใหม่เป็นแบบอะซิงโครนัส ซึ่งหมายความว่าในกรณีส่วนใหญ่จะไม่ใช่ การแทนที่แบบดรอปอิน คุณต้องลงทะเบียน Listener เมื่อการเรียก เสร็จสมบูรณ์ ดูข้อมูลโค้ดในโค้ดขั้นตอนที่ 3 - เพิ่ม
import org.tensorflow.lite.InterpreterApi;
และimport org.tensorflow.lite.InterpreterApi.Options.TfLiteRuntime;
ลงในไฟล์แหล่งข้อมูล โดยใช้คลาสorg.tensorflow.lite.Interpreter
หรือorg.tensorflow.lite.InterpreterApi
- หากการเรียกใช้
InterpreterApi.create()
ที่ได้มีอาร์กิวเมนต์เพียงรายการเดียว ให้ต่อท้ายnew InterpreterApi.Options()
ไปยังรายการอาร์กิวเมนต์ - ต่อท้าย
.setRuntime(TfLiteRuntime.FROM_SYSTEM_ONLY)
ไปยังอาร์กิวเมนต์สุดท้ายของการเรียกใช้InterpreterApi.create()
- แทนที่อินสแตนซ์อื่นๆ ทั้งหมดของคลาส
org.tensorflow.lite.Interpreter
ด้วยorg.tensorflow.lite.InterpreterApi
หากต้องการใช้ LiteRT แบบสแตนด์อโลนและ Play Services API ควบคู่กัน คุณต้องใช้ LiteRT (TF Lite) เวอร์ชัน 2.9 ขึ้นไป LiteRT (TF Lite) เวอร์ชัน 2.8 และเวอร์ชันก่อนหน้าใช้ร่วมกับ API ของบริการ Google Play ไม่ได้