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 } // don't use "default:" to emit compiler warnings
30 *os << "DNN_BACKEND_UNKNOWN(" << (int)v << ")";
33 void PrintTo(const cv::dnn::Target& v, std::ostream* os)
36 case DNN_TARGET_CPU: *os << "CPU"; return;
37 case DNN_TARGET_OPENCL: *os << "OCL"; return;
38 case DNN_TARGET_OPENCL_FP16: *os << "OCL_FP16"; return;
39 case DNN_TARGET_MYRIAD: *os << "MYRIAD"; return;
40 case DNN_TARGET_VULKAN: *os << "VULKAN"; return;
41 case DNN_TARGET_FPGA: *os << "FPGA"; return;
42 } // don't use "default:" to emit compiler warnings
43 *os << "DNN_TARGET_UNKNOWN(" << (int)v << ")";
46 void PrintTo(const tuple<cv::dnn::Backend, cv::dnn::Target> v, std::ostream* os)
48 PrintTo(get<0>(v), os);
50 PrintTo(get<1>(v), os);
58 namespace opencv_test {
61 cv::InputArray ref, cv::InputArray test, const char *comment /*= ""*/,
62 double l1 /*= 0.00001*/, double lInf /*= 0.0001*/)
64 double normL1 = cvtest::norm(ref, test, cv::NORM_L1) / ref.getMat().total();
65 EXPECT_LE(normL1, l1) << comment;
67 double normInf = cvtest::norm(ref, test, cv::NORM_INF);
68 EXPECT_LE(normInf, lInf) << comment;
71 std::vector<cv::Rect2d> matToBoxes(const cv::Mat& m)
73 EXPECT_EQ(m.type(), CV_32FC1);
77 std::vector<cv::Rect2d> boxes(m.rows);
78 for (int i = 0; i < m.rows; ++i)
80 CV_Assert(m.row(i).isContinuous());
81 const float* data = m.ptr<float>(i);
82 double l = data[0], t = data[1], r = data[2], b = data[3];
83 boxes[i] = cv::Rect2d(l, t, r - l, b - t);
88 void normAssertDetections(
89 const std::vector<int>& refClassIds,
90 const std::vector<float>& refScores,
91 const std::vector<cv::Rect2d>& refBoxes,
92 const std::vector<int>& testClassIds,
93 const std::vector<float>& testScores,
94 const std::vector<cv::Rect2d>& testBoxes,
95 const char *comment /*= ""*/, double confThreshold /*= 0.0*/,
96 double scores_diff /*= 1e-5*/, double boxes_iou_diff /*= 1e-4*/)
98 std::vector<bool> matchedRefBoxes(refBoxes.size(), false);
99 for (int i = 0; i < testBoxes.size(); ++i)
101 double testScore = testScores[i];
102 if (testScore < confThreshold)
105 int testClassId = testClassIds[i];
106 const cv::Rect2d& testBox = testBoxes[i];
107 bool matched = false;
108 for (int j = 0; j < refBoxes.size() && !matched; ++j)
110 if (!matchedRefBoxes[j] && testClassId == refClassIds[j] &&
111 std::abs(testScore - refScores[j]) < scores_diff)
113 double interArea = (testBox & refBoxes[j]).area();
114 double iou = interArea / (testBox.area() + refBoxes[j].area() - interArea);
115 if (std::abs(iou - 1.0) < boxes_iou_diff)
118 matchedRefBoxes[j] = true;
123 std::cout << cv::format("Unmatched prediction: class %d score %f box ",
124 testClassId, testScore) << testBox << std::endl;
125 EXPECT_TRUE(matched) << comment;
128 // Check unmatched reference detections.
129 for (int i = 0; i < refBoxes.size(); ++i)
131 if (!matchedRefBoxes[i] && refScores[i] > confThreshold)
133 std::cout << cv::format("Unmatched reference: class %d score %f box ",
134 refClassIds[i], refScores[i]) << refBoxes[i] << std::endl;
135 EXPECT_LE(refScores[i], confThreshold) << comment;
140 // For SSD-based object detection networks which produce output of shape 1x1xNx7
141 // where N is a number of detections and an every detection is represented by
142 // a vector [batchId, classId, confidence, left, top, right, bottom].
143 void normAssertDetections(
144 cv::Mat ref, cv::Mat out, const char *comment /*= ""*/,
145 double confThreshold /*= 0.0*/, double scores_diff /*= 1e-5*/,
146 double boxes_iou_diff /*= 1e-4*/)
148 CV_Assert(ref.total() % 7 == 0);
149 CV_Assert(out.total() % 7 == 0);
150 ref = ref.reshape(1, ref.total() / 7);
151 out = out.reshape(1, out.total() / 7);
153 cv::Mat refClassIds, testClassIds;
154 ref.col(1).convertTo(refClassIds, CV_32SC1);
155 out.col(1).convertTo(testClassIds, CV_32SC1);
156 std::vector<float> refScores(ref.col(2)), testScores(out.col(2));
157 std::vector<cv::Rect2d> refBoxes = matToBoxes(ref.colRange(3, 7));
158 std::vector<cv::Rect2d> testBoxes = matToBoxes(out.colRange(3, 7));
159 normAssertDetections(refClassIds, refScores, refBoxes, testClassIds, testScores,
160 testBoxes, comment, confThreshold, scores_diff, boxes_iou_diff);
163 void readFileContent(const std::string& filename, CV_OUT std::vector<char>& content)
165 const std::ios::openmode mode = std::ios::in | std::ios::binary;
166 std::ifstream ifs(filename.c_str(), mode);
167 ASSERT_TRUE(ifs.is_open());
171 ifs.seekg(0, std::ios::end);
172 const size_t sz = ifs.tellg();
174 ifs.seekg(0, std::ios::beg);
176 ifs.read((char*)content.data(), sz);
177 ASSERT_FALSE(ifs.fail());
181 testing::internal::ParamGenerator< tuple<Backend, Target> > dnnBackendsAndTargets(
182 bool withInferenceEngine /*= true*/,
183 bool withHalide /*= false*/,
184 bool withCpuOCV /*= true*/,
185 bool withVkCom /*= true*/
188 #ifdef HAVE_INF_ENGINE
189 bool withVPU = validateVPUType();
192 std::vector< tuple<Backend, Target> > targets;
193 std::vector< Target > available;
196 available = getAvailableTargets(DNN_BACKEND_HALIDE);
197 for (std::vector< Target >::const_iterator i = available.begin(); i != available.end(); ++i)
198 targets.push_back(make_tuple(DNN_BACKEND_HALIDE, *i));
200 #ifdef HAVE_INF_ENGINE
201 if (withInferenceEngine)
203 available = getAvailableTargets(DNN_BACKEND_INFERENCE_ENGINE);
204 for (std::vector< Target >::const_iterator i = available.begin(); i != available.end(); ++i)
206 if (*i == DNN_TARGET_MYRIAD && !withVPU)
208 targets.push_back(make_tuple(DNN_BACKEND_INFERENCE_ENGINE, *i));
212 CV_UNUSED(withInferenceEngine);
216 available = getAvailableTargets(DNN_BACKEND_VKCOM);
217 for (std::vector< Target >::const_iterator i = available.begin(); i != available.end(); ++i)
218 targets.push_back(make_tuple(DNN_BACKEND_VKCOM, *i));
221 available = getAvailableTargets(DNN_BACKEND_OPENCV);
222 for (std::vector< Target >::const_iterator i = available.begin(); i != available.end(); ++i)
224 if (!withCpuOCV && *i == DNN_TARGET_CPU)
226 targets.push_back(make_tuple(DNN_BACKEND_OPENCV, *i));
229 if (targets.empty()) // validate at least CPU mode
230 targets.push_back(make_tuple(DNN_BACKEND_OPENCV, DNN_TARGET_CPU));
231 return testing::ValuesIn(targets);
235 #ifdef HAVE_INF_ENGINE
236 static std::string getTestInferenceEngineVPUType()
238 static std::string param_vpu_type = utils::getConfigurationParameterString("OPENCV_TEST_DNN_IE_VPU_TYPE", "");
239 return param_vpu_type;
242 static bool validateVPUType_()
244 std::string test_vpu_type = getTestInferenceEngineVPUType();
245 if (test_vpu_type == "DISABLED" || test_vpu_type == "disabled")
250 std::vector<Target> available = getAvailableTargets(DNN_BACKEND_INFERENCE_ENGINE);
251 bool have_vpu_target = false;
252 for (std::vector<Target>::const_iterator i = available.begin(); i != available.end(); ++i)
254 if (*i == DNN_TARGET_MYRIAD)
256 have_vpu_target = true;
261 if (test_vpu_type.empty())
265 CV_LOG_INFO(NULL, "OpenCV-DNN-Test: VPU type for testing is not specified via 'OPENCV_TEST_DNN_IE_VPU_TYPE' parameter.")
270 if (!have_vpu_target)
272 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.");
275 std::string dnn_vpu_type = getInferenceEngineVPUType();
276 if (dnn_vpu_type != test_vpu_type)
278 CV_LOG_FATAL(NULL, "OpenCV-DNN-Test: 'testing' and 'detected' VPU types mismatch: '" << test_vpu_type << "' vs '" << dnn_vpu_type << "'. STOP.");
284 std::string dnn_vpu_type = getInferenceEngineVPUType();
285 if (dnn_vpu_type == CV_DNN_INFERENCE_ENGINE_VPU_TYPE_MYRIAD_2)
286 registerGlobalSkipTag(CV_TEST_TAG_DNN_SKIP_IE_MYRIAD_2);
287 if (dnn_vpu_type == CV_DNN_INFERENCE_ENGINE_VPU_TYPE_MYRIAD_X)
288 registerGlobalSkipTag(CV_TEST_TAG_DNN_SKIP_IE_MYRIAD_X);
293 bool validateVPUType()
295 static bool result = validateVPUType_();
298 #endif // HAVE_INF_ENGINE
303 const char* extraTestDataPath =
307 getenv("OPENCV_DNN_TEST_DATA_PATH");
309 if (extraTestDataPath)
310 cvtest::addDataSearchPath(extraTestDataPath);
312 registerGlobalSkipTag(
313 CV_TEST_TAG_DNN_SKIP_HALIDE,
314 CV_TEST_TAG_DNN_SKIP_OPENCL, CV_TEST_TAG_DNN_SKIP_OPENCL_FP16
316 #if defined(INF_ENGINE_RELEASE)
317 registerGlobalSkipTag(
318 #if INF_ENGINE_VER_MAJOR_EQ(2018050000)
319 CV_TEST_TAG_DNN_SKIP_IE_2018R5,
320 #elif INF_ENGINE_VER_MAJOR_EQ(2019010000)
321 CV_TEST_TAG_DNN_SKIP_IE_2019R1,
322 # if INF_ENGINE_RELEASE == 2019010100
323 CV_TEST_TAG_DNN_SKIP_IE_2019R1_1,
325 #elif INF_ENGINE_VER_MAJOR_EQ(2019020000)
326 CV_TEST_TAG_DNN_SKIP_IE_2019R2,
327 #elif INF_ENGINE_VER_MAJOR_EQ(2019030000)
328 CV_TEST_TAG_DNN_SKIP_IE_2019R3,
330 CV_TEST_TAG_DNN_SKIP_IE
333 registerGlobalSkipTag(
334 // see validateVPUType(): CV_TEST_TAG_DNN_SKIP_IE_MYRIAD_2, CV_TEST_TAG_DNN_SKIP_IE_MYRIAD_X
335 CV_TEST_TAG_DNN_SKIP_IE_OPENCL, CV_TEST_TAG_DNN_SKIP_IE_OPENCL_FP16
338 registerGlobalSkipTag(
339 CV_TEST_TAG_DNN_SKIP_VULKAN