mirror of
https://github.com/opencv/opencv.git
synced 2026-12-02 21:22:43 -06:00
dnn: handle min > max in Clip layer and enable ONNX conformance test (#30891)
* dnn: handle min > max in Clip layer and enable ONNX conformance test Under the ONNX specification, the Clip operator is defined mathematically as y = min(max(x, min), max). When min > max, the operation clamps identically to max for all input values. Previously, ClipLayerImpl asserted minValue <= maxValue in its constructor and actualMin <= actualMax in forward, causing assertion failures on valid ONNX models with min > max. Additionally, the adjacency graph fusion interpreter checked a < min first, returning min instead of max. This commit: 1. Removes the min <= max assertions in ClipLayerImpl constructor and forward, allowing ONNX-conforming evaluation where min > max clamps to max. 2. Updates FusionEltwiseOp::CLAMP in AdjacencyGraph interpreter to use std::min(std::max(a, n.scalar), n.scalar2), matching the ONNX evaluation order. 3. Removes test_clip_min_greater_than_max from the layer parser denylist. 4. Adds unit tests covering static and dynamic min > max clipping. * dnn: filter test_clip_min_greater_than_max for OpenVINO backend * dnn(test): address review comments on Clip min > max tests and fusion coverage
This commit is contained in:
@@ -437,7 +437,7 @@ inline float evalElement(const AdjacencyGraph& g, float x,
|
||||
case FusionEltwiseOp::SQRT: v[i] = std::sqrt(a); break;
|
||||
case FusionEltwiseOp::RECIP: v[i] = 1.f / a; break;
|
||||
case FusionEltwiseOp::CLAMP:
|
||||
v[i] = a < n.scalar ? n.scalar : (a > n.scalar2 ? n.scalar2 : a);
|
||||
v[i] = std::min(std::max(a, n.scalar), n.scalar2);
|
||||
break;
|
||||
case FusionEltwiseOp::TENSOR:
|
||||
// Only a sink that opted in is offered one, and it reads the nodes itself.
|
||||
|
||||
@@ -85,8 +85,6 @@ public:
|
||||
hasMax = params.has("max");
|
||||
if (hasMin) minValue = params.get<float>("min");
|
||||
if (hasMax) maxValue = params.get<float>("max");
|
||||
if (hasMin && hasMax)
|
||||
CV_Assert(minValue <= maxValue);
|
||||
}
|
||||
|
||||
bool unfoldMath(LayerMath& r, const ConstOperand& side) const
|
||||
@@ -172,7 +170,6 @@ public:
|
||||
|
||||
double actualMin = dynMin ? getScalar(inputs[1]) : (hasMin ? minValue : typeMin(data.depth()));
|
||||
double actualMax = dynMax ? getScalar(inputs[2]) : (hasMax ? maxValue : typeMax(data.depth()));
|
||||
CV_Assert(actualMin <= actualMax);
|
||||
|
||||
// Fused single-pass clamp for contiguous CV_32F. Halves memory traffic
|
||||
// vs the cv::max + cv::min pair, and lets the allocator run it in-place.
|
||||
|
||||
@@ -139,6 +139,10 @@ TEST(Fusion, MathMatchesClosedForm)
|
||||
r.clamp(LayerMath::INPUT_VALUE, 0.f, 6.f);
|
||||
EXPECT_FLOAT_EQ(std::min(std::max(x, 0.f), 6.f), eval1(r, x)) << "clip " << x;
|
||||
|
||||
r = LayerMath();
|
||||
r.clamp(LayerMath::INPUT_VALUE, 6.f, 0.f);
|
||||
EXPECT_FLOAT_EQ(std::min(std::max(x, 6.f), 0.f), eval1(r, x)) << "clip min > max " << x;
|
||||
|
||||
r = LayerMath(); fusion::detail::sigmoid(r);
|
||||
EXPECT_NEAR(1.f / (1.f + std::exp(-x)), eval1(r, x), 1e-5) << "sigmoid " << x;
|
||||
|
||||
|
||||
@@ -4910,4 +4910,80 @@ INSTANTIATE_TEST_CASE_P(/**/, Layer_Test_Hardmax, Values(
|
||||
make_tuple(CV_64F, -std::numeric_limits<double>::infinity())
|
||||
));
|
||||
|
||||
static void testClipMinGreaterThanMax(int depth, bool dynamic)
|
||||
{
|
||||
const int total = 64;
|
||||
Mat input(1, total, depth);
|
||||
Mat ref(1, total, depth);
|
||||
|
||||
const double minVal = 5.0;
|
||||
const double maxVal = 2.0;
|
||||
|
||||
if (depth == CV_32F)
|
||||
{
|
||||
float* inPtr = input.ptr<float>();
|
||||
float* refPtr = ref.ptr<float>();
|
||||
for (int i = 0; i < total; ++i)
|
||||
{
|
||||
inPtr[i] = (float)i * 0.5f - 15.0f;
|
||||
refPtr[i] = (float)maxVal;
|
||||
}
|
||||
}
|
||||
else // CV_64F
|
||||
{
|
||||
double* inPtr = input.ptr<double>();
|
||||
double* refPtr = ref.ptr<double>();
|
||||
for (int i = 0; i < total; ++i)
|
||||
{
|
||||
inPtr[i] = (double)i * 0.5 - 15.0;
|
||||
refPtr[i] = maxVal;
|
||||
}
|
||||
}
|
||||
|
||||
LayerParams lp;
|
||||
lp.type = "Clip";
|
||||
lp.name = "ClipLayer";
|
||||
|
||||
std::vector<Mat> inputs = {input};
|
||||
if (!dynamic)
|
||||
{
|
||||
lp.set("min", (float)minVal);
|
||||
lp.set("max", (float)maxVal);
|
||||
}
|
||||
else
|
||||
{
|
||||
Mat minMat(1, 1, depth, Scalar(minVal));
|
||||
Mat maxMat(1, 1, depth, Scalar(maxVal));
|
||||
inputs.push_back(minMat);
|
||||
inputs.push_back(maxMat);
|
||||
}
|
||||
|
||||
Ptr<Layer> layer = LayerFactory::createLayerInstance("Clip", lp);
|
||||
std::vector<Mat> outputs;
|
||||
runLayer(layer, inputs, outputs);
|
||||
|
||||
ASSERT_EQ(outputs.size(), 1u);
|
||||
normAssert(ref, outputs[0], "Clip min > max output mismatch");
|
||||
}
|
||||
|
||||
TEST(Layer_Test_Clip, MinGreaterThanMax_Float32)
|
||||
{
|
||||
testClipMinGreaterThanMax(CV_32F, false);
|
||||
}
|
||||
|
||||
TEST(Layer_Test_Clip, MinGreaterThanMax_Float64)
|
||||
{
|
||||
testClipMinGreaterThanMax(CV_64F, false);
|
||||
}
|
||||
|
||||
TEST(Layer_Test_Clip, DynamicMinGreaterThanMax_Float32)
|
||||
{
|
||||
testClipMinGreaterThanMax(CV_32F, true);
|
||||
}
|
||||
|
||||
TEST(Layer_Test_Clip, DynamicMinGreaterThanMax_Float64)
|
||||
{
|
||||
testClipMinGreaterThanMax(CV_64F, true);
|
||||
}
|
||||
|
||||
}} // namespace
|
||||
|
||||
@@ -720,6 +720,8 @@ CASE(test_clip_inbounds)
|
||||
SKIP;
|
||||
CASE(test_clip_inbounds_expanded)
|
||||
SKIP;
|
||||
CASE(test_clip_min_greater_than_max)
|
||||
SKIP;
|
||||
CASE(test_clip_min_greater_than_max_expanded)
|
||||
SKIP;
|
||||
CASE(test_clip_outbounds)
|
||||
|
||||
@@ -30,7 +30,6 @@
|
||||
"test_bernoulli_seed_expanded", // ---- same as above ---
|
||||
"test_castlike_FLOAT_to_STRING",
|
||||
"test_castlike_STRING_to_FLOAT", // Issues::Layer::Can't create layer "onnx_node_output_0!output" of type "CastLike" in function 'getLayerInstance'
|
||||
"test_clip_min_greater_than_max",
|
||||
"test_col2im",
|
||||
"test_col2im_5d",
|
||||
"test_col2im_dilations",
|
||||
|
||||
Reference in New Issue
Block a user