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 bool readFileInMemory(const std::string& filename, std::string& content)
163 std::ios::openmode mode = std::ios::in | std::ios::binary;
164 std::ifstream ifs(filename.c_str(), mode);
170 ifs.seekg(0, std::ios::end);
171 content.reserve(ifs.tellg());
172 ifs.seekg(0, std::ios::beg);
174 content.assign((std::istreambuf_iterator<char>(ifs)),
175 std::istreambuf_iterator<char>());
181 testing::internal::ParamGenerator< tuple<Backend, Target> > dnnBackendsAndTargets(
182 bool withInferenceEngine /*= true*/,
183 bool withHalide /*= false*/,
184 bool withCpuOCV /*= true*/
187 #ifdef HAVE_INF_ENGINE
188 bool withVPU = validateVPUType();
191 std::vector< tuple<Backend, Target> > targets;
192 std::vector< Target > available;
195 available = getAvailableTargets(DNN_BACKEND_HALIDE);
196 for (std::vector< Target >::const_iterator i = available.begin(); i != available.end(); ++i)
197 targets.push_back(make_tuple(DNN_BACKEND_HALIDE, *i));
199 #ifdef HAVE_INF_ENGINE
200 if (withInferenceEngine)
202 available = getAvailableTargets(DNN_BACKEND_INFERENCE_ENGINE);
203 for (std::vector< Target >::const_iterator i = available.begin(); i != available.end(); ++i)
205 if (*i == DNN_TARGET_MYRIAD && !withVPU)
207 targets.push_back(make_tuple(DNN_BACKEND_INFERENCE_ENGINE, *i));
211 CV_UNUSED(withInferenceEngine);
214 available = getAvailableTargets(DNN_BACKEND_OPENCV);
215 for (std::vector< Target >::const_iterator i = available.begin(); i != available.end(); ++i)
217 if (!withCpuOCV && *i == DNN_TARGET_CPU)
219 targets.push_back(make_tuple(DNN_BACKEND_OPENCV, *i));
222 if (targets.empty()) // validate at least CPU mode
223 targets.push_back(make_tuple(DNN_BACKEND_OPENCV, DNN_TARGET_CPU));
224 return testing::ValuesIn(targets);
228 #ifdef HAVE_INF_ENGINE
229 static std::string getTestInferenceEngineVPUType()
231 static std::string param_vpu_type = utils::getConfigurationParameterString("OPENCV_TEST_DNN_IE_VPU_TYPE", "");
232 return param_vpu_type;
235 static bool validateVPUType_()
237 std::string test_vpu_type = getTestInferenceEngineVPUType();
238 if (test_vpu_type == "DISABLED" || test_vpu_type == "disabled")
243 std::vector<Target> available = getAvailableTargets(DNN_BACKEND_INFERENCE_ENGINE);
244 bool have_vpu_target = false;
245 for (std::vector<Target>::const_iterator i = available.begin(); i != available.end(); ++i)
247 if (*i == DNN_TARGET_MYRIAD)
249 have_vpu_target = true;
254 if (test_vpu_type.empty())
258 CV_LOG_INFO(NULL, "OpenCV-DNN-Test: VPU type for testing is not specified via 'OPENCV_TEST_DNN_IE_VPU_TYPE' parameter.")
263 if (!have_vpu_target)
265 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.");
268 std::string dnn_vpu_type = getInferenceEngineVPUType();
269 if (dnn_vpu_type != test_vpu_type)
271 CV_LOG_FATAL(NULL, "OpenCV-DNN-Test: 'testing' and 'detected' VPU types mismatch: '" << test_vpu_type << "' vs '" << dnn_vpu_type << "'. STOP.");
278 bool validateVPUType()
280 static bool result = validateVPUType_();
283 #endif // HAVE_INF_ENGINE