test_common.hpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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. #ifndef __OPENCV_TEST_COMMON_HPP__
  5. #define __OPENCV_TEST_COMMON_HPP__
  6. #include "opencv2/dnn/utils/inference_engine.hpp"
  7. #ifdef HAVE_OPENCL
  8. #include "opencv2/core/ocl.hpp"
  9. #endif
  10. // src/op_inf_engine.hpp
  11. #define INF_ENGINE_VER_MAJOR_GT(ver) (((INF_ENGINE_RELEASE) / 10000) > ((ver) / 10000))
  12. #define INF_ENGINE_VER_MAJOR_GE(ver) (((INF_ENGINE_RELEASE) / 10000) >= ((ver) / 10000))
  13. #define INF_ENGINE_VER_MAJOR_LT(ver) (((INF_ENGINE_RELEASE) / 10000) < ((ver) / 10000))
  14. #define INF_ENGINE_VER_MAJOR_LE(ver) (((INF_ENGINE_RELEASE) / 10000) <= ((ver) / 10000))
  15. #define INF_ENGINE_VER_MAJOR_EQ(ver) (((INF_ENGINE_RELEASE) / 10000) == ((ver) / 10000))
  16. #define CV_TEST_TAG_DNN_SKIP_OPENCV_BACKEND "dnn_skip_opencv_backend"
  17. #define CV_TEST_TAG_DNN_SKIP_HALIDE "dnn_skip_halide"
  18. #define CV_TEST_TAG_DNN_SKIP_CPU "dnn_skip_cpu"
  19. #define CV_TEST_TAG_DNN_SKIP_OPENCL "dnn_skip_ocl"
  20. #define CV_TEST_TAG_DNN_SKIP_OPENCL_FP16 "dnn_skip_ocl_fp16"
  21. #define CV_TEST_TAG_DNN_SKIP_IE_NN_BUILDER "dnn_skip_ie_nn_builder"
  22. #define CV_TEST_TAG_DNN_SKIP_IE_NGRAPH "dnn_skip_ie_ngraph"
  23. #define CV_TEST_TAG_DNN_SKIP_IE "dnn_skip_ie"
  24. #define CV_TEST_TAG_DNN_SKIP_IE_2018R5 "dnn_skip_ie_2018r5"
  25. #define CV_TEST_TAG_DNN_SKIP_IE_2019R1 "dnn_skip_ie_2019r1"
  26. #define CV_TEST_TAG_DNN_SKIP_IE_2019R1_1 "dnn_skip_ie_2019r1_1"
  27. #define CV_TEST_TAG_DNN_SKIP_IE_2019R2 "dnn_skip_ie_2019r2"
  28. #define CV_TEST_TAG_DNN_SKIP_IE_2019R3 "dnn_skip_ie_2019r3"
  29. #define CV_TEST_TAG_DNN_SKIP_IE_CPU "dnn_skip_ie_cpu"
  30. #define CV_TEST_TAG_DNN_SKIP_IE_OPENCL "dnn_skip_ie_ocl"
  31. #define CV_TEST_TAG_DNN_SKIP_IE_OPENCL_FP16 "dnn_skip_ie_ocl_fp16"
  32. #define CV_TEST_TAG_DNN_SKIP_IE_MYRIAD_2 "dnn_skip_ie_myriad2"
  33. #define CV_TEST_TAG_DNN_SKIP_IE_MYRIAD_X "dnn_skip_ie_myriadx"
  34. #define CV_TEST_TAG_DNN_SKIP_IE_MYRIAD CV_TEST_TAG_DNN_SKIP_IE_MYRIAD_2, CV_TEST_TAG_DNN_SKIP_IE_MYRIAD_X
  35. #define CV_TEST_TAG_DNN_SKIP_IE_ARM_CPU "dnn_skip_ie_arm_cpu"
  36. #define CV_TEST_TAG_DNN_SKIP_VULKAN "dnn_skip_vulkan"
  37. #define CV_TEST_TAG_DNN_SKIP_CUDA "dnn_skip_cuda"
  38. #define CV_TEST_TAG_DNN_SKIP_CUDA_FP16 "dnn_skip_cuda_fp16"
  39. #define CV_TEST_TAG_DNN_SKIP_CUDA_FP32 "dnn_skip_cuda_fp32"
  40. #define CV_TEST_TAG_DNN_SKIP_ONNX_CONFORMANCE "dnn_skip_onnx_conformance"
  41. #define CV_TEST_TAG_DNN_SKIP_PARSER "dnn_skip_parser"
  42. #ifdef HAVE_INF_ENGINE
  43. #if INF_ENGINE_VER_MAJOR_EQ(2018050000)
  44. # define CV_TEST_TAG_DNN_SKIP_IE_VERSION CV_TEST_TAG_DNN_SKIP_IE, CV_TEST_TAG_DNN_SKIP_IE_2018R5
  45. #elif INF_ENGINE_VER_MAJOR_EQ(2019010000)
  46. # if INF_ENGINE_RELEASE < 2019010100
  47. # define CV_TEST_TAG_DNN_SKIP_IE_VERSION CV_TEST_TAG_DNN_SKIP_IE, CV_TEST_TAG_DNN_SKIP_IE_2019R1
  48. # else
  49. # define CV_TEST_TAG_DNN_SKIP_IE_VERSION CV_TEST_TAG_DNN_SKIP_IE, CV_TEST_TAG_DNN_SKIP_IE_2019R1_1
  50. # endif
  51. #elif INF_ENGINE_VER_MAJOR_EQ(2019020000)
  52. # define CV_TEST_TAG_DNN_SKIP_IE_VERSION CV_TEST_TAG_DNN_SKIP_IE, CV_TEST_TAG_DNN_SKIP_IE_2019R2
  53. #elif INF_ENGINE_VER_MAJOR_EQ(2019030000)
  54. # define CV_TEST_TAG_DNN_SKIP_IE_VERSION CV_TEST_TAG_DNN_SKIP_IE, CV_TEST_TAG_DNN_SKIP_IE_2019R3
  55. #endif
  56. #endif // HAVE_INF_ENGINE
  57. #ifndef CV_TEST_TAG_DNN_SKIP_IE_VERSION
  58. # define CV_TEST_TAG_DNN_SKIP_IE_VERSION CV_TEST_TAG_DNN_SKIP_IE
  59. #endif
  60. namespace cv { namespace dnn {
  61. CV__DNN_INLINE_NS_BEGIN
  62. void PrintTo(const cv::dnn::Backend& v, std::ostream* os);
  63. void PrintTo(const cv::dnn::Target& v, std::ostream* os);
  64. using opencv_test::tuple;
  65. using opencv_test::get;
  66. void PrintTo(const tuple<cv::dnn::Backend, cv::dnn::Target> v, std::ostream* os);
  67. CV__DNN_INLINE_NS_END
  68. }} // namespace cv::dnn
  69. namespace opencv_test {
  70. void initDNNTests();
  71. using namespace cv::dnn;
  72. static inline const std::string &getOpenCVExtraDir()
  73. {
  74. return cvtest::TS::ptr()->get_data_path();
  75. }
  76. void normAssert(
  77. cv::InputArray ref, cv::InputArray test, const char *comment = "",
  78. double l1 = 0.00001, double lInf = 0.0001);
  79. std::vector<cv::Rect2d> matToBoxes(const cv::Mat& m);
  80. void normAssertDetections(
  81. const std::vector<int>& refClassIds,
  82. const std::vector<float>& refScores,
  83. const std::vector<cv::Rect2d>& refBoxes,
  84. const std::vector<int>& testClassIds,
  85. const std::vector<float>& testScores,
  86. const std::vector<cv::Rect2d>& testBoxes,
  87. const char *comment = "", double confThreshold = 0.0,
  88. double scores_diff = 1e-5, double boxes_iou_diff = 1e-4);
  89. // For SSD-based object detection networks which produce output of shape 1x1xNx7
  90. // where N is a number of detections and an every detection is represented by
  91. // a vector [batchId, classId, confidence, left, top, right, bottom].
  92. void normAssertDetections(
  93. cv::Mat ref, cv::Mat out, const char *comment = "",
  94. double confThreshold = 0.0, double scores_diff = 1e-5,
  95. double boxes_iou_diff = 1e-4);
  96. // For text detection networks
  97. // Curved text polygon is not supported in the current version.
  98. // (concave polygon is invalid input to intersectConvexConvex)
  99. void normAssertTextDetections(
  100. const std::vector<std::vector<Point>>& gtPolys,
  101. const std::vector<std::vector<Point>>& testPolys,
  102. const char *comment = "", double boxes_iou_diff = 1e-4);
  103. void readFileContent(const std::string& filename, CV_OUT std::vector<char>& content);
  104. #ifdef HAVE_INF_ENGINE
  105. bool validateVPUType();
  106. #endif
  107. testing::internal::ParamGenerator< tuple<Backend, Target> > dnnBackendsAndTargets(
  108. bool withInferenceEngine = true,
  109. bool withHalide = false,
  110. bool withCpuOCV = true,
  111. bool withVkCom = true,
  112. bool withCUDA = true,
  113. bool withNgraph = true,
  114. bool withWebnn = true
  115. );
  116. testing::internal::ParamGenerator< tuple<Backend, Target> > dnnBackendsAndTargetsIE();
  117. class DNNTestLayer : public TestWithParam<tuple<Backend, Target> >
  118. {
  119. public:
  120. dnn::Backend backend;
  121. dnn::Target target;
  122. double default_l1, default_lInf;
  123. DNNTestLayer()
  124. {
  125. backend = (dnn::Backend)(int)get<0>(GetParam());
  126. target = (dnn::Target)(int)get<1>(GetParam());
  127. getDefaultThresholds(backend, target, &default_l1, &default_lInf);
  128. }
  129. static void getDefaultThresholds(int backend, int target, double* l1, double* lInf)
  130. {
  131. if (target == DNN_TARGET_CUDA_FP16 || target == DNN_TARGET_OPENCL_FP16 || target == DNN_TARGET_MYRIAD)
  132. {
  133. *l1 = 4e-3;
  134. *lInf = 2e-2;
  135. }
  136. else
  137. {
  138. *l1 = 1e-5;
  139. *lInf = 1e-4;
  140. }
  141. }
  142. static void checkBackend(int backend, int target, Mat* inp = 0, Mat* ref = 0)
  143. {
  144. CV_UNUSED(backend); CV_UNUSED(target); CV_UNUSED(inp); CV_UNUSED(ref);
  145. #if defined(INF_ENGINE_RELEASE) && INF_ENGINE_VER_MAJOR_LT(2021000000)
  146. if ((backend == DNN_BACKEND_INFERENCE_ENGINE_NN_BUILDER_2019 || backend == DNN_BACKEND_INFERENCE_ENGINE_NGRAPH)
  147. && target == DNN_TARGET_MYRIAD)
  148. {
  149. if (inp && ref && inp->dims == 4 && ref->dims == 4 &&
  150. inp->size[0] != 1 && inp->size[0] != ref->size[0])
  151. {
  152. std::cout << "Inconsistent batch size of input and output blobs for Myriad plugin" << std::endl;
  153. applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_MYRIAD);
  154. }
  155. }
  156. #endif
  157. }
  158. void expectNoFallbacks(Net& net, bool raiseError = true)
  159. {
  160. // Check if all the layers are supported with current backend and target.
  161. // Some layers might be fused so their timings equal to zero.
  162. std::vector<double> timings;
  163. net.getPerfProfile(timings);
  164. std::vector<String> names = net.getLayerNames();
  165. CV_Assert(names.size() == timings.size());
  166. bool hasFallbacks = false;
  167. for (int i = 0; i < names.size(); ++i)
  168. {
  169. Ptr<dnn::Layer> l = net.getLayer(net.getLayerId(names[i]));
  170. bool fused = !timings[i];
  171. if ((!l->supportBackend(backend) || l->preferableTarget != target) && !fused)
  172. {
  173. hasFallbacks = true;
  174. std::cout << "FALLBACK: Layer [" << l->type << "]:[" << l->name << "] is expected to has backend implementation" << endl;
  175. }
  176. }
  177. if (hasFallbacks && raiseError)
  178. CV_Error(Error::StsNotImplemented, "Implementation fallbacks are not expected in this test");
  179. }
  180. void expectNoFallbacksFromIE(Net& net)
  181. {
  182. if (backend == DNN_BACKEND_INFERENCE_ENGINE_NN_BUILDER_2019)
  183. expectNoFallbacks(net);
  184. if (backend == DNN_BACKEND_INFERENCE_ENGINE_NGRAPH)
  185. expectNoFallbacks(net, false);
  186. }
  187. void expectNoFallbacksFromCUDA(Net& net)
  188. {
  189. if (backend == DNN_BACKEND_CUDA)
  190. expectNoFallbacks(net);
  191. }
  192. protected:
  193. void checkBackend(Mat* inp = 0, Mat* ref = 0)
  194. {
  195. checkBackend(backend, target, inp, ref);
  196. }
  197. };
  198. } // namespace
  199. #endif