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.
This commit is contained in:
Ishita Singh
2026-12-01 13:49:15 +05:30
parent 9dae03b6a5
commit ac7d331a79
2 changed files with 125 additions and 2 deletions

View File

@@ -123,10 +123,12 @@ public:
{
std::vector<Mat> 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

View File

@@ -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> 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<float>();
for (size_t i = 0; i < inp3.total(); ++i)
p3[i] = static_cast<float>(i);
// First run: 3D input [2, 3, 4], end_axis -1 is axis 2 -> [2, 12]
std::vector<Mat> 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<float>();
for (size_t i = 0; i < inp4.total(); ++i)
p4[i] = static_cast<float>(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> 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<float>();
for (size_t i = 0; i < inp4.total(); ++i)
p4[i] = static_cast<float>(i);
// First run: 4D input [2, 3, 4, 5], end_axis -1 is axis 3 -> [2, 60]
std::vector<Mat> 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<float>();
for (size_t i = 0; i < inp3.total(); ++i)
p3[i] = static_cast<float>(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> 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<float>();
for (size_t i = 0; i < inp3.total(); ++i)
p3[i] = static_cast<float>(i);
// First run: 3D input [2, 3, 4], axes [-2, -1] -> [1, 2] -> [2, 12]
std::vector<Mat> 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<float>();
for (size_t i = 0; i < inp4.total(); ++i)
p4[i] = static_cast<float>(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