gapi_int_recompilation_test.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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 "../common/gapi_tests_common.hpp"
  8. #include "api/gcomputation_priv.hpp"
  9. #include <opencv2/gapi/fluid/gfluidkernel.hpp>
  10. #include <opencv2/gapi/fluid/core.hpp>
  11. #include <opencv2/gapi/fluid/imgproc.hpp>
  12. namespace opencv_test
  13. {
  14. TEST(GComputationCompile, NoRecompileWithSameMeta)
  15. {
  16. cv::GMat in;
  17. cv::GComputation cc(in, in+in);
  18. cv::Mat in_mat1 = cv::Mat::eye (32, 32, CV_8UC1);
  19. cv::Mat in_mat2 = cv::Mat::zeros(32, 32, CV_8UC1);
  20. cv::Mat out_mat;
  21. cc.apply(in_mat1, out_mat);
  22. auto comp1 = cc.priv().m_lastCompiled;
  23. cc.apply(in_mat2, out_mat);
  24. auto comp2 = cc.priv().m_lastCompiled;
  25. // Both compiled objects are actually the same unique executable
  26. EXPECT_EQ(&comp1.priv(), &comp2.priv());
  27. }
  28. TEST(GComputationCompile, NoRecompileWithWrongMeta)
  29. {
  30. cv::GMat in;
  31. cv::GComputation cc(in, in+in);
  32. cv::Mat in_mat1 = cv::Mat::eye (32, 32, CV_8UC1);
  33. cv::Mat in_mat2 = cv::Mat::zeros(32, 32, CV_8UC1);
  34. cv::Mat out_mat;
  35. cc.apply(in_mat1, out_mat);
  36. auto comp1 = cc.priv().m_lastCompiled;
  37. EXPECT_THROW(cc.apply(cv::gin(cv::Scalar(128)), cv::gout(out_mat)), std::logic_error);
  38. auto comp2 = cc.priv().m_lastCompiled;
  39. // Both compiled objects are actually the same unique executable
  40. EXPECT_EQ(&comp1.priv(), &comp2.priv());
  41. }
  42. TEST(GComputationCompile, RecompileWithDifferentMeta)
  43. {
  44. cv::GMat in;
  45. cv::GComputation cc(in, in+in);
  46. cv::Mat in_mat1 = cv::Mat::eye (32, 32, CV_8UC1);
  47. cv::Mat in_mat2 = cv::Mat::zeros(64, 64, CV_32F);
  48. cv::Mat out_mat;
  49. cc.apply(in_mat1, out_mat);
  50. auto comp1 = cc.priv().m_lastCompiled;
  51. cc.apply(in_mat2, out_mat);
  52. auto comp2 = cc.priv().m_lastCompiled;
  53. // Both compiled objects are different
  54. EXPECT_NE(&comp1.priv(), &comp2.priv());
  55. }
  56. TEST(GComputationCompile, FluidReshapeWithDifferentDims)
  57. {
  58. cv::GMat in;
  59. cv::GComputation cc(in, in+in);
  60. cv::Mat in_mat1 = cv::Mat::eye (32, 32, CV_8UC1);
  61. cv::Mat in_mat2 = cv::Mat::zeros(64, 64, CV_8UC1);
  62. cv::Mat out_mat;
  63. cc.apply(in_mat1, out_mat, cv::compile_args(cv::gapi::core::fluid::kernels()));
  64. auto comp1 = cc.priv().m_lastCompiled;
  65. cc.apply(in_mat2, out_mat);
  66. auto comp2 = cc.priv().m_lastCompiled;
  67. // Both compiled objects are actually the same unique executable
  68. EXPECT_EQ(&comp1.priv(), &comp2.priv());
  69. }
  70. TEST(GComputationCompile, FluidReshapeResizeDownScale)
  71. {
  72. cv::Size szOut(4, 4);
  73. cv::GMat in;
  74. cv::GComputation cc(in, cv::gapi::resize(in, szOut));
  75. cv::Mat in_mat1( 8, 8, CV_8UC3);
  76. cv::Mat in_mat2(16, 16, CV_8UC3);
  77. cv::randu(in_mat1, cv::Scalar::all(0), cv::Scalar::all(255));
  78. cv::randu(in_mat2, cv::Scalar::all(0), cv::Scalar::all(255));
  79. cv::Mat out_mat1, out_mat2;
  80. cc.apply(in_mat1, out_mat1, cv::compile_args(cv::gapi::imgproc::fluid::kernels()));
  81. auto comp1 = cc.priv().m_lastCompiled;
  82. cc.apply(in_mat2, out_mat2);
  83. auto comp2 = cc.priv().m_lastCompiled;
  84. // Both compiled objects are actually the same unique executable
  85. EXPECT_EQ(&comp1.priv(), &comp2.priv());
  86. cv::Mat cv_out_mat1, cv_out_mat2;
  87. cv::resize(in_mat1, cv_out_mat1, szOut);
  88. cv::resize(in_mat2, cv_out_mat2, szOut);
  89. // Fluid's and OpenCV's resizes aren't bit exact.
  90. // So 1 is here because it is max difference between them.
  91. EXPECT_TRUE(Tolerance_FloatRel_IntAbs(1e-5, 1).to_compare_f()(out_mat1, cv_out_mat1));
  92. EXPECT_TRUE(Tolerance_FloatRel_IntAbs(1e-5, 1).to_compare_f()(out_mat2, cv_out_mat2));
  93. }
  94. TEST(GComputationCompile, FluidReshapeSwitchToUpscaleFromDownscale)
  95. {
  96. cv::Size szOut(4, 4);
  97. cv::GMat in;
  98. cv::GComputation cc(in, cv::gapi::resize(in, szOut));
  99. cv::Mat in_mat1( 8, 8, CV_8UC3);
  100. cv::Mat in_mat2( 2, 2, CV_8UC3);
  101. cv::Mat in_mat3(16, 16, CV_8UC3);
  102. cv::randu(in_mat1, cv::Scalar::all(0), cv::Scalar::all(255));
  103. cv::randu(in_mat2, cv::Scalar::all(0), cv::Scalar::all(255));
  104. cv::randu(in_mat3, cv::Scalar::all(0), cv::Scalar::all(255));
  105. cv::Mat out_mat1, out_mat2, out_mat3;
  106. cc.apply(in_mat1, out_mat1, cv::compile_args(cv::gapi::imgproc::fluid::kernels()));
  107. auto comp1 = cc.priv().m_lastCompiled;
  108. cc.apply(in_mat2, out_mat2);
  109. auto comp2 = cc.priv().m_lastCompiled;
  110. cc.apply(in_mat3, out_mat3);
  111. auto comp3 = cc.priv().m_lastCompiled;
  112. EXPECT_EQ(&comp1.priv(), &comp2.priv());
  113. EXPECT_EQ(&comp1.priv(), &comp3.priv());
  114. cv::Mat cv_out_mat1, cv_out_mat2, cv_out_mat3;
  115. cv::resize(in_mat1, cv_out_mat1, szOut);
  116. cv::resize(in_mat2, cv_out_mat2, szOut);
  117. cv::resize(in_mat3, cv_out_mat3, szOut);
  118. // Fluid's and OpenCV's Resizes aren't bit exact.
  119. // So 1 is here because it is max difference between them.
  120. EXPECT_TRUE(Tolerance_FloatRel_IntAbs(1e-5, 1).to_compare_f()(out_mat1, cv_out_mat1));
  121. EXPECT_TRUE(Tolerance_FloatRel_IntAbs(1e-5, 1).to_compare_f()(out_mat2, cv_out_mat2));
  122. EXPECT_TRUE(Tolerance_FloatRel_IntAbs(1e-5, 1).to_compare_f()(out_mat3, cv_out_mat3));
  123. }
  124. TEST(GComputationCompile, ReshapeBlur)
  125. {
  126. cv::Size kernelSize{3, 3};
  127. cv::GMat in;
  128. cv::GComputation cc(in, cv::gapi::blur(in, kernelSize));
  129. cv::Mat in_mat1( 8, 8, CV_8UC1);
  130. cv::Mat in_mat2(16, 16, CV_8UC1);
  131. cv::randu(in_mat1, cv::Scalar::all(0), cv::Scalar::all(255));
  132. cv::randu(in_mat2, cv::Scalar::all(0), cv::Scalar::all(255));
  133. cv::Mat out_mat1, out_mat2;
  134. cc.apply(in_mat1, out_mat1, cv::compile_args(cv::gapi::imgproc::fluid::kernels()));
  135. auto comp1 = cc.priv().m_lastCompiled;
  136. cc.apply(in_mat2, out_mat2);
  137. auto comp2 = cc.priv().m_lastCompiled;
  138. // Both compiled objects are actually the same unique executable
  139. EXPECT_EQ(&comp1.priv(), &comp2.priv());
  140. cv::Mat cv_out_mat1, cv_out_mat2;
  141. cv::blur(in_mat1, cv_out_mat1, kernelSize);
  142. cv::blur(in_mat2, cv_out_mat2, kernelSize);
  143. EXPECT_EQ(0, cvtest::norm(out_mat1, cv_out_mat1, NORM_INF));
  144. EXPECT_EQ(0, cvtest::norm(out_mat2, cv_out_mat2, NORM_INF));
  145. }
  146. TEST(GComputationCompile, ReshapeRois)
  147. {
  148. cv::Size kernelSize{3, 3};
  149. cv::Size szOut(8, 8);
  150. cv::GMat in;
  151. auto blurred = cv::gapi::blur(in, kernelSize);
  152. cv::GComputation cc(in, cv::gapi::resize(blurred, szOut));
  153. cv::Mat first_in_mat(8, 8, CV_8UC3);
  154. cv::randn(first_in_mat, cv::Scalar::all(127), cv::Scalar::all(40.f));
  155. cv::Mat first_out_mat;
  156. auto fluidKernels = cv::gapi::combine(gapi::imgproc::fluid::kernels(),
  157. gapi::core::fluid::kernels());
  158. cc.apply(first_in_mat, first_out_mat, cv::compile_args(fluidKernels));
  159. auto first_comp = cc.priv().m_lastCompiled;
  160. constexpr int niter = 4;
  161. for (int i = 0; i < niter; i++)
  162. {
  163. int width = 4 + 2*i;
  164. int height = width;
  165. cv::Mat in_mat(width, height, CV_8UC3);
  166. cv::randn(in_mat, cv::Scalar::all(127), cv::Scalar::all(40.f));
  167. cv::Mat out_mat = cv::Mat::zeros(szOut, CV_8UC3);
  168. int x = 0;
  169. int y = szOut.height * i / niter;
  170. int roiW = szOut.width;
  171. int roiH = szOut.height / niter;
  172. cv::Rect roi{x, y, roiW, roiH};
  173. cc.apply(in_mat, out_mat, cv::compile_args(cv::GFluidOutputRois{{roi}}));
  174. auto comp = cc.priv().m_lastCompiled;
  175. EXPECT_EQ(&first_comp.priv(), &comp.priv());
  176. cv::Mat blur_mat, cv_out_mat;
  177. cv::blur(in_mat, blur_mat, kernelSize);
  178. cv::resize(blur_mat, cv_out_mat, szOut);
  179. // Fluid's and OpenCV's resizes aren't bit exact.
  180. // So 1 is here because it is max difference between them.
  181. EXPECT_TRUE(Tolerance_FloatRel_IntAbs(1e-5, 1).to_compare_f()(out_mat(roi), cv_out_mat(roi)));
  182. }
  183. }
  184. } // opencv_test