1 /*M///////////////////////////////////////////////////////////////////////////////////////
3 // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
5 // By downloading, copying, installing or using the software you agree to this license.
6 // If you do not agree to this license, do not download, install,
7 // copy or use the software.
11 // For Open Source Computer Vision Library
13 // Copyright (C) 2013, OpenCV Foundation, all rights reserved.
14 // Third party copyrights are property of their respective owners.
16 // Redistribution and use in source and binary forms, with or without modification,
17 // are permitted provided that the following conditions are met:
19 // * Redistribution's of source code must retain the above copyright notice,
20 // this list of conditions and the following disclaimer.
22 // * Redistribution's in binary form must reproduce the above copyright notice,
23 // this list of conditions and the following disclaimer in the documentation
24 // and/or other materials provided with the distribution.
26 // * The name of the copyright holders may not be used to endorse or promote products
27 // derived from this software without specific prior written permission.
29 // This software is provided by the copyright holders and contributors "as is" and
30 // any express or implied warranties, including, but not limited to, the implied
31 // warranties of merchantability and fitness for a particular purpose are disclaimed.
32 // In no event shall the Intel Corporation or contributors be liable for any direct,
33 // indirect, incidental, special, exemplary, or consequential damages
34 // (including, but not limited to, procurement of substitute goods or services;
35 // loss of use, data, or profits; or business interruption) however caused
36 // and on any theory of liability, whether in contract, strict liability,
37 // or tort (including negligence or otherwise) arising in any way out of
38 // the use of this software, even if advised of the possibility of such damage.
42 #ifndef __OPENCV_TEST_COMMON_HPP__
43 #define __OPENCV_TEST_COMMON_HPP__
46 #include "opencv2/core/ocl.hpp"
49 namespace cv { namespace dnn {
50 CV__DNN_EXPERIMENTAL_NS_BEGIN
51 static inline void PrintTo(const cv::dnn::Backend& v, std::ostream* os)
54 case DNN_BACKEND_DEFAULT: *os << "DEFAULT"; return;
55 case DNN_BACKEND_HALIDE: *os << "HALIDE"; return;
56 case DNN_BACKEND_INFERENCE_ENGINE: *os << "DLIE"; return;
57 case DNN_BACKEND_OPENCV: *os << "OCV"; return;
58 } // don't use "default:" to emit compiler warnings
59 *os << "DNN_BACKEND_UNKNOWN(" << (int)v << ")";
62 static inline void PrintTo(const cv::dnn::Target& v, std::ostream* os)
65 case DNN_TARGET_CPU: *os << "CPU"; return;
66 case DNN_TARGET_OPENCL: *os << "OCL"; return;
67 case DNN_TARGET_OPENCL_FP16: *os << "OCL_FP16"; return;
68 case DNN_TARGET_MYRIAD: *os << "MYRIAD"; return;
69 case DNN_TARGET_FPGA: *os << "FPGA"; return;
70 } // don't use "default:" to emit compiler warnings
71 *os << "DNN_TARGET_UNKNOWN(" << (int)v << ")";
74 using opencv_test::tuple;
75 using opencv_test::get;
76 static inline void PrintTo(const tuple<cv::dnn::Backend, cv::dnn::Target> v, std::ostream* os)
78 PrintTo(get<0>(v), os);
80 PrintTo(get<1>(v), os);
83 CV__DNN_EXPERIMENTAL_NS_END
87 static inline const std::string &getOpenCVExtraDir()
89 return cvtest::TS::ptr()->get_data_path();
92 static inline void normAssert(cv::InputArray ref, cv::InputArray test, const char *comment = "",
93 double l1 = 0.00001, double lInf = 0.0001)
95 double normL1 = cvtest::norm(ref, test, cv::NORM_L1) / ref.getMat().total();
96 EXPECT_LE(normL1, l1) << comment;
98 double normInf = cvtest::norm(ref, test, cv::NORM_INF);
99 EXPECT_LE(normInf, lInf) << comment;
102 static std::vector<cv::Rect2d> matToBoxes(const cv::Mat& m)
104 EXPECT_EQ(m.type(), CV_32FC1);
105 EXPECT_EQ(m.dims, 2);
106 EXPECT_EQ(m.cols, 4);
108 std::vector<cv::Rect2d> boxes(m.rows);
109 for (int i = 0; i < m.rows; ++i)
111 CV_Assert(m.row(i).isContinuous());
112 const float* data = m.ptr<float>(i);
113 double l = data[0], t = data[1], r = data[2], b = data[3];
114 boxes[i] = cv::Rect2d(l, t, r - l, b - t);
119 static inline void normAssertDetections(const std::vector<int>& refClassIds,
120 const std::vector<float>& refScores,
121 const std::vector<cv::Rect2d>& refBoxes,
122 const std::vector<int>& testClassIds,
123 const std::vector<float>& testScores,
124 const std::vector<cv::Rect2d>& testBoxes,
125 const char *comment = "", double confThreshold = 0.0,
126 double scores_diff = 1e-5, double boxes_iou_diff = 1e-4)
128 std::vector<bool> matchedRefBoxes(refBoxes.size(), false);
129 for (int i = 0; i < testBoxes.size(); ++i)
131 double testScore = testScores[i];
132 if (testScore < confThreshold)
135 int testClassId = testClassIds[i];
136 const cv::Rect2d& testBox = testBoxes[i];
137 bool matched = false;
138 for (int j = 0; j < refBoxes.size() && !matched; ++j)
140 if (!matchedRefBoxes[j] && testClassId == refClassIds[j] &&
141 std::abs(testScore - refScores[j]) < scores_diff)
143 double interArea = (testBox & refBoxes[j]).area();
144 double iou = interArea / (testBox.area() + refBoxes[j].area() - interArea);
145 if (std::abs(iou - 1.0) < boxes_iou_diff)
148 matchedRefBoxes[j] = true;
153 std::cout << cv::format("Unmatched prediction: class %d score %f box ",
154 testClassId, testScore) << testBox << std::endl;
155 EXPECT_TRUE(matched) << comment;
158 // Check unmatched reference detections.
159 for (int i = 0; i < refBoxes.size(); ++i)
161 if (!matchedRefBoxes[i] && refScores[i] > confThreshold)
163 std::cout << cv::format("Unmatched reference: class %d score %f box ",
164 refClassIds[i], refScores[i]) << refBoxes[i] << std::endl;
165 EXPECT_LE(refScores[i], confThreshold) << comment;
170 // For SSD-based object detection networks which produce output of shape 1x1xNx7
171 // where N is a number of detections and an every detection is represented by
172 // a vector [batchId, classId, confidence, left, top, right, bottom].
173 static inline void normAssertDetections(cv::Mat ref, cv::Mat out, const char *comment = "",
174 double confThreshold = 0.0, double scores_diff = 1e-5,
175 double boxes_iou_diff = 1e-4)
177 CV_Assert(ref.total() % 7 == 0);
178 CV_Assert(out.total() % 7 == 0);
179 ref = ref.reshape(1, ref.total() / 7);
180 out = out.reshape(1, out.total() / 7);
182 cv::Mat refClassIds, testClassIds;
183 ref.col(1).convertTo(refClassIds, CV_32SC1);
184 out.col(1).convertTo(testClassIds, CV_32SC1);
185 std::vector<float> refScores(ref.col(2)), testScores(out.col(2));
186 std::vector<cv::Rect2d> refBoxes = matToBoxes(ref.colRange(3, 7));
187 std::vector<cv::Rect2d> testBoxes = matToBoxes(out.colRange(3, 7));
188 normAssertDetections(refClassIds, refScores, refBoxes, testClassIds, testScores,
189 testBoxes, comment, confThreshold, scores_diff, boxes_iou_diff);
192 static inline bool readFileInMemory(const std::string& filename, std::string& content)
194 std::ios::openmode mode = std::ios::in | std::ios::binary;
195 std::ifstream ifs(filename.c_str(), mode);
201 ifs.seekg(0, std::ios::end);
202 content.reserve(ifs.tellg());
203 ifs.seekg(0, std::ios::beg);
205 content.assign((std::istreambuf_iterator<char>(ifs)),
206 std::istreambuf_iterator<char>());
211 namespace opencv_test {
213 using namespace cv::dnn;
216 testing::internal::ParamGenerator< tuple<Backend, Target> > dnnBackendsAndTargets(
217 bool withInferenceEngine = true,
218 bool withHalide = false,
219 bool withCpuOCV = true
222 std::vector< tuple<Backend, Target> > targets;
223 std::vector< Target > available;
226 available = getAvailableTargets(DNN_BACKEND_HALIDE);
227 for (std::vector< Target >::const_iterator i = available.begin(); i != available.end(); ++i)
228 targets.push_back(make_tuple(DNN_BACKEND_HALIDE, *i));
230 if (withInferenceEngine)
232 available = getAvailableTargets(DNN_BACKEND_INFERENCE_ENGINE);
233 for (std::vector< Target >::const_iterator i = available.begin(); i != available.end(); ++i)
234 targets.push_back(make_tuple(DNN_BACKEND_INFERENCE_ENGINE, *i));
237 available = getAvailableTargets(DNN_BACKEND_OPENCV);
238 for (std::vector< Target >::const_iterator i = available.begin(); i != available.end(); ++i)
240 if (!withCpuOCV && *i == DNN_TARGET_CPU)
242 targets.push_back(make_tuple(DNN_BACKEND_OPENCV, *i));
245 if (targets.empty()) // validate at least CPU mode
246 targets.push_back(make_tuple(DNN_BACKEND_OPENCV, DNN_TARGET_CPU));
247 return testing::ValuesIn(targets);
253 namespace opencv_test {
254 using namespace cv::dnn;
256 class DNNTestLayer : public TestWithParam<tuple<Backend, Target> >
259 dnn::Backend backend;
261 double default_l1, default_lInf;
265 backend = (dnn::Backend)(int)get<0>(GetParam());
266 target = (dnn::Target)(int)get<1>(GetParam());
267 getDefaultThresholds(backend, target, &default_l1, &default_lInf);
270 static void getDefaultThresholds(int backend, int target, double* l1, double* lInf)
272 if (target == DNN_TARGET_OPENCL_FP16 || target == DNN_TARGET_MYRIAD)
284 static void checkBackend(int backend, int target, Mat* inp = 0, Mat* ref = 0)
286 if (backend == DNN_BACKEND_INFERENCE_ENGINE && target == DNN_TARGET_MYRIAD)
288 #if defined(INF_ENGINE_RELEASE) && INF_ENGINE_RELEASE < 2018030000
289 if (inp && ref && inp->size[0] != 1)
291 // Myriad plugin supports only batch size 1. Slice a single sample.
292 if (inp->size[0] == ref->size[0])
294 std::vector<cv::Range> range(inp->dims, Range::all());
295 range[0] = Range(0, 1);
296 *inp = inp->operator()(range);
298 range = std::vector<cv::Range>(ref->dims, Range::all());
299 range[0] = Range(0, 1);
300 *ref = ref->operator()(range);
303 throw SkipTestException("Myriad plugin supports only batch size 1");
306 if (inp && ref && inp->dims == 4 && ref->dims == 4 &&
307 inp->size[0] != 1 && inp->size[0] != ref->size[0])
308 throw SkipTestException("Inconsistent batch size of input and output blobs for Myriad plugin");
315 void checkBackend(Mat* inp = 0, Mat* ref = 0)
317 checkBackend(backend, target, inp, ref);