TensorFlow Lite 변환기는 TensorFlow 모델을 사용하고 TensorFlow Lite 모델(.tflite
파일 확장자로 식별되는 최적화된 FlatBuffer 형식)을 생성합니다. 변환기를 사용하는 옵션에는 다음 두 가지가 있습니다.
- Python API(권장): 모델 개발 파이프라인의 일부로 모델을 더 쉽게 변환하고 최적화를 적용하며 메타데이터를 추가하는 등 다양한 기능이 많습니다.
- 명령줄: 기본 모델 변환만 지원합니다.
Python API
도우미 코드: 설치된 TensorFlow 버전을 확인하려면 print(tf.__version__)
를 실행하고 TensorFlow Lite 변환기 API에 관해 자세히 알아보려면 print(help(tf.lite.TFLiteConverter))
를 실행합니다.
TensorFlow 2.x를 설치한 경우 옵션에는 다음 두 가지가 있습니다. TensorFlow 1.x를 설치한 경우 GitHub를 참고하세요.
tf.lite.TFLiteConverter
를 사용하여 TensorFlow 2.x 모델을 변환합니다. TensorFlow 2.x 모델은 저장된 모델 형식을 사용하여 저장되며 상위 수준tf.keras.*
API(Keras 모델) 또는 하위 수준tf.*
API(여기서 구체적 함수 생성)를 사용하여 생성됩니다. 따라서 다음 세 가지 옵션이 있습니다. 예는 다음 몇 가지 섹션을 참고하세요.tf.lite.TFLiteConverter.from_saved_model()
(권장): 저장된 모델을 변환합니다.tf.lite.TFLiteConverter.from_keras_model()
: Keras 모델을 변환합니다.tf.lite.TFLiteConverter.from_concrete_functions()
: concrete 함수를 변환합니다.
tf.compat.v1.lite.TFLiteConverter
를 사용하여 TensorFlow 1.x 모델을 변환합니다(예는 GitHub 참고).tf.compat.v1.lite.TFLiteConverter.from_saved_model()
: 저장된 모델을 변환합니다.tf.compat.v1.lite.TFLiteConverter.from_keras_model_file()
: Keras 모델을 변환합니다.tf.compat.v1.lite.TFLiteConverter.from_session()
: 세션에서 GraphDef를 변환합니다.tf.compat.v1.lite.TFLiteConverter.from_frozen_graph()
: 파일에서 Frozen GraphDef를 변환합니다. 체크포인트가 있으면 먼저 체크포인트를 Frozen GraphDef 파일로 변환한 후 여기를 참고하여 이 API를 사용합니다.
저장된 모델 변환(권장)
다음 예는 저장된 모델을 TensorFlow Lite 모델로 변환하는 방법을 보여줍니다.
import tensorflow as tf
# Convert the model
converter = tf.lite.TFLiteConverter.from_saved_model(saved_model_dir) # path to the SavedModel directory
tflite_model = converter.convert()
# Save the model.
with open('model.tflite', 'wb') as f:
f.write(tflite_model)
Keras 모델 변환
다음 예는 Keras 모델을 TensorFlow Lite 모델로 변환하는 방법을 보여줍니다.
import tensorflow as tf
# Create a model using high-level tf.keras.* APIs
model = tf.keras.models.Sequential([
tf.keras.layers.Dense(units=1, input_shape=[1]),
tf.keras.layers.Dense(units=16, activation='relu'),
tf.keras.layers.Dense(units=1)
])
model.compile(optimizer='sgd', loss='mean_squared_error') # compile the model
model.fit(x=[-1, 0, 1], y=[-3, -1, 1], epochs=5) # train the model
# (to generate a SavedModel) tf.saved_model.save(model, "saved_model_keras_dir")
# Convert the model.
converter = tf.lite.TFLiteConverter.from_keras_model(model)
tflite_model = converter.convert()
# Save the model.
with open('model.tflite', 'wb') as f:
f.write(tflite_model)
concrete 함수 변환
다음 예는 concrete 함수를 TensorFlow Lite 모델로 변환하는 방법을 보여줍니다.
import tensorflow as tf
# Create a model using low-level tf.* APIs
class Squared(tf.Module):
@tf.function
def __call__(self, x):
return tf.square(x)
model = Squared()
# (ro run your model) result = Squared(5.0) # This prints "25.0"
# (to generate a SavedModel) tf.saved_model.save(model, "saved_model_tf_dir")
concrete_func = model.__call__.get_concrete_function()
# Convert the model
converter = tf.lite.TFLiteConverter.from_concrete_functions([concrete_func])
tflite_model = converter.convert()
# Save the model.
with open('model.tflite', 'wb') as f:
f.write(tflite_model)
기타 기능
최적화 적용: 일반적으로 사용되는 최적화는 학습 후 양자화로, 정확성을 최소한으로 손실하면서 모델 지연 시간과 크기를 추가로 줄일 수 있습니다.
지원되지 않는 작업 처리: 모델에 연산자가 있는 경우 다음과 같은 옵션이 있습니다.
TensorFlow에서 지원되지만 TensorFlow Lite에서 지원되지 않음: 크기 제약 조건이 있으면 TensorFlow Lite 연산자를 생성해야 합니다. 생성하지 않으면 TensorFlow Lite 모델에서 TensorFlow 연산자를 사용하면 됩니다.
TensorFlow에서 지원되지 않음: TensorFlow 연산자를 생성한 다음 TensorFlow Lite 연산자를 생성해야 합니다. TensorFlow 연산자 생성에 실패하거나 연산자를 만들지 않으려면(권장되지 않음, 주의해서 진행)
register_custom_opdefs
메서드를 사용하여 변환한 다음 직접 TensorFlow Lite 연산자를 생성할 수 있습니다.register_custom_opdefs
메서드는 OpDef가 포함된 문자열 목록을 가져옵니다. 다음은 입력 1개, 출력 1개, 속성 2개가 있는TFLiteAwesomeCustomOp
의 예입니다.import tensorflow as tf custom_opdef = """name: 'TFLiteAwesomeCustomOp' input_arg: { name: 'In' type: DT_FLOAT } output_arg: { name: 'Out' type: DT_FLOAT } attr : { name: 'a1' type: 'float'} attr : { name: 'a2' type: 'list(float)'}""" # Register custom opdefs before the invocation of converter API. tf.lite.python.convert.register_custom_opdefs([custom_opdef]) converter = tf.lite.TFLiteConverter.from_saved_model(...) converter.allow_custom_ops = True
명령줄 도구
가능하면 위에 나열된 Python API를 대신 사용하는 것이 좋습니다.
pip에서 TensorFlow 2.x를 설치한 경우 다음과 같이 tflite_convert
명령어를 사용합니다. 소스에서 TensorFlow 2.x를 설치한 경우 다음 섹션에서 'tflite_convert
'를 'bazel run
//tensorflow/lite/python:tflite_convert --
'로 대체할 수 있고 TensorFlow 1.x를 설치한 경우 GitHub(참조, 예)를 참고하세요.
tflite_convert
: 사용 가능한 모든 플래그를 보려면 다음 명령어를 사용합니다.
$ tflite_convert --help
`--output_file`. Type: string. Full path of the output file.
`--saved_model_dir`. Type: string. Full path to the SavedModel directory.
`--keras_model_file`. Type: string. Full path to the Keras H5 model file.
`--enable_v1_converter`. Type: bool. (default False) Enables the converter and flags used in TF 1.x instead of TF 2.x.
You are required to provide the `--output_file` flag and either the `--saved_model_dir` or `--keras_model_file` flag.
저장된 모델 변환
tflite_convert \
--saved_model_dir=/tmp/mobilenet_saved_model \
--output_file=/tmp/mobilenet.tflite
Keras H5 모델 변환
tflite_convert \
--keras_model_file=/tmp/mobilenet_keras_model.h5 \
--output_file=/tmp/mobilenet.tflite
다음 단계
- 기기에 모델을 배포할 때 플랫폼별 래퍼 코드를 더 쉽게 만들 수 있는 메타데이터를 추가합니다.
- TensorFlow Lite 인터프리터를 사용하여 클라이언트 기기(예: 모바일, 삽입형)에서 추론을 실행합니다.