File: perf_math.cpp

package info (click to toggle)
opencv 4.10.0%2Bdfsg-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 282,092 kB
  • sloc: cpp: 1,178,079; xml: 682,621; python: 49,092; lisp: 31,150; java: 25,469; ansic: 11,039; javascript: 6,085; sh: 1,214; cs: 601; perl: 494; objc: 210; makefile: 173
file content (390 lines) | stat: -rw-r--r-- 10,117 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
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
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
241
242
243
244
245
246
247
248
249
250
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
332
333
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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
#include "perf_precomp.hpp"

namespace opencv_test
{
using namespace perf;

namespace {

typedef perf::TestBaseWithParam<size_t> VectorLength;

PERF_TEST_P(VectorLength, phase32f, testing::Values(128, 1000, 128*1024, 512*1024, 1024*1024))
{
    size_t length = GetParam();
    vector<float> X(length);
    vector<float> Y(length);
    vector<float> angle(length);

    declare.in(X, Y, WARMUP_RNG).out(angle);

    TEST_CYCLE_N(200) cv::phase(X, Y, angle, true);

    SANITY_CHECK(angle, 5e-5);
}

PERF_TEST_P(VectorLength, phase64f, testing::Values(128, 1000, 128*1024, 512*1024, 1024*1024))
{
    size_t length = GetParam();
    vector<double> X(length);
    vector<double> Y(length);
    vector<double> angle(length);

    declare.in(X, Y, WARMUP_RNG).out(angle);

    TEST_CYCLE_N(200) cv::phase(X, Y, angle, true);

    SANITY_CHECK(angle, 5e-5);
}

// generates random vectors, performs Gram-Schmidt orthogonalization on them
Mat randomOrtho(int rows, int ftype, RNG& rng)
{
    Mat result(rows, rows, ftype);
    rng.fill(result, RNG::UNIFORM, cv::Scalar(-1), cv::Scalar(1));

    for (int i = 0; i < rows; i++)
    {
        Mat v = result.row(i);

        for (int j = 0; j < i; j++)
        {
            Mat p = result.row(j);
            v -= p.dot(v) * p;
        }

        v = v * (1. / cv::norm(v));
    }

    return result;
}

template<typename FType>
Mat buildRandomMat(int rows, int cols, RNG& rng, int rank, bool symmetrical)
{
    int mtype = cv::traits::Depth<FType>::value;
    Mat u = randomOrtho(rows, mtype, rng);
    Mat v = randomOrtho(cols, mtype, rng);
    Mat s(rows, cols, mtype, Scalar(0));

    std::vector<FType> singVals(rank);
    rng.fill(singVals, RNG::UNIFORM, Scalar(0), Scalar(10));
    std::sort(singVals.begin(), singVals.end());
    auto singIter = singVals.rbegin();
    for (int i = 0; i < rank; i++)
    {
        s.at<FType>(i, i) = *singIter++;
    }

    if (symmetrical)
        return u * s * u.t();
    else
        return u * s * v.t();
}

Mat buildRandomMat(int rows, int cols, int mtype, RNG& rng, int rank, bool symmetrical)
{
    if (mtype == CV_32F)
    {
        return buildRandomMat<float>(rows, cols, rng, rank, symmetrical);
    }
    else if (mtype == CV_64F)
    {
        return buildRandomMat<double>(rows, cols, rng, rank, symmetrical);
    }
    else
    {
        CV_Error(cv::Error::StsBadArg, "This type is not supported");
    }
}

CV_ENUM(SolveDecompEnum, DECOMP_LU, DECOMP_SVD, DECOMP_EIG, DECOMP_CHOLESKY, DECOMP_QR)

enum RankMatrixOptions
{
    RANK_HALF, RANK_MINUS_1, RANK_FULL
};

CV_ENUM(RankEnum, RANK_HALF, RANK_MINUS_1, RANK_FULL)

enum SolutionsOptions
{
    NO_SOLUTIONS, ONE_SOLUTION, MANY_SOLUTIONS
};

CV_ENUM(SolutionsEnum, NO_SOLUTIONS, ONE_SOLUTION, MANY_SOLUTIONS)

typedef perf::TestBaseWithParam<std::tuple<int, RankEnum, MatDepth, SolveDecompEnum, bool, SolutionsEnum>> SolveTest;

PERF_TEST_P(SolveTest, randomMat, ::testing::Combine(
    ::testing::Values(31, 64, 100),
    ::testing::Values(RANK_HALF, RANK_MINUS_1, RANK_FULL),
    ::testing::Values(CV_32F, CV_64F),
    ::testing::Values(DECOMP_LU, DECOMP_SVD, DECOMP_EIG, DECOMP_CHOLESKY, DECOMP_QR),
    ::testing::Bool(), // normal
    ::testing::Values(NO_SOLUTIONS, ONE_SOLUTION, MANY_SOLUTIONS)
    ))
{
    auto t = GetParam();
    int size       = std::get<0>(t);
    auto rankEnum  = std::get<1>(t);
    int mtype      = std::get<2>(t);
    int method     = std::get<3>(t);
    bool normal    = std::get<4>(t);
    auto solutions = std::get<5>(t);

    bool symmetrical = (method == DECOMP_CHOLESKY || method == DECOMP_LU);

    if (normal)
    {
        method |= DECOMP_NORMAL;
    }

    int rank = size;
    switch (rankEnum)
    {
        case RANK_HALF:    rank /= 2; break;
        case RANK_MINUS_1: rank -= 1; break;
        default: break;
    }

    RNG& rng = theRNG();
    Mat A = buildRandomMat(size, size, mtype, rng, rank, symmetrical);
    Mat x(size, 1, mtype);
    Mat b(size, 1, mtype);

    switch (solutions)
    {
        // no solutions, let's make b random
        case NO_SOLUTIONS:
        {
            rng.fill(b, RNG::UNIFORM, Scalar(-1), Scalar(1));
        }
        break;
        // exactly 1 solution, let's combine b from A and x
        case ONE_SOLUTION:
        {
            rng.fill(x, RNG::UNIFORM, Scalar(-10), Scalar(10));
            b = A * x;
        }
        break;
        // infinitely many solutions, let's make b zero
        default:
        {
            b = 0;
        }
        break;
    }

    TEST_CYCLE() cv::solve(A, b, x, method);

    SANITY_CHECK_NOTHING();
}

typedef perf::TestBaseWithParam<std::tuple<std::tuple<int, int>, RankEnum, MatDepth, bool, bool>> SvdTest;

PERF_TEST_P(SvdTest, decompose, ::testing::Combine(
    ::testing::Values(std::make_tuple(5, 15), std::make_tuple(32, 32), std::make_tuple(100, 100)),
    ::testing::Values(RANK_HALF, RANK_MINUS_1, RANK_FULL),
    ::testing::Values(CV_32F, CV_64F),
    ::testing::Bool(), // symmetrical
    ::testing::Bool() // needUV
    ))
{
    auto t = GetParam();
    auto rc          = std::get<0>(t);
    auto rankEnum    = std::get<1>(t);
    int mtype        = std::get<2>(t);
    bool symmetrical = std::get<3>(t);
    bool needUV      = std::get<4>(t);

    int rows = std::get<0>(rc);
    int cols = std::get<1>(rc);

    if (symmetrical)
    {
        rows = max(rows, cols);
        cols = rows;
    }

    int rank = std::min(rows, cols);
    switch (rankEnum)
    {
    case RANK_HALF:    rank /= 2; break;
    case RANK_MINUS_1: rank -= 1; break;
    default: break;
    }

    int flags = needUV ? 0 : SVD::NO_UV;

    RNG& rng = theRNG();
    Mat A = buildRandomMat(rows, cols, mtype, rng, rank, symmetrical);
    TEST_CYCLE() cv::SVD svd(A, flags);

    SANITY_CHECK_NOTHING();
}


PERF_TEST_P(SvdTest, backSubst, ::testing::Combine(
    ::testing::Values(std::make_tuple(5, 15), std::make_tuple(32, 32), std::make_tuple(100, 100)),
    ::testing::Values(RANK_HALF, RANK_MINUS_1, RANK_FULL),
    ::testing::Values(CV_32F, CV_64F),
    // back substitution works the same regardless of source matrix properties
    ::testing::Values(true),
    // back substitution has no sense without u and v
    ::testing::Values(true) // needUV
    ))
{
    auto t = GetParam();
    auto rc       = std::get<0>(t);
    auto rankEnum = std::get<1>(t);
    int mtype     = std::get<2>(t);

    int rows = std::get<0>(rc);
    int cols = std::get<1>(rc);

    int rank = std::min(rows, cols);
    switch (rankEnum)
    {
    case RANK_HALF:    rank /= 2; break;
    case RANK_MINUS_1: rank -= 1; break;
    default: break;
    }

    RNG& rng = theRNG();
    Mat A = buildRandomMat(rows, cols, mtype, rng, rank, /* symmetrical */ false);
    cv::SVD svd(A);
    // preallocate to not spend time on it during backSubst()
    Mat dst(cols, 1, mtype);
    Mat rhs(rows, 1, mtype);
    rng.fill(rhs, RNG::UNIFORM, Scalar(-10), Scalar(10));

    TEST_CYCLE() svd.backSubst(rhs, dst);

    SANITY_CHECK_NOTHING();
}


typedef perf::TestBaseWithParam< testing::tuple<int, int, int> > KMeans;

PERF_TEST_P_(KMeans, single_iter)
{
    RNG& rng = theRNG();
    const int K = testing::get<0>(GetParam());
    const int dims = testing::get<1>(GetParam());
    const int N = testing::get<2>(GetParam());
    const int attempts = 5;

    Mat data(N, dims, CV_32F);
    rng.fill(data, RNG::UNIFORM, -0.1, 0.1);

    const int N0 = K;
    Mat data0(N0, dims, CV_32F);
    rng.fill(data0, RNG::UNIFORM, -1, 1);

    for (int i = 0; i < N; i++)
    {
        int base = rng.uniform(0, N0);
        cv::add(data0.row(base), data.row(i), data.row(i));
    }

    declare.in(data);

    Mat labels, centers;

    TEST_CYCLE()
    {
        kmeans(data, K, labels, TermCriteria(TermCriteria::MAX_ITER+TermCriteria::EPS, 1, 0),
               attempts, KMEANS_PP_CENTERS, centers);
    }

    SANITY_CHECK_NOTHING();
}

PERF_TEST_P_(KMeans, good)
{
    RNG& rng = theRNG();
    const int K = testing::get<0>(GetParam());
    const int dims = testing::get<1>(GetParam());
    const int N = testing::get<2>(GetParam());
    const int attempts = 5;

    Mat data(N, dims, CV_32F);
    rng.fill(data, RNG::UNIFORM, -0.1, 0.1);

    const int N0 = K;
    Mat data0(N0, dims, CV_32F);
    rng.fill(data0, RNG::UNIFORM, -1, 1);

    for (int i = 0; i < N; i++)
    {
        int base = rng.uniform(0, N0);
        cv::add(data0.row(base), data.row(i), data.row(i));
    }

    declare.in(data);

    Mat labels, centers;

    TEST_CYCLE()
    {
        kmeans(data, K, labels, TermCriteria(TermCriteria::MAX_ITER+TermCriteria::EPS, 30, 0),
               attempts, KMEANS_PP_CENTERS, centers);
    }

    SANITY_CHECK_NOTHING();
}

PERF_TEST_P_(KMeans, with_duplicates)
{
    RNG& rng = theRNG();
    const int K = testing::get<0>(GetParam());
    const int dims = testing::get<1>(GetParam());
    const int N = testing::get<2>(GetParam());
    const int attempts = 5;

    Mat data(N, dims, CV_32F, Scalar::all(0));

    const int N0 = std::max(2, K * 2 / 3);
    Mat data0(N0, dims, CV_32F);
    rng.fill(data0, RNG::UNIFORM, -1, 1);

    for (int i = 0; i < N; i++)
    {
        int base = rng.uniform(0, N0);
        data0.row(base).copyTo(data.row(i));
    }

    declare.in(data);

    Mat labels, centers;

    TEST_CYCLE()
    {
        kmeans(data, K, labels, TermCriteria(TermCriteria::MAX_ITER+TermCriteria::EPS, 30, 0),
               attempts, KMEANS_PP_CENTERS, centers);
    }

    SANITY_CHECK_NOTHING();
}

INSTANTIATE_TEST_CASE_P(/*nothing*/ , KMeans,
    testing::Values(
        // K clusters, dims, N points
        testing::make_tuple(2, 3, 100000),
        testing::make_tuple(4, 3, 500),
        testing::make_tuple(4, 3, 1000),
        testing::make_tuple(4, 3, 10000),
        testing::make_tuple(8, 3, 1000),
        testing::make_tuple(8, 16, 1000),
        testing::make_tuple(8, 64, 1000),
        testing::make_tuple(16, 16, 1000),
        testing::make_tuple(16, 32, 1000),
        testing::make_tuple(32, 16, 1000),
        testing::make_tuple(32, 32, 1000),
        testing::make_tuple(100, 2, 1000)
    )
);

}

} // namespace