Skip to content

Commit 21b2e33

Browse files
authored
Merge pull request opencv#19497 from OrestChura:oc/kmeans_ptest
[G-API]: Performance tests for kmeans * - Perf.Tests for kmeans(2D, 3D (Point2f/3f), ND (Mat)) - New file for common parts of acc. and perf. tests for core kernels added - Some typos corrections * Applying comments
1 parent 0553543 commit 21b2e33

9 files changed

+374
-203
lines changed

modules/gapi/perf/common/gapi_core_perf_tests.hpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// It is subject to the license terms in the LICENSE file found in the top-level directory
33
// of this distribution and at http://opencv.org/license.html.
44
//
5-
// Copyright (C) 2018-2020 Intel Corporation
5+
// Copyright (C) 2018-2021 Intel Corporation
66

77

88
#ifndef OPENCV_GAPI_CORE_PERF_TESTS_HPP
@@ -73,6 +73,12 @@ namespace opencv_test
7373
class ConcatVertVecPerfTest : public TestPerfParams<tuple<cv::Size, MatType, cv::GCompileArgs>> {};
7474
class LUTPerfTest : public TestPerfParams<tuple<MatType, MatType, cv::Size, cv::GCompileArgs>> {};
7575
class ConvertToPerfTest : public TestPerfParams<tuple<compare_f, MatType, int, cv::Size, double, double, cv::GCompileArgs>> {};
76+
class KMeansNDPerfTest : public TestPerfParams<tuple<cv::Size, CompareMats, int,
77+
cv::KmeansFlags, cv::GCompileArgs>> {};
78+
class KMeans2DPerfTest : public TestPerfParams<tuple<int, int, cv::KmeansFlags,
79+
cv::GCompileArgs>> {};
80+
class KMeans3DPerfTest : public TestPerfParams<tuple<int, int, cv::KmeansFlags,
81+
cv::GCompileArgs>> {};
7682
class ResizePerfTest : public TestPerfParams<tuple<compare_f, MatType, int, cv::Size, cv::Size, cv::GCompileArgs>> {};
7783
class ResizeFxFyPerfTest : public TestPerfParams<tuple<compare_f, MatType, int, cv::Size, double, double, cv::GCompileArgs>> {};
7884
class ParseSSDBLPerfTest : public TestPerfParams<tuple<cv::Size, float, int, cv::GCompileArgs>>, public ParserSSDTest {};

modules/gapi/perf/common/gapi_core_perf_tests_inl.hpp

Lines changed: 132 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// It is subject to the license terms in the LICENSE file found in the top-level directory
33
// of this distribution and at http://opencv.org/license.html.
44
//
5-
// Copyright (C) 2018-2020 Intel Corporation
5+
// Copyright (C) 2018-2021 Intel Corporation
66

77

88
#ifndef OPENCV_GAPI_CORE_PERF_TESTS_INL_HPP
@@ -12,6 +12,8 @@
1212

1313
#include "gapi_core_perf_tests.hpp"
1414

15+
#include "../../test/common/gapi_core_tests_common.hpp"
16+
1517
namespace opencv_test
1618
{
1719
using namespace perf;
@@ -1905,6 +1907,135 @@ PERF_TEST_P_(ConvertToPerfTest, TestPerformance)
19051907

19061908
//------------------------------------------------------------------------------
19071909

1910+
PERF_TEST_P_(KMeansNDPerfTest, TestPerformance)
1911+
{
1912+
cv::Size sz;
1913+
CompareMats cmpF;
1914+
int K = -1;
1915+
cv::KmeansFlags flags = cv::KMEANS_RANDOM_CENTERS;
1916+
cv::GCompileArgs compile_args;
1917+
std::tie(sz, cmpF, K, flags, compile_args) = GetParam();
1918+
1919+
MatType2 type = CV_32FC1;
1920+
initMatrixRandU(type, sz, -1, false);
1921+
1922+
double compact_gapi = -1.;
1923+
cv::Mat labels_gapi, centers_gapi;
1924+
if (flags & cv::KMEANS_USE_INITIAL_LABELS)
1925+
{
1926+
const int amount = sz.height;
1927+
cv::Mat bestLabels(cv::Size{1, amount}, CV_32SC1);
1928+
cv::randu(bestLabels, 0, K);
1929+
1930+
cv::GComputation c(kmeansTestGAPI(in_mat1, bestLabels, K, flags, std::move(compile_args),
1931+
compact_gapi, labels_gapi, centers_gapi));
1932+
TEST_CYCLE()
1933+
{
1934+
c.apply(cv::gin(in_mat1, bestLabels),
1935+
cv::gout(compact_gapi, labels_gapi, centers_gapi));
1936+
}
1937+
kmeansTestOpenCVCompare(in_mat1, bestLabels, K, flags, compact_gapi, labels_gapi,
1938+
centers_gapi, cmpF);
1939+
}
1940+
else
1941+
{
1942+
cv::GComputation c(kmeansTestGAPI(in_mat1, K, flags, std::move(compile_args), compact_gapi,
1943+
labels_gapi, centers_gapi));
1944+
TEST_CYCLE()
1945+
{
1946+
c.apply(cv::gin(in_mat1), cv::gout(compact_gapi, labels_gapi, centers_gapi));
1947+
}
1948+
kmeansTestValidate(sz, type, K, compact_gapi, labels_gapi, centers_gapi);
1949+
}
1950+
SANITY_CHECK_NOTHING();
1951+
}
1952+
1953+
PERF_TEST_P_(KMeans2DPerfTest, TestPerformance)
1954+
{
1955+
int amount = -1;
1956+
int K = -1;
1957+
cv::KmeansFlags flags = cv::KMEANS_RANDOM_CENTERS;
1958+
cv::GCompileArgs compile_args;
1959+
std::tie(amount, K, flags, compile_args) = GetParam();
1960+
1961+
std::vector<cv::Point2f> in_vector{};
1962+
initPointsVectorRandU(amount, in_vector);
1963+
1964+
double compact_gapi = -1.;
1965+
std::vector<int> labels_gapi{};
1966+
std::vector<cv::Point2f> centers_gapi{};
1967+
if (flags & cv::KMEANS_USE_INITIAL_LABELS)
1968+
{
1969+
std::vector<int> bestLabels(amount);
1970+
cv::randu(bestLabels, 0, K);
1971+
1972+
cv::GComputation c(kmeansTestGAPI(in_vector, bestLabels, K, flags, std::move(compile_args),
1973+
compact_gapi, labels_gapi, centers_gapi));
1974+
TEST_CYCLE()
1975+
{
1976+
c.apply(cv::gin(in_vector, bestLabels),
1977+
cv::gout(compact_gapi, labels_gapi, centers_gapi));
1978+
}
1979+
kmeansTestOpenCVCompare(in_vector, bestLabels, K, flags, compact_gapi, labels_gapi,
1980+
centers_gapi);
1981+
}
1982+
else
1983+
{
1984+
cv::GComputation c(kmeansTestGAPI(in_vector, K, flags, std::move(compile_args),
1985+
compact_gapi, labels_gapi, centers_gapi));
1986+
TEST_CYCLE()
1987+
{
1988+
c.apply(cv::gin(in_vector), cv::gout(compact_gapi, labels_gapi, centers_gapi));
1989+
}
1990+
kmeansTestValidate({-1, amount}, -1, K, compact_gapi, labels_gapi, centers_gapi);
1991+
}
1992+
SANITY_CHECK_NOTHING();
1993+
}
1994+
1995+
PERF_TEST_P_(KMeans3DPerfTest, TestPerformance)
1996+
{
1997+
int amount = -1;
1998+
int K = -1;
1999+
cv::KmeansFlags flags = cv::KMEANS_RANDOM_CENTERS;
2000+
cv::GCompileArgs compile_args;
2001+
std::tie(amount, K, flags, compile_args) = GetParam();
2002+
2003+
std::vector<cv::Point3f> in_vector{};
2004+
initPointsVectorRandU(amount, in_vector);
2005+
2006+
double compact_gapi = -1.;
2007+
std::vector<int> labels_gapi;
2008+
std::vector<cv::Point3f> centers_gapi;
2009+
if (flags & cv::KMEANS_USE_INITIAL_LABELS)
2010+
{
2011+
std::vector<int> bestLabels(amount);
2012+
cv::randu(bestLabels, 0, K);
2013+
2014+
cv::GComputation c(kmeansTestGAPI(in_vector, bestLabels, K, flags, std::move(compile_args),
2015+
compact_gapi, labels_gapi, centers_gapi));
2016+
TEST_CYCLE()
2017+
{
2018+
c.apply(cv::gin(in_vector, bestLabels),
2019+
cv::gout(compact_gapi, labels_gapi, centers_gapi));
2020+
}
2021+
kmeansTestOpenCVCompare(in_vector, bestLabels, K, flags, compact_gapi, labels_gapi,
2022+
centers_gapi);
2023+
}
2024+
else
2025+
{
2026+
cv::GComputation c(kmeansTestGAPI(in_vector, K, flags, std::move(compile_args),
2027+
compact_gapi, labels_gapi, centers_gapi));
2028+
TEST_CYCLE()
2029+
{
2030+
c.apply(cv::gin(in_vector), cv::gout(compact_gapi, labels_gapi, centers_gapi));
2031+
}
2032+
kmeansTestValidate({-1, amount}, -1, K, compact_gapi, labels_gapi, centers_gapi);
2033+
}
2034+
SANITY_CHECK_NOTHING();
2035+
}
2036+
2037+
//------------------------------------------------------------------------------
2038+
19082039
PERF_TEST_P_(ResizePerfTest, TestPerformance)
19092040
{
19102041
compare_f cmpF = get<0>(GetParam());

modules/gapi/perf/cpu/gapi_core_perf_tests_cpu.cpp

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// It is subject to the license terms in the LICENSE file found in the top-level directory
33
// of this distribution and at http://opencv.org/license.html.
44
//
5-
// Copyright (C) 2018-2020 Intel Corporation
5+
// Copyright (C) 2018-2021 Intel Corporation
66

77

88
#include "../perf_precomp.hpp"
@@ -282,6 +282,35 @@ INSTANTIATE_TEST_CASE_P(ConvertToPerfTestCPU, ConvertToPerfTest,
282282
Values(0.0),
283283
Values(cv::compile_args(CORE_CPU))));
284284

285+
INSTANTIATE_TEST_CASE_P(KMeansNDPerfTestCPU, KMeansNDPerfTest,
286+
Combine(Values(cv::Size(1, 20),
287+
cv::Size(16, 4096)),
288+
Values(AbsTolerance(0.01).to_compare_obj()),
289+
Values(5, 15),
290+
Values(cv::KMEANS_RANDOM_CENTERS,
291+
cv::KMEANS_PP_CENTERS,
292+
cv::KMEANS_RANDOM_CENTERS | cv::KMEANS_USE_INITIAL_LABELS,
293+
cv::KMEANS_PP_CENTERS | cv::KMEANS_USE_INITIAL_LABELS),
294+
Values(cv::compile_args(CORE_CPU))));
295+
296+
INSTANTIATE_TEST_CASE_P(KMeans2DPerfTestCPU, KMeans2DPerfTest,
297+
Combine(Values(20, 4096),
298+
Values(5, 15),
299+
Values(cv::KMEANS_RANDOM_CENTERS,
300+
cv::KMEANS_PP_CENTERS,
301+
cv::KMEANS_RANDOM_CENTERS | cv::KMEANS_USE_INITIAL_LABELS,
302+
cv::KMEANS_PP_CENTERS | cv::KMEANS_USE_INITIAL_LABELS),
303+
Values(cv::compile_args(CORE_CPU))));
304+
305+
INSTANTIATE_TEST_CASE_P(KMeans3DPerfTestCPU, KMeans3DPerfTest,
306+
Combine(Values(20, 4096),
307+
Values(5, 15),
308+
Values(cv::KMEANS_RANDOM_CENTERS,
309+
cv::KMEANS_PP_CENTERS,
310+
cv::KMEANS_RANDOM_CENTERS | cv::KMEANS_USE_INITIAL_LABELS,
311+
cv::KMEANS_PP_CENTERS | cv::KMEANS_USE_INITIAL_LABELS),
312+
Values(cv::compile_args(CORE_CPU))));
313+
285314
INSTANTIATE_TEST_CASE_P(ResizePerfTestCPU, ResizePerfTest,
286315
Combine(Values(AbsExact().to_compare_f()),
287316
Values(CV_8UC1, CV_16UC1, CV_16SC1),

modules/gapi/test/common/gapi_core_tests.hpp

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// It is subject to the license terms in the LICENSE file found in the top-level directory
33
// of this distribution and at http://opencv.org/license.html.
44
//
5-
// Copyright (C) 2018-2020 Intel Corporation
5+
// Copyright (C) 2018-2021 Intel Corporation
66

77

88
#ifndef OPENCV_GAPI_CORE_TESTS_HPP
@@ -151,16 +151,9 @@ GAPI_TEST_FIXTURE(WarpPerspectiveTest, initMatrixRandU,
151151
GAPI_TEST_FIXTURE(WarpAffineTest, initMatrixRandU,
152152
FIXTURE_API(CompareMats, double , double, int, int, cv::Scalar),
153153
6, cmpF, angle, scale, flags, border_mode, border_value)
154-
GAPI_TEST_FIXTURE(KMeansNDNoInitTest, initMatrixRandU, FIXTURE_API(int, cv::KmeansFlags),
155-
2, K, flags)
156-
GAPI_TEST_FIXTURE(KMeansNDInitTest, initMatrixRandU,
157-
FIXTURE_API(CompareMats, int, cv::KmeansFlags), 3, cmpF, K, flags)
158-
GAPI_TEST_FIXTURE(KMeans2DNoInitTest, initNothing, FIXTURE_API(int, cv::KmeansFlags),
159-
2, K, flags)
160-
GAPI_TEST_FIXTURE(KMeans2DInitTest, initNothing, FIXTURE_API(int, cv::KmeansFlags), 2, K, flags)
161-
GAPI_TEST_FIXTURE(KMeans3DNoInitTest, initNothing, FIXTURE_API(int, cv::KmeansFlags),
162-
2, K, flags)
163-
GAPI_TEST_FIXTURE(KMeans3DInitTest, initNothing, FIXTURE_API(int, cv::KmeansFlags), 2, K, flags)
154+
GAPI_TEST_FIXTURE(KMeansNDTest, initMatrixRandU, FIXTURE_API(CompareMats, int, cv::KmeansFlags), 3, cmpF, K, flags)
155+
GAPI_TEST_FIXTURE(KMeans2DTest, initNothing, FIXTURE_API(int, cv::KmeansFlags), 2, K, flags)
156+
GAPI_TEST_FIXTURE(KMeans3DTest, initNothing, FIXTURE_API(int, cv::KmeansFlags), 2, K, flags)
164157

165158
GAPI_TEST_EXT_BASE_FIXTURE(ParseSSDBLTest, ParserSSDTest, initNothing,
166159
FIXTURE_API(float, int), 2, confidence_threshold, filter_label)

0 commit comments

Comments
 (0)