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-2012, Multicoreware, Inc., all rights reserved.
14 // Copyright (C) 2010-2012, Advanced Micro Devices, Inc., all rights reserved.
15 // Third party copyrights are property of their respective owners.
17 // Redistribution and use in source and binary forms, with or without modification,
18 // are permitted provided that the following conditions are met:
20 // * Redistribution's of source code must retain the above copyright notice,
21 // this list of conditions and the following disclaimer.
23 // * Redistribution's in binary form must reproduce the above copyright notice,
24 // this list of conditions and the following disclaimer in the documentation
25 // and/or other oclMaterials provided with the distribution.
27 // * The name of the copyright holders may not be used to endorse or promote products
28 // derived from this software without specific prior written permission.
30 // This software is provided by the copyright holders and contributors as is and
31 // any express or implied warranties, including, but not limited to, the implied
32 // warranties of merchantability and fitness for a particular purpose are disclaimed.
33 // In no event shall the Intel Corporation or contributors be liable for any direct,
34 // indirect, incidental, special, exemplary, or consequential damages
35 // (including, but not limited to, procurement of substitute goods or services;
36 // loss of use, data, or profits; or business interruption) however caused
37 // and on any theory of liability, whether in contract, strict liability,
38 // or tort (including negligence or otherwise) arising in any way out of
39 // the use of this software, even if advised of the possibility of such damage.
50 #include "opencv2/core/core.hpp"
51 #include "opencv2/imgproc/imgproc.hpp"
52 #include "opencv2/highgui/highgui.hpp"
53 #include "opencv2/calib3d/calib3d.hpp"
54 #include "opencv2/video/video.hpp"
55 #include "opencv2/objdetect/objdetect.hpp"
56 #include "opencv2/features2d/features2d.hpp"
57 #include "opencv2/ocl/ocl.hpp"
58 #include "opencv2/ts/ts.hpp"
59 #include "opencv2/ts/ts_perf.hpp"
60 #include "opencv2/ts/ts_gtest.h"
71 void gen(Mat &mat, int rows, int cols, int type, Scalar low, Scalar high);
72 void gen(Mat &mat, int rows, int cols, int type, int low, int high, int n);
74 string abspath(const string &relpath);
75 int CV_CDECL cvErrorCallback(int, const char *, const char *, const char *, int, void *);
81 COOR do_meanShift(int x0, int y0, uchar *sptr, uchar *dptr, int sstep,
82 cv::Size size, int sp, int sr, int maxIter, float eps, int *tab);
83 void meanShiftProc_(const Mat &src_roi, Mat &dst_roi, Mat &dstCoor_roi,
84 int sp, int sr, cv::TermCriteria crit);
87 template<class T1, class T2>
88 int ExpectedEQ(T1 expected, T2 actual)
90 if(expected == actual)
97 int EeceptDoubleEQ(T1 expected, T1 actual)
99 testing::internal::Double lhs(expected);
100 testing::internal::Double rhs(actual);
102 if (lhs.AlmostEquals(rhs))
111 int AssertEQ(T expected, T actual)
113 if(expected == actual)
120 int ExceptDoubleNear(double val1, double val2, double abs_error);
121 bool match_rect(cv::Rect r1, cv::Rect r2, int threshold);
123 double checkNorm(const cv::Mat &m);
124 double checkNorm(const cv::Mat &m1, const cv::Mat &m2);
125 double checkSimilarity(const cv::Mat &m1, const cv::Mat &m2);
127 int ExpectedMatNear(cv::Mat dst, cv::Mat cpu_dst, double eps);
128 int ExceptedMatSimilar(cv::Mat dst, cv::Mat cpu_dst, double eps);
133 explicit Runnable(const std::string &runname): name_(runname) {}
134 virtual ~Runnable() {}
136 const std::string &name() const
141 virtual void run() = 0;
150 static TestSystem &instance()
152 static TestSystem me;
156 void setWorkingDir(const std::string &val)
160 const std::string &workingDir() const
165 void setTestFilter(const std::string &val)
169 const std::string &testFilter() const
174 void setNumIters(int num_iters)
176 num_iters_ = num_iters;
178 void setGPUWarmupIters(int num_iters)
180 gpu_warmup_iters_ = num_iters;
182 void setCPUIters(int num_iters)
184 cpu_num_iters_ = num_iters;
187 void setTopThreshold(double top)
191 void setBottomThreshold(double bottom)
196 void addInit(Runnable *init)
198 inits_.push_back(init);
200 void addTest(Runnable *test)
202 tests_.push_back(test);
206 // It's public because OpenCV callback uses it
207 void printError(const std::string &msg);
209 std::stringstream &startNewSubtest()
211 finishCurrentSubtest();
212 return cur_subtest_description_;
217 return cur_iter_idx_ >= num_iters_;
220 bool cpu_stop() const
222 return cur_iter_idx_ >= cpu_num_iters_;
225 int get_cur_iter_idx()
227 return cur_iter_idx_;
230 int get_cpu_num_iters()
232 return cpu_num_iters_;
237 return cur_warmup_idx_++ >= gpu_warmup_iters_;
240 void warmupComplete()
247 cpu_started_ = cv::getTickCount();
251 int64 delta = cv::getTickCount() - cpu_started_;
252 cpu_times_.push_back(delta);
257 cpu_elapsed_ += meanTime(cpu_times_);
258 cur_subtest_is_empty_ = false;
264 gpu_started_ = cv::getTickCount();
268 int64 delta = cv::getTickCount() - gpu_started_;
269 gpu_times_.push_back(delta);
274 gpu_elapsed_ += meanTime(gpu_times_);
275 cur_subtest_is_empty_ = false;
281 gpu_full_started_ = cv::getTickCount();
285 int64 delta = cv::getTickCount() - gpu_full_started_;
286 gpu_full_times_.push_back(delta);
289 void gpufullComplete()
291 gpu_full_elapsed_ += meanTime(gpu_full_times_);
292 cur_subtest_is_empty_ = false;
296 bool isListMode() const
298 return is_list_mode_;
300 void setListMode(bool value)
302 is_list_mode_ = value;
305 void setRecordName(const std::string &name)
310 void setCurrentTest(const std::string &name)
313 itname_changed_ = true;
316 void setAccurate(int accurate, double diff)
318 is_accurate_ = accurate;
319 accurate_diff_ = diff;
322 void ExpectMatsNear(vector<Mat>& dst, vector<Mat>& cpu_dst, vector<double>& eps)
324 assert(dst.size() == cpu_dst.size());
325 assert(cpu_dst.size() == eps.size());
327 for(size_t i=0; i<dst.size(); i++)
329 double cur_diff = checkNorm(dst[i], cpu_dst[i]);
330 accurate_diff_ = max(accurate_diff_, cur_diff);
331 if(cur_diff > eps[i])
336 void ExpectedMatNear(cv::Mat& dst, cv::Mat& cpu_dst, double eps)
338 assert(dst.type() == cpu_dst.type());
339 assert(dst.size() == cpu_dst.size());
340 accurate_diff_ = checkNorm(dst, cpu_dst);
341 if(accurate_diff_ <= eps)
347 void ExceptedMatSimilar(cv::Mat& dst, cv::Mat& cpu_dst, double eps)
349 assert(dst.type() == cpu_dst.type());
350 assert(dst.size() == cpu_dst.size());
351 accurate_diff_ = checkSimilarity(cpu_dst, dst);
352 if(accurate_diff_ <= eps)
358 std::stringstream &getCurSubtestDescription()
360 return cur_subtest_description_;
365 cur_subtest_is_empty_(true), cpu_elapsed_(0),
366 gpu_elapsed_(0), gpu_full_elapsed_(0), speedup_total_(0.0),
367 num_subtests_called_(0),
368 speedup_faster_count_(0), speedup_slower_count_(0), speedup_equal_count_(0),
369 speedup_full_faster_count_(0), speedup_full_slower_count_(0), speedup_full_equal_count_(0), is_list_mode_(false),
370 num_iters_(10), cpu_num_iters_(2),
371 gpu_warmup_iters_(1), cur_iter_idx_(0), cur_warmup_idx_(0),
372 record_(0), recordname_("performance"), itname_changed_(true),
373 is_accurate_(-1), accurate_diff_(0.)
375 cpu_times_.reserve(num_iters_);
376 gpu_times_.reserve(num_iters_);
377 gpu_full_times_.reserve(num_iters_);
380 void finishCurrentSubtest();
381 void resetCurrentSubtest()
385 gpu_full_elapsed_ = 0;
386 cur_subtest_description_.str("");
387 cur_subtest_is_empty_ = true;
392 gpu_full_times_.clear();
397 double meanTime(const std::vector<int64> &samples);
401 void printMetrics(int is_accurate, double cpu_time, double gpu_time = 0.0f, double gpu_full_time = 0.0f, double speedup = 0.0f, double fullspeedup = 0.0f);
405 void writeMetrics(double cpu_time, double gpu_time = 0.0f, double gpu_full_time = 0.0f,
406 double speedup = 0.0f, double fullspeedup = 0.0f,
407 double gpu_min = 0.0f, double gpu_max = 0.0f, double std_dev = 0.0f);
409 std::string working_dir_;
410 std::string test_filter_;
412 std::vector<Runnable *> inits_;
413 std::vector<Runnable *> tests_;
415 std::stringstream cur_subtest_description_;
416 bool cur_subtest_is_empty_;
420 int64 gpu_full_started_;
423 double gpu_full_elapsed_;
425 double speedup_total_;
426 double speedup_full_total_;
427 int num_subtests_called_;
429 int speedup_faster_count_;
430 int speedup_slower_count_;
431 int speedup_equal_count_;
433 int speedup_full_faster_count_;
434 int speedup_full_slower_count_;
435 int speedup_full_equal_count_;
443 int cpu_num_iters_; //there's no need to set cpu running same times with gpu
444 int gpu_warmup_iters_; //gpu warm up times, default is 1
446 int cur_warmup_idx_; //current gpu warm up times
447 std::vector<int64> cpu_times_;
448 std::vector<int64> gpu_times_;
449 std::vector<int64> gpu_full_times_;
452 std::string recordname_;
454 bool itname_changed_;
457 double accurate_diff_;
461 #define GLOBAL_INIT(name) \
462 struct name##_init: Runnable { \
463 name##_init(): Runnable(#name) { \
464 TestSystem::instance().addInit(this); \
467 } name##_init_instance; \
468 void name##_init::run()
471 #define PERFTEST(name) \
472 struct name##_test: Runnable { \
473 name##_test(): Runnable(#name) { \
474 TestSystem::instance().addTest(this); \
477 } name##_test_instance; \
478 void name##_test::run()
480 #define SUBTEST TestSystem::instance().startNewSubtest()
483 while (!TestSystem::instance().cpu_stop()) { \
484 TestSystem::instance().cpuOn()
486 TestSystem::instance().cpuOff(); \
487 } TestSystem::instance().cpuComplete()
490 while (!TestSystem::instance().stop()) { \
491 TestSystem::instance().gpuOn()
494 TestSystem::instance().gpuOff(); \
495 } TestSystem::instance().gpuComplete()
497 #define GPU_FULL_ON \
498 while (!TestSystem::instance().stop()) { \
499 TestSystem::instance().gpufullOn()
500 #define GPU_FULL_OFF \
501 TestSystem::instance().gpufullOff(); \
502 } TestSystem::instance().gpufullComplete()
505 while (!TestSystem::instance().warmupStop()) {
508 } TestSystem::instance().warmupComplete()