mirror of
https://github.com/opencv/opencv.git
synced 2026-11-25 02:38:14 -06:00
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
This commit is contained in:
committed by
GitHub
parent
8c54b0252c
commit
19672eb03f
@@ -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<int>("count_include_pad", framework_name == "pytorch") != 0;
|
||||
layerParams.set("ave_pool_padded_area", countIncludePad);
|
||||
addLayer(layerParams, node_proto);
|
||||
}
|
||||
|
||||
|
||||
@@ -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",
|
||||
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user