13from onnx
import version_converter
15from onnxruntime.quantization
import quantize_static, CalibrationDataReader, QuantType
17from transform
import Compose, Resize, CenterCrop, Normalize, ColorConvert
20 def __init__(self, model_path, image_dir, transforms):
21 model = onnx.load(model_path)
32 for image_name
in os.listdir(image_dir):
33 image_name_suffix = image_name.split(
'.')[-1].lower()
34 if image_name_suffix !=
'jpg' and image_name_suffix !=
'jpeg':
36 img = cv.imread(os.path.join(image_dir, image_name))
38 blob = cv.dnn.blobFromImage(img)
43 def __init__(self, model_path, calibration_image_dir, transforms=Compose(), per_channel=
False, act_type=
'int8', wt_type=
'int8'):
44 self.
type_dict = {
"uint8" : QuantType.QUInt8,
"int8" : QuantType.QInt8}
58 if model.opset_import[0].version != 11:
59 print(
'\tmodel opset version: {}. Converting to opset 11'.format(model.opset_import[0].version))
61 model_opset11 = version_converter.convert_version(model, 11)
63 output_name =
'{}-opset11.onnx'.format(self.
model_path[:-5])
64 onnx.save_model(model_opset11, output_name)
76 os.remove(
'augmented_model.onnx')
77 os.remove(
'{}-opt.onnx'.format(self.
model_path[:-5]))
78 print(
'\tQuantized model saved to {}'.format(output_name))
81 yunet=
Quantize(model_path=
'../../models/face_detection_yunet/face_detection_yunet_2022mar.onnx',
82 calibration_image_dir=
'../../benchmark/data/face_detection',
84 sface=
Quantize(model_path=
'../../models/face_recognition_sface/face_recognition_sface_2021dec.onnx',
85 calibration_image_dir=
'../../benchmark/data/face_recognition',
87 pphumenseg=
Quantize(model_path=
'../../models/human_segmentation_pphumanseg/human_segmentation_pphumanseg_2021oct.onnx',
88 calibration_image_dir=
'../../benchmark/data/human_segmentation',
90 ppresnet50=
Quantize(model_path=
'../../models/image_classification_ppresnet/image_classification_ppresnet50_2022jan.onnx',
91 calibration_image_dir=
'../../benchmark/data/image_classification',
94 youtureid=
Quantize(model_path=
'../../models/person_reid_youtureid/person_reid_youtu_2021nov.onnx',
95 calibration_image_dir=
'../../benchmark/data/person_reid',
98 crnn_en=
Quantize(model_path=
'../../models/text_recognition_crnn/text_recognition_CRNN_EN_2021sep.onnx',
99 calibration_image_dir=
'../../benchmark/data/text',
101 crnn_cn=
Quantize(model_path=
'../../models/text_recognition_crnn/text_recognition_CRNN_CN_2021nov.onnx',
102 calibration_image_dir=
'../../benchmark/data/text',
106if __name__ ==
'__main__':
108 for i
in range(1, len(sys.argv)):
109 selected_models.append(sys.argv[i])
110 if not selected_models:
111 selected_models = list(models.keys())
112 print(
'Models to be quantized: {}'.format(str(selected_models)))
114 for selected_model_name
in selected_models:
115 q = models[selected_model_name]
__init__(self, model_path, image_dir, transforms)
get_calibration_data(self, image_dir)
check_opset(self, convert=True)
__init__(self, model_path, calibration_image_dir, transforms=Compose(), per_channel=False, act_type='int8', wt_type='int8')