Google Play 服务 Java API 中的 LiteRT

除了原生 API 之外,您还可以使用 Java API 访问 Google Play 服务中的 LiteRT。具体而言,Google Play 服务中的 LiteRT 可通过 LiteRT 解释器 API 使用。

使用 Interpreter API

TensorFlow 运行时提供的 LiteRT 解释器 API 提供了一个通用接口,用于构建和运行机器学习模型。使用 按照以下步骤使用 TensorFlow 和 Interpreter API 运行推断: Google Play 服务运行时中的精简版。

1. 添加项目依赖项

将以下依赖项添加到应用项目代码中,以便访问适用于 LiteRT 的 Play 服务 API:

dependencies {
...
    // LiteRT dependencies for Google Play services
    implementation 'com.google.android.gms:play-services-tflite-java:16.0.1'
    // Optional: include LiteRT Support Library
    implementation 'com.google.android.gms:play-services-tflite-support:16.0.1'
...
}

2. 添加 LiteRT 初始化

在使用 LiteRT API 之前,请先初始化 Google Play 服务 API 的 LiteRT 组件:

Kotlin

val initializeTask: Task<Void> by lazy { TfLite.initialize(this) }

Java

Task<Void> initializeTask = TfLite.initialize(context);

3. 创建解释器并设置运行时选项

使用 InterpreterApi.create() 创建一个解释器,并将其配置为使用 通过调用 InterpreterApi.Options.setRuntime() 调用 Google Play 服务运行时, 如以下示例代码所示:

Kotlin

import org.tensorflow.lite.InterpreterApi
import org.tensorflow.lite.InterpreterApi.Options.TfLiteRuntime
...
private lateinit var interpreter: InterpreterApi
...
initializeTask.addOnSuccessListener {
  val interpreterOption =
    InterpreterApi.Options().setRuntime(TfLiteRuntime.FROM_SYSTEM_ONLY)
  interpreter = InterpreterApi.create(
    modelBuffer,
    interpreterOption
  )}
  .addOnFailureListener { e ->
    Log.e("Interpreter", "Cannot initialize interpreter", e)
  }

Java

import org.tensorflow.lite.InterpreterApi
import org.tensorflow.lite.InterpreterApi.Options.TfLiteRuntime
...
private InterpreterApi interpreter;
...
initializeTask.addOnSuccessListener(a -> {
    interpreter = InterpreterApi.create(modelBuffer,
      new InterpreterApi.Options().setRuntime(TfLiteRuntime.FROM_SYSTEM_ONLY));
  })
  .addOnFailureListener(e -> {
    Log.e("Interpreter", String.format("Cannot initialize interpreter: %s",
          e.getMessage()));
  });

您应使用上述实现,因为它可以避免阻塞 Android 界面线程。如果您需要更密切地管理线程执行, 可以添加 Tasks.await() 调用来创建解释器:

Kotlin

import androidx.lifecycle.lifecycleScope
...
lifecycleScope.launchWhenStarted { // uses coroutine
  initializeTask.await()
}

Java

@BackgroundThread
InterpreterApi initializeInterpreter() {
    Tasks.await(initializeTask);
    return InterpreterApi.create(...);
}

4. 运行推理

使用您创建的 interpreter 对象,调用 run() 方法来生成 进行推理。

Kotlin

interpreter.run(inputBuffer, outputBuffer)

Java

interpreter.run(inputBuffer, outputBuffer);

硬件加速

使用 LiteRT,您可以使用 专用硬件处理器,例如图形处理单元 (GPU)。您可以使用名为代理的硬件驱动程序来充分利用这些专用处理器。

GPU 代理通过 Google Play 服务提供,并像 Play 服务版本的 Interpreter API 一样动态加载。

检查设备兼容性

并非所有设备都支持使用 TFLite 的 GPU 硬件加速。为了减少错误和潜在的崩溃,请使用 TfLiteGpu.isGpuDelegateAvailable 方法检查设备是否与 GPU 代理兼容。

使用此方法可确认设备是否与 GPU 兼容,并在 GPU 不受支持时使用 CPU 作为后备。

useGpuTask = TfLiteGpu.isGpuDelegateAvailable(context)

有了 useGpuTask 等变量后,您就可以使用它来确定设备是否使用 GPU 代理。

Kotlin

val interpreterTask = useGpuTask.continueWith { task ->
  val interpreterOptions = InterpreterApi.Options()
      .setRuntime(TfLiteRuntime.FROM_SYSTEM_ONLY)
  if (task.result) {
      interpreterOptions.addDelegateFactory(GpuDelegateFactory())
  }
  InterpreterApi.create(FileUtil.loadMappedFile(context, MODEL_PATH), interpreterOptions)
}
    

Java

Task<InterpreterApi.Options> interpreterOptionsTask = useGpuTask.continueWith({ task ->
  InterpreterApi.Options options =
      new InterpreterApi.Options().setRuntime(TfLiteRuntime.FROM_SYSTEM_ONLY);
  if (task.getResult()) {
     options.addDelegateFactory(new GpuDelegateFactory());
  }
  return options;
});
    

GPU 和解释器 API

如需将 GPU 代理与 Interpreter API 搭配使用,请执行以下操作:

  1. 更新项目依赖项以使用 Play 服务中的 GPU 代理:

    implementation 'com.google.android.gms:play-services-tflite-gpu:16.1.0'
    
  2. 在 TFlite 初始化中启用 GPU 代理选项:

    Kotlin

    TfLite.initialize(context,
      TfLiteInitializationOptions.builder()
        .setEnableGpuDelegateSupport(true)
        .build())
    

    Java

    TfLite.initialize(context,
      TfLiteInitializationOptions.builder()
        .setEnableGpuDelegateSupport(true)
        .build());
    
  3. 在解释器选项中启用 GPU 委托:将委托工厂设置为 GpuDelegateFactory:调用 addDelegateFactory() withinInterpreterApi.Options():

    Kotlin

    val interpreterOption = InterpreterApi.Options()
      .setRuntime(TfLiteRuntime.FROM_SYSTEM_ONLY)
      .addDelegateFactory(GpuDelegateFactory())
    

    Java

    Options interpreterOption = InterpreterApi.Options()
      .setRuntime(TfLiteRuntime.FROM_SYSTEM_ONLY)
      .addDelegateFactory(new GpuDelegateFactory());
    

从独立 LiteRT 迁移

如果您打算将应用从独立 LiteRT 迁移到 Play 服务 API,请参阅以下额外指南,了解如何更新应用项目代码:

  1. 请查看本页的限制部分,确保您的用例受支持。
  2. 在更新代码之前,请对您的代码执行性能和准确性检查 尤其是在您使用 因此,您有一个基准,可与新版本 实施。
  3. 如果您已将所有代码迁移到针对 LiteRT,则应移除现有的 LiteRT 运行时 library依赖项(包含 org.tensorflow:tensorflow-lite:*) 以便缩减应用大小
  4. 在代码中找出所有 new Interpreter 对象创建操作,并修改每个操作,使其使用 InterpreterApi.create() 调用。通过 新的 TfLite.initialize 是异步的,这意味着在大多数情况下,它不是 简易替换:您必须注册一个监听器来监听 。请参阅第 3 步代码中的代码段。
  5. import org.tensorflow.lite.InterpreterApi;import org.tensorflow.lite.InterpreterApi.Options.TfLiteRuntime; 添加到任何来源 使用 org.tensorflow.lite.Interpreterorg.tensorflow.lite.InterpreterApi 类。
  6. 如果对 InterpreterApi.create() 的任何一个调用仅包含 单个参数,请将 new InterpreterApi.Options() 附加到参数列表。
  7. .setRuntime(TfLiteRuntime.FROM_SYSTEM_ONLY) 附加到对 InterpreterApi.create() 的任何调用的最后一个实参。
  8. org.tensorflow.lite.Interpreter 类的所有其他出现情况替换为 org.tensorflow.lite.InterpreterApi

如果您想单独使用 LiteRT 和 Play Services API 必须使用 LiteRT 2.9 或更高版本。LiteRT 2.8 及更低版本与 Play 服务 API 版本不兼容。