|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#ifndef __OPENCV_DNN_GRAPH_SIMPLIFIER_HPP__
|
|
|
#define __OPENCV_DNN_GRAPH_SIMPLIFIER_HPP__
|
|
|
|
|
|
#include <string>
|
|
|
|
|
|
#include <opencv2/core.hpp>
|
|
|
|
|
|
namespace cv { namespace dnn {
|
|
|
|
|
|
class ImportNodeWrapper
|
|
|
{
|
|
|
public:
|
|
|
virtual ~ImportNodeWrapper() {}
|
|
|
|
|
|
virtual int getNumInputs() const = 0;
|
|
|
|
|
|
virtual std::string getInputName(int idx) const = 0;
|
|
|
|
|
|
virtual std::string getType() const = 0;
|
|
|
|
|
|
virtual void setType(const std::string& type) = 0;
|
|
|
|
|
|
virtual void setInputNames(const std::vector<std::string>& inputs) = 0;
|
|
|
};
|
|
|
|
|
|
class ImportGraphWrapper
|
|
|
{
|
|
|
public:
|
|
|
virtual ~ImportGraphWrapper() {}
|
|
|
|
|
|
virtual Ptr<ImportNodeWrapper> getNode(int idx) const = 0;
|
|
|
|
|
|
virtual int getNumNodes() const = 0;
|
|
|
|
|
|
virtual int getNumOutputs(int nodeId) const = 0;
|
|
|
|
|
|
virtual std::string getOutputName(int nodeId, int outId) const = 0;
|
|
|
|
|
|
virtual void removeNode(int idx) = 0;
|
|
|
|
|
|
virtual bool isCommutativeOp(const std::string& type) const = 0;
|
|
|
};
|
|
|
|
|
|
class Subgraph
|
|
|
{
|
|
|
public:
|
|
|
virtual ~Subgraph();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int addNodeToMatch(const std::string& op, int input_0 = -1, int input_1 = -1,
|
|
|
int input_2 = -1, int input_3 = -1);
|
|
|
|
|
|
int addNodeToMatch(const std::string& op, const std::vector<int>& inputs_);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void setFusedNode(const std::string& op, int input_0 = -1, int input_1 = -1,
|
|
|
int input_2 = -1, int input_3 = -1, int input_4 = -1,
|
|
|
int input_5 = -1);
|
|
|
|
|
|
void setFusedNode(const std::string& op, const std::vector<int>& inputs_);
|
|
|
|
|
|
static int getInputNodeId(const Ptr<ImportGraphWrapper>& net,
|
|
|
const Ptr<ImportNodeWrapper>& node,
|
|
|
int inpId);
|
|
|
|
|
|
|
|
|
|
|
|
virtual bool match(const Ptr<ImportGraphWrapper>& net, int nodeId,
|
|
|
std::vector<int>& matchedNodesIds);
|
|
|
|
|
|
|
|
|
void replace(const Ptr<ImportGraphWrapper>& net, const std::vector<int>& matchedNodesIds);
|
|
|
|
|
|
virtual void finalize(const Ptr<ImportGraphWrapper>& net,
|
|
|
const Ptr<ImportNodeWrapper>& fusedNode,
|
|
|
std::vector<Ptr<ImportNodeWrapper> >& inputs);
|
|
|
|
|
|
private:
|
|
|
std::vector<std::string> nodes;
|
|
|
std::vector<std::vector<int> > inputs;
|
|
|
|
|
|
std::string fusedNodeOp;
|
|
|
std::vector<int> fusedNodeInputs;
|
|
|
};
|
|
|
|
|
|
void simplifySubgraphs(const Ptr<ImportGraphWrapper>& net,
|
|
|
const std::vector<Ptr<Subgraph> >& patterns);
|
|
|
|
|
|
}}
|
|
|
|
|
|
#endif
|
|
|
|