画像処理装置(GPU)を使用して機械学習(ML)モデルを実行すると、ML 対応アプリケーションのパフォーマンスとユーザー エクスペリエンスが大幅に向上します。Android デバイスでは、デリゲートと次のいずれかの API を有効にできます。
- Interpreter API - このガイド
- ネイティブ(C/C++)API - ガイド
このページでは、Interpreter API を使用して Android アプリで TensorFlow Lite モデルの GPU アクセラレーションを有効にする方法について説明します。ベスト プラクティスや高度な手法など、TensorFlow Lite 用の GPU デリゲートの使用について詳しくは、GPU デリゲートのページをご覧ください。
Google Play 開発者サービスで TensorFlow Lite で GPU を使用する
TensorFlow Lite インタープリタ API は、ML アプリケーションを構築するための汎用 API のセットを提供します。このセクションでは、Google Play 開発者サービスの TensorFlow Lite で、これらの API で GPU アクセラレータ デリゲートを使用する方法について説明します。
Android で TensorFlow Lite を使用する場合には、Google Play 開発者サービスによる TensorFlow Lite をおすすめします。アプリが Google Play を実行していないデバイスをターゲットとしている場合は、インタープリタ API とスタンドアロン TensorFlow Lite を備えた GPU セクションをご覧ください。
プロジェクトの依存関係を追加する(.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 デリゲートへのアクセスを有効にするには、アプリの build.gradle
ファイルに com.google.android.gms:play-services-tflite-gpu
を追加します。
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 の ML モデル バインディングでも使用できます。詳細については、メタデータを使用してモデル インターフェースを生成するをご覧ください。
スタンドアロン TensorFlow Lite で GPU を使用する
アプリが Google Play を実行していないデバイスをターゲットにしている場合は、アプリに GPU デリゲートをバンドルして、TensorFlow Lite のスタンドアロン バージョンで使用できます。
プロジェクトの依存関係を追加する
GPU デリゲートへのアクセスを有効にするには、アプリの build.gradle
ファイルに org.tensorflow:tensorflow-lite-gpu-delegate-plugin
を追加します。
dependencies {
...
implementation 'org.tensorflow:tensorflow-lite'
implementation 'org.tensorflow:tensorflow-lite-gpu-delegate-plugin'
}
GPU アクセラレーションを有効にする
次に、TfLiteDelegate
を使用して GPU で TensorFlow Lite を実行します。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);
量子化モデル
Android GPU デリゲート ライブラリは、デフォルトで量子化モデルをサポートしています。GPU デリゲートで量子化モデルを使用するために、コードを変更する必要はありません。次のセクションでは、テストや試験運用版の目的で量子化サポートを無効にする方法について説明します。
量子化モデルのサポートを無効にする
次のコードは、量子化モデルのサポートを無効にする方法を示しています。
Java
GpuDelegate delegate = new GpuDelegate(new GpuDelegate.Options().setQuantizedModelsAllowed(false)); Interpreter.Options options = (new Interpreter.Options()).addDelegate(delegate);
GPU アクセラレーションを使用した量子化モデルの実行の詳細については、GPU デリゲートの概要をご覧ください。