Skip to content

Commit 94c8b74

Browse files
authored
Merge pull request opencv#28425 from Anemptyship:fix/squeeze-all-dims
DNN: Fix Squeeze to remove all size-1 dims when axes is empty opencv#28425 Fixes opencv#28424 OpenCV Extra: [opencv/opencv_extra#1308](opencv/opencv_extra#1308) This PR fixes the ONNX Squeeze operator to correctly remove all size-1 dimensions when `axes` is not provided, conforming to the ONNX specification. ### Details Per [ONNX Squeeze specification](https://onnx.ai/onnx/operators/onnx__Squeeze.html): > 'If axes is not provided, all the single dimensions will be removed from the shape.' Previously, OpenCV DNN would not remove any dimensions in this case, causing shape mismatch errors with models like LaMa (inpainting). ### Example ```python # Input: [1, 1, 2, 4] # Squeeze with no axes attribute # Before: [1, 1, 2, 4] ✗ (No change) # After: [2, 4] ✓ (matches ONNX Runtime) ``` ### Tests Added `testONNXModels("squeeze_no_axes")` which validates this behavior with new test data. opencv_extra_pr=opencv/opencv_extra#1324 ### 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 (4.x for bug fixes) - [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. - [x] The feature is well documented and sample code can be built with the project CMake
1 parent 36b2bb7 commit 94c8b74

2 files changed

Lines changed: 11 additions & 0 deletions

File tree

modules/dnn/src/onnx/onnx_importer.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2163,6 +2163,14 @@ void ONNXImporter::parseSqueeze(LayerParams& layerParams, const opencv_onnx::Nod
21632163
else
21642164
CV_Error(Error::StsNotImplemented, cv::format("ONNX/Squeeze: doesn't support non-constant 'axes' input"));
21652165
}
2166+
else
2167+
{
2168+
for (int i = 0; i < inpShape.size(); ++i)
2169+
{
2170+
if (inpShape[i] == 1)
2171+
maskedAxes[i] = true;
2172+
}
2173+
}
21662174

21672175
MatShape outShape;
21682176
for (int i = 0; i < inpShape.size(); ++i)

modules/dnn/test/test_onnx_importer.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,8 @@ class Test_ONNX_layers : public DNNTestLayer
103103
net.setInput(inps[i], inputNames[i]);
104104
Mat out = net.forward("");
105105

106+
EXPECT_EQ(shape(out), shape(ref));
107+
106108
if (useSoftmax)
107109
{
108110
LayerParams lp;
@@ -1187,6 +1189,7 @@ TEST_P(Test_ONNX_layers, Squeeze)
11871189
applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_MYRIAD, CV_TEST_TAG_DNN_SKIP_IE_NN_BUILDER);
11881190
testONNXModels("squeeze");
11891191
testONNXModels("squeeze_axes_op13");
1192+
testONNXModels("squeeze_no_axes");
11901193
}
11911194

11921195
TEST_P(Test_ONNX_layers, ReduceL2)

0 commit comments

Comments
 (0)