Skip to content

Commit

Permalink
Add the missing yaml config for quantizing MP-PalmDet and improve qua…
Browse files Browse the repository at this point in the history
…ntized MP-PalmDet (#60)
  • Loading branch information
fengyuentau authored Jun 8, 2022
1 parent ab53e0a commit bf148ee
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 14 deletions.
2 changes: 0 additions & 2 deletions models/palm_detection_mediapipe/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ python demo.py
python demo.py -i /path/to/image
```

NOTE: For the quantized model, you will need to install OpenCV 4.6.0 to have asymmetric paddings support for quantized convolution layer in OpenCV. Score threshold needs to be adjusted as well for the quantized model, which is empirically 0.49.

### Example outputs

![webcam demo](./examples/mppalmdet_demo.gif)
Expand Down
Git LFS file not shown
40 changes: 40 additions & 0 deletions tools/quantize/inc_configs/mp_palmdet.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#
# Copyright (c) 2021 Intel Corporation
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

version: 1.0

model: # mandatory. used to specify model specific information.
name: mp_palmdet
framework: onnxrt_qlinearops # mandatory. supported values are tensorflow, pytorch, pytorch_ipex, onnxrt_integer, onnxrt_qlinear or mxnet; allow new framework backend extension.

quantization: # optional. tuning constraints on model-wise for advance user to reduce tuning space.
approach: post_training_static_quant # optional. default value is post_training_static_quant.
calibration:
dataloader:
batch_size: 1
dataset:
dummy:
shape: [1, 256, 256, 3]
low: -1.0
high: 1.0
dtype: float32
label: True

tuning:
accuracy_criterion:
relative: 0.02 # optional. default value is relative, other value is absolute. this example allows relative accuracy loss: 1%.
exit_policy:
timeout: 0 # optional. tuning timeout (seconds). default value is 0 which means early stop. combine with max_trials field to decide when to exit.
random_seed: 9527 # optional. random seed for deterministic tuning.
35 changes: 25 additions & 10 deletions tools/quantize/quantize-inc.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,14 @@ def run(self):
q_model.save(output_name)

class Dataset:
def __init__(self, root, size=None, toTensor=False):
def __init__(self, root, size=None, dim='chw', mean=0.0, std=1.0, swapRB=False, toFP32=False):
self.root = root
self.size = size
self.toTensor = toTensor
self.dim = dim
self.mean = mean
self.std = std
self.swapRB = swapRB
self.toFP32 = toFP32

self.image_list = self.load_image_list(self.root)

Expand All @@ -45,27 +49,38 @@ def load_image_list(self, path):

def __getitem__(self, idx):
img = cv.imread(self.image_list[idx])

if self.swapRB:
img = cv.cvtColor(img, cv.COLOR_BGR2RGB)

if self.size:
img = cv.resize(img, dsize=self.size)
if self.toTensor:
img = img.transpose(2, 0, 1) # hwc -> chw

if self.toFP32:
img = img.astype(np.float32)

img = img - self.mean
img = img / self.std

if self.dim == 'chw':
img = img.transpose(2, 0, 1) # hwc -> chw

return img, 1

def __len__(self):
return len(self.image_list)

models=dict(
mobilenetv1=Quantize(model_path='../../models/image_classification_mobilenet/image_classification_mobilenetv1_2022apr.onnx',
config_path='./inc_configs/mobilenet.yaml'),
config_path='./inc_configs/mobilenet.yaml'),
mobilenetv2=Quantize(model_path='../../models/image_classification_mobilenet/image_classification_mobilenetv2_2022apr.onnx',
config_path='./inc_configs/mobilenet.yaml'),
mppalm_det=Quantize(model_path='../../models/palm_detection_mediapipe/palm_detection_mediapipe_2022may.onnx',
config_path='./inc_configs/mppalmdet.yaml',
custom_dataset=Dataset(root='../../benchmark/data/palm_detection')),
config_path='./inc_configs/mobilenet.yaml'),
mp_palmdet=Quantize(model_path='../../models/palm_detection_mediapipe/palm_detection_mediapipe_2022may.onnx',
config_path='./inc_configs/mp_palmdet.yaml',
custom_dataset=Dataset(root='../../benchmark/data/palm_detection', dim='hwc', swapRB=True, mean=127.5, std=127.5, toFP32=True)),
lpd_yunet=Quantize(model_path='../../models/license_plate_detection_yunet/license_plate_detection_lpd_yunet_2022may.onnx',
config_path='./inc_configs/lpd_yunet.yaml',
custom_dataset=Dataset(root='../../benchmark/data/license_plate_detection', size=(320, 240), toTensor=True)),
custom_dataset=Dataset(root='../../benchmark/data/license_plate_detection', size=(320, 240), dim='chw', toFP32=True)),
)

if __name__ == '__main__':
Expand Down

0 comments on commit bf148ee

Please sign in to comment.