-
-
Notifications
You must be signed in to change notification settings - Fork 583
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
84 additions
and
0 deletions.
There are no files selected for viewing
24 changes: 24 additions & 0 deletions
24
426_YOLOX-Body-Head-Hand/quantization/01_make_calib_data.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters