From 21b2e33ebb448b23ebdf3626e90342ac6ad2dcdb Mon Sep 17 00:00:00 2001 From: Orest Chura Date: Fri, 26 Feb 2021 00:58:52 +0300 Subject: [PATCH] Merge pull request #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 --- .../gapi/perf/common/gapi_core_perf_tests.hpp | 8 +- .../perf/common/gapi_core_perf_tests_inl.hpp | 133 ++++++++++++- .../perf/cpu/gapi_core_perf_tests_cpu.cpp | 31 ++- modules/gapi/test/common/gapi_core_tests.hpp | 15 +- .../test/common/gapi_core_tests_common.hpp | 180 +++++++++++++++++ .../gapi/test/common/gapi_core_tests_inl.hpp | 187 +----------------- .../test/common/gapi_imgproc_tests_common.hpp | 2 +- .../gapi/test/common/gapi_tests_common.hpp | 4 +- modules/gapi/test/cpu/gapi_core_tests_cpu.cpp | 17 +- 9 files changed, 374 insertions(+), 203 deletions(-) create mode 100644 modules/gapi/test/common/gapi_core_tests_common.hpp diff --git a/modules/gapi/perf/common/gapi_core_perf_tests.hpp b/modules/gapi/perf/common/gapi_core_perf_tests.hpp index 4a88b4f59c..be88e5a721 100644 --- a/modules/gapi/perf/common/gapi_core_perf_tests.hpp +++ b/modules/gapi/perf/common/gapi_core_perf_tests.hpp @@ -2,7 +2,7 @@ // It is subject to the license terms in the LICENSE file found in the top-level directory // of this distribution and at http://opencv.org/license.html. // -// Copyright (C) 2018-2020 Intel Corporation +// Copyright (C) 2018-2021 Intel Corporation #ifndef OPENCV_GAPI_CORE_PERF_TESTS_HPP @@ -73,6 +73,12 @@ namespace opencv_test class ConcatVertVecPerfTest : public TestPerfParams> {}; class LUTPerfTest : public TestPerfParams> {}; class ConvertToPerfTest : public TestPerfParams> {}; + class KMeansNDPerfTest : public TestPerfParams> {}; + class KMeans2DPerfTest : public TestPerfParams> {}; + class KMeans3DPerfTest : public TestPerfParams> {}; class ResizePerfTest : public TestPerfParams> {}; class ResizeFxFyPerfTest : public TestPerfParams> {}; class ParseSSDBLPerfTest : public TestPerfParams>, public ParserSSDTest {}; diff --git a/modules/gapi/perf/common/gapi_core_perf_tests_inl.hpp b/modules/gapi/perf/common/gapi_core_perf_tests_inl.hpp index ac90181184..7fe0ec4c26 100644 --- a/modules/gapi/perf/common/gapi_core_perf_tests_inl.hpp +++ b/modules/gapi/perf/common/gapi_core_perf_tests_inl.hpp @@ -2,7 +2,7 @@ // It is subject to the license terms in the LICENSE file found in the top-level directory // of this distribution and at http://opencv.org/license.html. // -// Copyright (C) 2018-2020 Intel Corporation +// Copyright (C) 2018-2021 Intel Corporation #ifndef OPENCV_GAPI_CORE_PERF_TESTS_INL_HPP @@ -12,6 +12,8 @@ #include "gapi_core_perf_tests.hpp" +#include "../../test/common/gapi_core_tests_common.hpp" + namespace opencv_test { using namespace perf; @@ -1905,6 +1907,135 @@ PERF_TEST_P_(ConvertToPerfTest, TestPerformance) //------------------------------------------------------------------------------ +PERF_TEST_P_(KMeansNDPerfTest, TestPerformance) +{ + cv::Size sz; + CompareMats cmpF; + int K = -1; + cv::KmeansFlags flags = cv::KMEANS_RANDOM_CENTERS; + cv::GCompileArgs compile_args; + std::tie(sz, cmpF, K, flags, compile_args) = GetParam(); + + MatType2 type = CV_32FC1; + initMatrixRandU(type, sz, -1, false); + + double compact_gapi = -1.; + cv::Mat labels_gapi, centers_gapi; + if (flags & cv::KMEANS_USE_INITIAL_LABELS) + { + const int amount = sz.height; + cv::Mat bestLabels(cv::Size{1, amount}, CV_32SC1); + cv::randu(bestLabels, 0, K); + + cv::GComputation c(kmeansTestGAPI(in_mat1, bestLabels, K, flags, std::move(compile_args), + compact_gapi, labels_gapi, centers_gapi)); + TEST_CYCLE() + { + c.apply(cv::gin(in_mat1, bestLabels), + cv::gout(compact_gapi, labels_gapi, centers_gapi)); + } + kmeansTestOpenCVCompare(in_mat1, bestLabels, K, flags, compact_gapi, labels_gapi, + centers_gapi, cmpF); + } + else + { + cv::GComputation c(kmeansTestGAPI(in_mat1, K, flags, std::move(compile_args), compact_gapi, + labels_gapi, centers_gapi)); + TEST_CYCLE() + { + c.apply(cv::gin(in_mat1), cv::gout(compact_gapi, labels_gapi, centers_gapi)); + } + kmeansTestValidate(sz, type, K, compact_gapi, labels_gapi, centers_gapi); + } + SANITY_CHECK_NOTHING(); +} + +PERF_TEST_P_(KMeans2DPerfTest, TestPerformance) +{ + int amount = -1; + int K = -1; + cv::KmeansFlags flags = cv::KMEANS_RANDOM_CENTERS; + cv::GCompileArgs compile_args; + std::tie(amount, K, flags, compile_args) = GetParam(); + + std::vector in_vector{}; + initPointsVectorRandU(amount, in_vector); + + double compact_gapi = -1.; + std::vector labels_gapi{}; + std::vector centers_gapi{}; + if (flags & cv::KMEANS_USE_INITIAL_LABELS) + { + std::vector bestLabels(amount); + cv::randu(bestLabels, 0, K); + + cv::GComputation c(kmeansTestGAPI(in_vector, bestLabels, K, flags, std::move(compile_args), + compact_gapi, labels_gapi, centers_gapi)); + TEST_CYCLE() + { + c.apply(cv::gin(in_vector, bestLabels), + cv::gout(compact_gapi, labels_gapi, centers_gapi)); + } + kmeansTestOpenCVCompare(in_vector, bestLabels, K, flags, compact_gapi, labels_gapi, + centers_gapi); + } + else + { + cv::GComputation c(kmeansTestGAPI(in_vector, K, flags, std::move(compile_args), + compact_gapi, labels_gapi, centers_gapi)); + TEST_CYCLE() + { + c.apply(cv::gin(in_vector), cv::gout(compact_gapi, labels_gapi, centers_gapi)); + } + kmeansTestValidate({-1, amount}, -1, K, compact_gapi, labels_gapi, centers_gapi); + } + SANITY_CHECK_NOTHING(); +} + +PERF_TEST_P_(KMeans3DPerfTest, TestPerformance) +{ + int amount = -1; + int K = -1; + cv::KmeansFlags flags = cv::KMEANS_RANDOM_CENTERS; + cv::GCompileArgs compile_args; + std::tie(amount, K, flags, compile_args) = GetParam(); + + std::vector in_vector{}; + initPointsVectorRandU(amount, in_vector); + + double compact_gapi = -1.; + std::vector labels_gapi; + std::vector centers_gapi; + if (flags & cv::KMEANS_USE_INITIAL_LABELS) + { + std::vector bestLabels(amount); + cv::randu(bestLabels, 0, K); + + cv::GComputation c(kmeansTestGAPI(in_vector, bestLabels, K, flags, std::move(compile_args), + compact_gapi, labels_gapi, centers_gapi)); + TEST_CYCLE() + { + c.apply(cv::gin(in_vector, bestLabels), + cv::gout(compact_gapi, labels_gapi, centers_gapi)); + } + kmeansTestOpenCVCompare(in_vector, bestLabels, K, flags, compact_gapi, labels_gapi, + centers_gapi); + } + else + { + cv::GComputation c(kmeansTestGAPI(in_vector, K, flags, std::move(compile_args), + compact_gapi, labels_gapi, centers_gapi)); + TEST_CYCLE() + { + c.apply(cv::gin(in_vector), cv::gout(compact_gapi, labels_gapi, centers_gapi)); + } + kmeansTestValidate({-1, amount}, -1, K, compact_gapi, labels_gapi, centers_gapi); + } + SANITY_CHECK_NOTHING(); +} + +//------------------------------------------------------------------------------ + PERF_TEST_P_(ResizePerfTest, TestPerformance) { compare_f cmpF = get<0>(GetParam()); diff --git a/modules/gapi/perf/cpu/gapi_core_perf_tests_cpu.cpp b/modules/gapi/perf/cpu/gapi_core_perf_tests_cpu.cpp index ffc4b1a646..8385169050 100644 --- a/modules/gapi/perf/cpu/gapi_core_perf_tests_cpu.cpp +++ b/modules/gapi/perf/cpu/gapi_core_perf_tests_cpu.cpp @@ -2,7 +2,7 @@ // It is subject to the license terms in the LICENSE file found in the top-level directory // of this distribution and at http://opencv.org/license.html. // -// Copyright (C) 2018-2020 Intel Corporation +// Copyright (C) 2018-2021 Intel Corporation #include "../perf_precomp.hpp" @@ -282,6 +282,35 @@ INSTANTIATE_TEST_CASE_P(ConvertToPerfTestCPU, ConvertToPerfTest, Values(0.0), Values(cv::compile_args(CORE_CPU)))); +INSTANTIATE_TEST_CASE_P(KMeansNDPerfTestCPU, KMeansNDPerfTest, + Combine(Values(cv::Size(1, 20), + cv::Size(16, 4096)), + Values(AbsTolerance(0.01).to_compare_obj()), + Values(5, 15), + Values(cv::KMEANS_RANDOM_CENTERS, + cv::KMEANS_PP_CENTERS, + cv::KMEANS_RANDOM_CENTERS | cv::KMEANS_USE_INITIAL_LABELS, + cv::KMEANS_PP_CENTERS | cv::KMEANS_USE_INITIAL_LABELS), + Values(cv::compile_args(CORE_CPU)))); + +INSTANTIATE_TEST_CASE_P(KMeans2DPerfTestCPU, KMeans2DPerfTest, + Combine(Values(20, 4096), + Values(5, 15), + Values(cv::KMEANS_RANDOM_CENTERS, + cv::KMEANS_PP_CENTERS, + cv::KMEANS_RANDOM_CENTERS | cv::KMEANS_USE_INITIAL_LABELS, + cv::KMEANS_PP_CENTERS | cv::KMEANS_USE_INITIAL_LABELS), + Values(cv::compile_args(CORE_CPU)))); + +INSTANTIATE_TEST_CASE_P(KMeans3DPerfTestCPU, KMeans3DPerfTest, + Combine(Values(20, 4096), + Values(5, 15), + Values(cv::KMEANS_RANDOM_CENTERS, + cv::KMEANS_PP_CENTERS, + cv::KMEANS_RANDOM_CENTERS | cv::KMEANS_USE_INITIAL_LABELS, + cv::KMEANS_PP_CENTERS | cv::KMEANS_USE_INITIAL_LABELS), + Values(cv::compile_args(CORE_CPU)))); + INSTANTIATE_TEST_CASE_P(ResizePerfTestCPU, ResizePerfTest, Combine(Values(AbsExact().to_compare_f()), Values(CV_8UC1, CV_16UC1, CV_16SC1), diff --git a/modules/gapi/test/common/gapi_core_tests.hpp b/modules/gapi/test/common/gapi_core_tests.hpp index f299d84d90..e87828200e 100644 --- a/modules/gapi/test/common/gapi_core_tests.hpp +++ b/modules/gapi/test/common/gapi_core_tests.hpp @@ -2,7 +2,7 @@ // It is subject to the license terms in the LICENSE file found in the top-level directory // of this distribution and at http://opencv.org/license.html. // -// Copyright (C) 2018-2020 Intel Corporation +// Copyright (C) 2018-2021 Intel Corporation #ifndef OPENCV_GAPI_CORE_TESTS_HPP @@ -151,16 +151,9 @@ GAPI_TEST_FIXTURE(WarpPerspectiveTest, initMatrixRandU, GAPI_TEST_FIXTURE(WarpAffineTest, initMatrixRandU, FIXTURE_API(CompareMats, double , double, int, int, cv::Scalar), 6, cmpF, angle, scale, flags, border_mode, border_value) -GAPI_TEST_FIXTURE(KMeansNDNoInitTest, initMatrixRandU, FIXTURE_API(int, cv::KmeansFlags), - 2, K, flags) -GAPI_TEST_FIXTURE(KMeansNDInitTest, initMatrixRandU, - FIXTURE_API(CompareMats, int, cv::KmeansFlags), 3, cmpF, K, flags) -GAPI_TEST_FIXTURE(KMeans2DNoInitTest, initNothing, FIXTURE_API(int, cv::KmeansFlags), - 2, K, flags) -GAPI_TEST_FIXTURE(KMeans2DInitTest, initNothing, FIXTURE_API(int, cv::KmeansFlags), 2, K, flags) -GAPI_TEST_FIXTURE(KMeans3DNoInitTest, initNothing, FIXTURE_API(int, cv::KmeansFlags), - 2, K, flags) -GAPI_TEST_FIXTURE(KMeans3DInitTest, initNothing, FIXTURE_API(int, cv::KmeansFlags), 2, K, flags) +GAPI_TEST_FIXTURE(KMeansNDTest, initMatrixRandU, FIXTURE_API(CompareMats, int, cv::KmeansFlags), 3, cmpF, K, flags) +GAPI_TEST_FIXTURE(KMeans2DTest, initNothing, FIXTURE_API(int, cv::KmeansFlags), 2, K, flags) +GAPI_TEST_FIXTURE(KMeans3DTest, initNothing, FIXTURE_API(int, cv::KmeansFlags), 2, K, flags) GAPI_TEST_EXT_BASE_FIXTURE(ParseSSDBLTest, ParserSSDTest, initNothing, FIXTURE_API(float, int), 2, confidence_threshold, filter_label) diff --git a/modules/gapi/test/common/gapi_core_tests_common.hpp b/modules/gapi/test/common/gapi_core_tests_common.hpp new file mode 100644 index 0000000000..faaa2f7061 --- /dev/null +++ b/modules/gapi/test/common/gapi_core_tests_common.hpp @@ -0,0 +1,180 @@ +// This file is part of OpenCV project. +// It is subject to the license terms in the LICENSE file found in the top-level directory +// of this distribution and at http://opencv.org/license.html. +// +// Copyright (C) 2021 Intel Corporation + +#ifndef OPENCV_GAPI_CORE_TESTS_COMMON_HPP +#define OPENCV_GAPI_CORE_TESTS_COMMON_HPP + +#include "gapi_tests_common.hpp" +#include "../../include/opencv2/gapi/core.hpp" + +#include + +namespace opencv_test +{ +namespace +{ +template +inline bool compareKMeansOutputs(const std::vector& outGAPI, + const std::vector& outOCV, + const CmpF& = AbsExact().to_compare_obj()) +{ + return AbsExactVector().to_compare_f()(outGAPI, outOCV); +} + +inline bool compareKMeansOutputs(const cv::Mat& outGAPI, + const cv::Mat& outOCV, + const CompareMats& cmpF) +{ + return cmpF(outGAPI, outOCV); +} +} + +// Overload with initializing the labels +template +cv::GComputation kmeansTestGAPI(const In& in, const Labels& bestLabels, const int K, + const cv::KmeansFlags flags, cv::GCompileArgs&& args, + double& compact_gapi, Labels& labels_gapi, In& centers_gapi) +{ + const cv::TermCriteria criteria(cv::TermCriteria::MAX_ITER + cv::TermCriteria::EPS, 30, 0); + const int attempts = 1; + + cv::detail::g_type_of_t gIn, centers; + cv::GOpaque compactness; + cv::detail::g_type_of_t inLabels, outLabels; + std::tie(compactness, outLabels, centers) = + cv::gapi::kmeans(gIn, K, inLabels, criteria, attempts, flags); + cv::GComputation c(cv::GIn(gIn, inLabels), cv::GOut(compactness, outLabels, centers)); + c.apply(cv::gin(in, bestLabels), cv::gout(compact_gapi, labels_gapi, centers_gapi), + std::move(args)); + return c; +} + +// Overload for vector tests w/o initializing the labels +template +cv::GComputation kmeansTestGAPI(const std::vector& in, const int K, + const cv::KmeansFlags flags, cv::GCompileArgs&& args, + double& compact_gapi, std::vector& labels_gapi, + std::vector& centers_gapi) +{ + const cv::TermCriteria criteria(cv::TermCriteria::MAX_ITER + cv::TermCriteria::EPS, 30, 0); + const int attempts = 1; + + cv::GArray gIn, centers; + cv::GOpaque compactness; + cv::GArray inLabels(std::vector{}), outLabels; + std::tie(compactness, outLabels, centers) = + cv::gapi::kmeans(gIn, K, inLabels, criteria, attempts, flags); + cv::GComputation c(cv::GIn(gIn), cv::GOut(compactness, outLabels, centers)); + c.apply(cv::gin(in), cv::gout(compact_gapi, labels_gapi, centers_gapi), std::move(args)); + return c; +} + +// Overload for Mat tests w/o initializing the labels +static cv::GComputation kmeansTestGAPI(const cv::Mat& in, const int K, + const cv::KmeansFlags flags, cv::GCompileArgs&& args, + double& compact_gapi, cv::Mat& labels_gapi, + cv::Mat& centers_gapi) +{ + const cv::TermCriteria criteria(cv::TermCriteria::MAX_ITER + cv::TermCriteria::EPS, 30, 0); + const int attempts = 1; + + cv::GMat gIn, centers, labels; + cv::GOpaque compactness; + std::tie(compactness, labels, centers) = cv::gapi::kmeans(gIn, K, criteria, attempts, flags); + cv::GComputation c(cv::GIn(gIn), cv::GOut(compactness, labels, centers)); + c.apply(cv::gin(in), cv::gout(compact_gapi, labels_gapi, centers_gapi), std::move(args)); + return c; +} + +template +void kmeansTestValidate(const cv::Size& sz, const MatType2&, const int K, + const double compact_gapi, const std::vector& labels_gapi, + const std::vector& centers_gapi) +{ + const int amount = sz.height; + // Validation + EXPECT_GE(compact_gapi, 0.); + EXPECT_EQ(labels_gapi.size(), static_cast(amount)); + EXPECT_EQ(centers_gapi.size(), static_cast(K)); +} + +static void kmeansTestValidate(const cv::Size& sz, const MatType2& type, const int K, + const double compact_gapi, const cv::Mat& labels_gapi, + const cv::Mat& centers_gapi) +{ + const int chan = (type >> CV_CN_SHIFT) + 1; + const int amount = sz.height != 1 ? sz.height : sz.width; + const int dim = sz.height != 1 ? sz.width * chan : chan; + // Validation + EXPECT_GE(compact_gapi, 0.); + EXPECT_FALSE(labels_gapi.empty()); + EXPECT_FALSE(centers_gapi.empty()); + EXPECT_EQ(labels_gapi.rows, amount); + EXPECT_EQ(labels_gapi.cols, 1); + EXPECT_EQ(centers_gapi.rows, K); + EXPECT_EQ(centers_gapi.cols, dim); +} + +template +void kmeansTestOpenCVCompare(const In& in, const Labels& bestLabels, const int K, + const cv::KmeansFlags flags, const double compact_gapi, + const Labels& labels_gapi, const In& centers_gapi, + const CompareMats& cmpF = AbsExact().to_compare_obj()) +{ + const cv::TermCriteria criteria(cv::TermCriteria::MAX_ITER + cv::TermCriteria::EPS, 30, 0); + const int attempts = 1; + Labels labels_ocv; + In centers_ocv; + { // step to generalize cv::Mat & std::vector cases of bestLabels' types + cv::Mat bestLabelsMat(bestLabels); + bestLabelsMat.copyTo(labels_ocv); + } + // OpenCV code ///////////////////////////////////////////////////////////// + double compact_ocv = cv::kmeans(in, K, labels_ocv, criteria, attempts, flags, centers_ocv); + // Comparison ////////////////////////////////////////////////////////////// + EXPECT_TRUE(compact_gapi == compact_ocv); + EXPECT_TRUE(compareKMeansOutputs(labels_gapi, labels_ocv, cmpF)); + EXPECT_TRUE(compareKMeansOutputs(centers_gapi, centers_ocv, cmpF)); +} + +// If an input type is cv::Mat, labels' type is also cv::Mat; +// in other cases, their type has to be std::vector +template +using KMeansLabelType = typename std::conditional::value, + cv::Mat, + std::vector + >::type; +template > +void kmeansTestBody(const In& in, const cv::Size& sz, const MatType2& type, const int K, + const cv::KmeansFlags flags, cv::GCompileArgs&& args, + const CompareMats& cmpF = AbsExact().to_compare_obj()) +{ + double compact_gapi = -1.; + Labels labels_gapi; + In centers_gapi; + if (flags & cv::KMEANS_USE_INITIAL_LABELS) + { + Labels bestLabels; + { // step to generalize cv::Mat & std::vector cases of bestLabels' types + const int amount = (sz.height != 1 || sz.width == -1) ? sz.height : sz.width; + cv::Mat bestLabelsMat(cv::Size{1, amount}, CV_32SC1); + cv::randu(bestLabelsMat, 0, K); + bestLabelsMat.copyTo(bestLabels); + } + kmeansTestGAPI(in, bestLabels, K, flags, std::move(args), compact_gapi, labels_gapi, + centers_gapi); + kmeansTestOpenCVCompare(in, bestLabels, K, flags, compact_gapi, labels_gapi, + centers_gapi, cmpF); + } + else + { + kmeansTestGAPI(in, K, flags, std::move(args), compact_gapi, labels_gapi, centers_gapi); + kmeansTestValidate(sz, type, K, compact_gapi, labels_gapi, centers_gapi); + } +} +} // namespace opencv_test + +#endif // OPENCV_GAPI_CORE_TESTS_COMMON_HPP diff --git a/modules/gapi/test/common/gapi_core_tests_inl.hpp b/modules/gapi/test/common/gapi_core_tests_inl.hpp index 683a1455a0..d4760e804e 100644 --- a/modules/gapi/test/common/gapi_core_tests_inl.hpp +++ b/modules/gapi/test/common/gapi_core_tests_inl.hpp @@ -2,7 +2,7 @@ // It is subject to the license terms in the LICENSE file found in the top-level directory // of this distribution and at http://opencv.org/license.html. // -// Copyright (C) 2018-2020 Intel Corporation +// Copyright (C) 2018-2021 Intel Corporation #ifndef OPENCV_GAPI_CORE_TESTS_INL_HPP @@ -12,19 +12,10 @@ #include #include "gapi_core_tests.hpp" -namespace opencv_test -{ +#include "gapi_core_tests_common.hpp" -namespace -{ -template -inline bool compareVectorsAbsExact(const std::vector& outGAPI, - const std::vector& outOCV) +namespace opencv_test { - return AbsExactVector().to_compare_f()(outGAPI, outOCV); -} -} - TEST_P(MathOpTest, MatricesAccuracyTest) { // G-API code & corresponding OpenCV code //////////////////////////////// @@ -1391,185 +1382,25 @@ TEST_P(NormalizeTest, Test) } } -TEST_P(KMeansNDNoInitTest, AccuracyTest) +TEST_P(KMeansNDTest, AccuracyTest) { - const int amount = sz.height != 1 ? sz.height : sz.width, - dim = sz.height != 1 ? sz.width : (type >> CV_CN_SHIFT) + 1; - // amount of channels - const cv::TermCriteria criteria(TermCriteria::MAX_ITER + TermCriteria::EPS, 30, 0); - const int attempts = 1; - double compact_gapi = -1.; - cv::Mat labels_gapi, centers_gapi; - // G-API code ////////////////////////////////////////////////////////////// - cv::GMat in; - cv::GOpaque compactness; - cv::GMat outLabels, centers; - std::tie(compactness, outLabels, centers) = cv::gapi::kmeans(in, K, criteria, attempts, flags); - cv::GComputation c(cv::GIn(in), cv::GOut(compactness, outLabels, centers)); - c.apply(cv::gin(in_mat1), cv::gout(compact_gapi, labels_gapi, centers_gapi), getCompileArgs()); - // Validation ////////////////////////////////////////////////////////////// - { - EXPECT_GE(compact_gapi, 0.); - EXPECT_EQ(labels_gapi.cols, 1); - EXPECT_EQ(labels_gapi.rows, amount); - EXPECT_FALSE(labels_gapi.empty()); - EXPECT_EQ(centers_gapi.cols, dim); - EXPECT_EQ(centers_gapi.rows, K); - EXPECT_FALSE(centers_gapi.empty()); - } -} - -TEST_P(KMeansNDInitTest, AccuracyTest) -{ - const int amount = sz.height != 1 ? sz.height : sz.width; - const cv::TermCriteria criteria(TermCriteria::MAX_ITER + TermCriteria::EPS, 30, 0); - const int attempts = 1; - cv::Mat bestLabels(cv::Size{1, amount}, CV_32SC1); - double compact_ocv = -1., compact_gapi = -1.; - cv::Mat labels_ocv, labels_gapi, centers_ocv, centers_gapi; - cv::randu(bestLabels, 0, K); - bestLabels.copyTo(labels_ocv); - // G-API code ////////////////////////////////////////////////////////////// - cv::GMat in, inLabels; - cv::GOpaque compactness; - cv::GMat outLabels, centers; - std::tie(compactness, outLabels, centers) = - cv::gapi::kmeans(in, K, inLabels, criteria, attempts, flags); - cv::GComputation c(cv::GIn(in, inLabels), cv::GOut(compactness, outLabels, centers)); - c.apply(cv::gin(in_mat1, bestLabels), cv::gout(compact_gapi, labels_gapi, centers_gapi), - getCompileArgs()); - // OpenCV code ///////////////////////////////////////////////////////////// - compact_ocv = cv::kmeans(in_mat1, K, labels_ocv, criteria, attempts, flags, centers_ocv); - // Comparison ////////////////////////////////////////////////////////////// - { - EXPECT_TRUE(compact_gapi == compact_ocv); - EXPECT_TRUE(cmpF(labels_gapi, labels_ocv)); - EXPECT_TRUE(cmpF(centers_gapi, centers_ocv)); - } -} - -TEST_P(KMeans2DNoInitTest, AccuracyTest) -{ - const int amount = sz.height; - const cv::TermCriteria criteria(TermCriteria::MAX_ITER + TermCriteria::EPS, 30, 0); - const int attempts = 1; - std::vector in_vector{}; - double compact_gapi = -1.; - std::vector labels_gapi{}; - std::vector centers_gapi{}; - initPointsVectorRandU(amount, in_vector); - // G-API code ////////////////////////////////////////////////////////////// - cv::GArray in; - cv::GArray inLabels(std::vector{}); - cv::GOpaque compactness; - cv::GArray outLabels; - cv::GArray centers; - std::tie(compactness, outLabels, centers) = - cv::gapi::kmeans(in, K, inLabels, criteria, attempts, flags); - cv::GComputation c(cv::GIn(in), cv::GOut(compactness, outLabels, centers)); - c.apply(cv::gin(in_vector), cv::gout(compact_gapi, labels_gapi, centers_gapi), getCompileArgs()); - // Validation ////////////////////////////////////////////////////////////// - { - EXPECT_GE(compact_gapi, 0.); - EXPECT_EQ(labels_gapi.size(), static_cast(amount)); - EXPECT_EQ(centers_gapi.size(), static_cast(K)); - } + kmeansTestBody(in_mat1, sz, type, K, flags, getCompileArgs(), cmpF); } -TEST_P(KMeans2DInitTest, AccuracyTest) +TEST_P(KMeans2DTest, AccuracyTest) { const int amount = sz.height; - const cv::TermCriteria criteria(TermCriteria::MAX_ITER + TermCriteria::EPS, 30, 0); - const int attempts = 1; std::vector in_vector{}; - std::vector bestLabels(amount); - double compact_ocv = -1., compact_gapi = -1.; - std::vector labels_ocv{}, labels_gapi{}; - std::vector centers_ocv{}, centers_gapi{}; initPointsVectorRandU(amount, in_vector); - cv::randu(bestLabels, 0, K); - labels_ocv = bestLabels; - // G-API code ////////////////////////////////////////////////////////////// - cv::GArray in; - cv::GArray inLabels; - cv::GOpaque compactness; - cv::GArray outLabels; - cv::GArray centers; - std::tie(compactness, outLabels, centers) = - cv::gapi::kmeans(in, K, inLabels, criteria, attempts, flags); - cv::GComputation c(cv::GIn(in, inLabels), cv::GOut(compactness, outLabels, centers)); - c.apply(cv::gin(in_vector, bestLabels), cv::gout(compact_gapi, labels_gapi, centers_gapi), - getCompileArgs()); - // OpenCV code ///////////////////////////////////////////////////////////// - compact_ocv = cv::kmeans(in_vector, K, labels_ocv, criteria, attempts, flags, centers_ocv); - // Comparison ////////////////////////////////////////////////////////////// - { - EXPECT_TRUE(compact_gapi == compact_ocv); - EXPECT_TRUE(compareVectorsAbsExact(labels_gapi, labels_ocv)); - EXPECT_TRUE(compareVectorsAbsExact(centers_gapi, centers_ocv)); - } + kmeansTestBody(in_vector, sz, type, K, flags, getCompileArgs()); } -TEST_P(KMeans3DNoInitTest, AccuracyTest) +TEST_P(KMeans3DTest, AccuracyTest) { const int amount = sz.height; - const cv::TermCriteria criteria(TermCriteria::MAX_ITER + TermCriteria::EPS, 30, 0); - const int attempts = 1; std::vector in_vector{}; - double compact_gapi = -1.; - std::vector labels_gapi{}; - std::vector centers_gapi{}; initPointsVectorRandU(amount, in_vector); - // G-API code ////////////////////////////////////////////////////////////// - cv::GArray in; - cv::GArray inLabels(std::vector{}); - cv::GOpaque compactness; - cv::GArray outLabels; - cv::GArray centers; - std::tie(compactness, outLabels, centers) = - cv::gapi::kmeans(in, K, inLabels, criteria, attempts, flags); - cv::GComputation c(cv::GIn(in), cv::GOut(compactness, outLabels, centers)); - c.apply(cv::gin(in_vector), cv::gout(compact_gapi, labels_gapi, centers_gapi), getCompileArgs()); - // Validation ////////////////////////////////////////////////////////////// - { - EXPECT_GE(compact_gapi, 0.); - EXPECT_EQ(labels_gapi.size(), static_cast(amount)); - EXPECT_EQ(centers_gapi.size(), static_cast(K)); - } -} - -TEST_P(KMeans3DInitTest, AccuracyTest) -{ - const int amount = sz.height; - const cv::TermCriteria criteria(TermCriteria::MAX_ITER + TermCriteria::EPS, 30, 0); - const int attempts = 1; - std::vector in_vector{}; - std::vector bestLabels(amount); - double compact_ocv = -1., compact_gapi = -1.; - std::vector labels_ocv{}, labels_gapi{}; - std::vector centers_ocv{}, centers_gapi{}; - initPointsVectorRandU(amount, in_vector); - cv::randu(bestLabels, 0, K); - labels_ocv = bestLabels; - // G-API code ////////////////////////////////////////////////////////////// - cv::GArray in; - cv::GArray inLabels; - cv::GOpaque compactness; - cv::GArray outLabels; - cv::GArray centers; - std::tie(compactness, outLabels, centers) = - cv::gapi::kmeans(in, K, inLabels, criteria, attempts, flags); - cv::GComputation c(cv::GIn(in, inLabels), cv::GOut(compactness, outLabels, centers)); - c.apply(cv::gin(in_vector, bestLabels), cv::gout(compact_gapi, labels_gapi, centers_gapi), - getCompileArgs()); - // OpenCV code ///////////////////////////////////////////////////////////// - compact_ocv = cv::kmeans(in_vector, K, labels_ocv, criteria, attempts, flags, centers_ocv); - // Comparison ////////////////////////////////////////////////////////////// - { - EXPECT_TRUE(compact_gapi == compact_ocv); - EXPECT_TRUE(compareVectorsAbsExact(labels_gapi, labels_ocv)); - EXPECT_TRUE(compareVectorsAbsExact(centers_gapi, centers_ocv)); - } + kmeansTestBody(in_vector, sz, type, K, flags, getCompileArgs()); } // PLEASE DO NOT PUT NEW ACCURACY TESTS BELOW THIS POINT! ////////////////////// diff --git a/modules/gapi/test/common/gapi_imgproc_tests_common.hpp b/modules/gapi/test/common/gapi_imgproc_tests_common.hpp index 9d83c37106..a5b9eeee4b 100644 --- a/modules/gapi/test/common/gapi_imgproc_tests_common.hpp +++ b/modules/gapi/test/common/gapi_imgproc_tests_common.hpp @@ -194,4 +194,4 @@ static void fitLineTestBody(const In& in, const cv::DistanceTypes distType, } } // namespace opencv_test -#endif // OPENCV_GAPI_VIDEO_TESTS_COMMON_HPP +#endif // OPENCV_GAPI_IMGPROC_TESTS_COMMON_HPP diff --git a/modules/gapi/test/common/gapi_tests_common.hpp b/modules/gapi/test/common/gapi_tests_common.hpp index 1da08442d0..151bf9ccc7 100644 --- a/modules/gapi/test/common/gapi_tests_common.hpp +++ b/modules/gapi/test/common/gapi_tests_common.hpp @@ -324,7 +324,7 @@ public: } template - inline void initPointRandU(cv::RNG& rng, T& pt) + inline void initPointRandU(cv::RNG& rng, T& pt) const { ::initPointRandU(rng, pt); } // Disable unreachable code warning for MSVS 2015 @@ -334,7 +334,7 @@ public: #endif // initialize std::vector>/std::vector> template class Pt> - void initPointsVectorRandU(const int sz_in, std::vector> &vec_) + void initPointsVectorRandU(const int sz_in, std::vector> &vec_) const { cv::RNG& rng = theRNG(); diff --git a/modules/gapi/test/cpu/gapi_core_tests_cpu.cpp b/modules/gapi/test/cpu/gapi_core_tests_cpu.cpp index 9f8c85fd45..5a06671ae3 100644 --- a/modules/gapi/test/cpu/gapi_core_tests_cpu.cpp +++ b/modules/gapi/test/cpu/gapi_core_tests_cpu.cpp @@ -2,7 +2,7 @@ // It is subject to the license terms in the LICENSE file found in the top-level directory // of this distribution and at http://opencv.org/license.html. // -// Copyright (C) 2018-2020 Intel Corporation +// Copyright (C) 2018-2021 Intel Corporation #include "../test_precomp.hpp" @@ -484,15 +484,16 @@ INSTANTIATE_TEST_CASE_P(NormalizeTestCPU, NormalizeTest, Values(NORM_MINMAX, NORM_INF, NORM_L1, NORM_L2), Values(-1, CV_8U, CV_16U, CV_16S, CV_32F))); -INSTANTIATE_TEST_CASE_P(KMeansNDNoInitTestCPU, KMeansNDNoInitTest, +INSTANTIATE_TEST_CASE_P(KMeansNDNoInitTestCPU, KMeansNDTest, Combine(Values(CV_32FC1), Values(cv::Size(2, 20)), Values(-1), Values(CORE_CPU), + Values(AbsTolerance(0.01).to_compare_obj()), Values(5), Values(cv::KMEANS_RANDOM_CENTERS, cv::KMEANS_PP_CENTERS))); -INSTANTIATE_TEST_CASE_P(KMeansNDInitTestCPU, KMeansNDInitTest, +INSTANTIATE_TEST_CASE_P(KMeansNDInitTestCPU, KMeansNDTest, Combine(Values(CV_32FC1, CV_32FC3), Values(cv::Size(1, 20), cv::Size(2, 20), @@ -504,7 +505,7 @@ INSTANTIATE_TEST_CASE_P(KMeansNDInitTestCPU, KMeansNDInitTest, Values(cv::KMEANS_RANDOM_CENTERS | cv::KMEANS_USE_INITIAL_LABELS, cv::KMEANS_PP_CENTERS | cv::KMEANS_USE_INITIAL_LABELS))); -INSTANTIATE_TEST_CASE_P(KMeansNDInitReverseTestCPU, KMeansNDInitTest, +INSTANTIATE_TEST_CASE_P(KMeansNDInitReverseTestCPU, KMeansNDTest, Combine(Values(CV_32FC3), Values(cv::Size(20, 1)), Values(-1), @@ -514,7 +515,7 @@ INSTANTIATE_TEST_CASE_P(KMeansNDInitReverseTestCPU, KMeansNDInitTest, Values(cv::KMEANS_RANDOM_CENTERS | cv::KMEANS_USE_INITIAL_LABELS, cv::KMEANS_PP_CENTERS | cv::KMEANS_USE_INITIAL_LABELS))); -INSTANTIATE_TEST_CASE_P(KMeans2DNoInitTestCPU, KMeans2DNoInitTest, +INSTANTIATE_TEST_CASE_P(KMeans2DNoInitTestCPU, KMeans2DTest, Combine(Values(-1), Values(cv::Size(-1, 20)), Values(-1), @@ -522,7 +523,7 @@ INSTANTIATE_TEST_CASE_P(KMeans2DNoInitTestCPU, KMeans2DNoInitTest, Values(5), Values(cv::KMEANS_RANDOM_CENTERS, cv::KMEANS_PP_CENTERS))); -INSTANTIATE_TEST_CASE_P(KMeans2DInitTestCPU, KMeans2DInitTest, +INSTANTIATE_TEST_CASE_P(KMeans2DInitTestCPU, KMeans2DTest, Combine(Values(-1), Values(cv::Size(-1, 720), cv::Size(-1, 20)), @@ -532,7 +533,7 @@ INSTANTIATE_TEST_CASE_P(KMeans2DInitTestCPU, KMeans2DInitTest, Values(cv::KMEANS_RANDOM_CENTERS | cv::KMEANS_USE_INITIAL_LABELS, cv::KMEANS_PP_CENTERS | cv::KMEANS_USE_INITIAL_LABELS))); -INSTANTIATE_TEST_CASE_P(KMeans3DNoInitTestCPU, KMeans3DNoInitTest, +INSTANTIATE_TEST_CASE_P(KMeans3DNoInitTestCPU, KMeans3DTest, Combine(Values(-1), Values(cv::Size(-1, 20)), Values(-1), @@ -540,7 +541,7 @@ INSTANTIATE_TEST_CASE_P(KMeans3DNoInitTestCPU, KMeans3DNoInitTest, Values(5), Values(cv::KMEANS_RANDOM_CENTERS, cv::KMEANS_PP_CENTERS))); -INSTANTIATE_TEST_CASE_P(KMeans3DInitTestCPU, KMeans3DInitTest, +INSTANTIATE_TEST_CASE_P(KMeans3DInitTestCPU, KMeans3DTest, Combine(Values(-1), Values(cv::Size(-1, 720), cv::Size(-1, 20)),