mirror of
https://github.com/opencv/opencv.git
synced 2026-11-19 07:45:26 -06:00
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