diff --git a/426_YOLOX-Body-Head-Hand/quantization/01_make_calib_data.py b/426_YOLOX-Body-Head-Hand/quantization/01_make_calib_data.py new file mode 100644 index 0000000000..b950a5d695 --- /dev/null +++ b/426_YOLOX-Body-Head-Hand/quantization/01_make_calib_data.py @@ -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) \ No newline at end of file diff --git a/426_YOLOX-Body-Head-Hand/quantization/02_int8.py b/426_YOLOX-Body-Head-Hand/quantization/02_int8.py new file mode 100644 index 0000000000..6064def2dd --- /dev/null +++ b/426_YOLOX-Body-Head-Hand/quantization/02_int8.py @@ -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 +""" \ No newline at end of file diff --git a/426_YOLOX-Body-Head-Hand/url.txt b/426_YOLOX-Body-Head-Hand/url.txt index 203a15bd17..5b66ed7a55 100644 --- a/426_YOLOX-Body-Head-Hand/url.txt +++ b/426_YOLOX-Body-Head-Hand/url.txt @@ -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