Skip to content

Commit ee5bb95

Browse files
fengyuentauklatism
authored andcommitted
Merge pull request opencv#1165 from fengyuentau:gold_yolo
[BugFix] dnn (ONNX): Foce dropping constant inputs in parseClip if they are shared opencv#25319 Resolves opencv#25278 Merge with opencv/opencv_extra#1165 In Gold-YOLO ,`Div` has a constant input `B=6` which is then parsed into a `Const` layer in the ONNX importer, but `Clip` also has the shared constant input `max=6` which is already a `Const` layer and then connected to `Elementwise` layer. This should not happen because in the `forward()` of `Elementwise` layer, the legacy code goes through and apply activation to each input. More details on opencv#25278 (comment). ### Pull Request Readiness Checklist See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request - [x] I agree to contribute to the project under Apache 2 License. - [x] To the best of my knowledge, the proposed patch is not based on a code under GPL or another license that is incompatible with OpenCV - [x] The PR is proposed to the proper branch - [x] There is a reference to the original bug report and related work - [x] There is accuracy test, performance test and test data in opencv_extra repository, if applicable Patch to opencv_extra has the same branch name. - [x] The feature is well documented and sample code can be built with the project CMake
1 parent 8a1041a commit ee5bb95

File tree

2 files changed

+11
-4
lines changed

2 files changed

+11
-4
lines changed

modules/dnn/src/onnx/onnx_importer.cpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,8 @@ class ONNXImporter
9191

9292
void addConstant(const std::string& name, const Mat& blob);
9393
void addLayer(LayerParams& layerParams,
94-
const opencv_onnx::NodeProto& node_proto);
94+
const opencv_onnx::NodeProto& node_proto,
95+
int num_inputs = std::numeric_limits<int>::max());
9596
void setParamsDtype(LayerParams& layerParams, const opencv_onnx::NodeProto& node_proto);
9697

9798
void lstm_extractConsts(LayerParams& layerParams, const opencv_onnx::NodeProto& lstm_proto, size_t idx, int* blobShape_, int size);
@@ -617,7 +618,8 @@ ONNXImporter::TensorInfo ONNXImporter::getBlobExtraInfo(const std::string& input
617618
}
618619

619620
void ONNXImporter::addLayer(LayerParams& layerParams,
620-
const opencv_onnx::NodeProto& node_proto)
621+
const opencv_onnx::NodeProto& node_proto,
622+
int num_inputs)
621623
{
622624
int depth = layerParams.get<int>("depth", CV_32F);
623625
int id = dstNet.addLayer(layerParams.name, layerParams.type, depth, layerParams);
@@ -632,7 +634,8 @@ void ONNXImporter::addLayer(LayerParams& layerParams,
632634

633635
std::vector<MatShape> layerInpShapes, layerOutShapes, layerInternalShapes;
634636
int inpNum = 0;
635-
for (int j = 0; j < node_proto.input_size(); j++)
637+
num_inputs = std::min(node_proto.input_size(), num_inputs);
638+
for (int j = 0; j < num_inputs; j++)
636639
{
637640
const std::string& input_name = node_proto.input(j);
638641
IterLayerId_t layerId = layer_id.find(input_name);
@@ -1799,7 +1802,7 @@ void ONNXImporter::parseClip(LayerParams& layerParams, const opencv_onnx::NodePr
17991802

18001803
layerParams.set("min_value", layerParams.get<float>("min", min_value));
18011804
layerParams.set("max_value", layerParams.get<float>("max", max_value));
1802-
addLayer(layerParams, node_proto);
1805+
addLayer(layerParams, node_proto, 1);
18031806
}
18041807

18051808
void ONNXImporter::parseLeakyRelu(LayerParams& layerParams, const opencv_onnx::NodeProto& node_proto)

modules/dnn/test/test_onnx_importer.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3096,6 +3096,10 @@ TEST_P(Test_ONNX_layers, MatMulAddFusion) {
30963096
testONNXModels("biased_matmul", npy, l1, lInf);
30973097
}
30983098

3099+
TEST_P(Test_ONNX_layers, ClipDivSharedConstant) {
3100+
testONNXModels("clip_div_shared_constant");
3101+
}
3102+
30993103
INSTANTIATE_TEST_CASE_P(/**/, Test_ONNX_nets, dnnBackendsAndTargets());
31003104

31013105
}} // namespace

0 commit comments

Comments
 (0)