From ac7d331a7985d58dd2e195f2263d0f02893af30e Mon Sep 17 00:00:00 2001 From: Ishita Singh Date: Tue, 1 Dec 2026 13:49:15 +0530 Subject: [PATCH] dnn: keep Flatten axis attributes unchanged in finalize FlattenLayerImpl::finalize previously overwrote _startAxis and _endAxis with normalize_axis(..., numAxes) for the rank of the first input. Since end_axis defaults to -1 (and axis can also be negative), this replaced relative negative indices with positive indices bound to the first input rank. Reusing the same Flatten layer on an input of a different rank then: - flattened the wrong range of dimensions when rank increased (e.g. [2, 3, 4, 5] flattened to [2, 12, 5] instead of [2, 60] for default axis=1, end_axis=-1), or - triggered an assertion failure in normalize_axis when rank decreased (e.g. stale end_axis=3 on a 3D input). getMemoryShapes, initCann, and initNgraph already normalize _startAxis and _endAxis locally, and forward/forward_ocl/initCUDA use the output shapes directly. Keep _startAxis and _endAxis unchanged in finalize while still validating the normalized axis range. --- modules/dnn/src/layers/flatten_layer.cpp | 6 +- modules/dnn/test/test_layers.cpp | 121 +++++++++++++++++++++++ 2 files changed, 125 insertions(+), 2 deletions(-) diff --git a/modules/dnn/src/layers/flatten_layer.cpp b/modules/dnn/src/layers/flatten_layer.cpp index 48950601f2..ea66a55306 100644 --- a/modules/dnn/src/layers/flatten_layer.cpp +++ b/modules/dnn/src/layers/flatten_layer.cpp @@ -123,10 +123,12 @@ public: { std::vector inputs; inputs_arr.getMatVector(inputs); + CV_Assert(!inputs.empty()); int numAxes = inputs[0].dims; - _startAxis = normalize_axis(_startAxis, numAxes); - _endAxis = normalize_axis(_endAxis, numAxes); + int startAxis = normalize_axis(_startAxis, numAxes); + int endAxis = normalize_axis(_endAxis, numAxes); + CV_Assert(startAxis >= 0 && endAxis >= startAxis && endAxis < numAxes); } #ifdef HAVE_OPENCL diff --git a/modules/dnn/test/test_layers.cpp b/modules/dnn/test/test_layers.cpp index 44ea82aab3..ceb781b2fb 100644 --- a/modules/dnn/test/test_layers.cpp +++ b/modules/dnn/test/test_layers.cpp @@ -3551,4 +3551,125 @@ TEST(Layer_Test_TopK_Axis, keepsNegativeAxisDecreasingRank) normAssert(outputs[1], Mat(2, 1, CV_32F, expected_idx)); } +TEST(Layer_Test_Flatten_Axis, keepsNegativeEndAxisIncreasingRank) +{ + LayerParams lp; + lp.type = "Flatten"; + lp.name = "test_flatten_end_inc"; + lp.set("axis", 1); + lp.set("end_axis", -1); + + Ptr layer = LayerFactory::createLayerInstance(lp.type, lp); + ASSERT_TRUE(layer); + + const int sz3[] = {2, 3, 4}; + Mat inp3(3, sz3, CV_32F); + float* p3 = inp3.ptr(); + for (size_t i = 0; i < inp3.total(); ++i) + p3[i] = static_cast(i); + + // First run: 3D input [2, 3, 4], end_axis -1 is axis 2 -> [2, 12] + std::vector inputs{inp3.clone()}, outputs; + runLayer(layer, inputs, outputs); + ASSERT_EQ(outputs.size(), (size_t)1); + EXPECT_EQ(shape(outputs[0]), shape(2, 12)); + normAssert(outputs[0], inp3.reshape(1, {2, 12})); + + const int sz4[] = {2, 3, 4, 5}; + Mat inp4(4, sz4, CV_32F); + float* p4 = inp4.ptr(); + for (size_t i = 0; i < inp4.total(); ++i) + p4[i] = static_cast(i); + + // Second run on the same layer: 4D input [2, 3, 4, 5], end_axis -1 must now be axis 3 -> [2, 60] + inputs = {inp4.clone()}; + runLayer(layer, inputs, outputs); + ASSERT_EQ(outputs.size(), (size_t)1); + EXPECT_EQ(shape(outputs[0]), shape(2, 60)); + normAssert(outputs[0], inp4.reshape(1, {2, 60})); +} + +TEST(Layer_Test_Flatten_Axis, keepsNegativeEndAxisDecreasingRank) +{ + LayerParams lp; + lp.type = "Flatten"; + lp.name = "test_flatten_end_dec"; + lp.set("axis", 1); + lp.set("end_axis", -1); + + Ptr layer = LayerFactory::createLayerInstance(lp.type, lp); + ASSERT_TRUE(layer); + + const int sz4[] = {2, 3, 4, 5}; + Mat inp4(4, sz4, CV_32F); + float* p4 = inp4.ptr(); + for (size_t i = 0; i < inp4.total(); ++i) + p4[i] = static_cast(i); + + // First run: 4D input [2, 3, 4, 5], end_axis -1 is axis 3 -> [2, 60] + std::vector inputs{inp4.clone()}, outputs; + runLayer(layer, inputs, outputs); + ASSERT_EQ(outputs.size(), (size_t)1); + EXPECT_EQ(shape(outputs[0]), shape(2, 60)); + normAssert(outputs[0], inp4.reshape(1, {2, 60})); + + const int sz3[] = {2, 3, 4}; + Mat inp3(3, sz3, CV_32F); + float* p3 = inp3.ptr(); + for (size_t i = 0; i < inp3.total(); ++i) + p3[i] = static_cast(i); + + // Second run on the same layer: 3D input [2, 3, 4], end_axis -1 must now be axis 2 -> [2, 12] + inputs = {inp3.clone()}; + runLayer(layer, inputs, outputs); + ASSERT_EQ(outputs.size(), (size_t)1); + EXPECT_EQ(shape(outputs[0]), shape(2, 12)); + normAssert(outputs[0], inp3.reshape(1, {2, 12})); +} + +TEST(Layer_Test_Flatten_Axis, keepsNegativeStartAndEndAxisAcrossRanks) +{ + LayerParams lp; + lp.type = "Flatten"; + lp.name = "test_flatten_start_end"; + lp.set("axis", -2); + lp.set("end_axis", -1); + + Ptr layer = LayerFactory::createLayerInstance(lp.type, lp); + ASSERT_TRUE(layer); + + const int sz3[] = {2, 3, 4}; + Mat inp3(3, sz3, CV_32F); + float* p3 = inp3.ptr(); + for (size_t i = 0; i < inp3.total(); ++i) + p3[i] = static_cast(i); + + // First run: 3D input [2, 3, 4], axes [-2, -1] -> [1, 2] -> [2, 12] + std::vector inputs{inp3.clone()}, outputs; + runLayer(layer, inputs, outputs); + ASSERT_EQ(outputs.size(), (size_t)1); + EXPECT_EQ(shape(outputs[0]), shape(2, 12)); + normAssert(outputs[0], inp3.reshape(1, {2, 12})); + + const int sz4[] = {2, 3, 4, 5}; + Mat inp4(4, sz4, CV_32F); + float* p4 = inp4.ptr(); + for (size_t i = 0; i < inp4.total(); ++i) + p4[i] = static_cast(i); + + // Second run: 4D input [2, 3, 4, 5], axes [-2, -1] -> [2, 3] -> [2, 3, 20] + inputs = {inp4.clone()}; + runLayer(layer, inputs, outputs); + ASSERT_EQ(outputs.size(), (size_t)1); + EXPECT_EQ(shape(outputs[0]), shape(2, 3, 20)); + normAssert(outputs[0], inp4.reshape(1, {2, 3, 20})); + + // Third run: back to 3D input [2, 3, 4], axes [-2, -1] -> [1, 2] -> [2, 12] + inputs = {inp3.clone()}; + runLayer(layer, inputs, outputs); + ASSERT_EQ(outputs.size(), (size_t)1); + EXPECT_EQ(shape(outputs[0]), shape(2, 12)); + normAssert(outputs[0], inp3.reshape(1, {2, 12})); +} + }} // namespace