IT Convergence Engineering/AI 버섯 어플

[Tensorflow] .tflite를 Quantization(양자화)하기(python)

Soo_buglosschestnut 2020. 8. 30. 00:24

.tflite를 Quantization(양자화)하기(python)


Python API는 quantization 옵션을 제공한다. 양자화는 float32로 학습된 weight값들을 단순화 시키는 작업을 말한다.

이 작업을 하게되면 .tflite이 파일크기가 대략 4분의 1정도로 줄어들게 된다.

 

float32로 학습된 weight값을 float16, int8등의 형태로 변환하는것이다.

이렇게 변환하게 되면 대부분 세밀한 데이터값을 버리는 작업이므로 전체적으로 보게되면 model의 정확도를 잃어버리는 작업이된다. 이렇게 됨으로써 작은 model 사이즈를 얻게 된다.

 

결과적으로 quantization은 model의 사이즈를 줄여 정확도가 떨어지는 대신에 빠른 추론 시간을 언게 된다고 볼수있다.

 

  • Quantization Code
import tensorflow as tf
from tensorflow import keras
import numpy as np


converter = tf.lite.TFLiteConverter.from_saved_model('~~~~~~')
converter.optimizations = [tf.lite.Optimize.DEFAULT]
converter.target_spec.supported_types = [tf.float16]
tflite_quant_model = converter.convert()
open("######.tflite", "wb").write(tflite_quant_model)

 

converter = tf.lite.TFLiteConverter.from_saved_model('~~~~~~')

→ ~~~~~~에는 .pb파일이 저장된 directory명을 적는다.

 

open("######.tflite", "wb").write(tflite_quant_model)

→ ######에는 quantization된 tflite파일의 이름을 적는다.

 

 

참고: www.tensorflow.org/lite/performance/post_training_quantization#full_integer_quantization

 

훈련 후 양자화  |  TensorFlow Lite

학습 후 양자화는 모델 정확도를 거의 저하시키지 않으면 서 CPU 및 하드웨어 가속기 대기 시간을 개선하면서 모델 크기를 줄일 수있는 변환 기술입니다. TensorFlow Lite Converter를 사용하여 TensorFlow

www.tensorflow.org

 

출처: devinlife.com/tensorflow%20lite/tflite-converter/

 

TFLite 모델 컨버터

TFLite converter로 TensorFlow 모델 파일을 TFLite 모델 파일로 변환하기.

devinlife.com

github.com/devinlife/my-tf-training/blob/master/032-convert-tflite.py

 

devinlife/my-tf-training

Contribute to devinlife/my-tf-training development by creating an account on GitHub.

github.com


ⓐ OFL

bugloss-chestnut.tistory.com/entry/Font-%EC%9D%BD%EC%9D%84-%EA%B1%B0%EB%A6%AC

 

[Font] 읽을 거리

nine8007.tistory.com/entry/%ED%8F%B0%ED%8A%B8-%EC%A0%80%EC%9E%91%EA%B6%8C%ED%91%9C%EA%B8%B0-CI-BI-%EC%9E%84%EB%B2%A0%EB%94%A9-OFL-%ED%8F%AC%EC%9E%A5%EC%A7%80-%EB%93%B1%EC%9D%98-%EB%9C%BB 폰트 저작권..

bugloss-chestnut.tistory.com

ⓑ 신경망