Repository navigation
dnn: read count_include_pad in the ONNX AveragePool importer 🤖🤖🤖 - #30842
Conversation
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
There was a problem hiding this comment.
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; |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
| "test_argmin_no_keepdims_random_select_last_index", | ||
| #endif | ||
| "test_averagepool_2d_pads_count_include_pad", | ||
| "test_averagepool_2d_precomputed_pads_count_include_pad", |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
|
@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.
|
@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. |
There was a problem hiding this comment.
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.
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.
Fixes #30834
parseAveragePoolin the 4.x ONNX importer choseave_pool_padded_areafrom the model'sproducer_nameonly (framework_name == "pytorch") and ignored the node'scount_include_padattribute. PyTorch models withnn.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 withcount_include_pad=1left the padding out.The importer now reads
count_include_padand 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_padandtest_averagepool_2d_precomputed_pads_count_include_padcome off theopencv_allconformance 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 inopencv_extra.Test_ONNX_layers.AveragePooling_exclude_padcovers the reported case: a PyTorch-exportednn.AvgPool2d(3, stride=1, padding=1, count_include_pad=False), where the explicitcount_include_pad=0has 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 theopencv_test_dnntests 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
Patch to opencv_extra has the same branch name.