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_INLINE_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_VKCOM: *os << "VKCOM"; return;
28 case DNN_BACKEND_OPENCV: *os << "OCV"; return;
29 case DNN_BACKEND_CUDA: *os << "CUDA"; return;
30 case DNN_BACKEND_INFERENCE_ENGINE_NN_BUILDER_2019: *os << "DLIE"; return;
31 case DNN_BACKEND_INFERENCE_ENGINE_NGRAPH: *os << "NGRAPH"; return;
32 } // don't use "default:" to emit compiler warnings
33 *os << "DNN_BACKEND_UNKNOWN(" << (int)v << ")";
36 void PrintTo(const cv::dnn::Target& v, std::ostream* os)
39 case DNN_TARGET_CPU: *os << "CPU"; return;
40 case DNN_TARGET_OPENCL: *os << "OCL"; return;
41 case DNN_TARGET_OPENCL_FP16: *os << "OCL_FP16"; return;
42 case DNN_TARGET_MYRIAD: *os << "MYRIAD"; return;
43 case DNN_TARGET_VULKAN: *os << "VULKAN"; return;
44 case DNN_TARGET_FPGA: *os << "FPGA"; return;
45 case DNN_TARGET_CUDA: *os << "CUDA"; return;
46 case DNN_TARGET_CUDA_FP16: *os << "CUDA_FP16"; return;
47 } // don't use "default:" to emit compiler warnings
48 *os << "DNN_TARGET_UNKNOWN(" << (int)v << ")";
51 void PrintTo(const tuple<cv::dnn::Backend, cv::dnn::Target> v, std::ostream* os)
53 PrintTo(get<0>(v), os);
55 PrintTo(get<1>(v), os);
63 namespace opencv_test {
66 cv::InputArray ref, cv::InputArray test, const char *comment /*= ""*/,
67 double l1 /*= 0.00001*/, double lInf /*= 0.0001*/)
69 double normL1 = cvtest::norm(ref, test, cv::NORM_L1) / ref.getMat().total();
70 EXPECT_LE(normL1, l1) << comment << " |ref| = " << cvtest::norm(ref, cv::NORM_INF);
72 double normInf = cvtest::norm(ref, test, cv::NORM_INF);
73 EXPECT_LE(normInf, lInf) << comment << " |ref| = " << cvtest::norm(ref, cv::NORM_INF);
76 std::vector<cv::Rect2d> matToBoxes(const cv::Mat& m)
78 EXPECT_EQ(m.type(), CV_32FC1);
82 std::vector<cv::Rect2d> boxes(m.rows);
83 for (int i = 0; i < m.rows; ++i)
85 CV_Assert(m.row(i).isContinuous());
86 const float* data = m.ptr<float>(i);
87 double l = data[0], t = data[1], r = data[2], b = data[3];
88 boxes[i] = cv::Rect2d(l, t, r - l, b - t);
93 void normAssertDetections(
94 const std::vector<int>& refClassIds,
95 const std::vector<float>& refScores,
96 const std::vector<cv::Rect2d>& refBoxes,
97 const std::vector<int>& testClassIds,
98 const std::vector<float>& testScores,
99 const std::vector<cv::Rect2d>& testBoxes,
100 const char *comment /*= ""*/, double confThreshold /*= 0.0*/,
101 double scores_diff /*= 1e-5*/, double boxes_iou_diff /*= 1e-4*/)
103 ASSERT_FALSE(testClassIds.empty()) << "No detections";
104 std::vector<bool> matchedRefBoxes(refBoxes.size(), false);
105 std::vector<double> refBoxesIoUDiff(refBoxes.size(), 1.0);
106 for (int i = 0; i < testBoxes.size(); ++i)
108 //cout << "Test[i=" << i << "]: score=" << testScores[i] << " id=" << testClassIds[i] << " box " << testBoxes[i] << endl;
109 double testScore = testScores[i];
110 if (testScore < confThreshold)
113 int testClassId = testClassIds[i];
114 const cv::Rect2d& testBox = testBoxes[i];
115 bool matched = false;
117 for (int j = 0; j < refBoxes.size() && !matched; ++j)
119 if (!matchedRefBoxes[j] && testClassId == refClassIds[j] &&
120 std::abs(testScore - refScores[j]) < scores_diff)
122 double interArea = (testBox & refBoxes[j]).area();
123 double iou = interArea / (testBox.area() + refBoxes[j].area() - interArea);
124 topIoU = std::max(topIoU, iou);
125 refBoxesIoUDiff[j] = std::min(refBoxesIoUDiff[j], 1.0f - iou);
126 if (1.0 - iou < boxes_iou_diff)
129 matchedRefBoxes[j] = true;
135 std::cout << cv::format("Unmatched prediction: class %d score %f box ",
136 testClassId, testScore) << testBox << std::endl;
137 std::cout << "Highest IoU: " << topIoU << std::endl;
139 EXPECT_TRUE(matched) << comment;
142 // Check unmatched reference detections.
143 for (int i = 0; i < refBoxes.size(); ++i)
145 if (!matchedRefBoxes[i] && refScores[i] > confThreshold)
147 std::cout << cv::format("Unmatched reference: class %d score %f box ",
148 refClassIds[i], refScores[i]) << refBoxes[i]
149 << " IoU diff: " << refBoxesIoUDiff[i]
151 EXPECT_LE(refScores[i], confThreshold) << comment;
156 // For SSD-based object detection networks which produce output of shape 1x1xNx7
157 // where N is a number of detections and an every detection is represented by
158 // a vector [batchId, classId, confidence, left, top, right, bottom].
159 void normAssertDetections(
160 cv::Mat ref, cv::Mat out, const char *comment /*= ""*/,
161 double confThreshold /*= 0.0*/, double scores_diff /*= 1e-5*/,
162 double boxes_iou_diff /*= 1e-4*/)
164 CV_Assert(ref.total() % 7 == 0);
165 CV_Assert(out.total() % 7 == 0);
166 ref = ref.reshape(1, ref.total() / 7);
167 out = out.reshape(1, out.total() / 7);
169 cv::Mat refClassIds, testClassIds;
170 ref.col(1).convertTo(refClassIds, CV_32SC1);
171 out.col(1).convertTo(testClassIds, CV_32SC1);
172 std::vector<float> refScores(ref.col(2)), testScores(out.col(2));
173 std::vector<cv::Rect2d> refBoxes = matToBoxes(ref.colRange(3, 7));
174 std::vector<cv::Rect2d> testBoxes = matToBoxes(out.colRange(3, 7));
175 normAssertDetections(refClassIds, refScores, refBoxes, testClassIds, testScores,
176 testBoxes, comment, confThreshold, scores_diff, boxes_iou_diff);
179 void readFileContent(const std::string& filename, CV_OUT std::vector<char>& content)
181 const std::ios::openmode mode = std::ios::in | std::ios::binary;
182 std::ifstream ifs(filename.c_str(), mode);
183 ASSERT_TRUE(ifs.is_open());
187 ifs.seekg(0, std::ios::end);
188 const size_t sz = ifs.tellg();
190 ifs.seekg(0, std::ios::beg);
192 ifs.read((char*)content.data(), sz);
193 ASSERT_FALSE(ifs.fail());
197 testing::internal::ParamGenerator< tuple<Backend, Target> > dnnBackendsAndTargets(
198 bool withInferenceEngine /*= true*/,
199 bool withHalide /*= false*/,
200 bool withCpuOCV /*= true*/,
201 bool withVkCom /*= true*/,
202 bool withCUDA /*= true*/,
203 bool withNgraph /*= true*/
206 #ifdef HAVE_INF_ENGINE
207 bool withVPU = validateVPUType();
210 std::vector< tuple<Backend, Target> > targets;
211 std::vector< Target > available;
214 available = getAvailableTargets(DNN_BACKEND_HALIDE);
215 for (std::vector< Target >::const_iterator i = available.begin(); i != available.end(); ++i)
216 targets.push_back(make_tuple(DNN_BACKEND_HALIDE, *i));
218 #ifdef HAVE_INF_ENGINE
219 if (withInferenceEngine)
221 available = getAvailableTargets(DNN_BACKEND_INFERENCE_ENGINE_NN_BUILDER_2019);
222 for (std::vector< Target >::const_iterator i = available.begin(); i != available.end(); ++i)
224 if (*i == DNN_TARGET_MYRIAD && !withVPU)
226 targets.push_back(make_tuple(DNN_BACKEND_INFERENCE_ENGINE_NN_BUILDER_2019, *i));
231 available = getAvailableTargets(DNN_BACKEND_INFERENCE_ENGINE_NGRAPH);
232 for (std::vector< Target >::const_iterator i = available.begin(); i != available.end(); ++i)
234 if (*i == DNN_TARGET_MYRIAD && !withVPU)
236 targets.push_back(make_tuple(DNN_BACKEND_INFERENCE_ENGINE_NGRAPH, *i));
241 CV_UNUSED(withInferenceEngine);
245 available = getAvailableTargets(DNN_BACKEND_VKCOM);
246 for (std::vector< Target >::const_iterator i = available.begin(); i != available.end(); ++i)
247 targets.push_back(make_tuple(DNN_BACKEND_VKCOM, *i));
253 for (auto target : getAvailableTargets(DNN_BACKEND_CUDA))
254 targets.push_back(make_tuple(DNN_BACKEND_CUDA, target));
259 available = getAvailableTargets(DNN_BACKEND_OPENCV);
260 for (std::vector< Target >::const_iterator i = available.begin(); i != available.end(); ++i)
262 if (!withCpuOCV && *i == DNN_TARGET_CPU)
264 targets.push_back(make_tuple(DNN_BACKEND_OPENCV, *i));
267 if (targets.empty()) // validate at least CPU mode
268 targets.push_back(make_tuple(DNN_BACKEND_OPENCV, DNN_TARGET_CPU));
269 return testing::ValuesIn(targets);
272 testing::internal::ParamGenerator< tuple<Backend, Target> > dnnBackendsAndTargetsIE()
274 #ifdef HAVE_INF_ENGINE
275 bool withVPU = validateVPUType();
277 std::vector< tuple<Backend, Target> > targets;
278 std::vector< Target > available;
281 available = getAvailableTargets(DNN_BACKEND_INFERENCE_ENGINE_NN_BUILDER_2019);
282 for (std::vector< Target >::const_iterator i = available.begin(); i != available.end(); ++i)
284 if (*i == DNN_TARGET_MYRIAD && !withVPU)
286 targets.push_back(make_tuple(DNN_BACKEND_INFERENCE_ENGINE_NN_BUILDER_2019, *i));
291 available = getAvailableTargets(DNN_BACKEND_INFERENCE_ENGINE_NGRAPH);
292 for (std::vector< Target >::const_iterator i = available.begin(); i != available.end(); ++i)
294 if (*i == DNN_TARGET_MYRIAD && !withVPU)
296 targets.push_back(make_tuple(DNN_BACKEND_INFERENCE_ENGINE_NGRAPH, *i));
301 return testing::ValuesIn(targets);
303 return testing::ValuesIn(std::vector< tuple<Backend, Target> >());
307 #ifdef HAVE_INF_ENGINE
308 static std::string getTestInferenceEngineVPUType()
310 static std::string param_vpu_type = utils::getConfigurationParameterString("OPENCV_TEST_DNN_IE_VPU_TYPE", "");
311 return param_vpu_type;
314 static bool validateVPUType_()
316 std::string test_vpu_type = getTestInferenceEngineVPUType();
317 if (test_vpu_type == "DISABLED" || test_vpu_type == "disabled")
322 std::vector<Target> available = getAvailableTargets(DNN_BACKEND_INFERENCE_ENGINE);
323 bool have_vpu_target = false;
324 for (std::vector<Target>::const_iterator i = available.begin(); i != available.end(); ++i)
326 if (*i == DNN_TARGET_MYRIAD)
328 have_vpu_target = true;
333 if (test_vpu_type.empty())
337 CV_LOG_INFO(NULL, "OpenCV-DNN-Test: VPU type for testing is not specified via 'OPENCV_TEST_DNN_IE_VPU_TYPE' parameter.")
342 if (!have_vpu_target)
344 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.");
347 std::string dnn_vpu_type = getInferenceEngineVPUType();
348 if (dnn_vpu_type != test_vpu_type)
350 CV_LOG_FATAL(NULL, "OpenCV-DNN-Test: 'testing' and 'detected' VPU types mismatch: '" << test_vpu_type << "' vs '" << dnn_vpu_type << "'. STOP.");
356 std::string dnn_vpu_type = getInferenceEngineVPUType();
357 if (dnn_vpu_type == CV_DNN_INFERENCE_ENGINE_VPU_TYPE_MYRIAD_2)
358 registerGlobalSkipTag(CV_TEST_TAG_DNN_SKIP_IE_MYRIAD_2);
359 if (dnn_vpu_type == CV_DNN_INFERENCE_ENGINE_VPU_TYPE_MYRIAD_X)
360 registerGlobalSkipTag(CV_TEST_TAG_DNN_SKIP_IE_MYRIAD_X);
365 bool validateVPUType()
367 static bool result = validateVPUType_();
370 #endif // HAVE_INF_ENGINE
375 const char* extraTestDataPath =
379 getenv("OPENCV_DNN_TEST_DATA_PATH");
381 if (extraTestDataPath)
382 cvtest::addDataSearchPath(extraTestDataPath);
384 registerGlobalSkipTag(
385 CV_TEST_TAG_DNN_SKIP_HALIDE,
386 CV_TEST_TAG_DNN_SKIP_OPENCL, CV_TEST_TAG_DNN_SKIP_OPENCL_FP16
388 #if defined(INF_ENGINE_RELEASE)
389 registerGlobalSkipTag(
390 CV_TEST_TAG_DNN_SKIP_IE,
391 #if INF_ENGINE_VER_MAJOR_EQ(2018050000)
392 CV_TEST_TAG_DNN_SKIP_IE_2018R5,
393 #elif INF_ENGINE_VER_MAJOR_EQ(2019010000)
394 CV_TEST_TAG_DNN_SKIP_IE_2019R1,
395 # if INF_ENGINE_RELEASE == 2019010100
396 CV_TEST_TAG_DNN_SKIP_IE_2019R1_1,
398 #elif INF_ENGINE_VER_MAJOR_EQ(2019020000)
399 CV_TEST_TAG_DNN_SKIP_IE_2019R2,
400 #elif INF_ENGINE_VER_MAJOR_EQ(2019030000)
401 CV_TEST_TAG_DNN_SKIP_IE_2019R3,
403 #ifdef HAVE_DNN_NGRAPH
404 CV_TEST_TAG_DNN_SKIP_IE_NGRAPH,
406 #ifdef HAVE_DNN_IE_NN_BUILDER_2019
407 CV_TEST_TAG_DNN_SKIP_IE_NN_BUILDER,
412 registerGlobalSkipTag(
413 // see validateVPUType(): CV_TEST_TAG_DNN_SKIP_IE_MYRIAD_2, CV_TEST_TAG_DNN_SKIP_IE_MYRIAD_X
414 CV_TEST_TAG_DNN_SKIP_IE_OPENCL, CV_TEST_TAG_DNN_SKIP_IE_OPENCL_FP16
417 registerGlobalSkipTag(
418 CV_TEST_TAG_DNN_SKIP_VULKAN
423 registerGlobalSkipTag(
424 CV_TEST_TAG_DNN_SKIP_CUDA, CV_TEST_TAG_DNN_SKIP_CUDA_FP32, CV_TEST_TAG_DNN_SKIP_CUDA_FP16