Last active
December 30, 2016 22:49
-
-
Save LaurentBerger/b062849f7355c1c38c0bffe22d08ba9b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <opencv2/imgproc/imgproc.hpp> | |
#include <opencv2/highgui/highgui.hpp> | |
#include <opencv2/core/core.hpp> | |
#include <opencv2/core/ocl.hpp> | |
#include <iostream> | |
using namespace cv; | |
using namespace std; | |
string imagePath="F:/lib/opencv/samples/data/aloeL.jpg"; | |
int TestOpenCL(int nbTest); | |
int main(int argc, char **argv) | |
{ | |
if (!cv::ocl::haveOpenCL()) | |
{ | |
cout << "OpenCL is not avaiable..." << endl; | |
return 0; | |
} | |
cv::ocl::Context context; | |
std::vector<cv::ocl::PlatformInfo> platforms; | |
cv::ocl::getPlatfomsInfo(platforms); | |
//OpenCL Platforms | |
for (size_t i = 0; i < platforms.size(); i++) | |
{ | |
//Access to Platform | |
const cv::ocl::PlatformInfo* platform = &platforms[i]; | |
//Platform Name | |
std::cout << "Platform Name: " << platform->name().c_str() << "\n"; | |
//Access Device within Platform | |
cv::ocl::Device current_device; | |
for (int j = 0; j < platform->deviceNumber(); j++) | |
{ | |
//Access Device | |
platform->getDevice(current_device, j); | |
//Device Type | |
int deviceType = current_device.type(); | |
switch (deviceType) { | |
case (1 << 1): | |
cout<<" CPU device\n"; | |
break; | |
case (1 << 2) : | |
cout << " GPU device\n"; | |
if (context.create(deviceType)) | |
TestOpenCL(50); | |
break; | |
} | |
cout << context.ndevices() << " GPU devices are detected." << std::endl; | |
cout<< deviceType <<"\n"; | |
} | |
} | |
return 0; | |
} | |
int TestOpenCL(int nbTest) | |
{ | |
cout << "Without opencl umat \t with opencl umat\t with opencl mat \t without opencl mat for cvtColor(0),Blur(1),Canny(2)\n"; | |
cout << "getNumberOfCPUs =" << getNumberOfCPUs() << "\t getNumThreads = " << getNumThreads() << "\n"; | |
Mat image = imread(imagePath.c_str(), CV_LOAD_IMAGE_UNCHANGED); | |
double totalTime = 0; | |
UMat uimage; | |
imread(imagePath.c_str(), CV_LOAD_IMAGE_UNCHANGED).copyTo(uimage); | |
vector<double> tps; | |
TickMeter myChrono; | |
double m,v; | |
// ********************** CVTCOLOR ************ | |
{ | |
// 1 UMAT ocl false | |
ocl::setUseOpenCL(false); | |
cout << "cvtColor = "; | |
tps.clear(); | |
tps.resize(nbTest); | |
UMat ugray; | |
for (int nTest = 0; nTest<nbTest; nTest++) | |
{ | |
myChrono.reset(); | |
myChrono.start(); | |
cvtColor(uimage, ugray, COLOR_BGR2GRAY); | |
myChrono.stop(); | |
tps[nTest] = myChrono.getTimeMilli(); | |
} | |
m=0;v=0; | |
for (int nTest = 0; nTest<nbTest; nTest++) | |
m+= tps[nTest]; | |
m /= nbTest; | |
for (int nTest = 0; nTest<nbTest; nTest++) | |
v += pow(m-tps[nTest],2.0); | |
v /= nbTest; | |
cout<<"("<<m<<" +/-"<<sqrt(v)<<")"; | |
if (!cv::ocl::useOpenCL()) | |
cout << "false\t\t" ; | |
else | |
cout << "true\t\t"; | |
} | |
{ | |
// 2 UMAT ocl true | |
ocl::setUseOpenCL(true); | |
tps.clear(); | |
tps.resize(nbTest); | |
UMat ugray; | |
for (int nTest = 0; nTest<nbTest; nTest++) | |
{ | |
myChrono.reset(); | |
myChrono.start(); | |
cvtColor(uimage, ugray, COLOR_BGR2GRAY); | |
myChrono.stop(); | |
tps[nTest]=myChrono.getTimeMilli(); | |
} | |
m = 0; v = 0; | |
for (int nTest = 0; nTest<nbTest; nTest++) | |
m += tps[nTest]; | |
m /= nbTest; | |
for (int nTest = 0; nTest<nbTest; nTest++) | |
v += pow(m - tps[nTest], 2.0); | |
v /= nbTest; | |
cout << "(" << m << " +/-" << sqrt(v) << ")"; | |
if (!cv::ocl::useOpenCL()) | |
cout << "false\t\t"; | |
else | |
cout << "true\t\t"; | |
} | |
// 3 MAT ocl false | |
{ | |
ocl::setUseOpenCL(false); | |
tps.clear(); | |
tps.resize(nbTest); | |
Mat gray; | |
for (int nTest = 0; nTest<nbTest; nTest++) | |
{ | |
myChrono.reset(); | |
myChrono.start(); | |
cvtColor(image, gray, COLOR_BGR2GRAY); | |
myChrono.stop(); | |
tps[nTest] = myChrono.getTimeMilli(); | |
} | |
m = 0; v = 0; | |
for (int nTest = 0; nTest<nbTest; nTest++) | |
m += tps[nTest]; | |
m /= nbTest; | |
for (int nTest = 0; nTest<nbTest; nTest++) | |
v += pow(m - tps[nTest], 2.0); | |
v /= nbTest; | |
cout << "(" << m << " +/-" << sqrt(v) << ")"; | |
if (!cv::ocl::useOpenCL()) | |
cout << "false\t\t"; | |
else | |
cout << "true\t\t"; | |
} | |
{ | |
// 4 MAT ocl true | |
ocl::setUseOpenCL(true); | |
tps.clear(); | |
tps.resize(nbTest); | |
Mat gray; | |
for (int nTest = 0; nTest<nbTest; nTest++) | |
{ | |
myChrono.reset(); | |
myChrono.start(); | |
cvtColor(image, gray, COLOR_BGR2GRAY); | |
myChrono.stop(); | |
tps[nTest] = myChrono.getTimeMilli(); | |
} | |
m = 0; v = 0; | |
for (int nTest = 0; nTest<nbTest; nTest++) | |
m += tps[nTest]; | |
m /= nbTest; | |
for (int nTest = 0; nTest<nbTest; nTest++) | |
v += pow(m - tps[nTest], 2.0); | |
v /= nbTest; | |
cout << "(" << m << " +/-" << sqrt(v) << ""; | |
if (!cv::ocl::useOpenCL()) | |
cout << "false\t\t"; | |
else | |
cout << "true\t\t"; | |
cout<<"\n"; | |
} | |
Size tailleBlur(101, 101); | |
// ********************** GAUSSIANBLUR ************ | |
{ | |
// 1 UMAT ocl false | |
cout << "gaussianblur = "; | |
ocl::setUseOpenCL(false); | |
tps.clear(); | |
tps.resize(nbTest); | |
UMat ugray, ugrayres; | |
cvtColor(uimage, ugray, COLOR_BGR2GRAY); | |
for (int nTest = 0; nTest<nbTest; nTest++) | |
{ | |
myChrono.reset(); | |
myChrono.start(); | |
GaussianBlur(ugray, ugrayres, tailleBlur, 1.5); | |
myChrono.stop(); | |
tps[nTest] = myChrono.getTimeMilli(); | |
} | |
m = 0; v = 0; | |
for (int nTest = 0; nTest<nbTest; nTest++) | |
m += tps[nTest]; | |
m /= nbTest; | |
for (int nTest = 0; nTest<nbTest; nTest++) | |
v += pow(m - tps[nTest], 2.0); | |
v /= nbTest; | |
cout << "(" << m << " +/-" << sqrt(v) << ")"; | |
if (!cv::ocl::useOpenCL()) | |
cout << "false\t\t"; | |
else | |
cout << "true\t\t"; | |
} | |
{ | |
// 2 UMAT ocl true | |
ocl::setUseOpenCL(true); | |
tps.clear(); | |
tps.resize(nbTest); | |
UMat ugray, ugrayres; | |
cvtColor(uimage, ugray, COLOR_BGR2GRAY); | |
for (int nTest = 0; nTest<nbTest; nTest++) | |
{ | |
myChrono.reset(); | |
myChrono.start(); | |
GaussianBlur(ugray, ugrayres, tailleBlur, 1.5); | |
myChrono.stop(); | |
tps[nTest] = myChrono.getTimeMilli(); | |
} | |
m = 0; v = 0; | |
for (int nTest = 0; nTest<nbTest; nTest++) | |
m += tps[nTest]; | |
m /= nbTest; | |
for (int nTest = 0; nTest<nbTest; nTest++) | |
v += pow(m - tps[nTest], 2.0); | |
v /= nbTest; | |
cout << "(" << m << " +/-" << sqrt(v) << ")"; | |
if (!cv::ocl::useOpenCL()) | |
cout << "false\t\t"; | |
else | |
cout << "true\t\t"; | |
} | |
{ | |
// 3 MAT ocl false | |
ocl::setUseOpenCL(false); | |
tps.clear(); | |
tps.resize(nbTest); | |
Mat gray,grayres; | |
cvtColor(image, gray, COLOR_BGR2GRAY); | |
for (int nTest = 0; nTest<nbTest; nTest++) | |
{ | |
myChrono.reset(); | |
myChrono.start(); | |
GaussianBlur(gray, grayres, tailleBlur, 1.5); | |
myChrono.stop(); | |
tps[nTest] = myChrono.getTimeMilli(); | |
} | |
m = 0; v = 0; | |
for (int nTest = 0; nTest<nbTest; nTest++) | |
m += tps[nTest]; | |
m /= nbTest; | |
for (int nTest = 0; nTest<nbTest; nTest++) | |
v += pow(m - tps[nTest], 2.0); | |
v /= nbTest; | |
cout << "(" << m << " +/-" << sqrt(v) << ")"; | |
if (!cv::ocl::useOpenCL()) | |
cout << "false\t\t"; | |
else | |
cout << "true\t\t"; | |
} | |
{ | |
// 4 MAT ocl true | |
ocl::setUseOpenCL(true); | |
tps.clear(); | |
tps.resize(nbTest); | |
Mat gray, grayres; | |
cvtColor(image, gray, COLOR_BGR2GRAY); | |
for (int nTest = 0; nTest<nbTest; nTest++) | |
{ | |
myChrono.reset(); | |
myChrono.start(); | |
GaussianBlur(gray, grayres, tailleBlur, 1.5); | |
myChrono.stop(); | |
tps[nTest] = myChrono.getTimeMilli(); | |
} | |
m = 0; v = 0; | |
for (int nTest = 0; nTest<nbTest; nTest++) | |
m += tps[nTest]; | |
m /= nbTest; | |
for (int nTest = 0; nTest<nbTest; nTest++) | |
v += pow(m - tps[nTest], 2.0); | |
v /= nbTest; | |
cout << "(" << m << " +/-" << sqrt(v) << ")"; | |
if (!cv::ocl::useOpenCL()) | |
cout << "false\t\t"; | |
else | |
cout << "true\t\t"; | |
cout<< "\n"; | |
} | |
// ********************** CANNY ************ | |
// 1 UMAT ocl false | |
cout << "Canny = "; | |
{ | |
ocl::setUseOpenCL(false); | |
tps.clear(); | |
tps.resize(nbTest); | |
UMat ugray, ugrayres; | |
cvtColor(uimage, ugray, COLOR_BGR2GRAY); | |
for (int nTest = 0; nTest<nbTest; nTest++) | |
{ | |
myChrono.reset(); | |
myChrono.start(); | |
Canny(ugray, ugrayres, 0, 50); | |
myChrono.stop(); | |
tps[nTest] = myChrono.getTimeMilli(); | |
} | |
m = 0; v = 0; | |
for (int nTest = 0; nTest<nbTest; nTest++) | |
m += tps[nTest]; | |
m /= nbTest; | |
for (int nTest = 0; nTest<nbTest; nTest++) | |
v += pow(m - tps[nTest], 2.0); | |
v /= nbTest; | |
cout << "(" << m << " +/-" << sqrt(v) << ")"; | |
if (!cv::ocl::useOpenCL()) | |
cout << "false\t\t"; | |
else | |
cout << "true\t\t"; | |
} | |
// 2 UMAT ocl true | |
{ | |
ocl::setUseOpenCL(true); | |
tps.clear(); | |
tps.resize(nbTest); | |
UMat ugray,ugrayres; | |
cvtColor(uimage, ugray, COLOR_BGR2GRAY); | |
for (int nTest = 0; nTest<nbTest; nTest++) | |
{ | |
myChrono.reset(); | |
myChrono.start(); | |
Canny(ugray, ugrayres, 0, 50); | |
myChrono.stop(); | |
tps[nTest] = myChrono.getTimeMilli(); | |
} | |
m = 0; v = 0; | |
for (int nTest = 0; nTest<nbTest; nTest++) | |
m += tps[nTest]; | |
m /= nbTest; | |
for (int nTest = 0; nTest<nbTest; nTest++) | |
v += pow(m - tps[nTest], 2.0); | |
v /= nbTest; | |
cout << "(" << m << " +/-" << sqrt(v) << ")"; | |
if (!cv::ocl::useOpenCL()) | |
cout << "false\t\t"; | |
else | |
cout << "true\t\t"; | |
} | |
// 3 MAT ocl false | |
{ | |
ocl::setUseOpenCL(false); | |
tps.clear(); | |
tps.resize(nbTest); | |
Mat gray,grayres; | |
cvtColor(image, gray, COLOR_BGR2GRAY); | |
for (int nTest = 0; nTest<nbTest; nTest++) | |
{ | |
myChrono.reset(); | |
myChrono.start(); | |
Canny(gray, grayres, 0, 50); | |
myChrono.stop(); | |
tps[nTest] = myChrono.getTimeMilli(); | |
} | |
m = 0; v = 0; | |
for (int nTest = 0; nTest<nbTest; nTest++) | |
m += tps[nTest]; | |
m /= nbTest; | |
for (int nTest = 0; nTest<nbTest; nTest++) | |
v += pow(m - tps[nTest], 2.0); | |
v /= nbTest; | |
cout << "(" << m << " +/-" << sqrt(v) << ")"; | |
if (!cv::ocl::useOpenCL()) | |
cout << "false\t\t"; | |
else | |
cout << "true\t\t"; | |
} | |
// 4 MAT ocl true | |
{ | |
ocl::setUseOpenCL(true); | |
tps.clear(); | |
tps.resize(nbTest); | |
Mat gray,grayres; | |
cvtColor(image, gray, COLOR_BGR2GRAY); | |
for (int nTest = 0; nTest<nbTest; nTest++) | |
{ | |
myChrono.reset(); | |
myChrono.start(); | |
Canny(gray, grayres, 0, 50); | |
myChrono.stop(); | |
tps[nTest] = myChrono.getTimeMilli(); | |
} | |
m = 0; v = 0; | |
for (int nTest = 0; nTest<nbTest; nTest++) | |
m += tps[nTest]; | |
m /= nbTest; | |
for (int nTest = 0; nTest<nbTest; nTest++) | |
v += pow(m - tps[nTest], 2.0); | |
v /= nbTest; | |
cout << "(" << m << " +/-" << sqrt(v) << ")"; | |
if (!cv::ocl::useOpenCL()) | |
cout << "false\t\t"; | |
else | |
cout << "true\t\t"; | |
cout << "\n"; | |
} | |
return 0; | |
} |
About variance it is a problem of bias or unbias estimator.... statiscal problem
program was wrong.
Now results are good :
Platform Name: NVIDIA CUDA
GPU device
Without opencl umat with opencl umat with opencl mat without opencl mat for cvtColor(0),Blur(1),Canny(2)
getNumberOfCPUs =12 getNumThreads = 13
cvtColor = (2.47917 +/-0.407063)false (0.0982429 +/-0.536083)true (0.410241 +/-0.0927372)false (0.334377 +/-0.0391464true
gaussianblur = (49.8595 +/-3.78194)false (5.49987 +/-6.54388)true (53.6972 +/-9.15954)false (51.5041 +/-5.4991)true
Canny = (19.0872 +/-4.87168)false (5.34425 +/-7.29371)true (18.9727 +/-3.15754)false (18.1416 +/-0.139933)true
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
all lines where you evaluate stdev should be changed to follow:
v = sqrt( v / (nbTest - 1) );