使用 Explainer API 的 GPU 加速代理

使用图形处理单元 (GPU) 运行机器学习 (ML) 模型可以显著提高支持机器学习的应用的性能和用户体验。在 Android 设备上,您可以启用委托和以下 API 之一:

  • Explainer API - 本指南
  • 原生 (C/C++) API - 指南

本页介绍了如何使用 Explainer API 在 Android 应用中为 TensorFlow Lite 模型启用 GPU 加速。如需详细了解如何为 TensorFlow Lite 使用 GPU 代理,包括最佳实践和高级技术,请参阅 GPU 代理页面。

将 GPU 与 TensorFlow Lite 与 Google Play 服务搭配使用

TensorFlow Lite Explainer API 提供了一组用于构建机器学习应用的通用 API。本部分介绍了如何将 GPU 加速器代理与这些 API 与包含 Google Play 服务的 TensorFlow Lite 搭配使用。

TensorFlow Lite 与 Google Play 服务是在 Android 上使用 TensorFlow Lite 的推荐途径。如果您的应用以未运行 Google Play 的设备为目标平台,请参阅包含 Explainer API 和独立 TensorFlow Lite 的 GPU 部分。

添加项目依赖项(使用 .toml 版本目录)

  1. 更新项目的 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" }
...
  1. 在应用的 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 代理概览。