From 19672eb03fa8fe5eacfc78624380d2d4e361b580 Mon Sep 17 00:00:00 2001 From: Muhammad Usman Mateen <74776275+usmanmateen@users.noreply.github.com> Date: Thu, 19 Nov 2026 05:44:27 +0000 Subject: [PATCH] Merge pull request #30842 from usmanmateen/fix/onnx-avgpool-count-include-pad MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- modules/dnn/src/onnx/onnx_importer.cpp | 5 ++++- ...nnx_conformance_layer_filter_opencv_all_denylist.inl.hpp | 2 -- modules/dnn/test/test_onnx_importer.cpp | 6 ++++++ 3 files changed, 10 insertions(+), 3 deletions(-) diff --git a/modules/dnn/src/onnx/onnx_importer.cpp b/modules/dnn/src/onnx/onnx_importer.cpp index b4dc3e1573..75b2d2a928 100644 --- a/modules/dnn/src/onnx/onnx_importer.cpp +++ b/modules/dnn/src/onnx/onnx_importer.cpp @@ -1135,7 +1135,10 @@ void ONNXImporter::parseAveragePool(LayerParams& layerParams, const opencv_onnx: layerParams.type = "Pooling"; layerParams.set("pool", "AVE"); setCeilMode(layerParams); - layerParams.set("ave_pool_padded_area", framework_name == "pytorch"); + // count_include_pad says whether padded values count towards the kernel area. + // Keep the previous producer-based default when the attribute is missing. + const bool countIncludePad = layerParams.get("count_include_pad", framework_name == "pytorch") != 0; + layerParams.set("ave_pool_padded_area", countIncludePad); addLayer(layerParams, node_proto); } diff --git a/modules/dnn/test/test_onnx_conformance_layer_filter_opencv_all_denylist.inl.hpp b/modules/dnn/test/test_onnx_conformance_layer_filter_opencv_all_denylist.inl.hpp index 0370b22764..48d0037d84 100644 --- a/modules/dnn/test/test_onnx_conformance_layer_filter_opencv_all_denylist.inl.hpp +++ b/modules/dnn/test/test_onnx_conformance_layer_filter_opencv_all_denylist.inl.hpp @@ -34,8 +34,6 @@ "test_argmin_no_keepdims_random", "test_argmin_no_keepdims_random_select_last_index", #endif -"test_averagepool_2d_pads_count_include_pad", -"test_averagepool_2d_precomputed_pads_count_include_pad", "test_averagepool_2d_same_lower", "test_cast_FLOAT_to_STRING", "test_cast_STRING_to_FLOAT", diff --git a/modules/dnn/test/test_onnx_importer.cpp b/modules/dnn/test/test_onnx_importer.cpp index 66889e097b..8954df1519 100644 --- a/modules/dnn/test/test_onnx_importer.cpp +++ b/modules/dnn/test/test_onnx_importer.cpp @@ -829,6 +829,12 @@ TEST_P(Test_ONNX_layers, AveragePooling) testONNXModels("average_pooling"); } +// PyTorch model with count_include_pad=0: the attribute must override the producer-based default +TEST_P(Test_ONNX_layers, AveragePooling_exclude_pad) +{ + testONNXModels("average_pooling_exclude_pad"); +} + TEST_P(Test_ONNX_layers, MaxPooling3D) { #if defined(INF_ENGINE_RELEASE) && INF_ENGINE_VER_MAJOR_EQ(2022010000)