Skip to content

Commit

Permalink
quantization
Browse files Browse the repository at this point in the history
  • Loading branch information
PINTO0309 committed Dec 16, 2023
1 parent 14d1cf3 commit b8104dd
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 0 deletions.
24 changes: 24 additions & 0 deletions 426_YOLOX-Body-Head-Hand/quantization/01_make_calib_data.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import cv2
import glob
import numpy as np

H=128
W=160
# H=256
# W=320
# H=480
# W=640

files = glob.glob("data/*.jpg")
img_datas = []
for idx, file in enumerate(files):
bgr_img = cv2.imread(file)
resized_img = cv2.resize(bgr_img, (W, H))
extend_batch_size_img = resized_img[np.newaxis, :].astype(np.float32)
print(
f'{str(idx+1).zfill(2)}. extend_batch_size_img.shape: {extend_batch_size_img.shape}'
)
img_datas.append(extend_batch_size_img)
calib_datas = np.vstack(img_datas)
print(f'calib_datas.shape: {calib_datas.shape}')
np.save(file=f'calibdata_bgr_no_norm_{H}x{W}.npy', arr=calib_datas)
59 changes: 59 additions & 0 deletions 426_YOLOX-Body-Head-Hand/quantization/02_int8.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import numpy as np
import tensorflow as tf

"""
onnx2tf -i yolox_ti_body_head_hand_n_1x3x128x160.onnx -coion -osd
onnx2tf -i yolox_ti_body_head_hand_n_1x3x256x320.onnx -coion -osd
onnx2tf -i yolox_ti_body_head_hand_n_1x3x480x640.onnx -coion -osd
"""

RESOLUTIONS = [
[128,160],
[256,320],
[480,640],
]

def representative_dataset_128x160():
images = np.load('calibdata_bgr_no_norm_128x160.npy')
for image in images:
yield {
"input": image[np.newaxis, ...],
}

def representative_dataset_256x320():
images = np.load('calibdata_bgr_no_norm_256x320.npy')
for image in images:
yield {
"input": image[np.newaxis, ...],
}

def representative_dataset_480x640():
images = np.load('calibdata_bgr_no_norm_480x640.npy')
for image in images:
yield {
"input": image[np.newaxis, ...],
}


for H, W in RESOLUTIONS:
print(f'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ {H}x{W}')
converter = tf.lite.TFLiteConverter.from_saved_model(f'saved_model_{H}x{W}')
converter.optimizations = [tf.lite.Optimize.DEFAULT]
if H == 128:
converter.representative_dataset = representative_dataset_128x160
elif H == 256:
converter.representative_dataset = representative_dataset_256x320
elif H == 480:
converter.representative_dataset = representative_dataset_480x640
converter.target_spec.supported_ops = [tf.lite.OpsSet.TFLITE_BUILTINS_INT8]
converter.inference_input_type = tf.uint8
# converter.inference_output_type = tf.int8
tflite_quant_model = converter.convert()
with open(f'saved_model_{H}x{W}/yolox_ti_body_head_hand_n_1x3x{H}x{W}_bgr_uint8.tflite', 'wb') as w:
w.write(tflite_quant_model)

"""
tfliteiorewriter -i saved_model_128x160/yolox_ti_body_head_hand_n_1x3x128x160_bgr_uint8.tflite
tfliteiorewriter -i saved_model_256x320/yolox_ti_body_head_hand_n_1x3x256x320_bgr_uint8.tflite
tfliteiorewriter -i saved_model_480x640/yolox_ti_body_head_hand_n_1x3x480x640_bgr_uint8.tflite
"""
1 change: 1 addition & 0 deletions 426_YOLOX-Body-Head-Hand/url.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
https://github.com/Megvii-BaseDetection/YOLOX
https://github.com/TexasInstruments/edgeai-yolox
https://github.com/Kazuhito00/YOLOX-Colaboratory-Training-Sample

https://github.com/PINTO0309/onnx2tf
Expand Down

0 comments on commit b8104dd

Please sign in to comment.