การใช้หน่วยประมวลผลกราฟิก (GPU) เพื่อรันโมเดล Machine Learning (ML) ของคุณสามารถปรับปรุงประสิทธิภาพและประสบการณ์ผู้ใช้ของแอปพลิเคชันที่เปิดใช้งาน ML ได้อย่างมาก บนอุปกรณ์ Android คุณสามารถเปิดใช้งานได้
ผู้รับมอบสิทธิ์ และหนึ่งใน API ต่อไปนี้:
หน้านี้อธิบายวิธีเปิดใช้งานการเร่งความเร็ว GPU สำหรับรุ่น TensorFlow Lite ในแอป Android โดยใช้ Interpreter API หากต้องการข้อมูลเพิ่มเติมเกี่ยวกับการใช้ตัวแทน GPU สำหรับ TensorFlow Lite รวมถึงแนวทางปฏิบัติที่ดีที่สุดและเทคนิคขั้นสูง โปรดดูที่หน้า ตัวแทน GPU
ใช้ GPU กับ TensorFlow Lite กับบริการ Google Play
TensorFlow Lite Java/Kotlin Interpreter API มอบชุด API วัตถุประสงค์ทั่วไปสำหรับการสร้างแอปพลิเคชันแมชชีนเลิร์นนิง ส่วนนี้จะอธิบายวิธีใช้ตัวแทนตัวเร่งความเร็ว GPU กับ API เหล่านี้ด้วย TensorFlow Lite กับบริการ Google Play
TensorFlow Lite พร้อมบริการ Google Play เป็นเส้นทางที่แนะนำในการใช้ TensorFlow Lite บน Android หากแอปพลิเคชันของคุณกำหนดเป้าหมายอุปกรณ์ที่ไม่ได้ใช้ Google Play โปรดดูส่วน GPU ที่มี Interpreter API และ TensorFlow Lite แบบสแตนด์อโลน
เพิ่มการพึ่งพาโครงการ
หากต้องการเปิดใช้งานการเข้าถึงตัวแทน 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:
คอตลิน
val useGpuTask = TfLiteGpu.isGpuDelegateAvailable(context) val interpreterTask = useGpuTask.continueWith { useGpuTask -> TfLite.initialize(context, TfLiteInitializationOptions.builder() .setEnableGpuDelegateSupport(useGpuTask.result) .build()) }
ชวา
TaskuseGpuTask = TfLiteGpu.isGpuDelegateAvailable(context); Task interpreterOptionsTask = useGpuTask.continueWith({ task -> TfLite.initialize(context, TfLiteInitializationOptions.builder() .setEnableGpuDelegateSupport(true) .build()); });
ในที่สุดคุณก็สามารถเริ่มต้นล่ามผ่าน GpuDelegateFactory
ผ่าน InterpreterApi.Options
:
คอตลิน
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)
ชวา
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 เวอร์ชันสแตนด์อโลนได้
เพิ่มการพึ่งพาโครงการ
หากต้องการเปิดใช้งานการเข้าถึงตัวแทน 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
คอตลิน
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)
ชวา
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);
โมเดลเชิงปริมาณ
ไลบรารีผู้รับมอบสิทธิ์ Android GPU รองรับโมเดลเชิงปริมาณตามค่าเริ่มต้น คุณไม่จำเป็นต้องทำการเปลี่ยนแปลงโค้ดใดๆ เพื่อใช้โมเดลเชิงปริมาณกับผู้แทน GPU ส่วนต่อไปนี้จะอธิบายวิธีปิดใช้งานการสนับสนุนเชิงปริมาณเพื่อการทดสอบหรือการทดลอง
ปิดใช้งานการสนับสนุนโมเดลเชิงปริมาณ
รหัสต่อไปนี้แสดงวิธี ปิดใช้งาน การสนับสนุนสำหรับโมเดลเชิงปริมาณ
ชวา
GpuDelegate delegate = new GpuDelegate(new GpuDelegate.Options().setQuantizedModelsAllowed(false)); Interpreter.Options options = (new Interpreter.Options()).addDelegate(delegate);
สำหรับข้อมูลเพิ่มเติมเกี่ยวกับการรันโมเดลเชิงปริมาณที่มีการเร่งความเร็วของ GPU โปรดดูภาพรวมของ ผู้แทน GPU