Skip to content

partial support for quantized models in ONNX importer#20264

Closed
vpisarev wants to merge 3 commits intoopencv:masterfrom
vpisarev:onnx_q
Closed

partial support for quantized models in ONNX importer#20264
vpisarev wants to merge 3 commits intoopencv:masterfrom
vpisarev:onnx_q

Conversation

@vpisarev
Copy link
Copy Markdown
Contributor

@vpisarev vpisarev commented Jun 12, 2021

See #20188. Merge together with opencv/opencv_extra#878.

  • Note that the code is not fully tested. There is just a smoke test that checks that one simple quantized model is loaded successfully, and then we can run inference on it and get 'some' result of the proper type and size. We need some real quantized deep nets to test the functionality properly.
  • QLinearConv is converted to a normal Convolution layer; QLinearMatMul is converted to MatMul. The original 8-bit weights and the quantization/de-quantization factors are preserved though, so Convlution and MatMul layers can, in principle, be extended to support 8-bit weights.
  • QuantizeLinear outputs FP32 data; it needs to be extended to output 8-bit tensors.
  • DequantizeLinear and QLinearAdd take FP32, as well as INT8/UINT8 inputs, but the output is always FP32 for now.

The PR is submitted to master branch, rather than 3.4, because the whole support for 8-bit compute paths will be added to master (see #20228)

Pull Request Readiness Checklist

  • I agree to contribute to the project under Apache 2 License.
  • To the best of my knowledge, the proposed patch is not based on a code under GPL or other license that is incompatible with OpenCV
  • The PR is proposed to proper branch
  • There is reference to original bug report and related work
  • There is accuracy test, performance test and test data in opencv_extra repository, if applicable
    Patch to opencv_extra has the same branch name.
  • The feature is well documented and sample code can be built with the project CMake
force_builders=Custom
build_image:Custom=ubuntu-openvino-2021.3.0:20.04
build_image:Custom Win=openvino-2021.3.0
build_image:Custom Mac=openvino-2021.3.0

test_modules:Custom=dnn,python2,python3,java
test_modules:Custom Win=dnn,python2,python3,java
test_modules:Custom Mac=dnn,python2,python3,java

buildworker:Custom=linux-1
# disabled due high memory usage: test_opencl:Custom=ON
test_opencl:Custom=OFF
test_bigdata:Custom=1
test_filter:Custom=*

…a file to opencv_extra/testdata/dnn/onnx/models subdirectory
static Ptr<DequantizeLinearLayer> create(const LayerParams& params);
};

class CV_EXPORTS QLinearAddLayer : public Layer
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It makes sense to add documentation with links on similar layers/nodes (e.g, from ONNX)


CV_Assert((inputs.size() > outputs.size() && blobs.empty()) ||
(!inputs.empty() && (blobs.size() == 1 || blobs.size() == 2)));
(!inputs.empty() && blobs.size() >= 1));
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what this class should do with blobs.size() = 3 ?

This is assertion check about the code below.
It is caller responsibility to properly call this code without assertions

Comment on lines +5 to +6
// Copyright (C) 2016, Intel Corporation, all rights reserved.
// Third party copyrights are property of their respective owners.
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

probably this is incorrect

int sc_total = (int)scale.total();
if (!scale.empty()) {
if(!((sc_total == 1 || (k == 1 && sc_total == outN)) && scale.type() == CV_32F))
CV_Error(CV_StsError,
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CV_StsError

C API constants should not be used. Here and below.

ASSERT_FALSE(net.empty());
net.setPreferableBackend(backend);
net.setPreferableTarget(target);
std::cout << net.dump() << "\n";
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if (cvtest::debugLevel > 0) or remove

net.setInput(input);
Mat output = net.forward();
//std::cout << output << std::endl;
ASSERT_EQ(output.size(), Size(128, 1));
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

*_EQ(expected_value, actual_value)

TEST_P(Test_ONNX_nets, QuantStatic)
{
if (backend != DNN_BACKEND_OPENCV || target != DNN_TARGET_CPU)
throw SkipTestException("Only the default backend; CPU target is supported");
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

default backend

default backend in builds with IE is IE, so this message should be corrected to be accurate.

@asmorkalov
Copy link
Copy Markdown
Contributor

@vpisarev Friendly reminder.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants