使用圖形處理器 (GPU) 執行機器學習 (ML) 模型,可大幅提升支援機器學習的應用程式效能和使用者體驗。在 Android 裝置上,您可以啟用委派和下列其中一個 API:
- 解譯 API - 本指南
- Native (C/C++) API - 指南
本頁面說明如何使用 translateer API,在 Android 應用程式中為 TensorFlow Lite 模型啟用 GPU 加速功能。如要進一步瞭解如何使用 TensorFlow Lite 的 GPU 委派功能 (包括最佳做法和進階技術),請參閱 GPU 委派頁面。
搭配 Google Play 服務使用 GPU 搭配 TensorFlow Lite
TensorFlow Lite Understandinger API 提供一組一般用途 API,可用於建構機器學習應用程式。本節說明如何搭配 Google Play 服務,將這些 API 加速器與 TensorFlow Lite 搭配使用。
如要在 Android 上使用 TensorFlow Lite,建議路徑搭配 Google Play 服務使用 TensorFlow Lite。如果您的應用程式指定的裝置並非執行 Google Play,請參閱「GPU with Understandinger API 和獨立的 TensorFlow Lite」一節。
新增專案依附元件 (含 .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" }
...
- 在應用程式的
build.gradle.kts
中新增專案依附元件
dependencies {
...
implementation(libraries.tflite.gpu)
implementation(libraries.tflite.gpu.api)
...
}
新增專案依附元件
如要啟用 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 加速功能
接著,請使用支援 GPU 的 Google Play 服務初始化 TensorFlow Lite:
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()); });
最後,您可以透過 InterpreterApi.Options
傳遞 GpuDelegateFactory
來初始化解譯器:
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 委派也可以與 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 加速功能
然後使用 TfLiteDelegate
在 GPU 上執行 TensorFlow Lite。在 Java 中,您可以透過 Interpreter.Options
指定 GpuDelegate
。
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);
量化模型
Android GPU 委派程式庫預設支援量化模型。您不必變更任何程式碼,即可搭配 GPU 委派使用量化模型。下一節說明如何停用量化支援,以用於測試或實驗。
停用量化模型支援
下列程式碼說明如何停用對量化模型的支援。
Java
GpuDelegate delegate = new GpuDelegate(new GpuDelegate.Options().setQuantizedModelsAllowed(false)); Interpreter.Options options = (new Interpreter.Options()).addDelegate(delegate);
如要進一步瞭解如何使用 GPU 加速功能執行量化模型,請參閱 GPU 委派總覽。