Files
opencv/modules/dnn
Savya Sanchi Sharma e6f8c94063 Merge pull request #30065 from SavyaSanchi-Sharma:fusion/shape
dnn: fold Shape of static-shape inputs in constFold() - #30065

When a graph input has a fully known shape, `constFold()` now replaces `Shape` ops on it with constants. The usual const-folding cascade then also removes the Gather/Concat/Cast/Slice ops that follow.

**What it adds**
- `graph_const_fold.cpp`: tracks known shapes and types through the graph during folding and folds `Shape` layers whose input shape is known.
- `net_impl2.cpp`: once a `Shape` has been folded, `setGraphInput()` rejects an input of a different shape instead of running with stale constants. It also fixes `tryInferGraphShapes()` to report untyped const or empty inputs as `Mat().type()`. The extra folding exposed a Resize type-check failure without this fix.
- `test_fusion.cpp`: two tests, one checking that no `Shape` layer remains after folding, one checking that a different input shape is rejected.

**Effect on graphs** (outputs identical to 5.x)
CRNN 84 → 34 layers, ViT/DeiT 361 → 274, BEiT 373 → 286, MobileViT_XS 352 → 289.

**Performance** (OCV/CPU, 32 threads; median of 5 interleaved runs per side)

| Model | 5.x, ms | PR, ms | Speedup |
|---|---|---|---|
| MobileViT_XS | 6.50 | 6.23 | 1.04x |
| BEiT_Base_Patch16_224 | 30.03 | 29.23 | 1.03x |
| VIT_Base_Patch16_224 | 27.17 | 26.53 | 1.02x |
| DeiT_Tiny_Patch16_224 | 4.90 | 4.81 | 1.02x |
| CRNN | 2.52 | 2.51 | 1.01x |
| FacePaint | 328.1 | 329.5 | 1.00x |

**Dependency**
This depends on the upcoming CUDA integration in DNN and should be merged after it.

### 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
- [x] The PR is proposed to the proper branch
- [ ] There is a reference to the original bug report and related work
- [x] There is accuracy test, performance test and test data in opencv_extra repository, if applicable
      Patch to opencv_extra has the same branch name.
- [x] The feature is well documented and sample code can be built with the project CMake
2026-09-29 08:39:46 +03:00
..
2026-07-28 20:27:29 +05:30