mirror of
https://github.com/opencv/opencv.git
synced 2026-12-03 05:33:03 -06:00
dnn: share equal constants and merge duplicate nodes in the ONNX importer - #30276 # Summary The ONNX importer now gives initializers with the same type, shape and bytes a single constant, and `parseNode()` skips a node that has the same op, attributes and inputs as an earlier one, reusing that node's outputs instead. Many exported models repeat identical work: RAFT pads the same correlation pyramid again in each of its 10 refinement iterations, and each iteration's `pads` constant has a different name, so the Pads only look identical when constants are compared by value. With this change RAFT runs 5 Pads instead of 41, as ONNX Runtime does. Custom-domain ops, random ops, nodes with subgraphs and nodes producing graph outputs are never merged. Neither are `QuantizeLinear`/`DequantizeLinear`: `fuseQDQ()` only folds a DQ into MatMul/Gemm/Conv while it has a single consumer, so merging them would switch off int8 fusion. Outputs match ONNX Runtime as closely as before. | Model | This PR | OpenCV 5.0.0 | ORT 1.30 | vs 5.0.0 | vs ORT 1.30 | |---|---:|---:|---:|---:|---:| | RAFT | 392.4 ms | 544.5 ms | 291.3 ms | 1.39× faster | 0.74× | | NAFNet | 1815.8 ms | 2157.6 ms | 1532.6 ms | 1.19× faster | 0.84× | | YOLOX | 28.3 ms | 29.8 ms | 15.1 ms | 1.05× faster | 0.53× | > Benchmarked on AMD Ryzen 9 9950X ### Pull Request Readiness Checklist See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request - [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 - [ ] The PR is proposed to the proper branch - [ ] There is a reference to the 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