perf_precomp.hpp 13.7 KB
Newer Older
N
niko 已提交
1 2 3 4 5 6 7 8 9
/*M///////////////////////////////////////////////////////////////////////////////////////
//
//  IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
//
//  By downloading, copying, installing or using the software you agree to this license.
//  If you do not agree to this license, do not download, install,
//  copy or use the software.
//
//
Y
yao 已提交
10
//                           License Agreement
N
niko 已提交
11 12
//                For Open Source Computer Vision Library
//
Y
yao 已提交
13 14
// Copyright (C) 2010-2012, Multicoreware, Inc., all rights reserved.
// Copyright (C) 2010-2012, Advanced Micro Devices, Inc., all rights reserved.
N
niko 已提交
15
// Third party copyrights are property of their respective owners.
Y
yao 已提交
16

N
niko 已提交
17 18 19 20 21 22 23 24
// Redistribution and use in source and binary forms, with or without modification,
// are permitted provided that the following conditions are met:
//
//   * Redistribution's of source code must retain the above copyright notice,
//     this list of conditions and the following disclaimer.
//
//   * Redistribution's in binary form must reproduce the above copyright notice,
//     this list of conditions and the following disclaimer in the documentation
Y
yao 已提交
25
//     and/or other oclMaterials provided with the distribution.
N
niko 已提交
26
//
Y
yao 已提交
27
//   * The name of the copyright holders may not be used to endorse or promote products
N
niko 已提交
28 29
//     derived from this software without specific prior written permission.
//
Y
yao 已提交
30
// This software is provided by the copyright holders and contributors as is and
N
niko 已提交
31 32 33 34 35 36 37 38 39 40 41
// any express or implied warranties, including, but not limited to, the implied
// warranties of merchantability and fitness for a particular purpose are disclaimed.
// In no event shall the Intel Corporation or contributors be liable for any direct,
// indirect, incidental, special, exemplary, or consequential damages
// (including, but not limited to, procurement of substitute goods or services;
// loss of use, data, or profits; or business interruption) however caused
// and on any theory of liability, whether in contract, strict liability,
// or tort (including negligence or otherwise) arising in any way out of
// the use of this software, even if advised of the possibility of such damage.
//
//M*/
A
Andrey Kamaev 已提交
42

43 44 45 46 47 48 49 50 51
#ifdef __GNUC__
#  pragma GCC diagnostic ignored "-Wmissing-declarations"
#  pragma GCC diagnostic ignored "-Wunused-function"
#  if defined __clang__ || defined __APPLE__
#    pragma GCC diagnostic ignored "-Wmissing-prototypes"
#    pragma GCC diagnostic ignored "-Wextra"
#  endif
#endif

52 53 54
#ifndef __OPENCV_PERF_PRECOMP_HPP__
#define __OPENCV_PERF_PRECOMP_HPP__

Y
yao 已提交
55 56
#include <iomanip>
#include <stdexcept>
N
niko 已提交
57
#include <string>
Y
yao 已提交
58 59 60 61
#include <iostream>
#include <cstdio>
#include <vector>
#include <numeric>
62

N
niko 已提交
63 64
#include "opencv2/core/core.hpp"
#include "opencv2/imgproc/imgproc.hpp"
Y
yao 已提交
65
#include "opencv2/highgui/highgui.hpp"
Y
yao 已提交
66
#include "opencv2/calib3d/calib3d.hpp"
N
niko 已提交
67
#include "opencv2/video/video.hpp"
Y
yao 已提交
68 69
#include "opencv2/objdetect/objdetect.hpp"
#include "opencv2/features2d/features2d.hpp"
N
niko 已提交
70
#include "opencv2/ocl/ocl.hpp"
Y
yao 已提交
71 72
#include "opencv2/ts/ts.hpp"

73 74 75 76 77
#define OCL_SIZE_1000 cv::Size(1000, 1000)
#define OCL_SIZE_2000 cv::Size(2000, 2000)
#define OCL_SIZE_4000 cv::Size(4000, 4000)

#define OCL_TYPICAL_MAT_SIZES ::testing::Values(OCL_SIZE_1000, OCL_SIZE_2000, OCL_SIZE_4000)
N
niko 已提交
78

Y
yao 已提交
79 80 81 82 83 84 85 86 87
#define Min_Size 1000
#define Max_Size 4000
#define Multiple 2
#define TAB "    "

using namespace std;
using namespace cv;

void gen(Mat &mat, int rows, int cols, int type, Scalar low, Scalar high);
Y
yao 已提交
88 89
void gen(Mat &mat, int rows, int cols, int type, int low, int high, int n);

Y
yao 已提交
90 91
string abspath(const string &relpath);
int CV_CDECL cvErrorCallback(int, const char *, const char *, const char *, int, void *);
92 93 94 95 96 97 98 99 100
//typedef struct
//{
//    short x;
//    short y;
//} COOR;
//COOR do_meanShift(int x0, int y0, uchar *sptr, uchar *dptr, int sstep,
//                  cv::Size size, int sp, int sr, int maxIter, float eps, int *tab);
//void meanShiftProc_(const Mat &src_roi, Mat &dst_roi, Mat &dstCoor_roi,
//                    int sp, int sr, cv::TermCriteria crit);
Y
yao 已提交
101

Y
yao 已提交
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117

template<class T1, class T2>
int ExpectedEQ(T1 expected, T2 actual)
{
    if(expected == actual)
        return 1;

    return 0;
}

template<class T1>
int EeceptDoubleEQ(T1 expected, T1 actual)
{
    testing::internal::Double lhs(expected);
    testing::internal::Double rhs(actual);

118
    if (lhs.AlmostEquals(rhs))
Y
yao 已提交
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
    {
        return 1;
    }

    return 0;
}

template<class T>
int AssertEQ(T expected, T actual)
{
    if(expected == actual)
    {
        return 1;
    }
    return 0;
}

int ExceptDoubleNear(double val1, double val2, double abs_error);
bool match_rect(cv::Rect r1, cv::Rect r2, int threshold);

double checkNorm(const cv::Mat &m);
double checkNorm(const cv::Mat &m1, const cv::Mat &m2);
double checkSimilarity(const cv::Mat &m1, const cv::Mat &m2);

int ExpectedMatNear(cv::Mat dst, cv::Mat cpu_dst, double eps);
int ExceptedMatSimilar(cv::Mat dst, cv::Mat cpu_dst, double eps);

Y
yao 已提交
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
class Runnable
{
public:
    explicit Runnable(const std::string &runname): name_(runname) {}
    virtual ~Runnable() {}

    const std::string &name() const
    {
        return name_;
    }

    virtual void run() = 0;

private:
    std::string name_;
};

class TestSystem
{
public:
    static TestSystem &instance()
    {
        static TestSystem me;
        return me;
    }

    void setWorkingDir(const std::string &val)
    {
        working_dir_ = val;
    }
    const std::string &workingDir() const
    {
        return working_dir_;
    }

    void setTestFilter(const std::string &val)
    {
        test_filter_ = val;
    }
    const std::string &testFilter() const
    {
        return test_filter_;
    }

    void setNumIters(int num_iters)
    {
        num_iters_ = num_iters;
    }
    void setGPUWarmupIters(int num_iters)
    {
        gpu_warmup_iters_ = num_iters;
    }
    void setCPUIters(int num_iters)
    {
        cpu_num_iters_ = num_iters;
    }

    void setTopThreshold(double top)
    {
        top_ = top;
    }
    void setBottomThreshold(double bottom)
    {
        bottom_ = bottom;
    }

    void addInit(Runnable *init)
    {
        inits_.push_back(init);
    }
    void addTest(Runnable *test)
    {
        tests_.push_back(test);
    }
    void run();

    // It's public because OpenCV callback uses it
    void printError(const std::string &msg);

    std::stringstream &startNewSubtest()
    {
        finishCurrentSubtest();
        return cur_subtest_description_;
    }

    bool stop() const
    {
        return cur_iter_idx_ >= num_iters_;
    }

    bool cpu_stop() const
    {
        return cur_iter_idx_ >= cpu_num_iters_;
    }

Y
yao 已提交
241 242 243 244 245 246 247 248 249 250
    int get_cur_iter_idx()
    {
        return cur_iter_idx_;
    }

    int get_cpu_num_iters()
    {
        return cpu_num_iters_;
    }

Y
yao 已提交
251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331
    bool warmupStop()
    {
        return cur_warmup_idx_++ >= gpu_warmup_iters_;
    }

    void warmupComplete()
    {
        cur_warmup_idx_ = 0;
    }

    void cpuOn()
    {
        cpu_started_ = cv::getTickCount();
    }
    void cpuOff()
    {
        int64 delta = cv::getTickCount() - cpu_started_;
        cpu_times_.push_back(delta);
        ++cur_iter_idx_;
    }
    void cpuComplete()
    {
        cpu_elapsed_ += meanTime(cpu_times_);
        cur_subtest_is_empty_ = false;
        cur_iter_idx_ = 0;
    }

    void gpuOn()
    {
        gpu_started_ = cv::getTickCount();
    }
    void gpuOff()
    {
        int64 delta = cv::getTickCount() - gpu_started_;
        gpu_times_.push_back(delta);
        ++cur_iter_idx_;
    }
    void gpuComplete()
    {
        gpu_elapsed_ += meanTime(gpu_times_);
        cur_subtest_is_empty_ = false;
        cur_iter_idx_ = 0;
    }

    void gpufullOn()
    {
        gpu_full_started_ = cv::getTickCount();
    }
    void gpufullOff()
    {
        int64 delta = cv::getTickCount() - gpu_full_started_;
        gpu_full_times_.push_back(delta);
        ++cur_iter_idx_;
    }
    void gpufullComplete()
    {
        gpu_full_elapsed_ += meanTime(gpu_full_times_);
        cur_subtest_is_empty_ = false;
        cur_iter_idx_ = 0;
    }

    bool isListMode() const
    {
        return is_list_mode_;
    }
    void setListMode(bool value)
    {
        is_list_mode_ = value;
    }

    void setRecordName(const std::string &name)
    {
        recordname_ = name;
    }

    void setCurrentTest(const std::string &name)
    {
        itname_ = name;
        itname_changed_ = true;
    }

Y
yao 已提交
332
    void setAccurate(int accurate, double diff)
Y
yao 已提交
333
    {
Y
yao 已提交
334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370
        is_accurate_ = accurate;
        accurate_diff_ = diff;
    }

    void ExpectMatsNear(vector<Mat>& dst, vector<Mat>& cpu_dst, vector<double>& eps)
    {
        assert(dst.size() == cpu_dst.size());
        assert(cpu_dst.size() == eps.size());
        is_accurate_ = 1;
        for(size_t i=0; i<dst.size(); i++)
        {
            double cur_diff = checkNorm(dst[i], cpu_dst[i]);
            accurate_diff_ = max(accurate_diff_, cur_diff);
            if(cur_diff > eps[i])
                is_accurate_ = 0;
        }
    }

    void ExpectedMatNear(cv::Mat& dst, cv::Mat& cpu_dst, double eps)
    {
        assert(dst.type() == cpu_dst.type());
        assert(dst.size() == cpu_dst.size());
        accurate_diff_ = checkNorm(dst, cpu_dst);
        if(accurate_diff_ <= eps)
            is_accurate_ = 1;
        else
            is_accurate_ = 0;
    }

    void ExceptedMatSimilar(cv::Mat& dst, cv::Mat& cpu_dst, double eps)
    {
        assert(dst.type() == cpu_dst.type());
        assert(dst.size() == cpu_dst.size());
        accurate_diff_ = checkSimilarity(cpu_dst, dst);
        if(accurate_diff_ <= eps)
            is_accurate_ = 1;
        else
371
            is_accurate_ = 0;
Y
yao 已提交
372 373 374 375 376 377 378
    }

    std::stringstream &getCurSubtestDescription()
    {
        return cur_subtest_description_;
    }

Y
yao 已提交
379 380 381 382 383 384 385 386 387
private:
    TestSystem():
        cur_subtest_is_empty_(true), cpu_elapsed_(0),
        gpu_elapsed_(0), gpu_full_elapsed_(0), speedup_total_(0.0),
        num_subtests_called_(0),
        speedup_faster_count_(0), speedup_slower_count_(0), speedup_equal_count_(0),
        speedup_full_faster_count_(0), speedup_full_slower_count_(0), speedup_full_equal_count_(0), is_list_mode_(false),
        num_iters_(10), cpu_num_iters_(2),
        gpu_warmup_iters_(1), cur_iter_idx_(0), cur_warmup_idx_(0),
388
        record_(0), recordname_("performance"), itname_changed_(true),
Y
yao 已提交
389
        is_accurate_(-1), accurate_diff_(0.)
Y
yao 已提交
390 391 392 393 394 395 396 397 398 399 400 401 402 403 404
    {
        cpu_times_.reserve(num_iters_);
        gpu_times_.reserve(num_iters_);
        gpu_full_times_.reserve(num_iters_);
    }

    void finishCurrentSubtest();
    void resetCurrentSubtest()
    {
        cpu_elapsed_ = 0;
        gpu_elapsed_ = 0;
        gpu_full_elapsed_ = 0;
        cur_subtest_description_.str("");
        cur_subtest_is_empty_ = true;
        cur_iter_idx_ = 0;
Y
yao 已提交
405
        cur_warmup_idx_ = 0;
Y
yao 已提交
406 407 408
        cpu_times_.clear();
        gpu_times_.clear();
        gpu_full_times_.clear();
Y
yao 已提交
409
        is_accurate_ = -1;
Y
yao 已提交
410
        accurate_diff_ = 0.;
Y
yao 已提交
411 412 413 414 415 416
    }

    double meanTime(const std::vector<int64> &samples);

    void printHeading();
    void printSummary();
Y
yao 已提交
417
    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);
Y
yao 已提交
418 419 420

    void writeHeading();
    void writeSummary();
Y
yao 已提交
421
    void writeMetrics(double cpu_time, double gpu_time = 0.0f, double gpu_full_time = 0.0f,
Y
yao 已提交
422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470
                      double speedup = 0.0f, double fullspeedup = 0.0f,
                      double gpu_min = 0.0f, double gpu_max = 0.0f, double std_dev = 0.0f);

    std::string working_dir_;
    std::string test_filter_;

    std::vector<Runnable *> inits_;
    std::vector<Runnable *> tests_;

    std::stringstream cur_subtest_description_;
    bool cur_subtest_is_empty_;

    int64 cpu_started_;
    int64 gpu_started_;
    int64 gpu_full_started_;
    double cpu_elapsed_;
    double gpu_elapsed_;
    double gpu_full_elapsed_;

    double speedup_total_;
    double speedup_full_total_;
    int num_subtests_called_;

    int speedup_faster_count_;
    int speedup_slower_count_;
    int speedup_equal_count_;

    int speedup_full_faster_count_;
    int speedup_full_slower_count_;
    int speedup_full_equal_count_;

    bool is_list_mode_;

    double top_;
    double bottom_;

    int num_iters_;
    int cpu_num_iters_;		//there's no need to set cpu running same times with gpu
    int gpu_warmup_iters_;	//gpu warm up times, default is 1
    int cur_iter_idx_;
    int cur_warmup_idx_;	//current gpu warm up times
    std::vector<int64> cpu_times_;
    std::vector<int64> gpu_times_;
    std::vector<int64> gpu_full_times_;

    FILE *record_;
    std::string recordname_;
    std::string itname_;
    bool itname_changed_;
Y
yao 已提交
471 472

    int is_accurate_;
Y
yao 已提交
473
    double accurate_diff_;
Y
yao 已提交
474 475 476 477 478
};


#define GLOBAL_INIT(name) \
struct name##_init: Runnable { \
R
Roman Donchenko 已提交
479 480
    name##_init(): Runnable(#name) { \
    TestSystem::instance().addInit(this); \
Y
yao 已提交
481
} \
R
Roman Donchenko 已提交
482
    void run(); \
Y
yao 已提交
483
} name##_init_instance; \
R
Roman Donchenko 已提交
484
    void name##_init::run()
Y
yao 已提交
485 486


Y
yao 已提交
487
#define PERFTEST(name) \
Y
yao 已提交
488
struct name##_test: Runnable { \
R
Roman Donchenko 已提交
489 490
    name##_test(): Runnable(#name) { \
    TestSystem::instance().addTest(this); \
Y
yao 已提交
491
} \
R
Roman Donchenko 已提交
492
    void run(); \
Y
yao 已提交
493
} name##_test_instance; \
R
Roman Donchenko 已提交
494
    void name##_test::run()
Y
yao 已提交
495 496 497 498

#define SUBTEST TestSystem::instance().startNewSubtest()

#define CPU_ON \
R
Roman Donchenko 已提交
499 500
    while (!TestSystem::instance().cpu_stop()) { \
    TestSystem::instance().cpuOn()
Y
yao 已提交
501
#define CPU_OFF \
R
Roman Donchenko 已提交
502 503
    TestSystem::instance().cpuOff(); \
    } TestSystem::instance().cpuComplete()
Y
yao 已提交
504 505

#define GPU_ON \
R
Roman Donchenko 已提交
506 507
    while (!TestSystem::instance().stop()) { \
    TestSystem::instance().gpuOn()
Y
yao 已提交
508
#define GPU_OFF \
R
Roman Donchenko 已提交
509 510 511
    ocl::finish();\
    TestSystem::instance().gpuOff(); \
    } TestSystem::instance().gpuComplete()
N
niko 已提交
512

Y
yao 已提交
513
#define GPU_FULL_ON \
R
Roman Donchenko 已提交
514 515
    while (!TestSystem::instance().stop()) { \
    TestSystem::instance().gpufullOn()
Y
yao 已提交
516
#define GPU_FULL_OFF \
R
Roman Donchenko 已提交
517 518
    TestSystem::instance().gpufullOff(); \
    } TestSystem::instance().gpufullComplete()
N
niko 已提交
519

Y
yao 已提交
520
#define WARMUP_ON \
R
Roman Donchenko 已提交
521
    while (!TestSystem::instance().warmupStop()) {
Y
yao 已提交
522
#define WARMUP_OFF \
R
Roman Donchenko 已提交
523 524
    ocl::finish();\
    } TestSystem::instance().warmupComplete()
525 526

#endif