TensorFlow Model Convert 사용해보기
TensorFlow lite

텐서플로우 라이트는 텐서플로우의 경량화 버전이다. 서버나 PC 같은 고성능 장치에서만 텐서플로우 모델을 동작을 하는 게 아니라 텐서플로우 라이트 포맷 형태로 변환을 거치면, 모바일과 같은 임베디드 시스템에서도 추론(Inference)할 수 있다. 이는 온-디바이스(On-Device), 즉 서버에서 추론한 결과를 뿌려주는 것이 아닌, 장치 내부에서 추론이 직접 가능하도록 하다는 의미이다.
TensorFlow Lite Architecture

TensorFlow Lite 변환기는 TensorFlow 모델을 사용하고 TensorFlow Lite 모델(. tflite 파일 확장자로 식별되는 최적화된 FlatBuffer 형식)을 생성한다. TensorFlow 를 만든 모델을 TensorFlow Lite Converter로 변환하여 Android, iOS에서 추론을 할 수 있다.
TensorFlow Lite 모델 변환 예제 코드
TensorFlow 2.0 이후 버전
import tensorflow as tf
export_dir = "model/my_saved_model"
converter = tf.lite.TFLiteConverter.from_saved_model(export_dir)
tflite_model = converter.convert()
open("converted_model.tflite", "wb").write(tflite_model)
Keras 에서 변환
import tensorflow as tf
converter = tf.lite.TFLiteConverter.from_concrete_functions([concrete_func])
tflite_model = converter.convert()
open("converted_model.tflite", "wb").write(tflite_model)
양자화(Quantization) 옵션
| 기법 | 샘플 데이터 요구 | 사이즈 감소 | 정확도 | 하드웨어 지원 |
| post-traning float 16 quantization |
X | 최대 50% | Insignificant accuracy loss | CPU GPU |
| post-traning dynamic range quantization |
X | 최대 75% | Accuracy loss | CPU |
| post-traning integer quantization |
Unlabelled representative sample | 최대 75% | Smaller accuracy loss | CPU EdgeTPU hexagon DSP |
| Quantization-aware traning | Labelled traning data | 최대 75% | Smallest accuracy loss | CPU EdgeTPU hexagon DSP |
양자화를 통하여 줄어든 모델 크기
320 x 232 크기의 이미지 3670개를 학습한 한 모델을 양자화 수행한 결과이다.

양자화 옵션 예제
Dynamic range quantization
import tensorflow as tf
converter = tf.lite.TFLiteConverter.from_saved_model(saved_model_dir)
converter.optimizations = [tf.lite.Optimize.DEFAULT]
tflite_quant_model = converter.convert()
Full integer quantization of weights and activations
import tensorflow as tf
def representative_dataset_gen():
for _ in range(num_calibration_steps):
# 원하는 방식대로 샘플 데이터를 numpy array로 반환
yield [input]
converter = tf.lite.TFLiteConverter.from_saved_model(saved_model_dir)
converter.optimizations = [tf.lite.Optimize.DEFAULT]
converter.representative_dataset = representative_dataset_gen
tflite_quant_model = converter.convert()
Float16 quantization of weights
import tensorflow as tf
converter = tf.lite.TFLiteConverter.from_saved_model(saved_model_dir)
converter.optimizations = [tf.lite.Optimize.DEFAULT]
converter.target_spec.supported_types = [tf.lite.constants.FLOAT16]
tflite_quant_model = converter.convert()
이번 포스팅에서는 TensorFlow Lite 가 무엇인지 알아 보았으며 TensorFlow 에서 학습한 모델을 TensorFlow Converter 를 사용하여 tflite 포맷으로 만들고 양자화 옵션을 통하여 모델의 사이즈를 줄이는 것을 실습 하였습니다. 모든 코드는 아래 Github 에 모두 업로드 하였습니다.
Github - https://github.com/cepiloth/tensorflow_lite_model_maker_example
참고문헌
https://wiserloner.tistory.com/1379
https://www.tensorflow.org/lite/guide/get_started?hl=ko
https://www.itworld.co.kr/news/108197?page=0,1
https://www.tensorflow.org/lite/guide?hl=ko