ytfeng commited on
Commit
80eebd6
·
1 Parent(s): 160af44

fix model quantization to support timvx backend (#209)

Browse files

* fix model quantization to support timvx backend

* update table

benchmark/README.md CHANGED
@@ -350,6 +350,7 @@ Benchmarking ...
350
  backend=cv.dnn.DNN_BACKEND_TIMVX
351
  target=cv.dnn.DNN_TARGET_NPU
352
  mean median min input size model
 
353
  45.83 47.06 43.04 [150, 150] SFace with ['face_recognition_sface_2021dec_int8.onnx']
354
  29.20 27.55 26.25 [112, 112] FacialExpressionRecog with ['facial_expression_recognition_mobilefacenet_2022july_int8.onnx']
355
  18.47 18.16 17.96 [224, 224] MPHandPose with ['handpose_estimation_mediapipe_2023feb_int8.onnx']
@@ -357,6 +358,7 @@ mean median min input size model
357
  149.05 155.10 144.42 [224, 224] MobileNet with ['image_classification_mobilenetv1_2022apr_int8.onnx']
358
  147.40 147.49 135.90 [224, 224] MobileNet with ['image_classification_mobilenetv2_2022apr_int8.onnx']
359
  75.91 79.27 71.98 [224, 224] PPResNet with ['image_classification_ppresnet50_2022jan_int8.onnx']
 
360
  117.71 119.69 107.37 [416, 416] NanoDet with ['object_detection_nanodet_2022nov_int8.onnx']
361
  379.46 366.19 360.02 [640, 640] YoloX with ['object_detection_yolox_2022nov_int8.onnx']
362
  33.90 36.32 31.71 [192, 192] MPPalmDet with ['palm_detection_mediapipe_2023feb_int8.onnx']
 
350
  backend=cv.dnn.DNN_BACKEND_TIMVX
351
  target=cv.dnn.DNN_TARGET_NPU
352
  mean median min input size model
353
+ 5.08 4.72 4.70 [160, 120] YuNet with ['face_detection_yunet_2023mar_int8.onnx']
354
  45.83 47.06 43.04 [150, 150] SFace with ['face_recognition_sface_2021dec_int8.onnx']
355
  29.20 27.55 26.25 [112, 112] FacialExpressionRecog with ['facial_expression_recognition_mobilefacenet_2022july_int8.onnx']
356
  18.47 18.16 17.96 [224, 224] MPHandPose with ['handpose_estimation_mediapipe_2023feb_int8.onnx']
 
358
  149.05 155.10 144.42 [224, 224] MobileNet with ['image_classification_mobilenetv1_2022apr_int8.onnx']
359
  147.40 147.49 135.90 [224, 224] MobileNet with ['image_classification_mobilenetv2_2022apr_int8.onnx']
360
  75.91 79.27 71.98 [224, 224] PPResNet with ['image_classification_ppresnet50_2022jan_int8.onnx']
361
+ 30.98 30.56 29.36 [320, 240] LPD_YuNet with ['license_plate_detection_lpd_yunet_2023mar_int8.onnx']
362
  117.71 119.69 107.37 [416, 416] NanoDet with ['object_detection_nanodet_2022nov_int8.onnx']
363
  379.46 366.19 360.02 [640, 640] YoloX with ['object_detection_yolox_2022nov_int8.onnx']
364
  33.90 36.32 31.71 [192, 192] MPPalmDet with ['palm_detection_mediapipe_2023feb_int8.onnx']
benchmark/color_table.svg CHANGED
tools/quantize/quantize-ort.py CHANGED
@@ -46,7 +46,7 @@ class DataReader(CalibrationDataReader):
46
  return blobs
47
 
48
  class Quantize:
49
- def __init__(self, model_path, calibration_image_dir, transforms=Compose(), per_channel=False, act_type='int8', wt_type='int8', data_dim='chw'):
50
  self.type_dict = {"uint8" : QuantType.QUInt8, "int8" : QuantType.QInt8}
51
 
52
  self.model_path = model_path
@@ -55,6 +55,7 @@ class Quantize:
55
  self.per_channel = per_channel
56
  self.act_type = act_type
57
  self.wt_type = wt_type
 
58
 
59
  # data reader
60
  self.dr = DataReader(self.model_path, self.calibration_image_dir, self.transforms, data_dim)
@@ -80,15 +81,18 @@ class Quantize:
80
  quant_format=QuantFormat.QOperator, # start from onnxruntime==1.11.0, quant_format is set to QuantFormat.QDQ by default, which performs fake quantization
81
  per_channel=self.per_channel,
82
  weight_type=self.type_dict[self.wt_type],
83
- activation_type=self.type_dict[self.act_type])
 
84
  if new_model_path != self.model_path:
85
  os.remove(new_model_path)
86
  print('\tQuantized model saved to {}'.format(output_name))
87
 
88
  models=dict(
89
- yunet=Quantize(model_path='../../models/face_detection_yunet/face_detection_yunet_2022mar.onnx',
90
  calibration_image_dir='../../benchmark/data/face_detection',
91
- transforms=Compose([Resize(size=(160, 120))])),
 
 
92
  sface=Quantize(model_path='../../models/face_recognition_sface/face_recognition_sface_2021dec.onnx',
93
  calibration_image_dir='../../benchmark/data/face_recognition',
94
  transforms=Compose([Resize(size=(112, 112))])),
@@ -119,7 +123,9 @@ models=dict(
119
  ColorConvert(ctype=cv.COLOR_BGR2RGB)]), data_dim='hwc'),
120
  lpd_yunet=Quantize(model_path='../../models/license_plate_detection_yunet/license_plate_detection_lpd_yunet_2023mar.onnx',
121
  calibration_image_dir='../../benchmark/data/license_plate_detection',
122
- transforms=Compose([Resize(size=(320, 240))])),
 
 
123
  )
124
 
125
  if __name__ == '__main__':
 
46
  return blobs
47
 
48
  class Quantize:
49
+ def __init__(self, model_path, calibration_image_dir, transforms=Compose(), per_channel=False, act_type='int8', wt_type='int8', data_dim='chw', nodes_to_exclude=[]):
50
  self.type_dict = {"uint8" : QuantType.QUInt8, "int8" : QuantType.QInt8}
51
 
52
  self.model_path = model_path
 
55
  self.per_channel = per_channel
56
  self.act_type = act_type
57
  self.wt_type = wt_type
58
+ self.nodes_to_exclude = nodes_to_exclude
59
 
60
  # data reader
61
  self.dr = DataReader(self.model_path, self.calibration_image_dir, self.transforms, data_dim)
 
81
  quant_format=QuantFormat.QOperator, # start from onnxruntime==1.11.0, quant_format is set to QuantFormat.QDQ by default, which performs fake quantization
82
  per_channel=self.per_channel,
83
  weight_type=self.type_dict[self.wt_type],
84
+ activation_type=self.type_dict[self.act_type],
85
+ nodes_to_exclude=self.nodes_to_exclude)
86
  if new_model_path != self.model_path:
87
  os.remove(new_model_path)
88
  print('\tQuantized model saved to {}'.format(output_name))
89
 
90
  models=dict(
91
+ yunet=Quantize(model_path='../../models/face_detection_yunet/face_detection_yunet_2023mar.onnx',
92
  calibration_image_dir='../../benchmark/data/face_detection',
93
+ transforms=Compose([Resize(size=(160, 120))]),
94
+ nodes_to_exclude=['MaxPool_5', 'MaxPool_18', 'MaxPool_25', 'MaxPool_32'],
95
+ ),
96
  sface=Quantize(model_path='../../models/face_recognition_sface/face_recognition_sface_2021dec.onnx',
97
  calibration_image_dir='../../benchmark/data/face_recognition',
98
  transforms=Compose([Resize(size=(112, 112))])),
 
123
  ColorConvert(ctype=cv.COLOR_BGR2RGB)]), data_dim='hwc'),
124
  lpd_yunet=Quantize(model_path='../../models/license_plate_detection_yunet/license_plate_detection_lpd_yunet_2023mar.onnx',
125
  calibration_image_dir='../../benchmark/data/license_plate_detection',
126
+ transforms=Compose([Resize(size=(320, 240))]),
127
+ nodes_to_exclude=['MaxPool_5', 'MaxPool_18', 'MaxPool_25', 'MaxPool_32', 'MaxPool_39'],
128
+ ),
129
  )
130
 
131
  if __name__ == '__main__':