|
|
@ -79,9 +79,9 @@ public: |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
static const tensorflow::NodeDef& getInputNode(const tensorflow::GraphDef& net, |
|
|
|
static int getInputNodeId(const tensorflow::GraphDef& net, |
|
|
|
const tensorflow::NodeDef& node, |
|
|
|
const tensorflow::NodeDef& node, |
|
|
|
int inpId) |
|
|
|
int inpId) |
|
|
|
{ |
|
|
|
{ |
|
|
|
CV_Assert(inpId < node.input_size()); |
|
|
|
CV_Assert(inpId < node.input_size()); |
|
|
|
std::string name = node.input(inpId); |
|
|
|
std::string name = node.input(inpId); |
|
|
@ -92,7 +92,7 @@ public: |
|
|
|
for (int i = 0; i < numNodes; ++i) |
|
|
|
for (int i = 0; i < numNodes; ++i) |
|
|
|
{ |
|
|
|
{ |
|
|
|
if (net.node(i).name() == name) |
|
|
|
if (net.node(i).name() == name) |
|
|
|
return net.node(i); |
|
|
|
return i; |
|
|
|
} |
|
|
|
} |
|
|
|
CV_Error(Error::StsParseError, "Input node with name " + name + " not found"); |
|
|
|
CV_Error(Error::StsParseError, "Input node with name " + name + " not found"); |
|
|
|
} |
|
|
|
} |
|
|
@ -104,36 +104,46 @@ public: |
|
|
|
matchedNodesIds.clear(); |
|
|
|
matchedNodesIds.clear(); |
|
|
|
matchedNodesIds.reserve(nodesToFuse.size()); |
|
|
|
matchedNodesIds.reserve(nodesToFuse.size()); |
|
|
|
|
|
|
|
|
|
|
|
int numNodes = net.node_size(); |
|
|
|
std::queue<int> nodesToMatch; |
|
|
|
for (int i = 0; i < nodesToFuse.size(); ++i) |
|
|
|
std::queue<int> targetNodes; |
|
|
|
|
|
|
|
nodesToMatch.push(nodeId); |
|
|
|
|
|
|
|
targetNodes.push(nodesToFuse.back()); |
|
|
|
|
|
|
|
while (!nodesToMatch.empty()) |
|
|
|
{ |
|
|
|
{ |
|
|
|
while (nodeId < numNodes && net.node(nodeId).op() == "Const") |
|
|
|
int nodeToMatch = nodesToMatch.front(); |
|
|
|
{ |
|
|
|
int targetNodeId = targetNodes.front(); |
|
|
|
nodeId += 1; |
|
|
|
nodesToMatch.pop(); |
|
|
|
} |
|
|
|
targetNodes.pop(); |
|
|
|
if (nodeId > numNodes - 1) |
|
|
|
|
|
|
|
return false; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const tensorflow::NodeDef& node = net.node(nodeId); |
|
|
|
if (std::find(matchedNodesIds.begin(), matchedNodesIds.end(), nodeToMatch) != |
|
|
|
|
|
|
|
matchedNodesIds.end()) |
|
|
|
|
|
|
|
continue; |
|
|
|
|
|
|
|
|
|
|
|
if (node.op() != nodes[nodesToFuse[i]]) |
|
|
|
const tensorflow::NodeDef& node = net.node(nodeToMatch); |
|
|
|
|
|
|
|
if (node.op() != nodes[targetNodeId]) |
|
|
|
return false; |
|
|
|
return false; |
|
|
|
|
|
|
|
|
|
|
|
std::vector<int>& inputNodes = inputs[nodesToFuse[i]]; |
|
|
|
std::vector<int>& inputNodes = inputs[targetNodeId]; |
|
|
|
if (inputNodes.size() != node.input_size()) |
|
|
|
if (inputNodes.size() != node.input_size()) |
|
|
|
return false; |
|
|
|
return false; |
|
|
|
|
|
|
|
|
|
|
|
for (int j = 0; j < inputNodes.size(); ++j) |
|
|
|
for (int j = 0; j < inputNodes.size(); ++j) |
|
|
|
{ |
|
|
|
{ |
|
|
|
if (nodes[inputNodes[j]].empty()) // Unknown input node type.
|
|
|
|
if (nodes[inputNodes[j]].empty()) // Unknown input node type.
|
|
|
|
continue; |
|
|
|
continue; |
|
|
|
const tensorflow::NodeDef& inpNode = getInputNode(net, node, j); |
|
|
|
nodeId = getInputNodeId(net, node, j); |
|
|
|
if (inpNode.op() != nodes[inputNodes[j]]) |
|
|
|
const tensorflow::NodeDef& inpNode = net.node(nodeId); |
|
|
|
|
|
|
|
if (inpNode.op() != "Const") |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
nodesToMatch.push(nodeId); |
|
|
|
|
|
|
|
targetNodes.push(inputNodes[j]); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
else if (nodes[inputNodes[j]] != "Const") |
|
|
|
return false; |
|
|
|
return false; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
matchedNodesIds.push_back(nodeToMatch); |
|
|
|
matchedNodesIds.push_back(nodeId); |
|
|
|
|
|
|
|
nodeId += 1; |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
std::sort(matchedNodesIds.begin(), matchedNodesIds.end()); |
|
|
|
return true; |
|
|
|
return true; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -181,7 +191,7 @@ public: |
|
|
|
std::vector<tensorflow::NodeDef*> inputNodes(inputsNames.size()); |
|
|
|
std::vector<tensorflow::NodeDef*> inputNodes(inputsNames.size()); |
|
|
|
for (int i = 0; i < inputsNames.size(); ++i) |
|
|
|
for (int i = 0; i < inputsNames.size(); ++i) |
|
|
|
{ |
|
|
|
{ |
|
|
|
inputNodes[i] = (tensorflow::NodeDef*)&getInputNode(net, *node, i); |
|
|
|
inputNodes[i] = net.mutable_node(getInputNodeId(net, *node, i)); |
|
|
|
} |
|
|
|
} |
|
|
|
finalize(net, node, inputNodes); |
|
|
|
finalize(net, node, inputNodes); |
|
|
|
} |
|
|
|
} |
|
|
@ -354,7 +364,7 @@ public: |
|
|
|
{ |
|
|
|
{ |
|
|
|
if (!Subgraph::match(net, nodeId, matchedNodesIds)) |
|
|
|
if (!Subgraph::match(net, nodeId, matchedNodesIds)) |
|
|
|
return false; |
|
|
|
return false; |
|
|
|
Mat maxValue = getTensorContent(net.node(nodeId + 1).attr().at("value").tensor()); |
|
|
|
Mat maxValue = getTensorContent(net.node(matchedNodesIds.front() + 1).attr().at("value").tensor()); |
|
|
|
return maxValue.type() == CV_32FC1 && maxValue.total() == 1 && maxValue.at<float>(0) == 6; |
|
|
|
return maxValue.type() == CV_32FC1 && maxValue.total() == 1 && maxValue.at<float>(0) == 6; |
|
|
|
} |
|
|
|
} |
|
|
|
}; |
|
|
|
}; |
|
|
@ -384,6 +394,17 @@ public: |
|
|
|
setFusedNode("Reshape", ids); |
|
|
|
setFusedNode("Reshape", ids); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
virtual bool match(const tensorflow::GraphDef& net, int nodeId, std::vector<int>& matchedNodesIds) CV_OVERRIDE |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
const tensorflow::NodeDef& node = net.node(nodeId); |
|
|
|
|
|
|
|
if (node.input_size() == 0) |
|
|
|
|
|
|
|
return false; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
inpName = node.input(0); |
|
|
|
|
|
|
|
return Subgraph::match(net, nodeId, matchedNodesIds); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
virtual void finalize(tensorflow::GraphDef&, tensorflow::NodeDef* fusedNode, |
|
|
|
virtual void finalize(tensorflow::GraphDef&, tensorflow::NodeDef* fusedNode, |
|
|
|
std::vector<tensorflow::NodeDef*>& inputNodes) CV_OVERRIDE |
|
|
|
std::vector<tensorflow::NodeDef*>& inputNodes) CV_OVERRIDE |
|
|
|
{ |
|
|
|
{ |
|
|
@ -395,6 +416,7 @@ public: |
|
|
|
} |
|
|
|
} |
|
|
|
tensorflow::TensorProto* shapeTensor = inputNodes[1]->mutable_attr()->at("value").mutable_tensor(); |
|
|
|
tensorflow::TensorProto* shapeTensor = inputNodes[1]->mutable_attr()->at("value").mutable_tensor(); |
|
|
|
fusedNode->mutable_input()->DeleteSubrange(2, numOutDims - 1); |
|
|
|
fusedNode->mutable_input()->DeleteSubrange(2, numOutDims - 1); |
|
|
|
|
|
|
|
fusedNode->set_input(0, inpName); |
|
|
|
|
|
|
|
|
|
|
|
shapeTensor->clear_int_val(); |
|
|
|
shapeTensor->clear_int_val(); |
|
|
|
for (int i = 0; i < shape.size(); ++i) |
|
|
|
for (int i = 0; i < shape.size(); ++i) |
|
|
@ -405,6 +427,7 @@ public: |
|
|
|
|
|
|
|
|
|
|
|
private: |
|
|
|
private: |
|
|
|
int numOutDims; |
|
|
|
int numOutDims; |
|
|
|
|
|
|
|
std::string inpName; |
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
class L2NormalizeSubgraph : public Subgraph |
|
|
|
class L2NormalizeSubgraph : public Subgraph |
|
|
@ -685,9 +708,9 @@ void simplifySubgraphs(tensorflow::GraphDef& net) |
|
|
|
subgraphs.push_back(Ptr<Subgraph>(new DeconvolutionSameKerasSubgraph())); |
|
|
|
subgraphs.push_back(Ptr<Subgraph>(new DeconvolutionSameKerasSubgraph())); |
|
|
|
subgraphs.push_back(Ptr<Subgraph>(new ResizeBilinearSubgraph())); |
|
|
|
subgraphs.push_back(Ptr<Subgraph>(new ResizeBilinearSubgraph())); |
|
|
|
subgraphs.push_back(Ptr<Subgraph>(new UpsamplingKerasSubgraph())); |
|
|
|
subgraphs.push_back(Ptr<Subgraph>(new UpsamplingKerasSubgraph())); |
|
|
|
subgraphs.push_back(Ptr<Subgraph>(new ReshapeAsShapeSubgraph())); |
|
|
|
|
|
|
|
subgraphs.push_back(Ptr<Subgraph>(new SoftMaxSlimSubgraph())); |
|
|
|
subgraphs.push_back(Ptr<Subgraph>(new SoftMaxSlimSubgraph())); |
|
|
|
subgraphs.push_back(Ptr<Subgraph>(new SoftMaxSlimV2Subgraph())); |
|
|
|
subgraphs.push_back(Ptr<Subgraph>(new SoftMaxSlimV2Subgraph())); |
|
|
|
|
|
|
|
subgraphs.push_back(Ptr<Subgraph>(new ReshapeAsShapeSubgraph())); |
|
|
|
|
|
|
|
|
|
|
|
int numNodes = net.node_size(); |
|
|
|
int numNodes = net.node_size(); |
|
|
|
std::vector<int> matchedNodesIds; |
|
|
|
std::vector<int> matchedNodesIds; |
|
|
|