test_backends.cpp 61 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594
  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. //
  5. // Copyright (C) 2018-2019, Intel Corporation, all rights reserved.
  6. // Third party copyrights are property of their respective owners.
  7. #include "test_precomp.hpp"
  8. #include "opencv2/core/ocl.hpp"
  9. namespace opencv_test { namespace {
  10. class DNNTestNetwork : public DNNTestLayer
  11. {
  12. public:
  13. void processNet(const std::string& weights, const std::string& proto,
  14. Size inpSize, const std::string& outputLayer = "",
  15. const std::string& halideScheduler = "",
  16. double l1 = 0.0, double lInf = 0.0)
  17. {
  18. // Create a common input blob.
  19. int blobSize[] = {1, 3, inpSize.height, inpSize.width};
  20. Mat inp(4, blobSize, CV_32FC1);
  21. randu(inp, 0.0f, 1.0f);
  22. processNet(weights, proto, inp, outputLayer, halideScheduler, l1, lInf);
  23. }
  24. void processNet(std::string weights, std::string proto,
  25. Mat inp, const std::string& outputLayer = "",
  26. std::string halideScheduler = "",
  27. double l1 = 0.0, double lInf = 0.0, double detectionConfThresh = 0.2, bool useWinograd = true)
  28. {
  29. checkBackend();
  30. l1 = l1 ? l1 : default_l1;
  31. lInf = lInf ? lInf : default_lInf;
  32. weights = findDataFile(weights, false);
  33. if (!proto.empty())
  34. proto = findDataFile(proto);
  35. // Create two networks - with default backend and target and a tested one.
  36. Net netDefault = readNet(weights, proto);
  37. netDefault.setPreferableBackend(DNN_BACKEND_OPENCV);
  38. netDefault.setInput(inp);
  39. Mat outDefault = netDefault.forward(outputLayer).clone();
  40. net = readNet(weights, proto);
  41. net.setInput(inp);
  42. net.setPreferableBackend(backend);
  43. net.setPreferableTarget(target);
  44. if (target == DNN_TARGET_CPU_FP16)
  45. net.enableWinograd(false);
  46. if (backend == DNN_BACKEND_HALIDE && !halideScheduler.empty())
  47. {
  48. halideScheduler = findDataFile(halideScheduler);
  49. net.setHalideScheduler(halideScheduler);
  50. }
  51. Mat out = net.forward(outputLayer).clone();
  52. check(outDefault, out, outputLayer, l1, lInf, detectionConfThresh, "First run");
  53. // Test 2: change input.
  54. float* inpData = (float*)inp.data;
  55. for (int i = 0; i < inp.size[0] * inp.size[1]; ++i)
  56. {
  57. Mat slice(inp.size[2], inp.size[3], CV_32F, inpData);
  58. cv::flip(slice, slice, 1);
  59. inpData += slice.total();
  60. }
  61. netDefault.setInput(inp);
  62. net.setInput(inp);
  63. outDefault = netDefault.forward(outputLayer).clone();
  64. out = net.forward(outputLayer).clone();
  65. check(outDefault, out, outputLayer, l1, lInf, detectionConfThresh, "Second run");
  66. }
  67. void check(Mat& ref, Mat& out, const std::string& outputLayer, double l1, double lInf,
  68. double detectionConfThresh, const char* msg)
  69. {
  70. if (outputLayer == "detection_out")
  71. {
  72. if (backend == DNN_BACKEND_INFERENCE_ENGINE_NN_BUILDER_2019)
  73. {
  74. // Inference Engine produces detections terminated by a row which starts from -1.
  75. out = out.reshape(1, out.total() / 7);
  76. int numDetections = 0;
  77. while (numDetections < out.rows && out.at<float>(numDetections, 0) != -1)
  78. {
  79. numDetections += 1;
  80. }
  81. out = out.rowRange(0, numDetections);
  82. }
  83. normAssertDetections(ref, out, msg, detectionConfThresh, l1, lInf);
  84. }
  85. else
  86. normAssert(ref, out, msg, l1, lInf);
  87. }
  88. Net net;
  89. };
  90. TEST_P(DNNTestNetwork, DISABLED_YOLOv8n) {
  91. processNet("dnn/onnx/models/yolov8n.onnx", "", Size(640, 640), "output0");
  92. expectNoFallbacksFromIE(net);
  93. expectNoFallbacksFromCUDA(net);
  94. }
  95. TEST_P(DNNTestNetwork, AlexNet)
  96. {
  97. applyTestTag(CV_TEST_TAG_MEMORY_1GB);
  98. processNet("dnn/bvlc_alexnet.caffemodel", "dnn/bvlc_alexnet.prototxt",
  99. Size(227, 227), "prob",
  100. target == DNN_TARGET_OPENCL ? "dnn/halide_scheduler_opencl_alexnet.yml" :
  101. "dnn/halide_scheduler_alexnet.yml");
  102. expectNoFallbacksFromIE(net);
  103. expectNoFallbacksFromCUDA(net);
  104. }
  105. TEST_P(DNNTestNetwork, ResNet_50)
  106. {
  107. applyTestTag(
  108. (target == DNN_TARGET_CPU ? CV_TEST_TAG_MEMORY_512MB : CV_TEST_TAG_MEMORY_1GB),
  109. CV_TEST_TAG_DEBUG_VERYLONG
  110. );
  111. processNet("dnn/ResNet-50-model.caffemodel", "dnn/ResNet-50-deploy.prototxt",
  112. Size(224, 224), "prob",
  113. target == DNN_TARGET_OPENCL ? "dnn/halide_scheduler_opencl_resnet_50.yml" :
  114. "dnn/halide_scheduler_resnet_50.yml");
  115. expectNoFallbacksFromIE(net);
  116. expectNoFallbacksFromCUDA(net);
  117. }
  118. TEST_P(DNNTestNetwork, SqueezeNet_v1_1)
  119. {
  120. processNet("dnn/squeezenet_v1.1.caffemodel", "dnn/squeezenet_v1.1.prototxt",
  121. Size(227, 227), "prob",
  122. target == DNN_TARGET_OPENCL ? "dnn/halide_scheduler_opencl_squeezenet_v1_1.yml" :
  123. "dnn/halide_scheduler_squeezenet_v1_1.yml");
  124. expectNoFallbacksFromIE(net);
  125. expectNoFallbacksFromCUDA(net);
  126. }
  127. TEST_P(DNNTestNetwork, GoogLeNet)
  128. {
  129. applyTestTag(target == DNN_TARGET_CPU ? "" : CV_TEST_TAG_MEMORY_512MB);
  130. processNet("dnn/bvlc_googlenet.caffemodel", "dnn/bvlc_googlenet.prototxt",
  131. Size(224, 224), "prob");
  132. expectNoFallbacksFromIE(net);
  133. expectNoFallbacksFromCUDA(net);
  134. }
  135. TEST_P(DNNTestNetwork, Inception_5h)
  136. {
  137. applyTestTag(CV_TEST_TAG_MEMORY_512MB);
  138. double l1 = default_l1, lInf = default_lInf;
  139. if (backend == DNN_BACKEND_INFERENCE_ENGINE_NN_BUILDER_2019 && (target == DNN_TARGET_CPU || target == DNN_TARGET_OPENCL))
  140. {
  141. l1 = 1.72e-5;
  142. lInf = 8e-4;
  143. }
  144. processNet("dnn/tensorflow_inception_graph.pb", "", Size(224, 224), "softmax2",
  145. target == DNN_TARGET_OPENCL ? "dnn/halide_scheduler_opencl_inception_5h.yml" :
  146. "dnn/halide_scheduler_inception_5h.yml",
  147. l1, lInf);
  148. expectNoFallbacksFromIE(net);
  149. expectNoFallbacksFromCUDA(net);
  150. }
  151. TEST_P(DNNTestNetwork, ENet)
  152. {
  153. applyTestTag(target == DNN_TARGET_CPU ? "" : CV_TEST_TAG_MEMORY_512MB);
  154. #if defined(INF_ENGINE_RELEASE) && INF_ENGINE_VER_MAJOR_LT(2023000000)
  155. if (backend == DNN_BACKEND_INFERENCE_ENGINE_NN_BUILDER_2019)
  156. applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_NN_BUILDER);
  157. if (backend == DNN_BACKEND_INFERENCE_ENGINE_NGRAPH)
  158. applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_NGRAPH);
  159. #endif
  160. if (backend == DNN_BACKEND_OPENCV && target == DNN_TARGET_OPENCL_FP16)
  161. applyTestTag(CV_TEST_TAG_DNN_SKIP_OPENCL_FP16);
  162. if (backend == DNN_BACKEND_CUDA && target == DNN_TARGET_CUDA_FP16)
  163. applyTestTag(CV_TEST_TAG_DNN_SKIP_CUDA_FP16);
  164. if (backend == DNN_BACKEND_OPENCV && target == DNN_TARGET_CPU_FP16)
  165. applyTestTag(CV_TEST_TAG_DNN_SKIP_CPU_FP16);
  166. processNet("dnn/Enet-model-best.net", "", Size(512, 512), "l367_Deconvolution",
  167. target == DNN_TARGET_OPENCL ? "dnn/halide_scheduler_opencl_enet.yml" :
  168. "dnn/halide_scheduler_enet.yml",
  169. 2e-5, 0.15);
  170. expectNoFallbacksFromCUDA(net);
  171. }
  172. TEST_P(DNNTestNetwork, MobileNet_SSD_Caffe)
  173. {
  174. applyTestTag(CV_TEST_TAG_MEMORY_512MB);
  175. if (backend == DNN_BACKEND_HALIDE)
  176. applyTestTag(CV_TEST_TAG_DNN_SKIP_HALIDE);
  177. Mat sample = imread(findDataFile("dnn/street.png"));
  178. Mat inp = blobFromImage(sample, 1.0f / 127.5, Size(300, 300), Scalar(127.5, 127.5, 127.5), false);
  179. float scoreDiff = (target == DNN_TARGET_OPENCL_FP16 || target == DNN_TARGET_MYRIAD || target == DNN_TARGET_CPU_FP16) ? 1.5e-2 : 0.0;
  180. float iouDiff = (target == DNN_TARGET_MYRIAD) ? 0.063 : 0.0;
  181. float detectionConfThresh = (target == DNN_TARGET_MYRIAD) ? 0.262 : FLT_MIN;
  182. processNet("dnn/MobileNetSSD_deploy_19e3ec3.caffemodel", "dnn/MobileNetSSD_deploy_19e3ec3.prototxt",
  183. inp, "detection_out", "", scoreDiff, iouDiff, detectionConfThresh);
  184. expectNoFallbacksFromIE(net);
  185. }
  186. TEST_P(DNNTestNetwork, MobileNet_SSD_Caffe_Different_Width_Height)
  187. {
  188. if (backend == DNN_BACKEND_HALIDE)
  189. applyTestTag(CV_TEST_TAG_DNN_SKIP_HALIDE);
  190. #if defined(INF_ENGINE_RELEASE) && INF_ENGINE_VER_MAJOR_EQ(2022010000)
  191. // May hang on some configurations
  192. if (backend == DNN_BACKEND_INFERENCE_ENGINE_NGRAPH && (target == DNN_TARGET_OPENCL || target == DNN_TARGET_OPENCL_FP16))
  193. applyTestTag(target == DNN_TARGET_OPENCL ? CV_TEST_TAG_DNN_SKIP_IE_OPENCL : CV_TEST_TAG_DNN_SKIP_IE_OPENCL_FP16,
  194. CV_TEST_TAG_DNN_SKIP_IE_NGRAPH, CV_TEST_TAG_DNN_SKIP_IE_VERSION
  195. );
  196. #elif defined(INF_ENGINE_RELEASE) && INF_ENGINE_VER_MAJOR_EQ(2021040000)
  197. // IE exception: Ngraph operation Transpose with name conv15_2_mbox_conf_perm has dynamic output shape on 0 port, but CPU plug-in supports only static shape
  198. if (backend == DNN_BACKEND_INFERENCE_ENGINE_NGRAPH && (target == DNN_TARGET_OPENCL || target == DNN_TARGET_OPENCL_FP16))
  199. applyTestTag(target == DNN_TARGET_OPENCL ? CV_TEST_TAG_DNN_SKIP_IE_OPENCL : CV_TEST_TAG_DNN_SKIP_IE_OPENCL_FP16,
  200. CV_TEST_TAG_DNN_SKIP_IE_NGRAPH, CV_TEST_TAG_DNN_SKIP_IE_VERSION
  201. );
  202. if ((backend == DNN_BACKEND_INFERENCE_ENGINE_NN_BUILDER_2019 || backend == DNN_BACKEND_INFERENCE_ENGINE_NGRAPH) &&
  203. target == DNN_TARGET_MYRIAD && getInferenceEngineVPUType() == CV_DNN_INFERENCE_ENGINE_VPU_TYPE_MYRIAD_X)
  204. applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_MYRIAD_X, CV_TEST_TAG_DNN_SKIP_IE_NGRAPH, CV_TEST_TAG_DNN_SKIP_IE_VERSION);
  205. #elif defined(INF_ENGINE_RELEASE)
  206. if ((backend == DNN_BACKEND_INFERENCE_ENGINE_NN_BUILDER_2019 || backend == DNN_BACKEND_INFERENCE_ENGINE_NGRAPH) &&
  207. target == DNN_TARGET_MYRIAD && getInferenceEngineVPUType() == CV_DNN_INFERENCE_ENGINE_VPU_TYPE_MYRIAD_X)
  208. applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_MYRIAD_X, CV_TEST_TAG_DNN_SKIP_IE_NGRAPH, CV_TEST_TAG_DNN_SKIP_IE_VERSION);
  209. #endif
  210. Mat sample = imread(findDataFile("dnn/street.png"));
  211. Mat inp = blobFromImage(sample, 1.0f / 127.5, Size(300, 560), Scalar(127.5, 127.5, 127.5), false);
  212. float scoreDiff = 0.0, iouDiff = 0.0;
  213. if (target == DNN_TARGET_OPENCL_FP16 || target == DNN_TARGET_MYRIAD || target == DNN_TARGET_CPU_FP16)
  214. {
  215. scoreDiff = 0.029;
  216. iouDiff = 0.09;
  217. }
  218. else if (target == DNN_TARGET_CUDA_FP16)
  219. {
  220. scoreDiff = 0.03;
  221. iouDiff = 0.08;
  222. }
  223. processNet("dnn/MobileNetSSD_deploy_19e3ec3.caffemodel", "dnn/MobileNetSSD_deploy_19e3ec3.prototxt",
  224. inp, "detection_out", "", scoreDiff, iouDiff);
  225. expectNoFallbacksFromIE(net);
  226. }
  227. TEST_P(DNNTestNetwork, MobileNet_SSD_v1_TensorFlow)
  228. {
  229. applyTestTag((target == DNN_TARGET_CPU || target == DNN_TARGET_CPU_FP16) ? "" : CV_TEST_TAG_MEMORY_512MB);
  230. if (backend == DNN_BACKEND_HALIDE)
  231. applyTestTag(CV_TEST_TAG_DNN_SKIP_HALIDE);
  232. Mat sample = imread(findDataFile("dnn/street.png"));
  233. Mat inp = blobFromImage(sample, 1.0f, Size(300, 300), Scalar(), false);
  234. float detectionConfThresh = (target == DNN_TARGET_MYRIAD) ? 0.216 : 0.2;
  235. float scoreDiff = 0.0, iouDiff = 0.0;
  236. if (target == DNN_TARGET_OPENCL_FP16 || target == DNN_TARGET_MYRIAD || target == DNN_TARGET_CPU_FP16)
  237. {
  238. scoreDiff = 0.095;
  239. iouDiff = 0.09;
  240. }
  241. else if (target == DNN_TARGET_CUDA_FP16)
  242. {
  243. scoreDiff = 0.007;
  244. iouDiff = 0.08;
  245. }
  246. processNet("dnn/ssd_mobilenet_v1_coco_2017_11_17.pb", "dnn/ssd_mobilenet_v1_coco_2017_11_17.pbtxt",
  247. inp, "detection_out", "", scoreDiff, iouDiff, detectionConfThresh);
  248. expectNoFallbacksFromIE(net);
  249. }
  250. TEST_P(DNNTestNetwork, MobileNet_SSD_v1_TensorFlow_Different_Width_Height)
  251. {
  252. if (backend == DNN_BACKEND_HALIDE)
  253. applyTestTag(CV_TEST_TAG_DNN_SKIP_HALIDE);
  254. #if defined(INF_ENGINE_RELEASE) && INF_ENGINE_VER_MAJOR_LT(2021040000)
  255. if ((backend == DNN_BACKEND_INFERENCE_ENGINE_NN_BUILDER_2019 || backend == DNN_BACKEND_INFERENCE_ENGINE_NGRAPH) &&
  256. target == DNN_TARGET_MYRIAD && getInferenceEngineVPUType() == CV_DNN_INFERENCE_ENGINE_VPU_TYPE_MYRIAD_X)
  257. applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_MYRIAD_X);
  258. #endif
  259. #if defined(INF_ENGINE_RELEASE) && INF_ENGINE_VER_MAJOR_EQ(2019020000)
  260. if (backend == DNN_BACKEND_INFERENCE_ENGINE_NN_BUILDER_2019 && target == DNN_TARGET_MYRIAD)
  261. applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_MYRIAD, CV_TEST_TAG_DNN_SKIP_IE_NN_BUILDER, CV_TEST_TAG_DNN_SKIP_IE_VERSION);
  262. #endif
  263. Mat sample = imread(findDataFile("dnn/street.png"));
  264. Mat inp = blobFromImage(sample, 1.0f, Size(300, 560), Scalar(), false);
  265. float scoreDiff = 0.0, iouDiff = 0.0;
  266. if (target == DNN_TARGET_OPENCL_FP16 || target == DNN_TARGET_MYRIAD || target == DNN_TARGET_CPU_FP16)
  267. {
  268. scoreDiff = 0.013;
  269. iouDiff = 0.06;
  270. }
  271. else if (target == DNN_TARGET_CUDA_FP16)
  272. {
  273. scoreDiff = 0.007;
  274. iouDiff = 0.06;
  275. }
  276. processNet("dnn/ssd_mobilenet_v1_coco_2017_11_17.pb", "dnn/ssd_mobilenet_v1_coco_2017_11_17.pbtxt",
  277. inp, "detection_out", "", scoreDiff, iouDiff);
  278. expectNoFallbacksFromIE(net);
  279. }
  280. TEST_P(DNNTestNetwork, MobileNet_SSD_v2_TensorFlow)
  281. {
  282. applyTestTag(target == DNN_TARGET_CPU ? CV_TEST_TAG_MEMORY_512MB : CV_TEST_TAG_MEMORY_1GB);
  283. if (backend == DNN_BACKEND_HALIDE)
  284. applyTestTag(CV_TEST_TAG_DNN_SKIP_HALIDE);
  285. Mat sample = imread(findDataFile("dnn/street.png"));
  286. Mat inp = blobFromImage(sample, 1.0f, Size(300, 300), Scalar(), false);
  287. float scoreDiff = 2e-5, iouDiff = 0.0;
  288. if (target == DNN_TARGET_OPENCL_FP16 || target == DNN_TARGET_MYRIAD || target == DNN_TARGET_CPU_FP16)
  289. {
  290. scoreDiff = 0.013;
  291. iouDiff = 0.062;
  292. }
  293. else if (target == DNN_TARGET_CUDA_FP16)
  294. {
  295. scoreDiff = 0.02;
  296. iouDiff = 0.07;
  297. }
  298. processNet("dnn/ssd_mobilenet_v2_coco_2018_03_29.pb", "dnn/ssd_mobilenet_v2_coco_2018_03_29.pbtxt",
  299. inp, "detection_out", "", scoreDiff, iouDiff, 0.25);
  300. expectNoFallbacksFromIE(net);
  301. }
  302. TEST_P(DNNTestNetwork, SSD_VGG16)
  303. {
  304. applyTestTag(
  305. CV_TEST_TAG_MEMORY_2GB,
  306. CV_TEST_TAG_LONG,
  307. CV_TEST_TAG_DEBUG_VERYLONG
  308. );
  309. if (backend == DNN_BACKEND_HALIDE && target == DNN_TARGET_CPU)
  310. applyTestTag(CV_TEST_TAG_DNN_SKIP_HALIDE); // TODO HALIDE_CPU
  311. Mat sample = imread(findDataFile("dnn/street.png"));
  312. Mat inp = blobFromImage(sample, 1.0f, Size(300, 300), Scalar(), false);
  313. float scoreDiff = 0.0, iouDiff = 0.0;
  314. if (target == DNN_TARGET_OPENCL_FP16 || target == DNN_TARGET_CPU_FP16)
  315. {
  316. scoreDiff = 0.04;
  317. }
  318. else if (target == DNN_TARGET_MYRIAD)
  319. {
  320. scoreDiff = 0.0325;
  321. iouDiff = 0.032;
  322. }
  323. else if (target == DNN_TARGET_CUDA_FP16)
  324. {
  325. scoreDiff = 0.03;
  326. iouDiff = 0.13;
  327. }
  328. processNet("dnn/VGG_ILSVRC2016_SSD_300x300_iter_440000.caffemodel",
  329. "dnn/ssd_vgg16.prototxt", inp, "detection_out", "", scoreDiff,
  330. iouDiff, 0.2, false);
  331. expectNoFallbacksFromIE(net);
  332. }
  333. TEST_P(DNNTestNetwork, OpenPose_pose_coco)
  334. {
  335. applyTestTag(CV_TEST_TAG_LONG, (target == DNN_TARGET_CPU ? CV_TEST_TAG_MEMORY_1GB : CV_TEST_TAG_MEMORY_2GB),
  336. CV_TEST_TAG_DEBUG_LONG);
  337. if (backend == DNN_BACKEND_HALIDE)
  338. applyTestTag(CV_TEST_TAG_DNN_SKIP_HALIDE);
  339. #if defined(INF_ENGINE_RELEASE) && INF_ENGINE_VER_MAJOR_LE(2018050000)
  340. if (backend == DNN_BACKEND_INFERENCE_ENGINE_NN_BUILDER_2019 && target == DNN_TARGET_MYRIAD
  341. && getInferenceEngineVPUType() == CV_DNN_INFERENCE_ENGINE_VPU_TYPE_MYRIAD_X)
  342. applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_MYRIAD_X, CV_TEST_TAG_DNN_SKIP_IE_VERSION);
  343. #endif
  344. const float l1 = (target == DNN_TARGET_MYRIAD) ? 0.009 : 0.0;
  345. const float lInf = (target == DNN_TARGET_MYRIAD) ? 0.09 : 0.0;
  346. processNet("dnn/openpose_pose_coco.caffemodel", "dnn/openpose_pose_coco.prototxt",
  347. Size(46, 46), "", "", l1, lInf);
  348. expectNoFallbacksFromIE(net);
  349. expectNoFallbacksFromCUDA(net);
  350. }
  351. TEST_P(DNNTestNetwork, OpenPose_pose_mpi)
  352. {
  353. applyTestTag(CV_TEST_TAG_LONG, (target == DNN_TARGET_CPU ? CV_TEST_TAG_MEMORY_1GB : CV_TEST_TAG_MEMORY_2GB),
  354. CV_TEST_TAG_DEBUG_VERYLONG);
  355. if (backend == DNN_BACKEND_HALIDE)
  356. applyTestTag(CV_TEST_TAG_DNN_SKIP_HALIDE);
  357. #if defined(INF_ENGINE_RELEASE) && INF_ENGINE_VER_MAJOR_LE(2018050000)
  358. if (backend == DNN_BACKEND_INFERENCE_ENGINE_NN_BUILDER_2019 && target == DNN_TARGET_MYRIAD
  359. && getInferenceEngineVPUType() == CV_DNN_INFERENCE_ENGINE_VPU_TYPE_MYRIAD_X)
  360. applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_MYRIAD_X, CV_TEST_TAG_DNN_SKIP_IE_VERSION);
  361. #endif
  362. // output range: [-0.001, 0.97]
  363. const float l1 = (target == DNN_TARGET_MYRIAD) ? 0.02 : 0.0;
  364. const float lInf = (target == DNN_TARGET_MYRIAD || target == DNN_TARGET_OPENCL_FP16 || target == DNN_TARGET_CPU_FP16) ? 0.2 : 0.0;
  365. processNet("dnn/openpose_pose_mpi.caffemodel", "dnn/openpose_pose_mpi.prototxt",
  366. Size(46, 46), "", "", l1, lInf);
  367. expectNoFallbacksFromIE(net);
  368. expectNoFallbacksFromCUDA(net);
  369. }
  370. TEST_P(DNNTestNetwork, OpenPose_pose_mpi_faster_4_stages)
  371. {
  372. applyTestTag(CV_TEST_TAG_LONG, CV_TEST_TAG_MEMORY_1GB);
  373. if (backend == DNN_BACKEND_HALIDE)
  374. applyTestTag(CV_TEST_TAG_DNN_SKIP_HALIDE);
  375. #if defined(INF_ENGINE_RELEASE) && INF_ENGINE_VER_MAJOR_LE(2018050000)
  376. if (backend == DNN_BACKEND_INFERENCE_ENGINE_NN_BUILDER_2019 && target == DNN_TARGET_MYRIAD
  377. && getInferenceEngineVPUType() == CV_DNN_INFERENCE_ENGINE_VPU_TYPE_MYRIAD_X)
  378. applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_MYRIAD_X, CV_TEST_TAG_DNN_SKIP_IE_VERSION);
  379. #endif
  380. // The same .caffemodel but modified .prototxt
  381. // See https://github.com/CMU-Perceptual-Computing-Lab/openpose/blob/master/src/openpose/pose/poseParameters.cpp
  382. processNet("dnn/openpose_pose_mpi.caffemodel", "dnn/openpose_pose_mpi_faster_4_stages.prototxt",
  383. Size(46, 46));
  384. expectNoFallbacksFromIE(net);
  385. expectNoFallbacksFromCUDA(net);
  386. }
  387. TEST_P(DNNTestNetwork, OpenFace)
  388. {
  389. #if defined(INF_ENGINE_RELEASE)
  390. #if INF_ENGINE_VER_MAJOR_EQ(2018050000)
  391. if (backend == DNN_BACKEND_INFERENCE_ENGINE_NN_BUILDER_2019 && target == DNN_TARGET_MYRIAD)
  392. applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_MYRIAD, CV_TEST_TAG_DNN_SKIP_IE_VERSION);
  393. #endif
  394. #endif
  395. if (backend == DNN_BACKEND_HALIDE)
  396. applyTestTag(CV_TEST_TAG_DNN_SKIP_HALIDE);
  397. const float l1 = (target == DNN_TARGET_MYRIAD) ? 0.0024 : 0.0;
  398. const float lInf = (target == DNN_TARGET_MYRIAD) ? 0.0071 : 0.0;
  399. processNet("dnn/openface_nn4.small2.v1.t7", "", Size(96, 96), "", "", l1, lInf);
  400. expectNoFallbacksFromCUDA(net);
  401. }
  402. TEST_P(DNNTestNetwork, opencv_face_detector)
  403. {
  404. if (backend == DNN_BACKEND_HALIDE)
  405. applyTestTag(CV_TEST_TAG_DNN_SKIP_HALIDE);
  406. Mat img = imread(findDataFile("gpu/lbpcascade/er.png"));
  407. Mat inp = blobFromImage(img, 1.0, Size(), Scalar(104.0, 177.0, 123.0), false, false);
  408. processNet("dnn/opencv_face_detector.caffemodel", "dnn/opencv_face_detector.prototxt",
  409. inp, "detection_out");
  410. expectNoFallbacksFromIE(net);
  411. }
  412. TEST_P(DNNTestNetwork, Inception_v2_SSD_TensorFlow)
  413. {
  414. applyTestTag(
  415. (target == DNN_TARGET_CPU ? CV_TEST_TAG_MEMORY_512MB : CV_TEST_TAG_MEMORY_1GB),
  416. CV_TEST_TAG_DEBUG_VERYLONG
  417. );
  418. #if defined(INF_ENGINE_RELEASE)
  419. if (backend == DNN_BACKEND_INFERENCE_ENGINE_NN_BUILDER_2019 && target == DNN_TARGET_MYRIAD
  420. && getInferenceEngineVPUType() == CV_DNN_INFERENCE_ENGINE_VPU_TYPE_MYRIAD_X)
  421. applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_MYRIAD_X);
  422. #endif
  423. #if defined(INF_ENGINE_RELEASE) && INF_ENGINE_VER_MAJOR_EQ(2019020000)
  424. if (backend == DNN_BACKEND_INFERENCE_ENGINE_NN_BUILDER_2019 && target == DNN_TARGET_MYRIAD)
  425. applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_MYRIAD, CV_TEST_TAG_DNN_SKIP_IE_NN_BUILDER, CV_TEST_TAG_DNN_SKIP_IE_VERSION);
  426. #endif
  427. if (backend == DNN_BACKEND_HALIDE)
  428. applyTestTag(CV_TEST_TAG_DNN_SKIP_HALIDE);
  429. Mat sample = imread(findDataFile("dnn/street.png"));
  430. Mat inp = blobFromImage(sample, 1.0f, Size(300, 300), Scalar(), false);
  431. float scoreDiff = 0.0, iouDiff = 0.0;
  432. if (target == DNN_TARGET_OPENCL_FP16 || target == DNN_TARGET_MYRIAD || target == DNN_TARGET_CPU_FP16)
  433. {
  434. scoreDiff = 0.02;
  435. iouDiff = 0.1;
  436. }
  437. else if (target == DNN_TARGET_CUDA_FP16)
  438. {
  439. scoreDiff = 0.015;
  440. iouDiff = 0.08;
  441. }
  442. processNet("dnn/ssd_inception_v2_coco_2017_11_17.pb", "dnn/ssd_inception_v2_coco_2017_11_17.pbtxt",
  443. inp, "detection_out", "", scoreDiff, iouDiff);
  444. expectNoFallbacksFromIE(net);
  445. }
  446. TEST_P(DNNTestNetwork, DenseNet_121)
  447. {
  448. applyTestTag(CV_TEST_TAG_MEMORY_512MB);
  449. if (backend == DNN_BACKEND_HALIDE)
  450. applyTestTag(CV_TEST_TAG_DNN_SKIP_HALIDE);
  451. // Reference output values are in range [-3.807, 4.605]
  452. float l1 = 0.0, lInf = 0.0;
  453. if (target == DNN_TARGET_OPENCL_FP16 || target == DNN_TARGET_CPU_FP16)
  454. {
  455. l1 = 2e-2;
  456. lInf = 9e-2;
  457. if (backend == DNN_BACKEND_INFERENCE_ENGINE_NGRAPH)
  458. lInf = 0.1f;
  459. }
  460. else if (target == DNN_TARGET_MYRIAD)
  461. {
  462. l1 = 0.1;
  463. lInf = 0.6;
  464. }
  465. else if (target == DNN_TARGET_CUDA_FP16)
  466. {
  467. l1 = 0.008;
  468. lInf = 0.06;
  469. }
  470. processNet("dnn/DenseNet_121.caffemodel", "dnn/DenseNet_121.prototxt", Size(224, 224), "", "", l1, lInf);
  471. if (target != DNN_TARGET_MYRIAD || getInferenceEngineVPUType() != CV_DNN_INFERENCE_ENGINE_VPU_TYPE_MYRIAD_X)
  472. expectNoFallbacksFromIE(net);
  473. expectNoFallbacksFromCUDA(net);
  474. }
  475. TEST_P(DNNTestNetwork, FastNeuralStyle_eccv16)
  476. {
  477. applyTestTag(CV_TEST_TAG_MEMORY_512MB, CV_TEST_TAG_DEBUG_VERYLONG);
  478. if (backend == DNN_BACKEND_HALIDE)
  479. applyTestTag(CV_TEST_TAG_DNN_SKIP_HALIDE);
  480. if (backend == DNN_BACKEND_INFERENCE_ENGINE_NN_BUILDER_2019 && target == DNN_TARGET_MYRIAD)
  481. applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_MYRIAD, CV_TEST_TAG_DNN_SKIP_IE_NN_BUILDER);
  482. if (backend == DNN_BACKEND_INFERENCE_ENGINE_NGRAPH && target == DNN_TARGET_MYRIAD)
  483. applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_MYRIAD, CV_TEST_TAG_DNN_SKIP_IE_NGRAPH);
  484. #if defined(INF_ENGINE_RELEASE)
  485. #if INF_ENGINE_VER_MAJOR_LE(2018050000)
  486. if (backend == DNN_BACKEND_INFERENCE_ENGINE_NN_BUILDER_2019 && target == DNN_TARGET_OPENCL)
  487. applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_OPENCL, CV_TEST_TAG_DNN_SKIP_IE_VERSION);
  488. #endif
  489. #endif
  490. Mat img = imread(findDataFile("dnn/googlenet_1.png"));
  491. Mat inp = blobFromImage(img, 1.0, Size(320, 240), Scalar(103.939, 116.779, 123.68), false, false);
  492. // Output image has values in range [-143.526, 148.539].
  493. float l1 = 2e-4, lInf = 2.4e-3;
  494. if (target == DNN_TARGET_OPENCL_FP16 || target == DNN_TARGET_MYRIAD)
  495. {
  496. l1 = 0.4;
  497. lInf = 7.46;
  498. }
  499. else if (target == DNN_TARGET_CUDA_FP16)
  500. {
  501. l1 = 0.3;
  502. lInf = 7.6;
  503. }
  504. else if (target == DNN_TARGET_CPU_FP16)
  505. {
  506. l1 = 0.4;
  507. lInf = 22.;
  508. }
  509. else if (target == DNN_TARGET_VULKAN)
  510. {
  511. l1 = 0.4;
  512. lInf = 7.46;
  513. }
  514. #if defined(INF_ENGINE_RELEASE) && INF_ENGINE_VER_MAJOR_EQ(2022010000)
  515. if (backend == DNN_BACKEND_INFERENCE_ENGINE_NGRAPH && target == DNN_TARGET_OPENCL)
  516. {
  517. l1 = 5e-3;
  518. lInf = 5e-3;
  519. }
  520. if (backend == DNN_BACKEND_INFERENCE_ENGINE_NGRAPH && target == DNN_TARGET_OPENCL_FP16)
  521. {
  522. lInf = 25;
  523. }
  524. #endif
  525. processNet("dnn/fast_neural_style_eccv16_starry_night.t7", "", inp, "", "", l1, lInf);
  526. #if defined(HAVE_INF_ENGINE) && INF_ENGINE_VER_MAJOR_GE(2019010000)
  527. expectNoFallbacksFromIE(net);
  528. #endif
  529. expectNoFallbacksFromCUDA(net);
  530. }
  531. INSTANTIATE_TEST_CASE_P(/*nothing*/, DNNTestNetwork, dnnBackendsAndTargets(true, true, false, true, true));
  532. /*
  533. Backend tests of layers
  534. */
  535. static void testLayer(Mat& input, Net& net, Backend backendId, Target targetId, bool skipCheck = false, bool randInput = true, double l1 = 0.0, double lInf = 0.0)
  536. {
  537. DNNTestLayer::checkBackend(backendId, targetId);
  538. if (randInput)
  539. randu(input, -1.0f, 1.0f);
  540. net.setInput(input);
  541. net.setPreferableBackend(DNN_BACKEND_OPENCV);
  542. Mat outputDefault = net.forward().clone();
  543. net.setPreferableBackend(backendId);
  544. net.setPreferableTarget(targetId);
  545. Mat output = net.forward().clone();
  546. if (skipCheck)
  547. return;
  548. double default_l1, default_lInf;
  549. DNNTestLayer::getDefaultThresholds(backendId, targetId, &default_l1, &default_lInf);
  550. if (l1 == 0.0)
  551. l1 = default_l1;
  552. if (lInf == 0.0)
  553. lInf = default_lInf;
  554. normAssert(outputDefault, output, "", l1, lInf);
  555. if (cvtest::debugLevel > 0 || testing::Test::HasFailure())
  556. {
  557. std::cout << "l1=" << l1 << " lInf=" << lInf << std::endl;
  558. std::cout << outputDefault.reshape(1, outputDefault.total()).t() << std::endl;
  559. std::cout << output.reshape(1, outputDefault.total()).t() << std::endl;
  560. }
  561. }
  562. static void testLayer(LayerParams& params, Mat& input, Backend backendId, Target targetId, bool skipCheck = false, double l1 = 0.0, double lInf = 0.0)
  563. {
  564. Net net;
  565. net.addLayerToPrev(params.name, params.type, params);
  566. testLayer(input, net, backendId, targetId, skipCheck, true, l1, lInf);
  567. }
  568. class Test_layers_backends : public DNNTestLayer {};
  569. ////////////////////////////////////////////////////////////////////////////////
  570. // Padding
  571. ////////////////////////////////////////////////////////////////////////////////
  572. TEST_P(Test_layers_backends, Padding)
  573. {
  574. static const int kNumRuns = 10;
  575. std::vector<int> paddings(8);
  576. cv::RNG& rng = cv::theRNG();
  577. for (int t = 0; t < kNumRuns; ++t)
  578. {
  579. for (int i = 0; i < paddings.size(); ++i)
  580. paddings[i] = rng(5);
  581. LayerParams lp;
  582. lp.set("paddings", DictValue::arrayInt<int*>(&paddings[0], paddings.size()));
  583. lp.type = "Padding";
  584. lp.name = "testLayer";
  585. int sz[] = {1 + (int)rng(10), 1 + (int)rng(10), 1 + (int)rng(10), 1 + (int)rng(10)};
  586. Mat input(4, &sz[0], CV_32F);
  587. testLayer(lp, input, backend, target);
  588. }
  589. }
  590. ////////////////////////////////////////////////////////////////////////////////
  591. // Convolution
  592. ////////////////////////////////////////////////////////////////////////////////
  593. typedef TestWithParam<tuple<Vec3i, Size, Size, Size, Size, Size, bool, tuple<Backend, Target> > > Convolution;
  594. TEST_P(Convolution, Accuracy)
  595. {
  596. int inChannels = get<0>(GetParam())[0];
  597. int outChannels = get<0>(GetParam())[1];
  598. int group = get<0>(GetParam())[2];
  599. Size inSize = get<1>(GetParam());
  600. Size kernel = get<2>(GetParam());
  601. Size stride = get<3>(GetParam());
  602. Size pad = get<4>(GetParam());
  603. Size dilation = get<5>(GetParam());
  604. bool hasBias = get<6>(GetParam());
  605. Backend backendId = get<0>(get<7>(GetParam()));
  606. Target targetId = get<1>(get<7>(GetParam()));
  607. bool skipCheck = false;
  608. int sz[] = {outChannels, inChannels / group, kernel.height, kernel.width};
  609. Mat weights(4, &sz[0], CV_32F);
  610. randu(weights, -1.0f, 1.0f);
  611. LayerParams lp;
  612. lp.set("kernel_w", kernel.width);
  613. lp.set("kernel_h", kernel.height);
  614. lp.set("pad_w", pad.width);
  615. lp.set("pad_h", pad.height);
  616. lp.set("stride_w", stride.width);
  617. lp.set("stride_h", stride.height);
  618. lp.set("dilation_w", dilation.width);
  619. lp.set("dilation_h", dilation.height);
  620. lp.set("num_output", outChannels);
  621. lp.set("group", group);
  622. lp.set("bias_term", hasBias);
  623. lp.type = "Convolution";
  624. lp.name = "testLayer";
  625. lp.blobs.push_back(weights);
  626. if (hasBias)
  627. {
  628. Mat bias(1, outChannels, CV_32F);
  629. randu(bias, -1.0f, 1.0f);
  630. lp.blobs.push_back(bias);
  631. }
  632. int inpSz[] = {1, inChannels, inSize.height, inSize.width};
  633. Mat input(4, &inpSz[0], CV_32F);
  634. testLayer(lp, input, backendId, targetId, skipCheck);
  635. if (skipCheck)
  636. throw SkipTestException("Skip checks in unstable test");
  637. }
  638. INSTANTIATE_TEST_CASE_P(Layer_Test_Backends, Convolution, testing::Combine(
  639. /*in channels, out channels, group*/
  640. testing::Values(Vec3i(6, 4, 1), Vec3i(6, 9, 1),
  641. Vec3i(6, 4, 2), Vec3i(6, 9, 3)),
  642. /*in size*/ testing::Values(Size(5, 6)),
  643. /*kernel*/ testing::Values(Size(3, 1), Size(1, 3)),
  644. /*stride*/ testing::Values(Size(1, 1), Size(2, 2)),
  645. /*pad*/ testing::Values(Size(1, 0), Size(0, 1)),
  646. /*dilation*/ testing::Values(Size(1, 1), Size(2, 2)),
  647. /*has bias*/ testing::Bool(),
  648. dnnBackendsAndTargets()
  649. ));
  650. ////////////////////////////////////////////////////////////////////////////////
  651. // Deconvolution
  652. ////////////////////////////////////////////////////////////////////////////////
  653. typedef TestWithParam<tuple<Vec3i, Size, Size, Size, Size, Vec4i, bool, tuple<Backend, Target> > > Deconvolution;
  654. TEST_P(Deconvolution, Accuracy)
  655. {
  656. int inChannels = get<0>(GetParam())[0];
  657. int outChannels = get<0>(GetParam())[1];
  658. int group = get<0>(GetParam())[2];
  659. Size inSize = get<1>(GetParam());
  660. Size kernel = get<2>(GetParam());
  661. Size pad = get<3>(GetParam());
  662. Size dilation = get<4>(GetParam());
  663. Size stride = Size(get<5>(GetParam())[0], get<5>(GetParam())[1]);
  664. Size adjPad = Size(get<5>(GetParam())[2], get<5>(GetParam())[3]);
  665. bool hasBias = get<6>(GetParam());
  666. Backend backendId = get<0>(get<7>(GetParam()));
  667. Target targetId = get<1>(get<7>(GetParam()));
  668. #if defined(INF_ENGINE_RELEASE) && INF_ENGINE_VER_MAJOR_EQ(2022010000)
  669. if (backendId == DNN_BACKEND_INFERENCE_ENGINE_NGRAPH && (targetId == DNN_TARGET_OPENCL || targetId == DNN_TARGET_OPENCL_FP16)
  670. && inChannels == 6 && outChannels == 4 && group == 1
  671. && kernel == Size(3, 1) && pad == Size(0, 1)
  672. && stride == Size(1, 1) && dilation == Size(1, 1))
  673. applyTestTag(targetId == DNN_TARGET_OPENCL ? CV_TEST_TAG_DNN_SKIP_IE_OPENCL : CV_TEST_TAG_DNN_SKIP_IE_OPENCL_FP16,
  674. CV_TEST_TAG_DNN_SKIP_IE_NGRAPH, CV_TEST_TAG_DNN_SKIP_IE_VERSION
  675. );
  676. if (backendId == DNN_BACKEND_INFERENCE_ENGINE_NGRAPH && (targetId == DNN_TARGET_OPENCL || targetId == DNN_TARGET_OPENCL_FP16)
  677. && inChannels == 6 && outChannels == 4 && group == 1
  678. && kernel == Size(1, 3) && pad == Size(1, 0)
  679. && stride == Size(1, 1) && dilation == Size(1, 1))
  680. applyTestTag(targetId == DNN_TARGET_OPENCL ? CV_TEST_TAG_DNN_SKIP_IE_OPENCL : CV_TEST_TAG_DNN_SKIP_IE_OPENCL_FP16,
  681. CV_TEST_TAG_DNN_SKIP_IE_NGRAPH, CV_TEST_TAG_DNN_SKIP_IE_VERSION
  682. );
  683. #endif
  684. #if defined(INF_ENGINE_RELEASE) && INF_ENGINE_VER_MAJOR_GE(2019010000)
  685. if (backendId == DNN_BACKEND_INFERENCE_ENGINE_NN_BUILDER_2019 && targetId == DNN_TARGET_MYRIAD
  686. && getInferenceEngineVPUType() == CV_DNN_INFERENCE_ENGINE_VPU_TYPE_MYRIAD_X
  687. && inChannels == 6 && outChannels == 4 && group == 1
  688. && kernel == Size(1, 3) && pad == Size(1, 0)
  689. && stride == Size(1, 1) && dilation == Size(1, 1))
  690. applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_MYRIAD_X);
  691. #endif
  692. if (targetId == DNN_TARGET_CUDA_FP16)
  693. applyTestTag(CV_TEST_TAG_DNN_SKIP_CUDA_FP16);
  694. int sz[] = {inChannels, outChannels / group, kernel.height, kernel.width};
  695. Mat weights(4, &sz[0], CV_32F);
  696. randu(weights, -1.0f, 1.0f);
  697. LayerParams lp;
  698. lp.set("kernel_w", kernel.width);
  699. lp.set("kernel_h", kernel.height);
  700. lp.set("pad_w", pad.width);
  701. lp.set("pad_h", pad.height);
  702. lp.set("stride_w", stride.width);
  703. lp.set("stride_h", stride.height);
  704. lp.set("dilation_w", dilation.width);
  705. lp.set("dilation_h", dilation.height);
  706. lp.set("adj_w", adjPad.width);
  707. lp.set("adj_h", adjPad.height);
  708. lp.set("num_output", outChannels);
  709. lp.set("group", group);
  710. lp.set("bias_term", hasBias);
  711. lp.type = "Deconvolution";
  712. lp.name = "testLayer";
  713. lp.blobs.push_back(weights);
  714. if (hasBias)
  715. {
  716. Mat bias(1, outChannels, CV_32F);
  717. randu(bias, -1.0f, 1.0f);
  718. lp.blobs.push_back(bias);
  719. }
  720. int inpSz[] = {1, inChannels, inSize.height, inSize.width};
  721. Mat input(4, &inpSz[0], CV_32F);
  722. testLayer(lp, input, backendId, targetId);
  723. }
  724. INSTANTIATE_TEST_CASE_P(Layer_Test_Backends, Deconvolution, testing::Combine(
  725. /*in channels, out channels, group*/
  726. testing::Values(Vec3i(6, 4, 1), Vec3i(6, 9, 3)),
  727. /*in size*/ testing::Values(Size(5, 6)),
  728. /*kernel*/ testing::Values(Size(3, 1), Size(1, 3)),
  729. /*pad*/ testing::Values(Size(1, 0), Size(0, 1)),
  730. /*dilation*/ testing::Values(Size(1, 1)),
  731. /*stride, adj. pad*/ testing::Values(Vec4i(1,1, 0,0), Vec4i(2,2, 1,0), Vec4i(1,2, 0,1)),
  732. /*has bias*/ testing::Bool(),
  733. dnnBackendsAndTargets()
  734. ));
  735. ////////////////////////////////////////////////////////////////////////////////
  736. // LRN
  737. ////////////////////////////////////////////////////////////////////////////////
  738. typedef TestWithParam<tuple<Vec3i, int, Vec3f, bool, std::string, tuple<Backend, Target> > > LRN;
  739. TEST_P(LRN, Accuracy)
  740. {
  741. int inChannels = get<0>(GetParam())[0];
  742. Size inSize = Size(get<0>(GetParam())[1], get<0>(GetParam())[2]);
  743. int localSize = get<1>(GetParam());
  744. float alpha = get<2>(GetParam())[0];
  745. float beta = get<2>(GetParam())[1];
  746. float bias = get<2>(GetParam())[2];
  747. bool normBySize = get<3>(GetParam());
  748. std::string nrmType = get<4>(GetParam());
  749. Backend backendId = get<0>(get<5>(GetParam()));
  750. Target targetId = get<1>(get<5>(GetParam()));
  751. #if defined(INF_ENGINE_RELEASE) && INF_ENGINE_VER_MAJOR_LT(2021040000)
  752. if ((inSize.width == 5 || inSize.height == 5) && targetId == DNN_TARGET_MYRIAD &&
  753. nrmType == "ACROSS_CHANNELS")
  754. applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_MYRIAD);
  755. #endif
  756. LayerParams lp;
  757. lp.set("norm_region", nrmType);
  758. lp.set("local_size", localSize);
  759. lp.set("alpha", alpha);
  760. lp.set("beta", beta);
  761. lp.set("bias", bias);
  762. lp.set("norm_by_size", normBySize);
  763. lp.type = "LRN";
  764. lp.name = "testLayer";
  765. int sz[] = {1, inChannels, inSize.height, inSize.width};
  766. Mat input(4, &sz[0], CV_32F);
  767. double l1 = 0.0, lInf = 0.0;
  768. // The OpenCL kernels use the native_ math functions which have
  769. // implementation defined accuracy, so we use relaxed thresholds. See
  770. // https://github.com/opencv/opencv/issues/9821 for more details.
  771. if (targetId == DNN_TARGET_OPENCL)
  772. {
  773. l1 = 0.01;
  774. lInf = 0.01;
  775. }
  776. testLayer(lp, input, backendId, targetId, false, l1, lInf);
  777. }
  778. INSTANTIATE_TEST_CASE_P(Layer_Test_Backends, LRN, testing::Combine(
  779. /*input ch,w,h*/ testing::Values(Vec3i(6, 5, 8), Vec3i(7, 11, 6)),
  780. /*local size*/ testing::Values(3, 5),
  781. testing::Values(Vec3f(0.9f, 1.0f, 1.1f), Vec3f(0.9f, 1.1f, 1.0f),
  782. /*alpha, beta, bias*/ Vec3f(1.0f, 0.9f, 1.1f), Vec3f(1.0f, 1.1f, 0.9f),
  783. Vec3f(1.1f, 0.9f, 1.0f), Vec3f(1.1f, 1.0f, 0.9f)),
  784. /*norm_by_size*/ testing::Bool(),
  785. /*norm_type*/ testing::Values("ACROSS_CHANNELS", "WITHIN_CHANNEL"),
  786. dnnBackendsAndTargets()
  787. ));
  788. ////////////////////////////////////////////////////////////////////////////////
  789. // Average pooling
  790. ////////////////////////////////////////////////////////////////////////////////
  791. typedef TestWithParam<tuple<int, Size, Size, Size, tuple<Backend, Target> > > AvePooling;
  792. TEST_P(AvePooling, Accuracy)
  793. {
  794. int inChannels = get<0>(GetParam());
  795. Size outSize = get<1>(GetParam());; // Input size will be computed from parameters.
  796. Size kernel = get<2>(GetParam());
  797. Size stride = get<3>(GetParam());
  798. Backend backendId = get<0>(get<4>(GetParam()));
  799. Target targetId = get<1>(get<4>(GetParam()));
  800. #if defined(INF_ENGINE_RELEASE)
  801. if (backendId == DNN_BACKEND_INFERENCE_ENGINE_NN_BUILDER_2019 && targetId == DNN_TARGET_MYRIAD
  802. && getInferenceEngineVPUType() == CV_DNN_INFERENCE_ENGINE_VPU_TYPE_MYRIAD_X
  803. && kernel == Size(1, 1) && (stride == Size(1, 1) || stride == Size(2, 2)))
  804. applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_MYRIAD_X);
  805. #endif
  806. const int inWidth = (outSize.width - 1) * stride.width + kernel.width;
  807. const int inHeight = (outSize.height - 1) * stride.height + kernel.height;
  808. LayerParams lp;
  809. lp.set("pool", "ave");
  810. lp.set("kernel_w", kernel.width);
  811. lp.set("kernel_h", kernel.height);
  812. lp.set("stride_w", stride.width);
  813. lp.set("stride_h", stride.height);
  814. lp.type = "Pooling";
  815. lp.name = "testLayer";
  816. int sz[] = {1, inChannels, inHeight, inWidth};
  817. Mat input(4, &sz[0], CV_32F);
  818. testLayer(lp, input, backendId, targetId);
  819. }
  820. INSTANTIATE_TEST_CASE_P(Layer_Test_Backends, AvePooling, testing::Combine(
  821. /*in channels*/ testing::Values(3, 4),
  822. /*out size*/ testing::Values(Size(1, 1), Size(2, 2), Size(3, 2), Size(4, 7)),
  823. /*kernel*/ testing::Values(Size(1, 1), Size(2, 2), Size(3, 3), Size(3, 2)),
  824. /*stride*/ testing::Values(Size(1, 1), Size(2, 2), Size(3, 2)),
  825. dnnBackendsAndTargets()
  826. ));
  827. ////////////////////////////////////////////////////////////////////////////////
  828. // Maximum pooling
  829. ////////////////////////////////////////////////////////////////////////////////
  830. typedef TestWithParam<tuple<int, Size, Size, Size, Size, tuple<Backend, Target> > > MaxPooling;
  831. TEST_P(MaxPooling, Accuracy)
  832. {
  833. int inChannels = get<0>(GetParam());
  834. Size inSize = get<1>(GetParam());
  835. Size kernel = get<2>(GetParam());
  836. Size stride = get<3>(GetParam());
  837. Size pad = get<4>(GetParam());
  838. Backend backendId = get<0>(get<5>(GetParam()));
  839. Target targetId = get<1>(get<5>(GetParam()));
  840. // https://github.com/openvinotoolkit/openvino/issues/18731
  841. if (backendId == DNN_BACKEND_INFERENCE_ENGINE_NGRAPH && stride != Size(1, 1)) {
  842. int ow = ceil(static_cast<float>(inSize.width + 2 * pad.width - kernel.width) / stride.width);
  843. int oh = ceil(static_cast<float>(inSize.height + 2 * pad.height - kernel.height) / stride.height);
  844. if (ow * stride.width >= inSize.width + pad.width || oh * stride.height >= inSize.height + pad.height)
  845. applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_NGRAPH);
  846. }
  847. #if defined(INF_ENGINE_RELEASE) && INF_ENGINE_VER_MAJOR_GE(2019010000)
  848. if (backendId == DNN_BACKEND_INFERENCE_ENGINE_NN_BUILDER_2019 && targetId == DNN_TARGET_MYRIAD
  849. && getInferenceEngineVPUType() == CV_DNN_INFERENCE_ENGINE_VPU_TYPE_MYRIAD_X
  850. && (stride == Size(1, 1) || stride == Size(2, 2))
  851. && (pad == Size(0, 1) || pad == Size(1, 1))
  852. )
  853. applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_MYRIAD_X, CV_TEST_TAG_DNN_SKIP_IE_VERSION);
  854. #endif
  855. #if defined(INF_ENGINE_RELEASE) && INF_ENGINE_VER_MAJOR_GE(2020020000)
  856. if (backendId == DNN_BACKEND_INFERENCE_ENGINE_NGRAPH && targetId == DNN_TARGET_MYRIAD)
  857. applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_MYRIAD, CV_TEST_TAG_DNN_SKIP_IE_NGRAPH, CV_TEST_TAG_DNN_SKIP_IE_VERSION);
  858. #endif
  859. LayerParams lp;
  860. lp.set("pool", "max");
  861. lp.set("kernel_w", kernel.width);
  862. lp.set("kernel_h", kernel.height);
  863. lp.set("stride_w", stride.width);
  864. lp.set("stride_h", stride.height);
  865. lp.set("pad_w", pad.width);
  866. lp.set("pad_h", pad.height);
  867. lp.type = "Pooling";
  868. lp.name = "testLayer";
  869. int sz[] = {1, inChannels, inSize.height, inSize.width};
  870. Mat input(4, &sz[0], CV_32F);
  871. testLayer(lp, input, backendId, targetId);
  872. }
  873. INSTANTIATE_TEST_CASE_P(Layer_Test_Backends, MaxPooling, testing::Combine(
  874. /*in channels*/ testing::Values(3, 4),
  875. /*in size*/ testing::Values(Size(5, 5), Size(7, 6)),
  876. /*kernel*/ testing::Values(Size(2, 2), Size(3, 3), Size(3, 2)),
  877. /*stride*/ testing::Values(Size(1, 1), Size(2, 2), Size(3, 2)),
  878. /*pad*/ testing::Values(Size(0, 0), Size(1, 1), Size(0, 1)),
  879. dnnBackendsAndTargets()
  880. ));
  881. ////////////////////////////////////////////////////////////////////////////////
  882. // Fully-connected
  883. ////////////////////////////////////////////////////////////////////////////////
  884. typedef TestWithParam<tuple<int, int, Size, int, bool, tuple<Backend, Target> > > FullyConnected;
  885. TEST_P(FullyConnected, Accuracy)
  886. {
  887. int batch = get<0>(GetParam());
  888. int inChannels = get<1>(GetParam());
  889. Size inSize = get<2>(GetParam());
  890. int outChannels = get<3>(GetParam());
  891. bool hasBias = get<4>(GetParam());
  892. Backend backendId = get<0>(get<5>(GetParam()));
  893. Target targetId = get<1>(get<5>(GetParam()));
  894. #if defined(INF_ENGINE_RELEASE) && INF_ENGINE_VER_MAJOR_LT(2021040000)
  895. if ((backendId == DNN_BACKEND_INFERENCE_ENGINE_NN_BUILDER_2019 ||
  896. backendId == DNN_BACKEND_INFERENCE_ENGINE_NGRAPH) && (targetId == DNN_TARGET_OPENCL_FP16 ||
  897. (targetId == DNN_TARGET_MYRIAD && getInferenceEngineVPUType() == CV_DNN_INFERENCE_ENGINE_VPU_TYPE_MYRIAD_X))) {
  898. applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_OPENCL_FP16);
  899. applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_MYRIAD_X);
  900. }
  901. #endif
  902. // https://github.com/openvinotoolkit/openvino/issues/19436
  903. if (backendId == DNN_BACKEND_INFERENCE_ENGINE_NGRAPH && targetId == DNN_TARGET_OPENCL_FP16 && batch == 16)
  904. applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_OPENCL_FP16);
  905. #if defined(INF_ENGINE_RELEASE) && INF_ENGINE_VER_MAJOR_EQ(2023000000)
  906. if (backendId == DNN_BACKEND_INFERENCE_ENGINE_NGRAPH && targetId == DNN_TARGET_OPENCL && batch == 16)
  907. applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_OPENCL);
  908. #endif
  909. Mat weights(outChannels, inChannels * inSize.height * inSize.width, CV_32F);
  910. randu(weights, -1.0f, 1.0f);
  911. Mat bias(1, outChannels, CV_32F);
  912. randu(bias, -1.0f, 1.0f);
  913. LayerParams lp;
  914. lp.set("num_output", outChannels);
  915. lp.set("bias_term", hasBias);
  916. lp.blobs.push_back(weights);
  917. lp.blobs.push_back(bias);
  918. lp.type = "InnerProduct";
  919. lp.name = "testLayer";
  920. int sz[] = {batch, inChannels, inSize.height, inSize.width};
  921. Mat input(4, &sz[0], CV_32F);
  922. double l1 = 0.0;
  923. double lInf = 0.0;
  924. #if defined(INF_ENGINE_RELEASE)
  925. if (targetId == DNN_TARGET_MYRIAD)
  926. {
  927. l1 = 0.015;
  928. lInf = 0.025;
  929. }
  930. if (backendId == DNN_BACKEND_INFERENCE_ENGINE_NGRAPH && targetId == DNN_TARGET_OPENCL_FP16)
  931. {
  932. l1 = 0.01;
  933. if (INF_ENGINE_VER_MAJOR_GE(2023000000))
  934. lInf = 0.016;
  935. }
  936. if (backendId == DNN_BACKEND_INFERENCE_ENGINE_NGRAPH && targetId == DNN_TARGET_OPENCL)
  937. {
  938. l1 = 5e-3;
  939. lInf = INF_ENGINE_VER_MAJOR_GE(2023000000) ? 0.016 : 7e-3;
  940. }
  941. #endif
  942. if (targetId == DNN_TARGET_CUDA_FP16)
  943. l1 = 0.015;
  944. testLayer(lp, input, backendId, targetId, false, l1, lInf);
  945. }
  946. INSTANTIATE_TEST_CASE_P(Layer_Test_Backends, FullyConnected, testing::Combine(
  947. /*batch*/ testing::Values(1, 2, 4, 8, 16),
  948. /*in channels*/ testing::Values(3, 4),
  949. /*in size*/ testing::Values(Size(5, 4), Size(4, 5), Size(1, 1)),
  950. /*out channels*/ testing::Values(3, 4),
  951. /*has bias*/ testing::Bool(),
  952. dnnBackendsAndTargets()
  953. ));
  954. ////////////////////////////////////////////////////////////////////////////////
  955. // SoftMax
  956. ////////////////////////////////////////////////////////////////////////////////
  957. typedef TestWithParam<tuple<int, tuple<Backend, Target> > > SoftMax;
  958. TEST_P(SoftMax, Accuracy)
  959. {
  960. int inChannels = get<0>(GetParam());
  961. Backend backendId = get<0>(get<1>(GetParam()));
  962. Target targetId = get<1>(get<1>(GetParam()));
  963. LayerParams lp;
  964. lp.type = "Softmax";
  965. lp.name = "testLayer";
  966. int sz[] = {1, inChannels, 1, 1};
  967. Mat input(4, &sz[0], CV_32F);
  968. testLayer(lp, input, backendId, targetId);
  969. }
  970. INSTANTIATE_TEST_CASE_P(Layer_Test_Backends, SoftMax, testing::Combine(
  971. testing::Values(3, 4, 5, 1024),
  972. dnnBackendsAndTargets()
  973. ));
  974. //////////////////////////////////////////////////////////////////////////////
  975. // Max pooling - unpooling
  976. //////////////////////////////////////////////////////////////////////////////
  977. TEST_P(Test_layers_backends, MaxPoolUnpool)
  978. {
  979. #if defined(INF_ENGINE_RELEASE) && INF_ENGINE_VER_MAJOR_LT(2023000000)
  980. if (backend == DNN_BACKEND_INFERENCE_ENGINE_NN_BUILDER_2019)
  981. applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_NN_BUILDER);
  982. if (backend == DNN_BACKEND_INFERENCE_ENGINE_NGRAPH)
  983. applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_NGRAPH);
  984. #endif
  985. LayerParams pool;
  986. pool.set("pool", "max");
  987. pool.set("kernel_w", 2);
  988. pool.set("kernel_h", 2);
  989. pool.set("stride_w", 2);
  990. pool.set("stride_h", 2);
  991. pool.set("pad_w", 0);
  992. pool.set("pad_h", 0);
  993. pool.type = "Pooling";
  994. pool.name = "testPool";
  995. LayerParams unpool;
  996. unpool.set("pool_k_w", 2);
  997. unpool.set("pool_k_h", 2);
  998. unpool.set("pool_stride_w", 2);
  999. unpool.set("pool_stride_h", 2);
  1000. unpool.set("pool_pad_w", 0);
  1001. unpool.set("pool_pad_h", 0);
  1002. unpool.type = "MaxUnpool";
  1003. unpool.name = "testUnpool";
  1004. Net net;
  1005. int poolId = net.addLayer(pool.name, pool.type, pool);
  1006. net.connect(0, 0, poolId, 0);
  1007. int unpoolId = net.addLayer(unpool.name, unpool.type, unpool);
  1008. net.connect(poolId, 0, unpoolId, 0);
  1009. net.connect(poolId, 1, unpoolId, 1);
  1010. int sz[] = {1, 1, 4, 4};
  1011. Mat input(4, &sz[0], CV_32F);
  1012. testLayer(input, net, backend, target);
  1013. }
  1014. ////////////////////////////////////////////////////////////////////////////////
  1015. // AvePooling + in-place layers
  1016. ////////////////////////////////////////////////////////////////////////////////
  1017. static const int kNumChannels = 3;
  1018. void testInPlaceActivation(LayerParams& lp, Backend backendId, Target targetId, double l1 = 0.0, double lInf = 0.0)
  1019. {
  1020. EXPECT_FALSE(lp.name.empty());
  1021. LayerParams pool;
  1022. pool.set("pool", "ave");
  1023. pool.set("kernel_w", 2);
  1024. pool.set("kernel_h", 2);
  1025. pool.set("stride_w", 2);
  1026. pool.set("stride_h", 2);
  1027. pool.type = "Pooling";
  1028. pool.name = "ave_pool";
  1029. Net net;
  1030. int poolId = net.addLayer(pool.name, pool.type, pool);
  1031. net.connect(0, 0, poolId, 0);
  1032. net.addLayerToPrev(lp.name, lp.type, lp);
  1033. int sz[] = {1, kNumChannels, 10, 10};
  1034. Mat input(4, &sz[0], CV_32F);
  1035. testLayer(input, net, backendId, targetId, false, true, l1, lInf);
  1036. }
  1037. typedef TestWithParam<tuple<bool, bool, float, tuple<Backend, Target> > > BatchNorm;
  1038. TEST_P(BatchNorm, Accuracy)
  1039. {
  1040. bool hasWeights = get<0>(GetParam());
  1041. bool hasBias = get<1>(GetParam());
  1042. float epsilon = get<2>(GetParam());
  1043. Backend backendId = get<0>(get<3>(GetParam()));
  1044. Target targetId = get<1>(get<3>(GetParam()));
  1045. LayerParams lp;
  1046. lp.set("has_weight", hasWeights);
  1047. lp.set("has_bias", hasBias);
  1048. lp.set("eps", epsilon);
  1049. lp.type = "BatchNorm";
  1050. lp.name = "testLayer";
  1051. lp.blobs.reserve(4);
  1052. for (int i = 0; i < 3; ++i)
  1053. lp.blobs.push_back(Mat(1, kNumChannels, CV_32F));
  1054. if (hasBias || hasWeights)
  1055. lp.blobs.push_back(Mat(1, kNumChannels, CV_32F));
  1056. for (int i = 0; i < lp.blobs.size(); ++i)
  1057. randu(lp.blobs[i], 0.0f, 1.0f);
  1058. testInPlaceActivation(lp, backendId, targetId);
  1059. }
  1060. INSTANTIATE_TEST_CASE_P(Layer_Test_Backends, BatchNorm, testing::Combine(
  1061. /*has weights*/ testing::Bool(),
  1062. /*has bias*/ testing::Bool(),
  1063. /*epsilon*/ testing::Values(1e-3f, 1e-5f),
  1064. dnnBackendsAndTargets()
  1065. ));
  1066. typedef TestWithParam<tuple<float, tuple<Backend, Target> > > ReLU;
  1067. TEST_P(ReLU, Accuracy)
  1068. {
  1069. float negativeSlope = get<0>(GetParam());
  1070. Backend backendId = get<0>(get<1>(GetParam()));
  1071. Target targetId = get<1>(get<1>(GetParam()));
  1072. #if defined(INF_ENGINE_RELEASE) && INF_ENGINE_VER_MAJOR_GE(2019020000)
  1073. if (backendId == DNN_BACKEND_INFERENCE_ENGINE_NN_BUILDER_2019 && targetId == DNN_TARGET_MYRIAD && negativeSlope < 0)
  1074. applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_NN_BUILDER, CV_TEST_TAG_DNN_SKIP_IE_VERSION);
  1075. #endif
  1076. LayerParams lp;
  1077. lp.set("negative_slope", negativeSlope);
  1078. lp.type = "ReLU";
  1079. lp.name = "testLayer";
  1080. testInPlaceActivation(lp, backendId, targetId);
  1081. }
  1082. INSTANTIATE_TEST_CASE_P(Layer_Test_Backends, ReLU, testing::Combine(
  1083. /*negative slope*/ testing::Values(2.0f, 0.3f, -0.1f, 0.0f),
  1084. dnnBackendsAndTargets()
  1085. ));
  1086. typedef TestWithParam<tuple<std::string, tuple<Backend, Target> > > NoParamActivation;
  1087. TEST_P(NoParamActivation, Accuracy)
  1088. {
  1089. Backend backendId = get<0>(get<1>(GetParam()));
  1090. Target targetId = get<1>(get<1>(GetParam()));
  1091. std::string layer_type = get<0>(GetParam());
  1092. LayerParams lp;
  1093. lp.type = layer_type;
  1094. lp.name = "testLayer";
  1095. testInPlaceActivation(lp, backendId, targetId);
  1096. }
  1097. INSTANTIATE_TEST_CASE_P(Layer_Test_Backends, NoParamActivation, testing::Combine(
  1098. /*type*/ testing::Values("TanH", "Sigmoid", "AbsVal", "BNLL", "Swish", "Mish"),
  1099. dnnBackendsAndTargets()
  1100. ));
  1101. typedef TestWithParam<tuple<Vec3f, tuple<Backend, Target> > > Power;
  1102. TEST_P(Power, Accuracy)
  1103. {
  1104. float power = get<0>(GetParam())[0];
  1105. float scale = get<0>(GetParam())[1];
  1106. float shift = get<0>(GetParam())[2];
  1107. Backend backendId = get<0>(get<1>(GetParam()));
  1108. Target targetId = get<1>(get<1>(GetParam()));
  1109. LayerParams lp;
  1110. lp.set("power", power);
  1111. lp.set("scale", scale);
  1112. lp.set("shift", shift);
  1113. lp.type = "Power";
  1114. lp.name = "testLayer";
  1115. testInPlaceActivation(lp, backendId, targetId);
  1116. }
  1117. INSTANTIATE_TEST_CASE_P(Layer_Test_Backends, Power, testing::Combine(
  1118. /*power, scale, shift*/ testing::Values(Vec3f(0.9f, 1.0f, 1.1f), Vec3f(0.9f, 1.1f, 1.0f),
  1119. Vec3f(1.0f, 0.9f, 1.1f), Vec3f(1.0f, 1.1f, 0.9f),
  1120. Vec3f(1.1f, 0.9f, 1.0f), Vec3f(1.1f, 1.0f, 0.9f)),
  1121. dnnBackendsAndTargets()
  1122. ));
  1123. typedef TestWithParam<tuple<Vec3f, tuple<Backend, Target> > > Exp;
  1124. TEST_P(Exp, Accuracy)
  1125. {
  1126. float base = get<0>(GetParam())[0];
  1127. float scale = get<0>(GetParam())[1];
  1128. float shift = get<0>(GetParam())[2];
  1129. Backend backendId = get<0>(get<1>(GetParam()));
  1130. Target targetId = get<1>(get<1>(GetParam()));
  1131. LayerParams lp;
  1132. lp.set("base", base);
  1133. lp.set("scale", scale);
  1134. lp.set("shift", shift);
  1135. lp.type = "Exp";
  1136. lp.name = "testLayer";
  1137. testInPlaceActivation(lp, backendId, targetId);
  1138. }
  1139. INSTANTIATE_TEST_CASE_P(Layer_Test_Backends, Exp, testing::Combine(
  1140. /*base, scale, shift*/ testing::Values(Vec3f(0.9f, -1.0f, 1.1f), Vec3f(0.9f, 1.1f, -1.0f),
  1141. Vec3f(-1.0f, 0.9f, 1.1f), Vec3f(-1.0f, 1.1f, 0.9f),
  1142. Vec3f(1.1f, 0.9f, -1.0f), Vec3f(1.1f, -1.0f, 0.9f)),
  1143. dnnBackendsAndTargets()
  1144. ));
  1145. TEST_P(Test_layers_backends, ChannelsPReLU)
  1146. {
  1147. LayerParams lp;
  1148. lp.type = "ChannelsPReLU";
  1149. lp.name = "testLayer";
  1150. lp.blobs.push_back(Mat(1, kNumChannels, CV_32F));
  1151. randu(lp.blobs[0], -1.0f, 1.0f);
  1152. testInPlaceActivation(lp, backend, target);
  1153. }
  1154. typedef TestWithParam<tuple<bool, tuple<Backend, Target> > > Scale;
  1155. TEST_P(Scale, Accuracy)
  1156. {
  1157. bool hasBias = get<0>(GetParam());
  1158. Backend backendId = get<0>(get<1>(GetParam()));
  1159. Target targetId = get<1>(get<1>(GetParam()));
  1160. LayerParams lp;
  1161. lp.set("bias_term", hasBias);
  1162. lp.type = "Scale";
  1163. lp.name = "testLayer";
  1164. lp.blobs.push_back(Mat(1, kNumChannels, CV_32F));
  1165. randu(lp.blobs[0], -1.0f, 1.0f);
  1166. if (hasBias)
  1167. {
  1168. lp.blobs.push_back(Mat(1, kNumChannels, CV_32F));
  1169. randu(lp.blobs[1], -1.0f, 1.0f);
  1170. }
  1171. testInPlaceActivation(lp, backendId, targetId);
  1172. }
  1173. INSTANTIATE_TEST_CASE_P(Layer_Test_Backends, Scale, testing::Combine(
  1174. testing::Bool(),
  1175. dnnBackendsAndTargets()
  1176. ));
  1177. ////////////////////////////////////////////////////////////////////////////////
  1178. // Concat layer
  1179. ////////////////////////////////////////////////////////////////////////////////
  1180. //
  1181. // input --- conv --- concat --- output
  1182. // `--- conv ----^ ^ ^
  1183. // `---- ... ------' '
  1184. // `-----------------'
  1185. typedef TestWithParam<tuple<Vec3i, Vec3i, tuple<Backend, Target> > > Concat;
  1186. TEST_P(Concat, Accuracy)
  1187. {
  1188. Vec3i inSize = get<0>(GetParam());
  1189. Vec3i numChannels = get<1>(GetParam());
  1190. Backend backendId = get<0>(get<2>(GetParam()));
  1191. Target targetId = get<1>(get<2>(GetParam()));
  1192. #if defined(INF_ENGINE_RELEASE) && INF_ENGINE_VER_MAJOR_LE(2018050000)
  1193. if (backendId == DNN_BACKEND_INFERENCE_ENGINE_NN_BUILDER_2019 && targetId == DNN_TARGET_MYRIAD
  1194. && inSize == Vec3i(1, 4, 5) && numChannels == Vec3i(1, 6, 2)
  1195. )
  1196. applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_MYRIAD, CV_TEST_TAG_DNN_SKIP_IE_NN_BUILDER, CV_TEST_TAG_DNN_SKIP_IE_VERSION); // crash
  1197. #endif
  1198. #if defined(INF_ENGINE_RELEASE) && INF_ENGINE_VER_MAJOR_GE(2019010000)
  1199. if (backendId == DNN_BACKEND_INFERENCE_ENGINE_NN_BUILDER_2019 && targetId == DNN_TARGET_CPU
  1200. && inSize == Vec3i(1, 4, 5) && numChannels == Vec3i(1, 6, 2)
  1201. )
  1202. applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_NN_BUILDER, CV_TEST_TAG_DNN_SKIP_IE_VERSION); // TODO: IE_CPU
  1203. #endif
  1204. Net net;
  1205. std::vector<int> convLayerIds;
  1206. convLayerIds.reserve(numChannels.channels);
  1207. for (int i = 0, n = numChannels.channels; i < n; ++i)
  1208. {
  1209. if (!numChannels[i])
  1210. break;
  1211. int sz[] = {numChannels[i], inSize[0], 1, 1};
  1212. Mat weights(4, &sz[0], CV_32F);
  1213. randu(weights, -1.0f, 1.0f);
  1214. LayerParams convParam;
  1215. convParam.set("kernel_w", 1);
  1216. convParam.set("kernel_h", 1);
  1217. convParam.set("num_output", numChannels[i]);
  1218. convParam.set("bias_term", false);
  1219. convParam.type = "Convolution";
  1220. std::ostringstream ss;
  1221. ss << "convLayer" << i;
  1222. convParam.name = ss.str();
  1223. convParam.blobs.push_back(weights);
  1224. int layerId = net.addLayer(convParam.name, convParam.type, convParam);
  1225. convLayerIds.push_back(layerId);
  1226. net.connect(0, 0, layerId, 0);
  1227. }
  1228. LayerParams concatParam;
  1229. concatParam.type = "Concat";
  1230. concatParam.name = "testLayer";
  1231. int concatId = net.addLayer(concatParam.name, concatParam.type, concatParam);
  1232. net.connect(0, 0, concatId, 0);
  1233. for (int i = 0; i < convLayerIds.size(); ++i)
  1234. {
  1235. net.connect(convLayerIds[i], 0, concatId, i + 1);
  1236. }
  1237. int sz[] = {1, inSize[0], inSize[1], inSize[2]};
  1238. Mat input(4, &sz[0], CV_32F);
  1239. testLayer(input, net, backendId, targetId);
  1240. }
  1241. INSTANTIATE_TEST_CASE_P(Layer_Test_Backends, Concat, testing::Combine(
  1242. /*input size*/ testing::Values(Vec3i(1, 4, 5), Vec3i(2, 8, 6)),
  1243. /*channels*/ testing::Values(Vec3i(2, 0, 0), Vec3i(3, 4, 0), Vec3i(1, 6, 2)),
  1244. dnnBackendsAndTargets()
  1245. ));
  1246. ////////////////////////////////////////////////////////////////////////////////
  1247. // Element-wise layers
  1248. ////////////////////////////////////////////////////////////////////////////////
  1249. //
  1250. // input --- conv --- eltwise --- output
  1251. // `--- conv ----^ ^ ^
  1252. // `---- ... ------' '
  1253. // `-----------------'
  1254. typedef TestWithParam<tuple<Vec3i, std::string, int, bool, tuple<Backend, Target> > > Eltwise;
  1255. TEST_P(Eltwise, Accuracy)
  1256. {
  1257. Vec3i inSize = get<0>(GetParam());
  1258. std::string op = get<1>(GetParam());
  1259. int numConv = get<2>(GetParam());
  1260. bool weighted = get<3>(GetParam());
  1261. Backend backendId = get<0>(get<4>(GetParam()));
  1262. Target targetId = get<1>(get<4>(GetParam()));
  1263. #if defined(INF_ENGINE_RELEASE) && INF_ENGINE_VER_MAJOR_EQ(2021040000)
  1264. // accuracy
  1265. if (backendId == DNN_BACKEND_INFERENCE_ENGINE_NGRAPH && targetId == DNN_TARGET_OPENCL &&
  1266. inSize == Vec3i(1, 4, 5) && op == "sum" && numConv == 1 && !weighted)
  1267. applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_OPENCL, CV_TEST_TAG_DNN_SKIP_IE_NGRAPH, CV_TEST_TAG_DNN_SKIP_IE_VERSION);
  1268. if (backendId == DNN_BACKEND_INFERENCE_ENGINE_NGRAPH && targetId == DNN_TARGET_OPENCL &&
  1269. inSize == Vec3i(2, 8, 6) && op == "sum" && numConv == 1 && !weighted)
  1270. applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_OPENCL, CV_TEST_TAG_DNN_SKIP_IE_NGRAPH, CV_TEST_TAG_DNN_SKIP_IE_VERSION);
  1271. #endif
  1272. #if defined(INF_ENGINE_RELEASE) && INF_ENGINE_VER_MAJOR_LE(2018050000)
  1273. if (backendId == DNN_BACKEND_INFERENCE_ENGINE_NN_BUILDER_2019 && targetId == DNN_TARGET_MYRIAD &&
  1274. inSize == Vec3i(1, 4, 5))
  1275. applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_MYRIAD, CV_TEST_TAG_DNN_SKIP_IE_NN_BUILDER, CV_TEST_TAG_DNN_SKIP_IE_VERSION);
  1276. #endif
  1277. #if defined(INF_ENGINE_RELEASE) && INF_ENGINE_VER_MAJOR_GE(2019010000) && INF_ENGINE_VER_MAJOR_LT(2021040000)
  1278. if (backendId == DNN_BACKEND_INFERENCE_ENGINE_NN_BUILDER_2019 && numConv > 1)
  1279. applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_NN_BUILDER, CV_TEST_TAG_DNN_SKIP_IE_VERSION);
  1280. #endif
  1281. #if defined(INF_ENGINE_RELEASE) && INF_ENGINE_VER_MAJOR_LT(2021040000)
  1282. if (backendId == DNN_BACKEND_INFERENCE_ENGINE_NN_BUILDER_2019 && targetId == DNN_TARGET_OPENCL &&
  1283. op == "sum" && numConv == 1 && !weighted)
  1284. applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_OPENCL, CV_TEST_TAG_DNN_SKIP_IE_NN_BUILDER);
  1285. #endif
  1286. #if defined(INF_ENGINE_RELEASE) && INF_ENGINE_VER_MAJOR_LT(2021040000)
  1287. if (backendId == DNN_BACKEND_INFERENCE_ENGINE_NGRAPH && numConv > 1)
  1288. applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_NGRAPH, CV_TEST_TAG_DNN_SKIP_IE_VERSION);
  1289. #endif
  1290. bool convInputShift = 1;
  1291. int numEltwiseInputs = numConv;
  1292. if (op == "div")
  1293. {
  1294. numConv = 1;
  1295. convInputShift = 0; // first input is convolution
  1296. }
  1297. Net net;
  1298. std::vector<int> convLayerIds(numConv);
  1299. for (int i = 0; i < numConv; ++i)
  1300. {
  1301. int sz[] = {inSize[0], inSize[0], 1, 1};
  1302. Mat weights(4, &sz[0], CV_32F);
  1303. randu(weights, -1.0f, 1.0f);
  1304. LayerParams convParam;
  1305. convParam.set("kernel_w", 1);
  1306. convParam.set("kernel_h", 1);
  1307. convParam.set("num_output", inSize[0]);
  1308. convParam.set("bias_term", false);
  1309. convParam.type = "Convolution";
  1310. std::ostringstream ss;
  1311. ss << "convLayer" << i;
  1312. convParam.name = ss.str();
  1313. convParam.blobs.push_back(weights);
  1314. convLayerIds[i] = net.addLayer(convParam.name, convParam.type, convParam);
  1315. net.connect(0, 0, convLayerIds[i], 0);
  1316. }
  1317. LayerParams eltwiseParam;
  1318. eltwiseParam.set("operation", op);
  1319. if (op == "sum" && weighted)
  1320. {
  1321. RNG& rng = cv::theRNG();
  1322. std::vector<float> coeff(1 + numConv);
  1323. for (int i = 0; i < coeff.size(); ++i)
  1324. {
  1325. coeff[i] = rng.uniform(-2.0f, 2.0f);
  1326. }
  1327. eltwiseParam.set("coeff", DictValue::arrayReal<float*>(&coeff[0], coeff.size()));
  1328. }
  1329. eltwiseParam.type = "Eltwise";
  1330. eltwiseParam.name = "testLayer";
  1331. int eltwiseId = net.addLayer(eltwiseParam.name, eltwiseParam.type, eltwiseParam);
  1332. if (convInputShift == 1)
  1333. net.connect(0, 0, eltwiseId, 0);
  1334. for (int i = 0; i < numConv; ++i)
  1335. {
  1336. net.connect(convLayerIds[i], 0, eltwiseId, i + convInputShift);
  1337. }
  1338. if (convInputShift == 0)
  1339. net.connect(0, 0, eltwiseId, numConv);
  1340. for (int i = numConv; i < numEltwiseInputs; ++i)
  1341. {
  1342. net.connect(0, 0, eltwiseId, i + 1);
  1343. }
  1344. int sz[] = {1, inSize[0], inSize[1], inSize[2]};
  1345. Mat input(4, &sz[0], CV_32F);
  1346. if (op == "div")
  1347. randu(input, 1.0f, 1.0f); // ensure no divisor value has absouluate value of less than 0.5
  1348. testLayer(input, net, backendId, targetId, /*skipCheck*/false, (op == "div") ? false : true);
  1349. }
  1350. INSTANTIATE_TEST_CASE_P(Layer_Test_Backends, Eltwise, testing::Combine(
  1351. /*input size*/ testing::Values(Vec3i(1, 4, 5), Vec3i(2, 8, 6)),
  1352. /*operation*/ testing::Values("prod", "sum", "div", "max", "min"),
  1353. /*num convs*/ testing::Values(1, 2, 3),
  1354. /*weighted(for sum only)*/ testing::Bool(),
  1355. dnnBackendsAndTargets()
  1356. ));
  1357. ////////////////////////////////////////////////////////////////////////////////
  1358. // Element-wise layers
  1359. ////////////////////////////////////////////////////////////////////////////////
  1360. using NaryEltwiseConcat = TestWithParam<tuple<std::vector<int>, tuple<Backend, Target>>>;
  1361. TEST_P(NaryEltwiseConcat, Accuracy) {
  1362. auto param = GetParam();
  1363. std::vector<int> input_shape = get<0>(param);
  1364. auto backend_id = get<0>(get<1>(param));
  1365. auto target_id = get<1>(get<1>(param));
  1366. /* Build the following net:
  1367. <1x4x84>
  1368. /
  1369. [Input] -+-> Mul(B<1x84>) -> Concat(axis=1) -> [Output]
  1370. | |
  1371. +-> Sigmoid ----------+
  1372. */
  1373. Net net;
  1374. std::vector<int> mul_B_shape(input_shape.size() - 1, 1);
  1375. mul_B_shape.back() = input_shape.back();
  1376. Mat mul_B(mul_B_shape, CV_32FC1);
  1377. randn(mul_B, 0.f, 1.f);
  1378. LayerParams mul_B_lp;
  1379. mul_B_lp.name = "mul_B";
  1380. mul_B_lp.type = "Const";
  1381. mul_B_lp.blobs.push_back(mul_B);
  1382. int id_mul_B = net.addLayer(mul_B_lp.name, mul_B_lp.type, mul_B_lp);
  1383. LayerParams mul_lp;
  1384. mul_lp.name = "mul";
  1385. mul_lp.type = "NaryEltwise";
  1386. mul_lp.set("operation", "mul");
  1387. int id_mul = net.addLayer(mul_lp.name, mul_lp.type, mul_lp);
  1388. net.connect(0, 0, id_mul, 0);
  1389. net.connect(id_mul_B, 0, id_mul, 1);
  1390. LayerParams sigmoid_lp;
  1391. sigmoid_lp.name = "sigmoid";
  1392. sigmoid_lp.type = "Sigmoid";
  1393. int id_sigmoid = net.addLayer(sigmoid_lp.name, sigmoid_lp.type, sigmoid_lp);
  1394. net.connect(0, 0, id_sigmoid, 0);
  1395. LayerParams concat_lp;
  1396. concat_lp.name = "concat";
  1397. concat_lp.type = "Concat";
  1398. concat_lp.set("axis", 1);
  1399. int id_concat = net.addLayer(concat_lp.name, concat_lp.type, concat_lp);
  1400. net.connect(id_mul, 0, id_concat, 0);
  1401. net.connect(id_sigmoid, 0, id_concat, 1);
  1402. // Run test
  1403. Mat input(input_shape, CV_32FC1);
  1404. testLayer(input, net, backend_id, target_id, false);
  1405. }
  1406. INSTANTIATE_TEST_CASE_P(Layer_Test_Backends, NaryEltwiseConcat, testing::Combine(
  1407. testing::Values(std::vector<int>{1, 4, 84}),
  1408. dnnBackendsAndTargets())
  1409. );
  1410. INSTANTIATE_TEST_CASE_P(/*nothing*/, Test_layers_backends, dnnBackendsAndTargets());
  1411. }} // namespace