グラフィックス プロセッシング ユニット (GPU) を使用して機械学習 (ML) モデルを実行すると、ML 対応アプリケーションのパフォーマンスとユーザー エクスペリエンスを大幅に向上させることができます。 Android デバイスでは、次のことを有効にできます。
デリゲートと次の API のいずれか:
このページでは、インタープリター API を使用して Android アプリで TensorFlow Lite モデルの GPU アクセラレーションを有効にする方法について説明します。 TensorFlow Lite の GPU デリゲートの使用に関する詳細 (ベスト プラクティスや高度なテクニックなど) については、 GPU デリゲートのページを参照してください。
Google Play サービスで TensorFlow Lite で GPU を使用する
TensorFlow Lite Java/Kotlinインタープリター API は、機械学習アプリケーションを構築するための汎用 API のセットを提供します。このセクションでは、Google Play サービスで TensorFlow Lite を使用してこれらの API で GPU アクセラレータ デリゲートを使用する方法について説明します。
Google Play サービスを使用した TensorFlow Lite は、 Android で TensorFlow Lite を使用するための推奨パスです。アプリケーションが Google Play を実行していないデバイスをターゲットにしている場合は、インタープリタ API とスタンドアロン TensorFlow Lite を備えた GPUセクションを参照してください。
プロジェクトの依存関係を追加する
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 を初期化します。
コトリン
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()); });
最後に、 InterpreterApi.Options
を通じてGpuDelegateFactory
を渡すインタープリターを初期化できます。
コトリン
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 デリゲートは、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 では、 Interpreter.Options
を通じてGpuDelegate
を指定できます。
コトリン
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 デリゲートの概要」を参照してください。