-
-
Notifications
You must be signed in to change notification settings - Fork 56.2k
IfLayer add to new DNN engine #27508
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: 5.x
Are you sure you want to change the base?
Conversation
Hi @vpisarev, |
static Ptr<IfLayer> create(const LayerParams& params); | ||
|
||
/** Returns the selected subgraph based on the boolean flag. */ | ||
virtual Ptr<Graph> then_else(bool flag) const = 0; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IfLayer should override Layer::subgraphs() method. then_else()
method is not needed, please, remove it.
@@ -0,0 +1,45 @@ | |||
#include "../precomp.hpp" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
don't forget to put OpenCV license in the beginning:
// This file is part of OpenCV project.
// It is subject to the license terms in the LICENSE file found in the top-level directory
// of this distribution and at http://opencv.org/license.html.
modules/dnn/src/layers/iflayer.cpp
Outdated
} | ||
|
||
private: | ||
Ptr<Graph> thenGraph; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- declare
std::vector<Ptr<Graph> > thenelse;
. - add
std::vector<Ptr<Graph> >* subgraphs() const CV_OVERRIDE { return &thenelse; }
put both 'then' and 'else' graph into this vector.
modules/dnn/src/layers/iflayer.cpp
Outdated
private: | ||
Ptr<Graph> thenGraph; | ||
Ptr<Graph> elseGraph; | ||
int condIndex; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if condIndex
stores the computed index, don't do that. Try to store as little of mutable information in each Layer as possible. Whenever possible, layer should not contain any state, because it's one of the major sources of errors.
modules/dnn/src/net_impl2.cpp
Outdated
@@ -239,7 +239,7 @@ Arg Net::Impl::newArg(const std::string& name, ArgKind kind, bool allowEmptyName | |||
int idx = (int)args.size(); | |||
|
|||
if (!name.empty()) { | |||
CV_Assert(argnames.find(name) == argnames.end()); | |||
// CV_Assert(argnames.find(name) == argnames.end()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
not sure why you disabled this check
@@ -677,9 +674,19 @@ void Net::Impl::forwardGraph(Ptr<Graph>& graph, InputArrayOfArrays inputs_, | |||
timestamp = getTickCount(); | |||
|
|||
// [TODO] handle If/Loop/... | |||
CV_Assert(!layer->subgraphs()); | |||
if (finalizeLayers) | |||
layer->finalize(inpMats, outMats); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I suggest to have check for empty subgraphs first.
std::vector<Ptr<Graph>>* subgraphs = layer->subgraphs();
if (!subgraphs) { // main branch, true for 99.9% of layers
if (finalizeLayers)
layer->finalize(inpMats, outMats);
layer->forward(inpMats, outMats, tempMats);
} else {
Ptr<IfLayer> iflayer = layer.dynamicCast<IfLayer>();
if (iflayer) {
// subgraphs already contains pointer to vector where, subgraphs[0] is then, subgraphs[1] is else.
Mat inp0 = inpMats[0];
CV_Assert(inp0.total() == 1u);
CV_Assert(inp0.type() == CV_Bool);
bool flag = inp0.at<bool>(0);
auto subgraph = subgraphs->at((int)(!flag));
forwardGraph(subgraph, inpMats, outMats, false);
} else {
CV_Error_(Error::StsNotImplemented, "unknown layer type '%s' with subgraphs", layer->type.c_str()));
}
}
Hi @vpisarev, opencv/modules/dnn/src/onnx/onnx_importer2.cpp Lines 1505 to 1559 in e0e1987
opencv/modules/dnn/src/onnx/onnx_importer2.cpp Lines 917 to 936 in e0e1987
Option I see to achieve this is by adding a custom addLayer implementation for IfLayer that takes vector<Ptr<Graph>> as input and directly add it to subgraphs in case of ifLayer. |
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.