Muhammad Usman Mateen 19672eb03f Merge pull request #30842 from usmanmateen/fix/onnx-avgpool-count-include-pad
dnn: read count_include_pad in the ONNX AveragePool importer 🤖🤖🤖 - #30842

Fixes #30834

`parseAveragePool` in the 4.x ONNX importer chose `ave_pool_padded_area` from the model's `producer_name` only (`framework_name == "pytorch"`) and ignored the node's `count_include_pad` attribute. PyTorch models with `nn.AvgPool2d(..., padding=p, count_include_pad=False)` (PoolFormer, timm Inception-v4 and Inception-ResNet-v2) therefore divided border windows by the full kernel area, and non-PyTorch models with `count_include_pad=1` left the padding out.

The importer now reads `count_include_pad` and falls back to the old producer-based default when the attribute is missing, the same way the 5.x importer does. Models without the attribute behave as before.

Tests: `test_averagepool_2d_pads_count_include_pad` and `test_averagepool_2d_precomputed_pads_count_include_pad` come off the `opencv_all` conformance denylist. Without the importer change they fail on OCV/CPU, OCV/OCL and OCV/OCL_FP16 (normInf 1.568 and 12.16), and with it they pass. Their test data is already in `opencv_extra`.

`Test_ONNX_layers.AveragePooling_exclude_pad` covers the reported case: a PyTorch-exported `nn.AvgPool2d(3, stride=1, padding=1, count_include_pad=False)`, where the explicit `count_include_pad=0` has to override the PyTorch default. The model is in opencv/opencv_extra#1466. Without the importer change the test fails on OCV/CPU, OCV/OCL and OCV/OCL_FP16 (normInf 0.3855), and with it the test passes.

Tested on Ubuntu 24.04 (WSL2), x86-64, GCC 15.2.0, Release, `WITH_OPENCL=ON`, OpenCL through PoCL on the CPU. With and without the patch, I ran the `opencv_test_dnn` tests matching `*averagepool*:*AvePool*:*AveragePool*:*Pooling*:*pool*:Test_ONNX_conformance.*:Test_ONNX_layers.*`. Both runs passed the same 4137 tests, and the only difference is that the six re-enabled cases run (and pass) instead of being skipped. The repro from the issue prints all ones after the fix, matching PyTorch.

### 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
- [x] 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.
- [ ] The feature is well documented and sample code can be built with the project CMake
2026-11-19 08:44:27 +03:00
2026-09-17 12:00:11 +03:00
2020-02-26 15:12:45 +03:00
2026-07-09 12:35:42 +03:00
2018-10-11 17:57:51 +00:00
2025-08-01 09:50:10 +03:00

OpenCV: Open Source Computer Vision Library

Resources

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

Description
Languages
C++ 87.6%
C 3.1%
Python 2.9%
CMake 2%
Java 1.5%
Other 2.7%