TensorFlow Lite 추론

추론이라는 용어는 입력 데이터를 기반으로 예측을 수행하기 위해 기기에서 TensorFlow Lite 모델을 실행하는 프로세스를 나타냅니다. TensorFlow Lite 모델로 추론을 수행하려면 인터프리터를 통해 추론해야 합니다. TensorFlow Lite 인터프리터는 가볍고 빠르게 설계되었습니다. 인터프리터는 정적 그래프 순서 지정과 커스텀 (덜 동적) 메모리 할당자를 사용하여 로드, 초기화, 실행 지연 시간을 최소화합니다.

이 페이지에서는 TensorFlow Lite 인터프리터에 액세스하고 C++, Java, Python을 사용하여 추론을 수행하는 방법과 각 지원되는 플랫폼의 다른 리소스 링크를 설명합니다.

주요 개념

TensorFlow Lite 추론은 일반적으로 다음 단계를 따릅니다.

  1. 모델 로드

    모델의 실행 그래프가 포함된 메모리에 .tflite 모델을 로드해야 합니다.

  2. 데이터 변환

    일반적으로 모델의 원시 입력 데이터는 모델에서 예상하는 입력 데이터 형식과 일치하지 않습니다. 예를 들어 모델과 호환되도록 이미지 크기를 조절하거나 이미지 형식을 변경해야 할 수 있습니다.

  3. 추론 실행

    이 단계에서는 TensorFlow Lite API를 사용하여 모델을 실행합니다. 여기에는 다음 섹션에 설명된 대로 인터프리터 빌드 및 텐서 할당과 같은 몇 가지 단계가 포함됩니다.

  4. 출력 해석

    모델 추론에서 결과를 받으면 애플리케이션에 유용한 의미 있는 방식으로 텐서를 해석해야 합니다.

    예를 들어 모델은 확률 목록만 반환할 수 있습니다. 이 확률을 관련 카테고리에 매핑하고 최종 사용자에게 제시하는 것은 개발자의 몫입니다.

지원되는 플랫폼

TensorFlow 추론 API는 Android, iOS, Linux와 같은 가장 일반적인 모바일/임베디드 플랫폼에 여러 프로그래밍 언어로 제공됩니다.

대부분의 경우 API 설계는 사용 편의성보다 성능에 중점을 둡니다. TensorFlow Lite는 소형 기기에서 빠른 추론을 위해 설계되었으므로 API가 편의를 위해 불필요한 복사를 피하려고 하는 것은 당연합니다. 마찬가지로 TensorFlow API와의 일관성은 분명한 목표가 아니었으며 언어 간 약간의 차이가 발생할 수 있습니다.

모든 라이브러리에서 TensorFlow Lite API를 사용하면 모델을 로드하고 입력을 피드하며 추론 출력을 가져올 수 있습니다.

Android 플랫폼

Android에서 TensorFlow Lite 추론은 Java 또는 C++ API를 사용하여 실행할 수 있습니다. Java API는 편의성을 제공하며 Android Activity 클래스 내에서 직접 사용할 수 있습니다. C++ API는 더 많은 유연성과 속도를 제공하지만, Java 및 C++ 레이어 간에 데이터를 이동하려면 JNI 래퍼를 작성해야 할 수 있습니다.

C++자바 사용에 관한 자세한 내용은 아래를 참고하거나 Android 빠른 시작에서 튜토리얼 및 예시 코드를 확인하세요.

TensorFlow Lite Android 래퍼 코드 생성기

메타데이터로 향상된 TensorFlow Lite 모델의 경우 개발자는 TensorFlow Lite Android 래퍼 코드 생성기를 사용하여 플랫폼별 래퍼 코드를 만들 수 있습니다. 래퍼 코드를 사용하면 Android에서 ByteBuffer와 직접 상호작용할 필요가 없습니다. 대신 개발자는 유형이 지정된 객체(예: BitmapRect)를 사용하여 TensorFlow Lite 모델과 상호작용할 수 있습니다. 자세한 내용은 TensorFlow Lite Android 래퍼 코드 생성기를 참고하세요.

iOS 플랫폼

iOS의 경우 TensorFlow Lite는 SwiftObjective-C로 작성된 네이티브 iOS 라이브러리와 함께 사용할 수 있습니다. Objective-C 코드에서 직접 C API를 사용할 수도 있습니다.

Swift, Objective-C, C API를 사용하는 방법에 대한 자세한 내용은 아래를 참조하거나 iOS 빠른 시작에서 튜토리얼 및 예시 코드를 참조하세요.

Linux 플랫폼

Linux 플랫폼 (Raspberry Pi 포함)에서는 다음 섹션과 같이 C++Python에서 제공되는 TensorFlow Lite API를 사용하여 추론을 실행할 수 있습니다.

모델 실행

TensorFlow Lite 모델을 실행하려면 몇 가지 간단한 단계를 따르세요.

  1. 모델을 메모리에 로드합니다.
  2. 기존 모델을 기반으로 Interpreter를 빌드합니다.
  3. 입력 텐서 값을 설정합니다. 사전 정의된 크기를 원하지 않으면 선택적으로 입력 텐서의 크기를 조절할 수 있습니다.
  4. 추론을 호출합니다.
  5. 출력 텐서 값을 읽습니다.

다음 섹션에서는 이러한 단계를 각 언어에서 수행하는 방법을 설명합니다.

Java에서 모델 로드 및 실행

플랫폼: Android

TensorFlow Lite로 추론을 실행하기 위한 Java API는 주로 Android에서 사용하도록 설계되었으므로 Android 라이브러리 종속 항목인 org.tensorflow:tensorflow-lite로 사용할 수 있습니다.

자바에서는 Interpreter 클래스를 사용하여 모델을 로드하고 모델 추론을 구동합니다. 대부분의 경우 이 API가 필요한 유일한 API일 수 있습니다.

.tflite 파일을 사용하여 Interpreter를 초기화할 수 있습니다.

public Interpreter(@NotNull File modelFile);

또는 MappedByteBuffer를 사용합니다.

public Interpreter(@NotNull MappedByteBuffer mappedByteBuffer);

두 경우 모두 유효한 TensorFlow Lite 모델을 제공해야 하며, 그러지 않으면 API에서 IllegalArgumentException이 발생합니다. MappedByteBuffer를 사용하여 Interpreter를 초기화하는 경우 Interpreter의 전체 수명 동안 변경되지 않아야 합니다.

모델에서 추론을 실행하는 가장 좋은 방법은 서명을 사용하는 것입니다. Tensorflow 2.5부터 변환된 모델에 사용할 수 있습니다.

try (Interpreter interpreter = new Interpreter(file_of_tensorflowlite_model)) {
  Map<String, Object> inputs = new HashMap<>();
  inputs.put("input_1", input1);
  inputs.put("input_2", input2);
  Map<String, Object> outputs = new HashMap<>();
  outputs.put("output_1", output1);
  interpreter.runSignature(inputs, outputs, "mySignature");
}

runSignature 메서드는 다음 세 가지 인수를 사용합니다.

  • 입력 : 서명의 입력 이름의 입력을 입력 객체로 매핑합니다.

  • 출력 : 서명의 출력 이름에서 출력 데이터로의 출력 매핑을 위한 맵입니다.

  • 서명 이름 (선택사항): 서명 이름 (모델에 단일 서명이 있는 경우 비워 둘 수 있음)입니다.

모델에 정의된 서명이 없을 때 추론을 실행하는 또 다른 방법입니다. Interpreter.run()를 호출하기만 하면 됩니다. 예를 들면 다음과 같습니다.

try (Interpreter interpreter = new Interpreter(file_of_a_tensorflowlite_model)) {
  interpreter.run(input, output);
}

run() 메서드는 하나의 입력만 사용하고 하나의 출력만 반환합니다. 따라서 모델에 입력 또는 출력이 여러 개인 경우에는 대신 다음을 사용하세요.

interpreter.runForMultipleInputsOutputs(inputs, map_of_indices_to_outputs);

이 경우 inputs의 각 항목은 입력 텐서에 상응하고 map_of_indices_to_outputs는 출력 텐서의 색인을 상응하는 출력 데이터에 매핑합니다.

두 경우 모두 텐서 색인은 모델을 만들 때 TensorFlow Lite 변환기에 지정한 값과 일치해야 합니다. input의 텐서 순서는 TensorFlow Lite 변환기에 지정된 순서와 일치해야 합니다.

Interpreter 클래스는 작업 이름을 사용하여 모델 입력 또는 출력의 색인을 가져올 수 있는 편리한 함수도 제공합니다.

public int getInputIndex(String opName);
public int getOutputIndex(String opName);

opName가 모델에서 유효한 연산이 아니면 IllegalArgumentException이 발생합니다.

또한 Interpreter는 리소스를 소유합니다. 메모리 누수를 방지하려면 다음에서 사용한 후 리소스를 해제해야 합니다.

interpreter.close();

자바를 사용한 프로젝트 예는 Android 이미지 분류 샘플을 참고하세요.

지원되는 데이터 유형 (Java)

TensorFlow Lite를 사용하려면 입력 및 출력 텐서의 데이터 유형이 다음 기본 유형 중 하나여야 합니다.

  • float
  • int
  • long
  • byte

String 유형도 지원되지만 기본 유형과 다르게 인코딩됩니다. 특히 문자열 텐서의 형태는 텐서에 있는 문자열의 수와 배열을 결정하며, 각 요소 자체는 가변 길이 문자열입니다. 이러한 의미에서 텐서의 (바이트) 크기는 모양과 유형만으로 계산할 수 없으므로 문자열을 단일 평면 ByteBuffer 인수로 제공할 수 없습니다.

IntegerFloat와 같은 박스형 유형을 비롯한 다른 데이터 유형을 사용하면 IllegalArgumentException이 발생합니다.

입력

각 입력은 지원되는 기본 유형의 배열 또는 다차원 배열이거나 적절한 크기의 원시 ByteBuffer여야 합니다. 입력이 배열 또는 다차원 배열이면 연결된 입력 텐서는 추론 시 배열의 차원으로 암시적으로 크기가 조정됩니다. 입력이 ByteBuffer인 경우 호출자는 추론을 실행하기 전에 먼저 Interpreter.resizeInput()를 통해 연결된 입력 텐서의 크기를 수동으로 조절해야 합니다.

ByteBuffer를 사용할 때는 직접 바이트 버퍼를 사용하는 것이 좋습니다. 이렇게 하면 Interpreter가 불필요한 사본을 피할 수 있기 때문입니다. ByteBuffer가 직접 바이트 버퍼인 경우 순서는 ByteOrder.nativeOrder()여야 합니다. 모델 추론에 사용된 후에는 모델 추론이 완료될 때까지 변경되지 않아야 합니다.

출력

각 출력은 지원되는 기본 유형의 배열 또는 다차원 배열이거나 적절한 크기의 ByteBuffer여야 합니다. 출력 텐서의 모양이 입력에 따라 달라질 수 있는 동적 출력이 있는 모델도 있습니다. 기존 Java 추론 API로 이를 처리하는 간단한 방법은 없지만 계획된 확장 프로그램을 사용하면 가능합니다.

Swift에서 모델 로드 및 실행

플랫폼: iOS

Swift API는 Cocoapods의 TensorFlowLiteSwift 포드에서 사용할 수 있습니다.

먼저 TensorFlowLite 모듈을 가져와야 합니다.

import TensorFlowLite
// Getting model path
guard
  let modelPath = Bundle.main.path(forResource: "model", ofType: "tflite")
else {
  // Error handling...
}

do {
  // Initialize an interpreter with the model.
  let interpreter = try Interpreter(modelPath: modelPath)

  // Allocate memory for the model's input `Tensor`s.
  try interpreter.allocateTensors()

  let inputData: Data  // Should be initialized

  // input data preparation...

  // Copy the input data to the input `Tensor`.
  try self.interpreter.copy(inputData, toInputAt: 0)

  // Run inference by invoking the `Interpreter`.
  try self.interpreter.invoke()

  // Get the output `Tensor`
  let outputTensor = try self.interpreter.output(at: 0)

  // Copy output to `Data` to process the inference results.
  let outputSize = outputTensor.shape.dimensions.reduce(1, {x, y in x * y})
  let outputData =
        UnsafeMutableBufferPointer<Float32>.allocate(capacity: outputSize)
  outputTensor.data.copyBytes(to: outputData)

  if (error != nil) { /* Error handling... */ }
} catch error {
  // Error handling...
}

Objective-C에서 모델 로드 및 실행

플랫폼: iOS

Objective-C API는 Cocoapods의 TensorFlowLiteObjC 포드에서 사용할 수 있습니다.

먼저 TensorFlowLite 모듈을 가져와야 합니다.

@import TensorFlowLite;
NSString *modelPath = [[NSBundle mainBundle] pathForResource:@"model"
                                                      ofType:@"tflite"];
NSError *error;

// Initialize an interpreter with the model.
TFLInterpreter *interpreter = [[TFLInterpreter alloc] initWithModelPath:modelPath
                                                                  error:&error];
if (error != nil) { /* Error handling... */ }

// Allocate memory for the model's input `TFLTensor`s.
[interpreter allocateTensorsWithError:&error];
if (error != nil) { /* Error handling... */ }

NSMutableData *inputData;  // Should be initialized
// input data preparation...

// Get the input `TFLTensor`
TFLTensor *inputTensor = [interpreter inputTensorAtIndex:0 error:&error];
if (error != nil) { /* Error handling... */ }

// Copy the input data to the input `TFLTensor`.
[inputTensor copyData:inputData error:&error];
if (error != nil) { /* Error handling... */ }

// Run inference by invoking the `TFLInterpreter`.
[interpreter invokeWithError:&error];
if (error != nil) { /* Error handling... */ }

// Get the output `TFLTensor`
TFLTensor *outputTensor = [interpreter outputTensorAtIndex:0 error:&error];
if (error != nil) { /* Error handling... */ }

// Copy output to `NSData` to process the inference results.
NSData *outputData = [outputTensor dataWithError:&error];
if (error != nil) { /* Error handling... */ }

Objective-C 코드에서 C API 사용

현재 Objective-C API는 대리자를 지원하지 않습니다. Objective-C 코드에서 대리자를 사용하려면 기본 C API를 직접 호출해야 합니다.

#include "tensorflow/lite/c/c_api.h"
TfLiteModel* model = TfLiteModelCreateFromFile([modelPath UTF8String]);
TfLiteInterpreterOptions* options = TfLiteInterpreterOptionsCreate();

// Create the interpreter.
TfLiteInterpreter* interpreter = TfLiteInterpreterCreate(model, options);

// Allocate tensors and populate the input tensor data.
TfLiteInterpreterAllocateTensors(interpreter);
TfLiteTensor* input_tensor =
    TfLiteInterpreterGetInputTensor(interpreter, 0);
TfLiteTensorCopyFromBuffer(input_tensor, input.data(),
                           input.size() * sizeof(float));

// Execute inference.
TfLiteInterpreterInvoke(interpreter);

// Extract the output tensor data.
const TfLiteTensor* output_tensor =
    TfLiteInterpreterGetOutputTensor(interpreter, 0);
TfLiteTensorCopyToBuffer(output_tensor, output.data(),
                         output.size() * sizeof(float));

// Dispose of the model and interpreter objects.
TfLiteInterpreterDelete(interpreter);
TfLiteInterpreterOptionsDelete(options);
TfLiteModelDelete(model);

C++에서 모델 로드 및 실행

플랫폼: Android, iOS, Linux

C++에서 모델은 FlatBufferModel 클래스에 저장됩니다. TensorFlow Lite 모델을 캡슐화하며, 모델이 저장된 위치에 따라 몇 가지 방법으로 빌드할 수 있습니다.

class FlatBufferModel {
  // Build a model based on a file. Return a nullptr in case of failure.
  static std::unique_ptr<FlatBufferModel> BuildFromFile(
      const char* filename,
      ErrorReporter* error_reporter);

  // Build a model based on a pre-loaded flatbuffer. The caller retains
  // ownership of the buffer and should keep it alive until the returned object
  // is destroyed. Return a nullptr in case of failure.
  static std::unique_ptr<FlatBufferModel> BuildFromBuffer(
      const char* buffer,
      size_t buffer_size,
      ErrorReporter* error_reporter);
};

이제 모델을 FlatBufferModel 객체로 만들었으므로 Interpreter를 사용하여 모델을 실행할 수 있습니다. 단일 FlatBufferModel를 둘 이상의 Interpreter에서 동시에 사용할 수 있습니다.

아래의 코드 스니펫은 Interpreter API의 중요한 부분을 보여줍니다. 다음 사항에 유의하세요.

  • 텐서는 문자열 비교(및 문자열 라이브러리의 고정된 종속 항목)를 피하기 위해 정수로 표현됩니다.
  • 동시 스레드에서 인터프리터에 액세스하면 안 됩니다.
  • 입력 및 출력 텐서의 메모리 할당은 텐서 크기를 조절한 직후 AllocateTensors()를 호출하여 트리거해야 합니다.

C++로 TensorFlow Lite를 사용하는 가장 간단한 방법은 다음과 같습니다.

// Load the model
std::unique_ptr<tflite::FlatBufferModel> model =
    tflite::FlatBufferModel::BuildFromFile(filename);

// Build the interpreter
tflite::ops::builtin::BuiltinOpResolver resolver;
std::unique_ptr<tflite::Interpreter> interpreter;
tflite::InterpreterBuilder(*model, resolver)(&interpreter);

// Resize input tensors, if desired.
interpreter->AllocateTensors();

float* input = interpreter->typed_input_tensor<float>(0);
// Fill `input`.

interpreter->Invoke();

float* output = interpreter->typed_output_tensor<float>(0);

더 많은 코드 예는 minimal.cclabel_image.cc를 참고하세요.

Python에서 모델 로드 및 실행

플랫폼: Linux

추론을 실행하는 Python API에서는 대부분 tf.lite.Interpreter만 있으면 모델을 로드하고 추론을 실행할 수 있습니다.

다음 예에서는 Python 인터프리터를 사용하여 .tflite 파일을 로드하고 임의의 입력 데이터로 추론을 실행하는 방법을 보여줍니다.

이 예는 SignatureDef가 정의된 저장된 모델에서 변환하는 경우에 권장됩니다. TensorFlow 2.5부터 사용 가능

class TestModel(tf.Module):
  def __init__(self):
    super(TestModel, self).__init__()

  @tf.function(input_signature=[tf.TensorSpec(shape=[1, 10], dtype=tf.float32)])
  def add(self, x):
    '''
    Simple method that accepts single input 'x' and returns 'x' + 4.
    '''
    # Name the output 'result' for convenience.
    return {'result' : x + 4}


SAVED_MODEL_PATH = 'content/saved_models/test_variable'
TFLITE_FILE_PATH = 'content/test_variable.tflite'

# Save the model
module = TestModel()
# You can omit the signatures argument and a default signature name will be
# created with name 'serving_default'.
tf.saved_model.save(
    module, SAVED_MODEL_PATH,
    signatures={'my_signature':module.add.get_concrete_function()})

# Convert the model using TFLiteConverter
converter = tf.lite.TFLiteConverter.from_saved_model(SAVED_MODEL_PATH)
tflite_model = converter.convert()
with open(TFLITE_FILE_PATH, 'wb') as f:
  f.write(tflite_model)

# Load the TFLite model in TFLite Interpreter
interpreter = tf.lite.Interpreter(TFLITE_FILE_PATH)
# There is only 1 signature defined in the model,
# so it will return it by default.
# If there are multiple signatures then we can pass the name.
my_signature = interpreter.get_signature_runner()

# my_signature is callable with input as arguments.
output = my_signature(x=tf.constant([1.0], shape=(1,10), dtype=tf.float32))
# 'output' is dictionary with all outputs from the inference.
# In this case we have single output 'result'.
print(output['result'])

모델에 SignatureDefs가 정의되지 않은 경우의 또 다른 예입니다.

import numpy as np
import tensorflow as tf

# Load the TFLite model and allocate tensors.
interpreter = tf.lite.Interpreter(model_path="converted_model.tflite")
interpreter.allocate_tensors()

# Get input and output tensors.
input_details = interpreter.get_input_details()
output_details = interpreter.get_output_details()

# Test the model on random input data.
input_shape = input_details[0]['shape']
input_data = np.array(np.random.random_sample(input_shape), dtype=np.float32)
interpreter.set_tensor(input_details[0]['index'], input_data)

interpreter.invoke()

# The function `get_tensor()` returns a copy of the tensor data.
# Use `tensor()` in order to get a pointer to the tensor.
output_data = interpreter.get_tensor(output_details[0]['index'])
print(output_data)

모델을 사전 변환된 .tflite 파일로 로드하는 대신 코드를 TensorFlow Lite Converter Python API와 결합하여 Keras 모델을 TensorFlow Lite 형식으로 변환한 후 추론을 실행할 수 있습니다.

import numpy as np
import tensorflow as tf

img = tf.keras.Input(shape=(64, 64, 3), name="img")
const = tf.constant([1., 2., 3.]) + tf.constant([1., 4., 4.])
val = img + const
out = tf.identity(val, name="out")

# Convert to TF Lite format
converter = tf.lite.TFLiteConverter.from_keras_model(tf.keras.models.Model(inputs=[img], outputs=[out]))
tflite_model = converter.convert()

# Load the TFLite model and allocate tensors.
interpreter = tf.lite.Interpreter(model_content=tflite_model)
interpreter.allocate_tensors()

# Continue to get tensors and so forth, as shown above...

더 많은 Python 샘플 코드는 label_image.py를 참조하세요.

동적 형태 모델로 추론 실행

동적 입력 모양으로 모델을 실행하려면 추론을 실행하기 전에 입력 모양 크기를 조절해야 합니다. 그렇지 않으면 TensorFlow 모델의 None 형태가 TFLite 모델에서 1의 자리표시자로 대체됩니다.

다음 예는 다른 언어로 추론을 실행하기 전에 입력 모양 크기를 조절하는 방법을 보여줍니다. 모든 예에서는 입력 도형이 [1/None, 10]로 정의되고 [3, 10]로 크기를 조절해야 한다고 가정합니다.

C++ 예시:

// Resize input tensors before allocate tensors
interpreter->ResizeInputTensor(/*tensor_index=*/0, std::vector<int>{3,10});
interpreter->AllocateTensors();

Python 예시:

# Load the TFLite model in TFLite Interpreter
interpreter = tf.lite.Interpreter(model_path=TFLITE_FILE_PATH)

# Resize input shape for dynamic shape model and allocate tensor
interpreter.resize_tensor_input(interpreter.get_input_details()[0]['index'], [3, 10])
interpreter.allocate_tensors()

# Get input and output tensors.
input_details = interpreter.get_input_details()
output_details = interpreter.get_output_details()

지원되는 작업

TensorFlow Lite는 TensorFlow 작업의 일부를 지원하며 일부 제한사항이 있습니다. 작업 및 제한사항의 전체 목록은 TF Lite 작업 페이지를 참고하세요.