gapi_basic_hetero_tests.cpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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) 2018 Intel Corporation
  6. #include "test_precomp.hpp"
  7. #include "gapi_mock_kernels.hpp"
  8. #include <opencv2/gapi/fluid/gfluidkernel.hpp>
  9. namespace opencv_test
  10. {
  11. namespace
  12. {
  13. GAPI_OCV_KERNEL(OCVFoo, I::Foo)
  14. {
  15. static void run(const cv::Mat &in, cv::Mat &out)
  16. {
  17. out = in + 2;
  18. }
  19. };
  20. GAPI_OCV_KERNEL(OCVBar, I::Bar)
  21. {
  22. static void run(const cv::Mat &a, const cv::Mat &b, cv::Mat &out)
  23. {
  24. out = 4*(a + b);
  25. }
  26. };
  27. void FluidFooRow(const uint8_t* in, uint8_t* out, int length)
  28. {
  29. for (int i = 0; i < length; i++)
  30. {
  31. out[i] = in[i] + 3;
  32. }
  33. }
  34. void FluidBarRow(const uint8_t* in1, const uint8_t* in2, uint8_t* out, int length)
  35. {
  36. for (int i = 0; i < length; i++)
  37. {
  38. out[i] = 3*(in1[i] + in2[i]);
  39. }
  40. }
  41. GAPI_FLUID_KERNEL(FFoo, I::Foo, false)
  42. {
  43. static const int Window = 1;
  44. static void run(const cv::gapi::fluid::View &in,
  45. cv::gapi::fluid::Buffer &out)
  46. {
  47. FluidFooRow(in.InLineB(0), out.OutLineB(), in.length());
  48. }
  49. };
  50. GAPI_FLUID_KERNEL(FBar, I::Bar, false)
  51. {
  52. static const int Window = 1;
  53. static void run(const cv::gapi::fluid::View &in1,
  54. const cv::gapi::fluid::View &in2,
  55. cv::gapi::fluid::Buffer &out)
  56. {
  57. FluidBarRow(in1.InLineB(0), in2.InLineB(0), out.OutLineB(), in1.length());
  58. }
  59. };
  60. G_TYPED_KERNEL(FluidFooI, <cv::GMat(cv::GMat)>, "test.kernels.fluid_foo")
  61. {
  62. static cv::GMatDesc outMeta(const cv::GMatDesc &in) { return in; }
  63. };
  64. G_TYPED_KERNEL(FluidBarI, <cv::GMat(cv::GMat,cv::GMat)>, "test.kernels.fluid_bar")
  65. {
  66. static cv::GMatDesc outMeta(const cv::GMatDesc &in, const cv::GMatDesc &) { return in; }
  67. };
  68. GAPI_FLUID_KERNEL(FluidFoo, FluidFooI, false)
  69. {
  70. static const int Window = 1;
  71. static void run(const cv::gapi::fluid::View &in,
  72. cv::gapi::fluid::Buffer &out)
  73. {
  74. FluidFooRow(in.InLineB(0), out.OutLineB(), in.length());
  75. }
  76. };
  77. GAPI_FLUID_KERNEL(FluidBar, FluidBarI, false)
  78. {
  79. static const int Window = 1;
  80. static void run(const cv::gapi::fluid::View &in1,
  81. const cv::gapi::fluid::View &in2,
  82. cv::gapi::fluid::Buffer &out)
  83. {
  84. FluidBarRow(in1.InLineB(0), in2.InLineB(0), out.OutLineB(), in1.length());
  85. }
  86. };
  87. GAPI_FLUID_KERNEL(FluidFoo2lpi, FluidFooI, false)
  88. {
  89. static const int Window = 1;
  90. static const int LPI = 2;
  91. static void run(const cv::gapi::fluid::View &in,
  92. cv::gapi::fluid::Buffer &out)
  93. {
  94. for (int l = 0; l < out.lpi(); l++)
  95. {
  96. FluidFooRow(in.InLineB(l), out.OutLineB(l), in.length());
  97. }
  98. }
  99. };
  100. cv::Mat ocvFoo(const cv::Mat &in)
  101. {
  102. cv::Mat out;
  103. OCVFoo::run(in, out);
  104. return out;
  105. }
  106. cv::Mat ocvBar(const cv::Mat &in1, const cv::Mat &in2)
  107. {
  108. cv::Mat out;
  109. OCVBar::run(in1, in2, out);
  110. return out;
  111. }
  112. cv::Mat fluidFoo(const cv::Mat &in)
  113. {
  114. cv::Mat out(in.rows, in.cols, in.type());
  115. for (int y = 0; y < in.rows; y++)
  116. {
  117. FluidFooRow(in.ptr(y), out.ptr(y), in.cols);
  118. }
  119. return out;
  120. }
  121. cv::Mat fluidBar(const cv::Mat &in1, const cv::Mat &in2)
  122. {
  123. cv::Mat out(in1.rows, in1.cols, in1.type());
  124. for (int y = 0; y < in1.rows; y++)
  125. {
  126. FluidBarRow(in1.ptr(y), in2.ptr(y), out.ptr(y), in1.cols);
  127. }
  128. return out;
  129. }
  130. } // anonymous namespace
  131. struct GAPIHeteroTest: public ::testing::Test
  132. {
  133. cv::GComputation m_comp;
  134. cv::GKernelPackage m_ocv_kernels;
  135. cv::GKernelPackage m_fluid_kernels;
  136. cv::GKernelPackage m_hetero_kernels;
  137. cv::Mat m_in_mat;
  138. cv::Mat m_out_mat;
  139. GAPIHeteroTest();
  140. };
  141. GAPIHeteroTest::GAPIHeteroTest()
  142. : m_comp([](){
  143. cv::GMat in;
  144. cv::GMat out = I::Bar::on(I::Foo::on(in),
  145. I::Foo::on(in));
  146. return cv::GComputation(in, out);
  147. })
  148. , m_ocv_kernels(cv::gapi::kernels<OCVFoo, OCVBar>())
  149. , m_fluid_kernels(cv::gapi::kernels<FFoo, FBar>())
  150. , m_hetero_kernels(cv::gapi::kernels<OCVFoo, FBar>())
  151. , m_in_mat(cv::Mat::eye(cv::Size(64, 64), CV_8UC1))
  152. {
  153. }
  154. TEST_F(GAPIHeteroTest, TestOCV)
  155. {
  156. EXPECT_TRUE(cv::gapi::cpu::backend() == m_ocv_kernels.lookup<I::Foo>());
  157. EXPECT_TRUE(cv::gapi::cpu::backend() == m_ocv_kernels.lookup<I::Bar>());
  158. cv::Mat ref = ocvBar(ocvFoo(m_in_mat), ocvFoo(m_in_mat));
  159. EXPECT_NO_THROW(m_comp.apply(m_in_mat, m_out_mat, cv::compile_args(m_ocv_kernels)));
  160. EXPECT_EQ(0, cvtest::norm(ref, m_out_mat, NORM_INF));
  161. }
  162. TEST_F(GAPIHeteroTest, TestFluid)
  163. {
  164. EXPECT_TRUE(cv::gapi::fluid::backend() == m_fluid_kernels.lookup<I::Foo>());
  165. EXPECT_TRUE(cv::gapi::fluid::backend() == m_fluid_kernels.lookup<I::Bar>());
  166. cv::Mat ref = fluidBar(fluidFoo(m_in_mat), fluidFoo(m_in_mat));
  167. EXPECT_NO_THROW(m_comp.apply(m_in_mat, m_out_mat, cv::compile_args(m_fluid_kernels)));
  168. EXPECT_EQ(0, cvtest::norm(ref, m_out_mat, NORM_INF));
  169. }
  170. TEST_F(GAPIHeteroTest, TestBoth)
  171. {
  172. EXPECT_TRUE(cv::gapi::cpu::backend() == m_hetero_kernels.lookup<I::Foo>());
  173. EXPECT_TRUE(cv::gapi::fluid::backend() == m_hetero_kernels.lookup<I::Bar>());
  174. cv::Mat ref = fluidBar(ocvFoo(m_in_mat), ocvFoo(m_in_mat));
  175. EXPECT_NO_THROW(m_comp.apply(m_in_mat, m_out_mat, cv::compile_args(m_hetero_kernels)));
  176. EXPECT_EQ(0, cvtest::norm(ref, m_out_mat, NORM_INF));
  177. }
  178. struct GAPIBigHeteroTest : public ::testing::TestWithParam<std::array<int, 9>>
  179. {
  180. cv::GComputation m_comp;
  181. cv::GKernelPackage m_kernels;
  182. cv::Mat m_in_mat;
  183. cv::Mat m_out_mat1;
  184. cv::Mat m_out_mat2;
  185. cv::Mat m_ref_mat1;
  186. cv::Mat m_ref_mat2;
  187. GAPIBigHeteroTest();
  188. };
  189. // Foo7
  190. // .-> Foo2 -> Foo3 -<
  191. // Foo0 -> Foo1 Bar -> Foo6
  192. // `-> Foo4 -> Foo5 -`
  193. GAPIBigHeteroTest::GAPIBigHeteroTest()
  194. : m_comp([&](){
  195. auto flags = GetParam();
  196. std::array<std::function<cv::GMat(cv::GMat)>, 8> foos;
  197. for (int i = 0; i < 8; i++)
  198. {
  199. foos[i] = flags[i] ? &I::Foo::on : &FluidFooI::on;
  200. }
  201. auto bar = flags[8] ? &I::Bar::on : &FluidBarI::on;
  202. cv::GMat in;
  203. auto foo1Out = foos[1](foos[0](in));
  204. auto foo3Out = foos[3](foos[2](foo1Out));
  205. auto foo6Out = foos[6](bar(foo3Out,
  206. foos[5](foos[4](foo1Out))));
  207. auto foo7Out = foos[7](foo3Out);
  208. return cv::GComputation(GIn(in), GOut(foo6Out, foo7Out));
  209. })
  210. , m_kernels(cv::gapi::kernels<OCVFoo, OCVBar, FluidFoo, FluidBar>())
  211. , m_in_mat(cv::Mat::eye(cv::Size(64, 64), CV_8UC1))
  212. {
  213. auto flags = GetParam();
  214. std::array<std::function<cv::Mat(cv::Mat)>, 8> foos;
  215. for (int i = 0; i < 8; i++)
  216. {
  217. foos[i] = flags[i] ? ocvFoo : fluidFoo;
  218. }
  219. auto bar = flags[8] ? ocvBar : fluidBar;
  220. cv::Mat foo1OutMat = foos[1](foos[0](m_in_mat));
  221. cv::Mat foo3OutMat = foos[3](foos[2](foo1OutMat));
  222. m_ref_mat1 = foos[6](bar(foo3OutMat,
  223. foos[5](foos[4](foo1OutMat))));
  224. m_ref_mat2 = foos[7](foo3OutMat);
  225. }
  226. TEST_P(GAPIBigHeteroTest, Test)
  227. {
  228. EXPECT_NO_THROW(m_comp.apply(gin(m_in_mat), gout(m_out_mat1, m_out_mat2), cv::compile_args(m_kernels)));
  229. EXPECT_EQ(0, cvtest::norm(m_ref_mat1, m_out_mat1, NORM_INF));
  230. EXPECT_EQ(0, cvtest::norm(m_ref_mat2 != m_out_mat2, NORM_INF));
  231. }
  232. static auto configurations = []()
  233. {
  234. // Fill all possible configurations
  235. // from 000000000 to 111111111
  236. std::array<std::array<int, 9>, 512> arr;
  237. for (auto n = 0; n < 512; n++)
  238. {
  239. for (auto i = 0; i < 9; i++)
  240. {
  241. arr[n][i] = (n >> (8 - i)) & 1;
  242. }
  243. }
  244. return arr;
  245. }();
  246. INSTANTIATE_TEST_CASE_P(GAPIBigHeteroTest, GAPIBigHeteroTest,
  247. ::testing::ValuesIn(configurations));
  248. TEST(GAPIHeteroTestLPI, Test)
  249. {
  250. cv::GMat in;
  251. auto mid = FluidFooI::on(in);
  252. auto out = FluidFooI::on(mid);
  253. cv::gapi::island("isl0", GIn(in), GOut(mid));
  254. cv::gapi::island("isl1", GIn(mid), GOut(out));
  255. cv::GComputation c(in, out);
  256. cv::Mat in_mat = cv::Mat::eye(cv::Size(64, 64), CV_8UC1);
  257. cv::Mat out_mat;
  258. EXPECT_NO_THROW(c.apply(in_mat, out_mat, cv::compile_args(cv::gapi::kernels<FluidFoo2lpi>())));
  259. cv::Mat ref = fluidFoo(fluidFoo(in_mat));
  260. EXPECT_EQ(0, cvtest::norm(ref, out_mat, NORM_INF));
  261. }
  262. } // namespace opencv_test