DNN: add another two Mish activation to onnx_graph_simplifier#22311
DNN: add another two Mish activation to onnx_graph_simplifier#22311asmorkalov merged 2 commits intoopencv:4.xfrom
Conversation
c55acc6 to
3c5377c
Compare
| // softplus(x) = log(exp(x) + 1) | ||
| class SoftplusSubgraph: public Subgraph | ||
| { | ||
| public: | ||
| SoftplusSubgraph() | ||
| { | ||
| int input = addNodeToMatch(""); | ||
| int exp = addNodeToMatch("Exp", input); | ||
| int addVal = addNodeToMatch(""); | ||
| int add = addNodeToMatch("Add", addVal, exp); | ||
| addNodeToMatch("Log", add); | ||
| setFusedNode("Softplus", input); | ||
| } | ||
| }; | ||
|
|
||
| class SoftplusSubgraph2: public Subgraph | ||
| { | ||
| public: | ||
| SoftplusSubgraph2() | ||
| { | ||
| int input = addNodeToMatch(""); | ||
| int exp = addNodeToMatch("Exp", input); | ||
| int addVal = addNodeToMatch(""); | ||
| int add = addNodeToMatch("Add", exp, addVal); | ||
| addNodeToMatch("Log", add); | ||
| setFusedNode("Softplus", input); | ||
| } | ||
| }; |
There was a problem hiding this comment.
We have two very similar SoftplusSubgrap because Softplus contains an Add which has two inputs (one is variable and one is Constant 1). Since the order of these two inputs is random, we use two SoftplusSubgraph to handle this. Is there a better solution?
There was a problem hiding this comment.
I think that simplifier should just match different nodes. You can make a small onnx model of both cases, remove the second class and test if it's working(by setting the break point inside Softplus's forward for example).
There was a problem hiding this comment.
Thanks for code reviewing! Actually, I have verified it. And the SoftplusSubgraph can work on Yolov4, and the SoftplusSubgraph2 can only work on this test case.
You can make a small onnx model of both cases.
I also tried to generate another case Mish case like Yolov4. But it fails. Probably due to this YoloV4 being converted from TensorFlow model.
There was a problem hiding this comment.
Hm, actually it makes sense - the operation is not required to be commutative. You could use CRTP to statically parametrise the order, but I don't think the complexity is worth it in this case. The second solution is to add logic of reordering into subgraph matching, but it'll take a while. I think this works for now.
Speed test on YoloV4
The Mish layer can be generated by two different implementations:
In order to make YoloV4 run faster on OpenCV, Mish's graph fusion optimization is essential.
For now, the original YoloV4 in ONNX's model_zoo cannot be loaded correctly by OpenCV, because original YoloV4 has very complicated struct and the input is dynamic shape.
So I convert it to a suitable format by following python code:
And the converted model can be found at this google drive.
Test case in OpenCV_extra.
Pull Request Readiness Checklist
See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request
Patch to opencv_extra has the same branch name.