GComputation class represents a captured computation graph. GComputation objects form boundaries for expression code user writes with G-API, allowing to compile and execute it.
More...
GComputation class represents a captured computation graph. GComputation objects form boundaries for expression code user writes with G-API, allowing to compile and execute it.
G-API computations are defined with input/output data objects. G-API will track automatically which operations connect specified outputs to the inputs, forming up a call graph to be executed. The below example expresses calculation of Sobel operator for edge detection ( \(G = \sqrt{G_x^2 + G_y^2}\)):
Input/output data objects on which a call graph should be reconstructed are passed using special wrappers cv::GIn and cv::GOut. G-API will track automatically which operations form a path from inputs to outputs and build the execution graph appropriately.
Note that cv::GComputation doesn't take ownership on data objects it is defined. Moreover, multiple GComputation objects may be defined on the same expressions, e.g. a smaller pipeline which expects that image gradients are already pre-calculated may be defined like this:
The resulting graph would expect two inputs and produce one output. In this case, it doesn't matter if gx/gy data objects are results of cv::gapi::Sobel operators – G-API will stop unrolling expressions and building the underlying graph one reaching this data objects.
The way how GComputation is defined is important as its definition specifies graph protocol – the way how the graph should be used. Protocol is defined by number of inputs, number of outputs, and shapes of inputs and outputs.
In the above example, sobelEdge expects one Mat on input and produces one Mat; while sobelEdgeSub expects two Mats on input and produces one Mat. GComputation's protocol defines how other computation methods should be used – cv::GComputation::compile() and cv::GComputation::apply(). For example, if a graph is defined on two GMat inputs, two cv::Mat objects have to be passed to apply() for execution. GComputation checks protocol correctness in runtime so passing a different number of objects in apply() or passing instead of there would compile well as a C++ source but raise an exception in run-time. G-API also comes with a typed wrapper cv::GComputationT<> which introduces this type-checking in compile-time.
cv::GComputation itself is a thin object which just captures what the graph is. The compiled graph (which actually process data) is represented by class GCompiled. Use compile() method to generate a compiled graph with given compile options. cv::GComputation can also be used to process data with implicit graph compilation on-the-fly, see apply() for details.
GComputation is a reference-counted object – once defined, all its copies will refer to the same instance.
Constructs a new graph with a given protocol, specified as a flow of operations connecting input/output objects. Throws if the passed boundaries are invalid, e.g. if there's no functional dependency (path) between given outputs and inputs.
Defines a computation with arbitrary input/output number.
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
Parameters
ins
vector of inputs GMats for this computation
outs
vector of outputs GMats for this computation
Use this overload for cases when number of computation inputs/outputs is not known in compile-time – e.g. when graph is programmatically generated to build an image pyramid with the given number of levels, etc.
Compile graph on-the-fly and immediately execute it on the inputs data vectors.
Number of input/output data objects must match GComputation's protocol, also types of host data objects (cv::Mat, cv::Scalar) must match the shapes of data objects from protocol (cv::GMat, cv::GScalar). If there's a mismatch, a run-time exception will be generated.
Internally, a cv::GCompiled object is created for the given input format configuration, which then is executed on the input data immediately. cv::GComputation caches compiled objects produced within apply() – if this method would be called next time with the same input parameters (image formats, image resolution, etc), the underlying compiled graph will be reused without recompilation. If new metadata doesn't match the cached one, the underlying compiled graph is regenerated.
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
Takes a variadic parameter pack with metadata descriptors for which a compiled object needs to be produced, followed by GCompileArgs object representing compilation arguments for this process.
Returns
GCompiled, an executable computation compiled specifically for the given input parameters.
Compile the computation for specific input format(s).
This method triggers compilation process and produces a new GCompiled object which then can process data of the given format. Passing data with different format to the compiled computation will generate a run-time exception.
Parameters
in_metas
vector of input metadata configuration. Grab metadata from real data objects (like cv::Mat or cv::Scalar) using cv::descr_of(), or create it on your own.
args
compilation arguments for this compilation process. Compilation arguments directly affect what kind of executable object would be produced, e.g. which kernels (and thus, devices) would be used to execute computation.
Returns
GCompiled, an executable computation compiled specifically for the given input parameters.
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
Takes a variadic parameter pack with metadata descriptors for which a compiled object needs to be produced, followed by GCompileArgs object representing compilation arguments for this process.
Returns
GStreamingCompiled, a streaming-oriented executable computation compiled specifically for the given input parameters.
This method triggers compilation process and produces a new GStreamingCompiled object which then can process video stream data in any format. Underlying mechanisms will be adjusted to every new input video stream automatically, but please note that not all existing backends support this (see reshape()).
Parameters
args
compilation arguments for this compilation process. Compilation arguments directly affect what kind of executable object would be produced, e.g. which kernels (and thus, devices) would be used to execute computation.
Returns
GStreamingCompiled, a streaming-oriented executable computation compiled for any input image format.
This method triggers compilation process and produces a new GStreamingCompiled object which then can process video stream data of the given format. Passing a stream in a different format to the compiled computation will generate a run-time exception.
Parameters
in_metas
vector of input metadata configuration. Grab metadata from real data objects (like cv::Mat or cv::Scalar) using cv::descr_of(), or create it on your own.
args
compilation arguments for this compilation process. Compilation arguments directly affect what kind of executable object would be produced, e.g. which kernels (and thus, devices) would be used to execute computation.
Returns
, a streaming-oriented executable computation compiled specifically for the given input parameters.
This may be useful since all temporary objects (cv::GMats) and namespaces can be localized to scope of lambda, without contaminating the parent scope with probably unnecessary objects and information.
Parameters
gen
generator function which returns a cv::GComputation, see Generator.
<GComputation object>
<GComputation object>
Note
compile() always triggers a compilation process and produces a new GCompiled object regardless if a similar one has been cached via apply() or not.
Parameters
ins
vector of input data to process. Don't create GRunArgs object manually, use cv::gin() wrapper instead.
outs
vector of output data to fill results in. cv::Mat objects may be empty in this vector, G-API will automatically initialize it with the required format & dimensions. Don't create GRunArgsP object manually, use cv::gout() wrapper instead.
args
a list of compilation arguments to pass to the underlying compilation process. Don't create GCompileArgs object manually, use cv::compile_args() wrapper instead.