From 94c8b747f01c72003315374077050c647e98edc2 Mon Sep 17 00:00:00 2001 From: Hanbin Bae Date: Wed, 18 Feb 2026 00:29:45 +0900 Subject: [PATCH] Merge pull request #28425 from Anemptyship:fix/squeeze-all-dims MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- modules/dnn/src/onnx/onnx_importer.cpp | 8 ++++++++ modules/dnn/test/test_onnx_importer.cpp | 3 +++ 2 files changed, 11 insertions(+) diff --git a/modules/dnn/src/onnx/onnx_importer.cpp b/modules/dnn/src/onnx/onnx_importer.cpp index 5af114e803..11d24eadb8 100644 --- a/modules/dnn/src/onnx/onnx_importer.cpp +++ b/modules/dnn/src/onnx/onnx_importer.cpp @@ -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) diff --git a/modules/dnn/test/test_onnx_importer.cpp b/modules/dnn/test/test_onnx_importer.cpp index c1b50f3fcf..a69e94990b 100644 --- a/modules/dnn/test/test_onnx_importer.cpp +++ b/modules/dnn/test/test_onnx_importer.cpp @@ -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)