7from neural_compressor.experimental
import Quantization, common
10 def __init__(self, model_path, config_path, custom_dataset=None):
16 print(
'Quantizing (int8) with Intel\'s Neural Compressor:')
20 output_name =
'{}-int8-quantized.onnx'.format(self.
model_path[:-5])
25 quantizer.calib_dataloader = common.DataLoader(self.
custom_dataset)
26 quantizer.model = common.Model(model)
28 q_model.save(output_name)
31 def __init__(self, root, size=None, dim='chw', mean=0.0, std=1.0, swapRB=False, toFP32=False):
44 for f
in os.listdir(path):
45 if not f.endswith(
'.jpg'):
47 image_list.append(os.path.join(path, f))
54 img = cv.cvtColor(img, cv.COLOR_BGR2RGB)
57 img = cv.resize(img, dsize=self.
size)
60 img = img.astype(np.float32)
66 img = img.transpose(2, 0, 1)
74 mobilenetv1=
Quantize(model_path=
'../../models/image_classification_mobilenet/image_classification_mobilenetv1_2022apr.onnx',
75 config_path=
'./inc_configs/mobilenet.yaml'),
76 mobilenetv2=
Quantize(model_path=
'../../models/image_classification_mobilenet/image_classification_mobilenetv2_2022apr.onnx',
77 config_path=
'./inc_configs/mobilenet.yaml'),
78 mp_palmdet=
Quantize(model_path=
'../../models/palm_detection_mediapipe/palm_detection_mediapipe_2022may.onnx',
79 config_path=
'./inc_configs/mp_palmdet.yaml',
80 custom_dataset=
Dataset(root=
'../../benchmark/data/palm_detection', dim=
'hwc', swapRB=
True, mean=127.5, std=127.5, toFP32=
True)),
81 lpd_yunet=
Quantize(model_path=
'../../models/license_plate_detection_yunet/license_plate_detection_lpd_yunet_2022may.onnx',
82 config_path=
'./inc_configs/lpd_yunet.yaml',
83 custom_dataset=
Dataset(root=
'../../benchmark/data/license_plate_detection', size=(320, 240), dim=
'chw', toFP32=
True)),
86if __name__ ==
'__main__':
88 for i
in range(1, len(sys.argv)):
89 selected_models.append(sys.argv[i])
90 if not selected_models:
91 selected_models = list(models.keys())
92 print(
'Models to be quantized: {}'.format(str(selected_models)))
94 for selected_model_name
in selected_models:
95 q = models[selected_model_name]
__init__(self, root, size=None, dim='chw', mean=0.0, std=1.0, swapRB=False, toFP32=False)
load_image_list(self, path)
__init__(self, model_path, config_path, custom_dataset=None)