mirror of
https://github.com/opencv/opencv.git
synced 2026-07-21 19:33:03 +04:00
b887e2fd3e6a50d26621ca298029c52eb11b476c
dnn: add DynamicQuantizeLinear ONNX layer support #29018 ### Pull Request Readiness Checklist - [x] I agree to contribute to the project under Apache 2 License. - [x] To the best of my knowledge, the proposed patch is not based on a code under GPL or another license that is incompatible with OpenCV - [x] The PR is proposed to the proper branch - [x] There is a reference to the original bug report and target platform(s) - [x] There is an accuracy test - [ ] There is a performance test ### Description Implements the ONNX `DynamicQuantizeLinear` operator (opset 11) for the OpenCV DNN module. **What it does:** - Adds `QuantizeDynamicLayer` and `DequantizeDynamicLayer` layer classes - Registers layers and adds ONNX importer dispatch for `DynamicQuantizeLinear` - Computes scale and zero-point at runtime from activation min/max - Quantizes FP32 input to int8 (stored as uint8 - 128, matching OpenCV convention) **Framework limitation & workaround:** Due to the single-dtype-per-layer constraint in `LayerData::dtype` (see #29017), the float32 scale output cannot be passed through the CV_8S blob pipeline directly. As a workaround, the scale is encoded as 4 raw bytes in a CV_8S `{1,4}` blob using `memcpy`, and decoded by the downstream `DequantizeDynamic` layer. **Testing:** - Custom accuracy tests reproduce all 3 ONNX conformance test cases: `test_dynamicquantizelinear`, `test_dynamicquantizelinear_max_adjusted`, `test_dynamicquantizelinear_min_adjusted` - Each test verifies: quantized values, scale, zero point, and round-trip dequantize accuracy - All existing quantization regression tests pass (42/42) **Conformance tests:** The 6 conformance tests for `DynamicQuantizeLinear` remain in the parser denylist (they were already denylisted before this PR) because the framework cannot produce mixed-type outputs. The custom tests provide equivalent coverage. ### Files changed - `modules/dnn/include/opencv2/dnn/all_layers.hpp` — layer class declarations - `modules/dnn/src/init.cpp` — layer registration - `modules/dnn/src/onnx/onnx_importer.cpp` — ONNX import dispatch - `modules/dnn/src/int8layers/quantization_utils.cpp` — layer implementations - `modules/dnn/test/test_onnx_importer.cpp` — custom accuracy tests
OpenCV: Open Source Computer Vision Library
Resources
- Homepage: https://opencv.org
- Courses: https://opencv.org/courses
- Docs: https://docs.opencv.org/4.x/
- Q&A forum: https://forum.opencv.org
- previous forum (read only): http://answers.opencv.org
- Issue tracking: https://github.com/opencv/opencv/issues
- Additional OpenCV functionality: https://github.com/opencv/opencv_contrib
- Donate to OpenCV: https://opencv.org/support/
Contributing
Please read the contribution guidelines before starting work on a pull request.
Summary of the guidelines:
- One pull request per issue;
- Choose the right base branch;
- Include tests and documentation;
- Clean up "oops" commits before submitting;
- Follow the coding style guide.
Additional Resources
- Submit your OpenCV-based project for inclusion in Community Friday on opencv.org
- Subscribe to the OpenCV YouTube Channel featuring OpenCV Live, an hour-long streaming show
- Follow OpenCV on LinkedIn for daily posts showing the state-of-the-art in computer vision & AI
- Apply to be an OpenCV Volunteer to help organize events and online campaigns as well as amplify them
- Follow OpenCV on Mastodon in the Fediverse
- Follow OpenCV on Twitter
- OpenCV.ai: Computer Vision and AI development services from the OpenCV team.
Description
Languages
C++
87.6%
C
3.2%
Python
2.9%
CMake
2%
Java
1.5%
Other
2.6%