From e528f39deff8f1b31ff53c9e3676e07b8227d0ed Mon Sep 17 00:00:00 2001 From: Jin Ma Date: Wed, 4 Sep 2013 14:37:46 +0800 Subject: [PATCH 1/5] Added the performance test for mog and mog2. --- modules/ocl/perf/perf_bgfg.cpp | 337 +++++++++++++++++++++++++++++++++ 1 file changed, 337 insertions(+) create mode 100644 modules/ocl/perf/perf_bgfg.cpp diff --git a/modules/ocl/perf/perf_bgfg.cpp b/modules/ocl/perf/perf_bgfg.cpp new file mode 100644 index 0000000000..188dffa132 --- /dev/null +++ b/modules/ocl/perf/perf_bgfg.cpp @@ -0,0 +1,337 @@ +/*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. +// +// +// License Agreement +// For Open Source Computer Vision Library +// +// Copyright (C) 2010-2012, Multicoreware, Inc., all rights reserved. +// Copyright (C) 2010-2012, Advanced Micro Devices, Inc., all rights reserved. +// Third party copyrights are property of their respective owners. +// +// @Authors +// Fangfang Bai, fangfang@multicorewareinc.com +// Jin Ma, jin@multicorewareinc.com +// +// 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 +// and/or other oclMaterials provided with the distribution. +// +// * The name of the copyright holders may not be used to endorse or promote products +// derived from this software without specific prior written permission. +// +// This software is provided by the copyright holders and contributors as is and +// 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*/ +#include "perf_precomp.hpp" + +///////////// PyrLKOpticalFlow //////////////////////// + +using namespace perf; +using std::tr1::get; +using std::tr1::tuple; +using std::tr1::make_tuple; + +#if defined(HAVE_XINE) || \ + defined(HAVE_GSTREAMER) || \ + defined(HAVE_QUICKTIME) || \ + defined(HAVE_AVFOUNDATION) || \ + defined(HAVE_FFMPEG) || \ + defined(WIN32) /* assume that we have ffmpeg */ + +# define BUILD_WITH_VIDEO_INPUT_SUPPORT 1 +#else +# define BUILD_WITH_VIDEO_INPUT_SUPPORT 0 +#endif + +#if BUILD_WITH_VIDEO_INPUT_SUPPORT + +typedef tuple VideoMOGParamType; +typedef TestBaseWithParam VideoMOGFixture; + +PERF_TEST_P(VideoMOGFixture, Video_MOG, + ::testing::Combine(::testing::Values("768x576.avi", "1920x1080.avi"), + ::testing::Values(1, 3), + ::testing::Values(0.0, 0.01))) +{ + VideoMOGParamType params = GetParam(); + + const string inputFile = perf::TestBase::getDataPath(get<0>(params)); + const int cn = get<1>(params); + const float learningRate = static_cast(get<2>(params)); + + cv::VideoCapture cap(inputFile); + ASSERT_TRUE(cap.isOpened()); + + cv::Mat frame; + + cap >> frame; + ASSERT_FALSE(frame.empty()); + + cv::Mat temp; + if (cn == 1) + cv::cvtColor(frame, temp, cv::COLOR_BGR2GRAY); + else + cv::cvtColor(frame, temp, cv::COLOR_BGR2BGRA); + cv::swap(temp, frame); + + if(RUN_PLAIN_IMPL) + { + cv::BackgroundSubtractorMOG mog; + cv::Mat foreground; + + mog(frame, foreground, learningRate); + + for (int i = 0; i < 10; ++i) + { + cap >> frame; + ASSERT_FALSE(frame.empty()); + + cv::Mat temp; + if (cn == 1) + cv::cvtColor(frame, temp, cv::COLOR_BGR2GRAY); + else + cv::cvtColor(frame, temp, cv::COLOR_BGR2BGRA); + + cv::swap(temp, frame); + + TEST_CYCLE() + mog(frame, foreground, learningRate); + + SANITY_CHECK(foreground); + } + }else if(RUN_OCL_IMPL) + { + cv::ocl::oclMat d_frame(frame); + cv::ocl::MOG d_mog; + cv::ocl::oclMat foreground; + cv::Mat foreground_h; + + d_mog(d_frame, foreground, learningRate); + + for (int i = 0; i < 10; ++i) + { + cap >> frame; + ASSERT_FALSE(frame.empty()); + + cv::Mat temp; + if (cn == 1) + cv::cvtColor(frame, temp, cv::COLOR_BGR2GRAY); + else + cv::cvtColor(frame, temp, cv::COLOR_BGR2BGRA); + cv::swap(temp, frame); + + d_frame.upload(frame); + + OCL_TEST_CYCLE() + d_mog(d_frame, foreground, learningRate); + + foreground.download(foreground_h); + SANITY_CHECK(foreground_h); + } + }else + OCL_PERF_ELSE +} +#endif + +#if BUILD_WITH_VIDEO_INPUT_SUPPORT + +typedef tuple VideoMOG2ParamType; +typedef TestBaseWithParam VideoMOG2Fixture; + +PERF_TEST_P(VideoMOG2Fixture, Video_MOG2, + ::testing::Combine(::testing::Values("768x576.avi", "1920x1080.avi"), + ::testing::Values(1, 3))) +{ + VideoMOG2ParamType params = GetParam(); + + const string inputFile = perf::TestBase::getDataPath(get<0>(params)); + const int cn = get<1>(params); + + cv::VideoCapture cap(inputFile); + ASSERT_TRUE(cap.isOpened()); + + cv::Mat frame; + + cap >> frame; + ASSERT_FALSE(frame.empty()); + + cv::Mat temp; + if (cn == 1) + cv::cvtColor(frame, temp, cv::COLOR_BGR2GRAY); + else + cv::cvtColor(frame, temp, cv::COLOR_BGR2BGRA); + cv::swap(temp, frame); + + if(RUN_PLAIN_IMPL) + { + cv::BackgroundSubtractorMOG2 mog2; + cv::Mat foreground; + + mog2.set("detectShadows", false); + mog2(frame, foreground); + + for (int i = 0; i < 10; ++i) + { + cap >> frame; + ASSERT_FALSE(frame.empty()); + + cv::Mat temp; + if (cn == 1) + cv::cvtColor(frame, temp, cv::COLOR_BGR2GRAY); + else + cv::cvtColor(frame, temp, cv::COLOR_BGR2BGRA); + + cv::swap(temp, frame); + + TEST_CYCLE() + mog2(frame, foreground); + + SANITY_CHECK(foreground); + } + }else if(RUN_OCL_IMPL) + { + cv::ocl::oclMat d_frame(frame); + cv::ocl::MOG2 d_mog2; + cv::ocl::oclMat foreground; + cv::Mat foreground_h; + + d_mog2(d_frame, foreground); + + for (int i = 0; i < 10; ++i) + { + cap >> frame; + ASSERT_FALSE(frame.empty()); + + cv::Mat temp; + if (cn == 1) + cv::cvtColor(frame, temp, cv::COLOR_BGR2GRAY); + else + cv::cvtColor(frame, temp, cv::COLOR_BGR2BGRA); + cv::swap(temp, frame); + + d_frame.upload(frame); + + OCL_TEST_CYCLE() + d_mog2(d_frame, foreground); + + foreground.download(foreground_h); + SANITY_CHECK(foreground_h); + } + }else + OCL_PERF_ELSE +} +#endif + +#if BUILD_WITH_VIDEO_INPUT_SUPPORT + +typedef TestBaseWithParam Video_MOG2GetBackgroundImage; + +PERF_TEST_P(Video_MOG2GetBackgroundImage, Video_MOG2, + ::testing::Combine(::testing::Values("768x576.avi", "1920x1080.avi"), + ::testing::Values(1, 3))) +{ + VideoMOG2ParamType params = GetParam(); + + const string inputFile = perf::TestBase::getDataPath(get<0>(params)); + const int cn = get<1>(params); + + cv::VideoCapture cap(inputFile); + ASSERT_TRUE(cap.isOpened()); + + cv::Mat frame; + + cap >> frame; + ASSERT_FALSE(frame.empty()); + + cv::Mat temp; + if (cn == 1) + cv::cvtColor(frame, temp, cv::COLOR_BGR2GRAY); + else + cv::cvtColor(frame, temp, cv::COLOR_BGR2BGRA); + cv::swap(temp, frame); + + if(RUN_PLAIN_IMPL) + { + cv::BackgroundSubtractorMOG2 mog2; + cv::Mat foreground; + + mog2.set("detectShadows", false); + mog2(frame, foreground); + + for (int i = 0; i < 10; ++i) + { + cap >> frame; + ASSERT_FALSE(frame.empty()); + + cv::Mat temp; + if (cn == 1) + cv::cvtColor(frame, temp, cv::COLOR_BGR2GRAY); + else + cv::cvtColor(frame, temp, cv::COLOR_BGR2BGRA); + + cv::swap(temp, frame); + + TEST_CYCLE() + mog2(frame, foreground); + } + cv::Mat background; + TEST_CYCLE() + mog2.getBackgroundImage(background); + + SANITY_CHECK(background); + }else if(RUN_OCL_IMPL) + { + cv::ocl::oclMat d_frame(frame); + cv::ocl::MOG2 d_mog2; + cv::ocl::oclMat foreground; + cv::Mat background_h; + + d_mog2(d_frame, foreground); + + for (int i = 0; i < 10; ++i) + { + cap >> frame; + ASSERT_FALSE(frame.empty()); + + cv::Mat temp; + if (cn == 1) + cv::cvtColor(frame, temp, cv::COLOR_BGR2GRAY); + else + cv::cvtColor(frame, temp, cv::COLOR_BGR2BGRA); + cv::swap(temp, frame); + + d_frame.upload(frame); + d_mog2(d_frame, foreground); + } + cv::ocl::oclMat background; + OCL_TEST_CYCLE() + d_mog2.getBackgroundImage(background); + + background.download(background_h); + SANITY_CHECK(background_h); + }else + OCL_PERF_ELSE +} +#endif + From dd73016c8ba1b70dd371c44995f51f3c206c2636 Mon Sep 17 00:00:00 2001 From: Jin Ma Date: Wed, 4 Sep 2013 15:00:36 +0800 Subject: [PATCH 2/5] Removed whitespace. --- modules/ocl/perf/perf_bgfg.cpp | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/modules/ocl/perf/perf_bgfg.cpp b/modules/ocl/perf/perf_bgfg.cpp index 188dffa132..185f704b3c 100644 --- a/modules/ocl/perf/perf_bgfg.cpp +++ b/modules/ocl/perf/perf_bgfg.cpp @@ -79,7 +79,7 @@ PERF_TEST_P(VideoMOGFixture, Video_MOG, const string inputFile = perf::TestBase::getDataPath(get<0>(params)); const int cn = get<1>(params); const float learningRate = static_cast(get<2>(params)); - + cv::VideoCapture cap(inputFile); ASSERT_TRUE(cap.isOpened()); @@ -296,7 +296,7 @@ PERF_TEST_P(Video_MOG2GetBackgroundImage, Video_MOG2, mog2(frame, foreground); } cv::Mat background; - TEST_CYCLE() + TEST_CYCLE() mog2.getBackgroundImage(background); SANITY_CHECK(background); @@ -333,5 +333,4 @@ PERF_TEST_P(Video_MOG2GetBackgroundImage, Video_MOG2, }else OCL_PERF_ELSE } -#endif - +#endif \ No newline at end of file From 1366df8250459821bf7252e4f4123e961cb3067b Mon Sep 17 00:00:00 2001 From: Jin Ma Date: Thu, 5 Sep 2013 19:33:27 +0800 Subject: [PATCH 3/5] MOG&MOG2: Modified the performance test according to the feedback of the community. --- modules/ocl/perf/perf_bgfg.cpp | 270 +++++++++++++-------------------- 1 file changed, 103 insertions(+), 167 deletions(-) diff --git a/modules/ocl/perf/perf_bgfg.cpp b/modules/ocl/perf/perf_bgfg.cpp index 185f704b3c..619145baef 100644 --- a/modules/ocl/perf/perf_bgfg.cpp +++ b/modules/ocl/perf/perf_bgfg.cpp @@ -44,33 +44,62 @@ // //M*/ #include "perf_precomp.hpp" - -///////////// PyrLKOpticalFlow //////////////////////// - using namespace perf; -using std::tr1::get; -using std::tr1::tuple; -using std::tr1::make_tuple; - +using namespace std; +using namespace cv::ocl; +using namespace cv; #if defined(HAVE_XINE) || \ defined(HAVE_GSTREAMER) || \ defined(HAVE_QUICKTIME) || \ defined(HAVE_AVFOUNDATION) || \ defined(HAVE_FFMPEG) || \ - defined(WIN32) /* assume that we have ffmpeg */ + defined(WIN32) # define BUILD_WITH_VIDEO_INPUT_SUPPORT 1 #else # define BUILD_WITH_VIDEO_INPUT_SUPPORT 0 #endif +static void cvtFrameFmt(vector& input, vector& output, int output_cn) +{ + for(int i = 0; i< (int)(input.size()); i++) + { + if(output_cn == 1) + cvtColor(input[i], output[i], COLOR_RGB2GRAY); + else + cvtColor(input[i], output[i], COLOR_RGB2RGBA); + } +} + +static void prepareData(VideoCapture& cap, int cn, vector& frame_buffer, vector& frame_buffer_ocl) +{ + cv::Mat frame; + std::vector frame_buffer_init; + int nFrame = (int)frame_buffer.size(); + for(int i = 0; i < nFrame; i++) + { + cap >> frame; + ASSERT_FALSE(frame.empty()); + frame_buffer_init.push_back(frame); + } + + if(cn == 1) + cvtFrameFmt(frame_buffer_init, frame_buffer, 1); + else + frame_buffer = frame_buffer_init; + + for(int i = 0; i < nFrame; i++) + frame_buffer_ocl.push_back(cv::ocl::oclMat(frame_buffer[i])); +} + +///////////// MOG //////////////////////// #if BUILD_WITH_VIDEO_INPUT_SUPPORT typedef tuple VideoMOGParamType; typedef TestBaseWithParam VideoMOGFixture; -PERF_TEST_P(VideoMOGFixture, Video_MOG, - ::testing::Combine(::testing::Values("768x576.avi", "1920x1080.avi"), +PERF_TEST_P(VideoMOGFixture, MOG, + ::testing::Combine(::testing::Values("gpu/video/768x576.avi", "gpu/video/1920x1080.avi"), ::testing::Values(1, 3), ::testing::Values(0.0, 0.01))) { @@ -80,108 +109,72 @@ PERF_TEST_P(VideoMOGFixture, Video_MOG, const int cn = get<1>(params); const float learningRate = static_cast(get<2>(params)); + const int nFrame = 5; + + Mat foreground_cpu; + std::vector frame_buffer(nFrame); + std::vector frame_buffer_ocl; + cv::VideoCapture cap(inputFile); ASSERT_TRUE(cap.isOpened()); - cv::Mat frame; - - cap >> frame; - ASSERT_FALSE(frame.empty()); - - cv::Mat temp; - if (cn == 1) - cv::cvtColor(frame, temp, cv::COLOR_BGR2GRAY); - else - cv::cvtColor(frame, temp, cv::COLOR_BGR2BGRA); - cv::swap(temp, frame); + prepareData(cap, cn, frame_buffer, frame_buffer_ocl); if(RUN_PLAIN_IMPL) { cv::BackgroundSubtractorMOG mog; cv::Mat foreground; - mog(frame, foreground, learningRate); - - for (int i = 0; i < 10; ++i) + TEST_CYCLE() { - cap >> frame; - ASSERT_FALSE(frame.empty()); - - cv::Mat temp; - if (cn == 1) - cv::cvtColor(frame, temp, cv::COLOR_BGR2GRAY); - else - cv::cvtColor(frame, temp, cv::COLOR_BGR2BGRA); - - cv::swap(temp, frame); - - TEST_CYCLE() - mog(frame, foreground, learningRate); - - SANITY_CHECK(foreground); + for (int i = 0; i < nFrame; i++) + { + mog(frame_buffer[i], foreground, learningRate); + } } + SANITY_CHECK(foreground); }else if(RUN_OCL_IMPL) { - cv::ocl::oclMat d_frame(frame); cv::ocl::MOG d_mog; cv::ocl::oclMat foreground; cv::Mat foreground_h; - - d_mog(d_frame, foreground, learningRate); - - for (int i = 0; i < 10; ++i) + OCL_TEST_CYCLE() { - cap >> frame; - ASSERT_FALSE(frame.empty()); - - cv::Mat temp; - if (cn == 1) - cv::cvtColor(frame, temp, cv::COLOR_BGR2GRAY); - else - cv::cvtColor(frame, temp, cv::COLOR_BGR2BGRA); - cv::swap(temp, frame); - - d_frame.upload(frame); - - OCL_TEST_CYCLE() - d_mog(d_frame, foreground, learningRate); - - foreground.download(foreground_h); - SANITY_CHECK(foreground_h); + for (int i = 0; i < nFrame; ++i) + { + d_mog(frame_buffer_ocl[i], foreground, learningRate); + } } + foreground.download(foreground_h); + SANITY_CHECK(foreground_h); }else OCL_PERF_ELSE } #endif +///////////// MOG2 //////////////////////// #if BUILD_WITH_VIDEO_INPUT_SUPPORT typedef tuple VideoMOG2ParamType; typedef TestBaseWithParam VideoMOG2Fixture; -PERF_TEST_P(VideoMOG2Fixture, Video_MOG2, - ::testing::Combine(::testing::Values("768x576.avi", "1920x1080.avi"), +PERF_TEST_P(VideoMOG2Fixture, MOG2, + ::testing::Combine(::testing::Values("gpu/video/768x576.avi", "gpu/video/1920x1080.avi"), ::testing::Values(1, 3))) { VideoMOG2ParamType params = GetParam(); const string inputFile = perf::TestBase::getDataPath(get<0>(params)); const int cn = get<1>(params); + int nFrame = 5; + + std::vector frame_buffer(nFrame); + std::vector frame_buffer_ocl; cv::VideoCapture cap(inputFile); ASSERT_TRUE(cap.isOpened()); - cv::Mat frame; - - cap >> frame; - ASSERT_FALSE(frame.empty()); - - cv::Mat temp; - if (cn == 1) - cv::cvtColor(frame, temp, cv::COLOR_BGR2GRAY); - else - cv::cvtColor(frame, temp, cv::COLOR_BGR2BGRA); - cv::swap(temp, frame); + prepareData(cap, cn, frame_buffer, frame_buffer_ocl); if(RUN_PLAIN_IMPL) { @@ -189,145 +182,88 @@ PERF_TEST_P(VideoMOG2Fixture, Video_MOG2, cv::Mat foreground; mog2.set("detectShadows", false); - mog2(frame, foreground); - for (int i = 0; i < 10; ++i) + TEST_CYCLE() { - cap >> frame; - ASSERT_FALSE(frame.empty()); - - cv::Mat temp; - if (cn == 1) - cv::cvtColor(frame, temp, cv::COLOR_BGR2GRAY); - else - cv::cvtColor(frame, temp, cv::COLOR_BGR2BGRA); - - cv::swap(temp, frame); - - TEST_CYCLE() - mog2(frame, foreground); - - SANITY_CHECK(foreground); + for (int i = 0; i < nFrame; i++) + { + mog2(frame_buffer[i], foreground); + } } + SANITY_CHECK(foreground); }else if(RUN_OCL_IMPL) { - cv::ocl::oclMat d_frame(frame); cv::ocl::MOG2 d_mog2; cv::ocl::oclMat foreground; cv::Mat foreground_h; - d_mog2(d_frame, foreground); - - for (int i = 0; i < 10; ++i) + OCL_TEST_CYCLE() { - cap >> frame; - ASSERT_FALSE(frame.empty()); - - cv::Mat temp; - if (cn == 1) - cv::cvtColor(frame, temp, cv::COLOR_BGR2GRAY); - else - cv::cvtColor(frame, temp, cv::COLOR_BGR2BGRA); - cv::swap(temp, frame); - - d_frame.upload(frame); - - OCL_TEST_CYCLE() - d_mog2(d_frame, foreground); - - foreground.download(foreground_h); - SANITY_CHECK(foreground_h); + for (int i = 0; i < nFrame; i++) + { + d_mog2(frame_buffer_ocl[i], foreground); + } } + foreground.download(foreground_h); + SANITY_CHECK(foreground_h); }else OCL_PERF_ELSE } #endif +///////////// MOG2_GetBackgroundImage ////////////////// #if BUILD_WITH_VIDEO_INPUT_SUPPORT typedef TestBaseWithParam Video_MOG2GetBackgroundImage; -PERF_TEST_P(Video_MOG2GetBackgroundImage, Video_MOG2, - ::testing::Combine(::testing::Values("768x576.avi", "1920x1080.avi"), +PERF_TEST_P(Video_MOG2GetBackgroundImage, MOG2, + ::testing::Combine(::testing::Values("gpu/video/768x576.avi", "gpu/video/1920x1080.avi"), ::testing::Values(1, 3))) { VideoMOG2ParamType params = GetParam(); const string inputFile = perf::TestBase::getDataPath(get<0>(params)); const int cn = get<1>(params); + int nFrame = 5; + + std::vector frame_buffer(nFrame); + std::vector frame_buffer_ocl; cv::VideoCapture cap(inputFile); ASSERT_TRUE(cap.isOpened()); - cv::Mat frame; - - cap >> frame; - ASSERT_FALSE(frame.empty()); - - cv::Mat temp; - if (cn == 1) - cv::cvtColor(frame, temp, cv::COLOR_BGR2GRAY); - else - cv::cvtColor(frame, temp, cv::COLOR_BGR2BGRA); - cv::swap(temp, frame); + prepareData(cap, cn, frame_buffer, frame_buffer_ocl); if(RUN_PLAIN_IMPL) { cv::BackgroundSubtractorMOG2 mog2; cv::Mat foreground; - - mog2.set("detectShadows", false); - mog2(frame, foreground); - - for (int i = 0; i < 10; ++i) - { - cap >> frame; - ASSERT_FALSE(frame.empty()); - - cv::Mat temp; - if (cn == 1) - cv::cvtColor(frame, temp, cv::COLOR_BGR2GRAY); - else - cv::cvtColor(frame, temp, cv::COLOR_BGR2BGRA); - - cv::swap(temp, frame); - - TEST_CYCLE() - mog2(frame, foreground); - } cv::Mat background; + mog2.set("detectShadows", false); TEST_CYCLE() + { + for (int i = 0; i < nFrame; i++) + { + mog2(frame_buffer[i], foreground); + } mog2.getBackgroundImage(background); - + } SANITY_CHECK(background); }else if(RUN_OCL_IMPL) { - cv::ocl::oclMat d_frame(frame); cv::ocl::MOG2 d_mog2; cv::ocl::oclMat foreground; cv::Mat background_h; - - d_mog2(d_frame, foreground); - - for (int i = 0; i < 10; ++i) - { - cap >> frame; - ASSERT_FALSE(frame.empty()); - - cv::Mat temp; - if (cn == 1) - cv::cvtColor(frame, temp, cv::COLOR_BGR2GRAY); - else - cv::cvtColor(frame, temp, cv::COLOR_BGR2BGRA); - cv::swap(temp, frame); - - d_frame.upload(frame); - d_mog2(d_frame, foreground); - } cv::ocl::oclMat background; - OCL_TEST_CYCLE() - d_mog2.getBackgroundImage(background); + OCL_TEST_CYCLE() + { + for (int i = 0; i < nFrame; i++) + { + d_mog2(frame_buffer_ocl[i], foreground); + } + d_mog2.getBackgroundImage(background); + } background.download(background_h); SANITY_CHECK(background_h); }else From 172242a94e6a9ab6ee20912c3f7899e900e7664f Mon Sep 17 00:00:00 2001 From: Jin Ma Date: Fri, 6 Sep 2013 13:39:41 +0800 Subject: [PATCH 4/5] Revised performance test according to the feedback of the community. --- modules/ocl/perf/perf_bgfg.cpp | 103 +++++++++++++++++++-------------- 1 file changed, 58 insertions(+), 45 deletions(-) diff --git a/modules/ocl/perf/perf_bgfg.cpp b/modules/ocl/perf/perf_bgfg.cpp index 619145baef..ec96c90070 100644 --- a/modules/ocl/perf/perf_bgfg.cpp +++ b/modules/ocl/perf/perf_bgfg.cpp @@ -48,6 +48,8 @@ using namespace perf; using namespace std; using namespace cv::ocl; using namespace cv; +using std::tr1::tuple; +using std::tr1::get; #if defined(HAVE_XINE) || \ defined(HAVE_GSTREAMER) || \ defined(HAVE_QUICKTIME) || \ @@ -60,6 +62,7 @@ using namespace cv; # define BUILD_WITH_VIDEO_INPUT_SUPPORT 0 #endif +#if BUILD_WITH_VIDEO_INPUT_SUPPORT static void cvtFrameFmt(vector& input, vector& output, int output_cn) { for(int i = 0; i< (int)(input.size()); i++) @@ -70,8 +73,8 @@ static void cvtFrameFmt(vector& input, vector& output, int output_cn) cvtColor(input[i], output[i], COLOR_RGB2RGBA); } } - -static void prepareData(VideoCapture& cap, int cn, vector& frame_buffer, vector& frame_buffer_ocl) +//prepare data for CPU +static void prepareData(VideoCapture& cap, int cn, vector& frame_buffer) { cv::Mat frame; std::vector frame_buffer_init; @@ -87,11 +90,14 @@ static void prepareData(VideoCapture& cap, int cn, vector& frame_buffer, ve cvtFrameFmt(frame_buffer_init, frame_buffer, 1); else frame_buffer = frame_buffer_init; - - for(int i = 0; i < nFrame; i++) +} +//copy CPU data to GPU +static void prepareData(vector& frame_buffer, vector& frame_buffer_ocl) +{ + for(int i = 0; i < (int)frame_buffer.size(); i++) frame_buffer_ocl.push_back(cv::ocl::oclMat(frame_buffer[i])); } - +#endif ///////////// MOG //////////////////////// #if BUILD_WITH_VIDEO_INPUT_SUPPORT @@ -118,15 +124,16 @@ PERF_TEST_P(VideoMOGFixture, MOG, cv::VideoCapture cap(inputFile); ASSERT_TRUE(cap.isOpened()); - prepareData(cap, cn, frame_buffer, frame_buffer_ocl); + prepareData(cap, cn, frame_buffer); + cv::Mat foreground; + cv::ocl::oclMat foreground_d; if(RUN_PLAIN_IMPL) { - cv::BackgroundSubtractorMOG mog; - cv::Mat foreground; - TEST_CYCLE() { + cv::BackgroundSubtractorMOG mog; + foreground.release(); for (int i = 0; i < nFrame; i++) { mog(frame_buffer[i], foreground, learningRate); @@ -135,18 +142,19 @@ PERF_TEST_P(VideoMOGFixture, MOG, SANITY_CHECK(foreground); }else if(RUN_OCL_IMPL) { - cv::ocl::MOG d_mog; - cv::ocl::oclMat foreground; - cv::Mat foreground_h; + prepareData(frame_buffer, frame_buffer_ocl); + CV_Assert((int)(frame_buffer_ocl.size()) == nFrame); OCL_TEST_CYCLE() { + cv::ocl::MOG d_mog; + foreground_d.release(); for (int i = 0; i < nFrame; ++i) { - d_mog(frame_buffer_ocl[i], foreground, learningRate); + d_mog(frame_buffer_ocl[i], foreground_d, learningRate); } } - foreground.download(foreground_h); - SANITY_CHECK(foreground_h); + foreground_d.download(foreground); + SANITY_CHECK(foreground); }else OCL_PERF_ELSE } @@ -173,18 +181,18 @@ PERF_TEST_P(VideoMOG2Fixture, MOG2, cv::VideoCapture cap(inputFile); ASSERT_TRUE(cap.isOpened()); - - prepareData(cap, cn, frame_buffer, frame_buffer_ocl); + prepareData(cap, cn, frame_buffer); + cv::Mat foreground; + cv::ocl::oclMat foreground_d; if(RUN_PLAIN_IMPL) { - cv::BackgroundSubtractorMOG2 mog2; - cv::Mat foreground; - - mog2.set("detectShadows", false); - TEST_CYCLE() { + cv::BackgroundSubtractorMOG2 mog2; + mog2.set("detectShadows", false); + foreground.release(); + for (int i = 0; i < nFrame; i++) { mog2(frame_buffer[i], foreground); @@ -193,19 +201,19 @@ PERF_TEST_P(VideoMOG2Fixture, MOG2, SANITY_CHECK(foreground); }else if(RUN_OCL_IMPL) { - cv::ocl::MOG2 d_mog2; - cv::ocl::oclMat foreground; - cv::Mat foreground_h; - + prepareData(frame_buffer, frame_buffer_ocl); + CV_Assert((int)(frame_buffer_ocl.size()) == nFrame); OCL_TEST_CYCLE() { + cv::ocl::MOG2 d_mog2; + foreground_d.release(); for (int i = 0; i < nFrame; i++) { - d_mog2(frame_buffer_ocl[i], foreground); + d_mog2(frame_buffer_ocl[i], foreground_d); } } - foreground.download(foreground_h); - SANITY_CHECK(foreground_h); + foreground_d.download(foreground); + SANITY_CHECK(foreground); }else OCL_PERF_ELSE } @@ -218,7 +226,7 @@ typedef TestBaseWithParam Video_MOG2GetBackgroundImage; PERF_TEST_P(Video_MOG2GetBackgroundImage, MOG2, ::testing::Combine(::testing::Values("gpu/video/768x576.avi", "gpu/video/1920x1080.avi"), - ::testing::Values(1, 3))) + ::testing::Values(3))) { VideoMOG2ParamType params = GetParam(); @@ -232,16 +240,21 @@ PERF_TEST_P(Video_MOG2GetBackgroundImage, MOG2, cv::VideoCapture cap(inputFile); ASSERT_TRUE(cap.isOpened()); - prepareData(cap, cn, frame_buffer, frame_buffer_ocl); + prepareData(cap, cn, frame_buffer); + + cv::Mat foreground; + cv::Mat background; + cv::ocl::oclMat foreground_d; + cv::ocl::oclMat background_d; if(RUN_PLAIN_IMPL) { - cv::BackgroundSubtractorMOG2 mog2; - cv::Mat foreground; - cv::Mat background; - mog2.set("detectShadows", false); TEST_CYCLE() { + cv::BackgroundSubtractorMOG2 mog2; + mog2.set("detectShadows", false); + foreground.release(); + background.release(); for (int i = 0; i < nFrame; i++) { mog2(frame_buffer[i], foreground); @@ -251,22 +264,22 @@ PERF_TEST_P(Video_MOG2GetBackgroundImage, MOG2, SANITY_CHECK(background); }else if(RUN_OCL_IMPL) { - cv::ocl::MOG2 d_mog2; - cv::ocl::oclMat foreground; - cv::Mat background_h; - cv::ocl::oclMat background; - + prepareData(frame_buffer, frame_buffer_ocl); + CV_Assert((int)(frame_buffer_ocl.size()) == nFrame); OCL_TEST_CYCLE() { + cv::ocl::MOG2 d_mog2; + foreground_d.release(); + background_d.release(); for (int i = 0; i < nFrame; i++) { - d_mog2(frame_buffer_ocl[i], foreground); + d_mog2(frame_buffer_ocl[i], foreground_d); } - d_mog2.getBackgroundImage(background); + d_mog2.getBackgroundImage(background_d); } - background.download(background_h); - SANITY_CHECK(background_h); + background_d.download(background); + SANITY_CHECK(background); }else OCL_PERF_ELSE } -#endif \ No newline at end of file +#endif From 2fb0d23104a4d6e7607269b51357974eae4a52e6 Mon Sep 17 00:00:00 2001 From: Jin Ma Date: Sat, 7 Sep 2013 10:40:30 +0800 Subject: [PATCH 5/5] Removed unnecessary process for conversion to 4-channel format. --- modules/ocl/perf/perf_bgfg.cpp | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/modules/ocl/perf/perf_bgfg.cpp b/modules/ocl/perf/perf_bgfg.cpp index ec96c90070..ecf1127024 100644 --- a/modules/ocl/perf/perf_bgfg.cpp +++ b/modules/ocl/perf/perf_bgfg.cpp @@ -63,14 +63,11 @@ using std::tr1::get; #endif #if BUILD_WITH_VIDEO_INPUT_SUPPORT -static void cvtFrameFmt(vector& input, vector& output, int output_cn) +static void cvtFrameFmt(vector& input, vector& output) { for(int i = 0; i< (int)(input.size()); i++) { - if(output_cn == 1) - cvtColor(input[i], output[i], COLOR_RGB2GRAY); - else - cvtColor(input[i], output[i], COLOR_RGB2RGBA); + cvtColor(input[i], output[i], COLOR_RGB2GRAY); } } //prepare data for CPU @@ -87,7 +84,7 @@ static void prepareData(VideoCapture& cap, int cn, vector& frame_buffer) } if(cn == 1) - cvtFrameFmt(frame_buffer_init, frame_buffer, 1); + cvtFrameFmt(frame_buffer_init, frame_buffer); else frame_buffer = frame_buffer_init; }