Google Play 服务 Java API 中的 LiteRT

您还可以使用 Java API 访问 Google Play 服务中的 LiteRT, 对原生 API 的补充。尤其是 Google Play 中的 LiteRT LiteRT 翻译工具 API

使用 Interpreter API

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

1. 添加项目依赖项

将以下依赖项添加到您的应用项目代码中,以访问 Play 适用于 LiteRT 的服务 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 初始化

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

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 服务提供 并且会动态加载,就像 Interpreter API。

正在检查设备兼容性

并非所有设备都支持使用 TFLite 的 GPU 硬件加速。为此, 使用 TfLiteGpu.isGpuDelegateAvailable 方法,用于检查设备是否 与 GPU 代理兼容。

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

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 Services API 中提供的更新,请查看以下额外指南,了解如何更新您的 应用项目代码:

  1. 请查看本页的限制部分,确保您的 用例。
  2. 在更新代码之前,请对您的代码执行性能和准确性检查 特别是在使用 因此,您有一个基准,可与新版本 实施。
  3. 如果您已迁移所有代码以使用 Play Services API 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 Services API 版本不兼容。