GPU 加速委派搭配 Java/Kotlin Interpreter API

使用圖形處理單元 (GPU) 執行機器學習 (ML) 模型,可大幅提升啟用 ML 功能的應用程式效能和使用者體驗。在 Android 裝置上,您可以啟用

委派和下列其中一個 API

  • Java/Kotlin Interpreter API - 本指南
  • Task library API - 指南
  • 原生 (C/C++) API - 指南

本頁說明如何在 Android 應用程式中使用 Interpreter API 為 TensorFlow Lite 模型啟用 GPU 加速。如要進一步瞭解如何將 GPU 委派用於 TensorFlow Lite (包括最佳做法和進階技巧),請參閱 GPU 委派頁面。

搭配 Google Play 服務將 GPU 與 TensorFlow Lite 搭配使用

TensorFlow Lite Java/Kotlin Interpreter API 提供一組通用 API,用於建構機器學習應用程式。本節說明如何搭配 Google Play 服務,將 GPU 加速器委派與這些 API 和 TensorFlow Lite 搭配使用。

搭配 Google Play 服務的 TensorFlow Lite 是在 Android 上使用 TensorFlow Lite 的建議路徑。如果您的應用程式目標裝置未執行 Google Play,請參閱「搭配 Interpreter API 和獨立 TensorFlow Lite 使用 GPU」一節。

新增專案依附元件

如要啟用 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 useGpuTask = TfLiteGpu.isGpuDelegateAvailable(context);

    Task 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 委派的存取權,請將 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 委派總覽。