test_darknet_importer.cpp 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157
  1. /*M///////////////////////////////////////////////////////////////////////////////////////
  2. //
  3. // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
  4. //
  5. // By downloading, copying, installing or using the software you agree to this license.
  6. // If you do not agree to this license, do not download, install,
  7. // copy or use the software.
  8. //
  9. //
  10. // License Agreement
  11. // For Open Source Computer Vision Library
  12. // (3-clause BSD License)
  13. //
  14. // Copyright (C) 2017, Intel Corporation, all rights reserved.
  15. // Third party copyrights are property of their respective owners.
  16. //
  17. // Redistribution and use in source and binary forms, with or without modification,
  18. // are permitted provided that the following conditions are met:
  19. //
  20. // * Redistributions of source code must retain the above copyright notice,
  21. // this list of conditions and the following disclaimer.
  22. //
  23. // * Redistributions in binary form must reproduce the above copyright notice,
  24. // this list of conditions and the following disclaimer in the documentation
  25. // and/or other materials provided with the distribution.
  26. //
  27. // * Neither the names of the copyright holders nor the names of the contributors
  28. // may be used to endorse or promote products derived from this software
  29. // without specific prior written permission.
  30. //
  31. // This software is provided by the copyright holders and contributors "as is" and
  32. // any express or implied warranties, including, but not limited to, the implied
  33. // warranties of merchantability and fitness for a particular purpose are disclaimed.
  34. // In no event shall copyright holders or contributors be liable for any direct,
  35. // indirect, incidental, special, exemplary, or consequential damages
  36. // (including, but not limited to, procurement of substitute goods or services;
  37. // loss of use, data, or profits; or business interruption) however caused
  38. // and on any theory of liability, whether in contract, strict liability,
  39. // or tort (including negligence or otherwise) arising in any way out of
  40. // the use of this software, even if advised of the possibility of such damage.
  41. //
  42. //M*/
  43. #include "test_precomp.hpp"
  44. #include "npy_blob.hpp"
  45. #include <opencv2/dnn/shape_utils.hpp>
  46. namespace opencv_test { namespace {
  47. template<typename TString>
  48. static std::string _tf(TString filename)
  49. {
  50. return (getOpenCVExtraDir() + "/dnn/") + filename;
  51. }
  52. TEST(Test_Darknet, read_tiny_yolo_voc)
  53. {
  54. Net net = readNetFromDarknet(_tf("tiny-yolo-voc.cfg"));
  55. ASSERT_FALSE(net.empty());
  56. }
  57. TEST(Test_Darknet, read_yolo_voc)
  58. {
  59. Net net = readNetFromDarknet(_tf("yolo-voc.cfg"));
  60. ASSERT_FALSE(net.empty());
  61. }
  62. TEST(Test_Darknet, read_yolo_voc_stream)
  63. {
  64. applyTestTag(
  65. CV_TEST_TAG_MEMORY_1GB,
  66. CV_TEST_TAG_DEBUG_VERYLONG
  67. );
  68. Mat ref;
  69. Mat sample = imread(_tf("dog416.png"));
  70. Mat inp = blobFromImage(sample, 1.0/255, Size(416, 416), Scalar(), true, false);
  71. const std::string cfgFile = findDataFile("dnn/yolo-voc.cfg");
  72. const std::string weightsFile = findDataFile("dnn/yolo-voc.weights", false);
  73. // Import by paths.
  74. {
  75. Net net = readNetFromDarknet(cfgFile, weightsFile);
  76. net.setInput(inp);
  77. net.setPreferableBackend(DNN_BACKEND_OPENCV);
  78. net.enableWinograd(false);
  79. ref = net.forward();
  80. }
  81. // Import from bytes array.
  82. {
  83. std::vector<char> cfg, weights;
  84. readFileContent(cfgFile, cfg);
  85. readFileContent(weightsFile, weights);
  86. Net net = readNetFromDarknet(cfg.data(), cfg.size(), weights.data(), weights.size());
  87. net.setInput(inp);
  88. net.setPreferableBackend(DNN_BACKEND_OPENCV);
  89. net.enableWinograd(false);
  90. Mat out = net.forward();
  91. normAssert(ref, out);
  92. }
  93. }
  94. class Test_Darknet_layers : public DNNTestLayer
  95. {
  96. public:
  97. void testDarknetLayer(const std::string& name, bool hasWeights = false, bool testBatchProcessing = true,
  98. double l1 = 0.0, double lInf = 0.0)
  99. {
  100. SCOPED_TRACE(name);
  101. Mat inp = blobFromNPY(findDataFile("dnn/darknet/" + name + "_in.npy"));
  102. Mat ref = blobFromNPY(findDataFile("dnn/darknet/" + name + "_out.npy"));
  103. l1 = l1 ? l1 : default_l1;
  104. lInf = lInf ? lInf : default_lInf;
  105. std::string cfg = findDataFile("dnn/darknet/" + name + ".cfg");
  106. std::string model = "";
  107. if (hasWeights)
  108. model = findDataFile("dnn/darknet/" + name + ".weights");
  109. checkBackend(&inp, &ref);
  110. Net net = readNet(cfg, model);
  111. net.setPreferableBackend(backend);
  112. net.setPreferableTarget(target);
  113. net.setInput(inp);
  114. Mat out = net.forward();
  115. normAssert(out, ref, "", l1, lInf);
  116. if (inp.size[0] == 1 && testBatchProcessing) // test handling of batch size
  117. {
  118. SCOPED_TRACE("batch size 2");
  119. #if defined(INF_ENGINE_RELEASE) && INF_ENGINE_VER_MAJOR_LT(2021040000)
  120. if (target == DNN_TARGET_MYRIAD && name == "shortcut")
  121. applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_MYRIAD);
  122. #endif
  123. std::vector<int> sz2 = shape(inp);
  124. sz2[0] = 2;
  125. Net net2 = readNet(cfg, model);
  126. net2.setPreferableBackend(backend);
  127. net2.setPreferableTarget(target);
  128. Range ranges0[4] = { Range(0, 1), Range::all(), Range::all(), Range::all() };
  129. Range ranges1[4] = { Range(1, 2), Range::all(), Range::all(), Range::all() };
  130. Mat inp2(sz2, inp.type(), Scalar::all(0));
  131. inp.copyTo(inp2(ranges0));
  132. inp.copyTo(inp2(ranges1));
  133. net2.setInput(inp2);
  134. Mat out2 = net2.forward();
  135. if (backend == DNN_BACKEND_INFERENCE_ENGINE_NGRAPH)
  136. {
  137. EXPECT_LT(cv::norm(out2(ranges0), out2(ranges1), NORM_INF), 1e-4) << "Batch result is not similar: " << name;
  138. }
  139. else
  140. {
  141. EXPECT_EQ(0, cv::norm(out2(ranges0), out2(ranges1), NORM_INF)) << "Batch result is not equal: " << name;
  142. }
  143. Mat ref2 = ref;
  144. if (ref.dims == 2 && out2.dims == 3)
  145. {
  146. int ref_3d_sizes[3] = {1, ref.rows, ref.cols};
  147. ref2 = Mat(3, ref_3d_sizes, ref.type(), (void*)ref.data);
  148. }
  149. /*else if (ref.dims == 3 && out2.dims == 4)
  150. {
  151. int ref_4d_sizes[4] = {1, ref.size[0], ref.size[1], ref.size[2]};
  152. ref2 = Mat(4, ref_4d_sizes, ref.type(), (void*)ref.data);
  153. }*/
  154. ASSERT_EQ(out2.dims, ref2.dims) << ref.dims;
  155. normAssert(out2(ranges0), ref2, "", l1, lInf);
  156. normAssert(out2(ranges1), ref2, "", l1, lInf);
  157. }
  158. }
  159. };
  160. class Test_Darknet_nets : public DNNTestLayer
  161. {
  162. public:
  163. // Test object detection network from Darknet framework.
  164. void testDarknetModel(const std::string& cfg, const std::string& weights,
  165. const std::vector<std::vector<int> >& refClassIds,
  166. const std::vector<std::vector<float> >& refConfidences,
  167. const std::vector<std::vector<Rect2d> >& refBoxes,
  168. double scoreDiff, double iouDiff, float confThreshold = 0.24,
  169. float nmsThreshold = 0.4, bool useWinograd = true,
  170. int zeroPadW = 0)
  171. {
  172. checkBackend();
  173. Mat img1 = imread(_tf("dog416.png"));
  174. Mat img2 = imread(_tf("street.png"));
  175. cv::resize(img2, img2, Size(416, 416));
  176. // Pad images by black pixel at the right to test not equal width and height sizes
  177. if (zeroPadW) {
  178. cv::copyMakeBorder(img1, img1, 0, 0, 0, zeroPadW, BORDER_CONSTANT);
  179. cv::copyMakeBorder(img2, img2, 0, 0, 0, zeroPadW, BORDER_CONSTANT);
  180. }
  181. std::vector<Mat> samples(2);
  182. samples[0] = img1; samples[1] = img2;
  183. // determine test type, whether batch or single img
  184. int batch_size = refClassIds.size();
  185. CV_Assert(batch_size == 1 || batch_size == 2);
  186. samples.resize(batch_size);
  187. Mat inp = blobFromImages(samples, 1.0/255, Size(), Scalar(), true, false);
  188. Net net = readNet(findDataFile("dnn/" + cfg),
  189. findDataFile("dnn/" + weights, false));
  190. net.setPreferableBackend(backend);
  191. net.setPreferableTarget(target);
  192. net.enableWinograd(useWinograd);
  193. net.setInput(inp);
  194. std::vector<Mat> outs;
  195. net.forward(outs, net.getUnconnectedOutLayersNames());
  196. for (int b = 0; b < batch_size; ++b)
  197. {
  198. std::vector<int> classIds;
  199. std::vector<float> confidences;
  200. std::vector<Rect2d> boxes;
  201. for (int i = 0; i < outs.size(); ++i)
  202. {
  203. Mat out;
  204. if (batch_size > 1){
  205. // get the sample slice from 3D matrix (batch, box, classes+5)
  206. Range ranges[3] = {Range(b, b+1), Range::all(), Range::all()};
  207. out = outs[i](ranges).reshape(1, outs[i].size[1]);
  208. }else{
  209. out = outs[i];
  210. }
  211. for (int j = 0; j < out.rows; ++j)
  212. {
  213. Mat scores = out.row(j).colRange(5, out.cols);
  214. double confidence;
  215. Point maxLoc;
  216. minMaxLoc(scores, 0, &confidence, 0, &maxLoc);
  217. if (confidence > confThreshold) {
  218. float* detection = out.ptr<float>(j);
  219. double centerX = detection[0];
  220. double centerY = detection[1];
  221. double width = detection[2];
  222. double height = detection[3];
  223. boxes.push_back(Rect2d(centerX - 0.5 * width, centerY - 0.5 * height,
  224. width, height));
  225. confidences.push_back(confidence);
  226. classIds.push_back(maxLoc.x);
  227. }
  228. }
  229. }
  230. // here we need NMS of boxes
  231. std::vector<int> indices;
  232. NMSBoxes(boxes, confidences, confThreshold, nmsThreshold, indices);
  233. std::vector<int> nms_classIds;
  234. std::vector<float> nms_confidences;
  235. std::vector<Rect2d> nms_boxes;
  236. for (size_t i = 0; i < indices.size(); ++i)
  237. {
  238. int idx = indices[i];
  239. Rect2d box = boxes[idx];
  240. float conf = confidences[idx];
  241. int class_id = classIds[idx];
  242. nms_boxes.push_back(box);
  243. nms_confidences.push_back(conf);
  244. nms_classIds.push_back(class_id);
  245. if (cvtest::debugLevel > 0)
  246. {
  247. std::cout << b << ", " << class_id << ", " << conf << "f, "
  248. << box.x << "f, " << box.y << "f, "
  249. << box.x + box.width << "f, " << box.y + box.height << "f,"
  250. << std::endl;
  251. }
  252. }
  253. if (cvIsNaN(iouDiff))
  254. {
  255. if (b == 0)
  256. std::cout << "Skip accuracy checks" << std::endl;
  257. continue;
  258. }
  259. // Return predictions from padded image to the origin
  260. if (zeroPadW) {
  261. float scale = static_cast<float>(inp.size[3]) / (inp.size[3] - zeroPadW);
  262. for (auto& box : nms_boxes) {
  263. box.x *= scale;
  264. box.width *= scale;
  265. }
  266. }
  267. normAssertDetections(refClassIds[b], refConfidences[b], refBoxes[b], nms_classIds,
  268. nms_confidences, nms_boxes, format("batch size %d, sample %d\n", batch_size, b).c_str(), confThreshold, scoreDiff, iouDiff);
  269. }
  270. }
  271. void testDarknetModel(const std::string& cfg, const std::string& weights,
  272. const std::vector<int>& refClassIds,
  273. const std::vector<float>& refConfidences,
  274. const std::vector<Rect2d>& refBoxes,
  275. double scoreDiff, double iouDiff, float confThreshold = 0.24,
  276. float nmsThreshold = 0.4, bool useWinograd = true,
  277. int zeroPadW = 0)
  278. {
  279. testDarknetModel(cfg, weights,
  280. std::vector<std::vector<int> >(1, refClassIds),
  281. std::vector<std::vector<float> >(1, refConfidences),
  282. std::vector<std::vector<Rect2d> >(1, refBoxes),
  283. scoreDiff, iouDiff, confThreshold, nmsThreshold, useWinograd, zeroPadW);
  284. }
  285. void testDarknetModel(const std::string& cfg, const std::string& weights,
  286. const cv::Mat& ref, double scoreDiff, double iouDiff,
  287. float confThreshold = 0.24, float nmsThreshold = 0.4, bool useWinograd = true,
  288. int zeroPadW = 0)
  289. {
  290. CV_Assert(ref.cols == 7);
  291. std::vector<std::vector<int> > refClassIds;
  292. std::vector<std::vector<float> > refScores;
  293. std::vector<std::vector<Rect2d> > refBoxes;
  294. for (int i = 0; i < ref.rows; ++i)
  295. {
  296. int batchId = static_cast<int>(ref.at<float>(i, 0));
  297. int classId = static_cast<int>(ref.at<float>(i, 1));
  298. float score = ref.at<float>(i, 2);
  299. float left = ref.at<float>(i, 3);
  300. float top = ref.at<float>(i, 4);
  301. float right = ref.at<float>(i, 5);
  302. float bottom = ref.at<float>(i, 6);
  303. Rect2d box(left, top, right - left, bottom - top);
  304. if (batchId >= refClassIds.size())
  305. {
  306. refClassIds.resize(batchId + 1);
  307. refScores.resize(batchId + 1);
  308. refBoxes.resize(batchId + 1);
  309. }
  310. refClassIds[batchId].push_back(classId);
  311. refScores[batchId].push_back(score);
  312. refBoxes[batchId].push_back(box);
  313. }
  314. testDarknetModel(cfg, weights, refClassIds, refScores, refBoxes,
  315. scoreDiff, iouDiff, confThreshold, nmsThreshold, useWinograd, zeroPadW);
  316. }
  317. };
  318. TEST_P(Test_Darknet_nets, YoloVoc)
  319. {
  320. applyTestTag(
  321. #if defined(OPENCV_32BIT_CONFIGURATION) && defined(HAVE_OPENCL)
  322. CV_TEST_TAG_MEMORY_2GB,
  323. #else
  324. CV_TEST_TAG_MEMORY_1GB,
  325. #endif
  326. CV_TEST_TAG_LONG,
  327. CV_TEST_TAG_DEBUG_VERYLONG
  328. );
  329. #if defined(INF_ENGINE_RELEASE) && INF_ENGINE_VER_MAJOR_EQ(2020040000) // nGraph compilation failure
  330. if (backend == DNN_BACKEND_INFERENCE_ENGINE_NGRAPH && target == DNN_TARGET_OPENCL)
  331. applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_OPENCL, CV_TEST_TAG_DNN_SKIP_IE_VERSION);
  332. if (backend == DNN_BACKEND_INFERENCE_ENGINE_NGRAPH && target == DNN_TARGET_OPENCL_FP16)
  333. applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_OPENCL_FP16, CV_TEST_TAG_DNN_SKIP_IE_VERSION);
  334. #elif defined(INF_ENGINE_RELEASE) && INF_ENGINE_VER_MAJOR_GE(2019010000)
  335. if (backend == DNN_BACKEND_INFERENCE_ENGINE_NN_BUILDER_2019 && target == DNN_TARGET_OPENCL_FP16)
  336. applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_OPENCL_FP16);
  337. #elif defined(INF_ENGINE_RELEASE)
  338. if ((backend == DNN_BACKEND_INFERENCE_ENGINE_NN_BUILDER_2019 || backend == DNN_BACKEND_INFERENCE_ENGINE_NGRAPH) &&
  339. target == DNN_TARGET_MYRIAD && getInferenceEngineVPUType() == CV_DNN_INFERENCE_ENGINE_VPU_TYPE_MYRIAD_X)
  340. applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_MYRIAD_X); // need to update check function
  341. #endif
  342. // batchId, classId, confidence, left, top, right, bottom
  343. Mat ref = (Mat_<float>(6, 7) << 0, 6, 0.750469f, 0.577374f, 0.127391f, 0.902949f, 0.300809f, // a car
  344. 0, 1, 0.780879f, 0.270762f, 0.264102f, 0.732475f, 0.745412f, // a bicycle
  345. 0, 11, 0.901615f, 0.1386f, 0.338509f, 0.421337f, 0.938789f, // a dog
  346. 1, 14, 0.623813f, 0.183179f, 0.381921f, 0.247726f, 0.625847f, // a person
  347. 1, 6, 0.667770f, 0.446555f, 0.453578f, 0.499986f, 0.519167f, // a car
  348. 1, 6, 0.844947f, 0.637058f, 0.460398f, 0.828508f, 0.66427f); // a car
  349. double nmsThreshold = (target == DNN_TARGET_MYRIAD || target == DNN_TARGET_CPU_FP16) ? 0.397 : 0.4;
  350. double scoreDiff = 8e-5, iouDiff = 3e-4;
  351. if (target == DNN_TARGET_OPENCL_FP16 || target == DNN_TARGET_MYRIAD || target == DNN_TARGET_CPU_FP16)
  352. {
  353. scoreDiff = 1e-2;
  354. iouDiff = 0.018;
  355. }
  356. else if (target == DNN_TARGET_CUDA_FP16)
  357. {
  358. scoreDiff = 0.03;
  359. iouDiff = 0.018;
  360. }
  361. #if defined(INF_ENGINE_RELEASE) && INF_ENGINE_VER_MAJOR_EQ(2022010000)
  362. // accuracy
  363. if (backend == DNN_BACKEND_INFERENCE_ENGINE_NGRAPH && target == DNN_TARGET_OPENCL_FP16)
  364. {
  365. iouDiff = std::numeric_limits<double>::quiet_NaN();
  366. }
  367. // accuracy
  368. if (backend == DNN_BACKEND_INFERENCE_ENGINE_NGRAPH && target == DNN_TARGET_MYRIAD)
  369. {
  370. iouDiff = std::numeric_limits<double>::quiet_NaN();
  371. }
  372. #elif defined(INF_ENGINE_RELEASE) && INF_ENGINE_VER_MAJOR_EQ(2021040000)
  373. // accuracy
  374. if (backend == DNN_BACKEND_INFERENCE_ENGINE_NGRAPH && target == DNN_TARGET_OPENCL_FP16)
  375. {
  376. iouDiff = std::numeric_limits<double>::quiet_NaN();
  377. }
  378. // accuracy
  379. if (backend == DNN_BACKEND_INFERENCE_ENGINE_NGRAPH && target == DNN_TARGET_MYRIAD)
  380. {
  381. iouDiff = std::numeric_limits<double>::quiet_NaN();
  382. }
  383. #endif
  384. std::string config_file = "yolo-voc.cfg";
  385. std::string weights_file = "yolo-voc.weights";
  386. {
  387. SCOPED_TRACE("batch size 1");
  388. testDarknetModel(config_file, weights_file, ref.rowRange(0, 3), scoreDiff, iouDiff, 0.24, 0.4, false);
  389. }
  390. #if defined(INF_ENGINE_RELEASE) && INF_ENGINE_VER_MAJOR_EQ(2022010000)
  391. // Exception: input != output
  392. if (backend == DNN_BACKEND_INFERENCE_ENGINE_NGRAPH && target == DNN_TARGET_MYRIAD)
  393. applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_MYRIAD, CV_TEST_TAG_DNN_SKIP_IE_NGRAPH, CV_TEST_TAG_DNN_SKIP_IE_VERSION);
  394. #elif defined(INF_ENGINE_RELEASE) && INF_ENGINE_VER_MAJOR_EQ(2021040000)
  395. // [ GENERAL_ERROR ] AssertionFailed: input != output
  396. if (backend == DNN_BACKEND_INFERENCE_ENGINE_NGRAPH && target == DNN_TARGET_MYRIAD)
  397. applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_MYRIAD, CV_TEST_TAG_DNN_SKIP_IE_NGRAPH CV_TEST_TAG_DNN_SKIP_IE_VERSION);
  398. #endif
  399. {
  400. SCOPED_TRACE("batch size 2");
  401. testDarknetModel(config_file, weights_file, ref, scoreDiff, iouDiff, 0.24, nmsThreshold, false);
  402. }
  403. #if defined(INF_ENGINE_RELEASE) && INF_ENGINE_VER_MAJOR_EQ(2022010000)
  404. // accuracy
  405. if (backend == DNN_BACKEND_INFERENCE_ENGINE_NGRAPH && target == DNN_TARGET_OPENCL_FP16)
  406. applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_OPENCL_FP16, CV_TEST_TAG_DNN_SKIP_IE_NGRAPH, CV_TEST_TAG_DNN_SKIP_IE_VERSION);
  407. #elif defined(INF_ENGINE_RELEASE) && INF_ENGINE_VER_MAJOR_EQ(2021040000)
  408. // accuracy
  409. if (backend == DNN_BACKEND_INFERENCE_ENGINE_NGRAPH && target == DNN_TARGET_OPENCL_FP16)
  410. applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_OPENCL_FP16, CV_TEST_TAG_DNN_SKIP_IE_NGRAPH, CV_TEST_TAG_DNN_SKIP_IE_VERSION);
  411. #endif
  412. }
  413. TEST_P(Test_Darknet_nets, TinyYoloVoc)
  414. {
  415. applyTestTag(CV_TEST_TAG_MEMORY_512MB);
  416. #if defined(INF_ENGINE_RELEASE) && INF_ENGINE_VER_MAJOR_EQ(2020040000) // nGraph compilation failure
  417. if (backend == DNN_BACKEND_INFERENCE_ENGINE_NGRAPH && target == DNN_TARGET_OPENCL)
  418. applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_OPENCL, CV_TEST_TAG_DNN_SKIP_IE_VERSION);
  419. if (backend == DNN_BACKEND_INFERENCE_ENGINE_NGRAPH && target == DNN_TARGET_OPENCL_FP16)
  420. applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_OPENCL_FP16, CV_TEST_TAG_DNN_SKIP_IE_VERSION);
  421. #endif
  422. #if defined(INF_ENGINE_RELEASE)
  423. if ((backend == DNN_BACKEND_INFERENCE_ENGINE_NN_BUILDER_2019 || backend == DNN_BACKEND_INFERENCE_ENGINE_NGRAPH) &&
  424. target == DNN_TARGET_MYRIAD && getInferenceEngineVPUType() == CV_DNN_INFERENCE_ENGINE_VPU_TYPE_MYRIAD_X)
  425. applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_MYRIAD_X); // need to update check function
  426. #endif
  427. // batchId, classId, confidence, left, top, right, bottom
  428. Mat ref = (Mat_<float>(4, 7) << 0, 6, 0.761967f, 0.579042f, 0.159161f, 0.894482f, 0.31994f, // a car
  429. 0, 11, 0.780595f, 0.129696f, 0.386467f, 0.445275f, 0.920994f, // a dog
  430. 1, 6, 0.651450f, 0.460526f, 0.458019f, 0.522527f, 0.5341f, // a car
  431. 1, 6, 0.928758f, 0.651024f, 0.463539f, 0.823784f, 0.654998f); // a car
  432. double scoreDiff = 8e-5, iouDiff = 3e-4;
  433. bool useWinograd = true;
  434. if (target == DNN_TARGET_OPENCL_FP16 || target == DNN_TARGET_MYRIAD)
  435. {
  436. scoreDiff = 8e-3;
  437. iouDiff = 0.018;
  438. }
  439. else if(target == DNN_TARGET_CUDA_FP16)
  440. {
  441. scoreDiff = 0.008;
  442. iouDiff = 0.02;
  443. }
  444. else if (target == DNN_TARGET_CPU_FP16)
  445. {
  446. useWinograd = false;
  447. scoreDiff = 8e-3;
  448. iouDiff = 0.018;
  449. }
  450. std::string config_file = "tiny-yolo-voc.cfg";
  451. std::string weights_file = "tiny-yolo-voc.weights";
  452. {
  453. SCOPED_TRACE("batch size 1");
  454. testDarknetModel(config_file, weights_file, ref.rowRange(0, 2), scoreDiff, iouDiff, 0.24, 0.4, useWinograd);
  455. }
  456. {
  457. SCOPED_TRACE("batch size 2");
  458. testDarknetModel(config_file, weights_file, ref, scoreDiff, iouDiff, 0.24, 0.4, useWinograd);
  459. }
  460. }
  461. #ifdef HAVE_INF_ENGINE
  462. static const std::chrono::milliseconds async_timeout(10000);
  463. typedef testing::TestWithParam<tuple<std::string, tuple<Backend, Target> > > Test_Darknet_nets_async;
  464. TEST_P(Test_Darknet_nets_async, Accuracy)
  465. {
  466. Backend backendId = get<0>(get<1>(GetParam()));
  467. Target targetId = get<1>(get<1>(GetParam()));
  468. std::string prefix = get<0>(GetParam());
  469. applyTestTag(CV_TEST_TAG_MEMORY_512MB);
  470. #if defined(INF_ENGINE_RELEASE) && INF_ENGINE_VER_MAJOR_LT(2021040000)
  471. if (INF_ENGINE_VER_MAJOR_LT(2019020000) && backendId == DNN_BACKEND_INFERENCE_ENGINE_NN_BUILDER_2019)
  472. applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_NN_BUILDER);
  473. if (backendId == DNN_BACKEND_INFERENCE_ENGINE_NGRAPH)
  474. applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_NGRAPH);
  475. #endif
  476. if (backendId != DNN_BACKEND_INFERENCE_ENGINE_NN_BUILDER_2019 && backendId != DNN_BACKEND_INFERENCE_ENGINE_NGRAPH)
  477. throw SkipTestException("No support for async forward");
  478. #if defined(INF_ENGINE_RELEASE) && INF_ENGINE_VER_MAJOR_EQ(2022010000)
  479. if (targetId == DNN_TARGET_MYRIAD && prefix == "yolov3") // NC_OUT_OF_MEMORY
  480. applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_MYRIAD, CV_TEST_TAG_DNN_SKIP_IE_NGRAPH, CV_TEST_TAG_DNN_SKIP_IE_VERSION);
  481. #elif defined(INF_ENGINE_RELEASE) && INF_ENGINE_VER_MAJOR_EQ(2021040000)
  482. if (targetId == DNN_TARGET_MYRIAD && prefix == "yolov3") // NC_OUT_OF_MEMORY
  483. applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_MYRIAD, CV_TEST_TAG_DNN_SKIP_IE_NGRAPH, CV_TEST_TAG_DNN_SKIP_IE_VERSION);
  484. #elif defined(INF_ENGINE_RELEASE)
  485. if (targetId == DNN_TARGET_MYRIAD && prefix == "yolov4") // NC_OUT_OF_MEMORY
  486. applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_MYRIAD, CV_TEST_TAG_DNN_SKIP_IE_VERSION);
  487. #endif
  488. const int numInputs = 2;
  489. std::vector<Mat> inputs(numInputs);
  490. int blobSize[] = {1, 3, 416, 416};
  491. for (int i = 0; i < numInputs; ++i)
  492. {
  493. inputs[i].create(4, &blobSize[0], CV_32F);
  494. randu(inputs[i], 0, 1);
  495. }
  496. Net netSync = readNet(findDataFile("dnn/" + prefix + ".cfg"),
  497. findDataFile("dnn/" + prefix + ".weights", false));
  498. netSync.setPreferableBackend(backendId);
  499. netSync.setPreferableTarget(targetId);
  500. // Run synchronously.
  501. std::vector<Mat> refs(numInputs);
  502. for (int i = 0; i < numInputs; ++i)
  503. {
  504. netSync.setInput(inputs[i]);
  505. refs[i] = netSync.forward().clone();
  506. }
  507. Net netAsync = readNet(findDataFile("dnn/" + prefix + ".cfg"),
  508. findDataFile("dnn/" + prefix + ".weights", false));
  509. netAsync.setPreferableBackend(backendId);
  510. netAsync.setPreferableTarget(targetId);
  511. double l1 = 0.0;
  512. double lInf = 0.0;
  513. #if defined(INF_ENGINE_RELEASE)
  514. if (backendId == DNN_BACKEND_INFERENCE_ENGINE_NGRAPH)
  515. {
  516. if (targetId == DNN_TARGET_MYRIAD && prefix == "yolo-voc")
  517. {
  518. l1 = 0.02;
  519. lInf = 0.15;
  520. }
  521. if (targetId == DNN_TARGET_OPENCL_FP16 && prefix == "yolo-voc")
  522. {
  523. l1 = 0.02;
  524. lInf = 0.1;
  525. }
  526. if (targetId == DNN_TARGET_OPENCL_FP16 && prefix == "yolov3")
  527. {
  528. l1 = 0.001;
  529. lInf = 0.007;
  530. }
  531. if (targetId == DNN_TARGET_OPENCL_FP16 && prefix == "yolov4")
  532. {
  533. l1 = 0.001;
  534. lInf = 0.005;
  535. }
  536. if (INF_ENGINE_VER_MAJOR_EQ(2021040000) && targetId == DNN_TARGET_OPENCL_FP16 && prefix == "yolov4-tiny-2020-12") // FIXIT: 4.x only, 3.4 branch works well
  537. {
  538. l1 = 0.001;
  539. lInf = 0.005;
  540. }
  541. if (INF_ENGINE_VER_MAJOR_EQ(2022010000) && targetId == DNN_TARGET_OPENCL_FP16 && prefix == "yolov4-tiny-2020-12") // FIXIT: 4.x only, 3.4 branch works well
  542. {
  543. l1 = 0.001;
  544. lInf = 0.005;
  545. }
  546. if (targetId == DNN_TARGET_MYRIAD && prefix == "yolov4")
  547. {
  548. l1 = 0.005;
  549. lInf = 1.6f; // |ref| = 0.95431125164031982
  550. }
  551. }
  552. #endif
  553. // Run asynchronously. To make test more robust, process inputs in the reversed order.
  554. for (int i = numInputs - 1; i >= 0; --i)
  555. {
  556. netAsync.setInput(inputs[i]);
  557. AsyncArray out = netAsync.forwardAsync();
  558. ASSERT_TRUE(out.valid());
  559. Mat result;
  560. EXPECT_TRUE(out.get(result, async_timeout));
  561. normAssert(refs[i], result, format("Index: %d", i).c_str(), l1, lInf);
  562. }
  563. }
  564. INSTANTIATE_TEST_CASE_P(/**/, Test_Darknet_nets_async, Combine(
  565. Values("yolo-voc", "tiny-yolo-voc", "yolov3", "yolov4", "yolov4-tiny-2020-12"),
  566. dnnBackendsAndTargets()
  567. ));
  568. #endif
  569. TEST_P(Test_Darknet_nets, YOLOv3)
  570. {
  571. applyTestTag(
  572. CV_TEST_TAG_LONG,
  573. CV_TEST_TAG_MEMORY_2GB,
  574. CV_TEST_TAG_DEBUG_VERYLONG
  575. );
  576. #if defined(INF_ENGINE_RELEASE) && INF_ENGINE_VER_MAJOR_EQ(2020040000) // nGraph compilation failure
  577. if (backend == DNN_BACKEND_INFERENCE_ENGINE_NGRAPH && target == DNN_TARGET_OPENCL)
  578. applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_OPENCL, CV_TEST_TAG_DNN_SKIP_IE_VERSION);
  579. if (backend == DNN_BACKEND_INFERENCE_ENGINE_NGRAPH && target == DNN_TARGET_OPENCL_FP16)
  580. applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_OPENCL_FP16, CV_TEST_TAG_DNN_SKIP_IE_VERSION);
  581. #endif
  582. if (backend == DNN_BACKEND_INFERENCE_ENGINE_NGRAPH && target == DNN_TARGET_MYRIAD)
  583. applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_MYRIAD, CV_TEST_TAG_DNN_SKIP_IE_NGRAPH);
  584. // batchId, classId, confidence, left, top, right, bottom
  585. const int N0 = 3;
  586. const int N1 = 6;
  587. static const float ref_[/* (N0 + N1) * 7 */] = {
  588. 0, 16, 0.998836f, 0.160024f, 0.389964f, 0.417885f, 0.943716f,
  589. 0, 1, 0.987908f, 0.150913f, 0.221933f, 0.742255f, 0.746261f,
  590. 0, 7, 0.952983f, 0.614621f, 0.150257f, 0.901368f, 0.289251f,
  591. 1, 2, 0.997412f, 0.647584f, 0.459939f, 0.821037f, 0.663947f,
  592. 1, 2, 0.989633f, 0.450719f, 0.463353f, 0.496306f, 0.522258f,
  593. 1, 0, 0.980053f, 0.195856f, 0.378454f, 0.258626f, 0.629257f,
  594. 1, 9, 0.785341f, 0.665503f, 0.373543f, 0.688893f, 0.439244f,
  595. 1, 9, 0.733275f, 0.376029f, 0.315694f, 0.401776f, 0.395165f,
  596. 1, 9, 0.384815f, 0.659824f, 0.372389f, 0.673927f, 0.429412f,
  597. };
  598. Mat ref(N0 + N1, 7, CV_32FC1, (void*)ref_);
  599. double scoreDiff = 8e-5, iouDiff = 3e-4;
  600. if (target == DNN_TARGET_OPENCL_FP16 || target == DNN_TARGET_MYRIAD || target == DNN_TARGET_CPU_FP16)
  601. {
  602. #if defined(INF_ENGINE_RELEASE) && INF_ENGINE_VER_MAJOR_GE(2022010000)
  603. if (backend == DNN_BACKEND_INFERENCE_ENGINE_NGRAPH)
  604. scoreDiff = 0.009;
  605. else
  606. #endif
  607. scoreDiff = 0.006;
  608. iouDiff = 0.042;
  609. }
  610. else if (target == DNN_TARGET_CUDA_FP16)
  611. {
  612. scoreDiff = 0.04;
  613. iouDiff = 0.03;
  614. }
  615. std::string config_file = "yolov3.cfg";
  616. std::string weights_file = "yolov3.weights";
  617. #if defined(INF_ENGINE_RELEASE)
  618. if ((backend == DNN_BACKEND_INFERENCE_ENGINE_NN_BUILDER_2019 ||
  619. backend == DNN_BACKEND_INFERENCE_ENGINE_NGRAPH) && target == DNN_TARGET_MYRIAD &&
  620. getInferenceEngineVPUType() == CV_DNN_INFERENCE_ENGINE_VPU_TYPE_MYRIAD_X)
  621. {
  622. scoreDiff = 0.04;
  623. iouDiff = 0.2;
  624. }
  625. #endif
  626. {
  627. SCOPED_TRACE("batch size 1");
  628. testDarknetModel(config_file, weights_file, ref.rowRange(0, N0), scoreDiff, iouDiff, 0.24, 0.4, false);
  629. }
  630. #if defined(INF_ENGINE_RELEASE)
  631. if (backend == DNN_BACKEND_INFERENCE_ENGINE_NN_BUILDER_2019)
  632. {
  633. if (target == DNN_TARGET_OPENCL)
  634. applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_OPENCL, CV_TEST_TAG_DNN_SKIP_IE_VERSION);
  635. else if (target == DNN_TARGET_OPENCL_FP16 && INF_ENGINE_VER_MAJOR_LE(202010000))
  636. applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_OPENCL_FP16, CV_TEST_TAG_DNN_SKIP_IE_VERSION);
  637. else if (target == DNN_TARGET_MYRIAD &&
  638. getInferenceEngineVPUType() == CV_DNN_INFERENCE_ENGINE_VPU_TYPE_MYRIAD_X)
  639. applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_MYRIAD_X);
  640. }
  641. #endif
  642. {
  643. SCOPED_TRACE("batch size 2");
  644. testDarknetModel(config_file, weights_file, ref, scoreDiff, iouDiff, 0.24, 0.4, false);
  645. }
  646. }
  647. TEST_P(Test_Darknet_nets, YOLOv4)
  648. {
  649. applyTestTag(
  650. CV_TEST_TAG_LONG,
  651. CV_TEST_TAG_MEMORY_2GB,
  652. CV_TEST_TAG_DEBUG_VERYLONG
  653. );
  654. #if defined(INF_ENGINE_RELEASE) && INF_ENGINE_VER_MAJOR_EQ(2020040000) // nGraph compilation failure
  655. if (backend == DNN_BACKEND_INFERENCE_ENGINE_NGRAPH && target == DNN_TARGET_OPENCL)
  656. applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_OPENCL, CV_TEST_TAG_DNN_SKIP_IE_VERSION);
  657. if (backend == DNN_BACKEND_INFERENCE_ENGINE_NGRAPH && target == DNN_TARGET_OPENCL_FP16)
  658. applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_OPENCL_FP16, CV_TEST_TAG_DNN_SKIP_IE_VERSION);
  659. #endif
  660. #if defined(INF_ENGINE_RELEASE) && INF_ENGINE_VER_MAJOR_LT(2022010000)
  661. if (target == DNN_TARGET_MYRIAD) // NC_OUT_OF_MEMORY
  662. applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_MYRIAD, CV_TEST_TAG_DNN_SKIP_IE_VERSION);
  663. #endif
  664. // batchId, classId, confidence, left, top, right, bottom
  665. const int N0 = 3;
  666. const int N1 = 7;
  667. static const float ref_[/* (N0 + N1) * 7 */] = {
  668. 0, 16, 0.992194f, 0.172375f, 0.402458f, 0.403918f, 0.932801f,
  669. 0, 1, 0.988326f, 0.166708f, 0.228236f, 0.737208f, 0.735803f,
  670. 0, 7, 0.94639f, 0.602523f, 0.130399f, 0.901623f, 0.298452f,
  671. 1, 2, 0.99761f, 0.646556f, 0.45985f, 0.816041f, 0.659067f,
  672. 1, 0, 0.988913f, 0.201726f, 0.360282f, 0.266181f, 0.631728f,
  673. 1, 2, 0.98233f, 0.452007f, 0.462217f, 0.495612f, 0.521687f,
  674. 1, 9, 0.919195f, 0.374642f, 0.316524f, 0.398126f, 0.393714f,
  675. 1, 9, 0.856303f, 0.666842f, 0.372215f, 0.685539f, 0.44141f,
  676. 1, 9, 0.313516f, 0.656791f, 0.374734f, 0.671959f, 0.438371f,
  677. 1, 9, 0.256625f, 0.940232f, 0.326931f, 0.967586f, 0.374002f,
  678. };
  679. Mat ref(N0 + N1, 7, CV_32FC1, (void*)ref_);
  680. double scoreDiff = (target == DNN_TARGET_OPENCL_FP16 || target == DNN_TARGET_MYRIAD || target == DNN_TARGET_CPU_FP16) ? 0.006 : 8e-5;
  681. double iouDiff = (target == DNN_TARGET_OPENCL_FP16 || target == DNN_TARGET_MYRIAD || target == DNN_TARGET_CPU_FP16) ? 0.042 : 3e-4;
  682. if (target == DNN_TARGET_CUDA_FP16)
  683. {
  684. scoreDiff = 0.008;
  685. iouDiff = 0.03;
  686. }
  687. std::string config_file = "yolov4.cfg";
  688. std::string weights_file = "yolov4.weights";
  689. #if defined(INF_ENGINE_RELEASE) && INF_ENGINE_VER_MAJOR_EQ(2022010000)
  690. // accuracy (batch 1): no detections
  691. if (backend == DNN_BACKEND_INFERENCE_ENGINE_NGRAPH && target == DNN_TARGET_OPENCL_FP16)
  692. {
  693. iouDiff = std::numeric_limits<double>::quiet_NaN();
  694. }
  695. // accuracy
  696. if (backend == DNN_BACKEND_INFERENCE_ENGINE_NGRAPH && target == DNN_TARGET_MYRIAD)
  697. {
  698. iouDiff = std::numeric_limits<double>::quiet_NaN();
  699. }
  700. #elif defined(INF_ENGINE_RELEASE) && INF_ENGINE_VER_MAJOR_EQ(2021040000)
  701. // accuracy (batch 1)
  702. if (backend == DNN_BACKEND_INFERENCE_ENGINE_NGRAPH && target == DNN_TARGET_OPENCL_FP16)
  703. {
  704. iouDiff = std::numeric_limits<double>::quiet_NaN();
  705. }
  706. #elif defined(INF_ENGINE_RELEASE) && INF_ENGINE_VER_MAJOR_LT(2022010000)
  707. if ((backend == DNN_BACKEND_INFERENCE_ENGINE_NN_BUILDER_2019 ||
  708. backend == DNN_BACKEND_INFERENCE_ENGINE_NGRAPH) && target == DNN_TARGET_MYRIAD &&
  709. getInferenceEngineVPUType() == CV_DNN_INFERENCE_ENGINE_VPU_TYPE_MYRIAD_X)
  710. {
  711. scoreDiff = 0.04;
  712. iouDiff = 0.2;
  713. }
  714. #endif
  715. {
  716. SCOPED_TRACE("batch size 1");
  717. testDarknetModel(config_file, weights_file, ref.rowRange(0, N0), scoreDiff, iouDiff, 0.24, 0.4, false);
  718. // Test not equal width and height applying zero padding
  719. testDarknetModel(config_file, weights_file, ref.rowRange(0, N0), 0.006, 0.008, 0.24, 0.4, false, /*zeroPadW*/ 32);
  720. }
  721. {
  722. SCOPED_TRACE("batch size 2");
  723. #if defined(INF_ENGINE_RELEASE) && INF_ENGINE_VER_MAJOR_EQ(2022010000)
  724. // accuracy (batch 2)
  725. if (backend == DNN_BACKEND_INFERENCE_ENGINE_NGRAPH && target == DNN_TARGET_OPENCL_FP16)
  726. {
  727. scoreDiff = 0.008f;
  728. iouDiff = 0.05f;
  729. }
  730. // accuracy
  731. if (backend == DNN_BACKEND_INFERENCE_ENGINE_NGRAPH && target == DNN_TARGET_MYRIAD)
  732. {
  733. iouDiff = std::numeric_limits<double>::quiet_NaN();
  734. }
  735. #elif defined(INF_ENGINE_RELEASE) && INF_ENGINE_VER_MAJOR_EQ(2021040000)
  736. // accuracy (batch 2)
  737. if (backend == DNN_BACKEND_INFERENCE_ENGINE_NGRAPH && target == DNN_TARGET_OPENCL_FP16)
  738. {
  739. iouDiff = 0.45f;
  740. }
  741. #elif defined(INF_ENGINE_RELEASE) && INF_ENGINE_VER_MAJOR_LT(2022010000)
  742. if (backend == DNN_BACKEND_INFERENCE_ENGINE_NN_BUILDER_2019)
  743. {
  744. if (target == DNN_TARGET_OPENCL)
  745. applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_OPENCL, CV_TEST_TAG_DNN_SKIP_IE_VERSION);
  746. else if (target == DNN_TARGET_OPENCL_FP16 && INF_ENGINE_VER_MAJOR_LE(202010000))
  747. applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_OPENCL_FP16, CV_TEST_TAG_DNN_SKIP_IE_VERSION);
  748. else if (target == DNN_TARGET_MYRIAD &&
  749. getInferenceEngineVPUType() == CV_DNN_INFERENCE_ENGINE_VPU_TYPE_MYRIAD_X)
  750. applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_MYRIAD_X);
  751. }
  752. #endif
  753. testDarknetModel(config_file, weights_file, ref, scoreDiff, iouDiff, 0.24, 0.4, false);
  754. }
  755. #if defined(INF_ENGINE_RELEASE) && INF_ENGINE_VER_MAJOR_EQ(2022010000)
  756. // accuracy
  757. if (backend == DNN_BACKEND_INFERENCE_ENGINE_NGRAPH && target == DNN_TARGET_OPENCL_FP16)
  758. applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_OPENCL_FP16, CV_TEST_TAG_DNN_SKIP_IE_NGRAPH, CV_TEST_TAG_DNN_SKIP_IE_VERSION);
  759. // accuracy
  760. if (backend == DNN_BACKEND_INFERENCE_ENGINE_NGRAPH && target == DNN_TARGET_MYRIAD)
  761. applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_MYRIAD, CV_TEST_TAG_DNN_SKIP_IE_NGRAPH, CV_TEST_TAG_DNN_SKIP_IE_VERSION);
  762. #elif defined(INF_ENGINE_RELEASE) && INF_ENGINE_VER_MAJOR_EQ(2021040000)
  763. // accuracy
  764. if (backend == DNN_BACKEND_INFERENCE_ENGINE_NGRAPH && target == DNN_TARGET_OPENCL_FP16)
  765. applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_OPENCL_FP16, CV_TEST_TAG_DNN_SKIP_IE_NGRAPH, CV_TEST_TAG_DNN_SKIP_IE_VERSION);
  766. #endif
  767. }
  768. TEST_P(Test_Darknet_nets, YOLOv4_tiny)
  769. {
  770. applyTestTag(
  771. target == DNN_TARGET_CPU ? CV_TEST_TAG_MEMORY_512MB : CV_TEST_TAG_MEMORY_1GB
  772. );
  773. #if defined(INF_ENGINE_RELEASE) && INF_ENGINE_VER_MAJOR_GE(2021010000) // nGraph compilation failure
  774. if (target == DNN_TARGET_MYRIAD)
  775. applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_MYRIAD, CV_TEST_TAG_DNN_SKIP_IE_VERSION);
  776. #endif
  777. const double confThreshold = 0.5;
  778. // batchId, classId, confidence, left, top, right, bottom
  779. const int N0 = 3;
  780. const int N1 = 3;
  781. static const float ref_[/* (N0 + N1) * 7 */] = {
  782. 0, 16, 0.889883f, 0.177204f, 0.356279f, 0.417204f, 0.937517f,
  783. 0, 7, 0.816615f, 0.604293f, 0.137345f, 0.918016f, 0.295708f,
  784. 0, 1, 0.595912f, 0.0940107f, 0.178122f, 0.750619f, 0.829336f,
  785. 1, 2, 0.998224f, 0.652883f, 0.463477f, 0.813952f, 0.657163f,
  786. 1, 2, 0.967396f, 0.4539f, 0.466368f, 0.497716f, 0.520299f,
  787. 1, 0, 0.807866f, 0.205039f, 0.361842f, 0.260984f, 0.643621f,
  788. };
  789. Mat ref(N0 + N1, 7, CV_32FC1, (void*)ref_);
  790. double scoreDiff = 0.012f;
  791. double iouDiff = (target == DNN_TARGET_OPENCL_FP16 || target == DNN_TARGET_MYRIAD || target == DNN_TARGET_CPU_FP16) ? 0.15 : 0.01f;
  792. if (target == DNN_TARGET_CUDA_FP16)
  793. iouDiff = 0.02;
  794. std::string config_file = "yolov4-tiny-2020-12.cfg";
  795. std::string weights_file = "yolov4-tiny-2020-12.weights";
  796. #if defined(INF_ENGINE_RELEASE)
  797. if (target == DNN_TARGET_MYRIAD) // bad accuracy
  798. iouDiff = std::numeric_limits<double>::quiet_NaN();
  799. if (backend == DNN_BACKEND_INFERENCE_ENGINE_NN_BUILDER_2019 && target == DNN_TARGET_OPENCL)
  800. iouDiff = std::numeric_limits<double>::quiet_NaN();
  801. if ((backend == DNN_BACKEND_INFERENCE_ENGINE_NN_BUILDER_2019 ||
  802. backend == DNN_BACKEND_INFERENCE_ENGINE_NGRAPH) && target == DNN_TARGET_OPENCL_FP16)
  803. iouDiff = std::numeric_limits<double>::quiet_NaN();
  804. #endif
  805. {
  806. SCOPED_TRACE("batch size 1");
  807. testDarknetModel(config_file, weights_file, ref.rowRange(0, N0), scoreDiff, iouDiff, confThreshold, 0.4, false);
  808. }
  809. {
  810. SCOPED_TRACE("batch size 2");
  811. testDarknetModel(config_file, weights_file, ref, scoreDiff, iouDiff, confThreshold, 0.4, false);
  812. }
  813. #if defined(INF_ENGINE_RELEASE)
  814. if (target == DNN_TARGET_MYRIAD) // bad accuracy
  815. applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_MYRIAD, CV_TEST_TAG_DNN_SKIP_IE_VERSION);
  816. if (backend == DNN_BACKEND_INFERENCE_ENGINE_NN_BUILDER_2019 && target == DNN_TARGET_OPENCL)
  817. applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_OPENCL, CV_TEST_TAG_DNN_SKIP_IE_VERSION);
  818. if ((backend == DNN_BACKEND_INFERENCE_ENGINE_NN_BUILDER_2019 ||
  819. backend == DNN_BACKEND_INFERENCE_ENGINE_NGRAPH) && target == DNN_TARGET_OPENCL_FP16)
  820. applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_OPENCL_FP16, CV_TEST_TAG_DNN_SKIP_IE_VERSION);
  821. #endif
  822. }
  823. TEST_P(Test_Darknet_nets, YOLOv4x_mish)
  824. {
  825. applyTestTag(
  826. CV_TEST_TAG_MEMORY_2GB,
  827. CV_TEST_TAG_LONG,
  828. CV_TEST_TAG_DEBUG_VERYLONG
  829. );
  830. #if defined(INF_ENGINE_RELEASE) && INF_ENGINE_VER_MAJOR_EQ(2021040000)
  831. // IE exception: Ngraph operation Transpose with name permute_168 has dynamic output shape on 0 port, but CPU plug-in supports only static shape
  832. if (backend == DNN_BACKEND_INFERENCE_ENGINE_NGRAPH && (target == DNN_TARGET_OPENCL || target == DNN_TARGET_OPENCL_FP16))
  833. applyTestTag(target == DNN_TARGET_OPENCL ? CV_TEST_TAG_DNN_SKIP_IE_OPENCL : CV_TEST_TAG_DNN_SKIP_IE_OPENCL_FP16,
  834. CV_TEST_TAG_DNN_SKIP_IE_NGRAPH, CV_TEST_TAG_DNN_SKIP_IE_VERSION
  835. );
  836. #endif
  837. #if defined(INF_ENGINE_RELEASE) && INF_ENGINE_VER_MAJOR_EQ(2020040000) // nGraph compilation failure
  838. if (backend == DNN_BACKEND_INFERENCE_ENGINE_NGRAPH && target == DNN_TARGET_OPENCL)
  839. applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_OPENCL, CV_TEST_TAG_DNN_SKIP_IE_VERSION);
  840. if (backend == DNN_BACKEND_INFERENCE_ENGINE_NGRAPH && target == DNN_TARGET_OPENCL_FP16)
  841. applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_OPENCL_FP16, CV_TEST_TAG_DNN_SKIP_IE_VERSION);
  842. #endif
  843. #if defined(INF_ENGINE_RELEASE)
  844. if (target == DNN_TARGET_MYRIAD) // NC_OUT_OF_MEMORY
  845. applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_MYRIAD, CV_TEST_TAG_DNN_SKIP_IE_VERSION);
  846. #endif
  847. // batchId, classId, confidence, left, top, right, bottom
  848. const int N0 = 3;
  849. const int N1 = 5;
  850. static const float ref_[/* (N0 + N1) * 7 */] = {
  851. 0, 16, 0.925536f, 0.17188f, 0.386832f, 0.406138f, 0.941696f,
  852. 0, 1, 0.912028f, 0.162125f, 0.208863f, 0.741316f, 0.729332f,
  853. 0, 7, 0.841018f, 0.608953f, 0.128653f, 0.900692f, 0.295657f,
  854. 1, 2, 0.925697f, 0.650438f, 0.458118f, 0.813927f, 0.661775f,
  855. 1, 0, 0.882156f, 0.203644f, 0.365763f, 0.265473f, 0.632195f,
  856. 1, 2, 0.848857f, 0.451044f, 0.462997f, 0.496629f, 0.522719f,
  857. 1, 9, 0.736015f, 0.374503f, 0.316029f, 0.399358f, 0.392883f,
  858. 1, 9, 0.727129f, 0.662469f, 0.373687f, 0.687877f, 0.441335f,
  859. };
  860. Mat ref(N0 + N1, 7, CV_32FC1, (void*)ref_);
  861. double scoreDiff = 8e-5;
  862. double iouDiff = 3e-4;
  863. if (target == DNN_TARGET_OPENCL_FP16 || target == DNN_TARGET_MYRIAD || target == DNN_TARGET_CUDA_FP16 || target == DNN_TARGET_CPU_FP16)
  864. {
  865. scoreDiff = 0.006;
  866. iouDiff = 0.042;
  867. }
  868. std::string config_file = "yolov4x-mish.cfg";
  869. std::string weights_file = "yolov4x-mish.weights";
  870. #if defined(INF_ENGINE_RELEASE)
  871. if ((backend == DNN_BACKEND_INFERENCE_ENGINE_NN_BUILDER_2019 ||
  872. backend == DNN_BACKEND_INFERENCE_ENGINE_NGRAPH) && target == DNN_TARGET_MYRIAD &&
  873. getInferenceEngineVPUType() == CV_DNN_INFERENCE_ENGINE_VPU_TYPE_MYRIAD_X)
  874. {
  875. scoreDiff = 0.04;
  876. iouDiff = 0.2;
  877. }
  878. #endif
  879. {
  880. SCOPED_TRACE("batch size 1");
  881. testDarknetModel(config_file, weights_file, ref.rowRange(0, N0), scoreDiff, iouDiff, 0.24, 0.4, false);
  882. }
  883. {
  884. SCOPED_TRACE("batch size 2");
  885. #if defined(INF_ENGINE_RELEASE)
  886. if (backend == DNN_BACKEND_INFERENCE_ENGINE_NN_BUILDER_2019)
  887. {
  888. if (target == DNN_TARGET_OPENCL)
  889. applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_OPENCL, CV_TEST_TAG_DNN_SKIP_IE_VERSION);
  890. else if (target == DNN_TARGET_OPENCL_FP16 && INF_ENGINE_VER_MAJOR_LE(202010000))
  891. applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_OPENCL_FP16, CV_TEST_TAG_DNN_SKIP_IE_VERSION);
  892. else if (target == DNN_TARGET_MYRIAD &&
  893. getInferenceEngineVPUType() == CV_DNN_INFERENCE_ENGINE_VPU_TYPE_MYRIAD_X)
  894. applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_MYRIAD_X);
  895. }
  896. #endif
  897. testDarknetModel(config_file, weights_file, ref, scoreDiff, iouDiff, 0.24, 0.4, false);
  898. }
  899. }
  900. INSTANTIATE_TEST_CASE_P(/**/, Test_Darknet_nets, dnnBackendsAndTargets());
  901. TEST_P(Test_Darknet_layers, shortcut)
  902. {
  903. testDarknetLayer("shortcut");
  904. }
  905. TEST_P(Test_Darknet_layers, shortcut_leaky)
  906. {
  907. testDarknetLayer("shortcut_leaky");
  908. }
  909. TEST_P(Test_Darknet_layers, shortcut_unequal)
  910. {
  911. testDarknetLayer("shortcut_unequal");
  912. }
  913. TEST_P(Test_Darknet_layers, shortcut_unequal_2)
  914. {
  915. testDarknetLayer("shortcut_unequal_2");
  916. }
  917. TEST_P(Test_Darknet_layers, upsample)
  918. {
  919. #if defined(INF_ENGINE_RELEASE) && INF_ENGINE_VER_MAJOR_EQ(2021030000)
  920. if (backend == DNN_BACKEND_INFERENCE_ENGINE_NGRAPH && target == DNN_TARGET_MYRIAD)
  921. applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_MYRIAD, CV_TEST_TAG_DNN_SKIP_IE_NGRAPH); // exception
  922. #endif
  923. testDarknetLayer("upsample");
  924. }
  925. TEST_P(Test_Darknet_layers, mish)
  926. {
  927. testDarknetLayer("mish", true);
  928. }
  929. TEST_P(Test_Darknet_layers, tanh)
  930. {
  931. testDarknetLayer("tanh");
  932. }
  933. TEST_P(Test_Darknet_layers, avgpool_softmax)
  934. {
  935. testDarknetLayer("avgpool_softmax");
  936. }
  937. TEST_P(Test_Darknet_layers, crop)
  938. {
  939. testDarknetLayer("crop");
  940. }
  941. TEST_P(Test_Darknet_layers, region)
  942. {
  943. #if defined(INF_ENGINE_RELEASE) && INF_ENGINE_VER_MAJOR_LT(2021040000)
  944. if (backend == DNN_BACKEND_INFERENCE_ENGINE_NGRAPH && INF_ENGINE_VER_MAJOR_GE(2020020000))
  945. applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_NGRAPH, CV_TEST_TAG_DNN_SKIP_IE_VERSION);
  946. #endif
  947. #if defined(INF_ENGINE_RELEASE) && INF_ENGINE_VER_MAJOR_GE(2022010000)
  948. // accuracy on CPU, OpenCL
  949. // Expected: (normL1) <= (l1), actual: 0.000358148 vs 1e-05
  950. // |ref| = 1.207319974899292
  951. // Expected: (normInf) <= (lInf), actual: 0.763223 vs 0.0001
  952. // |ref| = 1.207319974899292
  953. if (backend == DNN_BACKEND_INFERENCE_ENGINE_NGRAPH && target == DNN_TARGET_CPU)
  954. applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_CPU, CV_TEST_TAG_DNN_SKIP_IE_NGRAPH, CV_TEST_TAG_DNN_SKIP_IE_VERSION);
  955. if (backend == DNN_BACKEND_INFERENCE_ENGINE_NGRAPH && (target == DNN_TARGET_OPENCL || target == DNN_TARGET_OPENCL_FP16))
  956. applyTestTag(target == DNN_TARGET_OPENCL ? CV_TEST_TAG_DNN_SKIP_IE_OPENCL : CV_TEST_TAG_DNN_SKIP_IE_OPENCL_FP16,
  957. CV_TEST_TAG_DNN_SKIP_IE_NGRAPH, CV_TEST_TAG_DNN_SKIP_IE_VERSION
  958. );
  959. #elif defined(INF_ENGINE_RELEASE) && INF_ENGINE_VER_MAJOR_EQ(2021040000)
  960. // accuracy on CPU, OpenCL
  961. // Expected: (normInf) <= (lInf), actual: 0.763223 vs 0.0001
  962. // |ref| = 1.207319974899292
  963. if (backend == DNN_BACKEND_INFERENCE_ENGINE_NGRAPH && target == DNN_TARGET_CPU)
  964. applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_CPU, CV_TEST_TAG_DNN_SKIP_IE_NGRAPH, CV_TEST_TAG_DNN_SKIP_IE_VERSION);
  965. if (backend == DNN_BACKEND_INFERENCE_ENGINE_NGRAPH && (target == DNN_TARGET_OPENCL || target == DNN_TARGET_OPENCL_FP16))
  966. applyTestTag(target == DNN_TARGET_OPENCL ? CV_TEST_TAG_DNN_SKIP_IE_OPENCL : CV_TEST_TAG_DNN_SKIP_IE_OPENCL_FP16,
  967. CV_TEST_TAG_DNN_SKIP_IE_NGRAPH, CV_TEST_TAG_DNN_SKIP_IE_VERSION
  968. );
  969. #endif
  970. testDarknetLayer("region");
  971. }
  972. TEST_P(Test_Darknet_layers, reorg)
  973. {
  974. testDarknetLayer("reorg");
  975. }
  976. TEST_P(Test_Darknet_layers, route)
  977. {
  978. testDarknetLayer("route");
  979. testDarknetLayer("route_multi");
  980. }
  981. TEST_P(Test_Darknet_layers, maxpool)
  982. {
  983. #if defined(INF_ENGINE_RELEASE) && INF_ENGINE_VER_MAJOR_GE(2020020000)
  984. if (backend == DNN_BACKEND_INFERENCE_ENGINE_NGRAPH && target == DNN_TARGET_MYRIAD)
  985. applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_MYRIAD, CV_TEST_TAG_DNN_SKIP_IE_NGRAPH, CV_TEST_TAG_DNN_SKIP_IE_VERSION);
  986. #endif
  987. testDarknetLayer("maxpool");
  988. }
  989. TEST_P(Test_Darknet_layers, convolutional)
  990. {
  991. #if defined(INF_ENGINE_RELEASE)
  992. if (target == DNN_TARGET_MYRIAD)
  993. {
  994. default_l1 = 0.01f;
  995. }
  996. #endif
  997. testDarknetLayer("convolutional", true);
  998. }
  999. TEST_P(Test_Darknet_layers, scale_channels)
  1000. {
  1001. bool testBatches = backend == DNN_BACKEND_CUDA;
  1002. testDarknetLayer("scale_channels", false, testBatches);
  1003. }
  1004. TEST_P(Test_Darknet_layers, connected)
  1005. {
  1006. if (backend == DNN_BACKEND_OPENCV && target == DNN_TARGET_OPENCL_FP16)
  1007. applyTestTag(CV_TEST_TAG_DNN_SKIP_OPENCL_FP16);
  1008. if (backend == DNN_BACKEND_OPENCV && target == DNN_TARGET_CPU_FP16)
  1009. applyTestTag(CV_TEST_TAG_DNN_SKIP_CPU_FP16);
  1010. double l1 = 0.0;
  1011. if (backend == DNN_BACKEND_INFERENCE_ENGINE_NGRAPH && target == DNN_TARGET_OPENCL)
  1012. {
  1013. l1 = 3e-5;
  1014. }
  1015. testDarknetLayer("connected", true, true, l1);
  1016. }
  1017. TEST_P(Test_Darknet_layers, relu)
  1018. {
  1019. if (backend == DNN_BACKEND_INFERENCE_ENGINE_NN_BUILDER_2019 && target == DNN_TARGET_MYRIAD)
  1020. applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_MYRIAD);
  1021. testDarknetLayer("relu");
  1022. }
  1023. TEST_P(Test_Darknet_layers, sam)
  1024. {
  1025. testDarknetLayer("sam", true);
  1026. }
  1027. INSTANTIATE_TEST_CASE_P(/**/, Test_Darknet_layers, dnnBackendsAndTargets());
  1028. }} // namespace