Ask Your Question

How to use G-API with GPU?

asked 2019-01-02 15:23:22 -0500

flakes gravatar image

updated 2019-01-05 14:23:40 -0500

I've been reading about using the Graph API, and the syntax seems very nice. There's a section in the git documentation where it mentions the capacity to offload work to the GPU:

The idea behind G-API is to declare image processing task in form of expressions and then submit it for execution – using a number of available backends. At the moment, there’s reference “CPU” (OpenCV-based), "GPU" (also OpenCV T-API-based), and experimental “Fluid’ backends available, with other backends coming up next. ...

G-API GPU backend implements the majority of available functions and allows to run OpenCL kernels on available OpenCL-programmable devices. At the moment, GPU backend is based on OpenCV Transparent API; in future versions it will be extended to support integration of arbitrary OpenCL kernels (and likely be renamed to "OpenCL backend").

Right now I've been using the standard transparent API's which accept UMat in order to offload work on the GPU. How can I use the G-API while also offloading the work to the GPU? I see some examples available which describe applying different backends, but I do not see how I can apply them such that I can get the same speeds as using the transparent GPU API's.

edit retag flag offensive close merge delete

1 answer

Sort by » oldest newest most voted

answered 2019-01-03 01:25:01 -0500

berak gravatar image

updated 2019-01-03 01:30:04 -0500

there is a sample here, which shows changing the backend to "fluid", and to use the opencl backend, you'd do similar:

add a few headers:

//! [opencl_includes]
#include "opencv2/gapi/ocl/core.hpp"            // ocl Core kernel library
#include "opencv2/gapi/ocl/imgproc.hpp"         // ocl ImgProc kernel library
//! [opencl_includes]
#include "opencv2/gapi/ocl/goclkernel.hpp"      // ocl user kernel API

change the backend and run the graph:

// Prepare the kernel package and run the graph
cv::gapi::GKernelPackage ocl_kernels = cv::gapi::combine        // Define a custom kernel package:
    (cv::gapi::core::ocl::kernels(),                            // ...with ocl Core kernels
     cv::gapi::imgproc::ocl::kernels(),                         // ...and ocl ImgProc kernels

//! [kernel_pkg]
//! [kernel_hotfix]
// probably not nessecary for ocl !
//ocl_kernels.remove<cv::gapi::imgproc::GBoxFilter>();          // Remove ocl Box filter as unsuitable,
                                                                // G-API will fall-back to OpenCV there.
//! [kernel_hotfix]
//! [kernel_pkg_use]
segm.apply(cv::gin(imgIn),                                      // Input data vector
           cv::gout(imgOut, imgOutCoherency, imgOutOrientation), // Output data vector
           cv::compile_args(ocl_kernels));                      // Kernel package to use
edit flag offensive delete link more


all of it with a grain of salt ;), the example code throws:

  what():  Output meta doesn't coincide with the generated meta
Expected: 32FC1 640x480
Actual  : 32FC1 480x640

(imho, none of the "special" backends are ready for production yet)

berak gravatar imageberak ( 2019-01-03 01:28:58 -0500 )edit

I'm still not quite sure how I could apply this to basic operations like bitwise_x or copyTo. I'm imaginging its somehow by setting the GKernelPackage with cv::gapi::gpu::kernels(), but then the cv::gin and cv::gout stuctures wont accept the transparent UMats so I'm not sure. Any operation I've tried running on it has been running very slowly so I'm fairly certain I'm not hitting the GPU.

flakes gravatar imageflakes ( 2019-01-03 12:54:52 -0500 )edit
Login/Signup to Answer

Question Tools

1 follower


Asked: 2019-01-02 15:23:22 -0500

Seen: 304 times

Last updated: Jan 05