Path: blob/master/modules/dnn/src/tensorflow/tf_graph_simplifier.cpp
16345 views
// This file is part of OpenCV project.1// It is subject to the license terms in the LICENSE file found in the top-level directory2// of this distribution and at http://opencv.org/license.html.34// Copyright (C) 2018, Intel Corporation, all rights reserved.5// Third party copyrights are property of their respective owners.67#include "../precomp.hpp"89#ifdef HAVE_PROTOBUF1011#include "tf_graph_simplifier.hpp"1213namespace cv { namespace dnn {14CV__DNN_INLINE_NS_BEGIN1516using ::google::protobuf::RepeatedField;17using ::google::protobuf::MapPair;1819class Subgraph // Interface to match and replace TensorFlow subgraphs.20{21public:22virtual ~Subgraph() {}2324// Add a node to be matched in the origin graph. Specify ids of nodes that25// are expected to be inputs. Returns id of a newly added node.26// TODO: Replace inputs to std::vector<int> in C++1127int addNodeToMatch(const std::string& op, int input_0 = -1, int input_1 = -1,28int input_2 = -1, int input_3 = -1)29{30int nodeInputs[] = {input_0, input_1, input_2, input_3};31int numInputs = 0;32for (int i = 0; i < 4; ++i)33{34numInputs += (int)(nodeInputs[i] != -1);35}36return addNodeToMatch(op, std::vector<int>(&nodeInputs[0], &nodeInputs[0] + numInputs));37}3839int addNodeToMatch(const std::string& op, const std::vector<int>& inputs_)40{41for (int i = 0; i < inputs_.size(); ++i)42{43CV_Assert(inputs_[i] < (int)nodes.size());44}45nodes.push_back(op);46inputs.push_back(inputs_);47return nodes.size() - 1;48}4950// Specify resulting node. All the matched nodes in subgraph excluding51// input nodes will be fused into this single node.52// TODO: Replace inputs to std::vector<int> in C++1153void setFusedNode(const std::string& op, int input_0 = -1, int input_1 = -1,54int input_2 = -1, int input_3 = -1, int input_4 = -1,55int input_5 = -1)56{57int nodeInputs[] = {input_0, input_1, input_2, input_3, input_4, input_5};58int numInputs = 0;59for (int i = 0; i < 6; ++i)60{61CV_Assert(nodeInputs[i] < (int)nodes.size());62numInputs += (int)(nodeInputs[i] != -1);63}64setFusedNode(op, std::vector<int>(&nodeInputs[0], &nodeInputs[0] + numInputs));65}6667void setFusedNode(const std::string& op, const std::vector<int>& inputs_)68{69fusedNodeInputs = inputs_;70fusedNodeOp = op;71nodesToFuse.clear();72for (int i = 0; i < nodes.size(); ++i)73{74if (std::find(fusedNodeInputs.begin(), fusedNodeInputs.end(), i) == fusedNodeInputs.end() &&75nodes[i] != "Const")76nodesToFuse.push_back(i);77}78}7980static const tensorflow::NodeDef& getInputNode(const tensorflow::GraphDef& net,81const tensorflow::NodeDef& node,82int inpId)83{84CV_Assert(inpId < node.input_size());85std::string name = node.input(inpId);86// If operation produces several tensors, they are specified by index87// after ':' character. In example, "input:0".88name = name.substr(0, name.rfind(':'));89const int numNodes = net.node_size();90for (int i = 0; i < numNodes; ++i)91{92if (net.node(i).name() == name)93return net.node(i);94}95CV_Error(Error::StsParseError, "Input node with name " + name + " not found");96}9798// Match TensorFlow subgraph starting from <nodeId> with a set of nodes to be fused.99// Const nodes are skipped during matching. Returns true if nodes are matched and can be fused.100virtual bool match(const tensorflow::GraphDef& net, int nodeId, std::vector<int>& matchedNodesIds)101{102matchedNodesIds.clear();103matchedNodesIds.reserve(nodesToFuse.size());104105int numNodes = net.node_size();106for (int i = 0; i < nodesToFuse.size(); ++i)107{108while (nodeId < numNodes && net.node(nodeId).op() == "Const")109{110nodeId += 1;111}112if (nodeId > numNodes - 1)113return false;114115const tensorflow::NodeDef& node = net.node(nodeId);116117if (node.op() != nodes[nodesToFuse[i]])118return false;119120std::vector<int>& inputNodes = inputs[nodesToFuse[i]];121if (inputNodes.size() != node.input_size())122return false;123for (int j = 0; j < inputNodes.size(); ++j)124{125if (nodes[inputNodes[j]].empty()) // Unknown input node type.126continue;127const tensorflow::NodeDef& inpNode = getInputNode(net, node, j);128if (inpNode.op() != nodes[inputNodes[j]])129return false;130}131132matchedNodesIds.push_back(nodeId);133nodeId += 1;134}135return true;136}137138// Fuse matched subgraph.139void replace(tensorflow::GraphDef& net, const std::vector<int>& matchedNodesIds)140{141// Extract names of input nodes.142std::vector<std::string> inputsNames(fusedNodeInputs.size());143for (int i = 0; i < fusedNodeInputs.size(); ++i)144{145std::string inpName;146// Find input node name looking at inputs of fused nodes.147for (int j = 0; j < matchedNodesIds.size() && inpName.empty(); ++j)148{149const tensorflow::NodeDef &node = net.node(matchedNodesIds[j]);150std::vector<int>& inpIndices = inputs[nodesToFuse[j]];151152CV_Assert(node.input_size() == inpIndices.size());153for (int k = 0; k < inpIndices.size(); ++k)154{155if (inpIndices[k] == fusedNodeInputs[i])156{157inpName = node.input(k);158break;159}160}161}162CV_Assert(!inpName.empty());163inputsNames[i] = inpName;164}165166// Remove matched nodes except the last one. Indices in ascending order are expected.167tensorflow::NodeDef* node = net.mutable_node(matchedNodesIds.back());168for (int i = matchedNodesIds.size() - 2; i >= 0; --i)169net.mutable_node()->DeleteSubrange(matchedNodesIds[i], 1);170171// Modify the last node to be a fused one.172node->set_op(fusedNodeOp);173node->clear_input();174for (int i = 0; i < inputsNames.size(); ++i)175{176node->add_input(inputsNames[i]);177}178179std::vector<tensorflow::NodeDef*> inputNodes(inputsNames.size());180for (int i = 0; i < inputsNames.size(); ++i)181{182inputNodes[i] = (tensorflow::NodeDef*)&getInputNode(net, *node, i);183}184finalize(net, node, inputNodes);185}186187virtual void finalize(tensorflow::GraphDef&, tensorflow::NodeDef*,188std::vector<tensorflow::NodeDef*>&) {}189190private:191std::vector<std::string> nodes; // Nodes to be matched in the origin graph.192std::vector<std::vector<int> > inputs; // Connections of an every node to it's inputs.193194std::string fusedNodeOp; // Operation name of resulting fused node.195std::vector<int> nodesToFuse; // Set of nodes to be fused.196std::vector<int> fusedNodeInputs; // Inputs of fused node.197};198199class BatchNormSubgraph : public Subgraph200{201public:202BatchNormSubgraph()203{204int input = addNodeToMatch("");205int epsilon = addNodeToMatch("Const");206int moving_variance = addNodeToMatch("Const");207int moving_mean = addNodeToMatch("Const");208int beta = addNodeToMatch("Const");209int gamma = addNodeToMatch("Const");210int add = addNodeToMatch("Add", moving_variance, epsilon);211int rsqrt = addNodeToMatch("Rsqrt", add);212int mul = addNodeToMatch("Mul", rsqrt, gamma);213int mul_1 = addNodeToMatch("Mul", input, mul);214int mul_2 = addNodeToMatch("Mul", moving_mean, mul);215int sub = addNodeToMatch("Sub", beta, mul_2);216addNodeToMatch("Add", mul_1, sub);217218setFusedNode("FusedBatchNorm", input, gamma, beta, moving_mean, moving_variance, epsilon);219}220221virtual void finalize(tensorflow::GraphDef&, tensorflow::NodeDef* fusedNode,222std::vector<tensorflow::NodeDef*>& inputNodes) CV_OVERRIDE223{224Mat epsMat = getTensorContent(inputNodes.back()->attr().at("value").tensor());225CV_CheckEQ(epsMat.total(), (size_t)1, ""); CV_CheckTypeEQ(epsMat.type(), CV_32FC1, "");226227fusedNode->mutable_input()->RemoveLast();228fusedNode->clear_attr();229tensorflow::AttrValue epsilon;230epsilon.set_f(epsMat.at<float>(0));231fusedNode->mutable_attr()->insert(MapPair<std::string, tensorflow::AttrValue>("epsilon", epsilon));232}233};234235class BatchNormNoGammaSubgraph : public Subgraph236{237public:238BatchNormNoGammaSubgraph()239{240int input = addNodeToMatch("");241int epsilon = addNodeToMatch("Const");242int moving_variance = addNodeToMatch("Const");243int moving_mean = addNodeToMatch("Const");244int beta = addNodeToMatch("Const");245int add = addNodeToMatch("Add", moving_variance, epsilon);246int rsqrt = addNodeToMatch("Rsqrt", add);247int mul = addNodeToMatch("Mul", input, rsqrt);248int mul_1 = addNodeToMatch("Mul", moving_mean, rsqrt);249int sub = addNodeToMatch("Sub", beta, mul_1);250addNodeToMatch("Add", mul, sub);251252// There is a fake reference to beta that will be replaced to a new gamma tensor.253setFusedNode("FusedBatchNorm", input, beta, beta, moving_mean, moving_variance, epsilon);254}255256virtual void finalize(tensorflow::GraphDef& net, tensorflow::NodeDef* fusedNode,257std::vector<tensorflow::NodeDef*>& inputNodes) CV_OVERRIDE258{259Mat epsMat = getTensorContent(inputNodes.back()->attr().at("value").tensor());260CV_CheckEQ(epsMat.total(), (size_t)1, ""); CV_CheckTypeEQ(epsMat.type(), CV_32FC1, "");261262fusedNode->mutable_input()->RemoveLast();263fusedNode->clear_attr();264tensorflow::AttrValue epsilon;265epsilon.set_f(epsMat.at<float>(0));266fusedNode->mutable_attr()->insert(MapPair<std::string, tensorflow::AttrValue>("epsilon", epsilon));267268tensorflow::NodeDef* gamma = net.add_node();269gamma->set_op("Const");270gamma->set_name(fusedNode->name() + "/gamma");271// Just put a single value to recognize this node as Const.272gamma->mutable_attr()->insert(MapPair<std::string, tensorflow::AttrValue>("value", epsilon));273fusedNode->set_input(1, gamma->name());274}275};276277// tf.contrib.layers.flatten278class FlattenSubgraph : public Subgraph279{280public:281FlattenSubgraph()282{283int input = addNodeToMatch("");284int shape = addNodeToMatch("Const");285int stack = addNodeToMatch("Const");286int stack_1 = addNodeToMatch("Const");287int stack_2 = addNodeToMatch("Const");288int strided_slice = addNodeToMatch("StridedSlice", shape, stack, stack_1, stack_2);289int shape_pack = addNodeToMatch("Const");290int pack = addNodeToMatch("Pack", strided_slice, shape_pack);291addNodeToMatch("Reshape", input, pack);292293setFusedNode("Flatten", input);294}295};296297// tf.contrib.layers.flatten in case of unknown batch size298class FlattenShapeSubgraph : public Subgraph299{300public:301FlattenShapeSubgraph()302{303int input = addNodeToMatch("");304int shape = addNodeToMatch("Shape", input);305int stack = addNodeToMatch("Const");306int stack_1 = addNodeToMatch("Const");307int stack_2 = addNodeToMatch("Const");308int strided_slice = addNodeToMatch("StridedSlice", shape, stack, stack_1, stack_2);309int shape_pack = addNodeToMatch("Const");310int pack = addNodeToMatch("Pack", strided_slice, shape_pack);311addNodeToMatch("Reshape", input, pack);312313setFusedNode("Flatten", input);314}315};316317// K.layers.Softmax318class SoftMaxKerasSubgraph : public Subgraph319{320public:321SoftMaxKerasSubgraph()322{323int input = addNodeToMatch("");324int maxReductionIndices = addNodeToMatch("Const");325int smMax = addNodeToMatch("Max", input, maxReductionIndices);326int smSub = addNodeToMatch("Sub", input, smMax);327int smExp = addNodeToMatch("Exp", smSub);328int sumReductionIndices = addNodeToMatch("Const");329int smSum = addNodeToMatch("Sum", smExp, sumReductionIndices);330addNodeToMatch("RealDiv", smExp, smSum);331332setFusedNode("Softmax", input);333}334};335336class ReLU6KerasSubgraph : public Subgraph337{338public:339ReLU6KerasSubgraph()340{341int input = addNodeToMatch("");342int relu = addNodeToMatch("Relu", input);343int maxValue = addNodeToMatch("Const");344int clipValue = addNodeToMatch("Const");345int minimum = addNodeToMatch("Minimum", relu, maxValue);346addNodeToMatch("Maximum", minimum, clipValue);347348setFusedNode("Relu6", input);349}350351virtual bool match(const tensorflow::GraphDef& net, int nodeId, std::vector<int>& matchedNodesIds) CV_OVERRIDE352{353if (!Subgraph::match(net, nodeId, matchedNodesIds))354return false;355Mat maxValue = getTensorContent(net.node(nodeId + 1).attr().at("value").tensor());356return maxValue.type() == CV_32FC1 && maxValue.total() == 1 && maxValue.at<float>(0) == 6;357}358};359360// Keras' reshape stores output shape in separate Const nodes by one value.361// Need to merge them into a single Const node.362class ReshapeKerasSubgraph : public Subgraph363{364public:365ReshapeKerasSubgraph(int _numOutDims) : numOutDims(_numOutDims)366{367int input = addNodeToMatch("");368int shape = addNodeToMatch("Shape", input);369int stack = addNodeToMatch("Const");370int stack_1 = addNodeToMatch("Const");371int stack_2 = addNodeToMatch("Const");372int strided_slice = addNodeToMatch("StridedSlice", shape, stack, stack_1, stack_2);373374std::vector<int> ids(1 + numOutDims);375ids[0] = strided_slice;376for (int i = 0; i < numOutDims; ++i)377ids[1 + i] = addNodeToMatch("Const");378int pack = addNodeToMatch("Pack", ids);379addNodeToMatch("Reshape", input, pack);380381ids[0] = input;382setFusedNode("Reshape", ids);383}384385virtual void finalize(tensorflow::GraphDef&, tensorflow::NodeDef* fusedNode,386std::vector<tensorflow::NodeDef*>& inputNodes) CV_OVERRIDE387{388std::vector<int> shape(numOutDims + 1); // batch size in Keras is implicit.389shape[0] = -1;390for (int i = 0; i < numOutDims; ++i)391{392shape[1 + i] = inputNodes[1 + i]->attr().at("value").tensor().int_val(0);393}394tensorflow::TensorProto* shapeTensor = inputNodes[1]->mutable_attr()->at("value").mutable_tensor();395fusedNode->mutable_input()->DeleteSubrange(2, numOutDims - 1);396397shapeTensor->clear_int_val();398for (int i = 0; i < shape.size(); ++i)399{400shapeTensor->add_int_val(shape[i]);401}402}403404private:405int numOutDims;406};407408class L2NormalizeSubgraph : public Subgraph409{410public:411L2NormalizeSubgraph()412{413int input = addNodeToMatch("");414int square = addNodeToMatch("Square", input);415int reductionIndices = addNodeToMatch("Const");416int sum = addNodeToMatch("Sum", square, reductionIndices);417int y = addNodeToMatch("Const");418int maximum = addNodeToMatch("Maximum", sum, y);419int rsqrt = addNodeToMatch("Rsqrt", maximum);420addNodeToMatch("Mul", input, rsqrt);421setFusedNode("L2Normalize", input, reductionIndices);422}423};424425class DeconvolutionValidKerasSubgraph : public Subgraph426{427public:428DeconvolutionValidKerasSubgraph()429{430int input = addNodeToMatch("");431int shape = addNodeToMatch("Shape", input);432int kernel = addNodeToMatch("Const");433434int stack = addNodeToMatch("Const");435int stack_1 = addNodeToMatch("Const");436int stack_2 = addNodeToMatch("Const");437int strided_slice = addNodeToMatch("StridedSlice", shape, stack, stack_1, stack_2);438439stack = addNodeToMatch("Const");440stack_1 = addNodeToMatch("Const");441stack_2 = addNodeToMatch("Const");442int strided_slice_1 = addNodeToMatch("StridedSlice", shape, stack, stack_1, stack_2);443444stack = addNodeToMatch("Const");445stack_1 = addNodeToMatch("Const");446stack_2 = addNodeToMatch("Const");447int strided_slice_2 = addNodeToMatch("StridedSlice", shape, stack, stack_1, stack_2);448449int mul = addNodeToMatch("Mul", strided_slice_1, addNodeToMatch("Const"));450int add = addNodeToMatch("Add", mul, addNodeToMatch("Const"));451452int mul_1 = addNodeToMatch("Mul", strided_slice_2, addNodeToMatch("Const"));453int add_1 = addNodeToMatch("Add", mul_1, addNodeToMatch("Const"));454int pack = addNodeToMatch("Pack", strided_slice, add, add_1, addNodeToMatch("Const"));455addNodeToMatch("Conv2DBackpropInput", pack, kernel, input);456// Put any unused Const op to the first input.457setFusedNode("Conv2DBackpropInput", stack, kernel, input);458}459460virtual void finalize(tensorflow::GraphDef&, tensorflow::NodeDef* fusedNode,461std::vector<tensorflow::NodeDef*>& inputNodes) CV_OVERRIDE462{463// Disable adjusted paddings (see Conv2DBackpropInput layer at tf_importer.cpp)464// adj_w = (outW - (pad == "SAME") ? 1 : kernelW) % strideX;465// adj_h = (outH - (pad == "SAME") ? 1 : kernelH) % strideY;466// Where outH and outW are 1st and 2nd dimensions (NHWC) or 2nd and third (NCHW).467std::string padMode = fusedNode->attr().at("padding").s();468CV_Assert(padMode == "VALID");469470const tensorflow::TensorShapeProto& kernelShape =471inputNodes[1]->mutable_attr()->at("value").tensor().tensor_shape();472473CV_Assert(kernelShape.dim_size() == 4);474const int kernelHeight = kernelShape.dim(0).size();475const int kernelWidth = kernelShape.dim(1).size();476477tensorflow::TensorProto* outShape = inputNodes[0]->mutable_attr()->at("value").mutable_tensor();478outShape->clear_int_val();479outShape->add_int_val(-1);480outShape->add_int_val(kernelHeight);481outShape->add_int_val(kernelWidth);482outShape->add_int_val(-1);483}484};485486class DeconvolutionSameKerasSubgraph : public Subgraph487{488public:489DeconvolutionSameKerasSubgraph()490{491int input = addNodeToMatch("");492int shape = addNodeToMatch("Shape", input);493int kernel = addNodeToMatch("Const");494495int stack = addNodeToMatch("Const");496int stack_1 = addNodeToMatch("Const");497int stack_2 = addNodeToMatch("Const");498int strided_slice = addNodeToMatch("StridedSlice", shape, stack, stack_1, stack_2);499500stack = addNodeToMatch("Const");501stack_1 = addNodeToMatch("Const");502stack_2 = addNodeToMatch("Const");503int strided_slice_1 = addNodeToMatch("StridedSlice", shape, stack, stack_1, stack_2);504505stack = addNodeToMatch("Const");506stack_1 = addNodeToMatch("Const");507stack_2 = addNodeToMatch("Const");508int strided_slice_2 = addNodeToMatch("StridedSlice", shape, stack, stack_1, stack_2);509510int mul = addNodeToMatch("Mul", strided_slice_1, addNodeToMatch("Const"));511512int mul_1 = addNodeToMatch("Mul", strided_slice_2, addNodeToMatch("Const"));513int pack = addNodeToMatch("Pack", strided_slice, mul, mul_1, addNodeToMatch("Const"));514addNodeToMatch("Conv2DBackpropInput", pack, kernel, input);515// Put any unused Const op to the first input.516setFusedNode("Conv2DBackpropInput", stack, kernel, input);517}518519virtual void finalize(tensorflow::GraphDef&, tensorflow::NodeDef* fusedNode,520std::vector<tensorflow::NodeDef*>& inputNodes) CV_OVERRIDE521{522// Disable adjusted paddings (see Conv2DBackpropInput layer at tf_importer.cpp)523// adj_w = (outW - (pad == "SAME") ? 1 : kernelW) % strideX;524// adj_h = (outH - (pad == "SAME") ? 1 : kernelH) % strideY;525// Where outH and outW are 1st and 2nd dimensions (NHWC) or 2nd and third (NCHW).526std::string padMode = fusedNode->attr().at("padding").s();527CV_Assert(padMode == "SAME");528529const tensorflow::AttrValue_ListValue& strides = fusedNode->attr().at("strides").list();530CV_Assert(strides.i_size() == 4);531532const int strideY = strides.i(1);533const int strideX = strides.i(2);534535tensorflow::TensorProto* outShape = inputNodes[0]->mutable_attr()->at("value").mutable_tensor();536outShape->clear_int_val();537outShape->add_int_val(-1);538outShape->add_int_val(strideY);539outShape->add_int_val(strideX);540outShape->add_int_val(-1);541}542};543544// In case of resizing by factor.545class ResizeBilinearSubgraph : public Subgraph546{547public:548ResizeBilinearSubgraph()549{550int input = addNodeToMatch("");551552int shape = addNodeToMatch("Shape", input);553int stack = addNodeToMatch("Const");554int stack_1 = addNodeToMatch("Const");555int stack_2 = addNodeToMatch("Const");556int strided_slice = addNodeToMatch("StridedSlice", shape, stack, stack_1, stack_2);557int factorY = addNodeToMatch("Const");558int mul = addNodeToMatch("Mul", strided_slice, factorY);559560shape = addNodeToMatch("Shape", input);561stack = addNodeToMatch("Const");562stack_1 = addNodeToMatch("Const");563stack_2 = addNodeToMatch("Const");564strided_slice = addNodeToMatch("StridedSlice", shape, stack, stack_1, stack_2);565int factorX = addNodeToMatch("Const");566int mul_1 = addNodeToMatch("Mul", strided_slice, factorX);567568int pack = addNodeToMatch("Pack", mul, mul_1);569570addNodeToMatch("ResizeBilinear", input, pack);571setFusedNode("ResizeBilinear", input, factorY, factorX);572}573};574575// In case of resizing by factor.576class UpsamplingKerasSubgraph : public Subgraph577{578public:579UpsamplingKerasSubgraph()580{581int input = addNodeToMatch("");582int shape = addNodeToMatch("Shape", input);583int stack = addNodeToMatch("Const");584int stack_1 = addNodeToMatch("Const");585int stack_2 = addNodeToMatch("Const");586int strided_slice = addNodeToMatch("StridedSlice", shape, stack, stack_1, stack_2);587int factors = addNodeToMatch("Const");588int mul = addNodeToMatch("Mul", strided_slice, factors);589addNodeToMatch("ResizeNearestNeighbor", input, mul);590setFusedNode("ResizeNearestNeighbor", input, factors);591}592593virtual void finalize(tensorflow::GraphDef& net, tensorflow::NodeDef* fusedNode,594std::vector<tensorflow::NodeDef*>& inputNodes) CV_OVERRIDE595{596Mat factorsMat = getTensorContent(inputNodes[1]->attr().at("value").tensor());597CV_CheckEQ(factorsMat.total(), (size_t)2, ""); CV_CheckTypeEQ(factorsMat.type(), CV_32SC1, "");598599// Height scale factor600tensorflow::TensorProto* factorY = inputNodes[1]->mutable_attr()->at("value").mutable_tensor();601factorY->clear_int_val();602factorY->clear_tensor_content();603factorY->add_int_val(factorsMat.at<int>(0, 0));604605// Width scale factor.606tensorflow::NodeDef* factorXNode = net.add_node();607factorXNode->set_op("Const");608factorXNode->set_name(fusedNode->name() + "/factor_y");609610tensorflow::AttrValue factorX;611factorX.mutable_tensor()->set_dtype(tensorflow::DT_INT32);612factorX.mutable_tensor()->add_int_val(factorsMat.at<int>(0, 1));613factorXNode->mutable_attr()->insert(MapPair<std::string, tensorflow::AttrValue>("value", factorX));614615fusedNode->add_input(factorXNode->name());616}617};618619class ReshapeAsShapeSubgraph : public Subgraph620{621public:622ReshapeAsShapeSubgraph()623{624int input = addNodeToMatch("");625int shapeSrc = addNodeToMatch("");626int shape = addNodeToMatch("Shape", shapeSrc);627addNodeToMatch("Reshape", input, shape);628setFusedNode("Reshape", input, shapeSrc);629}630};631632void simplifySubgraphs(tensorflow::GraphDef& net)633{634std::vector<Ptr<Subgraph> > subgraphs;635subgraphs.push_back(Ptr<Subgraph>(new BatchNormSubgraph()));636subgraphs.push_back(Ptr<Subgraph>(new BatchNormNoGammaSubgraph()));637subgraphs.push_back(Ptr<Subgraph>(new FlattenSubgraph()));638subgraphs.push_back(Ptr<Subgraph>(new FlattenShapeSubgraph()));639subgraphs.push_back(Ptr<Subgraph>(new SoftMaxKerasSubgraph()));640subgraphs.push_back(Ptr<Subgraph>(new ReLU6KerasSubgraph()));641subgraphs.push_back(Ptr<Subgraph>(new ReshapeKerasSubgraph(3)));642subgraphs.push_back(Ptr<Subgraph>(new L2NormalizeSubgraph()));643subgraphs.push_back(Ptr<Subgraph>(new DeconvolutionValidKerasSubgraph()));644subgraphs.push_back(Ptr<Subgraph>(new DeconvolutionSameKerasSubgraph()));645subgraphs.push_back(Ptr<Subgraph>(new ResizeBilinearSubgraph()));646subgraphs.push_back(Ptr<Subgraph>(new UpsamplingKerasSubgraph()));647subgraphs.push_back(Ptr<Subgraph>(new ReshapeAsShapeSubgraph()));648649int numNodes = net.node_size();650std::vector<int> matchedNodesIds;651for (int i = 0; i < numNodes; ++i)652{653for (int j = 0; j < subgraphs.size(); ++j)654{655if (subgraphs[j]->match(net, i, matchedNodesIds))656{657subgraphs[j]->replace(net, matchedNodesIds);658numNodes -= matchedNodesIds.size() - 1; // #matchedNodes removed and one added.659break;660}661}662}663}664665void RemoveIdentityOps(tensorflow::GraphDef& net)666{667typedef std::map<String, String> IdentityOpsMap;668IdentityOpsMap identity_ops;669670std::vector<int> identity_ops_idx;671672int layersCount = net.node_size();673for (int li = 0; li < layersCount; li++)674{675const tensorflow::NodeDef &layer = net.node(li);676String type = layer.op();677678if (type == "Identity" || type == "Dropout") {679identity_ops_idx.push_back(li);680identity_ops[layer.name()] = layer.input(0);681}682}683684for (int li = 0; li < layersCount; li++)685{686tensorflow::NodeDef* layer = net.mutable_node(li);687for (int input_id = 0; input_id < layer->input_size(); input_id++) {688String input_op_name = layer->input(input_id);689IdentityOpsMap::iterator it = identity_ops.find(input_op_name);690691if (it != identity_ops.end()) {692layer->set_input(input_id, it->second);693}694}695}696697std::sort(identity_ops_idx.begin(), identity_ops_idx.end());698699int removed_nodes = 0;700for(size_t i = 0; i < identity_ops_idx.size(); i++) {701int start_id = identity_ops_idx[i] - removed_nodes;702net.mutable_node()->DeleteSubrange(start_id, 1);703removed_nodes++;704}705}706707Mat getTensorContent(const tensorflow::TensorProto &tensor)708{709const std::string& content = tensor.tensor_content();710switch (tensor.dtype())711{712case tensorflow::DT_FLOAT:713{714if (!content.empty())715return Mat(1, content.size() / sizeof(float), CV_32FC1, (void*)content.c_str()).clone();716else717{718const RepeatedField<float>& field = tensor.float_val();719CV_Assert(!field.empty());720return Mat(1, field.size(), CV_32FC1, (void*)field.data()).clone();721}722}723case tensorflow::DT_DOUBLE:724{725if (!content.empty())726return Mat(1, content.size() / sizeof(double), CV_64FC1, (void*)content.c_str()).clone();727else728{729const RepeatedField<double>& field = tensor.double_val();730CV_Assert(!field.empty());731return Mat(1, field.size(), CV_64FC1, (void*)field.data()).clone();732}733}734case tensorflow::DT_INT32:735{736if (!content.empty())737return Mat(1, content.size() / sizeof(int32_t), CV_32SC1, (void*)content.c_str()).clone();738else739{740const RepeatedField<int32_t>& field = tensor.int_val();741CV_Assert(!field.empty());742return Mat(1, field.size(), CV_32SC1, (void*)field.data()).clone();743}744}745case tensorflow::DT_HALF:746{747Mat halfs;748if (!content.empty())749{750static const int kHalfSize = 2;751halfs = Mat(1, content.size() / kHalfSize, CV_16UC1, (void*)content.c_str());752}753else754{755const RepeatedField<int32_t>& field = tensor.half_val();756CV_Assert(!field.empty());757Mat ints(1, field.size(), CV_32SC1, (void*)field.data());758ints.convertTo(halfs, CV_16UC1);759}760// Reinterpret as a signed shorts just for a convertFp16 call.761Mat halfsSigned(halfs.size(), CV_16SC1, halfs.data);762Mat floats(halfs.size(), CV_32FC1);763convertFp16(halfsSigned, floats);764return floats;765}766case tensorflow::DT_QUINT8:767{768CV_Assert(!content.empty());769return Mat(1, content.size(), CV_8UC1, (void*)content.c_str()).clone();770}771default:772CV_Error(Error::StsError, "Tensor's data type is not supported");773break;774}775return Mat();776}777778void releaseTensor(tensorflow::TensorProto* tensor)779{780if (!tensor->mutable_tensor_content()->empty())781{782delete tensor->release_tensor_content();783}784}785786static void permute(google::protobuf::RepeatedPtrField<tensorflow::NodeDef>* data,787const std::vector<int>& indices)788{789const int num = data->size();790CV_Assert(num == indices.size());791792std::vector<int> elemIdToPos(num);793std::vector<int> posToElemId(num);794for (int i = 0; i < num; ++i)795{796elemIdToPos[i] = i;797posToElemId[i] = i;798}799for (int i = 0; i < num; ++i)800{801int elemId = indices[i];802int pos = elemIdToPos[elemId];803if (pos != i)804{805data->SwapElements(i, pos);806const int swappedElemId = posToElemId[i];807elemIdToPos[elemId] = i;808elemIdToPos[swappedElemId] = pos;809810posToElemId[i] = elemId;811posToElemId[pos] = swappedElemId;812}813}814}815816// Is based on tensorflow::graph_transforms::SortByExecutionOrder817void sortByExecutionOrder(tensorflow::GraphDef& net)818{819// Maps node's name to index at net.node() list.820std::map<std::string, int> nodesMap;821std::map<std::string, int>::iterator nodesMapIt;822for (int i = 0; i < net.node_size(); ++i)823{824const tensorflow::NodeDef& node = net.node(i);825nodesMap.insert(std::make_pair(node.name(), i));826}827828// Indices of nodes which use specific node as input.829std::vector<std::vector<int> > edges(nodesMap.size());830std::vector<int> numRefsToAdd(nodesMap.size(), 0);831std::vector<int> nodesToAdd;832for (int i = 0; i < net.node_size(); ++i)833{834const tensorflow::NodeDef& node = net.node(i);835for (int j = 0; j < node.input_size(); ++j)836{837std::string inpName = node.input(j);838inpName = inpName.substr(0, inpName.rfind(':'));839inpName = inpName.substr(inpName.find('^') + 1);840841nodesMapIt = nodesMap.find(inpName);842CV_Assert(nodesMapIt != nodesMap.end());843edges[nodesMapIt->second].push_back(i);844}845if (node.input_size() == 0)846nodesToAdd.push_back(i);847else848{849if (node.op() == "Merge" || node.op() == "RefMerge")850{851int numControlEdges = 0;852for (int j = 0; j < node.input_size(); ++j)853numControlEdges += node.input(j)[0] == '^';854numRefsToAdd[i] = numControlEdges + 1;855}856else857numRefsToAdd[i] = node.input_size();858}859}860861std::vector<int> permIds;862permIds.reserve(net.node_size());863while (!nodesToAdd.empty())864{865int nodeToAdd = nodesToAdd.back();866nodesToAdd.pop_back();867868permIds.push_back(nodeToAdd);869// std::cout << net.node(nodeToAdd).name() << '\n';870871for (int i = 0; i < edges[nodeToAdd].size(); ++i)872{873int consumerId = edges[nodeToAdd][i];874if (numRefsToAdd[consumerId] > 0)875{876if (numRefsToAdd[consumerId] == 1)877nodesToAdd.push_back(consumerId);878else879CV_Assert(numRefsToAdd[consumerId] >= 0);880numRefsToAdd[consumerId] -= 1;881}882}883}884CV_Assert(permIds.size() == net.node_size());885permute(net.mutable_node(), permIds);886}887888CV__DNN_INLINE_NS_END889}} // namespace dnn, namespace cv890891#endif // HAVE_PROTOBUF892893894