Merge pull request #22100 from WanliZhong:issue_22015

Fix issue 22015, let Clip layer support 1-3 inputs

* Fix issue 22015.
Let layer Clip support 1-3 inputs.

* Resolve other problems caused by modifications

* Update onnx_importer.cpp

added extra checks to min/max handling in Clip

* Add assertions to check the size of the input

* Add test for clip with min and max initializers

* Separate test for "clip_init_min_max". Change the check method for input_size to provide a clearer message in case of problem.

* Add tests for clip with min or max initializers

* Change the implementation of getting input

Co-authored-by: Vadim Pisarevsky <vadim.pisarevsky@gmail.com>
pull/22156/head
Wanli 2 years ago committed by GitHub
parent 24a66a44bf
commit a6ca48a1c2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 25
      modules/dnn/src/onnx/onnx_importer.cpp
  2. 7
      modules/dnn/test/test_onnx_importer.cpp

@ -1931,10 +1931,29 @@ void ONNXImporter::parseImageScaler(LayerParams& layerParams, const opencv_onnx:
void ONNXImporter::parseClip(LayerParams& layerParams, const opencv_onnx::NodeProto& node_proto)
{
CV_CheckEQ(node_proto.input_size(), 1, "");
layerParams.type = "ReLU6";
layerParams.set("min_value", layerParams.get<float>("min", -FLT_MAX));
layerParams.set("max_value", layerParams.get<float>("max", FLT_MAX));
float min_value = -FLT_MAX, max_value = FLT_MAX;
int input_size = node_proto.input_size();
CV_Check(input_size, 1 <= input_size && input_size <= 3, "");
if (input_size >= 2 && !node_proto.input(1).empty())
{
if (constBlobs.find(node_proto.input(1)) != constBlobs.end())
min_value = getBlob(node_proto, 1).at<float>(0);
else
CV_Error(Error::StsNotImplemented, "Non-constant min values in Clip are not supported");
}
if (input_size == 3 && !node_proto.input(2).empty())
{
if (constBlobs.find(node_proto.input(2)) != constBlobs.end())
max_value = getBlob(node_proto, 2).at<float>(0);
else
CV_Error(Error::StsNotImplemented, "Non-constant max values in Clip are not supported");
}
layerParams.set("min_value", layerParams.get<float>("min", min_value));
layerParams.set("max_value", layerParams.get<float>("max", max_value));
addLayer(layerParams, node_proto);
}

@ -389,6 +389,13 @@ TEST_P(Test_ONNX_layers, Clip)
testONNXModels("clip", npy);
}
TEST_P(Test_ONNX_layers, Clip_init)
{
testONNXModels("clip_init_min_max");
testONNXModels("clip_init_min");
testONNXModels("clip_init_max");
}
TEST_P(Test_ONNX_layers, Shape)
{
testONNXModels("shape_of_constant");

Loading…
Cancel
Save