Merge pull request #19542 from OrestChura:oc/BGSub_ptest

[G-API]: Performance tests for BackgroundSubtractor

* Perf.Tests for BackgroundSubtractor kernel

* Fix CI

* Addressing comments

* Addressing a comment

* Test cycle and validation changes

* Addressing  comment

* Added assert
pull/19707/head
Orest Chura 4 years ago committed by GitHub
parent 12fa8d8444
commit c1a57a10a4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 3
      modules/gapi/perf/common/gapi_video_perf_tests.hpp
  2. 89
      modules/gapi/perf/common/gapi_video_perf_tests_inl.hpp
  3. 15
      modules/gapi/perf/cpu/gapi_video_perf_tests_cpu.cpp
  4. 109
      modules/gapi/test/common/gapi_video_tests_common.hpp
  5. 2
      modules/gapi/test/common/gapi_video_tests_inl.hpp
  6. 2
      modules/gapi/test/cpu/gapi_video_tests_cpu.cpp

@ -26,6 +26,9 @@ class OptFlowLKForPyrPerfTest : public TestPerfParams<tuple<std::string,int,tupl
class BuildPyr_CalcOptFlow_PipelinePerfTest : public TestPerfParams<tuple<std::string,int,int,bool,
cv::GCompileArgs>> {};
class BackgroundSubtractorPerfTest:
public TestPerfParams<tuple<cv::gapi::video::BackgroundSubtractorType, std::string,
bool, double, std::size_t, cv::GCompileArgs, CompareMats>> {};
} // opencv_test
#endif // OPENCV_GAPI_VIDEO_PERF_TESTS_HPP

@ -154,6 +154,95 @@ PERF_TEST_P_(BuildPyr_CalcOptFlow_PipelinePerfTest, TestPerformance)
//------------------------------------------------------------------------------
#ifdef HAVE_OPENCV_VIDEO
PERF_TEST_P_(BackgroundSubtractorPerfTest, TestPerformance)
{
namespace gvideo = cv::gapi::video;
initTestDataPath();
gvideo::BackgroundSubtractorType opType;
std::string filePath = "";
bool detectShadows = false;
double learningRate = -1.;
std::size_t testNumFrames = 0;
cv::GCompileArgs compileArgs;
CompareMats cmpF;
std::tie(opType, filePath, detectShadows, learningRate, testNumFrames,
compileArgs, cmpF) = GetParam();
const int histLength = 500;
double thr = -1;
switch (opType)
{
case gvideo::TYPE_BS_MOG2:
{
thr = 16.;
break;
}
case gvideo::TYPE_BS_KNN:
{
thr = 400.;
break;
}
default:
FAIL() << "unsupported type of BackgroundSubtractor";
}
const gvideo::BackgroundSubtractorParams bsp(opType, histLength, thr, detectShadows,
learningRate);
// Retrieving frames
std::vector<cv::Mat> frames;
frames.reserve(testNumFrames);
{
cv::Mat frame;
cv::VideoCapture cap;
if (!cap.open(findDataFile(filePath)))
throw SkipTestException("Video file can not be opened");
for (std::size_t i = 0; i < testNumFrames && cap.read(frame); i++)
{
frames.push_back(frame);
}
}
GAPI_Assert(testNumFrames == frames.size() && "Can't read required number of frames");
// G-API graph declaration
cv::GMat in;
cv::GMat out = cv::gapi::BackgroundSubtractor(in, bsp);
cv::GComputation c(cv::GIn(in), cv::GOut(out));
auto cc = c.compile(cv::descr_of(frames[0]), std::move(compileArgs));
cv::Mat gapiForeground;
TEST_CYCLE()
{
cc.prepareForNewStream();
for (size_t i = 0; i < testNumFrames; i++)
{
cc(cv::gin(frames[i]), cv::gout(gapiForeground));
}
}
// OpenCV Background Subtractor declaration
cv::Ptr<cv::BackgroundSubtractor> pOCVBackSub;
if (opType == gvideo::TYPE_BS_MOG2)
pOCVBackSub = cv::createBackgroundSubtractorMOG2(histLength, thr, detectShadows);
else if (opType == gvideo::TYPE_BS_KNN)
pOCVBackSub = cv::createBackgroundSubtractorKNN(histLength, thr, detectShadows);
cv::Mat ocvForeground;
for (size_t i = 0; i < testNumFrames; i++)
{
pOCVBackSub->apply(frames[i], ocvForeground, learningRate);
}
// Validation
EXPECT_TRUE(cmpF(gapiForeground, ocvForeground));
SANITY_CHECK_NOTHING();
}
//------------------------------------------------------------------------------
#endif // HAVE_OPENCV_VIDEO
} // opencv_test
#endif // OPENCV_GAPI_VIDEO_PERF_TESTS_INL_HPP

@ -70,7 +70,7 @@ INSTANTIATE_TEST_CASE_MACRO_P(WITH_VIDEO(OptFlowLKForPyrPerfTestCPU), OptFlowLKF
Values(cv::TermCriteria(cv::TermCriteria::COUNT |
cv::TermCriteria::EPS,
30, 0.01)),
Values(true, false),
testing::Bool(),
Values(cv::compile_args(VIDEO_CPU))));
INSTANTIATE_TEST_CASE_MACRO_P(WITH_VIDEO(OptFlowLKInternalPerfTestCPU),
@ -90,7 +90,7 @@ INSTANTIATE_TEST_CASE_MACRO_P(WITH_VIDEO(BuildPyr_CalcOptFlow_PipelinePerfTestCP
Combine(Values("cv/optflow/frames/1080p_%02d.png"),
Values(7, 11),
Values(1000),
Values(true, false),
testing::Bool(),
Values(cv::compile_args(VIDEO_CPU))));
INSTANTIATE_TEST_CASE_MACRO_P(WITH_VIDEO(BuildPyr_CalcOptFlow_PipelineInternalTestPerfCPU),
@ -100,4 +100,15 @@ INSTANTIATE_TEST_CASE_MACRO_P(WITH_VIDEO(BuildPyr_CalcOptFlow_PipelineInternalTe
Values(3),
Values(true),
Values(cv::compile_args(VIDEO_CPU))));
INSTANTIATE_TEST_CASE_MACRO_P(WITH_VIDEO(BackgroundSubtractorPerfTestCPU),
BackgroundSubtractorPerfTest,
Combine(Values(cv::gapi::video::TYPE_BS_MOG2,
cv::gapi::video::TYPE_BS_KNN),
Values("cv/video/768x576.avi", "cv/video/1920x1080.avi"),
testing::Bool(),
Values(0., 0.5, 1.),
Values(5),
Values(cv::compile_args(VIDEO_CPU)),
Values(AbsExact().to_compare_obj())));
} // opencv_test

@ -15,7 +15,6 @@
#endif // HAVE_OPENCV_VIDEO
namespace opencv_test
{
namespace
@ -128,6 +127,54 @@ struct OptFlowLKTestParams
int flags = 0;
};
inline void compareOutputPyramids(const BuildOpticalFlowPyramidTestOutput& outGAPI,
const BuildOpticalFlowPyramidTestOutput& outOCV)
{
GAPI_Assert(outGAPI.maxLevel == outOCV.maxLevel);
GAPI_Assert(outOCV.maxLevel >= 0);
const size_t maxLevel = static_cast<size_t>(outOCV.maxLevel);
for (size_t i = 0; i <= maxLevel; i++)
{
EXPECT_TRUE(AbsExact().to_compare_f()(outGAPI.pyramid[i], outOCV.pyramid[i]));
}
}
template <typename Elem>
inline bool compareVectorsAbsExactForOptFlow(const std::vector<Elem>& outGAPI,
const std::vector<Elem>& outOCV)
{
return AbsExactVector<Elem>().to_compare_f()(outGAPI, outOCV);
}
inline void compareOutputsOptFlow(const OptFlowLKTestOutput& outGAPI,
const OptFlowLKTestOutput& outOCV)
{
EXPECT_TRUE(compareVectorsAbsExactForOptFlow(outGAPI.nextPoints, outOCV.nextPoints));
EXPECT_TRUE(compareVectorsAbsExactForOptFlow(outGAPI.statuses, outOCV.statuses));
EXPECT_TRUE(compareVectorsAbsExactForOptFlow(outGAPI.errors, outOCV.errors));
}
inline std::ostream& operator<<(std::ostream& os, const cv::TermCriteria& criteria)
{
os << "{";
switch (criteria.type) {
case cv::TermCriteria::COUNT:
os << "COUNT; ";
break;
case cv::TermCriteria::EPS:
os << "EPS; ";
break;
case cv::TermCriteria::COUNT | cv::TermCriteria::EPS:
os << "COUNT | EPS; ";
break;
default:
os << "TypeUndefined; ";
break;
};
return os << criteria.maxCount << "; " << criteria.epsilon <<"}";
}
#ifdef HAVE_OPENCV_VIDEO
inline GComputation runOCVnGAPIBuildOptFlowPyramid(TestFunctional& testInst,
@ -390,54 +437,24 @@ inline GComputation runOCVnGAPIOptFlowPipeline(TestFunctional&,
#endif // HAVE_OPENCV_VIDEO
inline void compareOutputPyramids(const BuildOpticalFlowPyramidTestOutput& outGAPI,
const BuildOpticalFlowPyramidTestOutput& outOCV)
{
GAPI_Assert(outGAPI.maxLevel == outOCV.maxLevel);
GAPI_Assert(outOCV.maxLevel >= 0);
size_t maxLevel = static_cast<size_t>(outOCV.maxLevel);
for (size_t i = 0; i <= maxLevel; i++)
{
EXPECT_TRUE(AbsExact().to_compare_f()(outGAPI.pyramid[i], outOCV.pyramid[i]));
}
}
template <typename Elem>
inline bool compareVectorsAbsExactForOptFlow(std::vector<Elem> outGAPI, std::vector<Elem> outOCV)
{
return AbsExactVector<Elem>().to_compare_f()(outGAPI, outOCV);
}
} // namespace
} // namespace opencv_test
inline void compareOutputsOptFlow(const OptFlowLKTestOutput& outGAPI,
const OptFlowLKTestOutput& outOCV)
// Note: namespace must match the namespace of the type of the printed object
namespace cv { namespace gapi { namespace video
{
EXPECT_TRUE(compareVectorsAbsExactForOptFlow(outGAPI.nextPoints, outOCV.nextPoints));
EXPECT_TRUE(compareVectorsAbsExactForOptFlow(outGAPI.statuses, outOCV.statuses));
EXPECT_TRUE(compareVectorsAbsExactForOptFlow(outGAPI.errors, outOCV.errors));
}
inline std::ostream& operator<<(std::ostream& os, const cv::TermCriteria& criteria)
inline std::ostream& operator<<(std::ostream& os, const BackgroundSubtractorType op)
{
os << "{";
switch (criteria.type) {
case cv::TermCriteria::COUNT:
os << "COUNT; ";
break;
case cv::TermCriteria::EPS:
os << "EPS; ";
break;
case cv::TermCriteria::COUNT | cv::TermCriteria::EPS:
os << "COUNT | EPS; ";
break;
default:
os << "TypeUndefined; ";
break;
};
return os << criteria.maxCount << "; " << criteria.epsilon <<"}";
#define CASE(v) case BackgroundSubtractorType::v: os << #v; break
switch (op)
{
CASE(TYPE_BS_MOG2);
CASE(TYPE_BS_KNN);
default: GAPI_Assert(false && "unknown BackgroundSubtractor type");
}
#undef CASE
return os;
}
} // namespace
} // namespace opencv_test
}}} // namespace cv::gapi::video
#endif // OPENCV_GAPI_VIDEO_TESTS_COMMON_HPP

@ -111,7 +111,7 @@ TEST_P(BackgroundSubtractorTest, AccuracyTest)
auto gapiBackSub = c.compileStreaming(getCompileArgs());
// Testing G-API Background Substractor in streaming mode
auto path = findDataFile("cv/video/768x576.avi");
const auto path = findDataFile(filePath);
try
{
gapiBackSub.setSource(gapi::wip::make_src<cv::gapi::wip::GCaptureSource>(path));

@ -107,7 +107,7 @@ INSTANTIATE_TEST_CASE_MACRO_P(WITH_VIDEO(BackgroundSubtractorTestCPU),
std::make_tuple(cv::gapi::video::TYPE_BS_KNN, 400),
std::make_tuple(cv::gapi::video::TYPE_BS_KNN, 200)),
Values(500, 50),
Values(true, false),
testing::Bool(),
Values(-1, 0, 0.5, 1),
Values("cv/video/768x576.avi"),
Values(3)));

Loading…
Cancel
Save