Skip to content

Test data for qconv asymmetric padding support#973

Merged
opencv-pushbot merged 1 commit intoopencv:4.xfrom
fengyuentau:qconv_asympad
May 19, 2022
Merged

Test data for qconv asymmetric padding support#973
opencv-pushbot merged 1 commit intoopencv:4.xfrom
fengyuentau:qconv_asympad

Conversation

@fengyuentau
Copy link
Copy Markdown
Member

Related PR: opencv/opencv#21991

The model for testing is quantized from https://github.com/opencv/opencv_extra/blob/4.x/testdata/dnn/onnx/models/conv_asymmetric_pads.onnx with neural compressor. The reason why onnxruntime cannot be used for quantization is descriped in microsoft/onnxruntime#11525, which is fake quantization with onnxruntime in short.

YAML config file "qconv_asympad.yaml" for quantization using neural compressor:

version: 1.0

model:
  name: mppalmdet
  framework: onnxrt_qlinearops

quantization:
  approach: post_training_static_quant
  calibration:
    dataloader:
      batch_size: 1
      dataset:
          dummy:
              shape: [1, 2, 3, 4]
              low: -1.0
              high: 1.0
              dtype: float32
              label: True
      transform:
        Rescale: {}
        Cast:
          dtype: float32

tuning:
  accuracy_criterion:
    relative:  0.02
  exit_policy:
    timeout: 0
  random_seed: 9527

Python script for quantization using neural compressor with the config above:

import os
import numpy as np
from neural_compressor.experimental import Quantization, common

model_name = 'conv_asymmetric_pads.onnx'
model_path = os.path.join('/path/to/opencv_extra/testdata/dnn/onnx/models', model_name)
qmodel_path = 'quantized_{}_int8_weights.onnx'.format(model_name[:-5])

config_path = '/path/to/qconv_asympad.yaml'

class RandomDataset:
    def __init__(self):
        self.inputs = [np.random.randn(2, 3, 4).astype(np.float32)]

    def __getitem__(self, idx):
        return self.inputs[idx], 1

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

model = onnx.load(model_path)
quantizer = Quantization()
quantizer.calib_dataloader = common.DataLoader(RandomDataset())
quantizer.model = common.Model(model)
q_model = quantizer()
q_model.save(qmodel_path)


# generate input & output data
import onnxruntime as rt
sess = rt.InferenceSession(qmodel_path, None)
input = np.random.uniform(-1, 1, sess.get_inputs()[0].shape).astype("float32")
output = sess.run([sess.get_outputs()[0].name], {sess.get_inputs()[0].name : input})[0]

print("input has sizes {}".format(input.shape))
input_files = os.path.join("data", "input_" + name)
np.save(input_files, input.data)

print("output has sizes {}".format(output.shape))
output_files = os.path.join("data", "output_" + name)
np.save(output_files, np.ascontiguousarray(output.data))

@opencv opencv deleted a comment from yifan2 May 16, 2022
@opencv-pushbot opencv-pushbot merged commit 9c33634 into opencv:4.x May 19, 2022
@alalek alalek mentioned this pull request Aug 21, 2022
@fengyuentau fengyuentau deleted the qconv_asympad branch December 3, 2024 03:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants