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.
5 // Used in accuracy and perf tests as a content of .cpp file
6 // Note: don't use "precomp.hpp" here
7 #include "opencv2/ts.hpp"
8 #include "opencv2/ts/ts_perf.hpp"
9 #include "opencv2/core/utility.hpp"
10 #include "opencv2/core/ocl.hpp"
12 #include "opencv2/dnn.hpp"
13 #include "test_common.hpp"
15 #include <opencv2/core/utils/configuration.private.hpp>
16 #include <opencv2/core/utils/logger.hpp>
18 namespace cv { namespace dnn {
19 CV__DNN_EXPERIMENTAL_NS_BEGIN
21 void PrintTo(const cv::dnn::Backend& v, std::ostream* os)
24 case DNN_BACKEND_DEFAULT: *os << "DEFAULT"; return;
25 case DNN_BACKEND_HALIDE: *os << "HALIDE"; return;
26 case DNN_BACKEND_INFERENCE_ENGINE: *os << "DLIE"; return;
27 case DNN_BACKEND_OPENCV: *os << "OCV"; return;
28 } // don't use "default:" to emit compiler warnings
29 *os << "DNN_BACKEND_UNKNOWN(" << (int)v << ")";
32 void PrintTo(const cv::dnn::Target& v, std::ostream* os)
35 case DNN_TARGET_CPU: *os << "CPU"; return;
36 case DNN_TARGET_OPENCL: *os << "OCL"; return;
37 case DNN_TARGET_OPENCL_FP16: *os << "OCL_FP16"; return;
38 case DNN_TARGET_MYRIAD: *os << "MYRIAD"; return;
39 case DNN_TARGET_FPGA: *os << "FPGA"; return;
40 } // don't use "default:" to emit compiler warnings
41 *os << "DNN_TARGET_UNKNOWN(" << (int)v << ")";
44 void PrintTo(const tuple<cv::dnn::Backend, cv::dnn::Target> v, std::ostream* os)
46 PrintTo(get<0>(v), os);
48 PrintTo(get<1>(v), os);
51 CV__DNN_EXPERIMENTAL_NS_END
56 namespace opencv_test {
59 cv::InputArray ref, cv::InputArray test, const char *comment /*= ""*/,
60 double l1 /*= 0.00001*/, double lInf /*= 0.0001*/)
62 double normL1 = cvtest::norm(ref, test, cv::NORM_L1) / ref.getMat().total();
63 EXPECT_LE(normL1, l1) << comment;
65 double normInf = cvtest::norm(ref, test, cv::NORM_INF);
66 EXPECT_LE(normInf, lInf) << comment;
69 std::vector<cv::Rect2d> matToBoxes(const cv::Mat& m)
71 EXPECT_EQ(m.type(), CV_32FC1);
75 std::vector<cv::Rect2d> boxes(m.rows);
76 for (int i = 0; i < m.rows; ++i)
78 CV_Assert(m.row(i).isContinuous());
79 const float* data = m.ptr<float>(i);
80 double l = data[0], t = data[1], r = data[2], b = data[3];
81 boxes[i] = cv::Rect2d(l, t, r - l, b - t);
86 void normAssertDetections(
87 const std::vector<int>& refClassIds,
88 const std::vector<float>& refScores,
89 const std::vector<cv::Rect2d>& refBoxes,
90 const std::vector<int>& testClassIds,
91 const std::vector<float>& testScores,
92 const std::vector<cv::Rect2d>& testBoxes,
93 const char *comment /*= ""*/, double confThreshold /*= 0.0*/,
94 double scores_diff /*= 1e-5*/, double boxes_iou_diff /*= 1e-4*/)
96 std::vector<bool> matchedRefBoxes(refBoxes.size(), false);
97 for (int i = 0; i < testBoxes.size(); ++i)
99 double testScore = testScores[i];
100 if (testScore < confThreshold)
103 int testClassId = testClassIds[i];
104 const cv::Rect2d& testBox = testBoxes[i];
105 bool matched = false;
106 for (int j = 0; j < refBoxes.size() && !matched; ++j)
108 if (!matchedRefBoxes[j] && testClassId == refClassIds[j] &&
109 std::abs(testScore - refScores[j]) < scores_diff)
111 double interArea = (testBox & refBoxes[j]).area();
112 double iou = interArea / (testBox.area() + refBoxes[j].area() - interArea);
113 if (std::abs(iou - 1.0) < boxes_iou_diff)
116 matchedRefBoxes[j] = true;
121 std::cout << cv::format("Unmatched prediction: class %d score %f box ",
122 testClassId, testScore) << testBox << std::endl;
123 EXPECT_TRUE(matched) << comment;
126 // Check unmatched reference detections.
127 for (int i = 0; i < refBoxes.size(); ++i)
129 if (!matchedRefBoxes[i] && refScores[i] > confThreshold)
131 std::cout << cv::format("Unmatched reference: class %d score %f box ",
132 refClassIds[i], refScores[i]) << refBoxes[i] << std::endl;
133 EXPECT_LE(refScores[i], confThreshold) << comment;
138 // For SSD-based object detection networks which produce output of shape 1x1xNx7
139 // where N is a number of detections and an every detection is represented by
140 // a vector [batchId, classId, confidence, left, top, right, bottom].
141 void normAssertDetections(
142 cv::Mat ref, cv::Mat out, const char *comment /*= ""*/,
143 double confThreshold /*= 0.0*/, double scores_diff /*= 1e-5*/,
144 double boxes_iou_diff /*= 1e-4*/)
146 CV_Assert(ref.total() % 7 == 0);
147 CV_Assert(out.total() % 7 == 0);
148 ref = ref.reshape(1, ref.total() / 7);
149 out = out.reshape(1, out.total() / 7);
151 cv::Mat refClassIds, testClassIds;
152 ref.col(1).convertTo(refClassIds, CV_32SC1);
153 out.col(1).convertTo(testClassIds, CV_32SC1);
154 std::vector<float> refScores(ref.col(2)), testScores(out.col(2));
155 std::vector<cv::Rect2d> refBoxes = matToBoxes(ref.colRange(3, 7));
156 std::vector<cv::Rect2d> testBoxes = matToBoxes(out.colRange(3, 7));
157 normAssertDetections(refClassIds, refScores, refBoxes, testClassIds, testScores,
158 testBoxes, comment, confThreshold, scores_diff, boxes_iou_diff);
161 void readFileContent(const std::string& filename, CV_OUT std::vector<char>& content)
163 const std::ios::openmode mode = std::ios::in | std::ios::binary;
164 std::ifstream ifs(filename.c_str(), mode);
165 ASSERT_TRUE(ifs.is_open());
169 ifs.seekg(0, std::ios::end);
170 const size_t sz = ifs.tellg();
172 ifs.seekg(0, std::ios::beg);
174 ifs.read((char*)content.data(), sz);
175 ASSERT_FALSE(ifs.fail());
179 testing::internal::ParamGenerator< tuple<Backend, Target> > dnnBackendsAndTargets(
180 bool withInferenceEngine /*= true*/,
181 bool withHalide /*= false*/,
182 bool withCpuOCV /*= true*/
185 #ifdef HAVE_INF_ENGINE
186 bool withVPU = validateVPUType();
189 std::vector< tuple<Backend, Target> > targets;
190 std::vector< Target > available;
193 available = getAvailableTargets(DNN_BACKEND_HALIDE);
194 for (std::vector< Target >::const_iterator i = available.begin(); i != available.end(); ++i)
195 targets.push_back(make_tuple(DNN_BACKEND_HALIDE, *i));
197 #ifdef HAVE_INF_ENGINE
198 if (withInferenceEngine)
200 available = getAvailableTargets(DNN_BACKEND_INFERENCE_ENGINE);
201 for (std::vector< Target >::const_iterator i = available.begin(); i != available.end(); ++i)
203 if (*i == DNN_TARGET_MYRIAD && !withVPU)
205 targets.push_back(make_tuple(DNN_BACKEND_INFERENCE_ENGINE, *i));
209 CV_UNUSED(withInferenceEngine);
212 available = getAvailableTargets(DNN_BACKEND_OPENCV);
213 for (std::vector< Target >::const_iterator i = available.begin(); i != available.end(); ++i)
215 if (!withCpuOCV && *i == DNN_TARGET_CPU)
217 targets.push_back(make_tuple(DNN_BACKEND_OPENCV, *i));
220 if (targets.empty()) // validate at least CPU mode
221 targets.push_back(make_tuple(DNN_BACKEND_OPENCV, DNN_TARGET_CPU));
222 return testing::ValuesIn(targets);
226 #ifdef HAVE_INF_ENGINE
227 static std::string getTestInferenceEngineVPUType()
229 static std::string param_vpu_type = utils::getConfigurationParameterString("OPENCV_TEST_DNN_IE_VPU_TYPE", "");
230 return param_vpu_type;
233 static bool validateVPUType_()
235 std::string test_vpu_type = getTestInferenceEngineVPUType();
236 if (test_vpu_type == "DISABLED" || test_vpu_type == "disabled")
241 std::vector<Target> available = getAvailableTargets(DNN_BACKEND_INFERENCE_ENGINE);
242 bool have_vpu_target = false;
243 for (std::vector<Target>::const_iterator i = available.begin(); i != available.end(); ++i)
245 if (*i == DNN_TARGET_MYRIAD)
247 have_vpu_target = true;
252 if (test_vpu_type.empty())
256 CV_LOG_INFO(NULL, "OpenCV-DNN-Test: VPU type for testing is not specified via 'OPENCV_TEST_DNN_IE_VPU_TYPE' parameter.")
261 if (!have_vpu_target)
263 CV_LOG_FATAL(NULL, "OpenCV-DNN-Test: 'OPENCV_TEST_DNN_IE_VPU_TYPE' parameter requires VPU of type = '" << test_vpu_type << "', but VPU is not detected. STOP.");
266 std::string dnn_vpu_type = getInferenceEngineVPUType();
267 if (dnn_vpu_type != test_vpu_type)
269 CV_LOG_FATAL(NULL, "OpenCV-DNN-Test: 'testing' and 'detected' VPU types mismatch: '" << test_vpu_type << "' vs '" << dnn_vpu_type << "'. STOP.");
276 bool validateVPUType()
278 static bool result = validateVPUType_();
281 #endif // HAVE_INF_ENGINE