test_layers.cpp 95 KB

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