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