Skip to content

dnn: read count_include_pad in the ONNX AveragePool importer 🤖🤖🤖 - #30842

Merged
asmorkalov merged 3 commits into
opencv:4.xfrom
usmanmateen:fix/onnx-avgpool-count-include-pad
Nov 19, 2026
Merged

asmorkalov merged 3 commits into
opencv:4.xfrom
usmanmateen:fix/onnx-avgpool-count-include-pad

Conversation

@usmanmateen

@usmanmateen usmanmateen commented Nov 15, 2026 •

Copy link
Copy Markdown
Contributor

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

  • I agree to contribute to the project under Apache 2 License.
  • 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

test_averagepool_2d_pads_count_include_pad and test_averagepool_2d_precomputed_pads_count_include_pad were on the opencv_all denylist because the importer ignored count_include_pad. See opencv#30834.
parseAveragePool chose whether padded values count towards the kernel area from the model's producer name only, so PyTorch models exported with count_include_pad=False averaged over the full kernel, and other models with count_include_pad=1 left the padding out. Read the attribute and keep the producer-based default when it is missing, as the 5.x importer does.

Fixes opencv#30834

@codex-abhishek-gola codex-abhishek-gola Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Status: 🟡 Yellow

The importer now honors the ONNX attribute while preserving the prior fallback for models that omit it, and the re-enabled conformance cases cover the include-padding direction. Before merge, add regression coverage for the reported PyTorch-producer case where an explicit zero must override that fallback; exact-head CI has not run yet.

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<int>("count_include_pad", framework_name == "pytorch") != 0;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The two conformance cases removed from the denylist both set count_include_pad=1; they cover the non-PyTorch direction only. The reported failure is the opposite branch: a model with producer_name="pytorch" and an explicit count_include_pad=0 must override the producer fallback. Please add a focused importer regression for that case so this exact bug cannot return while these generic conformance tests remain green.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added in 51e97ad: Test_ONNX_layers.AveragePooling_exclude_pad loads a PyTorch-exported nn.AvgPool2d(3, stride=1, padding=1, count_include_pad=False) (producer "pytorch", count_include_pad=0), with the model in opencv/opencv_extra#1466 on the same branch name. It fails without the importer change on OCV/CPU, OCV/OCL and OCV/OCL_FP16 (normInf 0.3855) and passes with it.

@asmorkalov asmorkalov added category: dnn pr: needs test New functionality requires minimal tests set category: dnn (onnx) ONNX suport issues in DNN module labels Nov 16, 2026
@asmorkalov asmorkalov added this to the 4.15.0 milestone Nov 16, 2026
@sanjayk9
sanjayk9 self-requested a review November 16, 2026 12:09
"test_argmin_no_keepdims_random_select_last_index",
#endif
"test_averagepool_2d_pads_count_include_pad",
"test_averagepool_2d_precomputed_pads_count_include_pad",

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These two tests are also skipped in the Vulkan and Halide denylists. Both backends fall back to the CPU Pooling layer for padded average pooling, so those entries should pass now too. The CUDA and OpenVINO entries probably have the same cause

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. Halide does run padded average pooling on the CPU layer (its supportBackend only takes AVE without padding), and the CUDA path reads avePoolPaddedArea, so those entries may well pass now. I can only run OCV/CPU and OpenCL here, so I left the Halide, Vulkan, CUDA and OpenVINO lists alone rather than change them untested. I can drop them in this PR if a maintainer can confirm on those backends.

@asmorkalov

Copy link
Copy Markdown
Contributor

@usmanmateen Friendly reminder.

The conformance tests re-enabled earlier only cover count_include_pad=1. This model comes from PyTorch, so the old producer-based default counted the padding, and the explicit count_include_pad=0 has to override it.
@usmanmateen

Copy link
Copy Markdown
Contributor Author

@asmorkalov Thanks for the reminder. I added the requested regression test in 51e97ad. Its model is in opencv/opencv_extra#1466, on the same branch name.

@Prasadayus
Prasadayus self-requested a review November 18, 2026 12:18

@codex-abhishek-gola codex-abhishek-gola Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Status: 🟢 Green

LGTM. The new PyTorch regression covers the previously missing producer_name=pytorch plus explicit count_include_pad=0 case, with its companion model and reference data in opencv_extra. The earlier finding is addressed; exact-head CI is running without an attributable failure.

@asmorkalov asmorkalov removed the pr: needs test New functionality requires minimal tests set label Nov 18, 2026
asmorkalov pushed a commit to opencv/opencv_extra that referenced this pull request Nov 19, 2026
PyTorch nn.AvgPool2d(3, stride=1, padding=1, count_include_pad=False), exported with producer_name "pytorch" and count_include_pad=0, for the importer test in opencv/opencv#30842.
@asmorkalov
asmorkalov merged commit 19672eb into opencv:4.x Nov 19, 2026
31 of 32 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

category: dnn (onnx) ONNX suport issues in DNN module category: dnn ci:full

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants