test_common.impl.hpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498
  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.
  4. // Used in accuracy and perf tests as a content of .cpp file
  5. // Note: don't use "precomp.hpp" here
  6. #include "opencv2/ts.hpp"
  7. #include "opencv2/ts/ts_perf.hpp"
  8. #include "opencv2/core/utility.hpp"
  9. #include "opencv2/core/ocl.hpp"
  10. #include "opencv2/dnn.hpp"
  11. #include "test_common.hpp"
  12. #include <opencv2/core/utils/configuration.private.hpp>
  13. #include <opencv2/core/utils/logger.hpp>
  14. namespace cv { namespace dnn {
  15. CV__DNN_INLINE_NS_BEGIN
  16. void PrintTo(const cv::dnn::Backend& v, std::ostream* os)
  17. {
  18. switch (v) {
  19. case DNN_BACKEND_DEFAULT: *os << "DEFAULT"; return;
  20. case DNN_BACKEND_HALIDE: *os << "HALIDE"; return;
  21. case DNN_BACKEND_INFERENCE_ENGINE: *os << "DLIE*"; return;
  22. case DNN_BACKEND_VKCOM: *os << "VKCOM"; return;
  23. case DNN_BACKEND_OPENCV: *os << "OCV"; return;
  24. case DNN_BACKEND_CUDA: *os << "CUDA"; return;
  25. case DNN_BACKEND_INFERENCE_ENGINE_NN_BUILDER_2019: *os << "DLIE"; return;
  26. case DNN_BACKEND_INFERENCE_ENGINE_NGRAPH: *os << "NGRAPH"; return;
  27. case DNN_BACKEND_WEBNN: *os << "WEBNN"; return;
  28. } // don't use "default:" to emit compiler warnings
  29. *os << "DNN_BACKEND_UNKNOWN(" << (int)v << ")";
  30. }
  31. void PrintTo(const cv::dnn::Target& v, std::ostream* os)
  32. {
  33. switch (v) {
  34. case DNN_TARGET_CPU: *os << "CPU"; return;
  35. case DNN_TARGET_OPENCL: *os << "OCL"; return;
  36. case DNN_TARGET_OPENCL_FP16: *os << "OCL_FP16"; return;
  37. case DNN_TARGET_MYRIAD: *os << "MYRIAD"; return;
  38. case DNN_TARGET_HDDL: *os << "HDDL"; return;
  39. case DNN_TARGET_VULKAN: *os << "VULKAN"; return;
  40. case DNN_TARGET_FPGA: *os << "FPGA"; return;
  41. case DNN_TARGET_CUDA: *os << "CUDA"; return;
  42. case DNN_TARGET_CUDA_FP16: *os << "CUDA_FP16"; return;
  43. } // don't use "default:" to emit compiler warnings
  44. *os << "DNN_TARGET_UNKNOWN(" << (int)v << ")";
  45. }
  46. void PrintTo(const tuple<cv::dnn::Backend, cv::dnn::Target> v, std::ostream* os)
  47. {
  48. PrintTo(get<0>(v), os);
  49. *os << "/";
  50. PrintTo(get<1>(v), os);
  51. }
  52. CV__DNN_INLINE_NS_END
  53. }} // namespace
  54. namespace opencv_test {
  55. void normAssert(
  56. cv::InputArray ref, cv::InputArray test, const char *comment /*= ""*/,
  57. double l1 /*= 0.00001*/, double lInf /*= 0.0001*/)
  58. {
  59. double normL1 = cvtest::norm(ref, test, cv::NORM_L1) / ref.getMat().total();
  60. EXPECT_LE(normL1, l1) << comment << " |ref| = " << cvtest::norm(ref, cv::NORM_INF);
  61. double normInf = cvtest::norm(ref, test, cv::NORM_INF);
  62. EXPECT_LE(normInf, lInf) << comment << " |ref| = " << cvtest::norm(ref, cv::NORM_INF);
  63. }
  64. std::vector<cv::Rect2d> matToBoxes(const cv::Mat& m)
  65. {
  66. EXPECT_EQ(m.type(), CV_32FC1);
  67. EXPECT_EQ(m.dims, 2);
  68. EXPECT_EQ(m.cols, 4);
  69. std::vector<cv::Rect2d> boxes(m.rows);
  70. for (int i = 0; i < m.rows; ++i)
  71. {
  72. CV_Assert(m.row(i).isContinuous());
  73. const float* data = m.ptr<float>(i);
  74. double l = data[0], t = data[1], r = data[2], b = data[3];
  75. boxes[i] = cv::Rect2d(l, t, r - l, b - t);
  76. }
  77. return boxes;
  78. }
  79. void normAssertDetections(
  80. const std::vector<int>& refClassIds,
  81. const std::vector<float>& refScores,
  82. const std::vector<cv::Rect2d>& refBoxes,
  83. const std::vector<int>& testClassIds,
  84. const std::vector<float>& testScores,
  85. const std::vector<cv::Rect2d>& testBoxes,
  86. const char *comment /*= ""*/, double confThreshold /*= 0.0*/,
  87. double scores_diff /*= 1e-5*/, double boxes_iou_diff /*= 1e-4*/)
  88. {
  89. ASSERT_FALSE(testClassIds.empty()) << "No detections";
  90. std::vector<bool> matchedRefBoxes(refBoxes.size(), false);
  91. std::vector<double> refBoxesIoUDiff(refBoxes.size(), 1.0);
  92. for (int i = 0; i < testBoxes.size(); ++i)
  93. {
  94. //cout << "Test[i=" << i << "]: score=" << testScores[i] << " id=" << testClassIds[i] << " box " << testBoxes[i] << endl;
  95. double testScore = testScores[i];
  96. if (testScore < confThreshold)
  97. continue;
  98. int testClassId = testClassIds[i];
  99. const cv::Rect2d& testBox = testBoxes[i];
  100. bool matched = false;
  101. double topIoU = 0;
  102. for (int j = 0; j < refBoxes.size() && !matched; ++j)
  103. {
  104. if (!matchedRefBoxes[j] && testClassId == refClassIds[j] &&
  105. std::abs(testScore - refScores[j]) < scores_diff)
  106. {
  107. double interArea = (testBox & refBoxes[j]).area();
  108. double iou = interArea / (testBox.area() + refBoxes[j].area() - interArea);
  109. topIoU = std::max(topIoU, iou);
  110. refBoxesIoUDiff[j] = std::min(refBoxesIoUDiff[j], 1.0f - iou);
  111. if (1.0 - iou < boxes_iou_diff)
  112. {
  113. matched = true;
  114. matchedRefBoxes[j] = true;
  115. }
  116. }
  117. }
  118. if (!matched)
  119. {
  120. std::cout << cv::format("Unmatched prediction: class %d score %f box ",
  121. testClassId, testScore) << testBox << std::endl;
  122. std::cout << "Highest IoU: " << topIoU << std::endl;
  123. }
  124. EXPECT_TRUE(matched) << comment;
  125. }
  126. // Check unmatched reference detections.
  127. for (int i = 0; i < refBoxes.size(); ++i)
  128. {
  129. if (!matchedRefBoxes[i] && refScores[i] > confThreshold)
  130. {
  131. std::cout << cv::format("Unmatched reference: class %d score %f box ",
  132. refClassIds[i], refScores[i]) << refBoxes[i]
  133. << " IoU diff: " << refBoxesIoUDiff[i]
  134. << std::endl;
  135. EXPECT_LE(refScores[i], confThreshold) << comment;
  136. }
  137. }
  138. }
  139. // For SSD-based object detection networks which produce output of shape 1x1xNx7
  140. // where N is a number of detections and an every detection is represented by
  141. // a vector [batchId, classId, confidence, left, top, right, bottom].
  142. void normAssertDetections(
  143. cv::Mat ref, cv::Mat out, const char *comment /*= ""*/,
  144. double confThreshold /*= 0.0*/, double scores_diff /*= 1e-5*/,
  145. double boxes_iou_diff /*= 1e-4*/)
  146. {
  147. CV_Assert(ref.total() % 7 == 0);
  148. CV_Assert(out.total() % 7 == 0);
  149. ref = ref.reshape(1, ref.total() / 7);
  150. 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);
  159. }
  160. // For text detection networks
  161. // Curved text polygon is not supported in the current version.
  162. // (concave polygon is invalid input to intersectConvexConvex)
  163. void normAssertTextDetections(
  164. const std::vector<std::vector<Point>>& gtPolys,
  165. const std::vector<std::vector<Point>>& testPolys,
  166. const char *comment /*= ""*/, double boxes_iou_diff /*= 1e-4*/)
  167. {
  168. std::vector<bool> matchedRefBoxes(gtPolys.size(), false);
  169. for (uint i = 0; i < testPolys.size(); ++i)
  170. {
  171. const std::vector<Point>& testPoly = testPolys[i];
  172. bool matched = false;
  173. double topIoU = 0;
  174. for (uint j = 0; j < gtPolys.size() && !matched; ++j)
  175. {
  176. if (!matchedRefBoxes[j])
  177. {
  178. std::vector<Point> intersectionPolygon;
  179. float intersectArea = intersectConvexConvex(testPoly, gtPolys[j], intersectionPolygon, true);
  180. double iou = intersectArea / (contourArea(testPoly) + contourArea(gtPolys[j]) - intersectArea);
  181. topIoU = std::max(topIoU, iou);
  182. if (1.0 - iou < boxes_iou_diff)
  183. {
  184. matched = true;
  185. matchedRefBoxes[j] = true;
  186. }
  187. }
  188. }
  189. if (!matched) {
  190. std::cout << cv::format("Unmatched-det:") << testPoly << std::endl;
  191. std::cout << "Highest IoU: " << topIoU << std::endl;
  192. }
  193. EXPECT_TRUE(matched) << comment;
  194. }
  195. // Check unmatched groundtruth.
  196. for (uint i = 0; i < gtPolys.size(); ++i)
  197. {
  198. if (!matchedRefBoxes[i]) {
  199. std::cout << cv::format("Unmatched-gt:") << gtPolys[i] << std::endl;
  200. }
  201. EXPECT_TRUE(matchedRefBoxes[i]);
  202. }
  203. }
  204. void readFileContent(const std::string& filename, CV_OUT std::vector<char>& content)
  205. {
  206. const std::ios::openmode mode = std::ios::in | std::ios::binary;
  207. std::ifstream ifs(filename.c_str(), mode);
  208. ASSERT_TRUE(ifs.is_open());
  209. content.clear();
  210. ifs.seekg(0, std::ios::end);
  211. const size_t sz = ifs.tellg();
  212. content.resize(sz);
  213. ifs.seekg(0, std::ios::beg);
  214. ifs.read((char*)content.data(), sz);
  215. ASSERT_FALSE(ifs.fail());
  216. }
  217. testing::internal::ParamGenerator< tuple<Backend, Target> > dnnBackendsAndTargets(
  218. bool withInferenceEngine /*= true*/,
  219. bool withHalide /*= false*/,
  220. bool withCpuOCV /*= true*/,
  221. bool withVkCom /*= true*/,
  222. bool withCUDA /*= true*/,
  223. bool withNgraph /*= true*/,
  224. bool withWebnn /*= false*/
  225. )
  226. {
  227. #ifdef HAVE_INF_ENGINE
  228. bool withVPU = validateVPUType();
  229. #endif
  230. std::vector< tuple<Backend, Target> > targets;
  231. std::vector< Target > available;
  232. if (withHalide)
  233. {
  234. available = getAvailableTargets(DNN_BACKEND_HALIDE);
  235. for (std::vector< Target >::const_iterator i = available.begin(); i != available.end(); ++i)
  236. targets.push_back(make_tuple(DNN_BACKEND_HALIDE, *i));
  237. }
  238. #ifdef HAVE_INF_ENGINE
  239. if (withInferenceEngine)
  240. {
  241. available = getAvailableTargets(DNN_BACKEND_INFERENCE_ENGINE_NN_BUILDER_2019);
  242. for (std::vector< Target >::const_iterator i = available.begin(); i != available.end(); ++i)
  243. {
  244. if ((*i == DNN_TARGET_MYRIAD || *i == DNN_TARGET_HDDL) && !withVPU)
  245. continue;
  246. targets.push_back(make_tuple(DNN_BACKEND_INFERENCE_ENGINE_NN_BUILDER_2019, *i));
  247. }
  248. }
  249. if (withNgraph)
  250. {
  251. available = getAvailableTargets(DNN_BACKEND_INFERENCE_ENGINE_NGRAPH);
  252. for (std::vector< Target >::const_iterator i = available.begin(); i != available.end(); ++i)
  253. {
  254. if ((*i == DNN_TARGET_MYRIAD || *i == DNN_TARGET_HDDL) && !withVPU)
  255. continue;
  256. targets.push_back(make_tuple(DNN_BACKEND_INFERENCE_ENGINE_NGRAPH, *i));
  257. }
  258. }
  259. #else
  260. CV_UNUSED(withInferenceEngine);
  261. #endif
  262. if (withVkCom)
  263. {
  264. available = getAvailableTargets(DNN_BACKEND_VKCOM);
  265. for (std::vector< Target >::const_iterator i = available.begin(); i != available.end(); ++i)
  266. targets.push_back(make_tuple(DNN_BACKEND_VKCOM, *i));
  267. }
  268. #ifdef HAVE_CUDA
  269. if(withCUDA)
  270. {
  271. for (auto target : getAvailableTargets(DNN_BACKEND_CUDA))
  272. targets.push_back(make_tuple(DNN_BACKEND_CUDA, target));
  273. }
  274. #endif
  275. #ifdef HAVE_WEBNN
  276. if (withWebnn)
  277. {
  278. for (auto target : getAvailableTargets(DNN_BACKEND_WEBNN)) {
  279. targets.push_back(make_tuple(DNN_BACKEND_WEBNN, target));
  280. }
  281. }
  282. #else
  283. CV_UNUSED(withWebnn);
  284. #endif
  285. {
  286. available = getAvailableTargets(DNN_BACKEND_OPENCV);
  287. for (std::vector< Target >::const_iterator i = available.begin(); i != available.end(); ++i)
  288. {
  289. if (!withCpuOCV && *i == DNN_TARGET_CPU)
  290. continue;
  291. targets.push_back(make_tuple(DNN_BACKEND_OPENCV, *i));
  292. }
  293. }
  294. if (targets.empty()) // validate at least CPU mode
  295. targets.push_back(make_tuple(DNN_BACKEND_OPENCV, DNN_TARGET_CPU));
  296. return testing::ValuesIn(targets);
  297. }
  298. testing::internal::ParamGenerator< tuple<Backend, Target> > dnnBackendsAndTargetsIE()
  299. {
  300. #ifdef HAVE_INF_ENGINE
  301. bool withVPU = validateVPUType();
  302. std::vector< tuple<Backend, Target> > targets;
  303. std::vector< Target > available;
  304. {
  305. available = getAvailableTargets(DNN_BACKEND_INFERENCE_ENGINE_NN_BUILDER_2019);
  306. for (std::vector< Target >::const_iterator i = available.begin(); i != available.end(); ++i)
  307. {
  308. if ((*i == DNN_TARGET_MYRIAD || *i == DNN_TARGET_HDDL) && !withVPU)
  309. continue;
  310. targets.push_back(make_tuple(DNN_BACKEND_INFERENCE_ENGINE_NN_BUILDER_2019, *i));
  311. }
  312. }
  313. {
  314. available = getAvailableTargets(DNN_BACKEND_INFERENCE_ENGINE_NGRAPH);
  315. for (std::vector< Target >::const_iterator i = available.begin(); i != available.end(); ++i)
  316. {
  317. if ((*i == DNN_TARGET_MYRIAD || *i == DNN_TARGET_HDDL) && !withVPU)
  318. continue;
  319. targets.push_back(make_tuple(DNN_BACKEND_INFERENCE_ENGINE_NGRAPH, *i));
  320. }
  321. }
  322. return testing::ValuesIn(targets);
  323. #else
  324. return testing::ValuesIn(std::vector< tuple<Backend, Target> >());
  325. #endif
  326. }
  327. #ifdef HAVE_INF_ENGINE
  328. static std::string getTestInferenceEngineVPUType()
  329. {
  330. static std::string param_vpu_type = utils::getConfigurationParameterString("OPENCV_TEST_DNN_IE_VPU_TYPE", "");
  331. return param_vpu_type;
  332. }
  333. static bool validateVPUType_()
  334. {
  335. std::string test_vpu_type = getTestInferenceEngineVPUType();
  336. if (test_vpu_type == "DISABLED" || test_vpu_type == "disabled")
  337. {
  338. return false;
  339. }
  340. std::vector<Target> available = getAvailableTargets(DNN_BACKEND_INFERENCE_ENGINE);
  341. bool have_vpu_target = false;
  342. for (std::vector<Target>::const_iterator i = available.begin(); i != available.end(); ++i)
  343. {
  344. if (*i == DNN_TARGET_MYRIAD || *i == DNN_TARGET_HDDL)
  345. {
  346. have_vpu_target = true;
  347. break;
  348. }
  349. }
  350. if (test_vpu_type.empty())
  351. {
  352. if (have_vpu_target)
  353. {
  354. CV_LOG_INFO(NULL, "OpenCV-DNN-Test: VPU type for testing is not specified via 'OPENCV_TEST_DNN_IE_VPU_TYPE' parameter.")
  355. }
  356. }
  357. else
  358. {
  359. if (!have_vpu_target)
  360. {
  361. 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.");
  362. exit(1);
  363. }
  364. std::string dnn_vpu_type = getInferenceEngineVPUType();
  365. if (dnn_vpu_type != test_vpu_type)
  366. {
  367. CV_LOG_FATAL(NULL, "OpenCV-DNN-Test: 'testing' and 'detected' VPU types mismatch: '" << test_vpu_type << "' vs '" << dnn_vpu_type << "'. STOP.");
  368. exit(1);
  369. }
  370. }
  371. if (have_vpu_target)
  372. {
  373. std::string dnn_vpu_type = getInferenceEngineVPUType();
  374. if (dnn_vpu_type == CV_DNN_INFERENCE_ENGINE_VPU_TYPE_MYRIAD_2)
  375. registerGlobalSkipTag(CV_TEST_TAG_DNN_SKIP_IE_MYRIAD_2);
  376. if (dnn_vpu_type == CV_DNN_INFERENCE_ENGINE_VPU_TYPE_MYRIAD_X)
  377. registerGlobalSkipTag(CV_TEST_TAG_DNN_SKIP_IE_MYRIAD_X);
  378. }
  379. return true;
  380. }
  381. bool validateVPUType()
  382. {
  383. static bool result = validateVPUType_();
  384. return result;
  385. }
  386. #endif // HAVE_INF_ENGINE
  387. void initDNNTests()
  388. {
  389. const char* extraTestDataPath =
  390. #ifdef WINRT
  391. NULL;
  392. #else
  393. getenv("OPENCV_DNN_TEST_DATA_PATH");
  394. #endif
  395. if (extraTestDataPath)
  396. cvtest::addDataSearchPath(extraTestDataPath);
  397. registerGlobalSkipTag(
  398. CV_TEST_TAG_DNN_SKIP_OPENCV_BACKEND,
  399. CV_TEST_TAG_DNN_SKIP_CPU,
  400. CV_TEST_TAG_DNN_SKIP_OPENCL, CV_TEST_TAG_DNN_SKIP_OPENCL_FP16
  401. );
  402. #if defined(HAVE_HALIDE)
  403. registerGlobalSkipTag(
  404. CV_TEST_TAG_DNN_SKIP_HALIDE
  405. );
  406. #endif
  407. #if defined(INF_ENGINE_RELEASE)
  408. registerGlobalSkipTag(
  409. CV_TEST_TAG_DNN_SKIP_IE,
  410. #if INF_ENGINE_VER_MAJOR_EQ(2018050000)
  411. CV_TEST_TAG_DNN_SKIP_IE_2018R5,
  412. #elif INF_ENGINE_VER_MAJOR_EQ(2019010000)
  413. CV_TEST_TAG_DNN_SKIP_IE_2019R1,
  414. # if INF_ENGINE_RELEASE == 2019010100
  415. CV_TEST_TAG_DNN_SKIP_IE_2019R1_1,
  416. # endif
  417. #elif INF_ENGINE_VER_MAJOR_EQ(2019020000)
  418. CV_TEST_TAG_DNN_SKIP_IE_2019R2,
  419. #elif INF_ENGINE_VER_MAJOR_EQ(2019030000)
  420. CV_TEST_TAG_DNN_SKIP_IE_2019R3,
  421. #endif
  422. #ifdef HAVE_DNN_NGRAPH
  423. CV_TEST_TAG_DNN_SKIP_IE_NGRAPH,
  424. #endif
  425. #ifdef HAVE_DNN_IE_NN_BUILDER_2019
  426. CV_TEST_TAG_DNN_SKIP_IE_NN_BUILDER,
  427. #endif
  428. CV_TEST_TAG_DNN_SKIP_IE_CPU
  429. );
  430. registerGlobalSkipTag(
  431. // see validateVPUType(): CV_TEST_TAG_DNN_SKIP_IE_MYRIAD_2, CV_TEST_TAG_DNN_SKIP_IE_MYRIAD_X
  432. CV_TEST_TAG_DNN_SKIP_IE_OPENCL, CV_TEST_TAG_DNN_SKIP_IE_OPENCL_FP16
  433. );
  434. #endif
  435. #ifdef HAVE_VULKAN
  436. registerGlobalSkipTag(
  437. CV_TEST_TAG_DNN_SKIP_VULKAN
  438. );
  439. #endif
  440. #ifdef HAVE_CUDA
  441. registerGlobalSkipTag(
  442. CV_TEST_TAG_DNN_SKIP_CUDA, CV_TEST_TAG_DNN_SKIP_CUDA_FP32, CV_TEST_TAG_DNN_SKIP_CUDA_FP16
  443. );
  444. #endif
  445. registerGlobalSkipTag(
  446. CV_TEST_TAG_DNN_SKIP_ONNX_CONFORMANCE,
  447. CV_TEST_TAG_DNN_SKIP_PARSER
  448. );
  449. }
  450. } // namespace