모바일 애플리케이션 개발자는 일반적으로 비트맵과 같은 유형이 지정된 객체나 정수와 같은 기본 요소와 상호작용합니다. 하지만 기기 내 머신러닝 모델을 실행하는 LiteRT 인터프리터 API는 ByteBuffer 형식의 텐서를 사용하므로 디버그하고 조작하기 어려울 수 있습니다. LiteRT Android 지원 라이브러리는 LiteRT 모델의 입력 및 출력을 처리하고 LiteRT 인터프리터를 더 쉽게 사용할 수 있도록 설계되었습니다.
시작하기
Gradle 종속 항목 및 기타 설정 가져오기
모델이 실행될 Android 모듈의 애셋 디렉터리에 .tflite 모델 파일을 복사합니다. 파일을 압축하지 않도록 지정하고 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은 메타데이터 추출기 라이브러리와 기타 모델 정보를 통해 읽을 수 있습니다.
기본 오디오 데이터 처리
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 (가능성 가장 높음)까지 얻어야 합니다.
선택사항: 결과를 라벨에 매핑
개발자는 선택적으로 결과를 라벨에 매핑할 수도 있습니다. 먼저 라벨이 포함된 텍스트 파일을 모듈의 애셋 디렉터리에 복사합니다. 다음으로 다음 코드를 사용하여 라벨 파일을 로드합니다.
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 지원 라이브러리는 다음을 다룹니다.
- 일반적인 데이터 유형 (float, uint8, 이미지, 오디오, 이러한 객체의 배열)을 tflite 모델의 입력 및 출력으로 사용합니다.
- 기본 이미지 작업 (이미지 자르기, 크기 조절, 회전)
- 정규화 및 양자화
- 파일 유틸리티
향후 버전에서는 텍스트 관련 애플리케이션 지원이 개선될 예정입니다.
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와 동일하며 구현은 운영체제와 독립적입니다.
개발자는 맞춤 프로세서를 만들 수도 있습니다. 이러한 경우 학습 프로세스와 일치하는 것이 중요합니다. 즉, 재현성을 높이기 위해 학습과 추론 모두에 동일한 전처리를 적용해야 합니다.
양자화
TensorImage 또는 TensorBuffer와 같은 입력 또는 출력 객체를 시작할 때 유형을 DataType.UINT8 또는 DataType.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);
텐서의 양자화 매개변수는 메타데이터 추출기 라이브러리를 통해 읽을 수 있습니다.