Việc sử dụng bộ xử lý đồ họa (GPU) để chạy mô hình máy học (ML) có thể cải thiện đáng kể hiệu suất và trải nghiệm người dùng của các ứng dụng hỗ trợ ML của bạn. Trên thiết bị Android, bạn có thể bật
ủy quyền và một trong các API sau:
- API trình thông dịch Java/Kotlin - hướng dẫn này
- API thư viện tác vụ - hướng dẫn
- API gốc (C/C++) - hướng dẫn
Trang này mô tả cách bật khả năng tăng tốc GPU cho các mẫu TensorFlow Lite trong ứng dụng Android bằng API Trình thông dịch. Để biết thêm thông tin về cách sử dụng đại biểu GPU cho TensorFlow Lite, bao gồm các phương pháp hay nhất và kỹ thuật nâng cao, hãy xem trang đại biểu GPU .
Sử dụng GPU với TensorFlow Lite với các dịch vụ của Google Play
API TensorFlow Lite Java/Kotlin Interpreter cung cấp một bộ API có mục đích chung để xây dựng các ứng dụng máy học. Phần này mô tả cách sử dụng ủy quyền bộ tăng tốc GPU với các API này với TensorFlow Lite với các dịch vụ Google Play.
TensorFlow Lite với dịch vụ Google Play là đường dẫn được khuyến nghị để sử dụng TensorFlow Lite trên Android. Nếu ứng dụng của bạn đang nhắm mục tiêu các thiết bị không chạy Google Play, hãy xem GPU có API Phiên dịch và phần TensorFlow Lite độc lập .
Thêm phụ thuộc dự án
Để cho phép quyền truy cập vào đại biểu GPU, hãy thêm com.google.android.gms:play-services-tflite-gpu
vào tệp build.gradle
của ứng dụng của bạn:
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'
}
Bật tăng tốc GPU
Sau đó khởi chạy TensorFlow Lite với các dịch vụ Google Play có hỗ trợ GPU:
Kotlin
val useGpuTask = TfLiteGpu.isGpuDelegateAvailable(context) val interpreterTask = useGpuTask.continueWith { useGpuTask -> TfLite.initialize(context, TfLiteInitializationOptions.builder() .setEnableGpuDelegateSupport(useGpuTask.result) .build()) }
Java
TaskuseGpuTask = TfLiteGpu.isGpuDelegateAvailable(context); Task interpreterOptionsTask = useGpuTask.continueWith({ task -> TfLite.initialize(context, TfLiteInitializationOptions.builder() .setEnableGpuDelegateSupport(true) .build()); });
Cuối cùng, bạn có thể khởi tạo trình thông dịch truyền GpuDelegateFactory
thông qua 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);
Đại biểu GPU cũng có thể được sử dụng với liên kết mô hình ML trong Android Studio. Để biết thêm thông tin, hãy xem Tạo giao diện mô hình bằng siêu dữ liệu .
Sử dụng GPU với TensorFlow Lite độc lập
Nếu ứng dụng của bạn nhắm mục tiêu đến các thiết bị không chạy Google Play, bạn có thể đóng gói đại biểu GPU cho ứng dụng của mình và sử dụng nó với phiên bản độc lập của TensorFlow Lite.
Thêm phụ thuộc dự án
Để cho phép quyền truy cập vào đại biểu GPU, hãy thêm org.tensorflow:tensorflow-lite-gpu-delegate-plugin
vào tệp build.gradle
của ứng dụng của bạn:
dependencies {
...
implementation 'org.tensorflow:tensorflow-lite'
implementation 'org.tensorflow:tensorflow-lite-gpu-delegate-plugin'
}
Bật tăng tốc GPU
Sau đó chạy TensorFlow Lite trên GPU bằng TfLiteDelegate
. Trong Java, bạn có thể chỉ định GpuDelegate
thông qua 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);
Mô hình lượng tử hóa
Theo mặc định, thư viện đại biểu GPU của Android hỗ trợ các mô hình lượng tử hóa. Bạn không cần phải thực hiện bất kỳ thay đổi mã nào để sử dụng các mô hình lượng tử hóa với đại biểu GPU. Phần sau đây giải thích cách tắt hỗ trợ lượng tử hóa cho mục đích thử nghiệm hoặc thử nghiệm.
Tắt hỗ trợ mô hình lượng tử hóa
Đoạn mã sau cho biết cách tắt hỗ trợ cho các mô hình lượng tử hóa.
Java
GpuDelegate delegate = new GpuDelegate(new GpuDelegate.Options().setQuantizedModelsAllowed(false)); Interpreter.Options options = (new Interpreter.Options()).addDelegate(delegate);
Để biết thêm thông tin về cách chạy các mô hình lượng tử hóa với khả năng tăng tốc GPU, hãy xem tổng quan về đại biểu GPU .