Files
opencv-MIRROR/modules/dnn/src
Savya Sanchi Sharma 02dcf56058 Merge pull request #30276 from SavyaSanchi-Sharma:opti/consts
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
2026-10-28 21:28:47 +03:00
..
2026-02-20 22:05:35 +03:00
2022-10-07 16:57:31 +00:00
2022-10-07 16:57:31 +00:00
2022-01-18 01:22:22 +03:00
2022-03-19 06:54:07 +00:00
2022-10-07 16:57:31 +00:00
2026-07-29 20:19:40 +05:30
2026-07-29 20:19:40 +05:30
2025-06-10 10:16:50 +03:00
2026-03-19 11:29:57 +03:00
2026-01-06 22:57:02 +08:00
2022-03-08 19:22:46 +00:00
2024-03-18 15:05:50 +04:00
2024-07-17 10:08:16 +03:00