Merge pull request #31249 from sshivensolanki/dnn-hardmax-stale-axis

Problem
Hardmax::forward wrote the normalized axis back into the axis attribute, so a later input of a different rank used the stale index: the wrong axis for higher rank, an assertion for lower rank.

Fix
Store the result of normalize_axis in a local normAxis and pass it to hardmaxApply. The axis attribute keeps the value from the model.
This commit is contained in:
Abhishek Gola
2026-11-30 09:42:49 +05:30
committed by GitHub
2 changed files with 40 additions and 10 deletions

View File

@@ -68,7 +68,7 @@ public:
Mat src = inputs[0];
Mat dst = outputs[0];
axis = normalize_axis(axis, src.dims);
const int normAxis = normalize_axis(axis, src.dims);
MatShape shape(src.size.p, src.size.p + src.dims);
// Prepare output
@@ -76,15 +76,15 @@ public:
switch (src.depth())
{
case CV_8U: hardmaxApply<uchar>(src, dst, axis); break;
case CV_8S: hardmaxApply<schar>(src, dst, axis); break;
case CV_16U: hardmaxApply<ushort>(src, dst, axis); break;
case CV_16S: hardmaxApply<short>(src, dst, axis); break;
case CV_32S: hardmaxApply<int>(src, dst, axis); break;
case CV_64S: hardmaxApply<int64_t>(src, dst, axis); break;
case CV_Bool: hardmaxApply<bool>(src, dst, axis); break;
case CV_32F: hardmaxApply<float>(src, dst, axis); break;
case CV_64F: hardmaxApply<double>(src, dst, axis); break;
case CV_8U: hardmaxApply<uchar>(src, dst, normAxis); break;
case CV_8S: hardmaxApply<schar>(src, dst, normAxis); break;
case CV_16U: hardmaxApply<ushort>(src, dst, normAxis); break;
case CV_16S: hardmaxApply<short>(src, dst, normAxis); break;
case CV_32S: hardmaxApply<int>(src, dst, normAxis); break;
case CV_64S: hardmaxApply<int64_t>(src, dst, normAxis); break;
case CV_Bool: hardmaxApply<bool>(src, dst, normAxis); break;
case CV_32F: hardmaxApply<float>(src, dst, normAxis); break;
case CV_64F: hardmaxApply<double>(src, dst, normAxis); break;
default:
CV_Error(Error::StsUnsupportedFormat, "Unsupported input data type");
}

View File

@@ -6617,4 +6617,34 @@ TEST(Test_TensorScatter, InplaceNewEngineGraph)
EXPECT_EQ(pastBuf, outBuf);
}
TEST(Layer_Test_Hardmax_Axis, keepsNegativeAxisAcrossRanks)
{
LayerParams lp;
lp.type = "Hardmax";
lp.name = "test_hardmax";
lp.set("axis", -1);
Ptr<Layer> layer = LayerFactory::createLayerInstance(lp.type, lp);
ASSERT_TRUE(layer);
// The largest value in each group of 3 is at index 1 and index 0
float data[] = {0, 5, 1,
9, 0, 0};
float expected[] = {0, 1, 0,
1, 0, 0};
// First run: 2D input [2, 3], so axis -1 is index 1
std::vector<Mat> inputs{Mat(2, 3, CV_32F, data).clone()}, outputs;
runLayer(layer, inputs, outputs);
ASSERT_EQ(outputs.size(), (size_t)1);
EXPECT_EQ(0, cvtest::norm(outputs[0], Mat(2, 3, CV_32F, expected), NORM_INF));
// Second run on the same layer: 3D input [1, 2, 3], so axis -1 must now be index 2
int sz[] = {1, 2, 3};
inputs = {Mat(3, sz, CV_32F, data).clone()};
runLayer(layer, inputs, outputs);
ASSERT_EQ(outputs.size(), (size_t)1);
EXPECT_EQ(0, cvtest::norm(outputs[0], Mat(3, sz, CV_32F, expected), NORM_INF));
}
}} // namespace