使用 LiteRT 支持库处理输入和输出数据

移动应用开发者通常会与类型化对象(例如位图)或基元(例如整数)进行交互。不过,运行设备端机器学习模型的 LiteRT 解释器 API 使用 ByteBuffer 形式的张量,这可能难以调试和操作。LiteRT Android 支持库旨在帮助处理 LiteRT 模型的输入和输出,并使 LiteRT 解析器更易于使用。

使用入门

导入 Gradle 依赖项和其他设置

.tflite 模型文件复制到将运行该模型的 Android 模块的 assets 目录中。指定不应压缩该文件,并将 LiteRT 库添加到模块的 build.gradle 文件中:

android {
    // Other settings

    // Specify tflite file should not be compressed for the app apk
    aaptOptions {
        noCompress "tflite"
    }

}

dependencies {
    // Other dependencies

    // Import tflite dependencies
    implementation 'org.tensorflow:tensorflow-lite:0.0.0-nightly-SNAPSHOT'
    // The GPU delegate library is optional. Depend on it as needed.
    implementation 'com.google.ai.edge.litert:litert-gpu:0.0.0-nightly-SNAPSHOT'
    implementation 'com.google.ai.edge.litert:litert-support:0.0.0-nightly-SNAPSHOT'
}

探索 MavenCentral 上托管的 LiteRT 支持库 AAR,了解不同版本的支持库。

基本图片处理和转换

LiteRT 支持库包含一套基本的图像处理方法,例如裁剪和调整大小。如需使用该功能,请创建 ImagePreprocessor 并添加所需的操作。如需将图片转换为 LiteRT 解释器所需的张量格式,请创建 TensorImage 以用作输入:

import org.tensorflow.lite.DataType;
import org.tensorflow.lite.support.image.ImageProcessor;
import org.tensorflow.lite.support.image.TensorImage;
import org.tensorflow.lite.support.image.ops.ResizeOp;

// Initialization code
// Create an ImageProcessor with all ops required. For more ops, please
// refer to the ImageProcessor Architecture section in this README.
ImageProcessor imageProcessor =
    new ImageProcessor.Builder()
        .add(new ResizeOp(224, 224, ResizeOp.ResizeMethod.BILINEAR))
        .build();

// Create a TensorImage object. This creates the tensor of the corresponding
// tensor type (uint8 in this case) that the LiteRT interpreter needs.
TensorImage tensorImage = new TensorImage(DataType.UINT8);

// Analysis code for every frame
// Preprocess the image
tensorImage.load(bitmap);
tensorImage = imageProcessor.process(tensorImage);

DataType 可以通过 Metadata Extractor 库以及其他模型信息读取。

基本音频数据处理

LiteRT 支持库还定义了一个 TensorAudio 类,用于封装一些基本的音频数据处理方法。它通常与 AudioRecord 一起使用,并在环形缓冲区中捕获音频样本。

import android.media.AudioRecord;
import org.tensorflow.lite.support.audio.TensorAudio;

// Create an `AudioRecord` instance.
AudioRecord record = AudioRecord(...)

// Create a `TensorAudio` object from Android AudioFormat.
TensorAudio tensorAudio = new TensorAudio(record.getFormat(), size)

// Load all audio samples available in the AudioRecord without blocking.
tensorAudio.load(record)

// Get the `TensorBuffer` for inference.
TensorBuffer buffer = tensorAudio.getTensorBuffer()

创建输出对象并运行模型

在运行模型之前,我们需要创建用于存储结果的容器对象:

import org.tensorflow.lite.DataType;
import org.tensorflow.lite.support.tensorbuffer.TensorBuffer;

// Create a container for the result and specify that this is a quantized model.
// Hence, the 'DataType' is defined as UINT8 (8-bit unsigned integer)
TensorBuffer probabilityBuffer =
    TensorBuffer.createFixedSize(new int[]{1, 1001}, DataType.UINT8);

加载模型并运行推理:

import java.nio.MappedByteBuffer;
import org.tensorflow.lite.InterpreterFactory;
import org.tensorflow.lite.InterpreterApi;

// Initialise the model
try{
    MappedByteBuffer tfliteModel
        = FileUtil.loadMappedFile(activity,
            "mobilenet_v1_1.0_224_quant.tflite");
    InterpreterApi tflite = new InterpreterFactory().create(
        tfliteModel, new InterpreterApi.Options());
} catch (IOException e){
    Log.e("tfliteSupport", "Error reading model", e);
}

// Running inference
if(null != tflite) {
    tflite.run(tImage.getBuffer(), probabilityBuffer.getBuffer());
}

访问结果

开发者可以直接通过 probabilityBuffer.getFloatArray() 访问输出。如果模型生成量化输出,请务必转换结果。对于 MobileNet 量化模型,开发者需要将每个输出值除以 255,以获得每个类别的概率,范围从 0(可能性最低)到 1(可能性最高)。

可选:将结果映射到标签

开发者还可以选择将结果映射到标签。首先,将包含标签的文本文件复制到模块的 assets 目录中。接下来,使用以下代码加载标签文件:

import org.tensorflow.lite.support.common.FileUtil;

final String ASSOCIATED_AXIS_LABELS = "labels.txt";
List<String> associatedAxisLabels = null;

try {
    associatedAxisLabels = FileUtil.loadLabels(this, ASSOCIATED_AXIS_LABELS);
} catch (IOException e) {
    Log.e("tfliteSupport", "Error reading label file", e);
}

以下代码段演示了如何将概率与类别标签相关联:

import java.util.Map;
import org.tensorflow.lite.support.common.TensorProcessor;
import org.tensorflow.lite.support.common.ops.NormalizeOp;
import org.tensorflow.lite.support.label.TensorLabel;

// Post-processor which dequantize the result
TensorProcessor probabilityProcessor =
    new TensorProcessor.Builder().add(new NormalizeOp(0, 255)).build();

if (null != associatedAxisLabels) {
    // Map of labels and their corresponding probability
    TensorLabel labels = new TensorLabel(associatedAxisLabels,
        probabilityProcessor.process(probabilityBuffer));

    // Create a map to access the result based on label
    Map<String, Float> floatMap = labels.getMapWithFloatValue();
}

当前使用场景覆盖范围

当前版本的 LiteRT 支持库涵盖以下内容:

  • 作为 TFLite 模型的输入和输出的常见数据类型(浮点数、uint8、图片、音频和这些对象的数组)。
  • 基本图片操作(剪裁图片、调整大小和旋转)。
  • 归一化和量化
  • 文件实用程序

未来的版本将改进对文本相关应用的支持。

ImageProcessor 架构

ImageProcessor 的设计允许预先定义图片操作,并在构建过程中进行优化。ImageProcessor 目前支持三种基本预处理操作,如下面代码段中的三条注释所述:

import org.tensorflow.lite.support.common.ops.NormalizeOp;
import org.tensorflow.lite.support.common.ops.QuantizeOp;
import org.tensorflow.lite.support.image.ops.ResizeOp;
import org.tensorflow.lite.support.image.ops.ResizeWithCropOrPadOp;
import org.tensorflow.lite.support.image.ops.Rot90Op;

int width = bitmap.getWidth();
int height = bitmap.getHeight();

int size = height > width ? width : height;

ImageProcessor imageProcessor =
    new ImageProcessor.Builder()
        // Center crop the image to the largest square possible
        .add(new ResizeWithCropOrPadOp(size, size))
        // Resize using Bilinear or Nearest neighbour
        .add(new ResizeOp(224, 224, ResizeOp.ResizeMethod.BILINEAR));
        // Rotation counter-clockwise in 90 degree increments
        .add(new Rot90Op(rotateDegrees / 90))
        .add(new NormalizeOp(127.5, 127.5))
        .add(new QuantizeOp(128.0, 1/128.0))
        .build();

如需详细了解归一化和量化,请点击此处

支持库的最终目标是支持所有 tf.image 转换。这意味着转换将与 TensorFlow 相同,并且实现将独立于操作系统。

我们还欢迎开发者创建自定义处理器。在这些情况下,与训练过程保持一致非常重要,也就是说,应将相同的预处理应用于训练和推理,以提高可重现性。

量化

初始化输入或输出对象(例如 TensorImageTensorBuffer)时,您需要将其类型指定为 DataType.UINT8DataType.FLOAT32

TensorImage tensorImage = new TensorImage(DataType.UINT8);
TensorBuffer probabilityBuffer =
    TensorBuffer.createFixedSize(new int[]{1, 1001}, DataType.UINT8);

TensorProcessor 可用于量化输入张量或反量化输出张量。例如,在处理量化输出 TensorBuffer 时,开发者可以使用 DequantizeOp 将结果反量化为介于 0 到 1 之间的浮点概率:

import org.tensorflow.lite.support.common.TensorProcessor;

// Post-processor which dequantize the result
TensorProcessor probabilityProcessor =
    new TensorProcessor.Builder().add(new DequantizeOp(0, 1/255.0)).build();
TensorBuffer dequantizedBuffer = probabilityProcessor.process(probabilityBuffer);

张量的量化参数可以通过元数据提取器库读取。