2018-09-27 02:50:39 +08:00
|
|
|
// 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.
|
|
|
|
//
|
|
|
|
// Copyright (C) 2018 Intel Corporation
|
|
|
|
|
|
|
|
|
|
|
|
#ifndef OPENCV_GAPI_GMODEL_HPP
|
|
|
|
#define OPENCV_GAPI_GMODEL_HPP
|
|
|
|
|
|
|
|
#include <memory> // shared_ptr
|
|
|
|
#include <unordered_map>
|
|
|
|
#include <functional> // std::function
|
|
|
|
|
|
|
|
#include <ade/graph.hpp>
|
|
|
|
#include <ade/typed_graph.hpp>
|
|
|
|
#include <ade/passes/topological_sort.hpp>
|
|
|
|
|
|
|
|
// /!\ ATTENTION:
|
|
|
|
//
|
|
|
|
// No API includes like GMat, GNode, GCall here!
|
|
|
|
// This part of the system is API-unaware by its design.
|
|
|
|
//
|
|
|
|
|
2019-08-05 22:56:34 +08:00
|
|
|
#include <opencv2/gapi/util/any.hpp>
|
|
|
|
|
2019-06-15 00:27:19 +08:00
|
|
|
#include <opencv2/gapi/garg.hpp>
|
|
|
|
#include <opencv2/gapi/gkernel.hpp>
|
2018-09-27 02:50:39 +08:00
|
|
|
|
|
|
|
#include "compiler/gobjref.hpp"
|
|
|
|
#include "compiler/gislandmodel.hpp"
|
|
|
|
|
|
|
|
namespace cv { namespace gimpl {
|
|
|
|
|
|
|
|
// TODO: Document all metadata types
|
|
|
|
|
|
|
|
struct NodeType
|
|
|
|
{
|
|
|
|
static const char *name() { return "NodeType"; }
|
|
|
|
enum { OP, DATA } t;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct Input
|
|
|
|
{
|
|
|
|
static const char *name() { return "Input"; }
|
|
|
|
std::size_t port;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct Output
|
|
|
|
{
|
|
|
|
static const char *name() { return "Output"; }
|
|
|
|
std::size_t port;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct Op
|
|
|
|
{
|
|
|
|
static const char *name() { return "Op"; }
|
|
|
|
cv::GKernel k;
|
|
|
|
std::vector<GArg> args; // TODO: Introduce a new type for internal args?
|
|
|
|
std::vector<RcDesc> outs; // TODO: Introduce a new type for resource references
|
|
|
|
|
|
|
|
cv::gapi::GBackend backend;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct Data
|
|
|
|
{
|
|
|
|
static const char *name() { return "Data"; }
|
|
|
|
|
|
|
|
// FIXME: This is a _pure_ duplication of RcDesc now! (except storage)
|
|
|
|
GShape shape; // FIXME: Probably to be replaced by GMetaArg?
|
|
|
|
int rc;
|
|
|
|
GMetaArg meta;
|
|
|
|
HostCtor ctor; // T-specific helper to deal with unknown types in our code
|
|
|
|
// FIXME: Why rc+shape+meta is not represented as RcDesc here?
|
|
|
|
|
|
|
|
enum class Storage
|
|
|
|
{
|
|
|
|
INTERNAL, // data object is not listed in GComputation protocol
|
|
|
|
INPUT, // data object is listed in GComputation protocol as Input
|
|
|
|
OUTPUT, // data object is listed in GComputation protocol as Output
|
2019-08-05 22:56:34 +08:00
|
|
|
CONST_VAL, // data object is constant.
|
|
|
|
// Note: CONST is sometimes defined in Win sys headers
|
2018-09-27 02:50:39 +08:00
|
|
|
};
|
|
|
|
Storage storage;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct ConstValue
|
|
|
|
{
|
|
|
|
static const char *name() { return "ConstValue"; }
|
|
|
|
GRunArg arg;
|
|
|
|
};
|
|
|
|
|
|
|
|
// This metadata is valid for both DATA and OP kinds of nodes
|
|
|
|
// FIXME: Rename to IslandTag
|
|
|
|
struct Island
|
|
|
|
{
|
|
|
|
static const char *name() { return "Island"; }
|
|
|
|
std::string island; // can be set by user, otherwise is set by fusion
|
|
|
|
};
|
|
|
|
|
|
|
|
struct Protocol
|
|
|
|
{
|
|
|
|
static const char *name() { return "Protocol"; }
|
|
|
|
// TODO: Replace the whole thing with a "Protocol" object
|
|
|
|
std::vector<RcDesc> inputs;
|
|
|
|
std::vector<RcDesc> outputs;
|
|
|
|
|
|
|
|
std::vector<ade::NodeHandle> in_nhs;
|
|
|
|
std::vector<ade::NodeHandle> out_nhs;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct OutputMeta
|
|
|
|
{
|
|
|
|
static const char *name() { return "OutputMeta"; }
|
|
|
|
GMetaArgs outMeta;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct Journal
|
|
|
|
{
|
|
|
|
static const char *name() { return "Journal"; }
|
|
|
|
std::vector<std::string> messages;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Unique data object counter (per-type)
|
|
|
|
class DataObjectCounter
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
static const char* name() { return "DataObjectCounter"; }
|
|
|
|
int GetNewId(GShape shape) { return m_next_data_id[shape]++; }
|
|
|
|
private:
|
|
|
|
std::unordered_map<cv::GShape, int> m_next_data_id;
|
|
|
|
};
|
|
|
|
|
|
|
|
// A projected graph of Islands (generated from graph of Operations)
|
|
|
|
struct IslandModel
|
|
|
|
{
|
|
|
|
static const char* name() { return "IslandModel"; }
|
|
|
|
std::shared_ptr<ade::Graph> model;
|
|
|
|
};
|
|
|
|
|
|
|
|
// List of backends selected for current graph execution
|
|
|
|
struct ActiveBackends
|
|
|
|
{
|
|
|
|
static const char *name() { return "ActiveBackends"; }
|
|
|
|
std::unordered_set<cv::gapi::GBackend> backends;
|
|
|
|
};
|
|
|
|
|
2019-08-05 22:56:34 +08:00
|
|
|
// Backend-specific inference parameters for a neural network.
|
|
|
|
// Since these parameters are set on compilation stage (not
|
|
|
|
// on a construction stage), these parameters are bound lately
|
|
|
|
// to the operation node.
|
|
|
|
// NB: These parameters are not included into GModel by default
|
|
|
|
// since it is not used regularly by all parties.
|
|
|
|
struct NetworkParams
|
|
|
|
{
|
|
|
|
static const char *name() { return "NetworkParams"; }
|
|
|
|
cv::util::any opaque;
|
|
|
|
};
|
|
|
|
|
|
|
|
// This is a custom metadata handling operator.
|
|
|
|
// Sometimes outMeta() can't be bound to input parameters only
|
|
|
|
// so several backends (today -- mainly inference) may find this useful.
|
|
|
|
// If provided, the meta inference pass uses this function instead of
|
|
|
|
// OP.k.outMeta.
|
|
|
|
struct CustomMetaFunction
|
|
|
|
{
|
|
|
|
static const char *name() { return "CustomMetaFunction"; }
|
|
|
|
using CM = std::function< cv::GMetaArgs( const ade::Graph &,
|
|
|
|
const ade::NodeHandle &,
|
|
|
|
const cv::GMetaArgs &,
|
|
|
|
const cv::GArgs &)>;
|
|
|
|
CM customOutMeta;
|
|
|
|
};
|
|
|
|
|
2018-09-27 02:50:39 +08:00
|
|
|
namespace GModel
|
|
|
|
{
|
|
|
|
using Graph = ade::TypedGraph
|
|
|
|
< NodeType
|
|
|
|
, Input
|
|
|
|
, Output
|
|
|
|
, Op
|
|
|
|
, Data
|
|
|
|
, ConstValue
|
|
|
|
, Island
|
|
|
|
, Protocol
|
|
|
|
, OutputMeta
|
|
|
|
, Journal
|
|
|
|
, ade::passes::TopologicalSortData
|
|
|
|
, DataObjectCounter
|
|
|
|
, IslandModel
|
|
|
|
, ActiveBackends
|
2019-08-05 22:56:34 +08:00
|
|
|
, CustomMetaFunction
|
2018-09-27 02:50:39 +08:00
|
|
|
>;
|
|
|
|
|
|
|
|
// FIXME: How to define it based on GModel???
|
|
|
|
using ConstGraph = ade::ConstTypedGraph
|
|
|
|
< NodeType
|
|
|
|
, Input
|
|
|
|
, Output
|
|
|
|
, Op
|
|
|
|
, Data
|
|
|
|
, ConstValue
|
|
|
|
, Island
|
|
|
|
, Protocol
|
|
|
|
, OutputMeta
|
|
|
|
, Journal
|
|
|
|
, ade::passes::TopologicalSortData
|
|
|
|
, DataObjectCounter
|
|
|
|
, IslandModel
|
|
|
|
, ActiveBackends
|
2019-08-05 22:56:34 +08:00
|
|
|
, CustomMetaFunction
|
2018-09-27 02:50:39 +08:00
|
|
|
>;
|
|
|
|
|
2019-04-18 02:54:47 +08:00
|
|
|
// FIXME:
|
|
|
|
// Export a single class, not a bunch of functions inside a namespace
|
|
|
|
|
2018-09-27 02:50:39 +08:00
|
|
|
// User should initialize graph before using it
|
|
|
|
// GAPI_EXPORTS for tests
|
|
|
|
GAPI_EXPORTS void init (Graph& g);
|
|
|
|
|
2019-04-18 02:54:47 +08:00
|
|
|
GAPI_EXPORTS ade::NodeHandle mkOpNode(Graph &g, const GKernel &k, const std::vector<GArg>& args, const std::string &island);
|
2018-09-27 02:50:39 +08:00
|
|
|
|
2019-04-18 02:54:47 +08:00
|
|
|
GAPI_EXPORTS ade::NodeHandle mkDataNode(Graph &g, const GShape shape);
|
2018-09-27 02:50:39 +08:00
|
|
|
|
|
|
|
// Adds a string message to a node. Any node can be subject of log, messages then
|
|
|
|
// appear in the dumped .dot file.x
|
2019-04-18 02:54:47 +08:00
|
|
|
GAPI_EXPORTS void log(Graph &g, ade::NodeHandle op, std::string &&message, ade::NodeHandle updater = ade::NodeHandle());
|
|
|
|
GAPI_EXPORTS void log(Graph &g, ade::EdgeHandle op, std::string &&message, ade::NodeHandle updater = ade::NodeHandle());
|
2019-08-14 15:03:52 +08:00
|
|
|
// Clears logged messages of a node.
|
|
|
|
GAPI_EXPORTS void log_clear(Graph &g, ade::NodeHandle node);
|
2018-09-27 02:50:39 +08:00
|
|
|
|
2019-04-18 02:54:47 +08:00
|
|
|
GAPI_EXPORTS void linkIn (Graph &g, ade::NodeHandle op, ade::NodeHandle obj, std::size_t in_port);
|
|
|
|
GAPI_EXPORTS void linkOut (Graph &g, ade::NodeHandle op, ade::NodeHandle obj, std::size_t out_port);
|
2018-09-27 02:50:39 +08:00
|
|
|
|
2019-04-18 02:54:47 +08:00
|
|
|
GAPI_EXPORTS void redirectReaders(Graph &g, ade::NodeHandle from, ade::NodeHandle to);
|
|
|
|
GAPI_EXPORTS void redirectWriter (Graph &g, ade::NodeHandle from, ade::NodeHandle to);
|
2018-09-27 02:50:39 +08:00
|
|
|
|
2019-05-23 02:13:13 +08:00
|
|
|
GAPI_EXPORTS std::vector<ade::NodeHandle> orderedInputs (ConstGraph &g, ade::NodeHandle nh);
|
|
|
|
GAPI_EXPORTS std::vector<ade::NodeHandle> orderedOutputs(ConstGraph &g, ade::NodeHandle nh);
|
2018-09-27 02:50:39 +08:00
|
|
|
|
|
|
|
// Returns input meta array for given op node
|
|
|
|
// Array is sparse, as metadata for non-gapi input objects is empty
|
|
|
|
// TODO:
|
|
|
|
// Cover with tests!!
|
2019-04-18 02:54:47 +08:00
|
|
|
GAPI_EXPORTS GMetaArgs collectInputMeta(GModel::ConstGraph cg, ade::NodeHandle node);
|
|
|
|
GAPI_EXPORTS GMetaArgs collectOutputMeta(GModel::ConstGraph cg, ade::NodeHandle node);
|
2018-09-27 02:50:39 +08:00
|
|
|
|
2019-04-18 02:54:47 +08:00
|
|
|
GAPI_EXPORTS ade::EdgeHandle getInEdgeByPort(const GModel::ConstGraph& cg, const ade::NodeHandle& nh, std::size_t in_port);
|
2018-09-27 02:50:39 +08:00
|
|
|
|
|
|
|
// Returns true if the given backend participates in the execution
|
2019-04-18 02:54:47 +08:00
|
|
|
GAPI_EXPORTS bool isActive(const GModel::Graph &cg, const cv::gapi::GBackend &backend);
|
2018-09-27 02:50:39 +08:00
|
|
|
} // namespace GModel
|
|
|
|
|
|
|
|
|
|
|
|
}} // namespace cv::gimpl
|
|
|
|
|
|
|
|
#endif // OPENCV_GAPI_GMODEL_HPP
|