diff --git a/modules/dnn/src/onnx/onnx_importer.cpp b/modules/dnn/src/onnx/onnx_importer.cpp index 7e035b3f23..15e6919f19 100644 --- a/modules/dnn/src/onnx/onnx_importer.cpp +++ b/modules/dnn/src/onnx/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("min", -FLT_MAX)); - layerParams.set("max_value", layerParams.get("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(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(0); + else + CV_Error(Error::StsNotImplemented, "Non-constant max values in Clip are not supported"); + } + + layerParams.set("min_value", layerParams.get("min", min_value)); + layerParams.set("max_value", layerParams.get("max", max_value)); addLayer(layerParams, node_proto); } diff --git a/modules/dnn/test/test_onnx_importer.cpp b/modules/dnn/test/test_onnx_importer.cpp index bf59cbba97..578e0442b2 100644 --- a/modules/dnn/test/test_onnx_importer.cpp +++ b/modules/dnn/test/test_onnx_importer.cpp @@ -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");