การใช้หน่วยประมวลผลกราฟิก (GPU) เพื่อเรียกใช้โมเดลแมชชีนเลิร์นนิง (ML) จะช่วยปรับปรุงประสิทธิภาพและประสบการณ์ของผู้ใช้ในแอปพลิเคชันที่เปิดใช้ ML ได้อย่างมาก ในอุปกรณ์ Android คุณสามารถเปิดใช้ผู้รับมอบสิทธิ์และ API อย่างใดอย่างหนึ่งต่อไปนี้
- Debuger API - คู่มือนี้
- Native (C/C++) API - คู่มือ
หน้านี้อธิบายวิธีเปิดใช้การเร่ง GPU สำหรับโมเดล TensorFlow Lite ในแอป Android ที่ใช้ Reportinger API ดูข้อมูลเพิ่มเติมเกี่ยวกับการใช้การมอบสิทธิ์ GPU สำหรับ TensorFlow Lite รวมถึงแนวทางปฏิบัติแนะนำและเทคนิคขั้นสูงได้ที่หน้าผู้รับมอบสิทธิ์ GPU
ใช้ GPU กับ TensorFlow Lite กับบริการ Google Play
Translation API ของ TensorFlow Lite มีชุด API สำหรับวัตถุประสงค์ทั่วไปสำหรับการสร้างแอปพลิเคชันแมชชีนเลิร์นนิง ส่วนนี้อธิบายวิธีใช้การมอบสิทธิ์ตัวเร่ง GPU กับ API เหล่านี้กับ TensorFlow Lite กับบริการ Google Play
TensorFlow Lite กับบริการ Google Play เป็นเส้นทางที่แนะนำในการใช้ TensorFlow Lite บน Android หากแอปพลิเคชันกำหนดเป้าหมายเป็นอุปกรณ์ที่ไม่ได้ใช้ Google Play โปรดดูส่วน GPU พร้อม Translation API และ TensorFlow Lite แบบสแตนด์อโลน
เพิ่มทรัพยากร Dependency ของโปรเจ็กต์ (มีแคตตาล็อกเวอร์ชัน .toml)
- อัปเดตไฟล์
libs.versions.toml
ของโปรเจ็กต์
[libraries]
...
tflite-gpu = { module = "org.tensorflow:tensorflow-lite-gpu", version = "2.X.Y" }
tflite-gpu-api = { module = "org.tensorflow:tensorflow-lite-gpu-api", version = "2.X.Y" }
...
- เพิ่มทรัพยากร Dependency ของโปรเจ็กต์ใน
build.gradle.kts
ของแอป
dependencies {
...
implementation(libraries.tflite.gpu)
implementation(libraries.tflite.gpu.api)
...
}
เพิ่มทรัพยากร Dependency ของโปรเจ็กต์
หากต้องการเปิดใช้การเข้าถึงการมอบสิทธิ์ GPU ให้เพิ่ม com.google.android.gms:play-services-tflite-gpu
ลงในไฟล์ build.gradle
ของแอป:
dependencies {
...
implementation 'com.google.android.gms:play-services-tflite-java:16.0.1'
implementation 'com.google.android.gms:play-services-tflite-gpu:16.1.0'
}
เปิดใช้การเร่ง GPU
จากนั้นเริ่มต้น TensorFlow Lite ด้วยบริการ Google Play ด้วยการรองรับ GPU ดังนี้
Kotlin
val useGpuTask = TfLiteGpu.isGpuDelegateAvailable(context) val interpreterTask = useGpuTask.continueWith { useGpuTask -> TfLite.initialize(context, TfLiteInitializationOptions.builder() .setEnableGpuDelegateSupport(useGpuTask.result) .build()) }
Java
Task<boolean> useGpuTask = TfLiteGpu.isGpuDelegateAvailable(context); Task<Options> interpreterOptionsTask = useGpuTask.continueWith({ task -> TfLite.initialize(context, TfLiteInitializationOptions.builder() .setEnableGpuDelegateSupport(true) .build()); });
คุณจะเริ่มต้นโปรแกรมล่ามที่ส่งผ่าน GpuDelegateFactory
ผ่าน InterpreterApi.Options
ได้โดยทำดังนี้
Kotlin
val options = InterpreterApi.Options() .setRuntime(TfLiteRuntime.FROM_SYSTEM_ONLY) .addDelegateFactory(GpuDelegateFactory()) val interpreter = InterpreterApi(model, options) // Run inference writeToInput(input) interpreter.run(input, output) readFromOutput(output)
Java
Options options = InterpreterApi.Options() .setRuntime(TfLiteRuntime.FROM_SYSTEM_ONLY) .addDelegateFactory(new GpuDelegateFactory()); Interpreter interpreter = new InterpreterApi(model, options); // Run inference writeToInput(input); interpreter.run(input, output); readFromOutput(output);
นอกจากนี้ยังใช้การมอบสิทธิ์ GPU กับการเชื่อมโยงโมเดล ML ใน Android Studio ได้ด้วย ดูข้อมูลเพิ่มเติมได้ที่สร้างอินเทอร์เฟซของโมเดลโดยใช้ข้อมูลเมตา
ใช้ GPU กับ TensorFlow Lite แบบสแตนด์อโลน
หากแอปพลิเคชันของคุณกำหนดเป้าหมายเป็นอุปกรณ์ที่ไม่ได้ใช้ Google Play คุณอาจรวมการมอบสิทธิ์ GPU เข้ากับแอปพลิเคชันและใช้ร่วมกับ TensorFlow Lite เวอร์ชันสแตนด์อโลนได้
เพิ่มทรัพยากร Dependency ของโปรเจ็กต์
หากต้องการเปิดใช้การเข้าถึงการมอบสิทธิ์ GPU ให้เพิ่ม org.tensorflow:tensorflow-lite-gpu-delegate-plugin
ลงในไฟล์ build.gradle
ของแอป:
dependencies {
...
implementation 'org.tensorflow:tensorflow-lite'
implementation 'org.tensorflow:tensorflow-lite-gpu-delegate-plugin'
}
เปิดใช้การเร่ง GPU
จากนั้นเรียกใช้ TensorFlow Lite บน GPU ด้วย TfLiteDelegate
ใน Java คุณสามารถระบุ GpuDelegate
ถึง Interpreter.Options
ได้
Kotlin
import org.tensorflow.lite.Interpreter import org.tensorflow.lite.gpu.CompatibilityList import org.tensorflow.lite.gpu.GpuDelegate val compatList = CompatibilityList() val options = Interpreter.Options().apply{ if(compatList.isDelegateSupportedOnThisDevice){ // if the device has a supported GPU, add the GPU delegate val delegateOptions = compatList.bestOptionsForThisDevice this.addDelegate(GpuDelegate(delegateOptions)) } else { // if the GPU is not supported, run on 4 threads this.setNumThreads(4) } } val interpreter = Interpreter(model, options) // Run inference writeToInput(input) interpreter.run(input, output) readFromOutput(output)
Java
import org.tensorflow.lite.Interpreter; import org.tensorflow.lite.gpu.CompatibilityList; import org.tensorflow.lite.gpu.GpuDelegate; // Initialize interpreter with GPU delegate Interpreter.Options options = new Interpreter.Options(); CompatibilityList compatList = CompatibilityList(); if(compatList.isDelegateSupportedOnThisDevice()){ // if the device has a supported GPU, add the GPU delegate GpuDelegate.Options delegateOptions = compatList.getBestOptionsForThisDevice(); GpuDelegate gpuDelegate = new GpuDelegate(delegateOptions); options.addDelegate(gpuDelegate); } else { // if the GPU is not supported, run on 4 threads options.setNumThreads(4); } Interpreter interpreter = new Interpreter(model, options); // Run inference writeToInput(input); interpreter.run(input, output); readFromOutput(output);
โมเดลที่แบ่งปริมาณ
ไลบรารีการมอบสิทธิ์ GPU ของ Android จะรองรับโมเดลที่เล็กลงโดยค่าเริ่มต้น คุณไม่ต้องเปลี่ยนแปลงโค้ดใดๆ เพื่อใช้โมเดลที่วัดปริมาณด้วยตัวมอบสิทธิ์ GPU ส่วนต่อไปนี้จะอธิบายวิธีปิดใช้การสนับสนุนเชิงปริมาณสำหรับการทดสอบหรือการทดสอบ
ปิดใช้การสนับสนุนโมเดลที่เล็กลง
โค้ดต่อไปนี้แสดงวิธีปิดใช้การรองรับโมเดลที่ปรับขนาดแล้ว
Java
GpuDelegate delegate = new GpuDelegate(new GpuDelegate.Options().setQuantizedModelsAllowed(false)); Interpreter.Options options = (new Interpreter.Options()).addDelegate(delegate);
ดูข้อมูลเพิ่มเติมเกี่ยวกับการเรียกใช้โมเดลที่วัดปริมาณด้วยการเร่ง GPU ได้ที่ภาพรวมการมอบสิทธิ์ GPU