gapi_int_dynamic_graph.cpp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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. #include "../test_precomp.hpp"
  7. #include <opencv2/gapi/cpu/core.hpp>
  8. #include <opencv2/gapi/cpu/imgproc.hpp>
  9. namespace opencv_test
  10. {
  11. typedef ::testing::Types<cv::GMat, cv::GMatP, cv::GFrame,
  12. cv::GScalar, cv::GOpaque<int>,
  13. cv::GArray<int>> VectorProtoTypes;
  14. template<typename T> struct DynamicGraphProtoArgs: public ::testing::Test { using Type = T; };
  15. TYPED_TEST_CASE(DynamicGraphProtoArgs, VectorProtoTypes);
  16. TYPED_TEST(DynamicGraphProtoArgs, AddProtoInputArgsSmoke)
  17. {
  18. using T = typename TestFixture::Type;
  19. auto ins = GIn();
  20. T in;
  21. EXPECT_NO_THROW(ins += GIn(in));
  22. }
  23. TYPED_TEST(DynamicGraphProtoArgs, AddProtoInputArgs)
  24. {
  25. using T = typename TestFixture::Type;
  26. T in1, in2;
  27. auto ins1 = GIn();
  28. ins1 += GIn(in1);
  29. ins1 += GIn(in2);
  30. auto ins2 = GIn(in1, in2);
  31. EXPECT_EQ(ins1.m_args.size(), ins2.m_args.size());
  32. }
  33. TYPED_TEST(DynamicGraphProtoArgs, AddProtoOutputArgsSmoke)
  34. {
  35. using T = typename TestFixture::Type;
  36. auto outs = GOut();
  37. T out;
  38. EXPECT_NO_THROW(outs += GOut(out));
  39. }
  40. TYPED_TEST(DynamicGraphProtoArgs, AddProtoOutputArgs)
  41. {
  42. using T = typename TestFixture::Type;
  43. T out1, out2;
  44. auto outs1 = GOut();
  45. outs1 += GOut(out1);
  46. outs1 += GOut(out2);
  47. auto outs2 = GOut(out1, out2);
  48. EXPECT_EQ(outs1.m_args.size(), outs2.m_args.size());
  49. }
  50. typedef ::testing::Types<cv::Mat,
  51. #if !defined(GAPI_STANDALONE)
  52. cv::UMat,
  53. #endif // !defined(GAPI_STANDALONE)
  54. cv::Scalar,
  55. cv::detail::VectorRef,
  56. cv::detail::OpaqueRef> VectorRunTypes;
  57. template<typename T> struct DynamicGraphRunArgs: public ::testing::Test { using Type = T; };
  58. TYPED_TEST_CASE(DynamicGraphRunArgs, VectorRunTypes);
  59. TYPED_TEST(DynamicGraphRunArgs, AddRunArgsSmoke)
  60. {
  61. auto in_vector = cv::gin();
  62. using T = typename TestFixture::Type;
  63. T in;
  64. EXPECT_NO_THROW(in_vector += cv::gin(in));
  65. }
  66. TYPED_TEST(DynamicGraphRunArgs, AddRunArgs)
  67. {
  68. using T = typename TestFixture::Type;
  69. T in1, in2;
  70. auto in_vector1 = cv::gin();
  71. in_vector1 += cv::gin(in1);
  72. in_vector1 += cv::gin(in2);
  73. auto in_vector2 = cv::gin(in1, in2);
  74. EXPECT_EQ(in_vector1.size(), in_vector2.size());
  75. }
  76. TYPED_TEST(DynamicGraphRunArgs, AddRunArgsPSmoke)
  77. {
  78. auto out_vector = cv::gout();
  79. using T = typename TestFixture::Type;
  80. T out;
  81. EXPECT_NO_THROW(out_vector += cv::gout(out));
  82. }
  83. TYPED_TEST(DynamicGraphRunArgs, AddRunArgsP)
  84. {
  85. using T = typename TestFixture::Type;
  86. T out1, out2;
  87. auto out_vector1 = cv::gout();
  88. out_vector1 += cv::gout(out1);
  89. out_vector1 += cv::gout(out2);
  90. auto out_vector2 = cv::gout(out1, out2);
  91. EXPECT_EQ(out_vector1.size(), out_vector2.size());
  92. }
  93. TEST(DynamicGraph, ProtoInputArgsExecute)
  94. {
  95. cv::GComputation cc([]() {
  96. cv::GMat in1;
  97. auto ins = GIn(in1);
  98. cv::GMat in2;
  99. ins += GIn(in2);
  100. cv::GMat out = cv::gapi::copy(in1 + in2);
  101. return cv::GComputation(std::move(ins), GOut(out));
  102. });
  103. cv::Mat in_mat1 = cv::Mat::eye(32, 32, CV_8UC1);
  104. cv::Mat in_mat2 = cv::Mat::eye(32, 32, CV_8UC1);
  105. cv::Mat out_mat;
  106. EXPECT_NO_THROW(cc.apply(cv::gin(in_mat1, in_mat2), cv::gout(out_mat)));
  107. }
  108. TEST(DynamicGraph, ProtoOutputArgsExecute)
  109. {
  110. cv::GComputation cc([]() {
  111. cv::GMat in;
  112. cv::GMat out1 = cv::gapi::copy(in);
  113. auto outs = GOut(out1);
  114. cv::GMat out2 = cv::gapi::copy(in);
  115. outs += GOut(out2);
  116. return cv::GComputation(cv::GIn(in), std::move(outs));
  117. });
  118. cv::Mat in_mat1 = cv::Mat::eye(32, 32, CV_8UC1);
  119. cv::Mat out_mat1;
  120. cv::Mat out_mat2;
  121. EXPECT_NO_THROW(cc.apply(cv::gin(in_mat1), cv::gout(out_mat1, out_mat1)));
  122. }
  123. TEST(DynamicGraph, ProtoOutputInputArgsExecute)
  124. {
  125. cv::GComputation cc([]() {
  126. cv::GMat in1;
  127. auto ins = GIn(in1);
  128. cv::GMat in2;
  129. ins += GIn(in2);
  130. cv::GMat out1 = cv::gapi::copy(in1 + in2);
  131. auto outs = GOut(out1);
  132. cv::GMat out2 = cv::gapi::copy(in1 + in2);
  133. outs += GOut(out2);
  134. return cv::GComputation(std::move(ins), std::move(outs));
  135. });
  136. cv::Mat in_mat1 = cv::Mat::eye(32, 32, CV_8UC1);
  137. cv::Mat in_mat2 = cv::Mat::eye(32, 32, CV_8UC1);
  138. cv::Mat out_mat1, out_mat2;
  139. EXPECT_NO_THROW(cc.apply(cv::gin(in_mat1, in_mat2), cv::gout(out_mat1, out_mat2)));
  140. }
  141. TEST(DynamicGraph, ProtoArgsExecute)
  142. {
  143. cv::GComputation cc([]() {
  144. cv::GMat in1;
  145. auto ins = GIn(in1);
  146. cv::GMat in2;
  147. ins += GIn(in2);
  148. cv::GMat out1 = cv::gapi::copy(in1 + in2);
  149. auto outs = GOut(out1);
  150. cv::GMat out2 = cv::gapi::copy(in1 + in2);
  151. outs += GOut(out2);
  152. return cv::GComputation(std::move(ins), std::move(outs));
  153. });
  154. cv::Mat in_mat1 = cv::Mat::eye(32, 32, CV_8UC1);
  155. cv::Mat in_mat2 = cv::Mat::eye(32, 32, CV_8UC1);
  156. cv::Mat out_mat1, out_mat2;
  157. EXPECT_NO_THROW(cc.apply(cv::gin(in_mat1, in_mat2), cv::gout(out_mat1, out_mat2)));
  158. }
  159. TEST(DynamicGraph, ProtoOutputInputArgsAccuracy)
  160. {
  161. cv::Size szOut(4, 4);
  162. cv::GComputation cc([&](){
  163. cv::GMat in1;
  164. auto ins = GIn(in1);
  165. cv::GMat in2;
  166. ins += GIn(in2);
  167. cv::GMat out1 = cv::gapi::resize(in1, szOut);
  168. auto outs = GOut(out1);
  169. cv::GMat out2 = cv::gapi::resize(in2, szOut);
  170. outs += GOut(out2);
  171. return cv::GComputation(std::move(ins), std::move(outs));
  172. });
  173. // G-API test code
  174. cv::Mat in_mat1( 8, 8, CV_8UC3);
  175. cv::Mat in_mat2(16, 16, CV_8UC3);
  176. cv::randu(in_mat1, cv::Scalar::all(0), cv::Scalar::all(255));
  177. cv::randu(in_mat2, cv::Scalar::all(0), cv::Scalar::all(255));
  178. auto in_vector = cv::gin();
  179. in_vector += cv::gin(in_mat1);
  180. in_vector += cv::gin(in_mat2);
  181. cv::Mat out_mat1, out_mat2;
  182. auto out_vector = cv::gout();
  183. out_vector += cv::gout(out_mat1);
  184. out_vector += cv::gout(out_mat2);
  185. cc.apply(std::move(in_vector), std::move(out_vector));
  186. // OCV ref code
  187. cv::Mat cv_out_mat1, cv_out_mat2;
  188. cv::resize(in_mat1, cv_out_mat1, szOut);
  189. cv::resize(in_mat2, cv_out_mat2, szOut);
  190. EXPECT_EQ(0, cvtest::norm(out_mat1, cv_out_mat1, NORM_INF));
  191. EXPECT_EQ(0, cvtest::norm(out_mat2, cv_out_mat2, NORM_INF));
  192. }
  193. TEST(DynamicGraph, Streaming)
  194. {
  195. cv::GComputation cc([&](){
  196. cv::Size szOut(4, 4);
  197. cv::GMat in1;
  198. auto ins = GIn(in1);
  199. cv::GMat in2;
  200. ins += GIn(in2);
  201. cv::GMat out1 = cv::gapi::resize(in1, szOut);
  202. auto outs = GOut(out1);
  203. cv::GMat out2 = cv::gapi::resize(in2, szOut);
  204. outs += GOut(out2);
  205. return cv::GComputation(std::move(ins), std::move(outs));
  206. });
  207. EXPECT_NO_THROW(cc.compileStreaming(cv::compile_args(cv::gapi::core::cpu::kernels())));
  208. }
  209. TEST(DynamicGraph, StreamingAccuracy)
  210. {
  211. cv::Size szOut(4, 4);
  212. cv::GComputation cc([&](){
  213. cv::GMat in1;
  214. auto ins = GIn(in1);
  215. cv::GMat in2;
  216. ins += GIn(in2);
  217. cv::GMat out1 = cv::gapi::resize(in1, szOut);
  218. cv::GProtoOutputArgs outs = GOut(out1);
  219. cv::GMat out2 = cv::gapi::resize(in2, szOut);
  220. outs += GOut(out2);
  221. return cv::GComputation(std::move(ins), std::move(outs));
  222. });
  223. // G-API test code
  224. cv::Mat in_mat1( 8, 8, CV_8UC3);
  225. cv::Mat in_mat2(16, 16, CV_8UC3);
  226. cv::randu(in_mat1, cv::Scalar::all(0), cv::Scalar::all(255));
  227. cv::randu(in_mat2, cv::Scalar::all(0), cv::Scalar::all(255));
  228. auto in_vector = cv::gin();
  229. in_vector += cv::gin(in_mat1);
  230. in_vector += cv::gin(in_mat2);
  231. cv::Mat out_mat1, out_mat2;
  232. auto out_vector = cv::gout();
  233. out_vector += cv::gout(out_mat1);
  234. out_vector += cv::gout(out_mat2);
  235. auto stream = cc.compileStreaming(cv::compile_args(cv::gapi::core::cpu::kernels()));
  236. stream.setSource(std::move(in_vector));
  237. stream.start();
  238. stream.pull(std::move(out_vector));
  239. stream.stop();
  240. // OCV ref code
  241. cv::Mat cv_out_mat1, cv_out_mat2;
  242. cv::resize(in_mat1, cv_out_mat1, szOut);
  243. cv::resize(in_mat2, cv_out_mat2, szOut);
  244. EXPECT_EQ(0, cvtest::norm(out_mat1, cv_out_mat1, NORM_INF));
  245. EXPECT_EQ(0, cvtest::norm(out_mat2, cv_out_mat2, NORM_INF));
  246. }
  247. } // namespace opencv_test