OpenCV 3.0.0 and OpenCL benchmark: Sobel edge detection
I am trying to understand the potential of OpenCL module of OpenCV 3.0.0.
This is related to a previous question of mine, where I understood that there might not exist many answers about this topic just yet. So I decided to ask you guys to help me benchmarking a piece of code.
I wrote an easy to compile simple code snippet that uses a webcam stream and finds Sobel edges and then blurs image some dozens of times. I'm blurring the image multiple times to create a strain on the processor, but feel free to change that part of the code, what is important is the comparison between OpenCL and non-OpenCL versions.
EDIT: Edited code to correct mistake in FPS calculation
CODE:
#include <iostream>
#include <ctime>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/core/ocl.hpp>
using namespace std;
int main()
{
cout << "Have OpenCL?: " << cv::ocl::haveOpenCL() << endl;
/* SWITCH OPENCL ON/OFF IN LINE BELLOW */
cv::ocl::setUseOpenCL(true);
/* */
int nBlurs = 50;
cv::VideoCapture cam;
if (!cam.open(0))
std::cout << "Problem connecting to cam " << std::endl;
else
std::cout << "Successfuly connected to camera " << std::endl;
long frameCounter = 0;
std::time_t timeBegin = std::time(0);
int tick = 0;
cv::UMat frame;
cv::UMat frameGray;
cv::UMat frameSobelx;
cv::UMat frameSobely;
cv::UMat frameSobel;
cv::UMat blurredSobel;
while (1)
{
cam.read(frame);
cv::cvtColor(frame, frameGray, cv::COLOR_BGR2GRAY);
cv::Sobel(frameGray, frameSobelx, frameGray.depth(), 1, 0, 3);
cv::Sobel(frameGray, frameSobely, frameGray.depth(), 0, 1, 3);
cv::bitwise_or(frameSobelx, frameSobely, frameSobel);
for (int n = 0; n < nBlurs; n++)
cv::blur(frameSobel, blurredSobel, cv::Size(3,3));
cv::imshow("Sobel blurred Frame", blurredSobel);
cv::waitKey(1);
frameCounter++;
std::time_t timeNow = std::time(0) - timeBegin;
if (timeNow - tick >= 1)
{
tick++;
cout << "Frames per second: " << frameCounter << endl;
frameCounter = 0;
}
}
return 0;
}
My System
Processor: Intel Core 2 duo 3GHZ
Grahics Card: Nvidia GeForce GT 220
OS: Linux Gentoo
My Results
With OpenCL: 21 FPS, 180% CPU usage and 88% GPU usage
Without OpenCL: 26 FPS, 98% CPU usage and 5% GPU usage
This result is extremely strange.. how come that with OpenCL the GPU is working at 88% capacity and the CPU is at 150%? And, on top of that? It is slower than the non-GPU version?
I'm trying to figure out if this is a hardware problem. Could anyone please compile and make this test?
OPENCL
NO-OPENCL
Best regards!
@Pedro Batista I am facing the same behaviour here with an ati 5850 radeon graphics card, core2quad 2.6Ghz, archlinux OS, Opencv v.3.0.0 master branch from github and mesa/radeon open source drivers with opencl enabled.
Wait, i've just realized that the FPS calculation is wrong. I'll update this question soon
Can you please remove the
cv::UMat
declarations from while loop and declare it outside. Preferably with allocating its size and type before going into loop. This is the only idea I have at this moment. Normally the compiler will do this to optimize its code, but perhabs it doesn't.Edited the question to change the code. Results are the same (also tried declaring UMat as pointers and allocating space from them)
Did you test this code, @matman?
EDIT: Actually results aren't the same... the FPS are still 21, but the CPU usage raises to 180%. This is becoming more and more confusing..
At the moment I have no PC with GPU acceleration. At work I had testet a loop just with add, subtract, multiply and divide for CPU (Core 2 Quad) OpenCL and Cuda (GLX280 (not sure)), where OpenCL and Cuda was about 50 times faster than CPU. I'm interestet in comparing CPU and GPU, but at the moment theres no time for more testing. Perhabs I can test your code tomorrow.
I did a quick test using the pre-build OpenCV 3.0.0 Beta, Windows platform, VS2010+Release mode. Unfortunately, I experience the same issue:
It doesn't seem to be an hardware problem as some other person have the same issue. Maybe it is a problem with the beta version ? Maybe it is a problem related with the application (we should try to filter an Hi-res image ?
I have an integrated graphic chipset (white/red light when the Intel/GPU chipset is used). No matter if I change setUseOpenCL to true or false, I have a red light showing that the GPU is used. I use GPU-Z to see the GPU load and it is in fact the Intel chipset ...(more)
I am really starting to believe that it is a problem with opencv 3.0.0 opencl module, since everyone is getting these strange results in different systems, and you showed in your answer that the same code for opencv 2.4 is being correctly accelerated.
Btw, is indeed strange you only get 10 FPS in your i7 processor.
I looked closer to your results:
Still I don't have a complete answer to explain the issue.
Still strange results for theodore...
@Eduardo when I find some time I will try your example and see how it performs and provide a feedback
@Eduardo Even a low performance video card should outperform the CPU: I never tried OpenCL on OpenCV, but with direct OpenCL programming I got consistently 4x better frame rates on a GeForce 550M with 96 cuda cores than a 2.7GHz Core i7 (quad core) processor.