1
/*M///////////////////////////////////////////////////////////////////////////////////////
3
// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
5
// By downloading, copying, installing or using the software you agree to this license.
6
// If you do not agree to this license, do not download, install,
7
// copy or use the software.
11
// For Open Source Computer Vision Library
13
// Copyright (C) 2010-2013, Advanced Micro Devices, Inc., all rights reserved.
14
// Third party copyrights are property of their respective owners.
16
// Redistribution and use in source and binary forms, with or without modification,
17
// are permitted provided that the following conditions are met:
19
// * Redistribution's of source code must retain the above copyright notice,
20
// this list of conditions and the following disclaimer.
22
// * Redistribution's in binary form must reproduce the above copyright notice,
23
// this list of conditions and the following disclaimer in the documentation
24
// and/or other materials provided with the distribution.
26
// * The name of the copyright holders may not be used to endorse or promote products
27
// derived from this software without specific prior written permission.
29
// This software is provided by the copyright holders and contributors "as is" and
30
// any express or implied warranties, including, but not limited to, the implied
31
// warranties of merchantability and fitness for a particular purpose are disclaimed.
32
// In no event shall the OpenCV Foundation or contributors be liable for any direct,
33
// indirect, incidental, special, exemplary, or consequential damages
34
// (including, but not limited to, procurement of substitute goods or services;
35
// loss of use, data, or profits; or business interruption) however caused
36
// and on any theory of liability, whether in contract, strict liability,
37
// or tort (including negligence or otherwise) arising in any way out of
38
// the use of this software, even if advised of the possibility of such damage.
42
#include "precomp.hpp"
44
#include "opencv2/ts/ocl_test.hpp"
51
int test_loop_times = 1; // TODO Read from command line / environment
55
#define DUMP_PROPERTY_XML(propertyName, propertyValue) \
57
std::stringstream ssName, ssValue;\
58
ssName << propertyName;\
59
ssValue << (propertyValue); \
60
::testing::Test::RecordProperty(ssName.str(), ssValue.str()); \
63
#define DUMP_MESSAGE_STDOUT(msg) \
65
std::cout << msg << std::endl; \
68
static std::string bytesToStringRepr(size_t value)
70
size_t b = value % 1024;
73
size_t kb = value % 1024;
76
size_t mb = value % 1024;
81
std::ostringstream stream;
84
stream << gb << " GB ";
86
stream << mb << " MB ";
88
stream << kb << " kB ";
95
void dumpOpenCLDevice()
97
using namespace cv::ocl;
103
DUMP_MESSAGE_STDOUT("OpenCL is disabled");
104
DUMP_PROPERTY_XML("cv_ocl", "disabled");
108
std::vector<PlatformInfo> platforms;
109
cv::ocl::getPlatfomsInfo(platforms);
110
if (platforms.size() > 0)
112
DUMP_MESSAGE_STDOUT("OpenCL Platforms: ");
113
for (size_t i = 0; i < platforms.size(); i++)
115
const PlatformInfo* platform = &platforms[i];
116
DUMP_MESSAGE_STDOUT(" " << platform->name().c_str());
117
Device current_device;
118
for (int j = 0; j < platform->deviceNumber(); j++)
120
platform->getDevice(current_device, j);
121
const char* deviceTypeStr = current_device.type() == Device::TYPE_CPU
122
? ("CPU") : (current_device.type() == Device::TYPE_GPU ? current_device.hostUnifiedMemory() ? "iGPU" : "dGPU" : "unknown");
123
DUMP_MESSAGE_STDOUT( " " << deviceTypeStr << ": " << current_device.name().c_str() << " (" << current_device.version().c_str() << ")");
124
DUMP_PROPERTY_XML( cv::format("cv_ocl_platform_%d_device_%d", (int)i, (int)j ),
125
cv::format("(Platform=%s)(Type=%s)(Name=%s)(Version=%s)",
126
platform->name().c_str(), deviceTypeStr, current_device.name().c_str(), current_device.version().c_str()) );
132
DUMP_MESSAGE_STDOUT("OpenCL is not available");
133
DUMP_PROPERTY_XML("cv_ocl", "not available");
137
const Device& device = Device::getDefault();
138
if (!device.available())
139
CV_ErrorNoReturn(CV_OpenCLInitError, "OpenCL device is not available");
141
DUMP_MESSAGE_STDOUT("Current OpenCL device: ");
144
DUMP_MESSAGE_STDOUT(" Platform = "<< device.getPlatform().name());
145
DUMP_PROPERTY_XML("cv_ocl_current_platformName", device.getPlatform().name());
148
const char* deviceTypeStr = device.type() == Device::TYPE_CPU
149
? ("CPU") : (device.type() == Device::TYPE_GPU ? device.hostUnifiedMemory() ? "iGPU" : "dGPU" : "unknown");
150
DUMP_MESSAGE_STDOUT(" Type = "<< deviceTypeStr);
151
DUMP_PROPERTY_XML("cv_ocl_current_deviceType", deviceTypeStr);
153
DUMP_MESSAGE_STDOUT(" Name = "<< device.name());
154
DUMP_PROPERTY_XML("cv_ocl_current_deviceName", device.name());
156
DUMP_MESSAGE_STDOUT(" Version = " << device.version());
157
DUMP_PROPERTY_XML("cv_ocl_current_deviceVersion", device.version());
159
DUMP_MESSAGE_STDOUT(" Compute units = "<< device.maxComputeUnits());
160
DUMP_PROPERTY_XML("cv_ocl_current_maxComputeUnits", device.maxComputeUnits());
162
DUMP_MESSAGE_STDOUT(" Max work group size = "<< device.maxWorkGroupSize());
163
DUMP_PROPERTY_XML("cv_ocl_current_maxWorkGroupSize", device.maxWorkGroupSize());
165
std::string localMemorySizeStr = bytesToStringRepr(device.localMemSize());
166
DUMP_MESSAGE_STDOUT(" Local memory size = " << localMemorySizeStr);
167
DUMP_PROPERTY_XML("cv_ocl_current_localMemSize", device.localMemSize());
169
std::string maxMemAllocSizeStr = bytesToStringRepr(device.maxMemAllocSize());
170
DUMP_MESSAGE_STDOUT(" Max memory allocation size = "<< maxMemAllocSizeStr);
171
DUMP_PROPERTY_XML("cv_ocl_current_maxMemAllocSize", device.maxMemAllocSize());
173
const char* doubleSupportStr = device.doubleFPConfig() > 0 ? "Yes" : "No";
174
DUMP_MESSAGE_STDOUT(" Double support = "<< doubleSupportStr);
175
DUMP_PROPERTY_XML("cv_ocl_current_haveDoubleSupport", device.doubleFPConfig() > 0);
177
const char* isUnifiedMemoryStr = device.hostUnifiedMemory() ? "Yes" : "No";
178
DUMP_MESSAGE_STDOUT(" Host unified memory = "<< isUnifiedMemoryStr);
179
DUMP_PROPERTY_XML("cv_ocl_current_hostUnifiedMemory", device.hostUnifiedMemory());
181
const char* haveAmdBlasStr = haveAmdBlas() ? "Yes" : "No";
182
DUMP_MESSAGE_STDOUT(" Has AMD Blas = "<< haveAmdBlasStr);
183
DUMP_PROPERTY_XML("cv_ocl_current_AmdBlas", haveAmdBlas());
185
const char* haveAmdFftStr = haveAmdFft() ? "Yes" : "No";
186
DUMP_MESSAGE_STDOUT(" Has AMD Fft = "<< haveAmdFftStr);
187
DUMP_PROPERTY_XML("cv_ocl_current_AmdFft", haveAmdFft());
190
DUMP_MESSAGE_STDOUT(" Preferred vector width char = "<< device.preferredVectorWidthChar());
191
DUMP_PROPERTY_XML("cv_ocl_current_preferredVectorWidthChar", device.preferredVectorWidthChar());
193
DUMP_MESSAGE_STDOUT(" Preferred vector width short = "<< device.preferredVectorWidthShort());
194
DUMP_PROPERTY_XML("cv_ocl_current_preferredVectorWidthShort", device.preferredVectorWidthShort());
196
DUMP_MESSAGE_STDOUT(" Preferred vector width int = "<< device.preferredVectorWidthInt());
197
DUMP_PROPERTY_XML("cv_ocl_current_preferredVectorWidthInt", device.preferredVectorWidthInt());
199
DUMP_MESSAGE_STDOUT(" Preferred vector width long = "<< device.preferredVectorWidthLong());
200
DUMP_PROPERTY_XML("cv_ocl_current_preferredVectorWidthLong", device.preferredVectorWidthLong());
202
DUMP_MESSAGE_STDOUT(" Preferred vector width float = "<< device.preferredVectorWidthFloat());
203
DUMP_PROPERTY_XML("cv_ocl_current_preferredVectorWidthFloat", device.preferredVectorWidthFloat());
205
DUMP_MESSAGE_STDOUT(" Preferred vector width double = "<< device.preferredVectorWidthDouble());
206
DUMP_PROPERTY_XML("cv_ocl_current_preferredVectorWidthDouble", device.preferredVectorWidthDouble());
210
DUMP_MESSAGE_STDOUT("Exception. Can't dump OpenCL info");
211
DUMP_MESSAGE_STDOUT("OpenCL device not available");
212
DUMP_PROPERTY_XML("cv_ocl", "not available");
215
#undef DUMP_MESSAGE_STDOUT
216
#undef DUMP_PROPERTY_XML
220
Mat TestUtils::readImage(const String &fileName, int flags)
222
return cv::imread(cvtest::TS::ptr()->get_data_path() + fileName, flags);
225
Mat TestUtils::readImageType(const String &fname, int type)
227
Mat src = readImage(fname, CV_MAT_CN(type) == 1 ? cv::IMREAD_GRAYSCALE : cv::IMREAD_COLOR);
228
if (CV_MAT_CN(type) == 4)
231
cv::cvtColor(src, temp, cv::COLOR_BGR2BGRA);
234
src.convertTo(src, CV_MAT_DEPTH(type));
238
double TestUtils::checkNorm1(InputArray m, InputArray mask)
240
return cvtest::norm(m.getMat(), NORM_INF, mask.getMat());
243
double TestUtils::checkNorm2(InputArray m1, InputArray m2, InputArray mask)
245
return cvtest::norm(m1.getMat(), m2.getMat(), NORM_INF, mask.getMat());
248
double TestUtils::checkSimilarity(InputArray m1, InputArray m2)
251
matchTemplate(m1.getMat(), m2.getMat(), diff, CV_TM_CCORR_NORMED);
252
return std::abs(diff.at<float>(0, 0) - 1.f);
255
double TestUtils::checkRectSimilarity(const Size & sz, std::vector<Rect>& ob1, std::vector<Rect>& ob2)
257
double final_test_result = 0.0;
258
size_t sz1 = ob1.size();
259
size_t sz2 = ob2.size();
262
return sz1 > sz2 ? (double)(sz1 - sz2) : (double)(sz2 - sz1);
265
if (sz1 == 0 && sz2 == 0)
267
cv::Mat cpu_result(sz, CV_8UC1);
270
for (vector<Rect>::const_iterator r = ob1.begin(); r != ob1.end(); r++)
272
cv::Mat cpu_result_roi(cpu_result, *r);
273
cpu_result_roi.setTo(1);
274
cpu_result.copyTo(cpu_result);
276
int cpu_area = cv::countNonZero(cpu_result > 0);
278
cv::Mat gpu_result(sz, CV_8UC1);
280
for(vector<Rect>::const_iterator r2 = ob2.begin(); r2 != ob2.end(); r2++)
282
cv::Mat gpu_result_roi(gpu_result, *r2);
283
gpu_result_roi.setTo(1);
284
gpu_result.copyTo(gpu_result);
288
multiply(cpu_result, gpu_result, result_);
289
int result = cv::countNonZero(result_ > 0);
290
if (cpu_area!=0 && result!=0)
291
final_test_result = 1.0 - (double)result/(double)cpu_area;
292
else if(cpu_area==0 && result!=0)
293
final_test_result = -1;
295
return final_test_result;
298
void TestUtils::showDiff(InputArray _src, InputArray _gold, InputArray _actual, double eps, bool alwaysShow)
300
Mat src = _src.getMat(), actual = _actual.getMat(), gold = _gold.getMat();
302
Mat diff, diff_thresh;
303
absdiff(gold, actual, diff);
304
diff.convertTo(diff, CV_32F);
305
threshold(diff, diff_thresh, eps, 255.0, cv::THRESH_BINARY);
307
if (alwaysShow || cv::countNonZero(diff_thresh.reshape(1)) > 0)
310
std::cout << "Source: " << std::endl << src << std::endl;
311
std::cout << "Expected: " << std::endl << gold << std::endl;
312
std::cout << "Actual: " << std::endl << actual << std::endl;
315
namedWindow("src", WINDOW_NORMAL);
316
namedWindow("gold", WINDOW_NORMAL);
317
namedWindow("actual", WINDOW_NORMAL);
318
namedWindow("diff", WINDOW_NORMAL);
321
imshow("gold", gold);
322
imshow("actual", actual);
323
imshow("diff", diff);
329
} } // namespace cvtest::ocl