1
0
mirror of https://github.com/opencv/opencv.git synced 2026-07-21 19:33:03 +04:00

Merge pull request #28425 from Anemptyship:fix/squeeze-all-dims

DNN: Fix Squeeze to remove all size-1 dims when axes is empty #28425

Fixes #28424
OpenCV Extra: [opencv/opencv_extra#1308](https://github.com/opencv/opencv_extra/pull/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
This commit is contained in:
Hanbin Bae
2026-02-18 00:29:45 +09:00
committed by GitHub
parent 36b2bb70cf
commit 94c8b747f0
2 changed files with 11 additions and 0 deletions
+8
View File
@@ -2163,6 +2163,14 @@ void ONNXImporter::parseSqueeze(LayerParams& layerParams, const opencv_onnx::Nod
else
CV_Error(Error::StsNotImplemented, cv::format("ONNX/Squeeze: doesn't support non-constant 'axes' input"));
}
else
{
for (int i = 0; i < inpShape.size(); ++i)
{
if (inpShape[i] == 1)
maskedAxes[i] = true;
}
}
MatShape outShape;
for (int i = 0; i < inpShape.size(); ++i)
+3
View File
@@ -103,6 +103,8 @@ public:
net.setInput(inps[i], inputNames[i]);
Mat out = net.forward("");
EXPECT_EQ(shape(out), shape(ref));
if (useSoftmax)
{
LayerParams lp;
@@ -1187,6 +1189,7 @@ TEST_P(Test_ONNX_layers, Squeeze)
applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_MYRIAD, CV_TEST_TAG_DNN_SKIP_IE_NN_BUILDER);
testONNXModels("squeeze");
testONNXModels("squeeze_axes_op13");
testONNXModels("squeeze_no_axes");
}
TEST_P(Test_ONNX_layers, ReduceL2)