gapi_video_perf_tests_inl.hpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. // This file is part of OpenCV project.
  2. // It is subject to the license terms in the LICENSE file found in the top-level directory
  3. // of this distribution and at http://opencv.org/license.html.
  4. //
  5. // Copyright (C) 2020 Intel Corporation
  6. #ifndef OPENCV_GAPI_VIDEO_PERF_TESTS_INL_HPP
  7. #define OPENCV_GAPI_VIDEO_PERF_TESTS_INL_HPP
  8. #include <iostream>
  9. #include "gapi_video_perf_tests.hpp"
  10. namespace opencv_test
  11. {
  12. using namespace perf;
  13. //------------------------------------------------------------------------------
  14. PERF_TEST_P_(BuildOptFlowPyramidPerfTest, TestPerformance)
  15. {
  16. std::vector<Mat> outPyrOCV, outPyrGAPI;
  17. int outMaxLevelOCV = 0, outMaxLevelGAPI = 0;
  18. Scalar outMaxLevelSc;
  19. BuildOpticalFlowPyramidTestParams params;
  20. std::tie(params.fileName, params.winSize,
  21. params.maxLevel, params.withDerivatives,
  22. params.pyrBorder, params.derivBorder,
  23. params.tryReuseInputImage, params.compileArgs) = GetParam();
  24. BuildOpticalFlowPyramidTestOutput outOCV { outPyrOCV, outMaxLevelOCV };
  25. BuildOpticalFlowPyramidTestOutput outGAPI { outPyrGAPI, outMaxLevelGAPI };
  26. GComputation c = runOCVnGAPIBuildOptFlowPyramid(*this, params, outOCV, outGAPI);
  27. declare.in(in_mat1).out(outPyrGAPI);
  28. TEST_CYCLE()
  29. {
  30. c.apply(cv::gin(in_mat1), cv::gout(outPyrGAPI, outMaxLevelSc));
  31. }
  32. outMaxLevelGAPI = static_cast<int>(outMaxLevelSc[0]);
  33. // Comparison //////////////////////////////////////////////////////////////
  34. compareOutputPyramids(outGAPI, outOCV);
  35. SANITY_CHECK_NOTHING();
  36. }
  37. PERF_TEST_P_(OptFlowLKPerfTest, TestPerformance)
  38. {
  39. std::vector<cv::Point2f> outPtsOCV, outPtsGAPI, inPts;
  40. std::vector<uchar> outStatusOCV, outStatusGAPI;
  41. std::vector<float> outErrOCV, outErrGAPI;
  42. OptFlowLKTestParams params;
  43. std::tie(params.fileNamePattern, params.channels,
  44. params.pointsNum, params.winSize, params.criteria,
  45. params.compileArgs) = GetParam();
  46. OptFlowLKTestOutput outOCV { outPtsOCV, outStatusOCV, outErrOCV };
  47. OptFlowLKTestOutput outGAPI { outPtsGAPI, outStatusGAPI, outErrGAPI };
  48. cv::GComputation c = runOCVnGAPIOptFlowLK(*this, inPts, params, outOCV, outGAPI);
  49. declare.in(in_mat1, in_mat2, inPts).out(outPtsGAPI, outStatusGAPI, outErrGAPI);
  50. TEST_CYCLE()
  51. {
  52. c.apply(cv::gin(in_mat1, in_mat2, inPts, std::vector<cv::Point2f>{ }),
  53. cv::gout(outPtsGAPI, outStatusGAPI, outErrGAPI));
  54. }
  55. // Comparison //////////////////////////////////////////////////////////////
  56. compareOutputsOptFlow(outGAPI, outOCV);
  57. SANITY_CHECK_NOTHING();
  58. }
  59. //------------------------------------------------------------------------------
  60. PERF_TEST_P_(OptFlowLKForPyrPerfTest, TestPerformance)
  61. {
  62. std::vector<cv::Mat> inPyr1, inPyr2;
  63. std::vector<cv::Point2f> outPtsOCV, outPtsGAPI, inPts;
  64. std::vector<uchar> outStatusOCV, outStatusGAPI;
  65. std::vector<float> outErrOCV, outErrGAPI;
  66. bool withDeriv = false;
  67. OptFlowLKTestParams params;
  68. std::tie(params.fileNamePattern, params.channels,
  69. params.pointsNum, params.winSize, params.criteria,
  70. withDeriv, params.compileArgs) = GetParam();
  71. OptFlowLKTestInput<std::vector<cv::Mat>> in { inPyr1, inPyr2, inPts };
  72. OptFlowLKTestOutput outOCV { outPtsOCV, outStatusOCV, outErrOCV };
  73. OptFlowLKTestOutput outGAPI { outPtsGAPI, outStatusGAPI, outErrGAPI };
  74. cv::GComputation c = runOCVnGAPIOptFlowLKForPyr(*this, in, params, withDeriv, outOCV, outGAPI);
  75. declare.in(inPyr1, inPyr2, inPts).out(outPtsGAPI, outStatusGAPI, outErrGAPI);
  76. TEST_CYCLE()
  77. {
  78. c.apply(cv::gin(inPyr1, inPyr2, inPts, std::vector<cv::Point2f>{ }),
  79. cv::gout(outPtsGAPI, outStatusGAPI, outErrGAPI));
  80. }
  81. // Comparison //////////////////////////////////////////////////////////////
  82. compareOutputsOptFlow(outGAPI, outOCV);
  83. SANITY_CHECK_NOTHING();
  84. }
  85. PERF_TEST_P_(BuildPyr_CalcOptFlow_PipelinePerfTest, TestPerformance)
  86. {
  87. std::vector<Point2f> outPtsOCV, outPtsGAPI, inPts;
  88. std::vector<uchar> outStatusOCV, outStatusGAPI;
  89. std::vector<float> outErrOCV, outErrGAPI;
  90. BuildOpticalFlowPyramidTestParams params;
  91. params.pyrBorder = BORDER_DEFAULT;
  92. params.derivBorder = BORDER_DEFAULT;
  93. params.tryReuseInputImage = true;
  94. std::tie(params.fileName, params.winSize,
  95. params.maxLevel, params.withDerivatives,
  96. params.compileArgs) = GetParam();
  97. auto customKernel = gapi::kernels<GCPUMinScalar>();
  98. auto kernels = gapi::combine(customKernel,
  99. params.compileArgs[0].get<GKernelPackage>());
  100. params.compileArgs = compile_args(kernels);
  101. OptFlowLKTestOutput outOCV { outPtsOCV, outStatusOCV, outErrOCV };
  102. OptFlowLKTestOutput outGAPI { outPtsGAPI, outStatusGAPI, outErrGAPI };
  103. cv::GComputation c = runOCVnGAPIOptFlowPipeline(*this, params, outOCV, outGAPI, inPts);
  104. declare.in(in_mat1, in_mat2, inPts).out(outPtsGAPI, outStatusGAPI, outErrGAPI);
  105. TEST_CYCLE()
  106. {
  107. c.apply(cv::gin(in_mat1, in_mat2, inPts, std::vector<cv::Point2f>{ }),
  108. cv::gout(outPtsGAPI, outStatusGAPI, outErrGAPI));
  109. }
  110. // Comparison //////////////////////////////////////////////////////////////
  111. compareOutputsOptFlow(outGAPI, outOCV);
  112. SANITY_CHECK_NOTHING();
  113. }
  114. //------------------------------------------------------------------------------
  115. #ifdef HAVE_OPENCV_VIDEO
  116. PERF_TEST_P_(BackgroundSubtractorPerfTest, TestPerformance)
  117. {
  118. namespace gvideo = cv::gapi::video;
  119. gvideo::BackgroundSubtractorType opType;
  120. std::string filePath = "";
  121. bool detectShadows = false;
  122. double learningRate = -1.;
  123. std::size_t testNumFrames = 0;
  124. cv::GCompileArgs compileArgs;
  125. CompareMats cmpF;
  126. std::tie(opType, filePath, detectShadows, learningRate, testNumFrames,
  127. compileArgs, cmpF) = GetParam();
  128. const int histLength = 500;
  129. double thr = -1;
  130. switch (opType)
  131. {
  132. case gvideo::TYPE_BS_MOG2:
  133. {
  134. thr = 16.;
  135. break;
  136. }
  137. case gvideo::TYPE_BS_KNN:
  138. {
  139. thr = 400.;
  140. break;
  141. }
  142. default:
  143. FAIL() << "unsupported type of BackgroundSubtractor";
  144. }
  145. const gvideo::BackgroundSubtractorParams bsp(opType, histLength, thr, detectShadows,
  146. learningRate);
  147. // Retrieving frames
  148. std::vector<cv::Mat> frames;
  149. frames.reserve(testNumFrames);
  150. {
  151. cv::Mat frame;
  152. cv::VideoCapture cap;
  153. if (!cap.open(findDataFile(filePath)))
  154. throw SkipTestException("Video file can not be opened");
  155. for (std::size_t i = 0; i < testNumFrames && cap.read(frame); i++)
  156. {
  157. frames.push_back(frame);
  158. }
  159. }
  160. GAPI_Assert(testNumFrames == frames.size() && "Can't read required number of frames");
  161. // G-API graph declaration
  162. cv::GMat in;
  163. cv::GMat out = cv::gapi::BackgroundSubtractor(in, bsp);
  164. cv::GComputation c(cv::GIn(in), cv::GOut(out));
  165. auto cc = c.compile(cv::descr_of(frames[0]), std::move(compileArgs));
  166. cv::Mat gapiForeground;
  167. TEST_CYCLE()
  168. {
  169. cc.prepareForNewStream();
  170. for (size_t i = 0; i < testNumFrames; i++)
  171. {
  172. cc(cv::gin(frames[i]), cv::gout(gapiForeground));
  173. }
  174. }
  175. // OpenCV Background Subtractor declaration
  176. cv::Ptr<cv::BackgroundSubtractor> pOCVBackSub;
  177. if (opType == gvideo::TYPE_BS_MOG2)
  178. pOCVBackSub = cv::createBackgroundSubtractorMOG2(histLength, thr, detectShadows);
  179. else if (opType == gvideo::TYPE_BS_KNN)
  180. pOCVBackSub = cv::createBackgroundSubtractorKNN(histLength, thr, detectShadows);
  181. cv::Mat ocvForeground;
  182. for (size_t i = 0; i < testNumFrames; i++)
  183. {
  184. pOCVBackSub->apply(frames[i], ocvForeground, learningRate);
  185. }
  186. // Validation
  187. EXPECT_TRUE(cmpF(gapiForeground, ocvForeground));
  188. SANITY_CHECK_NOTHING();
  189. }
  190. //------------------------------------------------------------------------------
  191. inline void generateInputKalman(const int mDim, const MatType2& type,
  192. const size_t testNumMeasurements, const bool receiveRandMeas,
  193. std::vector<bool>& haveMeasurements,
  194. std::vector<cv::Mat>& measurements)
  195. {
  196. cv::RNG& rng = cv::theRNG();
  197. measurements.clear();
  198. haveMeasurements = std::vector<bool>(testNumMeasurements, true);
  199. for (size_t i = 0; i < testNumMeasurements; i++)
  200. {
  201. if (receiveRandMeas)
  202. {
  203. haveMeasurements[i] = rng(2u) == 1; // returns 0 or 1 - whether we have measurement
  204. // at this iteration or not
  205. } // if not - testing the slowest case in which we have measurements at every iteration
  206. cv::Mat measurement = cv::Mat::zeros(mDim, 1, type);
  207. if (haveMeasurements[i])
  208. {
  209. cv::randu(measurement, cv::Scalar::all(-1), cv::Scalar::all(1));
  210. }
  211. measurements.push_back(measurement.clone());
  212. }
  213. }
  214. inline void generateInputKalman(const int mDim, const int cDim, const MatType2& type,
  215. const size_t testNumMeasurements, const bool receiveRandMeas,
  216. std::vector<bool>& haveMeasurements,
  217. std::vector<cv::Mat>& measurements,
  218. std::vector<cv::Mat>& ctrls)
  219. {
  220. generateInputKalman(mDim, type, testNumMeasurements, receiveRandMeas,
  221. haveMeasurements, measurements);
  222. ctrls.clear();
  223. cv::Mat ctrl(cDim, 1, type);
  224. for (size_t i = 0; i < testNumMeasurements; i++)
  225. {
  226. cv::randu(ctrl, cv::Scalar::all(-1), cv::Scalar::all(1));
  227. ctrls.push_back(ctrl.clone());
  228. }
  229. }
  230. PERF_TEST_P_(KalmanFilterControlPerfTest, TestPerformance)
  231. {
  232. MatType2 type = -1;
  233. int dDim = -1, mDim = -1;
  234. size_t testNumMeasurements = 0;
  235. bool receiveRandMeas = true;
  236. cv::GCompileArgs compileArgs;
  237. std::tie(type, dDim, mDim, testNumMeasurements, receiveRandMeas, compileArgs) = GetParam();
  238. const int cDim = 2;
  239. cv::gapi::KalmanParams kp;
  240. initKalmanParams(type, dDim, mDim, cDim, kp);
  241. // Generating input
  242. std::vector<bool> haveMeasurements;
  243. std::vector<cv::Mat> measurements, ctrls;
  244. generateInputKalman(mDim, cDim, type, testNumMeasurements, receiveRandMeas,
  245. haveMeasurements, measurements, ctrls);
  246. // G-API graph declaration
  247. cv::GMat m, ctrl;
  248. cv::GOpaque<bool> have_m;
  249. cv::GMat out = cv::gapi::KalmanFilter(m, have_m, ctrl, kp);
  250. cv::GComputation c(cv::GIn(m, have_m, ctrl), cv::GOut(out));
  251. auto cc = c.compile(
  252. cv::descr_of(cv::gin(cv::Mat(mDim, 1, type), true, cv::Mat(cDim, 1, type))),
  253. std::move(compileArgs));
  254. cv::Mat gapiKState(dDim, 1, type);
  255. TEST_CYCLE()
  256. {
  257. cc.prepareForNewStream();
  258. for (size_t i = 0; i < testNumMeasurements; i++)
  259. {
  260. bool hvMeas = haveMeasurements[i];
  261. cc(cv::gin(measurements[i], hvMeas, ctrls[i]), cv::gout(gapiKState));
  262. }
  263. }
  264. // OpenCV reference KalmanFilter initialization
  265. cv::KalmanFilter ocvKalman(dDim, mDim, cDim, type);
  266. initKalmanFilter(kp, true, ocvKalman);
  267. cv::Mat ocvKState(dDim, 1, type);
  268. for (size_t i = 0; i < testNumMeasurements; i++)
  269. {
  270. ocvKState = ocvKalman.predict(ctrls[i]);
  271. if (haveMeasurements[i])
  272. ocvKState = ocvKalman.correct(measurements[i]);
  273. }
  274. // Validation
  275. EXPECT_TRUE(AbsExact().to_compare_f()(gapiKState, ocvKState));
  276. SANITY_CHECK_NOTHING();
  277. }
  278. PERF_TEST_P_(KalmanFilterNoControlPerfTest, TestPerformance)
  279. {
  280. MatType2 type = -1;
  281. int dDim = -1, mDim = -1;
  282. size_t testNumMeasurements = 0;
  283. bool receiveRandMeas = true;
  284. cv::GCompileArgs compileArgs;
  285. std::tie(type, dDim, mDim, testNumMeasurements, receiveRandMeas, compileArgs) = GetParam();
  286. const int cDim = 0;
  287. cv::gapi::KalmanParams kp;
  288. initKalmanParams(type, dDim, mDim, cDim, kp);
  289. // Generating input
  290. std::vector<bool> haveMeasurements;
  291. std::vector<cv::Mat> measurements;
  292. generateInputKalman(mDim, type, testNumMeasurements, receiveRandMeas,
  293. haveMeasurements, measurements);
  294. // G-API graph declaration
  295. cv::GMat m;
  296. cv::GOpaque<bool> have_m;
  297. cv::GMat out = cv::gapi::KalmanFilter(m, have_m, kp);
  298. cv::GComputation c(cv::GIn(m, have_m), cv::GOut(out));
  299. auto cc = c.compile(cv::descr_of(cv::gin(cv::Mat(mDim, 1, type), true)),
  300. std::move(compileArgs));
  301. cv::Mat gapiKState(dDim, 1, type);
  302. TEST_CYCLE()
  303. {
  304. cc.prepareForNewStream();
  305. for (size_t i = 0; i < testNumMeasurements; i++)
  306. {
  307. bool hvMeas = haveMeasurements[i];
  308. cc(cv::gin(measurements[i], hvMeas), cv::gout(gapiKState));
  309. }
  310. }
  311. // OpenCV reference KalmanFilter declaration
  312. cv::KalmanFilter ocvKalman(dDim, mDim, cDim, type);
  313. initKalmanFilter(kp, false, ocvKalman);
  314. cv::Mat ocvKState(dDim, 1, type);
  315. for (size_t i = 0; i < testNumMeasurements; i++)
  316. {
  317. ocvKState = ocvKalman.predict();
  318. if (haveMeasurements[i])
  319. ocvKState = ocvKalman.correct(measurements[i]);
  320. }
  321. // Validation
  322. EXPECT_TRUE(AbsExact().to_compare_f()(gapiKState, ocvKState));
  323. SANITY_CHECK_NOTHING();
  324. }
  325. #endif // HAVE_OPENCV_VIDEO
  326. } // opencv_test
  327. #endif // OPENCV_GAPI_VIDEO_PERF_TESTS_INL_HPP