test_layers.cpp 88 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614
  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. //
  13. // Copyright (C) 2017, Intel Corporation, all rights reserved.
  14. // Third party copyrights are property of their respective owners.
  15. //
  16. // Redistribution and use in source and binary forms, with or without modification,
  17. // are permitted provided that the following conditions are met:
  18. //
  19. // * Redistribution's of source code must retain the above copyright notice,
  20. // this list of conditions and the following disclaimer.
  21. //
  22. // * Redistribution's in binary form must reproduce the above copyright notice,
  23. // this list of conditions and the following disclaimer in the documentation
  24. // and/or other materials provided with the distribution.
  25. //
  26. // * The name of the copyright holders may not be used to endorse or promote products
  27. // derived from this software without specific prior written permission.
  28. //
  29. // This software is provided by the copyright holders and contributors "as is" and
  30. // any express or implied warranties, including, but not limited to, the implied
  31. // warranties of merchantability and fitness for a particular purpose are disclaimed.
  32. // In no event shall the Intel Corporation or contributors be liable for any direct,
  33. // indirect, incidental, special, exemplary, or consequential damages
  34. // (including, but not limited to, procurement of substitute goods or services;
  35. // loss of use, data, or profits; or business interruption) however caused
  36. // and on any theory of liability, whether in contract, strict liability,
  37. // or tort (including negligence or otherwise) arising in any way out of
  38. // the use of this software, even if advised of the possibility of such damage.
  39. //
  40. //M*/
  41. #include "test_precomp.hpp"
  42. #include <opencv2/core/ocl.hpp>
  43. #include "npy_blob.hpp"
  44. #include <opencv2/dnn/shape_utils.hpp>
  45. #include <opencv2/dnn/all_layers.hpp>
  46. #include <opencv2/dnn/layer.details.hpp> // CV_DNN_REGISTER_LAYER_CLASS
  47. #ifdef HAVE_INF_ENGINE
  48. #include <thread>
  49. #endif
  50. namespace opencv_test { namespace {
  51. template<typename TString>
  52. static String _tf(TString filename)
  53. {
  54. String basetestdir = getOpenCVExtraDir();
  55. size_t len = basetestdir.size();
  56. if(len > 0 && basetestdir[len-1] != '/' && basetestdir[len-1] != '\\')
  57. return (basetestdir + "/dnn/layers") + filename;
  58. return (basetestdir + "dnn/layers/") + filename;
  59. }
  60. void runLayer(Ptr<Layer> layer, std::vector<Mat> &inpBlobs, std::vector<Mat> &outBlobs)
  61. {
  62. size_t ninputs = inpBlobs.size();
  63. std::vector<Mat> inp(ninputs), outp, intp;
  64. std::vector<MatShape> inputs, outputs, internals;
  65. for (size_t i = 0; i < ninputs; i++)
  66. {
  67. inp[i] = inpBlobs[i].clone();
  68. inputs.push_back(shape(inp[i]));
  69. }
  70. layer->getMemoryShapes(inputs, 0, outputs, internals);
  71. for (size_t i = 0; i < outputs.size(); i++)
  72. {
  73. outp.push_back(Mat(outputs[i], CV_32F));
  74. }
  75. for (size_t i = 0; i < internals.size(); i++)
  76. {
  77. intp.push_back(Mat(internals[i], CV_32F));
  78. }
  79. layer->finalize(inp, outp);
  80. layer->forward(inp, outp, intp);
  81. size_t noutputs = outp.size();
  82. outBlobs.resize(noutputs);
  83. for (size_t i = 0; i < noutputs; i++)
  84. outBlobs[i] = outp[i];
  85. }
  86. class Test_Caffe_layers : public DNNTestLayer
  87. {
  88. public:
  89. void testLayerUsingCaffeModels(const String& basename, bool useCaffeModel = false,
  90. bool useCommonInputBlob = true, double l1 = 0.0, double lInf = 0.0,
  91. int numInps = 1, int numOuts = 1)
  92. {
  93. CV_Assert_N(numInps >= 1, numInps <= 10, numOuts >= 1, numOuts <= 10);
  94. String prototxt = _tf(basename + ".prototxt");
  95. String caffemodel = _tf(basename + ".caffemodel");
  96. std::vector<Mat> inps, refs, outs;
  97. if (numInps > 1)
  98. {
  99. for (int i = 0; i < numInps; i++)
  100. {
  101. String inpfile = _tf(basename + cv::format(".input_%d.npy", i));
  102. inps.push_back(blobFromNPY(inpfile));
  103. }
  104. }
  105. else
  106. {
  107. String inpfile = (useCommonInputBlob) ? _tf("blob.npy") : _tf(basename + ".input.npy");
  108. inps.push_back(blobFromNPY(inpfile));
  109. }
  110. if (numOuts > 1)
  111. {
  112. for (int i = 0; i < numOuts; i++)
  113. {
  114. String outfile = _tf(basename + cv::format("_%d.npy", i));
  115. refs.push_back(blobFromNPY(outfile));
  116. }
  117. }
  118. else
  119. {
  120. String outfile = _tf(basename + ".npy");
  121. refs.push_back(blobFromNPY(outfile));
  122. }
  123. Net net = readNetFromCaffe(prototxt, (useCaffeModel) ? caffemodel : String());
  124. ASSERT_FALSE(net.empty());
  125. checkBackend(&inps[0], &refs[0]);
  126. net.setPreferableBackend(backend);
  127. net.setPreferableTarget(target);
  128. String inp_name = "input";
  129. if (numInps > 1)
  130. {
  131. for (int i = 0; i < numInps; i++)
  132. {
  133. net.setInput(inps[i], inp_name + cv::format("_%d", i));
  134. }
  135. }
  136. else
  137. {
  138. net.setInput(inps.back(), inp_name);
  139. }
  140. net.forward(outs);
  141. for (int i = 0; i < refs.size(); i++)
  142. {
  143. normAssert(refs[i], outs[i], "", l1 ? l1 : default_l1, lInf ? lInf : default_lInf);
  144. }
  145. }
  146. };
  147. TEST_P(Test_Caffe_layers, Softmax)
  148. {
  149. testLayerUsingCaffeModels("layer_softmax");
  150. }
  151. TEST_P(Test_Caffe_layers, LRN)
  152. {
  153. double l1 = 0.0, lInf = 0.0;
  154. // The OpenCL kernels use the native_ math functions which have
  155. // implementation defined accuracy, so we use relaxed thresholds. See
  156. // https://github.com/opencv/opencv/issues/9821 for more details.
  157. if (target == DNN_TARGET_OPENCL)
  158. {
  159. l1 = 0.01;
  160. lInf = 0.01;
  161. }
  162. testLayerUsingCaffeModels("layer_lrn_spatial", false, true, l1, lInf);
  163. testLayerUsingCaffeModels("layer_lrn_channels", false, true, l1, lInf);
  164. }
  165. TEST_P(Test_Caffe_layers, Convolution)
  166. {
  167. testLayerUsingCaffeModels("layer_convolution", true);
  168. }
  169. TEST_P(Test_Caffe_layers, DeConvolution)
  170. {
  171. if(target == DNN_TARGET_CUDA_FP16)
  172. applyTestTag(CV_TEST_TAG_DNN_SKIP_CUDA_FP16);
  173. testLayerUsingCaffeModels("layer_deconvolution", true, false);
  174. }
  175. TEST_P(Test_Caffe_layers, InnerProduct)
  176. {
  177. #if defined(INF_ENGINE_RELEASE) && INF_ENGINE_VER_MAJOR_LT(2021040000)
  178. if (backend == DNN_BACKEND_INFERENCE_ENGINE_NN_BUILDER_2019)
  179. applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_NN_BUILDER);
  180. if (backend == DNN_BACKEND_INFERENCE_ENGINE_NGRAPH)
  181. applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_NGRAPH);
  182. #endif
  183. #if defined(INF_ENGINE_RELEASE) && INF_ENGINE_VER_MAJOR_EQ(2021040000)
  184. // IE exception: Ngraph operation Reshape with name Reshape_4219609 has dynamic output shape on 0 port, but CPU plug-in supports only static shape
  185. if (backend == DNN_BACKEND_INFERENCE_ENGINE_NGRAPH && (target == DNN_TARGET_OPENCL || target == DNN_TARGET_OPENCL_FP16))
  186. applyTestTag(target == DNN_TARGET_OPENCL ? CV_TEST_TAG_DNN_SKIP_IE_OPENCL : CV_TEST_TAG_DNN_SKIP_IE_OPENCL_FP16,
  187. CV_TEST_TAG_DNN_SKIP_IE_NGRAPH, CV_TEST_TAG_DNN_SKIP_IE_VERSION
  188. );
  189. #endif
  190. if (backend == DNN_BACKEND_OPENCV && target == DNN_TARGET_OPENCL_FP16)
  191. applyTestTag(CV_TEST_TAG_DNN_SKIP_OPENCL_FP16);
  192. testLayerUsingCaffeModels("layer_inner_product", true);
  193. }
  194. TEST_P(Test_Caffe_layers, Pooling_max)
  195. {
  196. testLayerUsingCaffeModels("layer_pooling_max");
  197. }
  198. TEST_P(Test_Caffe_layers, Pooling_ave)
  199. {
  200. testLayerUsingCaffeModels("layer_pooling_ave");
  201. }
  202. TEST_P(Test_Caffe_layers, MVN)
  203. {
  204. if(backend == DNN_BACKEND_CUDA)
  205. applyTestTag(CV_TEST_TAG_DNN_SKIP_CUDA); /* MVN is unsupported */
  206. testLayerUsingCaffeModels("layer_mvn");
  207. }
  208. void testReshape(const MatShape& inputShape, const MatShape& targetShape,
  209. int axis = 0, int num_axes = -1,
  210. MatShape mask = MatShape())
  211. {
  212. LayerParams params;
  213. params.set("axis", axis);
  214. params.set("num_axes", num_axes);
  215. if (!mask.empty())
  216. {
  217. params.set("dim", DictValue::arrayInt<int*>(&mask[0], mask.size()));
  218. }
  219. Mat inp(inputShape.size(), &inputShape[0], CV_32F);
  220. std::vector<Mat> inpVec(1, inp);
  221. std::vector<Mat> outVec, intVec;
  222. Ptr<Layer> rl = LayerFactory::createLayerInstance("Reshape", params);
  223. runLayer(rl, inpVec, outVec);
  224. Mat& out = outVec[0];
  225. MatShape shape(out.size.p, out.size.p + out.dims);
  226. EXPECT_EQ(shape, targetShape);
  227. }
  228. TEST(Layer_Test_Reshape, Accuracy)
  229. {
  230. {
  231. int inp[] = {4, 3, 1, 2};
  232. int out[] = {4, 3, 2};
  233. testReshape(MatShape(inp, inp + 4), MatShape(out, out + 3), 2, 1);
  234. }
  235. {
  236. int inp[] = {1, 128, 4, 4};
  237. int out[] = {1, 2048};
  238. int mask[] = {-1, 2048};
  239. testReshape(MatShape(inp, inp + 4), MatShape(out, out + 2), 0, -1,
  240. MatShape(mask, mask + 2));
  241. }
  242. {
  243. int inp[] = {1, 2, 3};
  244. int out[] = {3, 1, 2};
  245. int mask[] = {3, 1, 2};
  246. testReshape(MatShape(inp, inp + 3), MatShape(out, out + 3), 0, -1,
  247. MatShape(mask, mask + 3));
  248. }
  249. }
  250. TEST_P(Test_Caffe_layers, BatchNorm)
  251. {
  252. testLayerUsingCaffeModels("layer_batch_norm", true);
  253. testLayerUsingCaffeModels("layer_batch_norm_local_stats", true, false);
  254. }
  255. TEST_P(Test_Caffe_layers, ReLU)
  256. {
  257. testLayerUsingCaffeModels("layer_relu");
  258. }
  259. TEST_P(Test_Caffe_layers, Dropout)
  260. {
  261. testLayerUsingCaffeModels("layer_dropout");
  262. }
  263. TEST_P(Test_Caffe_layers, Concat)
  264. {
  265. #if defined(INF_ENGINE_RELEASE)
  266. #if INF_ENGINE_VER_MAJOR_GE(2019010000) && INF_ENGINE_VER_MAJOR_LT(2019020000)
  267. if (backend == DNN_BACKEND_INFERENCE_ENGINE_NN_BUILDER_2019 && target == DNN_TARGET_MYRIAD)
  268. applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_MYRIAD, CV_TEST_TAG_DNN_SKIP_IE_NN_BUILDER, CV_TEST_TAG_DNN_SKIP_IE_VERSION);
  269. #elif INF_ENGINE_VER_MAJOR_EQ(2019020000)
  270. if (backend == DNN_BACKEND_INFERENCE_ENGINE_NN_BUILDER_2019 &&
  271. (target == DNN_TARGET_OPENCL || target == DNN_TARGET_OPENCL_FP16))
  272. applyTestTag(target == DNN_TARGET_OPENCL ? CV_TEST_TAG_DNN_SKIP_IE_OPENCL : CV_TEST_TAG_DNN_SKIP_IE_OPENCL_FP16,
  273. CV_TEST_TAG_DNN_SKIP_IE_NN_BUILDER, CV_TEST_TAG_DNN_SKIP_IE_VERSION);
  274. #endif
  275. #if INF_ENGINE_VER_MAJOR_LT(2021040000)
  276. if (backend == DNN_BACKEND_INFERENCE_ENGINE_NGRAPH &&
  277. (target == DNN_TARGET_OPENCL || target == DNN_TARGET_OPENCL_FP16))
  278. applyTestTag(target == DNN_TARGET_OPENCL ? CV_TEST_TAG_DNN_SKIP_IE_OPENCL : CV_TEST_TAG_DNN_SKIP_IE_OPENCL_FP16,
  279. CV_TEST_TAG_DNN_SKIP_IE_NGRAPH, CV_TEST_TAG_DNN_SKIP_IE_VERSION);
  280. #endif
  281. #endif
  282. testLayerUsingCaffeModels("layer_concat");
  283. testLayerUsingCaffeModels("layer_concat_optim", true, false);
  284. testLayerUsingCaffeModels("layer_concat_shared_input", true, false);
  285. }
  286. TEST_P(Test_Caffe_layers, Fused_Concat)
  287. {
  288. if (backend == DNN_BACKEND_INFERENCE_ENGINE_NN_BUILDER_2019 && (target == DNN_TARGET_OPENCL || target == DNN_TARGET_OPENCL_FP16))
  289. applyTestTag(target == DNN_TARGET_OPENCL ? CV_TEST_TAG_DNN_SKIP_IE_OPENCL : CV_TEST_TAG_DNN_SKIP_IE_OPENCL_FP16,
  290. CV_TEST_TAG_DNN_SKIP_IE_NN_BUILDER, CV_TEST_TAG_DNN_SKIP_IE_VERSION);
  291. checkBackend();
  292. // Test case
  293. // input
  294. // |
  295. // v
  296. // some_layer
  297. // | |
  298. // v v
  299. // concat
  300. Net net;
  301. int interLayer;
  302. {
  303. LayerParams lp;
  304. lp.type = "AbsVal";
  305. lp.name = "someLayer";
  306. interLayer = net.addLayerToPrev(lp.name, lp.type, lp);
  307. }
  308. {
  309. LayerParams lp;
  310. lp.set("axis", 1);
  311. lp.type = "Concat";
  312. lp.name = "testConcat";
  313. int id = net.addLayer(lp.name, lp.type, lp);
  314. net.connect(interLayer, 0, id, 0);
  315. net.connect(interLayer, 0, id, 1);
  316. }
  317. int shape[] = {1, 2, 3, 4};
  318. Mat input(4, shape, CV_32F);
  319. randu(input, 0.0f, 1.0f); // [0, 1] to make AbsVal an identity transformation.
  320. net.setInput(input);
  321. net.setPreferableBackend(backend);
  322. net.setPreferableTarget(target);
  323. Mat out = net.forward();
  324. normAssert(slice(out, Range::all(), Range(0, 2), Range::all(), Range::all()), input, "", default_l1, default_lInf);
  325. normAssert(slice(out, Range::all(), Range(2, 4), Range::all(), Range::all()), input, "", default_l1, default_lInf);
  326. }
  327. TEST_P(Test_Caffe_layers, Eltwise)
  328. {
  329. if (backend == DNN_BACKEND_INFERENCE_ENGINE_NN_BUILDER_2019 && target == DNN_TARGET_MYRIAD)
  330. applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_MYRIAD);
  331. testLayerUsingCaffeModels("layer_eltwise");
  332. }
  333. TEST_P(Test_Caffe_layers, PReLU)
  334. {
  335. double lInf = (target == DNN_TARGET_MYRIAD || target == DNN_TARGET_OPENCL_FP16) ? 0.021 : 0.0;
  336. testLayerUsingCaffeModels("layer_prelu", true, true, 0.0, lInf);
  337. }
  338. // TODO: fix an unstable test case
  339. TEST_P(Test_Caffe_layers, layer_prelu_fc)
  340. {
  341. if (backend == DNN_BACKEND_OPENCV && target == DNN_TARGET_OPENCL_FP16)
  342. applyTestTag(CV_TEST_TAG_DNN_SKIP_OPENCL_FP16);
  343. // Reference output values are in range [-0.0001, 10.3906]
  344. double l1 = (target == DNN_TARGET_MYRIAD) ? 0.005 : 0.0;
  345. double lInf = (target == DNN_TARGET_MYRIAD) ? 0.021 : 0.0;
  346. #if defined(INF_ENGINE_RELEASE) && INF_ENGINE_VER_MAJOR_GE(2020040000)
  347. if (backend == DNN_BACKEND_INFERENCE_ENGINE_NGRAPH && target == DNN_TARGET_OPENCL)
  348. {
  349. l1 = 0.006f; lInf = 0.05f;
  350. }
  351. if (backend == DNN_BACKEND_INFERENCE_ENGINE_NGRAPH && target == DNN_TARGET_OPENCL_FP16)
  352. {
  353. l1 = 0.01f; lInf = 0.05f;
  354. }
  355. #endif
  356. testLayerUsingCaffeModels("layer_prelu_fc", true, false, l1, lInf);
  357. }
  358. TEST_P(Test_Caffe_layers, Reshape_Split_Slice)
  359. {
  360. if (backend == DNN_BACKEND_INFERENCE_ENGINE_NN_BUILDER_2019)
  361. applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_NN_BUILDER);
  362. if (backend == DNN_BACKEND_INFERENCE_ENGINE_NGRAPH)
  363. applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_NGRAPH);
  364. Net net = readNetFromCaffe(_tf("reshape_and_slice_routines.prototxt"));
  365. ASSERT_FALSE(net.empty());
  366. net.setPreferableBackend(backend);
  367. net.setPreferableTarget(target);
  368. Mat input(6, 12, CV_32F);
  369. RNG rng(0);
  370. rng.fill(input, RNG::UNIFORM, -1, 1);
  371. net.setInput(input, "input");
  372. Mat output = net.forward("output");
  373. normAssert(input, output, "", default_l1, default_lInf);
  374. }
  375. TEST_P(Test_Caffe_layers, Conv_Elu)
  376. {
  377. #if defined(INF_ENGINE_RELEASE) && INF_ENGINE_RELEASE <= 2018050000
  378. if (backend == DNN_BACKEND_INFERENCE_ENGINE_NN_BUILDER_2019 && target == DNN_TARGET_MYRIAD)
  379. applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_MYRIAD, CV_TEST_TAG_DNN_SKIP_IE_VERSION);
  380. #endif
  381. Net net = readNetFromTensorflow(_tf("layer_elu_model.pb"));
  382. ASSERT_FALSE(net.empty());
  383. Mat inp = blobFromNPY(_tf("layer_elu_in.npy"));
  384. Mat ref = blobFromNPY(_tf("layer_elu_out.npy"));
  385. net.setInput(inp, "input");
  386. net.setPreferableBackend(backend);
  387. net.setPreferableTarget(target);
  388. Mat out = net.forward();
  389. double l1 = default_l1, lInf = default_lInf;
  390. if (target == DNN_TARGET_CUDA_FP16)
  391. {
  392. l1 = 0.0002;
  393. lInf = 0.0005;
  394. }
  395. normAssert(ref, out, "", l1, lInf);
  396. }
  397. class Layer_LSTM_Test : public ::testing::Test
  398. {
  399. public:
  400. int numInp, numOut;
  401. Mat Wh, Wx, b, h, c;
  402. Ptr<LSTMLayer> layer;
  403. std::vector<Mat> inputs, outputs;
  404. Layer_LSTM_Test() {}
  405. void init(const MatShape &inpShape_, const MatShape &outShape_,
  406. bool produceCellOutput, bool useTimestampDim)
  407. {
  408. numInp = total(inpShape_);
  409. numOut = total(outShape_);
  410. Wh = Mat::ones(4 * numOut, numOut, CV_32F);
  411. Wx = Mat::ones(4 * numOut, numInp, CV_32F);
  412. b = Mat::ones(4 * numOut, 1, CV_32F);
  413. h = Mat::ones(4, numOut, CV_32F);
  414. c = Mat::ones(4, numOut, CV_32F);
  415. LayerParams lp;
  416. lp.blobs.resize(5);
  417. lp.blobs[0] = Wh;
  418. lp.blobs[1] = Wx;
  419. lp.blobs[2] = b;
  420. lp.blobs[3] = h;
  421. lp.blobs[4] = c;
  422. lp.set<bool>("produce_cell_output", produceCellOutput);
  423. lp.set<bool>("use_timestamp_dim", useTimestampDim);
  424. layer = LSTMLayer::create(lp);
  425. layer->setOutShape(outShape_);
  426. }
  427. };
  428. TEST_F(Layer_LSTM_Test, get_set_test)
  429. {
  430. const int TN = 4;
  431. MatShape inpShape = shape(5, 3, 2);
  432. MatShape outShape = shape(3, 1, 2);
  433. MatShape inpResShape = concat(shape(TN), inpShape);
  434. MatShape outResShape = concat(shape(TN), outShape);
  435. init(inpShape, outShape, true, false);
  436. layer->setOutShape(outShape);
  437. Mat C((int)outResShape.size(), &outResShape[0], CV_32F);
  438. randu(C, -1., 1.);
  439. Mat H = C.clone();
  440. randu(H, -1., 1.);
  441. Mat inp((int)inpResShape.size(), &inpResShape[0], CV_32F);
  442. randu(inp, -1., 1.);
  443. inputs.push_back(inp);
  444. runLayer(layer, inputs, outputs);
  445. EXPECT_EQ(2u, outputs.size());
  446. print(outResShape, "outResShape");
  447. print(shape(outputs[0]), "out0");
  448. print(shape(outputs[0]), "out1");
  449. EXPECT_EQ(outResShape, shape(outputs[0]));
  450. EXPECT_EQ(outResShape, shape(outputs[1]));
  451. EXPECT_EQ(0, layer->inputNameToIndex("x"));
  452. EXPECT_EQ(0, layer->outputNameToIndex("h"));
  453. EXPECT_EQ(1, layer->outputNameToIndex("c"));
  454. }
  455. TEST(Layer_LSTM_Test_Accuracy_with_, CaffeRecurrent)
  456. {
  457. LayerParams lp;
  458. lp.blobs.resize(5);
  459. lp.blobs[0] = blobFromNPY(_tf("lstm.prototxt.w_2.npy")); // Wh
  460. lp.blobs[1] = blobFromNPY(_tf("lstm.prototxt.w_0.npy")); // Wx
  461. lp.blobs[2] = blobFromNPY(_tf("lstm.prototxt.w_1.npy")); // bias
  462. lp.blobs[3] = Mat::zeros(2, 17, CV_32F); // h_0
  463. lp.blobs[4] = Mat::zeros(2, 17, CV_32F); // c_0
  464. Ptr<LSTMLayer> layer = LSTMLayer::create(lp);
  465. Mat inp = blobFromNPY(_tf("recurrent.input.npy"));
  466. std::vector<Mat> inputs(1, inp), outputs;
  467. runLayer(layer, inputs, outputs);
  468. Mat h_t_reference = blobFromNPY(_tf("lstm.prototxt.h_1.npy"));
  469. normAssert(h_t_reference, outputs[0]);
  470. }
  471. TEST(Layer_LSTM_Test_Accuracy_with_, HiddenParams)
  472. {
  473. Mat Wx = blobFromNPY(_tf("lstm.hidden.W.npy"));
  474. Mat Wh = blobFromNPY(_tf("lstm.hidden.R.npy"));
  475. Mat b = blobFromNPY(_tf("lstm.hidden.B.npy"));
  476. Mat h0 = blobFromNPY(_tf("lstm.hidden.h0.npy"));
  477. Mat c0 = blobFromNPY(_tf("lstm.hidden.c0.npy"));
  478. const int numHidden = 3;
  479. const int numDirs = Wx.size[0];
  480. const int numFeatures = Wx.size[2];
  481. b = b.reshape(1, b.size[0]);
  482. Mat bx = b.colRange(0, b.cols / 2);
  483. Mat bh = b.colRange(b.cols / 2, b.cols);
  484. b = bx + bh;
  485. // IFGO->IGFO
  486. for (int k = 0; k < numDirs; ++k)
  487. {
  488. float* WxData = Wx.ptr<float>(k);
  489. float* WhData = Wh.ptr<float>(k);
  490. float* biasData = b.ptr<float>(k);
  491. for (int j = 0; j < numHidden; ++j)
  492. {
  493. for (int i = 0; i < numFeatures; ++i)
  494. {
  495. std::swap(WxData[(numHidden + j) * numFeatures + i],
  496. WxData[(numHidden * 2 + j) * numFeatures + i]);
  497. }
  498. for (int i = 0; i < numHidden; ++i)
  499. {
  500. std::swap(WhData[(numHidden + j) * numHidden + i],
  501. WhData[(numHidden * 2 + j) * numHidden + i]);
  502. }
  503. std::swap(biasData[numHidden + j], biasData[numHidden * 2 + j]);
  504. }
  505. }
  506. Wx = Wx.reshape(1, Wx.size[0] * Wx.size[1]);
  507. Wh = Wh.reshape(1, Wh.size[0] * Wh.size[1]);
  508. h0 = h0.reshape(1, h0.size[0] * h0.size[1]);
  509. c0 = c0.reshape(1, c0.size[0] * c0.size[1]);
  510. LayerParams lstmParams;
  511. lstmParams.blobs.resize(5);
  512. lstmParams.blobs[0] = Wh;
  513. lstmParams.blobs[1] = Wx;
  514. lstmParams.blobs[2] = b;
  515. lstmParams.blobs[3] = h0;
  516. lstmParams.blobs[4] = c0;
  517. lstmParams.set("bidirectional", false);
  518. Ptr<LSTMLayer> layer = LSTMLayer::create(lstmParams);
  519. Mat inp = blobFromNPY(_tf("lstm.hidden.input.npy"));
  520. std::vector<Mat> inputs(1, inp), outputs;
  521. runLayer(layer, inputs, outputs);
  522. Mat h_t_reference = blobFromNPY(_tf("lstm.hidden.output.npy"));
  523. normAssert(h_t_reference, outputs[0]);
  524. }
  525. TEST(Layer_GRU_Test_Accuracy_with_, Pytorch)
  526. {
  527. Mat Wx = blobFromNPY(_tf("gru.W.npy"));
  528. Mat Wh = blobFromNPY(_tf("gru.R.npy"));
  529. Mat b = blobFromNPY(_tf("gru.B.npy"));
  530. Mat h0 = blobFromNPY(_tf("gru.h0.npy"));
  531. Wx = Wx.reshape(1, Wx.size[0] * Wx.size[1]);
  532. Wh = Wh.reshape(1, Wh.size[0] * Wh.size[1]);
  533. h0 = h0.reshape(1, h0.size[0] * h0.size[1]);
  534. b = b.reshape(1, b.size[0]);
  535. LayerParams gruParams;
  536. gruParams.blobs.resize(4);
  537. gruParams.blobs[0] = Wh;
  538. gruParams.blobs[1] = Wx;
  539. gruParams.blobs[2] = b;
  540. gruParams.blobs[3] = h0;
  541. gruParams.set("bidirectional", false);
  542. Ptr<GRULayer> layer = GRULayer::create(gruParams);
  543. Mat inp = blobFromNPY(_tf("gru.input.npy"));
  544. std::vector<Mat> inputs(1, inp), outputs;
  545. runLayer(layer, inputs, outputs);
  546. Mat h_t_reference = blobFromNPY(_tf("gru.output.npy"));
  547. normAssert(h_t_reference, outputs[0]);
  548. }
  549. TEST(Layer_RNN_Test_Accuracy_with_, CaffeRecurrent)
  550. {
  551. Ptr<RNNLayer> layer = RNNLayer::create(LayerParams());
  552. layer->setWeights(
  553. blobFromNPY(_tf("rnn.prototxt.w_0.npy")),
  554. blobFromNPY(_tf("rnn.prototxt.w_1.npy")),
  555. blobFromNPY(_tf("rnn.prototxt.w_2.npy")),
  556. blobFromNPY(_tf("rnn.prototxt.w_3.npy")),
  557. blobFromNPY(_tf("rnn.prototxt.w_4.npy")) );
  558. std::vector<Mat> output, input(1, blobFromNPY(_tf("recurrent.input.npy")));
  559. runLayer(layer, input, output);
  560. Mat h_ref = blobFromNPY(_tf("rnn.prototxt.h_1.npy"));
  561. normAssert(h_ref, output[0]);
  562. }
  563. TEST(Layer_LSTM_Test_Accuracy_, Reverse)
  564. {
  565. // This handcrafted setup calculates (approximately) the prefix sum of the
  566. // input, assuming the inputs are suitably small.
  567. cv::Mat input(2, 1, CV_32FC1);
  568. input.at<float>(0, 0) = 1e-5f;
  569. input.at<float>(1, 0) = 2e-5f;
  570. cv::Mat Wx(4, 1, CV_32FC1);
  571. Wx.at<float>(0, 0) = 0.f; // Input gate
  572. Wx.at<float>(1, 0) = 0.f; // Forget gate
  573. Wx.at<float>(2, 0) = 0.f; // Output gate
  574. Wx.at<float>(3, 0) = 1.f; // Update signal
  575. cv::Mat Wh(4, 1, CV_32FC1);
  576. Wh.at<float>(0, 0) = 0.f; // Input gate
  577. Wh.at<float>(1, 0) = 0.f; // Forget gate
  578. Wh.at<float>(2, 0) = 0.f; // Output gate
  579. Wh.at<float>(3, 0) = 0.f; // Update signal
  580. cv::Mat bias(4, 1, CV_32FC1);
  581. bias.at<float>(0, 0) = 1e10f; // Input gate - always allows input to c
  582. bias.at<float>(1, 0) = 1e10f; // Forget gate - never forget anything on c
  583. bias.at<float>(2, 0) = 1e10f; // Output gate - always output everything
  584. bias.at<float>(3, 0) = 0.f; // Update signal
  585. cv::Mat hInternal = cv::Mat::zeros(1, 1, CV_32FC1);
  586. cv::Mat cInternal = cv::Mat::zeros(1, 1, CV_32FC1);
  587. LayerParams lp;
  588. lp.set("reverse", true);
  589. lp.set("use_timestamp_dim", true);
  590. lp.blobs.clear();
  591. lp.blobs.push_back(Wh);
  592. lp.blobs.push_back(Wx);
  593. lp.blobs.push_back(bias);
  594. lp.blobs.push_back(hInternal);
  595. lp.blobs.push_back(cInternal);
  596. cv::Ptr<cv::dnn::LSTMLayer> layer = LSTMLayer::create(lp);
  597. std::vector<cv::Mat> outputs;
  598. std::vector<cv::Mat> inputs;
  599. inputs.push_back(input);
  600. runLayer(layer, inputs, outputs);
  601. ASSERT_EQ(1, outputs.size());
  602. cv::Mat out = outputs[0];
  603. ASSERT_EQ(3, out.dims);
  604. ASSERT_EQ(shape(2, 1, 1), shape(out));
  605. float* data = reinterpret_cast<float*>(out.data);
  606. EXPECT_NEAR(std::tanh(1e-5f) + std::tanh(2e-5f), data[0], 1e-10);
  607. EXPECT_NEAR(std::tanh(2e-5f), data[1], 1e-10);
  608. }
  609. class Layer_RNN_Test : public ::testing::Test
  610. {
  611. public:
  612. int nX, nH, nO, nT, nS;
  613. Mat Whh, Wxh, bh, Who, bo;
  614. Ptr<RNNLayer> layer;
  615. std::vector<Mat> inputs, outputs;
  616. Layer_RNN_Test()
  617. {
  618. nT = 3;
  619. nS = 5;
  620. nX = 31;
  621. nH = 64;
  622. nO = 100;
  623. Whh = Mat::ones(nH, nH, CV_32F);
  624. Wxh = Mat::ones(nH, nX, CV_32F);
  625. bh = Mat::ones(nH, 1, CV_32F);
  626. Who = Mat::ones(nO, nH, CV_32F);
  627. bo = Mat::ones(nO, 1, CV_32F);
  628. layer = RNNLayer::create(LayerParams());
  629. layer->setProduceHiddenOutput(true);
  630. layer->setWeights(Wxh, bh, Whh, Who, bo);
  631. }
  632. };
  633. TEST_F(Layer_RNN_Test, get_set_test)
  634. {
  635. int sz[] = { nT, nS, 1, nX };
  636. Mat inp(4, sz, CV_32F);
  637. randu(inp, -1., 1.);
  638. inputs.push_back(inp);
  639. runLayer(layer, inputs, outputs);
  640. EXPECT_EQ(outputs.size(), 2u);
  641. EXPECT_EQ(shape(outputs[0]), shape(nT, nS, nO));
  642. EXPECT_EQ(shape(outputs[1]), shape(nT, nS, nH));
  643. }
  644. TEST_P(Test_Caffe_layers, Accum)
  645. {
  646. if (backend == DNN_BACKEND_OPENCV && target != DNN_TARGET_CPU)
  647. applyTestTag(CV_TEST_TAG_DNN_SKIP_OPENCL, CV_TEST_TAG_DNN_SKIP_OPENCL_FP16);
  648. testLayerUsingCaffeModels("accum", false, false, 0.0, 0.0, 2);
  649. testLayerUsingCaffeModels("accum_ref", false, false, 0.0, 0.0, 2);
  650. }
  651. TEST_P(Test_Caffe_layers, FlowWarp)
  652. {
  653. if (backend == DNN_BACKEND_OPENCV && target == DNN_TARGET_OPENCL_FP16)
  654. applyTestTag(CV_TEST_TAG_DNN_SKIP_OPENCL_FP16);
  655. testLayerUsingCaffeModels("flow_warp", false, false, 0.0, 0.0, 2);
  656. }
  657. TEST_P(Test_Caffe_layers, ChannelNorm)
  658. {
  659. if (backend == DNN_BACKEND_OPENCV && target == DNN_TARGET_OPENCL_FP16)
  660. applyTestTag(CV_TEST_TAG_DNN_SKIP_OPENCL_FP16);
  661. testLayerUsingCaffeModels("channel_norm", false, false);
  662. }
  663. TEST_P(Test_Caffe_layers, DataAugmentation)
  664. {
  665. if (backend == DNN_BACKEND_OPENCV && target == DNN_TARGET_OPENCL_FP16)
  666. applyTestTag(CV_TEST_TAG_DNN_SKIP_OPENCL_FP16);
  667. testLayerUsingCaffeModels("data_augmentation", true, false);
  668. testLayerUsingCaffeModels("data_augmentation_2x1", true, false);
  669. testLayerUsingCaffeModels("data_augmentation_8x6", true, false);
  670. }
  671. TEST_P(Test_Caffe_layers, Resample)
  672. {
  673. if (backend != DNN_BACKEND_OPENCV)
  674. applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_NN_BUILDER, CV_TEST_TAG_DNN_SKIP_IE_NGRAPH);
  675. testLayerUsingCaffeModels("nearest_2inps", false, false, 0.0, 0.0, 2);
  676. testLayerUsingCaffeModels("nearest", false, false);
  677. }
  678. TEST_P(Test_Caffe_layers, Correlation)
  679. {
  680. if (backend == DNN_BACKEND_OPENCV && target == DNN_TARGET_OPENCL_FP16)
  681. applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_NGRAPH, CV_TEST_TAG_DNN_SKIP_IE_NN_BUILDER,
  682. CV_TEST_TAG_DNN_SKIP_OPENCL, CV_TEST_TAG_DNN_SKIP_OPENCL_FP16);
  683. testLayerUsingCaffeModels("correlation", false, false, 0.0, 0.0, 2);
  684. }
  685. TEST_P(Test_Caffe_layers, Convolution2Inputs)
  686. {
  687. testLayerUsingCaffeModels("conv_2_inps", true, false, 0.0, 0.0, 2);
  688. }
  689. TEST_P(Test_Caffe_layers, ROIPooling_Accuracy)
  690. {
  691. Net net = readNetFromCaffe(_tf("net_roi_pooling.prototxt"));
  692. ASSERT_FALSE(net.empty());
  693. Mat inp = blobFromNPY(_tf("net_roi_pooling.input.npy"));
  694. Mat rois = blobFromNPY(_tf("net_roi_pooling.rois.npy"));
  695. Mat ref = blobFromNPY(_tf("net_roi_pooling.npy"));
  696. checkBackend(&inp, &ref);
  697. net.setPreferableBackend(backend);
  698. net.setPreferableTarget(target);
  699. net.setInput(inp, "input");
  700. net.setInput(rois, "rois");
  701. Mat out = net.forward();
  702. double l1 = (target == DNN_TARGET_OPENCL_FP16 || target == DNN_TARGET_MYRIAD) ? 1e-3 : 1e-5;
  703. double lInf = (target == DNN_TARGET_OPENCL_FP16 || target == DNN_TARGET_MYRIAD) ? 1e-3 : 1e-4;
  704. if (target == DNN_TARGET_CUDA_FP16)
  705. {
  706. l1 = 2e-4;
  707. lInf = 9e-4;
  708. }
  709. normAssert(out, ref, "", l1, lInf);
  710. }
  711. TEST_P(Test_Caffe_layers, FasterRCNN_Proposal)
  712. {
  713. if (backend == DNN_BACKEND_OPENCV && target == DNN_TARGET_OPENCL_FP16)
  714. applyTestTag(CV_TEST_TAG_DNN_SKIP_OPENCL_FP16);
  715. if (backend == DNN_BACKEND_INFERENCE_ENGINE_NN_BUILDER_2019)
  716. applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_NN_BUILDER);
  717. if (backend == DNN_BACKEND_INFERENCE_ENGINE_NGRAPH)
  718. applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_NGRAPH);
  719. if(backend == DNN_BACKEND_CUDA)
  720. applyTestTag(CV_TEST_TAG_DNN_SKIP_CUDA); /* Proposal layer is unsupported */
  721. Net net = readNetFromCaffe(_tf("net_faster_rcnn_proposal.prototxt"));
  722. Mat scores = blobFromNPY(_tf("net_faster_rcnn_proposal.scores.npy"));
  723. Mat deltas = blobFromNPY(_tf("net_faster_rcnn_proposal.deltas.npy"));
  724. Mat imInfo = (Mat_<float>(1, 3) << 600, 800, 1.6f);
  725. net.setInput(scores, "rpn_cls_prob_reshape");
  726. net.setInput(deltas, "rpn_bbox_pred");
  727. net.setInput(imInfo, "im_info");
  728. std::vector<Mat> outs;
  729. net.setPreferableBackend(backend);
  730. net.setPreferableTarget(target);
  731. net.forward(outs, "output");
  732. for (int i = 0; i < 2; ++i)
  733. {
  734. Mat ref = blobFromNPY(_tf(i == 0 ? "net_faster_rcnn_proposal.out_rois.npy" :
  735. "net_faster_rcnn_proposal.out_scores.npy"));
  736. const int numDets = ref.size[0];
  737. EXPECT_LE(numDets, outs[i].size[0]);
  738. normAssert(outs[i].rowRange(0, numDets), ref);
  739. if (numDets < outs[i].size[0])
  740. {
  741. EXPECT_EQ(countNonZero(outs[i].rowRange(numDets, outs[i].size[0])), 0);
  742. }
  743. }
  744. }
  745. typedef testing::TestWithParam<tuple<Vec4i, Vec2i, bool> > Scale_untrainable;
  746. TEST_P(Scale_untrainable, Accuracy)
  747. {
  748. Vec4i inpShapeVec = get<0>(GetParam());
  749. int axis = get<1>(GetParam())[0];
  750. int weightsDims = get<1>(GetParam())[1];
  751. bool testFusion = get<2>(GetParam());
  752. const int inpShape[] = {inpShapeVec[0], inpShapeVec[1], inpShapeVec[2], inpShapeVec[3]};
  753. // Create a network with two inputs. Scale layer multiplies a first input to
  754. // a second one. See http://caffe.berkeleyvision.org/tutorial/layers/scale.html
  755. Net net;
  756. // Check that this version of Scale layer won't be fused with Convolution layer.
  757. if (testFusion)
  758. {
  759. LayerParams lp;
  760. lp.set("kernel_size", 1);
  761. lp.set("num_output", 3);
  762. lp.set("group", 3);
  763. lp.set("bias_term", false);
  764. lp.type = "Convolution";
  765. lp.name = "testConv";
  766. std::vector<int> weightsShape(4);
  767. weightsShape[0] = 3; // #outChannels
  768. weightsShape[1] = 1; // #inpChannels / group
  769. weightsShape[2] = 1; // height
  770. weightsShape[3] = 1; // width
  771. Mat weights(weightsShape, CV_32F);
  772. weights.setTo(1);
  773. lp.blobs.push_back(weights);
  774. net.addLayerToPrev(lp.name, lp.type, lp);
  775. }
  776. LayerParams lp;
  777. lp.type = "Scale";
  778. lp.name = "testLayer";
  779. lp.set("axis", axis);
  780. int id = net.addLayerToPrev(lp.name, lp.type, lp);
  781. net.connect(0, 1, id, 1);
  782. Mat input(4, inpShape, CV_32F);
  783. Mat weights(weightsDims, &inpShape[axis], CV_32F);
  784. randu(input, -1, 1);
  785. randu(weights, -1, 1);
  786. std::vector<String> inpNames(2);
  787. inpNames[0] = "scale_input";
  788. inpNames[1] = "scale_weights";
  789. net.setInputsNames(inpNames);
  790. net.setInput(input, inpNames[0]);
  791. net.setInput(weights, inpNames[1]);
  792. net.setPreferableBackend(DNN_BACKEND_OPENCV);
  793. Mat out = net.forward();
  794. Mat ref(input.dims, input.size, CV_32F);
  795. float* inpData = (float*)input.data;
  796. float* refData = (float*)ref.data;
  797. float* weightsData = (float*)weights.data;
  798. int spatialSize = 1;
  799. for (int i = axis + weightsDims; i < 4; ++i)
  800. spatialSize *= inpShape[i];
  801. for (int i = 0; i < ref.total(); ++i)
  802. {
  803. float w = weightsData[(i / spatialSize) % weights.total()];
  804. refData[i] = inpData[i] * w;
  805. }
  806. normAssert(out, ref);
  807. }
  808. INSTANTIATE_TEST_CASE_P(Layer_Test, Scale_untrainable, Combine(
  809. /*input size*/ Values(Vec4i(2, 3, 4, 5)),
  810. /*axis, #dims*/ Values(Vec2i(0, 1), Vec2i(0, 2), Vec2i(0, 3), Vec2i(0, 4),
  811. Vec2i(1, 1), Vec2i(1, 2), Vec2i(1, 3),
  812. Vec2i(2, 1), Vec2i(2, 2),
  813. Vec2i(3, 1)),
  814. /*conv fusion*/ testing::Bool()
  815. ));
  816. typedef testing::TestWithParam<tuple<Vec4i, Vec4i, int, int, int> > Crop;
  817. TEST_P(Crop, Accuracy)
  818. {
  819. Vec4i inpShapeVec = get<0>(GetParam());
  820. Vec4i sizShapeVec = get<1>(GetParam());
  821. int axis = get<2>(GetParam());
  822. int numOffsets = get<3>(GetParam());
  823. int offsetVal = get<4>(GetParam());
  824. const int inpShape[] = {inpShapeVec[0], inpShapeVec[1], inpShapeVec[2], inpShapeVec[3]};
  825. const int sizShape[] = {sizShapeVec[0], sizShapeVec[1], sizShapeVec[2], sizShapeVec[3]};
  826. // Create a network with two inputs. Crop layer crops a first input to
  827. // the size of a second one.
  828. // See http://caffe.berkeleyvision.org/tutorial/layers/crop.html
  829. Net net;
  830. LayerParams lp;
  831. lp.name = "testCrop";
  832. lp.type = "Crop";
  833. lp.set("axis", axis);
  834. if (numOffsets > 0)
  835. {
  836. std::vector<int> offsets(numOffsets, offsetVal);
  837. lp.set("offset", DictValue::arrayInt<int*>(&offsets[0], offsets.size()));
  838. }
  839. else
  840. offsetVal = 0;
  841. int id = net.addLayerToPrev(lp.name, lp.type, lp);
  842. net.connect(0, 1, id, 1);
  843. Mat inpImage(4, inpShape, CV_32F);
  844. Mat sizImage(4, sizShape, CV_32F);
  845. randu(inpImage, -1, 1);
  846. randu(sizImage, -1, 1);
  847. std::vector<String> inpNames(2);
  848. inpNames[0] = "cropImage";
  849. inpNames[1] = "sizImage";
  850. net.setInputsNames(inpNames);
  851. net.setInput(inpImage, inpNames[0]);
  852. net.setInput(sizImage, inpNames[1]);
  853. net.setPreferableBackend(DNN_BACKEND_OPENCV);
  854. // There are a few conditions that represent invalid input to the crop
  855. // layer, so in those cases we want to verify an exception is thrown.
  856. bool shouldThrowException = false;
  857. if (numOffsets > 1 && numOffsets != 4 - axis)
  858. shouldThrowException = true;
  859. else
  860. for (int i = axis; i < 4; i++)
  861. if (sizShape[i] + offsetVal > inpShape[i])
  862. shouldThrowException = true;
  863. Mat out;
  864. if (shouldThrowException)
  865. {
  866. ASSERT_ANY_THROW(out = net.forward());
  867. return;
  868. }
  869. else
  870. out = net.forward();
  871. // Finally, compare the cropped output blob from the DNN layer (out)
  872. // to a reference blob (ref) that we compute here.
  873. std::vector<Range> crop_range;
  874. crop_range.resize(4, Range::all());
  875. for (int i = axis; i < 4; i++)
  876. crop_range[i] = Range(offsetVal, sizShape[i] + offsetVal);
  877. Mat ref(sizImage.dims, sizImage.size, CV_32F);
  878. inpImage(&crop_range[0]).copyTo(ref);
  879. normAssert(out, ref);
  880. }
  881. INSTANTIATE_TEST_CASE_P(Layer_Test, Crop, Combine(
  882. /*input blob shape*/ Values(Vec4i(1, 3, 20, 30)),
  883. /*cropsize blob shape*/ Values(Vec4i(1, 3, 10, 12)),
  884. /*start axis*/ Values(0, 1, 2),
  885. /*number of offsets*/ Values(0, 1, 2, 4),
  886. /*offset value*/ Values(3, 4)
  887. ));
  888. // Check that by default average pooling layer should not count zero padded values
  889. // into the normalization area.
  890. TEST_P(Test_Caffe_layers, Average_pooling_kernel_area)
  891. {
  892. LayerParams lp;
  893. lp.name = "testAvePool";
  894. lp.type = "Pooling";
  895. lp.set("kernel_size", 2);
  896. lp.set("stride", 2);
  897. lp.set("pool", "AVE");
  898. Net net;
  899. net.addLayerToPrev(lp.name, lp.type, lp);
  900. // 1 2 | 3
  901. // 4 5 | 6
  902. // ----+--
  903. // 7 8 | 9
  904. Mat inp = (Mat_<float>(3, 3) << 1, 2, 3, 4, 5, 6, 7, 8, 9);
  905. Mat ref = (Mat_<float>(2, 2) << (1 + 2 + 4 + 5) / 4.f, (3 + 6) / 2.f, (7 + 8) / 2.f, 9);
  906. Mat tmp = blobFromImage(inp);
  907. net.setInput(blobFromImage(inp));
  908. net.setPreferableBackend(backend);
  909. net.setPreferableTarget(target);
  910. Mat out = net.forward();
  911. normAssert(out, blobFromImage(ref));
  912. }
  913. TEST_P(Test_Caffe_layers, PriorBox_repeated)
  914. {
  915. Net net = readNet(_tf("prior_box.prototxt"));
  916. int inp_size[] = {1, 3, 10, 10};
  917. int shape_size[] = {1, 2, 3, 4};
  918. Mat inp(4, inp_size, CV_32F);
  919. randu(inp, -1.0f, 1.0f);
  920. Mat shape(4, shape_size, CV_32F);
  921. randu(shape, -1.0f, 1.0f);
  922. net.setInput(inp, "data");
  923. net.setInput(shape, "shape");
  924. net.setPreferableBackend(backend);
  925. net.setPreferableTarget(target);
  926. Mat out = net.forward();
  927. Mat ref = blobFromNPY(_tf("priorbox_output.npy"));
  928. double l1 = (target == DNN_TARGET_OPENCL_FP16 || target == DNN_TARGET_MYRIAD) ? 1e-3 : 1e-5;
  929. double lInf = (target == DNN_TARGET_OPENCL_FP16 || target == DNN_TARGET_MYRIAD) ? 1e-3 : 1e-4;
  930. if (target == DNN_TARGET_CUDA_FP16)
  931. {
  932. l1 = 7e-5;
  933. lInf = 0.0005;
  934. }
  935. normAssert(out, ref, "", l1, lInf);
  936. }
  937. // Test PriorBoxLayer in case of no aspect ratios (just squared proposals).
  938. TEST_P(Test_Caffe_layers, PriorBox_squares)
  939. {
  940. if (backend == DNN_BACKEND_INFERENCE_ENGINE_NN_BUILDER_2019 && target == DNN_TARGET_MYRIAD)
  941. applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_MYRIAD, CV_TEST_TAG_DNN_SKIP_IE_NN_BUILDER);
  942. if (backend == DNN_BACKEND_INFERENCE_ENGINE_NGRAPH && target == DNN_TARGET_MYRIAD)
  943. applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_MYRIAD, CV_TEST_TAG_DNN_SKIP_IE_NGRAPH);
  944. LayerParams lp;
  945. lp.name = "testPriorBox";
  946. lp.type = "PriorBox";
  947. lp.set("min_size", 2);
  948. lp.set("flip", true);
  949. lp.set("clip", true);
  950. float variance[] = {0.1f, 0.1f, 0.2f, 0.2f};
  951. float aspectRatios[] = {1.0f}; // That should be ignored.
  952. lp.set("variance", DictValue::arrayReal<float*>(&variance[0], 4));
  953. lp.set("aspect_ratio", DictValue::arrayReal<float*>(&aspectRatios[0], 1));
  954. Net net;
  955. int id = net.addLayerToPrev(lp.name, lp.type, lp);
  956. net.connect(0, 0, id, 1); // The second input is an input image. Shapes are used for boxes normalization.
  957. Mat inp(1, 2, CV_32F);
  958. randu(inp, -1, 1);
  959. net.setInput(blobFromImage(inp));
  960. net.setPreferableBackend(backend);
  961. net.setPreferableTarget(target);
  962. Mat out = net.forward();
  963. Mat ref = (Mat_<float>(4, 4) << 0.0, 0.0, 0.75, 1.0,
  964. 0.25, 0.0, 1.0, 1.0,
  965. 0.1f, 0.1f, 0.2f, 0.2f,
  966. 0.1f, 0.1f, 0.2f, 0.2f);
  967. double l1 = 1e-5;
  968. if (target == DNN_TARGET_OPENCL_FP16 || target == DNN_TARGET_MYRIAD || target == DNN_TARGET_CUDA_FP16)
  969. l1 = 2e-5;
  970. normAssert(out.reshape(1, 4), ref, "", l1);
  971. }
  972. typedef TestWithParam<tuple<int, int> > Layer_Test_DWconv_Prelu;
  973. TEST_P(Layer_Test_DWconv_Prelu, Accuracy)
  974. {
  975. // Test case
  976. // input img size 3x16x16 value all 1
  977. // |
  978. // v
  979. // dw_conv weight[0]=-1 weight[1]=-2 weight[2]=-3 bias={1,2,3}
  980. // |
  981. // v
  982. // prelu weight={1,2,3}
  983. // |
  984. // v
  985. // output out size 3x14x14 if right: out[0]=-8 out[0]=-32 out[0]=-72
  986. // but current opencv output: out[0]=-24 out[0]=-48 out[0]=-72
  987. const int num_input = get<0>(GetParam()); //inpChannels
  988. const int group = 3; //outChannels=group when group>1
  989. const int num_output = get<1>(GetParam());
  990. const int kernel_depth = num_input/group;
  991. CV_Assert_N(num_output >= group, num_output % group == 0, num_input % group == 0);
  992. Net net;
  993. //layer 1: dwconv
  994. LayerParams lp;
  995. lp.name = "dwconv";
  996. lp.type = "Convolution";
  997. lp.set("kernel_size", 3);
  998. lp.set("num_output", num_output);
  999. lp.set("pad", 0);
  1000. lp.set("group", group);
  1001. lp.set("stride", 1);
  1002. lp.set("engine", "CAFFE");
  1003. lp.set("bias_term", "true");
  1004. std::vector<int> weightsShape(4);
  1005. weightsShape[0] = num_output; // #outChannels
  1006. weightsShape[1] = kernel_depth; // #inpChannels / group
  1007. weightsShape[2] = 3; // height
  1008. weightsShape[3] = 3; // width
  1009. Mat weights(weightsShape, CV_32F, Scalar(1));
  1010. //assign weights
  1011. for (int i = 0; i < weightsShape[0]; ++i)
  1012. {
  1013. for (int j = 0; j < weightsShape[1]; ++j)
  1014. {
  1015. for (int k = 0; k < weightsShape[2]; ++k)
  1016. {
  1017. for (int l = 0; l < weightsShape[3]; ++l)
  1018. {
  1019. weights.ptr<float>(i, j, k)[l]=-1*(i+1);
  1020. }
  1021. }
  1022. }
  1023. }
  1024. lp.blobs.push_back(weights);
  1025. //assign bias
  1026. Mat bias(1, num_output, CV_32F, Scalar(1));
  1027. for (int i = 0; i < 1; ++i)
  1028. {
  1029. for (int j = 0; j < num_output; ++j)
  1030. {
  1031. bias.ptr<float>(i)[j]=j+1;
  1032. }
  1033. }
  1034. lp.blobs.push_back(bias);
  1035. net.addLayerToPrev(lp.name, lp.type, lp);
  1036. //layer 2: prelu
  1037. LayerParams lpr;
  1038. lpr.name = "dw_relu";
  1039. lpr.type = "PReLU";
  1040. Mat weightsp(1, num_output, CV_32F, Scalar(1));
  1041. //assign weights
  1042. for (int i = 0; i < 1; ++i)
  1043. {
  1044. for (int j = 0; j < num_output; ++j)
  1045. {
  1046. weightsp.ptr<float>(i)[j]=j+1;
  1047. }
  1048. }
  1049. lpr.blobs.push_back(weightsp);
  1050. net.addLayerToPrev(lpr.name, lpr.type, lpr);
  1051. int shape[] = {1, num_input, 16, 16};
  1052. Mat in_blob(4, &shape[0], CV_32FC1, Scalar(1));
  1053. net.setPreferableBackend(DNN_BACKEND_OPENCV);
  1054. net.setInput(in_blob);
  1055. Mat out = net.forward();
  1056. //assign target
  1057. std::vector<int> outShape(4);
  1058. outShape[0] = 1;
  1059. outShape[1] = num_output; // outChannels
  1060. outShape[2] = 14; // height
  1061. outShape[3] = 14; // width
  1062. Mat target(outShape, CV_32F, Scalar(1));
  1063. for (int i = 0; i < outShape[0]; ++i)
  1064. {
  1065. for (int j = 0; j < outShape[1]; ++j)
  1066. {
  1067. for (int k = 0; k < outShape[2]; ++k)
  1068. {
  1069. for (int l = 0; l < outShape[3]; ++l)
  1070. {
  1071. target.ptr<float>(i, j, k)[l]=(-9*kernel_depth*(j+1)+j+1)*(j+1);
  1072. }
  1073. }
  1074. }
  1075. }
  1076. normAssert(out, target);
  1077. }
  1078. INSTANTIATE_TEST_CASE_P(/**/, Layer_Test_DWconv_Prelu, Combine(Values(3, 6), Values(3, 6)));
  1079. #ifdef HAVE_INF_ENGINE
  1080. // Using Intel's Model Optimizer generate .xml and .bin files:
  1081. // ./ModelOptimizer -w /path/to/caffemodel -d /path/to/prototxt \
  1082. // -p FP32 -i -b ${batch_size} -o /path/to/output/folder
  1083. typedef testing::TestWithParam<tuple<Backend, Target> > Layer_Test_Convolution_DLDT;
  1084. TEST_P(Layer_Test_Convolution_DLDT, Accuracy)
  1085. {
  1086. const Backend backendId = get<0>(GetParam());
  1087. const Target targetId = get<1>(GetParam());
  1088. if (backendId == DNN_BACKEND_INFERENCE_ENGINE_NN_BUILDER_2019 && targetId == DNN_TARGET_MYRIAD)
  1089. applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_MYRIAD, CV_TEST_TAG_DNN_SKIP_IE_NN_BUILDER);
  1090. if (backendId != DNN_BACKEND_INFERENCE_ENGINE_NN_BUILDER_2019 && backendId != DNN_BACKEND_INFERENCE_ENGINE_NGRAPH)
  1091. throw SkipTestException("No support for async forward");
  1092. if (backendId == DNN_BACKEND_INFERENCE_ENGINE_NN_BUILDER_2019)
  1093. setInferenceEngineBackendType(CV_DNN_BACKEND_INFERENCE_ENGINE_NN_BUILDER_API);
  1094. else if (backendId == DNN_BACKEND_INFERENCE_ENGINE_NGRAPH)
  1095. setInferenceEngineBackendType(CV_DNN_BACKEND_INFERENCE_ENGINE_NGRAPH);
  1096. else
  1097. FAIL() << "Unknown backendId";
  1098. Net netDefault = readNet(_tf("layer_convolution.caffemodel"), _tf("layer_convolution.prototxt"));
  1099. Net net = readNet(_tf("layer_convolution.xml"), _tf("layer_convolution.bin"));
  1100. Mat inp = blobFromNPY(_tf("blob.npy"));
  1101. netDefault.setInput(inp);
  1102. netDefault.setPreferableBackend(DNN_BACKEND_OPENCV);
  1103. Mat outDefault = netDefault.forward();
  1104. net.setInput(inp);
  1105. net.setPreferableBackend(backendId);
  1106. net.setPreferableTarget(targetId);
  1107. Mat out = net.forward();
  1108. double l1 = (targetId == DNN_TARGET_OPENCL_FP16 || targetId == DNN_TARGET_MYRIAD) ? 1.5e-3 : 1e-5;
  1109. double lInf = (targetId == DNN_TARGET_OPENCL_FP16 || targetId == DNN_TARGET_MYRIAD) ? 1.8e-2 : 1e-4;
  1110. normAssert(outDefault, out, "", l1, lInf);
  1111. std::vector<int> outLayers = net.getUnconnectedOutLayers();
  1112. ASSERT_EQ(net.getLayer(outLayers[0])->name, "output");
  1113. if (backendId == DNN_BACKEND_INFERENCE_ENGINE_NN_BUILDER_2019)
  1114. ASSERT_EQ(net.getLayer(outLayers[0])->type, "Convolution");
  1115. else
  1116. ASSERT_EQ(net.getLayer(outLayers[0])->type, "Add");
  1117. }
  1118. TEST_P(Layer_Test_Convolution_DLDT, setInput_uint8)
  1119. {
  1120. const Backend backendId = get<0>(GetParam());
  1121. const Target targetId = get<1>(GetParam());
  1122. if (backendId == DNN_BACKEND_INFERENCE_ENGINE_NN_BUILDER_2019 && targetId == DNN_TARGET_MYRIAD)
  1123. applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_MYRIAD, CV_TEST_TAG_DNN_SKIP_IE_NN_BUILDER);
  1124. if (backendId != DNN_BACKEND_INFERENCE_ENGINE_NN_BUILDER_2019 && backendId != DNN_BACKEND_INFERENCE_ENGINE_NGRAPH)
  1125. throw SkipTestException("No support for async forward");
  1126. if (backendId == DNN_BACKEND_INFERENCE_ENGINE_NN_BUILDER_2019)
  1127. setInferenceEngineBackendType(CV_DNN_BACKEND_INFERENCE_ENGINE_NN_BUILDER_API);
  1128. else if (backendId == DNN_BACKEND_INFERENCE_ENGINE_NGRAPH)
  1129. setInferenceEngineBackendType(CV_DNN_BACKEND_INFERENCE_ENGINE_NGRAPH);
  1130. else
  1131. FAIL() << "Unknown backendId";
  1132. int blobSize[] = {2, 6, 75, 113};
  1133. Mat inputs[] = {Mat(4, &blobSize[0], CV_8U), Mat()};
  1134. randu(inputs[0], 0, 255);
  1135. inputs[0].convertTo(inputs[1], CV_32F);
  1136. Mat outs[2];
  1137. for (int i = 0; i < 2; ++i)
  1138. {
  1139. Net net = readNet(_tf("layer_convolution.xml"), _tf("layer_convolution.bin"));
  1140. net.setPreferableBackend(backendId);
  1141. net.setPreferableTarget(targetId);
  1142. net.setInput(inputs[i]);
  1143. outs[i] = net.forward();
  1144. ASSERT_EQ(outs[i].type(), CV_32F);
  1145. }
  1146. if (targetId != DNN_TARGET_MYRIAD)
  1147. normAssert(outs[0], outs[1]);
  1148. }
  1149. TEST_P(Layer_Test_Convolution_DLDT, multithreading)
  1150. {
  1151. const Backend backendId = get<0>(GetParam());
  1152. const Target targetId = get<1>(GetParam());
  1153. if (backendId == DNN_BACKEND_INFERENCE_ENGINE_NN_BUILDER_2019 && targetId == DNN_TARGET_MYRIAD)
  1154. applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_MYRIAD, CV_TEST_TAG_DNN_SKIP_IE_NN_BUILDER);
  1155. if (backendId != DNN_BACKEND_INFERENCE_ENGINE_NN_BUILDER_2019 && backendId != DNN_BACKEND_INFERENCE_ENGINE_NGRAPH)
  1156. throw SkipTestException("No support for async forward");
  1157. if (backendId == DNN_BACKEND_INFERENCE_ENGINE_NN_BUILDER_2019)
  1158. setInferenceEngineBackendType(CV_DNN_BACKEND_INFERENCE_ENGINE_NN_BUILDER_API);
  1159. else if (backendId == DNN_BACKEND_INFERENCE_ENGINE_NGRAPH)
  1160. setInferenceEngineBackendType(CV_DNN_BACKEND_INFERENCE_ENGINE_NGRAPH);
  1161. else
  1162. FAIL() << "Unknown backendId";
  1163. std::string xmlPath = _tf("layer_convolution.xml");
  1164. std::string binPath = _tf("layer_convolution.bin");
  1165. Net firstNet = readNet(xmlPath, binPath);
  1166. Net secondNet = readNet(xmlPath, binPath);
  1167. Mat inp = blobFromNPY(_tf("blob.npy"));
  1168. firstNet.setInput(inp);
  1169. secondNet.setInput(inp);
  1170. firstNet.setPreferableBackend(backendId);
  1171. firstNet.setPreferableTarget(targetId);
  1172. secondNet.setPreferableBackend(backendId);
  1173. secondNet.setPreferableTarget(targetId);
  1174. Mat out1, out2;
  1175. std::thread t1([&]{out1 = firstNet.forward();});
  1176. std::thread t2([&]{out2 = secondNet.forward();});
  1177. t1.join();
  1178. t2.join();
  1179. Mat ref = blobFromNPY(_tf("layer_convolution.npy"));
  1180. double l1 = (targetId == DNN_TARGET_OPENCL_FP16 || targetId == DNN_TARGET_MYRIAD) ? 1.5e-3 : 1e-5;
  1181. double lInf = (targetId == DNN_TARGET_OPENCL_FP16 || targetId == DNN_TARGET_MYRIAD) ? 1.8e-2 : 1e-4;
  1182. normAssert(out1, ref, "first thread", l1, lInf);
  1183. normAssert(out2, ref, "second thread", l1, lInf);
  1184. }
  1185. INSTANTIATE_TEST_CASE_P(/**/, Layer_Test_Convolution_DLDT,
  1186. dnnBackendsAndTargetsIE()
  1187. );
  1188. // 1. Create a .prototxt file with the following network:
  1189. // layer {
  1190. // type: "Input" name: "data" top: "data"
  1191. // input_param { shape { dim: 1 dim: 2 dim: 3 } }
  1192. // }
  1193. // layer {
  1194. // type: "Input" name: "second_input" top: "second_input"
  1195. // input_param { shape { dim: 1 dim: 2 dim: 3 } }
  1196. // }
  1197. // layer {
  1198. // type: "Eltwise" name: "output" top: "output"
  1199. // bottom: "data" bottom: "second_input"
  1200. // eltwise_param { operation: SUM }
  1201. // }
  1202. //
  1203. // 2. Create a .caffemodel file using Caffe:
  1204. //
  1205. // import caffe
  1206. // net = caffe.Net('/path/to/prototxt', caffe.TEST)
  1207. // net.save('/path/to/caffemodel')
  1208. //
  1209. // 3. Convert using ModelOptimizer.
  1210. typedef testing::TestWithParam<tuple<int, int, Target, std::vector<int> > > Test_DLDT_two_inputs_3dim;
  1211. TEST_P(Test_DLDT_two_inputs_3dim, as_IR)
  1212. {
  1213. int firstInpType = get<0>(GetParam());
  1214. int secondInpType = get<1>(GetParam());
  1215. Target targetId = get<2>(GetParam());
  1216. Net net = readNet(_tf("net_two_inputs.xml"), _tf("net_two_inputs.bin"));
  1217. std::vector<int> inpSize = get<3>(GetParam());
  1218. Mat firstInp(3, inpSize.data(), firstInpType);
  1219. Mat secondInp(3, inpSize.data(), secondInpType);
  1220. randu(firstInp, 0, 255);
  1221. randu(secondInp, 0, 255);
  1222. net.setInput(firstInp, "data");
  1223. net.setInput(secondInp, "second_input");
  1224. net.setPreferableTarget(targetId);
  1225. double l1 = ((targetId == DNN_TARGET_OPENCL_FP16 || targetId == DNN_TARGET_MYRIAD) &&
  1226. (firstInpType == CV_32F || secondInpType == CV_32F)) ? 0.06 : 0.0;
  1227. double lInf = ((targetId == DNN_TARGET_OPENCL_FP16 || targetId == DNN_TARGET_MYRIAD) &&
  1228. (firstInpType == CV_32F || secondInpType == CV_32F)) ? 0.23 : 0.0;
  1229. Mat out = net.forward();
  1230. Mat ref;
  1231. cv::add(firstInp, secondInp, ref, Mat(), CV_32F);
  1232. normAssert(out, ref, "", l1, lInf);
  1233. }
  1234. std::vector< std::vector<int> > list_sizes{ {1, 2, 3}, {3, 2, 1}, {5, 5, 5}, {13, 7, 11} };
  1235. INSTANTIATE_TEST_CASE_P(/*nothing*/, Test_DLDT_two_inputs_3dim, Combine(
  1236. Values(CV_8U, CV_32F), Values(CV_8U, CV_32F),
  1237. testing::ValuesIn(getAvailableTargets(DNN_BACKEND_INFERENCE_ENGINE_NN_BUILDER_2019)),
  1238. testing::ValuesIn(list_sizes)
  1239. ));
  1240. class UnsupportedLayer : public Layer
  1241. {
  1242. public:
  1243. UnsupportedLayer(const LayerParams &params) : Layer(params) {}
  1244. static Ptr<Layer> create(const LayerParams& params)
  1245. {
  1246. return Ptr<Layer>(new UnsupportedLayer(params));
  1247. }
  1248. virtual bool supportBackend(int backendId) CV_OVERRIDE
  1249. {
  1250. return backendId == DNN_BACKEND_OPENCV;
  1251. }
  1252. virtual void forward(cv::InputArrayOfArrays inputs, cv::OutputArrayOfArrays outputs, cv::OutputArrayOfArrays internals) CV_OVERRIDE {}
  1253. };
  1254. typedef DNNTestLayer Test_DLDT_layers;
  1255. static void test_dldt_fused_output(Backend backend, Target target)
  1256. {
  1257. static const int kNumChannels = 3;
  1258. Net net;
  1259. {
  1260. LayerParams lp;
  1261. lp.set("kernel_size", 1);
  1262. lp.set("num_output", 3);
  1263. lp.set("bias_term", false);
  1264. lp.type = "Convolution";
  1265. lp.name = "testConv";
  1266. lp.blobs.push_back(Mat({kNumChannels, 1, 1, 1}, CV_32F, Scalar(1)));
  1267. net.addLayerToPrev(lp.name, lp.type, lp);
  1268. }
  1269. {
  1270. LayerParams lp;
  1271. lp.set("bias_term", false);
  1272. lp.type = "Scale";
  1273. lp.name = "testScale";
  1274. lp.blobs.push_back(Mat({kNumChannels}, CV_32F, Scalar(1)));
  1275. net.addLayerToPrev(lp.name, lp.type, lp);
  1276. }
  1277. {
  1278. LayerParams lp;
  1279. net.addLayerToPrev("unsupported_layer", "Unsupported", lp);
  1280. }
  1281. net.setPreferableBackend(backend);
  1282. net.setPreferableTarget(target);
  1283. net.setInput(Mat({1, 1, 2, 3}, CV_32FC1, Scalar(1)));
  1284. net.forward();
  1285. }
  1286. TEST_P(Test_DLDT_layers, fused_output)
  1287. {
  1288. CV_DNN_REGISTER_LAYER_CLASS(Unsupported, UnsupportedLayer);
  1289. try
  1290. {
  1291. test_dldt_fused_output(backend, target);
  1292. }
  1293. catch (const std::exception& e)
  1294. {
  1295. ADD_FAILURE() << "Exception: " << e.what();
  1296. }
  1297. catch(...)
  1298. {
  1299. ADD_FAILURE() << "Unknown exception";
  1300. }
  1301. LayerFactory::unregisterLayer("Unsupported");
  1302. }
  1303. TEST_P(Test_DLDT_layers, multiple_networks)
  1304. {
  1305. Net nets[2];
  1306. for (int i = 0; i < 2; ++i)
  1307. {
  1308. nets[i].setInputsNames(std::vector<String>(1, format("input_%d", i)));
  1309. LayerParams lp;
  1310. lp.set("kernel_size", 1);
  1311. lp.set("num_output", 1);
  1312. lp.set("bias_term", false);
  1313. lp.type = "Convolution";
  1314. lp.name = format("testConv_%d", i);
  1315. lp.blobs.push_back(Mat({1, 1, 1, 1}, CV_32F, Scalar(1 + i)));
  1316. nets[i].addLayerToPrev(lp.name, lp.type, lp);
  1317. nets[i].setPreferableBackend(backend);
  1318. nets[i].setPreferableTarget(target);
  1319. nets[i].setInput(Mat({1, 1, 2, 3}, CV_32FC1, Scalar(1)));
  1320. }
  1321. Mat out_1 = nets[0].forward();
  1322. Mat out_2 = nets[1].forward();
  1323. // After the second model is initialized we try to receive an output from the first network again.
  1324. out_1 = nets[0].forward();
  1325. normAssert(2 * out_1, out_2);
  1326. }
  1327. INSTANTIATE_TEST_CASE_P(/*nothing*/, Test_DLDT_layers, dnnBackendsAndTargets());
  1328. #endif // HAVE_INF_ENGINE
  1329. // Test a custom layer.
  1330. class CustomInterpLayer CV_FINAL : public Layer
  1331. {
  1332. public:
  1333. CustomInterpLayer(const LayerParams &params) : Layer(params)
  1334. {
  1335. zoomFactor = params.get<int>("zoom_factor", 0);
  1336. outWidth = params.get<int>("width", 0);
  1337. outHeight = params.get<int>("height", 0);
  1338. }
  1339. static Ptr<Layer> create(LayerParams& params)
  1340. {
  1341. return Ptr<Layer>(new CustomInterpLayer(params));
  1342. }
  1343. virtual bool getMemoryShapes(const std::vector<std::vector<int> > &inputs,
  1344. const int requiredOutputs,
  1345. std::vector<std::vector<int> > &outputs,
  1346. std::vector<std::vector<int> > &internals) const CV_OVERRIDE
  1347. {
  1348. const int batchSize = inputs[0][0];
  1349. const int numChannels = inputs[0][1];
  1350. const int inpHeight = inputs[0][2];
  1351. const int inpWidth = inputs[0][3];
  1352. std::vector<int> outShape(4);
  1353. outShape[0] = batchSize;
  1354. outShape[1] = numChannels;
  1355. outShape[2] = outHeight != 0 ? outHeight : (inpHeight + (inpHeight - 1) * (zoomFactor - 1));
  1356. outShape[3] = outWidth != 0 ? outWidth : (inpWidth + (inpWidth - 1) * (zoomFactor - 1));
  1357. outputs.assign(1, outShape);
  1358. return false;
  1359. }
  1360. virtual void finalize(InputArrayOfArrays, OutputArrayOfArrays outputs_arr) CV_OVERRIDE
  1361. {
  1362. std::vector<Mat> outputs;
  1363. outputs_arr.getMatVector(outputs);
  1364. if (!outWidth && !outHeight)
  1365. {
  1366. outHeight = outputs[0].size[2];
  1367. outWidth = outputs[0].size[3];
  1368. }
  1369. }
  1370. // Implementation of this custom layer is based on https://github.com/cdmh/deeplab-public/blob/master/src/caffe/layers/interp_layer.cpp
  1371. void forward(InputArrayOfArrays inputs_arr, OutputArrayOfArrays outputs_arr, OutputArrayOfArrays internals_arr) CV_OVERRIDE
  1372. {
  1373. CV_TRACE_FUNCTION();
  1374. CV_TRACE_ARG_VALUE(name, "name", name.c_str());
  1375. if (inputs_arr.depth() == CV_16S)
  1376. {
  1377. forward_fallback(inputs_arr, outputs_arr, internals_arr);
  1378. return;
  1379. }
  1380. std::vector<Mat> inputs, outputs;
  1381. inputs_arr.getMatVector(inputs);
  1382. outputs_arr.getMatVector(outputs);
  1383. Mat& inp = inputs[0];
  1384. Mat& out = outputs[0];
  1385. const float* inpData = (float*)inp.data;
  1386. float* outData = (float*)out.data;
  1387. const int batchSize = inp.size[0];
  1388. const int numChannels = inp.size[1];
  1389. const int inpHeight = inp.size[2];
  1390. const int inpWidth = inp.size[3];
  1391. const float rheight = (outHeight > 1) ? static_cast<float>(inpHeight - 1) / (outHeight - 1) : 0.f;
  1392. const float rwidth = (outWidth > 1) ? static_cast<float>(inpWidth - 1) / (outWidth - 1) : 0.f;
  1393. for (int h2 = 0; h2 < outHeight; ++h2)
  1394. {
  1395. const float h1r = rheight * h2;
  1396. const int h1 = h1r;
  1397. const int h1p = (h1 < inpHeight - 1) ? 1 : 0;
  1398. const float h1lambda = h1r - h1;
  1399. const float h0lambda = 1.f - h1lambda;
  1400. for (int w2 = 0; w2 < outWidth; ++w2)
  1401. {
  1402. const float w1r = rwidth * w2;
  1403. const int w1 = w1r;
  1404. const int w1p = (w1 < inpWidth - 1) ? 1 : 0;
  1405. const float w1lambda = w1r - w1;
  1406. const float w0lambda = 1.f - w1lambda;
  1407. const float* pos1 = inpData + h1 * inpWidth + w1;
  1408. float* pos2 = outData + h2 * outWidth + w2;
  1409. for (int c = 0; c < batchSize * numChannels; ++c)
  1410. {
  1411. pos2[0] =
  1412. h0lambda * (w0lambda * pos1[0] + w1lambda * pos1[w1p]) +
  1413. h1lambda * (w0lambda * pos1[h1p * inpWidth] + w1lambda * pos1[h1p * inpWidth + w1p]);
  1414. pos1 += inpWidth * inpHeight;
  1415. pos2 += outWidth * outHeight;
  1416. }
  1417. }
  1418. }
  1419. }
  1420. private:
  1421. int outWidth, outHeight, zoomFactor;
  1422. };
  1423. #ifndef OPENCV_DNN_EXTERNAL_PROTOBUF
  1424. TEST_P(Test_Caffe_layers, Interp)
  1425. #else
  1426. TEST_P(Test_Caffe_layers, DISABLED_Interp) // requires patched protobuf (available in OpenCV source tree only)
  1427. #endif
  1428. {
  1429. #if defined(INF_ENGINE_RELEASE) && INF_ENGINE_VER_MAJOR_EQ(2021030000)
  1430. if (backend == DNN_BACKEND_INFERENCE_ENGINE_NGRAPH && target == DNN_TARGET_MYRIAD)
  1431. applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_MYRIAD, CV_TEST_TAG_DNN_SKIP_IE_NGRAPH); // exception
  1432. #endif
  1433. if (backend == DNN_BACKEND_INFERENCE_ENGINE_NN_BUILDER_2019 && target == DNN_TARGET_MYRIAD)
  1434. applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_MYRIAD);
  1435. // Test a custom layer.
  1436. CV_DNN_REGISTER_LAYER_CLASS(Interp, CustomInterpLayer);
  1437. try
  1438. {
  1439. testLayerUsingCaffeModels("layer_interp", false, false);
  1440. }
  1441. catch (...)
  1442. {
  1443. LayerFactory::unregisterLayer("Interp");
  1444. throw;
  1445. }
  1446. LayerFactory::unregisterLayer("Interp");
  1447. // Test an implemented layer.
  1448. testLayerUsingCaffeModels("layer_interp", false, false);
  1449. }
  1450. INSTANTIATE_TEST_CASE_P(/*nothing*/, Test_Caffe_layers, dnnBackendsAndTargets());
  1451. TEST(Layer_Test_PoolingIndices, Accuracy)
  1452. {
  1453. Net net;
  1454. LayerParams lp;
  1455. lp.set("pool", "max");
  1456. lp.set("kernel_w", 2);
  1457. lp.set("kernel_h", 2);
  1458. lp.set("stride_w", 2);
  1459. lp.set("stride_h", 2);
  1460. lp.set("pad_w", 0);
  1461. lp.set("pad_h", 0);
  1462. lp.name = "testLayer.name"; // This test also checks that OpenCV lets use names with dots.
  1463. lp.type = "Pooling";
  1464. net.addLayerToPrev(lp.name, lp.type, lp);
  1465. Mat inp(10, 10, CV_8U);
  1466. randu(inp, 0, 255);
  1467. Mat maxValues(5, 5, CV_32F, Scalar(-1)), indices(5, 5, CV_32F, Scalar(-1));
  1468. for (int y = 0; y < 10; ++y)
  1469. {
  1470. int dstY = y / 2;
  1471. for (int x = 0; x < 10; ++x)
  1472. {
  1473. int dstX = x / 2;
  1474. uint8_t val = inp.at<uint8_t>(y, x);
  1475. if ((float)inp.at<uint8_t>(y, x) > maxValues.at<float>(dstY, dstX))
  1476. {
  1477. maxValues.at<float>(dstY, dstX) = val;
  1478. indices.at<float>(dstY, dstX) = y * 10 + x;
  1479. }
  1480. }
  1481. }
  1482. net.setPreferableBackend(DNN_BACKEND_OPENCV);
  1483. net.setInput(blobFromImage(inp));
  1484. std::vector<Mat> outputs;
  1485. net.forward(outputs, lp.name);
  1486. normAssert(maxValues, outputs[0].reshape(1, 5));
  1487. normAssert(indices, outputs[1].reshape(1, 5));
  1488. }
  1489. typedef testing::TestWithParam<tuple<Vec4i, int, tuple<Backend, Target> > > Layer_Test_ShuffleChannel;
  1490. TEST_P(Layer_Test_ShuffleChannel, Accuracy)
  1491. {
  1492. Vec4i inpShapeVec = get<0>(GetParam());
  1493. int group = get<1>(GetParam());
  1494. ASSERT_EQ(inpShapeVec[1] % group, 0);
  1495. const int groupSize = inpShapeVec[1] / group;
  1496. int backendId = get<0>(get<2>(GetParam()));
  1497. int targetId = get<1>(get<2>(GetParam()));
  1498. Net net;
  1499. LayerParams lp;
  1500. lp.set("group", group);
  1501. lp.type = "ShuffleChannel";
  1502. lp.name = "testLayer";
  1503. net.addLayerToPrev(lp.name, lp.type, lp);
  1504. const int inpShape[] = {inpShapeVec[0], inpShapeVec[1], inpShapeVec[2], inpShapeVec[3]};
  1505. Mat inp(4, inpShape, CV_32F);
  1506. randu(inp, 0, 255);
  1507. net.setInput(inp);
  1508. net.setPreferableBackend(backendId);
  1509. net.setPreferableTarget(targetId);
  1510. Mat out = net.forward();
  1511. double l1 = 1e-5, lInf = 1e-4;
  1512. if (targetId == DNN_TARGET_OPENCL_FP16)
  1513. {
  1514. l1 = 5e-2;
  1515. lInf = 7e-2;
  1516. }
  1517. else if (targetId == DNN_TARGET_CUDA_FP16)
  1518. {
  1519. l1 = 0.06;
  1520. lInf = 0.07;
  1521. }
  1522. for (int n = 0; n < inpShapeVec[0]; ++n)
  1523. {
  1524. for (int c = 0; c < inpShapeVec[1]; ++c)
  1525. {
  1526. Mat outChannel = getPlane(out, n, c);
  1527. Mat inpChannel = getPlane(inp, n, groupSize * (c % group) + c / group);
  1528. normAssert(outChannel, inpChannel, "", l1, lInf);
  1529. }
  1530. }
  1531. }
  1532. INSTANTIATE_TEST_CASE_P(/**/, Layer_Test_ShuffleChannel, Combine(
  1533. /*input shape*/ Values(Vec4i(1, 6, 5, 7), Vec4i(3, 12, 1, 4)),
  1534. /*group*/ Values(1, 2, 3, 6), dnnBackendsAndTargets(/*with IE*/ false)
  1535. ));
  1536. // Check if relu is not fused to convolution if we requested it's output
  1537. TEST(Layer_Test_Convolution, relu_fusion)
  1538. {
  1539. Net net;
  1540. {
  1541. LayerParams lp;
  1542. lp.set("kernel_size", 1);
  1543. lp.set("num_output", 1);
  1544. lp.set("bias_term", false);
  1545. lp.type = "Convolution";
  1546. lp.name = "testConv";
  1547. int weightsShape[] = {1, 1, 1, 1};
  1548. Mat weights(4, &weightsShape[0], CV_32F, Scalar(1));
  1549. lp.blobs.push_back(weights);
  1550. net.addLayerToPrev(lp.name, lp.type, lp);
  1551. }
  1552. {
  1553. LayerParams lp;
  1554. lp.type = "ReLU";
  1555. lp.name = "testReLU";
  1556. net.addLayerToPrev(lp.name, lp.type, lp);
  1557. }
  1558. int sz[] = {1, 1, 2, 3};
  1559. Mat input(4, &sz[0], CV_32F);
  1560. randu(input, -1.0, -0.1);
  1561. net.setInput(input);
  1562. net.setPreferableBackend(DNN_BACKEND_OPENCV);
  1563. Mat output = net.forward("testConv");
  1564. normAssert(input, output);
  1565. }
  1566. typedef testing::TestWithParam<tuple<bool, tuple<Backend, Target> > > Layer_Test_Eltwise_unequal;
  1567. TEST_P(Layer_Test_Eltwise_unequal, accuracy_input_0_truncate)
  1568. {
  1569. bool weighted = get<0>(GetParam());
  1570. int backendId = get<0>(get<1>(GetParam()));
  1571. int targetId = get<1>(get<1>(GetParam()));
  1572. if (backendId == DNN_BACKEND_CUDA && weighted)
  1573. applyTestTag(CV_TEST_TAG_DNN_SKIP_CUDA);
  1574. Net net;
  1575. LayerParams lp;
  1576. lp.type = "Eltwise";
  1577. lp.name = "testLayer";
  1578. lp.set<std::string>("output_channels_mode", "input_0_truncate");
  1579. const int inpShapes[][4] = {{1, 4, 2, 2}, {1, 5, 2, 2}, {1, 3, 2, 2}};
  1580. const int out_channels = inpShapes[0][1];
  1581. std::vector<String> inpNames(3);
  1582. std::vector<Mat> inputs(3);
  1583. std::vector<float> weights(3, 1);
  1584. if (weighted)
  1585. {
  1586. for (int i = 0; i < inputs.size(); ++i)
  1587. weights[i] = -0.125f + i * 0.25f;
  1588. lp.set("coeff", DictValue::arrayReal<float*>(&weights[0], weights.size()));
  1589. }
  1590. int eltwiseId = net.addLayer(lp.name, lp.type, lp);
  1591. for (int i = 0; i < inputs.size(); ++i)
  1592. {
  1593. inputs[i].create(4, inpShapes[i], CV_32F);
  1594. size_t total = inputs[i].total();
  1595. for (size_t j = 0; j < total; j++)
  1596. inputs[i].ptr<float>()[j] = j + i * 100;
  1597. inpNames[i] = format("input_%d", i);
  1598. net.connect(0, i, eltwiseId, i);
  1599. }
  1600. Mat ref(4, inpShapes[0], CV_32F, Scalar(0));
  1601. net.setInputsNames(inpNames);
  1602. for (int i = 0; i < inputs.size(); ++i)
  1603. {
  1604. //std::cout << ref.reshape(1,1) << endl;
  1605. net.setInput(inputs[i], inpNames[i]);
  1606. for (size_t batchId = 0; batchId < ref.size[0]; batchId++)
  1607. {
  1608. int input_channels = inputs[i].size[1];
  1609. Range ranges[4] = { Range(batchId, batchId + 1), Range(0, std::min(out_channels, input_channels)), Range::all(), Range::all() };
  1610. Mat ref_slice = ref(ranges);
  1611. Mat input_slice = inputs[i](ranges);
  1612. ref_slice += weights[i] * input_slice;
  1613. }
  1614. }
  1615. net.setPreferableBackend(backendId);
  1616. net.setPreferableTarget(targetId);
  1617. Mat out = net.forward();
  1618. normAssert(out, ref);
  1619. if (testing::Test::HasFailure())
  1620. {
  1621. std::cout << out.reshape(1,1) << endl;
  1622. std::cout << ref.reshape(1,1) << endl;
  1623. }
  1624. }
  1625. TEST_P(Layer_Test_Eltwise_unequal, accuracy_input_0)
  1626. {
  1627. bool weighted = get<0>(GetParam());
  1628. int backendId = get<0>(get<1>(GetParam()));
  1629. int targetId = get<1>(get<1>(GetParam()));
  1630. Net net;
  1631. LayerParams lp;
  1632. lp.type = "Eltwise";
  1633. lp.name = "testLayer";
  1634. lp.set<std::string>("output_channels_mode", "input_0");
  1635. if (backendId == DNN_BACKEND_CUDA && weighted)
  1636. applyTestTag(CV_TEST_TAG_DNN_SKIP_CUDA);
  1637. const int inpShapes[][4] = {{1, 4, 2, 2}, {1, 2, 2, 2}, {1, 3, 2, 2}};
  1638. const int out_channels = inpShapes[0][1];
  1639. std::vector<String> inpNames(3);
  1640. std::vector<Mat> inputs(3);
  1641. std::vector<float> weights(3, 1);
  1642. if (weighted)
  1643. {
  1644. for (int i = 0; i < inputs.size(); ++i)
  1645. weights[i] = -0.125f + i * 0.25f;
  1646. lp.set("coeff", DictValue::arrayReal<float*>(&weights[0], weights.size()));
  1647. }
  1648. int eltwiseId = net.addLayer(lp.name, lp.type, lp);
  1649. for (int i = 0; i < inputs.size(); ++i)
  1650. {
  1651. inputs[i].create(4, inpShapes[i], CV_32F);
  1652. size_t total = inputs[i].total();
  1653. for (size_t j = 0; j < total; j++)
  1654. inputs[i].ptr<float>()[j] = j + i * 100;
  1655. inpNames[i] = format("input_%d", i);
  1656. net.connect(0, i, eltwiseId, i);
  1657. }
  1658. Mat ref(4, inpShapes[0], CV_32F, Scalar(0));
  1659. net.setInputsNames(inpNames);
  1660. for (int i = 0; i < inputs.size(); ++i)
  1661. {
  1662. //std::cout << ref.reshape(1,1) << endl;
  1663. net.setInput(inputs[i], inpNames[i]);
  1664. for (size_t batchId = 0; batchId < ref.size[0]; batchId++)
  1665. {
  1666. int input_channels = inputs[i].size[1];
  1667. Range ranges[4] = { Range(batchId, batchId + 1), Range(0, std::min(out_channels, input_channels)), Range::all(), Range::all() };
  1668. Mat ref_slice = ref(ranges);
  1669. Mat input_slice = inputs[i](ranges);
  1670. ref_slice += weights[i] * input_slice;
  1671. }
  1672. }
  1673. net.setPreferableBackend(backendId);
  1674. net.setPreferableTarget(targetId);
  1675. Mat out = net.forward();
  1676. normAssert(out, ref);
  1677. if (testing::Test::HasFailure())
  1678. {
  1679. std::cout << out.reshape(1,1) << endl;
  1680. std::cout << ref.reshape(1,1) << endl;
  1681. }
  1682. }
  1683. INSTANTIATE_TEST_CASE_P(/**/, Layer_Test_Eltwise_unequal, Combine(
  1684. testing::Bool(),
  1685. dnnBackendsAndTargets()
  1686. ));
  1687. typedef testing::TestWithParam<tuple<Backend, Target> > Layer_Test_Resize;
  1688. TEST_P(Layer_Test_Resize, change_input)
  1689. {
  1690. int backendId = get<0>(GetParam());
  1691. int targetId = get<1>(GetParam());
  1692. Net net;
  1693. LayerParams lp;
  1694. lp.type = "Resize";
  1695. lp.name = "testLayer";
  1696. lp.set("zoom_factor", 2);
  1697. lp.set("interpolation", "nearest");
  1698. net.addLayerToPrev(lp.name, lp.type, lp);
  1699. for (int i = 0; i < 2; ++i)
  1700. {
  1701. Mat inp(4 + i, 5 + i, CV_8UC3), ref;
  1702. randu(inp, 0, 255);
  1703. resize(inp, ref, Size(0, 0), 2, 2, INTER_NEAREST);
  1704. ref = blobFromImage(ref);
  1705. net.setInput(blobFromImage(inp));
  1706. net.setPreferableBackend(backendId);
  1707. net.setPreferableTarget(targetId);
  1708. Mat out = net.forward();
  1709. normAssert(out, ref);
  1710. }
  1711. }
  1712. INSTANTIATE_TEST_CASE_P(/**/, Layer_Test_Resize, dnnBackendsAndTargets());
  1713. struct Layer_Test_Slice : public testing::TestWithParam<tuple<Backend, Target> >
  1714. {
  1715. template<int DIMS>
  1716. void test_slice(const int* inputShape, const int* begin, const int* end)
  1717. {
  1718. int backendId = get<0>(GetParam());
  1719. int targetId = get<1>(GetParam());
  1720. Mat input(DIMS, inputShape, CV_32FC1, Scalar::all(0));
  1721. for (int i = 0; i < (int)input.total(); ++i)
  1722. input.ptr<float>()[i] = (float)i;
  1723. std::vector<Range> range(DIMS);
  1724. for (int i = 0; i < DIMS; ++i)
  1725. range[i] = Range(begin[i], end[i]);
  1726. Net net;
  1727. LayerParams lp;
  1728. lp.type = "Slice";
  1729. lp.name = "testLayer";
  1730. lp.set("begin", DictValue::arrayInt<int*>((int*)&begin[0], DIMS));
  1731. lp.set("end", DictValue::arrayInt<int*>((int*)&end[0], DIMS));
  1732. net.addLayerToPrev(lp.name, lp.type, lp);
  1733. {
  1734. net.setInput(input);
  1735. net.setPreferableBackend(backendId);
  1736. net.setPreferableTarget(targetId);
  1737. Mat out = net.forward();
  1738. EXPECT_GT(cv::norm(out, NORM_INF), 0);
  1739. normAssert(out, input(range));
  1740. #if 0
  1741. cout << input(range).clone().reshape(1, 1) << endl;
  1742. cout << out.reshape(1, 1) << endl;
  1743. #endif
  1744. }
  1745. }
  1746. };
  1747. TEST_P(Layer_Test_Slice, slice_channels_17762)
  1748. {
  1749. const int inputShape[4] = {1, 16, 6, 8};
  1750. const int begin[] = {0, 4, 0, 0};
  1751. const int end[] = {1, 8, 6, 8};
  1752. test_slice<4>(inputShape, begin, end);
  1753. }
  1754. TEST_P(Layer_Test_Slice, slice_channels_with_batch_17762)
  1755. {
  1756. const int inputShape[4] = {4, 4, 3, 4};
  1757. const int begin[] = {0, 1, 0, 0};
  1758. const int end[] = {4, 3, 3, 4};
  1759. test_slice<4>(inputShape, begin, end);
  1760. }
  1761. TEST_P(Layer_Test_Slice, slice_channels_and_batch_17762)
  1762. {
  1763. int backend = get<0>(GetParam());
  1764. if (backend == DNN_BACKEND_INFERENCE_ENGINE_NN_BUILDER_2019)
  1765. applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_NN_BUILDER, CV_TEST_TAG_DNN_SKIP_IE_VERSION);
  1766. const int inputShape[4] = {4, 4, 3, 4};
  1767. const int begin[] = {2, 1, 0, 0};
  1768. const int end[] = {4, 3, 3, 4};
  1769. test_slice<4>(inputShape, begin, end);
  1770. }
  1771. TEST_P(Layer_Test_Slice, slice_rows)
  1772. {
  1773. const int inputShape[4] = {1, 2, 6, 4};
  1774. const int begin[] = {0, 0, 4, 0};
  1775. const int end[] = {1, 2, 6, 4};
  1776. test_slice<4>(inputShape, begin, end);
  1777. }
  1778. TEST_P(Layer_Test_Slice, slice_cols)
  1779. {
  1780. const int inputShape[4] = {1, 2, 3, 8};
  1781. const int begin[] = {0, 0, 0, 4};
  1782. const int end[] = {1, 2, 3, 8};
  1783. test_slice<4>(inputShape, begin, end);
  1784. }
  1785. TEST_P(Layer_Test_Slice, slice_complex_1_unaligned)
  1786. {
  1787. const int inputShape[4] = {1, 4, 2, 3};
  1788. const int begin[] = {0, 2, 1, 0};
  1789. const int end[] = {1, 3, 2, 2};
  1790. test_slice<4>(inputShape, begin, end);
  1791. }
  1792. TEST_P(Layer_Test_Slice, slice_complex_2_x4)
  1793. {
  1794. const int inputShape[4] = {1, 3, 2, 4};
  1795. const int begin[] = {0, 2, 1, 0};
  1796. const int end[] = {1, 3, 2, 2};
  1797. test_slice<4>(inputShape, begin, end);
  1798. }
  1799. TEST_P(Layer_Test_Slice, slice_complex_3)
  1800. {
  1801. const int inputShape[4] = {1, 6, 4, 8};
  1802. const int begin[] = {0, 2, 1, 4};
  1803. const int end[] = {1, 4, 3, 8};
  1804. test_slice<4>(inputShape, begin, end);
  1805. }
  1806. TEST_P(Layer_Test_Slice, variable_input_shape)
  1807. {
  1808. int backendId = get<0>(GetParam());
  1809. int targetId = get<1>(GetParam());
  1810. int begin[] = {0, 0, 0, 0};
  1811. int end[] = {-1, -1, -1, -1};
  1812. Net net;
  1813. LayerParams lp;
  1814. lp.type = "Slice";
  1815. lp.name = "testLayer";
  1816. lp.set("begin", DictValue::arrayInt<int*>(&begin[0], 4));
  1817. lp.set("end", DictValue::arrayInt<int*>(&end[0], 4));
  1818. net.addLayerToPrev(lp.name, lp.type, lp);
  1819. for (int i = 0; i < 2; ++i)
  1820. {
  1821. Mat inp(4 + i, 5 + i, CV_8UC1);
  1822. randu(inp, 0, 255);
  1823. inp = blobFromImage(inp);
  1824. net.setInput(inp);
  1825. net.setPreferableBackend(backendId);
  1826. net.setPreferableTarget(targetId);
  1827. Mat out = net.forward();
  1828. normAssert(out, inp);
  1829. }
  1830. }
  1831. INSTANTIATE_TEST_CASE_P(/**/, Layer_Test_Slice, dnnBackendsAndTargets());
  1832. typedef testing::TestWithParam<tuple<Backend, Target> > Layer_Test_BatchNorm;
  1833. TEST_P(Layer_Test_BatchNorm, fusion)
  1834. {
  1835. // This tests reinitializes network by forwarding different batch size input.
  1836. // We check BatchNorm layer weights restoring after fusion.
  1837. int backendId = get<0>(GetParam());
  1838. int targetId = get<1>(GetParam());
  1839. const int ch = 4;
  1840. Mat mean(1, ch, CV_32F), var(1, ch, CV_32F), weights(1, ch, CV_32F);
  1841. randu(mean, 0, 1);
  1842. randu(var, 0, 1);
  1843. randu(weights, 0, 1);
  1844. Net net;
  1845. {
  1846. LayerParams lp;
  1847. lp.type = "BatchNorm";
  1848. lp.name = "bn";
  1849. lp.set("has_weight", false);
  1850. lp.set("has_bias", false);
  1851. lp.blobs.push_back(mean);
  1852. lp.blobs.push_back(var);
  1853. net.addLayerToPrev(lp.name, lp.type, lp);
  1854. }
  1855. {
  1856. LayerParams lp;
  1857. lp.type = "Scale";
  1858. lp.name = "scale";
  1859. lp.set("has_bias", false);
  1860. lp.blobs.push_back(weights);
  1861. net.addLayerToPrev(lp.name, lp.type, lp);
  1862. }
  1863. Mat inp(4, 5, CV_32FC(ch));
  1864. randu(inp, 0, 1);
  1865. net.setPreferableBackend(backendId);
  1866. net.setPreferableTarget(targetId);
  1867. net.setInput(blobFromImage(inp));
  1868. Mat ref = net.forward();
  1869. net.setInput(blobFromImages(std::vector<Mat>(2, inp)));
  1870. Mat out = net.forward();
  1871. for (int i = 0; i < 2; ++i)
  1872. {
  1873. std::vector<Range> ranges(4, Range::all());
  1874. ranges[0].start = i;
  1875. ranges[0].end = i + 1;
  1876. normAssert(out(ranges), ref);
  1877. }
  1878. }
  1879. INSTANTIATE_TEST_CASE_P(/**/, Layer_Test_BatchNorm, dnnBackendsAndTargets());
  1880. class TestLayerFusion : public DNNTestLayer {
  1881. public:
  1882. static void makeDefaultTestConvolutionLayer(LayerParams& convParams, int in_channels, int num_filters, bool bias_term)
  1883. {
  1884. const int kernel_h = 3, kernel_w = 3;
  1885. const int pad_h = kernel_h / 2, pad_w = kernel_w / 2;
  1886. convParams.set("kernel_h", kernel_h);
  1887. convParams.set("kernel_w", kernel_w);
  1888. convParams.set("pad_h", pad_h);
  1889. convParams.set("pad_w", pad_w);
  1890. convParams.set("num_output", num_filters);
  1891. convParams.set("bias_term", bias_term);
  1892. convParams.type = "Convolution";
  1893. convParams.name = "convolution";
  1894. float conv_init_magnitude = 1.0f / in_channels / kernel_h / kernel_w;
  1895. int weightsShape[] = {num_filters, in_channels, kernel_h, kernel_w};
  1896. Mat weights(4, &weightsShape[0], CV_32F);
  1897. randu(weights, -conv_init_magnitude, conv_init_magnitude);
  1898. convParams.blobs.push_back(weights);
  1899. if (bias_term)
  1900. {
  1901. Mat bias(1, num_filters, CV_32F);
  1902. randu(bias, -1.0f, 1.0f);
  1903. convParams.blobs.push_back(bias);
  1904. }
  1905. }
  1906. static void makeDefaultTestActivationLayer(LayerParams& activationParams, const std::string& type, int in_channels)
  1907. {
  1908. activationParams.type = type;
  1909. activationParams.name = "activation";
  1910. if (activationParams.type == "ReLU")
  1911. activationParams.set("negative_slope", 0.1f);
  1912. else if (activationParams.type == "Power")
  1913. {
  1914. activationParams.set("power", 2.0f);
  1915. activationParams.set("scale", 0.5f);
  1916. activationParams.set("shift", 0.3f);
  1917. }
  1918. else if (activationParams.type == "ReLU6")
  1919. {
  1920. activationParams.set("min_value", -1.0f);
  1921. activationParams.set("max_value", 1.0f);
  1922. }
  1923. else if (activationParams.type == "ChannelsPReLU")
  1924. {
  1925. Mat scales(1, in_channels, CV_32F);
  1926. randu(scales, -1.0f, 1.0f);
  1927. activationParams.blobs.push_back(scales);
  1928. }
  1929. else if (activationParams.type == "Exp")
  1930. {
  1931. activationParams.set("base", -1.0f);
  1932. activationParams.set("scale", 0.3f);
  1933. activationParams.set("shift", 0.6f);
  1934. }
  1935. }
  1936. static void makeDefaultTestEltwiseLayer(LayerParams& eltwiseParams, const std::string& op, bool withCoefficients)
  1937. {
  1938. eltwiseParams.type = "Eltwise";
  1939. eltwiseParams.name = "eltwise";
  1940. eltwiseParams.set("operation", op);
  1941. if (withCoefficients)
  1942. {
  1943. float coeff[] = {0.3f, 0.5f};
  1944. eltwiseParams.set("coeff", DictValue::arrayReal<float*>(coeff, 2));
  1945. }
  1946. }
  1947. static void test(Mat& input, Net& net, Backend backendId, Target targetId, std::vector<int> expectedFusedLayers = std::vector<int>(), double l1 = 0.0, double lInf = 0.0)
  1948. {
  1949. DNNTestLayer::checkBackend(backendId, targetId);
  1950. net.enableFusion(false);
  1951. net.setPreferableBackend(DNN_BACKEND_OPENCV);
  1952. net.setPreferableTarget(DNN_TARGET_CPU);
  1953. net.setInput(input);
  1954. Mat outputReference = net.forward().clone();
  1955. std::vector<double> refTimings;
  1956. net.getPerfProfile(refTimings);
  1957. for (int i = 0; i < refTimings.size(); i++)
  1958. {
  1959. CV_Assert(refTimings[i] != 0.0);
  1960. }
  1961. net.enableFusion(true);
  1962. net.setPreferableBackend(backendId);
  1963. net.setPreferableTarget(targetId);
  1964. net.setInput(input);
  1965. Mat outputTest = net.forward().clone();
  1966. std::vector<double> testTimings;
  1967. net.getPerfProfile(testTimings);
  1968. for (int i = 0; i < testTimings.size(); i++)
  1969. {
  1970. if(std::find(expectedFusedLayers.begin(), expectedFusedLayers.end(), i + 1) != expectedFusedLayers.end())
  1971. {
  1972. EXPECT_EQ(testTimings[i], 0.0);
  1973. }
  1974. else
  1975. {
  1976. EXPECT_NE(testTimings[i], 0.0);
  1977. }
  1978. }
  1979. // double ref_max_value, ref_min_value;
  1980. // minMaxLoc(outputReference.reshape(1, 1), &ref_min_value, &ref_max_value);
  1981. // std::cout << "reference range: " << ref_min_value << ' ' << ref_max_value << std::endl;
  1982. double default_l1, default_lInf;
  1983. DNNTestLayer::getDefaultThresholds(backendId, targetId, &default_l1, &default_lInf);
  1984. if (l1 == 0.0)
  1985. l1 = default_l1;
  1986. if (lInf == 0.0)
  1987. lInf = default_lInf;
  1988. normAssert(outputReference, outputTest, "", l1, lInf);
  1989. }
  1990. static testing::internal::ParamGenerator<std::string> eltwiseOpList()
  1991. {
  1992. // TODO: automate list generation
  1993. return Values("sum", "max", "min", "prod", "div");
  1994. }
  1995. static testing::internal::ParamGenerator<std::string> activationLayersList()
  1996. {
  1997. // TODO: automate list generation
  1998. return Values("ReLU", "ReLU6", "ChannelsPReLU", "TanH", "Swish", "Mish", "Sigmoid", "ELU", "AbsVal", "BNLL", "Power", "Exp");
  1999. }
  2000. static testing::internal::ParamGenerator<tuple<Backend, Target> > dnnBackendsAndTargetsForFusionTests()
  2001. {
  2002. return dnnBackendsAndTargets(false, false, true, false, true, false); // OCV OpenCL + OCV CPU + CUDA
  2003. }
  2004. };
  2005. typedef TestWithParam<tuple<bool, std::string, tuple<Backend, Target> > > ConvolutionActivationFusion;
  2006. TEST_P(ConvolutionActivationFusion, Accuracy)
  2007. {
  2008. // input
  2009. // |
  2010. // -----------------------
  2011. // | convolution |
  2012. // -----------------------
  2013. // |
  2014. // -----------------------
  2015. // | activation |
  2016. // -----------------------
  2017. // |
  2018. // output
  2019. const int batch_size = 2, in_channels = 16;
  2020. const int in_height = 16, in_width = 16;
  2021. int inputShape[] = {batch_size, in_channels, in_height, in_width};
  2022. Mat input(4, &inputShape[0], CV_32F);
  2023. randu(input, 1.0f, 2.0f);
  2024. bool bias_term = get<0>(GetParam());
  2025. LayerParams convParams;
  2026. TestLayerFusion::makeDefaultTestConvolutionLayer(convParams, in_channels, in_channels, bias_term);
  2027. std::string actType = get<1>(GetParam());
  2028. LayerParams activationParams;
  2029. TestLayerFusion::makeDefaultTestActivationLayer(activationParams, actType, in_channels);
  2030. Backend backendId = get<0>(get<2>(GetParam()));
  2031. Target targetId = get<1>(get<2>(GetParam()));
  2032. Net net;
  2033. int convId = net.addLayer(convParams.name, convParams.type, convParams);
  2034. int activId = net.addLayerToPrev(activationParams.name, activationParams.type, activationParams);
  2035. net.connect(0, 0, convId, 0);
  2036. std::vector<int> expectedFusedLayers;
  2037. if (backendId == DNN_BACKEND_OPENCV)
  2038. {
  2039. if (targetId == DNN_TARGET_CPU)
  2040. expectedFusedLayers.push_back(activId); // all activations are fused
  2041. else if (targetId == DNN_TARGET_OPENCL || targetId == DNN_TARGET_OPENCL_FP16)
  2042. {
  2043. if (actType == "ReLU" || actType == "ChannelsPReLU" || actType == "ReLU6" || actType == "TanH" /*|| actType == "Power"*/)
  2044. expectedFusedLayers.push_back(activId);
  2045. }
  2046. }
  2047. else if (backendId == DNN_BACKEND_CUDA)
  2048. {
  2049. if (actType == "ReLU" || actType == "ReLU6" || actType == "TanH" || actType == "Swish" ||
  2050. actType == "Mish" || actType == "Sigmoid" || actType == "Power")
  2051. expectedFusedLayers.push_back(activId);
  2052. }
  2053. TestLayerFusion::test(input, net, backendId, targetId, expectedFusedLayers);
  2054. }
  2055. INSTANTIATE_TEST_CASE_P(TestLayerFusion, ConvolutionActivationFusion, Combine(
  2056. /* bias */ testing::Bool(),
  2057. /* activation */ TestLayerFusion::activationLayersList(),
  2058. TestLayerFusion::dnnBackendsAndTargetsForFusionTests()
  2059. ));
  2060. typedef TestWithParam<tuple<bool, std::string, bool, tuple<Backend, Target> > > ConvolutionEltwiseFusion;
  2061. TEST_P(ConvolutionEltwiseFusion, Accuracy)
  2062. {
  2063. // input
  2064. // |
  2065. // -------------------------------
  2066. // | |
  2067. // | ---------------
  2068. // | | convolution |
  2069. // | ---------------
  2070. // | |
  2071. // | ---------------- |
  2072. // --------| eltwise op |-------
  2073. // ----------------
  2074. // |
  2075. // output
  2076. const int batch_size = 2, in_channels = 16;
  2077. const int in_height = 16, in_width = 16;
  2078. int inputShape[] = {batch_size, in_channels, in_height, in_width};
  2079. Mat input(4, &inputShape[0], CV_32F);
  2080. randu(input, 1.0f, 2.0f); // avoid small values to test eltwise div
  2081. bool bias_term = get<0>(GetParam());
  2082. LayerParams convParams;
  2083. TestLayerFusion::makeDefaultTestConvolutionLayer(convParams, in_channels, in_channels, bias_term);
  2084. std::string eltwiseOp = get<1>(GetParam());
  2085. bool weightedEltwise = get<2>(GetParam());
  2086. if (eltwiseOp != "sum" && weightedEltwise)
  2087. throw SkipTestException("weighted eltwise not supported");
  2088. LayerParams eltwiseParams;
  2089. TestLayerFusion::makeDefaultTestEltwiseLayer(eltwiseParams, eltwiseOp, weightedEltwise);
  2090. Net net;
  2091. int convId = net.addLayer(convParams.name, convParams.type, convParams);
  2092. int eltwiseId = net.addLayer(eltwiseParams.name, eltwiseParams.type, eltwiseParams);
  2093. net.connect(0, 0, convId, 0);
  2094. net.connect(convId, 0, eltwiseId, 0);
  2095. net.connect(0, 0, eltwiseId, 1);
  2096. Backend backendId = get<0>(get<3>(GetParam()));
  2097. Target targetId = get<1>(get<3>(GetParam()));
  2098. std::vector<int> expectedFusedLayers;
  2099. if (backendId == DNN_BACKEND_CUDA && eltwiseOp == "sum" && !weightedEltwise)
  2100. expectedFusedLayers.push_back(eltwiseId);
  2101. TestLayerFusion::test(input, net, backendId, targetId, expectedFusedLayers);
  2102. }
  2103. INSTANTIATE_TEST_CASE_P(TestLayerFusion, ConvolutionEltwiseFusion, Combine(
  2104. /* bias */ testing::Bool(),
  2105. /* eltwise op */ TestLayerFusion::eltwiseOpList(),
  2106. /* eltwise weighted */ testing::Bool(),
  2107. TestLayerFusion::dnnBackendsAndTargetsForFusionTests()
  2108. ));
  2109. typedef TestWithParam<tuple<bool, std::string, bool, std::string, tuple<Backend, Target> > > ConvolutionEltwiseActivationFusion;
  2110. TEST_P(ConvolutionEltwiseActivationFusion, Accuracy)
  2111. {
  2112. // input
  2113. // |
  2114. // -------------------------------
  2115. // | |
  2116. // | ---------------
  2117. // | | convolution |
  2118. // | ---------------
  2119. // | |
  2120. // | ---------------- |
  2121. // --------| eltwise op |-------
  2122. // ----------------
  2123. // |
  2124. // ----------------
  2125. // | activation |
  2126. // ----------------
  2127. // |
  2128. // output
  2129. const int batch_size = 2, in_channels = 16;
  2130. const int in_height = 16, in_width = 16;
  2131. int inputShape[] = {batch_size, in_channels, in_height, in_width};
  2132. Mat input(4, &inputShape[0], CV_32F);
  2133. randu(input, 1.0f, 2.0f); // avoid small values to test eltwise div
  2134. bool bias_term = get<0>(GetParam());
  2135. LayerParams convParams;
  2136. TestLayerFusion::makeDefaultTestConvolutionLayer(convParams, in_channels, in_channels, bias_term);
  2137. std::string eltwiseOp = get<1>(GetParam());
  2138. bool weightedEltwise = get<2>(GetParam());
  2139. if (eltwiseOp != "sum" && weightedEltwise)
  2140. throw SkipTestException("weighted eltwise not supported");
  2141. LayerParams eltwiseParams;
  2142. TestLayerFusion::makeDefaultTestEltwiseLayer(eltwiseParams, eltwiseOp, weightedEltwise);
  2143. std::string actType = get<3>(GetParam());
  2144. LayerParams activationParams;
  2145. TestLayerFusion::makeDefaultTestActivationLayer(activationParams, actType, in_channels);
  2146. Backend backendId = get<0>(get<4>(GetParam()));
  2147. Target targetId = get<1>(get<4>(GetParam()));
  2148. Net net;
  2149. int convId = net.addLayer(convParams.name, convParams.type, convParams);
  2150. int eltwiseId = net.addLayer(eltwiseParams.name, eltwiseParams.type, eltwiseParams);
  2151. int activId = net.addLayer(activationParams.name, activationParams.type, activationParams);
  2152. net.connect(0, 0, convId, 0);
  2153. net.connect(convId, 0, eltwiseId, 0);
  2154. net.connect(0, 0, eltwiseId, 1);
  2155. net.connect(eltwiseId, 0, activId, 0);
  2156. std::vector<int> expectedFusedLayers;
  2157. if (backendId == DNN_BACKEND_OPENCV)
  2158. {
  2159. if (targetId == DNN_TARGET_CPU)
  2160. expectedFusedLayers.push_back(activId); // activation is fused with eltwise layer
  2161. else if (targetId == DNN_TARGET_OPENCL || targetId == DNN_TARGET_OPENCL_FP16)
  2162. {
  2163. if (eltwiseOp == "sum" && !weightedEltwise &&
  2164. (actType == "ReLU" || actType == "ChannelsPReLU" /*|| actType == "Power"*/)
  2165. )
  2166. {
  2167. expectedFusedLayers.push_back(eltwiseId);
  2168. expectedFusedLayers.push_back(activId);
  2169. }
  2170. }
  2171. }
  2172. else if(backendId == DNN_BACKEND_CUDA)
  2173. {
  2174. if (eltwiseOp == "sum" && !weightedEltwise)
  2175. {
  2176. expectedFusedLayers.push_back(eltwiseId);
  2177. if (actType == "ReLU" || actType == "ReLU6" || actType == "TanH" || actType == "Swish" ||
  2178. actType == "Mish" || actType == "Sigmoid" || actType == "Power")
  2179. expectedFusedLayers.push_back(activId);
  2180. }
  2181. }
  2182. TestLayerFusion::test(input, net, backendId, targetId, expectedFusedLayers);
  2183. }
  2184. INSTANTIATE_TEST_CASE_P(TestLayerFusion, ConvolutionEltwiseActivationFusion, Combine(
  2185. /* bias */ testing::Bool(),
  2186. /* eltwise op */ TestLayerFusion::eltwiseOpList(),
  2187. /* eltwise weighted */ testing::Bool(),
  2188. /* activation */ TestLayerFusion::activationLayersList(),
  2189. TestLayerFusion::dnnBackendsAndTargetsForFusionTests()
  2190. ));
  2191. typedef TestWithParam<tuple<bool, std::string, std::string, bool, tuple<Backend, Target> > > ConvolutionActivationEltwiseFusion;
  2192. TEST_P(ConvolutionActivationEltwiseFusion, Accuracy)
  2193. {
  2194. // input
  2195. // |
  2196. // -------------------------------
  2197. // | |
  2198. // | ----------------
  2199. // | | convolution |
  2200. // | ----------------
  2201. // | |
  2202. // | ----------------
  2203. // | | activation |
  2204. // | ----------------
  2205. // | |
  2206. // | ---------------- |
  2207. // --------| eltwise sum |-------
  2208. // ----------------
  2209. // |
  2210. const int batch_size = 2, in_channels = 16;
  2211. const int in_height = 16, in_width = 16;
  2212. int inputShape[] = {batch_size, in_channels, in_height, in_width};
  2213. Mat input(4, &inputShape[0], CV_32F);
  2214. randu(input, 1.0f, 2.0f); // avoid small values to test eltwise div
  2215. bool bias_term = get<0>(GetParam());
  2216. LayerParams convParams;
  2217. TestLayerFusion::makeDefaultTestConvolutionLayer(convParams, in_channels, in_channels, bias_term);
  2218. std::string actType = get<1>(GetParam());
  2219. LayerParams activationParams;
  2220. TestLayerFusion::makeDefaultTestActivationLayer(activationParams, actType, in_channels);
  2221. std::string eltwiseOp = get<2>(GetParam());
  2222. bool weightedEltwise = get<3>(GetParam());
  2223. if (eltwiseOp != "sum" && weightedEltwise)
  2224. throw SkipTestException("weighted eltwise not supported");
  2225. LayerParams eltwiseParams;
  2226. TestLayerFusion::makeDefaultTestEltwiseLayer(eltwiseParams, eltwiseOp, weightedEltwise);
  2227. Backend backendId = get<0>(get<4>(GetParam()));
  2228. Target targetId = get<1>(get<4>(GetParam()));
  2229. Net net;
  2230. int convId = net.addLayer(convParams.name, convParams.type, convParams);
  2231. int activId = net.addLayer(activationParams.name, activationParams.type, activationParams);
  2232. int eltwiseId = net.addLayer(eltwiseParams.name, eltwiseParams.type, eltwiseParams);
  2233. net.connect(0, 0, convId, 0);
  2234. net.connect(convId, 0, activId, 0);
  2235. net.connect(activId, 0, eltwiseId, 0);
  2236. net.connect(0, 0, eltwiseId, 1);
  2237. std::vector<int> expectedFusedLayers;
  2238. if (backendId == DNN_BACKEND_OPENCV)
  2239. {
  2240. if (targetId == DNN_TARGET_CPU)
  2241. expectedFusedLayers.push_back(activId); // activation fused with convolution
  2242. else if (targetId == DNN_TARGET_OPENCL || targetId == DNN_TARGET_OPENCL_FP16)
  2243. {
  2244. if (actType == "ReLU" || actType == "ChannelsPReLU" || actType == "ReLU6" || actType == "TanH" /*|| actType == "Power"*/)
  2245. expectedFusedLayers.push_back(activId); // activation fused with convolution
  2246. }
  2247. }
  2248. else if(backendId == DNN_BACKEND_CUDA)
  2249. {
  2250. if (actType == "ReLU" || actType == "ReLU6" || actType == "TanH" || actType == "Swish" ||
  2251. actType == "Mish" || actType == "Sigmoid" || actType == "Power")
  2252. {
  2253. expectedFusedLayers.push_back(activId);
  2254. if (eltwiseOp == "sum" && !weightedEltwise)
  2255. expectedFusedLayers.push_back(eltwiseId);
  2256. }
  2257. }
  2258. TestLayerFusion::test(input, net, backendId, targetId, expectedFusedLayers);
  2259. }
  2260. INSTANTIATE_TEST_CASE_P(TestLayerFusion, ConvolutionActivationEltwiseFusion, Combine(
  2261. /* bias */ testing::Bool(),
  2262. /* activation */ TestLayerFusion::activationLayersList(),
  2263. /* eltwise op */ TestLayerFusion::eltwiseOpList(),
  2264. /* eltwise weighted */ testing::Bool(),
  2265. TestLayerFusion::dnnBackendsAndTargetsForFusionTests()
  2266. ));
  2267. }} // namespace