gapi_fluid_parallel_rois_test.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  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) 2019 Intel Corporation
  6. #include "test_precomp.hpp"
  7. #include "gapi_fluid_test_kernels.hpp"
  8. namespace opencv_test
  9. {
  10. namespace {
  11. cv::Mat randomMat(cv::Size img_sz, int type = CV_8UC1, cv::Scalar mean = cv::Scalar(127.0f), cv::Scalar stddev = cv::Scalar(40.f)){
  12. cv::Mat mat(img_sz, type);
  13. cv::randn(mat, mean, stddev);
  14. return mat;
  15. }
  16. cv::GFluidParallelOutputRois asGFluidParallelOutputRois(const std::vector<cv::Rect>& rois){
  17. cv::GFluidParallelOutputRois parallel_rois;
  18. for (auto const& roi : rois) {
  19. parallel_rois.parallel_rois.emplace_back(GFluidOutputRois{{roi}});
  20. }
  21. return parallel_rois;
  22. }
  23. void adjust_empty_roi(cv::Rect& roi, cv::Size size){
  24. if (roi.empty()) roi = cv::Rect{{0,0}, size};
  25. }
  26. cv::GCompileArgs combine(cv::GCompileArgs&& lhs, cv::GCompileArgs const& rhs){
  27. lhs.insert(lhs.end(), rhs.begin(), rhs.end());
  28. return std::move(lhs);
  29. }
  30. }
  31. using namespace cv::gapi_test_kernels;
  32. //As GTest can not simultaneously parameterize test with both types and values - lets use type-erasure and virtual interfaces
  33. //to use different computation pipelines
  34. struct ComputationPair {
  35. void run_with_gapi(const cv::Mat& in_mat, cv::GCompileArgs const& compile_args, cv::Mat& out_mat){
  36. run_with_gapi_impl(in_mat, combine(cv::compile_args(fluidTestPackage), compile_args), out_mat);
  37. }
  38. void run_with_gapi(const cv::Mat& in_mat, cv::GFluidParallelOutputRois const& parallel_rois, cv::Mat& out_mat){
  39. run_with_gapi_impl(in_mat, cv::compile_args(fluidTestPackage, parallel_rois), out_mat);
  40. }
  41. virtual void run_with_ocv (const cv::Mat& in_mat, const std::vector<cv::Rect>& rois, cv::Mat& out_mat) = 0;
  42. virtual std::string name() const { return {}; }
  43. virtual ~ComputationPair () = default;
  44. friend std::ostream& operator<<(std::ostream& o, ComputationPair const* cp){
  45. std::string custom_name = cp->name();
  46. return o << (custom_name.empty() ? typeid(cp).name() : custom_name );
  47. }
  48. private:
  49. virtual void run_with_gapi_impl(const cv::Mat& in_mat, cv::GCompileArgs const& comp_args, cv::Mat& out_mat) = 0;
  50. };
  51. struct Blur3x3CP : ComputationPair{
  52. static constexpr int borderType = BORDER_REPLICATE;
  53. static constexpr int kernelSize = 3;
  54. std::string name() const override { return "Blur3x3"; }
  55. void run_with_gapi_impl(const cv::Mat& in_mat, cv::GCompileArgs const& comp_args, cv::Mat& out_mat_gapi) override {
  56. cv::GMat in;
  57. cv::GMat out = TBlur3x3::on(in, borderType, {});
  58. cv::GComputation c(cv::GIn(in), cv::GOut(out));
  59. // Run G-API
  60. auto cc = c.compile(cv::descr_of(in_mat), comp_args);
  61. cc(cv::gin(in_mat), cv::gout(out_mat_gapi));
  62. }
  63. void run_with_ocv(const cv::Mat& in_mat, const std::vector<cv::Rect>& rois, cv::Mat& out_mat_ocv) override {
  64. cv::Point anchor = {-1, -1};
  65. // Check with OpenCV
  66. for (auto roi : rois) {
  67. adjust_empty_roi(roi, in_mat.size());
  68. cv::blur(in_mat(roi), out_mat_ocv(roi), {kernelSize, kernelSize}, anchor, borderType);
  69. }
  70. }
  71. };
  72. struct AddCCP : ComputationPair{
  73. std::string name() const override { return "AddC"; }
  74. void run_with_gapi_impl(const cv::Mat& in_mat, cv::GCompileArgs const& comp_args, cv::Mat& out_mat_gapi) override {
  75. cv::GMat in;
  76. cv::GMat out = TAddCSimple::on(in, 1);
  77. cv::GComputation c(cv::GIn(in), cv::GOut(out));
  78. // Run G-API
  79. auto cc = c.compile(cv::descr_of(in_mat), comp_args);
  80. cc(cv::gin(in_mat), cv::gout(out_mat_gapi));
  81. }
  82. void run_with_ocv(const cv::Mat& in_mat, const std::vector<cv::Rect>& rois, cv::Mat& out_mat_ocv) override {
  83. // Check with OpenCV
  84. for (auto roi : rois) {
  85. adjust_empty_roi(roi, in_mat.size());
  86. out_mat_ocv(roi) = in_mat(roi) + 1u;
  87. }
  88. }
  89. };
  90. template<BorderTypes _borderType>
  91. struct SequenceOfBlursCP : ComputationPair{
  92. BorderTypes borderType = _borderType;
  93. std::string name() const override { return "SequenceOfBlurs, border type: " + std::to_string(static_cast<int>(borderType)); }
  94. void run_with_gapi_impl(const cv::Mat& in_mat, cv::GCompileArgs const& comp_args, cv::Mat& out_mat) override {
  95. cv::Scalar borderValue(0);
  96. GMat in;
  97. auto mid = TBlur3x3::on(in, borderType, borderValue);
  98. auto out = TBlur5x5::on(mid, borderType, borderValue);
  99. GComputation c(GIn(in), GOut(out));
  100. auto cc = c.compile(descr_of(in_mat), comp_args);
  101. cc(cv::gin(in_mat), cv::gout(out_mat));
  102. }
  103. void run_with_ocv(const cv::Mat& in_mat, const std::vector<cv::Rect>& rois, cv::Mat& out_mat) override {
  104. cv::Mat mid_mat_ocv = Mat::zeros(in_mat.size(), in_mat.type());
  105. cv::Point anchor = {-1, -1};
  106. for (auto roi : rois) {
  107. adjust_empty_roi(roi, in_mat.size());
  108. cv::blur(in_mat, mid_mat_ocv, {3,3}, anchor, borderType);
  109. cv::blur(mid_mat_ocv(roi), out_mat(roi), {5,5}, anchor, borderType);
  110. }
  111. }
  112. };
  113. struct TiledComputation : public TestWithParam <std::tuple<ComputationPair*, cv::Size, std::vector<cv::Rect>, decltype(cv::GFluidParallelFor::parallel_for)>> {};
  114. TEST_P(TiledComputation, Test)
  115. {
  116. ComputationPair* cp;
  117. cv::Size img_sz;
  118. std::vector<cv::Rect> rois ;
  119. decltype(cv::GFluidParallelFor::parallel_for) pfor;
  120. auto mat_type = CV_8UC1;
  121. std::tie(cp, img_sz, rois, pfor) = GetParam();
  122. cv::Mat in_mat = randomMat(img_sz, mat_type);
  123. cv::Mat out_mat_gapi = cv::Mat::zeros(img_sz, mat_type);
  124. cv::Mat out_mat_ocv = cv::Mat::zeros(img_sz, mat_type);
  125. auto comp_args = combine(cv::compile_args(asGFluidParallelOutputRois(rois)), pfor ? cv::compile_args(cv::GFluidParallelFor{pfor}) : cv::GCompileArgs{});
  126. cp->run_with_gapi(in_mat, comp_args, out_mat_gapi);
  127. cp->run_with_ocv (in_mat, rois, out_mat_ocv);
  128. EXPECT_EQ(0, cvtest::norm(out_mat_gapi, out_mat_ocv, NORM_INF))
  129. << "in_mat : \n" << in_mat << std::endl
  130. << "diff matrix :\n " << (out_mat_gapi != out_mat_ocv) << std::endl
  131. << "out_mat_gapi: \n" << out_mat_gapi << std::endl
  132. << "out_mat_ocv: \n" << out_mat_ocv << std::endl;;
  133. }
  134. namespace {
  135. //this is ugly but other variants (like using shared_ptr) are IMHO even more ugly :)
  136. template<typename T, typename... Arg>
  137. T* addr_of_static(Arg... arg) {
  138. static T obj(std::forward<Arg>(arg)...);
  139. return &obj;
  140. }
  141. }
  142. auto single_arg_computations = [](){
  143. return Values( addr_of_static<Blur3x3CP>(),
  144. addr_of_static<AddCCP>(),
  145. addr_of_static<SequenceOfBlursCP<BORDER_CONSTANT>>(),
  146. addr_of_static<SequenceOfBlursCP<BORDER_REPLICATE>>(),
  147. addr_of_static<SequenceOfBlursCP<BORDER_REFLECT_101>>()
  148. );
  149. };
  150. auto tilesets_8x10 = [](){
  151. return Values(std::vector<cv::Rect>{cv::Rect{}},
  152. std::vector<cv::Rect>{cv::Rect{0,0,8,5}, cv::Rect{0,5,8,5}},
  153. std::vector<cv::Rect>{cv::Rect{0,1,8,3}, cv::Rect{0,4,8,3}},
  154. std::vector<cv::Rect>{cv::Rect{0,2,8,3}, cv::Rect{0,5,8,2}},
  155. std::vector<cv::Rect>{cv::Rect{0,3,8,4}, cv::Rect{0,9,8,1}});
  156. };
  157. auto tilesets_20x15 = [](){
  158. return Values(std::vector<cv::Rect>{cv::Rect{}},
  159. std::vector<cv::Rect>{cv::Rect{{0,0},cv::Size{20,7}},
  160. cv::Rect{{0,7},cv::Size{20,8}}});
  161. };
  162. auto tilesets_320x240 = [](){
  163. return Values(std::vector<cv::Rect>{cv::Rect{{0,0}, cv::Size{320,120}},
  164. cv::Rect{{0,120}, cv::Size{320,120}}},
  165. std::vector<cv::Rect>{cv::Rect{{0,0}, cv::Size{320,120}},
  166. cv::Rect{{0,120}, cv::Size{320,120}}},
  167. std::vector<cv::Rect>{cv::Rect{{0,0}, cv::Size{320,60}},
  168. cv::Rect{{0,60}, cv::Size{320,60}},
  169. cv::Rect{{0,120},cv::Size{320,120}}});
  170. };
  171. namespace{
  172. auto no_custom_pfor = decltype(cv::GFluidParallelFor::parallel_for){};
  173. }
  174. INSTANTIATE_TEST_CASE_P(FluidTiledSerial8x10, TiledComputation,
  175. Combine(
  176. single_arg_computations(),
  177. Values(cv::Size(8, 10)),
  178. tilesets_8x10(),
  179. Values(no_custom_pfor))
  180. );
  181. INSTANTIATE_TEST_CASE_P(FluidTiledSerial20x15, TiledComputation,
  182. Combine(
  183. single_arg_computations(),
  184. Values(cv::Size(20, 15)),
  185. tilesets_20x15(),
  186. Values(no_custom_pfor))
  187. );
  188. INSTANTIATE_TEST_CASE_P(FluidTiledSerial320x240, TiledComputation,
  189. Combine(
  190. single_arg_computations(),
  191. Values(cv::Size(320, 240)),
  192. tilesets_320x240(),
  193. Values(no_custom_pfor))
  194. );
  195. //FIXME: add multiple outputs tests
  196. TEST(FluidTiledParallelFor, basic)
  197. {
  198. cv::Size img_sz{8,20};
  199. auto mat_type = CV_8UC1;
  200. cv::GMat in;
  201. cv::GMat out = TAddCSimple::on(in, 1);
  202. cv::GComputation c(cv::GIn(in), cv::GOut(out));
  203. cv::Mat in_mat = randomMat(img_sz, mat_type);
  204. cv::Mat out_mat_gapi = cv::Mat::zeros(img_sz, mat_type);
  205. auto parallel_rois = asGFluidParallelOutputRois( std::vector<cv::Rect>{cv::Rect{0,0,8,5}, cv::Rect{0,5,8,5}});
  206. std::size_t items_count = 0;
  207. auto pfor = [&items_count](std::size_t count, std::function<void(std::size_t)> ){
  208. items_count = count;
  209. };
  210. // Run G-API
  211. auto cc = c.compile(cv::descr_of(in_mat), cv::compile_args(fluidTestPackage, parallel_rois, GFluidParallelFor{pfor}));
  212. cc(cv::gin(in_mat), cv::gout(out_mat_gapi));
  213. ASSERT_EQ(parallel_rois.parallel_rois.size(), items_count);
  214. }
  215. namespace {
  216. auto serial_for = [](std::size_t count, std::function<void(std::size_t)> f){
  217. for (std::size_t i = 0; i < count; ++i){
  218. f(i);
  219. }
  220. };
  221. auto cv_parallel_for = [](std::size_t count, std::function<void(std::size_t)> f){
  222. cv::parallel_for_(cv::Range(0, static_cast<int>(count)), [f](const cv::Range& r){
  223. for (auto i = r.start; i < r.end; ++i){
  224. f(i);
  225. } });
  226. };
  227. }
  228. INSTANTIATE_TEST_CASE_P(FluidTiledParallel8x10, TiledComputation,
  229. Combine(
  230. single_arg_computations(),
  231. Values(cv::Size(8, 10)),
  232. tilesets_8x10(),
  233. Values(serial_for, cv_parallel_for))
  234. );
  235. } // namespace opencv_test
  236. //define custom printer for "parallel_for" test parameter
  237. namespace std {
  238. void PrintTo(decltype(cv::GFluidParallelFor::parallel_for) const& f, std::ostream* o);
  239. }
  240. //separate declaration and definition are needed to please the compiler
  241. void std::PrintTo(decltype(cv::GFluidParallelFor::parallel_for) const& f, std::ostream* o){
  242. if (f) {
  243. using namespace opencv_test;
  244. if (f.target<decltype(serial_for)>()){
  245. *o <<"serial_for";
  246. }
  247. else if (f.target<decltype(cv_parallel_for)>()){
  248. *o <<"cv_parallel_for";
  249. }
  250. else {
  251. *o <<"parallel_for of type: " << f.target_type().name();
  252. }
  253. }
  254. else
  255. {
  256. *o << "default parallel_for";
  257. }
  258. }