test_tf_importer.cpp 66 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770
  1. // This file is part of OpenCV project.
  2. // It is subject to the license terms in the LICENSE file found in the top-level directory
  3. // of this distribution and at http://opencv.org/license.html.
  4. // Copyright (C) 2017-2019, Intel Corporation, all rights reserved.
  5. // Third party copyrights are property of their respective owners.
  6. /*
  7. Test for Tensorflow models loading
  8. */
  9. #include "test_precomp.hpp"
  10. #include "npy_blob.hpp"
  11. #include <opencv2/dnn/layer.details.hpp> // CV_DNN_REGISTER_LAYER_CLASS
  12. #include <opencv2/dnn/utils/debug_utils.hpp>
  13. namespace opencv_test
  14. {
  15. using namespace cv;
  16. using namespace cv::dnn;
  17. template<typename TString>
  18. static std::string _tf(TString filename)
  19. {
  20. return (getOpenCVExtraDir() + "/dnn/") + filename;
  21. }
  22. TEST(Test_TensorFlow, read_inception)
  23. {
  24. Net net;
  25. {
  26. const string model = findDataFile("dnn/tensorflow_inception_graph.pb", false);
  27. net = readNetFromTensorflow(model);
  28. ASSERT_FALSE(net.empty());
  29. }
  30. net.setPreferableBackend(DNN_BACKEND_OPENCV);
  31. Mat sample = imread(_tf("grace_hopper_227.png"));
  32. ASSERT_TRUE(!sample.empty());
  33. Mat input;
  34. resize(sample, input, Size(224, 224));
  35. input -= Scalar::all(117); // mean sub
  36. Mat inputBlob = blobFromImage(input);
  37. net.setInput(inputBlob, "input");
  38. Mat out = net.forward("softmax2");
  39. std::cout << out.dims << std::endl;
  40. }
  41. TEST(Test_TensorFlow, inception_accuracy)
  42. {
  43. Net net;
  44. {
  45. const string model = findDataFile("dnn/tensorflow_inception_graph.pb", false);
  46. net = readNetFromTensorflow(model);
  47. ASSERT_FALSE(net.empty());
  48. }
  49. net.setPreferableBackend(DNN_BACKEND_OPENCV);
  50. Mat sample = imread(_tf("grace_hopper_227.png"));
  51. ASSERT_TRUE(!sample.empty());
  52. Mat inputBlob = blobFromImage(sample, 1.0, Size(224, 224), Scalar(), /*swapRB*/true);
  53. net.setInput(inputBlob, "input");
  54. Mat out = net.forward("softmax2");
  55. Mat ref = blobFromNPY(_tf("tf_inception_prob.npy"));
  56. normAssert(ref, out);
  57. }
  58. static std::string path(const std::string& file)
  59. {
  60. return findDataFile("dnn/tensorflow/" + file);
  61. }
  62. class Test_TensorFlow_layers : public DNNTestLayer
  63. {
  64. public:
  65. void runTensorFlowNet(const std::string& prefix, bool hasText = false,
  66. double l1 = 0.0, double lInf = 0.0, bool memoryLoad = false, const std::string& groupPrefix = "")
  67. {
  68. if (cvtest::debugLevel > 0)
  69. {
  70. std::cout << prefix << groupPrefix << std::endl;
  71. }
  72. std::string netPath = path(prefix + groupPrefix + "_net.pb");
  73. std::string netConfig = (hasText ? path(prefix + groupPrefix + "_net.pbtxt") : "");
  74. std::string inpPath = path(prefix + "_in.npy");
  75. std::string outPath = path(prefix + groupPrefix + "_out.npy");
  76. cv::Mat input = blobFromNPY(inpPath);
  77. cv::Mat ref = blobFromNPY(outPath);
  78. checkBackend(&input, &ref);
  79. Net net;
  80. if (memoryLoad)
  81. {
  82. // Load files into a memory buffers
  83. std::vector<char> dataModel;
  84. readFileContent(netPath, dataModel);
  85. std::vector<char> dataConfig;
  86. if (hasText)
  87. {
  88. readFileContent(netConfig, dataConfig);
  89. }
  90. net = readNetFromTensorflow(dataModel.data(), dataModel.size(),
  91. dataConfig.data(), dataConfig.size());
  92. }
  93. else
  94. net = readNetFromTensorflow(netPath, netConfig);
  95. ASSERT_FALSE(net.empty());
  96. net.setPreferableBackend(backend);
  97. net.setPreferableTarget(target);
  98. net.setInput(input);
  99. cv::Mat output = net.forward();
  100. normAssert(ref, output, "", l1 ? l1 : default_l1, lInf ? lInf : default_lInf);
  101. if (cvtest::debugLevel > 0 || HasFailure())
  102. {
  103. std::cout << "input: " << input.size << std::endl;
  104. std::cout << input.reshape(1, 1) << std::endl;
  105. std::cout << "ref " << ref.size << std::endl;
  106. std::cout << ref.reshape(1, 1) << std::endl;
  107. std::cout << "output: " << output.size << std::endl;
  108. std::cout << output.reshape(1, 1) << std::endl;
  109. }
  110. }
  111. };
  112. TEST_P(Test_TensorFlow_layers, reduce_mean)
  113. {
  114. if (backend == DNN_BACKEND_INFERENCE_ENGINE_NN_BUILDER_2019)
  115. applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_NN_BUILDER);
  116. runTensorFlowNet("global_pool_by_axis");
  117. }
  118. TEST_P(Test_TensorFlow_layers, reduce_max)
  119. {
  120. if (backend == DNN_BACKEND_INFERENCE_ENGINE_NN_BUILDER_2019)
  121. applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_NN_BUILDER);
  122. runTensorFlowNet("max_pool_by_axis", false, 0.0f, 0.0f);
  123. }
  124. TEST_P(Test_TensorFlow_layers, reduce_sum)
  125. {
  126. if (backend == DNN_BACKEND_INFERENCE_ENGINE_NN_BUILDER_2019)
  127. applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_NN_BUILDER);
  128. runTensorFlowNet("sum_pool_by_axis");
  129. }
  130. TEST_P(Test_TensorFlow_layers, reduce_max_channel)
  131. {
  132. #if defined(INF_ENGINE_RELEASE) && INF_ENGINE_VER_MAJOR_GE(2020040000)
  133. if (backend == DNN_BACKEND_INFERENCE_ENGINE_NGRAPH && target == DNN_TARGET_MYRIAD) // incorrect result
  134. applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_MYRIAD, CV_TEST_TAG_DNN_SKIP_IE_NGRAPH, CV_TEST_TAG_DNN_SKIP_IE_VERSION);
  135. #endif
  136. runTensorFlowNet("reduce_max_channel", false, 0.0f, 0.0f);
  137. }
  138. TEST_P(Test_TensorFlow_layers, reduce_sum_channel)
  139. {
  140. runTensorFlowNet("reduce_sum_channel");
  141. }
  142. TEST_P(Test_TensorFlow_layers, reduce_max_channel_keep_dims)
  143. {
  144. #if defined(INF_ENGINE_RELEASE) && INF_ENGINE_VER_MAJOR_GE(2020040000)
  145. if (backend == DNN_BACKEND_INFERENCE_ENGINE_NGRAPH && target == DNN_TARGET_MYRIAD) // incorrect result
  146. applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_MYRIAD, CV_TEST_TAG_DNN_SKIP_IE_NGRAPH, CV_TEST_TAG_DNN_SKIP_IE_VERSION);
  147. #endif
  148. runTensorFlowNet("reduce_max_channel", false, 0.0, 0.0, false, "_keep_dims");
  149. }
  150. TEST_P(Test_TensorFlow_layers, reduce_sum_channel_keep_dims)
  151. {
  152. runTensorFlowNet("reduce_sum_channel", false, 0.0, 0.0, false, "_keep_dims");
  153. }
  154. TEST_P(Test_TensorFlow_layers, ArgLayer)
  155. {
  156. if (backend != DNN_BACKEND_OPENCV || target != DNN_TARGET_CPU)
  157. throw SkipTestException("Only CPU is supported"); // FIXIT use tags
  158. runTensorFlowNet("argmax");
  159. runTensorFlowNet("argmin");
  160. }
  161. TEST_P(Test_TensorFlow_layers, conv_single_conv)
  162. {
  163. runTensorFlowNet("single_conv");
  164. }
  165. TEST_P(Test_TensorFlow_layers, conv_atrous_conv2d_valid)
  166. {
  167. runTensorFlowNet("atrous_conv2d_valid");
  168. }
  169. TEST_P(Test_TensorFlow_layers, conv_atrous_conv2d_same)
  170. {
  171. runTensorFlowNet("atrous_conv2d_same");
  172. }
  173. TEST_P(Test_TensorFlow_layers, conv_depthwise_conv2d)
  174. {
  175. runTensorFlowNet("depthwise_conv2d");
  176. }
  177. TEST_P(Test_TensorFlow_layers, conv_keras_atrous_conv2d_same)
  178. {
  179. runTensorFlowNet("keras_atrous_conv2d_same");
  180. }
  181. TEST_P(Test_TensorFlow_layers, conv_pool_nchw)
  182. {
  183. #if defined(INF_ENGINE_RELEASE) && INF_ENGINE_VER_MAJOR_GE(2020020000)
  184. if (backend == DNN_BACKEND_INFERENCE_ENGINE_NGRAPH && target == DNN_TARGET_MYRIAD)
  185. applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_MYRIAD, CV_TEST_TAG_DNN_SKIP_IE_NGRAPH, CV_TEST_TAG_DNN_SKIP_IE_VERSION);
  186. #endif
  187. runTensorFlowNet("conv_pool_nchw");
  188. }
  189. TEST_P(Test_TensorFlow_layers, Convolution3D)
  190. {
  191. #if defined(INF_ENGINE_RELEASE) && INF_ENGINE_VER_MAJOR_LT(2021040000)
  192. if (backend == DNN_BACKEND_INFERENCE_ENGINE_NN_BUILDER_2019 && target != DNN_TARGET_CPU)
  193. applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_NN_BUILDER); // Only CPU on DLIE backend is supported
  194. if (backend == DNN_BACKEND_INFERENCE_ENGINE_NGRAPH && target != DNN_TARGET_CPU)
  195. applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_NGRAPH); // Only CPU on DLIE backend is supported
  196. #endif
  197. runTensorFlowNet("conv3d");
  198. }
  199. TEST_P(Test_TensorFlow_layers, padding)
  200. {
  201. runTensorFlowNet("padding_valid");
  202. runTensorFlowNet("spatial_padding");
  203. runTensorFlowNet("mirror_pad");
  204. #if defined(INF_ENGINE_RELEASE) && INF_ENGINE_VER_MAJOR_GE(2019020000) && INF_ENGINE_VER_MAJOR_LT(2021040000)
  205. if (target == DNN_TARGET_MYRIAD)
  206. {
  207. if (backend == DNN_BACKEND_INFERENCE_ENGINE_NN_BUILDER_2019)
  208. applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_MYRIAD, CV_TEST_TAG_DNN_SKIP_IE_NN_BUILDER, CV_TEST_TAG_DNN_SKIP_IE_VERSION);
  209. if (backend == DNN_BACKEND_INFERENCE_ENGINE_NGRAPH)
  210. applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_MYRIAD, CV_TEST_TAG_DNN_SKIP_IE_NGRAPH, CV_TEST_TAG_DNN_SKIP_IE_VERSION);
  211. }
  212. #endif
  213. runTensorFlowNet("keras_pad_concat");
  214. }
  215. TEST_P(Test_TensorFlow_layers, padding_asymmetric_1)
  216. {
  217. runTensorFlowNet("conv2d_asymmetric_pads_nchw");
  218. }
  219. TEST_P(Test_TensorFlow_layers, padding_asymmetric_2)
  220. {
  221. runTensorFlowNet("conv2d_asymmetric_pads_nhwc");
  222. }
  223. TEST_P(Test_TensorFlow_layers, padding_asymmetric_3)
  224. {
  225. #if defined(INF_ENGINE_RELEASE) && INF_ENGINE_VER_MAJOR_LT(2021040000)
  226. if (backend == DNN_BACKEND_INFERENCE_ENGINE_NGRAPH && target == DNN_TARGET_CPU) // Exception: Unsupported pad value
  227. applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_CPU, CV_TEST_TAG_DNN_SKIP_IE_NGRAPH, CV_TEST_TAG_DNN_SKIP_IE_VERSION);
  228. #endif
  229. #if defined(INF_ENGINE_RELEASE) && INF_ENGINE_VER_MAJOR_GE(2020020000)
  230. if (backend == DNN_BACKEND_INFERENCE_ENGINE_NGRAPH && target == DNN_TARGET_MYRIAD) // Exception: Unsupported pad value
  231. applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_MYRIAD, CV_TEST_TAG_DNN_SKIP_IE_NGRAPH, CV_TEST_TAG_DNN_SKIP_IE_VERSION);
  232. #endif
  233. runTensorFlowNet("max_pool2d_asymmetric_pads_nchw");
  234. }
  235. TEST_P(Test_TensorFlow_layers, padding_asymmetric_4)
  236. {
  237. #if defined(INF_ENGINE_RELEASE) && INF_ENGINE_VER_MAJOR_LT(2021040000)
  238. if (backend == DNN_BACKEND_INFERENCE_ENGINE_NGRAPH && target == DNN_TARGET_CPU) // Exception: Unsupported pad value
  239. applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_CPU, CV_TEST_TAG_DNN_SKIP_IE_NGRAPH, CV_TEST_TAG_DNN_SKIP_IE_VERSION);
  240. #endif
  241. #if defined(INF_ENGINE_RELEASE) && INF_ENGINE_VER_MAJOR_GE(2020020000)
  242. if (backend == DNN_BACKEND_INFERENCE_ENGINE_NGRAPH && target == DNN_TARGET_MYRIAD) // Exception: Unsupported pad value
  243. applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_MYRIAD, CV_TEST_TAG_DNN_SKIP_IE_NGRAPH, CV_TEST_TAG_DNN_SKIP_IE_VERSION);
  244. #endif
  245. runTensorFlowNet("max_pool2d_asymmetric_pads_nhwc");
  246. }
  247. TEST_P(Test_TensorFlow_layers, padding_asymmetric_5)
  248. {
  249. runTensorFlowNet("conv2d_backprop_input_asymmetric_pads_nchw");
  250. }
  251. TEST_P(Test_TensorFlow_layers, padding_asymmetric_6)
  252. {
  253. runTensorFlowNet("conv2d_backprop_input_asymmetric_pads_nhwc");
  254. }
  255. TEST_P(Test_TensorFlow_layers, padding_same)
  256. {
  257. // Reference output values are in range [0.0006, 2.798]
  258. runTensorFlowNet("padding_same");
  259. }
  260. TEST_P(Test_TensorFlow_layers, eltwise)
  261. {
  262. runTensorFlowNet("eltwise_add_mul");
  263. runTensorFlowNet("eltwise_sub");
  264. }
  265. TEST_P(Test_TensorFlow_layers, eltwise_add_vec)
  266. {
  267. runTensorFlowNet("eltwise_add_vec");
  268. }
  269. TEST_P(Test_TensorFlow_layers, eltwise_mul_vec)
  270. {
  271. runTensorFlowNet("eltwise_mul_vec");
  272. }
  273. TEST_P(Test_TensorFlow_layers, channel_broadcast)
  274. {
  275. if (backend == DNN_BACKEND_INFERENCE_ENGINE_NN_BUILDER_2019)
  276. applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_NN_BUILDER);
  277. runTensorFlowNet("channel_broadcast");
  278. }
  279. TEST_P(Test_TensorFlow_layers, pad_and_concat)
  280. {
  281. runTensorFlowNet("pad_and_concat");
  282. }
  283. TEST_P(Test_TensorFlow_layers, concat_axis_1)
  284. {
  285. #if defined(INF_ENGINE_RELEASE) && INF_ENGINE_VER_MAJOR_EQ(2021040000)
  286. // IE Exception: Ngraph operation Transpose with name Flatten_1/flatten/Reshape/nhwc has dynamic output shape on 0 port, but CPU plug-in supports only static shape
  287. if (backend == DNN_BACKEND_INFERENCE_ENGINE_NGRAPH && (target == DNN_TARGET_OPENCL || target == DNN_TARGET_OPENCL_FP16))
  288. applyTestTag(target == DNN_TARGET_OPENCL ? CV_TEST_TAG_DNN_SKIP_IE_OPENCL : CV_TEST_TAG_DNN_SKIP_IE_OPENCL_FP16,
  289. CV_TEST_TAG_DNN_SKIP_IE_NGRAPH, CV_TEST_TAG_DNN_SKIP_IE_VERSION
  290. );
  291. #endif
  292. #if defined(INF_ENGINE_RELEASE) && INF_ENGINE_VER_MAJOR_EQ(2021030000)
  293. if (backend == DNN_BACKEND_INFERENCE_ENGINE_NGRAPH && target == DNN_TARGET_OPENCL)
  294. applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_OPENCL, CV_TEST_TAG_DNN_SKIP_IE_NGRAPH); // exception
  295. if (backend == DNN_BACKEND_INFERENCE_ENGINE_NGRAPH && target == DNN_TARGET_OPENCL_FP16)
  296. applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_OPENCL_FP16, CV_TEST_TAG_DNN_SKIP_IE_NGRAPH); // exception
  297. #endif
  298. runTensorFlowNet("concat_axis_1");
  299. }
  300. TEST_P(Test_TensorFlow_layers, concat_3d)
  301. {
  302. #if defined(INF_ENGINE_RELEASE) && INF_ENGINE_VER_MAJOR_LT(2021040000)
  303. if (backend == DNN_BACKEND_OPENCV && target != DNN_TARGET_CPU)
  304. {
  305. if (target == DNN_TARGET_OPENCL_FP16) applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_OPENCL_FP16);
  306. if (target == DNN_TARGET_OPENCL) applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_OPENCL);
  307. }
  308. if ((backend == DNN_BACKEND_INFERENCE_ENGINE_NGRAPH ||
  309. backend == DNN_BACKEND_INFERENCE_ENGINE_NN_BUILDER_2019) && target == DNN_TARGET_MYRIAD)
  310. applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_MYRIAD, CV_TEST_TAG_DNN_SKIP_IE_NGRAPH, CV_TEST_TAG_DNN_SKIP_IE_NN_BUILDER);
  311. #endif
  312. runTensorFlowNet("concat_3d");
  313. }
  314. TEST_P(Test_TensorFlow_layers, batch_norm_1)
  315. {
  316. runTensorFlowNet("batch_norm");
  317. }
  318. TEST_P(Test_TensorFlow_layers, batch_norm_2)
  319. {
  320. runTensorFlowNet("batch_norm", false, 0.0, 0.0, true);
  321. }
  322. TEST_P(Test_TensorFlow_layers, batch_norm_3)
  323. {
  324. runTensorFlowNet("fused_batch_norm");
  325. }
  326. TEST_P(Test_TensorFlow_layers, batch_norm_4)
  327. {
  328. runTensorFlowNet("fused_batch_norm", false, 0.0, 0.0, true);
  329. }
  330. TEST_P(Test_TensorFlow_layers, batch_norm_5)
  331. {
  332. runTensorFlowNet("batch_norm_text", true);
  333. }
  334. TEST_P(Test_TensorFlow_layers, batch_norm_6)
  335. {
  336. runTensorFlowNet("batch_norm_text", true, 0.0, 0.0, true);
  337. }
  338. TEST_P(Test_TensorFlow_layers, batch_norm_7)
  339. {
  340. runTensorFlowNet("unfused_batch_norm");
  341. }
  342. TEST_P(Test_TensorFlow_layers, batch_norm_8)
  343. {
  344. runTensorFlowNet("fused_batch_norm_no_gamma");
  345. }
  346. TEST_P(Test_TensorFlow_layers, batch_norm_9)
  347. {
  348. runTensorFlowNet("unfused_batch_norm_no_gamma");
  349. }
  350. TEST_P(Test_TensorFlow_layers, batch_norm_10)
  351. {
  352. runTensorFlowNet("mvn_batch_norm");
  353. }
  354. TEST_P(Test_TensorFlow_layers, batch_norm_11)
  355. {
  356. #if defined(INF_ENGINE_RELEASE) && INF_ENGINE_VER_MAJOR_EQ(2021030000)
  357. if (backend == DNN_BACKEND_INFERENCE_ENGINE_NGRAPH && target == DNN_TARGET_CPU)
  358. applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_CPU, CV_TEST_TAG_DNN_SKIP_IE_NGRAPH); // nan
  359. #endif
  360. if (backend == DNN_BACKEND_INFERENCE_ENGINE_NGRAPH && target == DNN_TARGET_MYRIAD)
  361. applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_MYRIAD, CV_TEST_TAG_DNN_SKIP_IE_NGRAPH);
  362. runTensorFlowNet("mvn_batch_norm_1x1");
  363. }
  364. TEST_P(Test_TensorFlow_layers, batch_norm_12)
  365. {
  366. runTensorFlowNet("switch_identity");
  367. }
  368. TEST_P(Test_TensorFlow_layers, batch_norm_13)
  369. {
  370. runTensorFlowNet("keras_batch_norm_training");
  371. }
  372. TEST_P(Test_TensorFlow_layers, batch_norm3D)
  373. {
  374. if (backend == DNN_BACKEND_INFERENCE_ENGINE_NN_BUILDER_2019 && target != DNN_TARGET_CPU)
  375. {
  376. if (target == DNN_TARGET_OPENCL_FP16) applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_OPENCL_FP16, CV_TEST_TAG_DNN_SKIP_IE_NN_BUILDER);
  377. if (target == DNN_TARGET_OPENCL) applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_OPENCL, CV_TEST_TAG_DNN_SKIP_IE_NN_BUILDER);
  378. if (target == DNN_TARGET_MYRIAD) applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_MYRIAD, CV_TEST_TAG_DNN_SKIP_IE_NN_BUILDER);
  379. throw SkipTestException("");
  380. }
  381. runTensorFlowNet("batch_norm3d");
  382. }
  383. TEST_P(Test_TensorFlow_layers, slim_batch_norm)
  384. {
  385. #if defined(INF_ENGINE_RELEASE) && INF_ENGINE_VER_MAJOR_LT(2021040000)
  386. if (backend == DNN_BACKEND_INFERENCE_ENGINE_NN_BUILDER_2019)
  387. applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_NN_BUILDER);
  388. if (backend == DNN_BACKEND_INFERENCE_ENGINE_NGRAPH)
  389. applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_NGRAPH);
  390. #endif
  391. // Output values range: [-40.0597, 207.827]
  392. double l1 = default_l1;
  393. double lInf = default_lInf;
  394. if (target == DNN_TARGET_OPENCL_FP16 || target == DNN_TARGET_MYRIAD)
  395. {
  396. l1 = 0.041;
  397. lInf = 0.33;
  398. }
  399. #if defined(INF_ENGINE_RELEASE)
  400. else if (backend == DNN_BACKEND_INFERENCE_ENGINE_NGRAPH && target == DNN_TARGET_CPU)
  401. {
  402. lInf = 0.0002;
  403. }
  404. #endif
  405. else if (target == DNN_TARGET_CUDA_FP16)
  406. {
  407. l1 = 0.005;
  408. lInf = 0.33;
  409. }
  410. runTensorFlowNet("slim_batch_norm", false, l1, lInf);
  411. }
  412. TEST_P(Test_TensorFlow_layers, pooling_max_pool_even)
  413. {
  414. #if defined(INF_ENGINE_RELEASE) && INF_ENGINE_VER_MAJOR_GE(2020020000)
  415. if (backend == DNN_BACKEND_INFERENCE_ENGINE_NGRAPH && target == DNN_TARGET_MYRIAD)
  416. applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_MYRIAD, CV_TEST_TAG_DNN_SKIP_IE_NGRAPH, CV_TEST_TAG_DNN_SKIP_IE_VERSION);
  417. #endif
  418. runTensorFlowNet("max_pool_even");
  419. }
  420. TEST_P(Test_TensorFlow_layers, pooling_max_pool_odd_valid)
  421. {
  422. #if defined(INF_ENGINE_RELEASE) && INF_ENGINE_VER_MAJOR_GE(2020020000)
  423. if (backend == DNN_BACKEND_INFERENCE_ENGINE_NGRAPH && target == DNN_TARGET_MYRIAD)
  424. applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_MYRIAD, CV_TEST_TAG_DNN_SKIP_IE_NGRAPH, CV_TEST_TAG_DNN_SKIP_IE_VERSION);
  425. #endif
  426. runTensorFlowNet("max_pool_odd_valid");
  427. }
  428. TEST_P(Test_TensorFlow_layers, pooling_max_pool_odd_same)
  429. {
  430. #if defined(INF_ENGINE_RELEASE) && INF_ENGINE_VER_MAJOR_GE(2020020000)
  431. if (backend == DNN_BACKEND_INFERENCE_ENGINE_NGRAPH && target == DNN_TARGET_MYRIAD)
  432. applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_MYRIAD, CV_TEST_TAG_DNN_SKIP_IE_NGRAPH, CV_TEST_TAG_DNN_SKIP_IE_VERSION);
  433. #endif
  434. runTensorFlowNet("max_pool_odd_same");
  435. }
  436. TEST_P(Test_TensorFlow_layers, pooling_reduce_mean)
  437. {
  438. runTensorFlowNet("reduce_mean"); // an average pooling over all spatial dimensions.
  439. }
  440. TEST_P(Test_TensorFlow_layers, pooling_reduce_max)
  441. {
  442. runTensorFlowNet("reduce_max"); // a MAX pooling over all spatial dimensions.
  443. }
  444. TEST_P(Test_TensorFlow_layers, pooling_reduce_sum)
  445. {
  446. runTensorFlowNet("reduce_sum"); // a SUM pooling over all spatial dimensions.
  447. }
  448. TEST_P(Test_TensorFlow_layers, pooling_reduce_sum_0_false)
  449. {
  450. runTensorFlowNet("reduce_sum_0_False");
  451. }
  452. TEST_P(Test_TensorFlow_layers, pooling_reduce_sum_1_false)
  453. {
  454. runTensorFlowNet("reduce_sum_1_False");
  455. }
  456. TEST_P(Test_TensorFlow_layers, pooling_reduce_sum_2_false)
  457. {
  458. runTensorFlowNet("reduce_sum_2_False");
  459. }
  460. TEST_P(Test_TensorFlow_layers, pooling_reduce_sum_3_false)
  461. {
  462. runTensorFlowNet("reduce_sum_3_False");
  463. }
  464. TEST_P(Test_TensorFlow_layers, pooling_reduce_sum_1_2_false)
  465. {
  466. #if defined(INF_ENGINE_RELEASE)
  467. if (backend == DNN_BACKEND_INFERENCE_ENGINE_NGRAPH && target == DNN_TARGET_MYRIAD)
  468. {
  469. default_l1 = 0.01f;
  470. }
  471. if (backend == DNN_BACKEND_INFERENCE_ENGINE_NGRAPH && target == DNN_TARGET_OPENCL_FP16)
  472. {
  473. default_l1 = 0.01f;
  474. }
  475. #endif
  476. runTensorFlowNet("reduce_sum_1_2_False");
  477. }
  478. TEST_P(Test_TensorFlow_layers, pooling_reduce_sum_0_true)
  479. {
  480. runTensorFlowNet("reduce_sum_0_True");
  481. }
  482. TEST_P(Test_TensorFlow_layers, pooling_reduce_sum_1_true)
  483. {
  484. runTensorFlowNet("reduce_sum_1_True");
  485. }
  486. TEST_P(Test_TensorFlow_layers, pooling_reduce_sum_2_true)
  487. {
  488. runTensorFlowNet("reduce_sum_2_True");
  489. }
  490. TEST_P(Test_TensorFlow_layers, pooling_reduce_sum_3_true)
  491. {
  492. runTensorFlowNet("reduce_sum_3_True");
  493. }
  494. TEST_P(Test_TensorFlow_layers, pooling_reduce_sum_1_2_true)
  495. {
  496. #if defined(INF_ENGINE_RELEASE)
  497. if (backend == DNN_BACKEND_INFERENCE_ENGINE_NGRAPH && target == DNN_TARGET_MYRIAD)
  498. {
  499. default_l1 = 0.01f;
  500. }
  501. if (backend == DNN_BACKEND_INFERENCE_ENGINE_NGRAPH && target == DNN_TARGET_OPENCL_FP16)
  502. {
  503. default_l1 = 0.01f;
  504. }
  505. #endif
  506. runTensorFlowNet("reduce_sum_1_2_True");
  507. }
  508. TEST_P(Test_TensorFlow_layers, max_pool_grad)
  509. {
  510. if (backend == DNN_BACKEND_INFERENCE_ENGINE_NN_BUILDER_2019)
  511. applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_NN_BUILDER);
  512. if (backend == DNN_BACKEND_INFERENCE_ENGINE_NGRAPH)
  513. applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_NGRAPH);
  514. runTensorFlowNet("max_pool_grad");
  515. }
  516. // TODO: fix tests and replace to pooling
  517. TEST_P(Test_TensorFlow_layers, ave_pool_same)
  518. {
  519. // Reference output values are in range [-0.519531, 0.112976]
  520. #if defined(INF_ENGINE_RELEASE) && INF_ENGINE_VER_MAJOR_GE(2019010000) && INF_ENGINE_VER_MAJOR_LT(2021040000)
  521. if (target == DNN_TARGET_MYRIAD && getInferenceEngineVPUType() == CV_DNN_INFERENCE_ENGINE_VPU_TYPE_MYRIAD_X)
  522. {
  523. if (backend == DNN_BACKEND_INFERENCE_ENGINE_NN_BUILDER_2019)
  524. applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_MYRIAD_X, CV_TEST_TAG_DNN_SKIP_IE_NN_BUILDER, CV_TEST_TAG_DNN_SKIP_IE_VERSION);
  525. else if (backend == DNN_BACKEND_INFERENCE_ENGINE_NGRAPH)
  526. applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_MYRIAD_X, CV_TEST_TAG_DNN_SKIP_IE_NGRAPH, CV_TEST_TAG_DNN_SKIP_IE_VERSION);
  527. }
  528. #endif
  529. runTensorFlowNet("ave_pool_same");
  530. }
  531. TEST_P(Test_TensorFlow_layers, MaxPooling3D)
  532. {
  533. #if defined(INF_ENGINE_RELEASE) && INF_ENGINE_VER_MAJOR_EQ(2021040000)
  534. if (backend == DNN_BACKEND_INFERENCE_ENGINE_NGRAPH)
  535. {
  536. // accuracy
  537. if (target == DNN_TARGET_OPENCL || target == DNN_TARGET_OPENCL_FP16)
  538. applyTestTag(target == DNN_TARGET_OPENCL ? CV_TEST_TAG_DNN_SKIP_IE_OPENCL : CV_TEST_TAG_DNN_SKIP_IE_OPENCL_FP16,
  539. CV_TEST_TAG_DNN_SKIP_IE_NGRAPH, CV_TEST_TAG_DNN_SKIP_IE_VERSION
  540. );
  541. // IE exception: [ GENERAL_ERROR ] AssertionFailed: !expired()
  542. if (target == DNN_TARGET_MYRIAD)
  543. applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_MYRIAD, CV_TEST_TAG_DNN_SKIP_IE_NGRAPH, CV_TEST_TAG_DNN_SKIP_IE_VERSION);
  544. }
  545. #endif
  546. #if defined(INF_ENGINE_RELEASE) && INF_ENGINE_VER_MAJOR_LT(2021040000)
  547. if (backend == DNN_BACKEND_INFERENCE_ENGINE_NN_BUILDER_2019 && target != DNN_TARGET_CPU)
  548. applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_NN_BUILDER); // Only CPU on DLIE backend is supported
  549. else if (backend == DNN_BACKEND_INFERENCE_ENGINE_NGRAPH && target != DNN_TARGET_CPU)
  550. applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_NGRAPH); // Only CPU on DLIE backend is supported
  551. #endif
  552. if (backend == DNN_BACKEND_OPENCV && target != DNN_TARGET_CPU)
  553. throw SkipTestException("Only CPU is supported"); // FIXIT use tags
  554. if (backend == DNN_BACKEND_VKCOM)
  555. applyTestTag(CV_TEST_TAG_DNN_SKIP_VULKAN);
  556. runTensorFlowNet("max_pool3d");
  557. }
  558. TEST_P(Test_TensorFlow_layers, AvePooling3D)
  559. {
  560. #if defined(INF_ENGINE_RELEASE) && INF_ENGINE_VER_MAJOR_LT(2021040000)
  561. if (backend == DNN_BACKEND_INFERENCE_ENGINE_NN_BUILDER_2019 && target != DNN_TARGET_CPU)
  562. applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_NN_BUILDER); // Only CPU on DLIE backend is supported
  563. else if (backend == DNN_BACKEND_INFERENCE_ENGINE_NGRAPH && target != DNN_TARGET_CPU)
  564. applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_NGRAPH); // Only CPU on DLIE backend is supported
  565. #endif
  566. if (backend == DNN_BACKEND_OPENCV && target != DNN_TARGET_CPU)
  567. throw SkipTestException("Only CPU is supported"); // FIXIT use tags
  568. if (backend == DNN_BACKEND_VKCOM)
  569. applyTestTag(CV_TEST_TAG_DNN_SKIP_VULKAN);
  570. runTensorFlowNet("ave_pool3d");
  571. }
  572. TEST_P(Test_TensorFlow_layers, deconvolution)
  573. {
  574. if (backend == DNN_BACKEND_CUDA)
  575. applyTestTag(CV_TEST_TAG_DNN_SKIP_CUDA);
  576. runTensorFlowNet("deconvolution");
  577. runTensorFlowNet("deconvolution_same");
  578. runTensorFlowNet("deconvolution_stride_2_same");
  579. runTensorFlowNet("deconvolution_adj_pad_valid");
  580. runTensorFlowNet("deconvolution_adj_pad_same");
  581. runTensorFlowNet("keras_deconv_valid");
  582. runTensorFlowNet("keras_deconv_same");
  583. runTensorFlowNet("keras_deconv_same_v2");
  584. }
  585. TEST_P(Test_TensorFlow_layers, matmul)
  586. {
  587. if (backend == DNN_BACKEND_OPENCV && target == DNN_TARGET_OPENCL_FP16)
  588. applyTestTag(CV_TEST_TAG_DNN_SKIP_OPENCL_FP16);
  589. runTensorFlowNet("matmul");
  590. runTensorFlowNet("nhwc_transpose_reshape_matmul");
  591. // Reference output values are in range [-5.688, 4.484]
  592. double l1 = target == DNN_TARGET_MYRIAD ? 6.1e-3 : default_l1;
  593. runTensorFlowNet("nhwc_reshape_matmul", false, l1);
  594. runTensorFlowNet("matmul_layout");
  595. runTensorFlowNet("two_inputs_matmul");
  596. }
  597. TEST_P(Test_TensorFlow_layers, batch_matmul)
  598. {
  599. if (backend == DNN_BACKEND_OPENCV && target == DNN_TARGET_OPENCL_FP16)
  600. applyTestTag(CV_TEST_TAG_DNN_SKIP_OPENCL_FP16);
  601. runTensorFlowNet("batch_matmul");
  602. }
  603. TEST_P(Test_TensorFlow_layers, square)
  604. {
  605. if (backend == DNN_BACKEND_OPENCV && target == DNN_TARGET_OPENCL_FP16)
  606. applyTestTag(CV_TEST_TAG_DNN_SKIP_OPENCL_FP16);
  607. runTensorFlowNet("square");
  608. }
  609. TEST_P(Test_TensorFlow_layers, reshape)
  610. {
  611. #if defined(INF_ENGINE_RELEASE) && INF_ENGINE_VER_MAJOR_LT(2021040000)
  612. if (backend == DNN_BACKEND_INFERENCE_ENGINE_NN_BUILDER_2019)
  613. applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_NN_BUILDER);
  614. if (backend == DNN_BACKEND_INFERENCE_ENGINE_NGRAPH)
  615. applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_NGRAPH);
  616. #endif
  617. runTensorFlowNet("shift_reshape_no_reorder");
  618. runTensorFlowNet("reshape_no_reorder");
  619. runTensorFlowNet("reshape_reduce");
  620. runTensorFlowNet("reshape_as_shape");
  621. }
  622. TEST_P(Test_TensorFlow_layers, flatten)
  623. {
  624. #if defined(INF_ENGINE_RELEASE)
  625. if (backend == DNN_BACKEND_INFERENCE_ENGINE_NN_BUILDER_2019 && target == DNN_TARGET_MYRIAD
  626. && getInferenceEngineVPUType() == CV_DNN_INFERENCE_ENGINE_VPU_TYPE_MYRIAD_2
  627. )
  628. applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_MYRIAD_2, CV_TEST_TAG_DNN_SKIP_IE_NN_BUILDER);
  629. #endif
  630. runTensorFlowNet("flatten", true);
  631. }
  632. TEST_P(Test_TensorFlow_layers, unfused_flatten)
  633. {
  634. runTensorFlowNet("unfused_flatten");
  635. runTensorFlowNet("unfused_flatten_unknown_batch");
  636. }
  637. TEST_P(Test_TensorFlow_layers, reshape_layer)
  638. {
  639. runTensorFlowNet("reshape_layer");
  640. }
  641. TEST_P(Test_TensorFlow_layers, reshape_nchw)
  642. {
  643. runTensorFlowNet("reshape_nchw");
  644. }
  645. TEST_P(Test_TensorFlow_layers, reshape_conv)
  646. {
  647. runTensorFlowNet("reshape_conv");
  648. }
  649. TEST_P(Test_TensorFlow_layers, leaky_relu)
  650. {
  651. #if defined(INF_ENGINE_RELEASE) && INF_ENGINE_VER_MAJOR_EQ(2018050000)
  652. if (backend == DNN_BACKEND_INFERENCE_ENGINE_NN_BUILDER_2019 && target == DNN_TARGET_OPENCL)
  653. applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_OPENCL, CV_TEST_TAG_DNN_SKIP_IE_NN_BUILDER, CV_TEST_TAG_DNN_SKIP_IE_VERSION);
  654. #endif
  655. runTensorFlowNet("leaky_relu");
  656. runTensorFlowNet("leaky_relu_order1");
  657. runTensorFlowNet("leaky_relu_order2");
  658. runTensorFlowNet("leaky_relu_order3");
  659. }
  660. TEST_P(Test_TensorFlow_layers, l2_normalize)
  661. {
  662. #if defined(INF_ENGINE_RELEASE) && INF_ENGINE_VER_MAJOR_GE(2019010000)
  663. if (backend == DNN_BACKEND_INFERENCE_ENGINE_NN_BUILDER_2019 && target == DNN_TARGET_MYRIAD
  664. && getInferenceEngineVPUType() == CV_DNN_INFERENCE_ENGINE_VPU_TYPE_MYRIAD_X
  665. )
  666. applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_MYRIAD_X, CV_TEST_TAG_DNN_SKIP_IE_NN_BUILDER, CV_TEST_TAG_DNN_SKIP_IE_VERSION);
  667. #endif
  668. runTensorFlowNet("l2_normalize");
  669. }
  670. TEST_P(Test_TensorFlow_layers, BiasAdd)
  671. {
  672. #if defined(INF_ENGINE_RELEASE) && INF_ENGINE_VER_MAJOR_GE(2019010000)
  673. if (backend == DNN_BACKEND_INFERENCE_ENGINE_NN_BUILDER_2019 && target == DNN_TARGET_MYRIAD
  674. && getInferenceEngineVPUType() == CV_DNN_INFERENCE_ENGINE_VPU_TYPE_MYRIAD_X
  675. )
  676. applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_MYRIAD_X, CV_TEST_TAG_DNN_SKIP_IE_NN_BUILDER, CV_TEST_TAG_DNN_SKIP_IE_VERSION);
  677. #endif
  678. runTensorFlowNet("bias_add_1");
  679. }
  680. TEST_P(Test_TensorFlow_layers, ExpandDims)
  681. {
  682. #if defined(INF_ENGINE_RELEASE) && INF_ENGINE_VER_MAJOR_EQ(2021040000)
  683. if (backend == DNN_BACKEND_INFERENCE_ENGINE_NGRAPH && target == DNN_TARGET_CPU)
  684. applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_CPU, CV_TEST_TAG_DNN_SKIP_IE_NGRAPH); // Layout::ANY is broken on CPU
  685. #endif
  686. #if defined(INF_ENGINE_RELEASE) && INF_ENGINE_VER_MAJOR_GE(2019010000)
  687. if (backend == DNN_BACKEND_INFERENCE_ENGINE_NN_BUILDER_2019 && target == DNN_TARGET_MYRIAD
  688. && getInferenceEngineVPUType() == CV_DNN_INFERENCE_ENGINE_VPU_TYPE_MYRIAD_X
  689. )
  690. applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_MYRIAD_X, CV_TEST_TAG_DNN_SKIP_IE_NN_BUILDER, CV_TEST_TAG_DNN_SKIP_IE_VERSION);
  691. #endif
  692. runTensorFlowNet("expand_dims_1");
  693. runTensorFlowNet("expand_dims_2");
  694. }
  695. // TODO: fix it and add to l2_normalize
  696. TEST_P(Test_TensorFlow_layers, l2_normalize_3d)
  697. {
  698. #if defined(INF_ENGINE_RELEASE) && INF_ENGINE_VER_MAJOR_EQ(2018050000)
  699. if (backend == DNN_BACKEND_INFERENCE_ENGINE_NN_BUILDER_2019
  700. && (target == DNN_TARGET_OPENCL || target == DNN_TARGET_OPENCL_FP16)
  701. )
  702. applyTestTag(target == DNN_TARGET_OPENCL ? CV_TEST_TAG_DNN_SKIP_IE_OPENCL : CV_TEST_TAG_DNN_SKIP_IE_OPENCL_FP16,
  703. CV_TEST_TAG_DNN_SKIP_IE_NN_BUILDER, CV_TEST_TAG_DNN_SKIP_IE_VERSION);
  704. #endif
  705. #if defined(INF_ENGINE_RELEASE)
  706. if (backend == DNN_BACKEND_INFERENCE_ENGINE_NN_BUILDER_2019 && target == DNN_TARGET_MYRIAD)
  707. applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_MYRIAD, CV_TEST_TAG_DNN_SKIP_IE_NN_BUILDER);
  708. if (backend == DNN_BACKEND_INFERENCE_ENGINE_NGRAPH && target == DNN_TARGET_MYRIAD)
  709. applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_MYRIAD, CV_TEST_TAG_DNN_SKIP_IE_NGRAPH);
  710. #endif
  711. runTensorFlowNet("l2_normalize_3d");
  712. }
  713. class Test_TensorFlow_diagnostics : public DNNTestLayer {
  714. public:
  715. Test_TensorFlow_diagnostics()
  716. {
  717. enableModelDiagnostics(true);
  718. skipModelImport(true);
  719. }
  720. ~Test_TensorFlow_diagnostics()
  721. {
  722. enableModelDiagnostics(false);
  723. skipModelImport(false);
  724. }
  725. void runFailingTensorFlowNet(const std::string& prefix, bool hasText = false)
  726. {
  727. std::string netPath = path(prefix + "_net.pb");
  728. std::string netConfig = (hasText ? path(prefix + "_net.pbtxt") : "");
  729. Net net = readNetFromTensorflow(netPath, netConfig);
  730. }
  731. };
  732. TEST_P(Test_TensorFlow_diagnostics, not_implemented_layer)
  733. {
  734. runFailingTensorFlowNet("not_implemented_layer");
  735. }
  736. TEST_P(Test_TensorFlow_diagnostics, broken_parameters)
  737. {
  738. runFailingTensorFlowNet("broken_layer");
  739. }
  740. INSTANTIATE_TEST_CASE_P(/**/, Test_TensorFlow_diagnostics, dnnBackendsAndTargets());
  741. class Test_TensorFlow_nets : public DNNTestLayer {};
  742. TEST_P(Test_TensorFlow_nets, MobileNet_SSD)
  743. {
  744. #if defined(INF_ENGINE_RELEASE)
  745. if (target == DNN_TARGET_MYRIAD)
  746. {
  747. #if INF_ENGINE_VER_MAJOR_GE(2019020000)
  748. if (getInferenceEngineVPUType() == CV_DNN_INFERENCE_ENGINE_VPU_TYPE_MYRIAD_X)
  749. applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_MYRIAD_X,
  750. backend == DNN_BACKEND_INFERENCE_ENGINE_NN_BUILDER_2019 ?
  751. CV_TEST_TAG_DNN_SKIP_IE_NN_BUILDER :
  752. CV_TEST_TAG_DNN_SKIP_IE_NGRAPH,
  753. CV_TEST_TAG_DNN_SKIP_IE_VERSION);
  754. #endif
  755. if (backend == DNN_BACKEND_INFERENCE_ENGINE_NGRAPH)
  756. applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_MYRIAD, CV_TEST_TAG_DNN_SKIP_IE_NGRAPH, CV_TEST_TAG_DNN_SKIP_IE_VERSION);
  757. }
  758. #endif
  759. checkBackend();
  760. std::string imgPath = findDataFile("dnn/street.png");
  761. std::string netConfig = findDataFile("dnn/ssd_mobilenet_v1_coco.pbtxt");
  762. std::string netPath = findDataFile("dnn/ssd_mobilenet_v1_coco.pb", false);
  763. Mat inp;
  764. resize(imread(imgPath), inp, Size(300, 300));
  765. inp = blobFromImage(inp, 1.0f / 127.5, Size(), Scalar(127.5, 127.5, 127.5), true);
  766. Mat ref = blobFromNPY(findDataFile("dnn/tensorflow/ssd_mobilenet_v1_coco.detection_out.npy"));
  767. Net net = readNetFromTensorflow(netPath, netConfig);
  768. net.setPreferableBackend(backend);
  769. net.setPreferableTarget(target);
  770. net.setInput(inp);
  771. Mat out = net.forward();
  772. double scoreDiff = default_l1, iouDiff = default_lInf;
  773. if (target == DNN_TARGET_OPENCL_FP16 || target == DNN_TARGET_MYRIAD)
  774. {
  775. scoreDiff = 0.01;
  776. iouDiff = 0.1;
  777. }
  778. else if (target == DNN_TARGET_CUDA_FP16)
  779. {
  780. iouDiff = 0.04;
  781. }
  782. normAssertDetections(ref, out, "", 0.2, scoreDiff, iouDiff);
  783. #if defined(INF_ENGINE_RELEASE) && INF_ENGINE_RELEASE >= 2019010000
  784. expectNoFallbacksFromIE(net);
  785. #endif
  786. }
  787. TEST_P(Test_TensorFlow_nets, Inception_v2_SSD)
  788. {
  789. applyTestTag(target == DNN_TARGET_CPU ? CV_TEST_TAG_MEMORY_512MB : CV_TEST_TAG_MEMORY_1GB);
  790. #if defined(INF_ENGINE_RELEASE) && INF_ENGINE_VER_MAJOR_LE(2019010000)
  791. if (backend == DNN_BACKEND_INFERENCE_ENGINE_NN_BUILDER_2019 && target == DNN_TARGET_MYRIAD &&
  792. getInferenceEngineVPUType() == CV_DNN_INFERENCE_ENGINE_VPU_TYPE_MYRIAD_X)
  793. applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_MYRIAD_X, CV_TEST_TAG_DNN_SKIP_IE_NN_BUILDER, CV_TEST_TAG_DNN_SKIP_IE_VERSION);
  794. #endif
  795. checkBackend();
  796. Mat img = imread(findDataFile("dnn/street.png"));
  797. std::string proto = findDataFile("dnn/ssd_inception_v2_coco_2017_11_17.pbtxt");
  798. std::string model = findDataFile("dnn/ssd_inception_v2_coco_2017_11_17.pb", false);
  799. Net net = readNetFromTensorflow(model, proto);
  800. Mat blob = blobFromImage(img, 1.0f, Size(300, 300), Scalar(), true, false);
  801. net.setPreferableBackend(backend);
  802. net.setPreferableTarget(target);
  803. net.setInput(blob);
  804. // Output has shape 1x1xNx7 where N - number of detections.
  805. // An every detection is a vector of values [id, classId, confidence, left, top, right, bottom]
  806. Mat out = net.forward();
  807. Mat ref = (Mat_<float>(5, 7) << 0, 1, 0.90176028, 0.19872092, 0.36311883, 0.26461923, 0.63498729,
  808. 0, 3, 0.93569964, 0.64865261, 0.45906419, 0.80675775, 0.65708131,
  809. 0, 3, 0.75838411, 0.44668293, 0.45907149, 0.49459291, 0.52197015,
  810. 0, 10, 0.95932811, 0.38349164, 0.32528657, 0.40387636, 0.39165527,
  811. 0, 10, 0.93973452, 0.66561931, 0.37841269, 0.68074018, 0.42907384);
  812. double scoreDiff = default_l1, iouDiff = default_lInf;
  813. if (target == DNN_TARGET_OPENCL_FP16 || target == DNN_TARGET_MYRIAD)
  814. {
  815. scoreDiff = 0.0097;
  816. iouDiff = 0.09;
  817. }
  818. else if (target == DNN_TARGET_CUDA_FP16)
  819. {
  820. scoreDiff = 6e-3;
  821. iouDiff = 0.05;
  822. }
  823. normAssertDetections(ref, out, "", 0.5, scoreDiff, iouDiff);
  824. expectNoFallbacksFromIE(net);
  825. }
  826. TEST_P(Test_TensorFlow_nets, MobileNet_v1_SSD)
  827. {
  828. checkBackend();
  829. std::string proto = findDataFile("dnn/ssd_mobilenet_v1_coco_2017_11_17.pbtxt");
  830. std::string model = findDataFile("dnn/ssd_mobilenet_v1_coco_2017_11_17.pb", false);
  831. Net net = readNetFromTensorflow(model, proto);
  832. Mat img = imread(findDataFile("dnn/dog416.png"));
  833. Mat blob = blobFromImage(img, 1.0f, Size(300, 300), Scalar(), true, false);
  834. net.setPreferableBackend(backend);
  835. net.setPreferableTarget(target);
  836. net.setInput(blob);
  837. Mat out = net.forward();
  838. Mat ref = blobFromNPY(findDataFile("dnn/tensorflow/ssd_mobilenet_v1_coco_2017_11_17.detection_out.npy"));
  839. float scoreDiff = 1.5e-5, iouDiff = 1e-3;
  840. float detectionConfThresh = (target == DNN_TARGET_MYRIAD) ? 0.35 : 0.3;
  841. if (target == DNN_TARGET_OPENCL_FP16 || target == DNN_TARGET_MYRIAD)
  842. {
  843. scoreDiff = 0.011;
  844. iouDiff = 0.012;
  845. }
  846. else if (target == DNN_TARGET_CUDA_FP16)
  847. {
  848. scoreDiff = 0.006;
  849. iouDiff = 0.01;
  850. }
  851. #if defined(INF_ENGINE_RELEASE)
  852. if (backend == DNN_BACKEND_INFERENCE_ENGINE_NN_BUILDER_2019 && target == DNN_TARGET_MYRIAD &&
  853. getInferenceEngineVPUType() == CV_DNN_INFERENCE_ENGINE_VPU_TYPE_MYRIAD_X)
  854. {
  855. scoreDiff = 0.061;
  856. iouDiff = 0.12;
  857. detectionConfThresh = 0.36;
  858. }
  859. #endif
  860. normAssertDetections(ref, out, "", detectionConfThresh, scoreDiff, iouDiff);
  861. expectNoFallbacksFromIE(net);
  862. }
  863. TEST_P(Test_TensorFlow_nets, Faster_RCNN_inception_v2_coco_2018_01_28)
  864. {
  865. applyTestTag(
  866. (target == DNN_TARGET_CPU ? CV_TEST_TAG_MEMORY_1GB : CV_TEST_TAG_MEMORY_2GB),
  867. CV_TEST_TAG_LONG,
  868. CV_TEST_TAG_DEBUG_VERYLONG
  869. );
  870. #ifdef INF_ENGINE_RELEASE
  871. if (backend == DNN_BACKEND_INFERENCE_ENGINE_NN_BUILDER_2019 &&
  872. (INF_ENGINE_VER_MAJOR_LT(2019020000) || target != DNN_TARGET_CPU))
  873. applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_NN_BUILDER, CV_TEST_TAG_DNN_SKIP_IE_VERSION);
  874. if (INF_ENGINE_VER_MAJOR_GT(2019030000) &&
  875. backend == DNN_BACKEND_INFERENCE_ENGINE_NGRAPH && target == DNN_TARGET_MYRIAD)
  876. applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_MYRIAD, CV_TEST_TAG_DNN_SKIP_IE_NGRAPH);
  877. #endif
  878. #if defined(INF_ENGINE_RELEASE) && INF_ENGINE_VER_MAJOR_LT(2021040000)
  879. // segfault: inference-engine/thirdparty/clDNN/src/gpu/detection_output_cpu.cpp:111:
  880. // Assertion `prior_height > 0' failed.
  881. if (backend == DNN_BACKEND_INFERENCE_ENGINE_NGRAPH && target == DNN_TARGET_OPENCL_FP16)
  882. applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_OPENCL_FP16, CV_TEST_TAG_DNN_SKIP_IE_NGRAPH);
  883. #endif
  884. if (backend == DNN_BACKEND_CUDA && target == DNN_TARGET_CUDA_FP16)
  885. applyTestTag(CV_TEST_TAG_DNN_SKIP_CUDA_FP16);
  886. checkBackend();
  887. double scoresDiff = 1e-5;
  888. double iouDiff = 1e-4;
  889. if (backend == DNN_BACKEND_INFERENCE_ENGINE_NN_BUILDER_2019 || backend == DNN_BACKEND_INFERENCE_ENGINE_NGRAPH)
  890. {
  891. scoresDiff = 0.02;
  892. iouDiff = 0.1;
  893. }
  894. std::string name = "faster_rcnn_inception_v2_coco_2018_01_28";
  895. {
  896. std::string proto = findDataFile("dnn/" + name + ".pbtxt");
  897. std::string model = findDataFile("dnn/" + name + ".pb", false);
  898. Net net = readNetFromTensorflow(model, proto);
  899. net.setPreferableBackend(backend);
  900. net.setPreferableTarget(target);
  901. Mat img = imread(findDataFile("dnn/dog416.png"));
  902. Mat blob = blobFromImage(img, 1.0f, Size(800, 600), Scalar(), true, false);
  903. net.setInput(blob);
  904. Mat out = net.forward();
  905. Mat ref = blobFromNPY(findDataFile("dnn/tensorflow/" + name + ".detection_out.npy"));
  906. // accuracy (both OpenCV & IE)
  907. if (target == DNN_TARGET_OPENCL_FP16)
  908. applyTestTag(CV_TEST_TAG_DNN_SKIP_OPENCL_FP16);
  909. normAssertDetections(ref, out, name.c_str(), 0.3, scoresDiff, iouDiff);
  910. }
  911. }
  912. TEST_P(Test_TensorFlow_nets, Faster_RCNN_resnet50_coco_2018_01_28)
  913. {
  914. applyTestTag(
  915. (target == DNN_TARGET_CPU ? CV_TEST_TAG_MEMORY_1GB : CV_TEST_TAG_MEMORY_2GB),
  916. CV_TEST_TAG_LONG,
  917. CV_TEST_TAG_DEBUG_VERYLONG
  918. );
  919. #if defined(INF_ENGINE_RELEASE) && INF_ENGINE_VER_MAJOR_EQ(2021040000)
  920. // IE exception: Ngraph operation Transpose with name FirstStageBoxPredictor/ClassPredictor/reshape_1/nhwc has dynamic output shape on 0 port, but CPU plug-in supports only static shape
  921. if (backend == DNN_BACKEND_INFERENCE_ENGINE_NGRAPH && (target == DNN_TARGET_OPENCL || target == DNN_TARGET_OPENCL_FP16))
  922. applyTestTag(target == DNN_TARGET_OPENCL ? CV_TEST_TAG_DNN_SKIP_IE_OPENCL : CV_TEST_TAG_DNN_SKIP_IE_OPENCL_FP16,
  923. CV_TEST_TAG_DNN_SKIP_IE_NGRAPH, CV_TEST_TAG_DNN_SKIP_IE_VERSION
  924. );
  925. #endif
  926. #ifdef INF_ENGINE_RELEASE
  927. if (backend == DNN_BACKEND_INFERENCE_ENGINE_NN_BUILDER_2019 &&
  928. (INF_ENGINE_VER_MAJOR_LT(2019020000) || target != DNN_TARGET_CPU))
  929. applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_NN_BUILDER, CV_TEST_TAG_DNN_SKIP_IE_VERSION);
  930. if (INF_ENGINE_VER_MAJOR_GT(2019030000) &&
  931. backend == DNN_BACKEND_INFERENCE_ENGINE_NGRAPH && target == DNN_TARGET_MYRIAD)
  932. applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_MYRIAD, CV_TEST_TAG_DNN_SKIP_IE_NGRAPH);
  933. #endif
  934. #if defined(INF_ENGINE_RELEASE) && INF_ENGINE_VER_MAJOR_LT(2021040000)
  935. // segfault: inference-engine/thirdparty/clDNN/src/gpu/detection_output_cpu.cpp:111:
  936. // Assertion `prior_height > 0' failed.
  937. if (backend == DNN_BACKEND_INFERENCE_ENGINE_NGRAPH && target == DNN_TARGET_OPENCL_FP16)
  938. applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_OPENCL_FP16, CV_TEST_TAG_DNN_SKIP_IE_NGRAPH);
  939. #endif
  940. if (backend == DNN_BACKEND_CUDA && target == DNN_TARGET_CUDA_FP16)
  941. applyTestTag(CV_TEST_TAG_DNN_SKIP_CUDA_FP16);
  942. checkBackend();
  943. double scoresDiff = backend == DNN_BACKEND_INFERENCE_ENGINE_NN_BUILDER_2019 ? 2.9e-5 : 1e-5;
  944. double iouDiff = 1e-4;
  945. if (target == DNN_TARGET_CUDA)
  946. {
  947. scoresDiff = 0.06;
  948. iouDiff = 0.08;
  949. }
  950. std::string name = "faster_rcnn_resnet50_coco_2018_01_28";
  951. {
  952. std::string proto = findDataFile("dnn/" + name + ".pbtxt");
  953. std::string model = findDataFile("dnn/" + name + ".pb", false);
  954. Net net = readNetFromTensorflow(model, proto);
  955. net.setPreferableBackend(backend);
  956. net.setPreferableTarget(target);
  957. Mat img = imread(findDataFile("dnn/dog416.png"));
  958. Mat blob = blobFromImage(img, 1.0f, Size(800, 600), Scalar(), true, false);
  959. net.setInput(blob);
  960. Mat out = net.forward();
  961. Mat ref = blobFromNPY(findDataFile("dnn/tensorflow/" + name + ".detection_out.npy"));
  962. // accuracy
  963. if (target == DNN_TARGET_OPENCL_FP16)
  964. applyTestTag(CV_TEST_TAG_DNN_SKIP_OPENCL_FP16);
  965. normAssertDetections(ref, out, name.c_str(), 0.3, scoresDiff, iouDiff);
  966. }
  967. }
  968. TEST_P(Test_TensorFlow_nets, MobileNet_v1_SSD_PPN)
  969. {
  970. #if defined(INF_ENGINE_RELEASE) && INF_ENGINE_VER_MAJOR_EQ(2018050000)
  971. if (backend == DNN_BACKEND_INFERENCE_ENGINE_NN_BUILDER_2019 && (target == DNN_TARGET_OPENCL || target == DNN_TARGET_OPENCL_FP16))
  972. applyTestTag(target == DNN_TARGET_OPENCL ? CV_TEST_TAG_DNN_SKIP_IE_OPENCL : CV_TEST_TAG_DNN_SKIP_IE_OPENCL_FP16,
  973. CV_TEST_TAG_DNN_SKIP_IE_NN_BUILDER, CV_TEST_TAG_DNN_SKIP_IE_VERSION);
  974. #endif
  975. checkBackend();
  976. std::string proto = findDataFile("dnn/ssd_mobilenet_v1_ppn_coco.pbtxt");
  977. std::string model = findDataFile("dnn/ssd_mobilenet_v1_ppn_coco.pb", false);
  978. Net net = readNetFromTensorflow(model, proto);
  979. Mat img = imread(findDataFile("dnn/dog416.png"));
  980. Mat ref = blobFromNPY(findDataFile("dnn/tensorflow/ssd_mobilenet_v1_ppn_coco.detection_out.npy"));
  981. Mat blob = blobFromImage(img, 1.0f, Size(300, 300), Scalar(), true, false);
  982. net.setPreferableBackend(backend);
  983. net.setPreferableTarget(target);
  984. net.setInput(blob);
  985. Mat out = net.forward();
  986. double scoreDiff = 1.1e-5, iouDiff = default_lInf;
  987. if (target == DNN_TARGET_OPENCL_FP16 || target == DNN_TARGET_MYRIAD)
  988. {
  989. scoreDiff = 0.048;
  990. iouDiff = 0.058;
  991. }
  992. else if (target == DNN_TARGET_CUDA_FP16)
  993. {
  994. scoreDiff = 0.006;
  995. iouDiff = 0.05;
  996. }
  997. normAssertDetections(ref, out, "", 0.45, scoreDiff, iouDiff);
  998. expectNoFallbacksFromIE(net);
  999. }
  1000. TEST_P(Test_TensorFlow_nets, opencv_face_detector_uint8)
  1001. {
  1002. checkBackend();
  1003. std::string proto = findDataFile("dnn/opencv_face_detector.pbtxt");
  1004. std::string model = findDataFile("dnn/opencv_face_detector_uint8.pb", false);
  1005. Net net = readNetFromTensorflow(model, proto);
  1006. Mat img = imread(findDataFile("gpu/lbpcascade/er.png"));
  1007. Mat blob = blobFromImage(img, 1.0, Size(), Scalar(104.0, 177.0, 123.0), false, false);
  1008. net.setPreferableBackend(backend);
  1009. net.setPreferableTarget(target);
  1010. net.setInput(blob);
  1011. // Output has shape 1x1xNx7 where N - number of detections.
  1012. // An every detection is a vector of values [id, classId, confidence, left, top, right, bottom]
  1013. Mat out = net.forward();
  1014. // References are from test for Caffe model.
  1015. Mat ref = (Mat_<float>(6, 7) << 0, 1, 0.99520785, 0.80997437, 0.16379407, 0.87996572, 0.26685631,
  1016. 0, 1, 0.9934696, 0.2831718, 0.50738752, 0.345781, 0.5985168,
  1017. 0, 1, 0.99096733, 0.13629119, 0.24892329, 0.19756334, 0.3310290,
  1018. 0, 1, 0.98977017, 0.23901358, 0.09084064, 0.29902688, 0.1769477,
  1019. 0, 1, 0.97203469, 0.67965847, 0.06876482, 0.73999709, 0.1513494,
  1020. 0, 1, 0.95097077, 0.51901293, 0.45863652, 0.5777427, 0.5347801);
  1021. double scoreDiff = 3.4e-3, iouDiff = 1e-2;
  1022. if (target == DNN_TARGET_OPENCL_FP16 || target == DNN_TARGET_MYRIAD)
  1023. {
  1024. scoreDiff = 4e-3;
  1025. iouDiff = 0.024;
  1026. }
  1027. else if (target == DNN_TARGET_CUDA_FP16)
  1028. {
  1029. scoreDiff = 4e-3;
  1030. iouDiff = 0.02;
  1031. }
  1032. normAssertDetections(ref, out, "", 0.9, scoreDiff, iouDiff);
  1033. expectNoFallbacksFromIE(net);
  1034. }
  1035. // inp = cv.imread('opencv_extra/testdata/cv/ximgproc/sources/08.png')
  1036. // inp = inp[:,:,[2, 1, 0]].astype(np.float32).reshape(1, 512, 512, 3)
  1037. // outs = sess.run([sess.graph.get_tensor_by_name('feature_fusion/Conv_7/Sigmoid:0'),
  1038. // sess.graph.get_tensor_by_name('feature_fusion/concat_3:0')],
  1039. // feed_dict={'input_images:0': inp})
  1040. // scores = np.ascontiguousarray(outs[0].transpose(0, 3, 1, 2))
  1041. // geometry = np.ascontiguousarray(outs[1].transpose(0, 3, 1, 2))
  1042. // np.save('east_text_detection.scores.npy', scores)
  1043. // np.save('east_text_detection.geometry.npy', geometry)
  1044. TEST_P(Test_TensorFlow_nets, EAST_text_detection)
  1045. {
  1046. applyTestTag(
  1047. (target == DNN_TARGET_CPU ? CV_TEST_TAG_MEMORY_512MB : CV_TEST_TAG_MEMORY_1GB),
  1048. CV_TEST_TAG_DEBUG_LONG
  1049. );
  1050. #if defined(INF_ENGINE_RELEASE)
  1051. if (backend == DNN_BACKEND_INFERENCE_ENGINE_NN_BUILDER_2019 && target == DNN_TARGET_MYRIAD)
  1052. applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_MYRIAD, CV_TEST_TAG_DNN_SKIP_IE_NN_BUILDER);
  1053. if (backend == DNN_BACKEND_INFERENCE_ENGINE_NGRAPH && target == DNN_TARGET_MYRIAD)
  1054. applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_MYRIAD, CV_TEST_TAG_DNN_SKIP_IE_NGRAPH);
  1055. if (backend == DNN_BACKEND_INFERENCE_ENGINE_NN_BUILDER_2019 && target == DNN_TARGET_OPENCL_FP16 &&
  1056. (INF_ENGINE_VER_MAJOR_EQ(2019020000) || INF_ENGINE_VER_MAJOR_GE(2020010000))
  1057. )
  1058. applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_OPENCL_FP16, CV_TEST_TAG_DNN_SKIP_IE_NN_BUILDER, CV_TEST_TAG_DNN_SKIP_IE_VERSION);
  1059. if (backend == DNN_BACKEND_INFERENCE_ENGINE_NGRAPH && target == DNN_TARGET_OPENCL_FP16)
  1060. applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_OPENCL_FP16, CV_TEST_TAG_DNN_SKIP_IE_NGRAPH, CV_TEST_TAG_DNN_SKIP_IE_VERSION);
  1061. #endif
  1062. checkBackend();
  1063. std::string netPath = findDataFile("dnn/frozen_east_text_detection.pb", false);
  1064. std::string imgPath = findDataFile("cv/ximgproc/sources/08.png");
  1065. std::string refScoresPath = findDataFile("dnn/east_text_detection.scores.npy");
  1066. std::string refGeometryPath = findDataFile("dnn/east_text_detection.geometry.npy");
  1067. Net net = readNet(netPath);
  1068. net.setPreferableBackend(backend);
  1069. net.setPreferableTarget(target);
  1070. Mat img = imread(imgPath);
  1071. Mat inp = blobFromImage(img, 1.0, Size(), Scalar(123.68, 116.78, 103.94), true, false);
  1072. net.setInput(inp);
  1073. std::vector<Mat> outs;
  1074. std::vector<String> outNames(2);
  1075. outNames[0] = "feature_fusion/Conv_7/Sigmoid";
  1076. outNames[1] = "feature_fusion/concat_3";
  1077. net.forward(outs, outNames);
  1078. Mat scores = outs[0];
  1079. Mat geometry = outs[1];
  1080. // Scores are in range [0, 1]. Geometry values are in range [-0.23, 290]
  1081. double l1_scores = default_l1, lInf_scores = default_lInf;
  1082. double l1_geometry = default_l1, lInf_geometry = default_lInf;
  1083. if (target == DNN_TARGET_OPENCL_FP16)
  1084. {
  1085. lInf_scores = backend == DNN_BACKEND_INFERENCE_ENGINE_NN_BUILDER_2019 ? 0.16 : 0.11;
  1086. l1_geometry = 0.28; lInf_geometry = 5.94;
  1087. }
  1088. else if (target == DNN_TARGET_MYRIAD)
  1089. {
  1090. lInf_scores = 0.41;
  1091. l1_geometry = 0.28; lInf_geometry = 5.94;
  1092. }
  1093. else if (target == DNN_TARGET_CUDA_FP16)
  1094. {
  1095. lInf_scores = 0.1;
  1096. l1_geometry = 0.3; lInf_geometry = 7;
  1097. }
  1098. else
  1099. {
  1100. l1_geometry = 1e-4, lInf_geometry = 3e-3;
  1101. }
  1102. normAssert(scores, blobFromNPY(refScoresPath), "scores", l1_scores, lInf_scores);
  1103. normAssert(geometry, blobFromNPY(refGeometryPath), "geometry", l1_geometry, lInf_geometry);
  1104. expectNoFallbacksFromIE(net);
  1105. }
  1106. INSTANTIATE_TEST_CASE_P(/**/, Test_TensorFlow_nets, dnnBackendsAndTargets());
  1107. TEST_P(Test_TensorFlow_layers, fp16_weights_fp16_single_conv)
  1108. {
  1109. float l1 = 0.00078, lInf = 0.012;
  1110. runTensorFlowNet("fp16_single_conv", false, l1, lInf);
  1111. }
  1112. TEST_P(Test_TensorFlow_layers, fp16_weights_fp16_max_pool_odd_same)
  1113. {
  1114. #if defined(INF_ENGINE_RELEASE) && INF_ENGINE_VER_MAJOR_GE(2020020000)
  1115. if (backend == DNN_BACKEND_INFERENCE_ENGINE_NGRAPH && target == DNN_TARGET_MYRIAD)
  1116. applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_MYRIAD, CV_TEST_TAG_DNN_SKIP_IE_NGRAPH, CV_TEST_TAG_DNN_SKIP_IE_VERSION);
  1117. #endif
  1118. float l1 = 0.00078, lInf = 0.012;
  1119. runTensorFlowNet("fp16_max_pool_odd_same", false, l1, lInf);
  1120. }
  1121. TEST_P(Test_TensorFlow_layers, fp16_weights_fp16_eltwise_add_mul)
  1122. {
  1123. float l1 = 0.00078, lInf = 0.012;
  1124. runTensorFlowNet("fp16_eltwise_add_mul", false, l1, lInf);
  1125. }
  1126. TEST_P(Test_TensorFlow_layers, fp16_weights_fp16_pad_and_concat)
  1127. {
  1128. float l1 = 0.00078, lInf = 0.012;
  1129. runTensorFlowNet("fp16_pad_and_concat", false, l1, lInf);
  1130. }
  1131. TEST_P(Test_TensorFlow_layers, fp16_weights_fp16_padding_valid)
  1132. {
  1133. float l1 = 0.00078, lInf = 0.012;
  1134. runTensorFlowNet("fp16_padding_valid", false, l1, lInf);
  1135. }
  1136. TEST_P(Test_TensorFlow_layers, fp16_weights_fp16_max_pool_even)
  1137. {
  1138. #if defined(INF_ENGINE_RELEASE) && INF_ENGINE_VER_MAJOR_GE(2020020000)
  1139. if (backend == DNN_BACKEND_INFERENCE_ENGINE_NGRAPH && target == DNN_TARGET_MYRIAD)
  1140. applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_MYRIAD, CV_TEST_TAG_DNN_SKIP_IE_NGRAPH, CV_TEST_TAG_DNN_SKIP_IE_VERSION);
  1141. #endif
  1142. float l1 = 0.00078, lInf = 0.012;
  1143. // Reference output values are in range [0.0889, 1.651]
  1144. runTensorFlowNet("fp16_max_pool_even", false, (target == DNN_TARGET_MYRIAD) ? 0.003 : l1, lInf);
  1145. }
  1146. TEST_P(Test_TensorFlow_layers, fp16_weights_fp16_deconvolution)
  1147. {
  1148. float l1 = 0.00078, lInf = 0.012;
  1149. if (target == DNN_TARGET_MYRIAD) {
  1150. l1 = 0.0041;
  1151. lInf = 0.024;
  1152. }
  1153. // Reference output values are in range [0, 10.75]
  1154. runTensorFlowNet("fp16_deconvolution", false, l1, lInf);
  1155. }
  1156. TEST_P(Test_TensorFlow_layers, fp16_weights_fp16_max_pool_odd_valid)
  1157. {
  1158. #if defined(INF_ENGINE_RELEASE) && INF_ENGINE_VER_MAJOR_GE(2020020000)
  1159. if (backend == DNN_BACKEND_INFERENCE_ENGINE_NGRAPH && target == DNN_TARGET_MYRIAD)
  1160. applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_MYRIAD, CV_TEST_TAG_DNN_SKIP_IE_NGRAPH, CV_TEST_TAG_DNN_SKIP_IE_VERSION);
  1161. #endif
  1162. float l1 = 0.00078, lInf = 0.012;
  1163. if (target == DNN_TARGET_MYRIAD) {
  1164. l1 = 0.0041;
  1165. lInf = 0.024;
  1166. }
  1167. // Reference output values are in range [0.418, 2.297]
  1168. runTensorFlowNet("fp16_max_pool_odd_valid", false, l1, lInf);
  1169. }
  1170. TEST_P(Test_TensorFlow_layers, fp16_padding_same)
  1171. {
  1172. // Reference output values are in range [-3.504, -0.002]
  1173. runTensorFlowNet("fp16_padding_same", false, 7e-4, 4e-3);
  1174. }
  1175. TEST_P(Test_TensorFlow_layers, defun)
  1176. {
  1177. runTensorFlowNet("defun_dropout");
  1178. }
  1179. TEST_P(Test_TensorFlow_layers, quantized)
  1180. {
  1181. runTensorFlowNet("uint8_single_conv");
  1182. }
  1183. TEST_P(Test_TensorFlow_layers, lstm)
  1184. {
  1185. if(backend == DNN_BACKEND_CUDA)
  1186. applyTestTag(CV_TEST_TAG_DNN_SKIP_CUDA); /* not supported */
  1187. #if defined(INF_ENGINE_RELEASE) && INF_ENGINE_VER_MAJOR_LT(2021040000)
  1188. if (backend == DNN_BACKEND_INFERENCE_ENGINE_NN_BUILDER_2019)
  1189. applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_NN_BUILDER);
  1190. if (backend == DNN_BACKEND_INFERENCE_ENGINE_NGRAPH)
  1191. applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_NGRAPH);
  1192. #endif
  1193. #if defined(INF_ENGINE_RELEASE) && INF_ENGINE_VER_MAJOR_EQ(2021040000)
  1194. if (backend == DNN_BACKEND_INFERENCE_ENGINE_NGRAPH)
  1195. {
  1196. // Exception: Ngraph operation Reshape with name Reshape has dynamic output shape on 0 port, but CPU plug-in supports only static shape
  1197. if (target == DNN_TARGET_OPENCL || target == DNN_TARGET_OPENCL_FP16)
  1198. applyTestTag(target == DNN_TARGET_OPENCL ? CV_TEST_TAG_DNN_SKIP_IE_OPENCL : CV_TEST_TAG_DNN_SKIP_IE_OPENCL_FP16,
  1199. CV_TEST_TAG_DNN_SKIP_IE_NGRAPH, CV_TEST_TAG_DNN_SKIP_IE_VERSION
  1200. );
  1201. // Xlink
  1202. if (target == DNN_TARGET_MYRIAD)
  1203. applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_MYRIAD, CV_TEST_TAG_DNN_SKIP_IE_NGRAPH, CV_TEST_TAG_DNN_SKIP_IE_VERSION);
  1204. }
  1205. #endif
  1206. if (backend == DNN_BACKEND_OPENCV && target == DNN_TARGET_OPENCL_FP16)
  1207. applyTestTag(CV_TEST_TAG_DNN_SKIP_OPENCL_FP16);
  1208. runTensorFlowNet("lstm", true);
  1209. runTensorFlowNet("lstm", true, 0.0, 0.0, true);
  1210. }
  1211. TEST_P(Test_TensorFlow_layers, split)
  1212. {
  1213. if (backend == DNN_BACKEND_INFERENCE_ENGINE_NN_BUILDER_2019 && target == DNN_TARGET_MYRIAD)
  1214. applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_MYRIAD, CV_TEST_TAG_DNN_SKIP_IE_NN_BUILDER);
  1215. if (backend == DNN_BACKEND_INFERENCE_ENGINE_NGRAPH)
  1216. applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_NGRAPH);
  1217. runTensorFlowNet("split");
  1218. }
  1219. TEST_P(Test_TensorFlow_layers, split_equals)
  1220. {
  1221. if (backend == DNN_BACKEND_INFERENCE_ENGINE_NN_BUILDER_2019)
  1222. applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_NN_BUILDER);
  1223. if (backend == DNN_BACKEND_INFERENCE_ENGINE_NGRAPH)
  1224. applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_NGRAPH);
  1225. runTensorFlowNet("split_equals");
  1226. }
  1227. TEST_P(Test_TensorFlow_layers, resize_nearest_neighbor)
  1228. {
  1229. runTensorFlowNet("resize_nearest_neighbor");
  1230. runTensorFlowNet("keras_upsampling2d");
  1231. }
  1232. TEST_P(Test_TensorFlow_layers, resize_nearest_neighbor_align_corners)
  1233. {
  1234. runTensorFlowNet("resize_nearest_neighbor", false, 0.0, 0.0, false, "_align_corners");
  1235. }
  1236. TEST_P(Test_TensorFlow_layers, resize_nearest_neighbor_half_pixel)
  1237. {
  1238. #if defined(INF_ENGINE_RELEASE) && INF_ENGINE_VER_MAJOR_LT(2021040000)
  1239. if (backend == DNN_BACKEND_INFERENCE_ENGINE_NGRAPH)
  1240. applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_NGRAPH);
  1241. #endif
  1242. runTensorFlowNet("resize_nearest_neighbor", false, 0.0, 0.0, false, "_half_pixel");
  1243. }
  1244. TEST_P(Test_TensorFlow_layers, fused_resize_conv)
  1245. {
  1246. runTensorFlowNet("fused_resize_conv");
  1247. }
  1248. TEST_P(Test_TensorFlow_layers, slice)
  1249. {
  1250. if (backend == DNN_BACKEND_INFERENCE_ENGINE_NN_BUILDER_2019 &&
  1251. (target == DNN_TARGET_OPENCL || target == DNN_TARGET_OPENCL_FP16))
  1252. applyTestTag(target == DNN_TARGET_OPENCL ? CV_TEST_TAG_DNN_SKIP_IE_OPENCL : CV_TEST_TAG_DNN_SKIP_IE_OPENCL_FP16,
  1253. CV_TEST_TAG_DNN_SKIP_IE_NN_BUILDER);
  1254. double l1 = target == DNN_TARGET_MYRIAD ? 4.9e-3 : default_l1;
  1255. runTensorFlowNet("crop2d", false, l1);
  1256. runTensorFlowNet("slice_4d");
  1257. runTensorFlowNet("strided_slice");
  1258. }
  1259. TEST_P(Test_TensorFlow_layers, softmax)
  1260. {
  1261. runTensorFlowNet("keras_softmax");
  1262. runTensorFlowNet("slim_softmax");
  1263. }
  1264. TEST_P(Test_TensorFlow_layers, slim_softmax_v2)
  1265. {
  1266. #if defined(INF_ENGINE_RELEASE)
  1267. if (backend == DNN_BACKEND_INFERENCE_ENGINE_NN_BUILDER_2019 && target == DNN_TARGET_MYRIAD &&
  1268. getInferenceEngineVPUType() == CV_DNN_INFERENCE_ENGINE_VPU_TYPE_MYRIAD_2
  1269. )
  1270. applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_MYRIAD_2, CV_TEST_TAG_DNN_SKIP_IE_NN_BUILDER);
  1271. #endif
  1272. runTensorFlowNet("slim_softmax_v2");
  1273. }
  1274. TEST_P(Test_TensorFlow_layers, relu6)
  1275. {
  1276. runTensorFlowNet("keras_relu6");
  1277. runTensorFlowNet("keras_relu6", /*hasText*/ true);
  1278. }
  1279. TEST_P(Test_TensorFlow_layers, subpixel)
  1280. {
  1281. if (backend == DNN_BACKEND_INFERENCE_ENGINE_NN_BUILDER_2019)
  1282. applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_NN_BUILDER);
  1283. if (backend == DNN_BACKEND_INFERENCE_ENGINE_NGRAPH)
  1284. applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_NGRAPH);
  1285. runTensorFlowNet("subpixel");
  1286. }
  1287. TEST_P(Test_TensorFlow_layers, keras_mobilenet_head)
  1288. {
  1289. runTensorFlowNet("keras_mobilenet_head");
  1290. runTensorFlowNet("keras_learning_phase");
  1291. }
  1292. // TF case: align_corners=False, half_pixel_centers=False
  1293. TEST_P(Test_TensorFlow_layers, resize_bilinear)
  1294. {
  1295. #if defined(INF_ENGINE_RELEASE) && INF_ENGINE_VER_MAJOR_EQ(2021030000)
  1296. if (backend == DNN_BACKEND_INFERENCE_ENGINE_NGRAPH && target == DNN_TARGET_MYRIAD)
  1297. applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_MYRIAD, CV_TEST_TAG_DNN_SKIP_IE_NGRAPH); // exception
  1298. #endif
  1299. runTensorFlowNet("resize_bilinear");
  1300. }
  1301. // TF case: align_corners=True, half_pixel_centers=False
  1302. TEST_P(Test_TensorFlow_layers, resize_bilinear_align_corners)
  1303. {
  1304. #if defined(INF_ENGINE_RELEASE) && INF_ENGINE_VER_MAJOR_EQ(2021030000)
  1305. if (backend == DNN_BACKEND_INFERENCE_ENGINE_NGRAPH && target == DNN_TARGET_MYRIAD)
  1306. applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_MYRIAD, CV_TEST_TAG_DNN_SKIP_IE_NGRAPH); // exception
  1307. #endif
  1308. runTensorFlowNet("resize_bilinear",
  1309. false, 0.0, 0.0, false, // default parameters
  1310. "_align_corners");
  1311. }
  1312. // TF case: align_corners=False, half_pixel_centers=True
  1313. TEST_P(Test_TensorFlow_layers, resize_bilinear_half_pixel)
  1314. {
  1315. if (backend == DNN_BACKEND_INFERENCE_ENGINE_NGRAPH)
  1316. applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_NGRAPH);
  1317. runTensorFlowNet("resize_bilinear", false, 0.0, 0.0, false, "_half_pixel");
  1318. }
  1319. // TF case: align_corners=False, half_pixel_centers=False
  1320. TEST_P(Test_TensorFlow_layers, resize_bilinear_factor)
  1321. {
  1322. runTensorFlowNet("resize_bilinear_factor");
  1323. }
  1324. // TF case: align_corners=False, half_pixel_centers=True
  1325. TEST_P(Test_TensorFlow_layers, resize_bilinear_factor_half_pixel)
  1326. {
  1327. if (backend == DNN_BACKEND_INFERENCE_ENGINE_NGRAPH)
  1328. applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_NGRAPH);
  1329. runTensorFlowNet("resize_bilinear_factor", false, 0.0, 0.0, false, "_half_pixel");
  1330. }
  1331. // TF case: align_corners=True, half_pixel_centers=False
  1332. TEST_P(Test_TensorFlow_layers, resize_bilinear_factor_align_corners)
  1333. {
  1334. runTensorFlowNet("resize_bilinear_factor", false, 0.0, 0.0, false, "_align_corners");
  1335. }
  1336. // TF case: align_corners=False, half_pixel_centers=False
  1337. TEST_P(Test_TensorFlow_layers, resize_bilinear_down)
  1338. {
  1339. runTensorFlowNet("resize_bilinear_down");
  1340. }
  1341. TEST_P(Test_TensorFlow_layers, resize_concat_optimization)
  1342. {
  1343. #if defined(INF_ENGINE_RELEASE) && INF_ENGINE_VER_MAJOR_EQ(2021040000)
  1344. if (backend == DNN_BACKEND_INFERENCE_ENGINE_NGRAPH && target != DNN_TARGET_CPU) // Exception: Function contains several inputs and outputs with one friendly name! (HETERO bug?)
  1345. applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_NGRAPH, CV_TEST_TAG_DNN_SKIP_IE_VERSION);
  1346. #endif
  1347. runTensorFlowNet("resize_concat_optimization");
  1348. }
  1349. TEST_P(Test_TensorFlow_layers, tf2_dense)
  1350. {
  1351. runTensorFlowNet("tf2_dense");
  1352. }
  1353. TEST_P(Test_TensorFlow_layers, clip_by_value)
  1354. {
  1355. runTensorFlowNet("clip_by_value");
  1356. }
  1357. TEST_P(Test_TensorFlow_layers, tf2_prelu)
  1358. {
  1359. if (backend == DNN_BACKEND_CUDA)
  1360. applyTestTag(CV_TEST_TAG_DNN_SKIP_CUDA); // not supported; only across channels is supported
  1361. #if defined(INF_ENGINE_RELEASE) && INF_ENGINE_VER_MAJOR_LT(2021040000)
  1362. if (backend == DNN_BACKEND_INFERENCE_ENGINE_NN_BUILDER_2019)
  1363. applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_NN_BUILDER);
  1364. if (backend == DNN_BACKEND_INFERENCE_ENGINE_NGRAPH)
  1365. applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_NGRAPH);
  1366. #endif
  1367. #if defined(INF_ENGINE_RELEASE) && INF_ENGINE_VER_MAJOR_EQ(2021040000)
  1368. if (backend == DNN_BACKEND_INFERENCE_ENGINE_NGRAPH)
  1369. {
  1370. // IE exception: Input prelu:StatefulPartitionedCall/StatefulPartitionedCall/sequential/p_re_lu/add hasn't been found in primitiveIDs map
  1371. if (target == DNN_TARGET_OPENCL || target == DNN_TARGET_OPENCL_FP16)
  1372. applyTestTag(target == DNN_TARGET_OPENCL ? CV_TEST_TAG_DNN_SKIP_IE_OPENCL : CV_TEST_TAG_DNN_SKIP_IE_OPENCL_FP16,
  1373. CV_TEST_TAG_DNN_SKIP_IE_NGRAPH, CV_TEST_TAG_DNN_SKIP_IE_VERSION
  1374. );
  1375. // IE exception: Eltwise node with name `StatefulPartitionedCall/StatefulPartitionedCall/sequential/p_re_lu/add` has invalid input/output dims configuration
  1376. if (target == DNN_TARGET_CPU)
  1377. applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_CPU, CV_TEST_TAG_DNN_SKIP_IE_NGRAPH, CV_TEST_TAG_DNN_SKIP_IE_VERSION);
  1378. }
  1379. #endif
  1380. runTensorFlowNet("tf2_prelu");
  1381. }
  1382. TEST_P(Test_TensorFlow_layers, tf2_permute_nhwc_ncwh)
  1383. {
  1384. runTensorFlowNet("tf2_permute_nhwc_ncwh");
  1385. }
  1386. TEST_P(Test_TensorFlow_layers, squeeze)
  1387. {
  1388. #if defined(INF_ENGINE_RELEASE)
  1389. if (backend == DNN_BACKEND_INFERENCE_ENGINE_NN_BUILDER_2019 && target == DNN_TARGET_MYRIAD
  1390. && getInferenceEngineVPUType() == CV_DNN_INFERENCE_ENGINE_VPU_TYPE_MYRIAD_2
  1391. )
  1392. applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_MYRIAD_2, CV_TEST_TAG_DNN_SKIP_IE_NN_BUILDER);
  1393. #endif
  1394. int inpShapes[][4] = {{1, 3, 4, 2}, {1, 3, 1, 2}, {1, 3, 4, 1}, {1, 3, 4, 1}}; // TensorFlow's shape (NHWC)
  1395. int outShapes[][3] = {{3, 4, 2}, {1, 3, 2}, {1, 3, 4}, {1, 3, 4}};
  1396. int squeeze_dims[] = {0, 2, 3, -1};
  1397. for (int i = 0; i < 4; ++i)
  1398. {
  1399. SCOPED_TRACE(format("i=%d", i));
  1400. std::string pbtxt =
  1401. "node { name: \"input\" op: \"Placeholder\""
  1402. "attr { key: \"data_format\" value { s: \"NHWC\" } } }"
  1403. "node { name: \"squeeze\" op: \"Squeeze\" input: \"input\""
  1404. "attr { key: \"squeeze_dims\" value { list { i:" + format("%d", squeeze_dims[i]) + "}}}}";
  1405. Net net = readNetFromTensorflow(0, 0, pbtxt.c_str(), pbtxt.size());
  1406. net.setPreferableBackend(backend);
  1407. net.setPreferableTarget(target);
  1408. Mat tfInp(4, &inpShapes[i][0], CV_32F);
  1409. randu(tfInp, -1, 1);
  1410. // NHWC to NCHW
  1411. CV_Assert(inpShapes[i][0] == 1);
  1412. std::swap(inpShapes[i][2], inpShapes[i][3]);
  1413. std::swap(inpShapes[i][1], inpShapes[i][2]);
  1414. Mat cvInp = tfInp.reshape(1, tfInp.total() / inpShapes[i][1]).t();
  1415. cvInp = cvInp.reshape(1, 4, &inpShapes[i][0]);
  1416. net.setInput(cvInp);
  1417. Mat out = net.forward();
  1418. normAssert(tfInp.reshape(1, 3, &outShapes[i][0]), out, "", default_l1, default_lInf);
  1419. }
  1420. }
  1421. INSTANTIATE_TEST_CASE_P(/**/, Test_TensorFlow_layers, dnnBackendsAndTargets());
  1422. TEST(Test_TensorFlow, two_inputs)
  1423. {
  1424. Net net = readNet(path("two_inputs_net.pbtxt"));
  1425. net.setPreferableBackend(DNN_BACKEND_OPENCV);
  1426. Mat firstInput(2, 3, CV_32FC1), secondInput(2, 3, CV_32FC1);
  1427. randu(firstInput, -1, 1);
  1428. randu(secondInput, -1, 1);
  1429. net.setInput(firstInput, "first_input");
  1430. net.setInput(secondInput, "second_input");
  1431. Mat out = net.forward();
  1432. normAssert(out, firstInput + secondInput);
  1433. }
  1434. TEST_P(Test_TensorFlow_nets, Mask_RCNN)
  1435. {
  1436. static const double kMaskThreshold = 0.5;
  1437. if (backend == DNN_BACKEND_INFERENCE_ENGINE_NN_BUILDER_2019 && target == DNN_TARGET_MYRIAD)
  1438. applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_MYRIAD, CV_TEST_TAG_DNN_SKIP_IE_NN_BUILDER);
  1439. if (target == DNN_TARGET_MYRIAD && getInferenceEngineVPUType() == CV_DNN_INFERENCE_ENGINE_VPU_TYPE_MYRIAD_X)
  1440. applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_MYRIAD_X, CV_TEST_TAG_DNN_SKIP_IE_NGRAPH);
  1441. if (target == DNN_TARGET_CUDA_FP16)
  1442. applyTestTag(CV_TEST_TAG_DNN_SKIP_CUDA_FP16);
  1443. applyTestTag(CV_TEST_TAG_MEMORY_1GB, CV_TEST_TAG_DEBUG_VERYLONG);
  1444. Mat img = imread(findDataFile("dnn/street.png"));
  1445. std::string proto = findDataFile("dnn/mask_rcnn_inception_v2_coco_2018_01_28.pbtxt");
  1446. std::string model = findDataFile("dnn/mask_rcnn_inception_v2_coco_2018_01_28.pb", false);
  1447. Net net = readNetFromTensorflow(model, proto);
  1448. Mat refDetections = blobFromNPY(path("mask_rcnn_inception_v2_coco_2018_01_28.detection_out.npy"));
  1449. Mat refMasks = blobFromNPY(path("mask_rcnn_inception_v2_coco_2018_01_28.detection_masks.npy"));
  1450. Mat blob = blobFromImage(img, 1.0f, Size(800, 800), Scalar(), true, false);
  1451. net.setPreferableBackend(backend);
  1452. net.setPreferableTarget(target);
  1453. net.setInput(blob);
  1454. // Mask-RCNN predicts bounding boxes and segmentation masks.
  1455. std::vector<String> outNames(2);
  1456. outNames[0] = "detection_out_final";
  1457. outNames[1] = "detection_masks";
  1458. std::vector<Mat> outs;
  1459. net.forward(outs, outNames);
  1460. Mat outDetections = outs[0];
  1461. Mat outMasks = outs[1];
  1462. double scoreDiff = (target == DNN_TARGET_OPENCL_FP16 || target == DNN_TARGET_MYRIAD) ? 0.2 : 2e-5;
  1463. double iouDiff = (target == DNN_TARGET_OPENCL_FP16 || target == DNN_TARGET_MYRIAD) ? 0.018 : default_lInf;
  1464. normAssertDetections(refDetections, outDetections, "", /*threshold for zero confidence*/1e-5, scoreDiff, iouDiff);
  1465. // Output size of masks is NxCxHxW where
  1466. // N - number of detected boxes
  1467. // C - number of classes (excluding background)
  1468. // HxW - segmentation shape
  1469. const int numDetections = outDetections.size[2];
  1470. int masksSize[] = {1, numDetections, outMasks.size[2], outMasks.size[3]};
  1471. Mat masks(4, &masksSize[0], CV_32F);
  1472. std::vector<cv::Range> srcRanges(4, cv::Range::all());
  1473. std::vector<cv::Range> dstRanges(4, cv::Range::all());
  1474. outDetections = outDetections.reshape(1, outDetections.total() / 7);
  1475. for (int i = 0; i < numDetections; ++i)
  1476. {
  1477. // Get a class id for this bounding box and copy mask only for that class.
  1478. int classId = static_cast<int>(outDetections.at<float>(i, 1));
  1479. srcRanges[0] = dstRanges[1] = cv::Range(i, i + 1);
  1480. srcRanges[1] = cv::Range(classId, classId + 1);
  1481. outMasks(srcRanges).copyTo(masks(dstRanges));
  1482. }
  1483. cv::Range topRefMasks[] = {Range::all(), Range(0, numDetections), Range::all(), Range::all()};
  1484. refMasks = refMasks(&topRefMasks[0]);
  1485. // make binary masks
  1486. cv::threshold(masks.reshape(1, 1), masks, kMaskThreshold, 1, THRESH_BINARY);
  1487. cv::threshold(refMasks.reshape(1, 1), refMasks, kMaskThreshold, 1, THRESH_BINARY);
  1488. double inter = cv::countNonZero(masks & refMasks);
  1489. double area = cv::countNonZero(masks | refMasks);
  1490. EXPECT_GE(inter / area, (target == DNN_TARGET_OPENCL_FP16 || target == DNN_TARGET_MYRIAD) ? 0.98 : 0.99);
  1491. if (backend == DNN_BACKEND_INFERENCE_ENGINE_NGRAPH)
  1492. expectNoFallbacks(net);
  1493. }
  1494. TEST_P(Test_TensorFlow_nets, EfficientDet)
  1495. {
  1496. if (target != DNN_TARGET_CPU)
  1497. {
  1498. if (target == DNN_TARGET_OPENCL_FP16) applyTestTag(CV_TEST_TAG_DNN_SKIP_OPENCL_FP16);
  1499. if (target == DNN_TARGET_OPENCL) applyTestTag(CV_TEST_TAG_DNN_SKIP_OPENCL);
  1500. if (target == DNN_TARGET_MYRIAD) applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_MYRIAD);
  1501. }
  1502. checkBackend();
  1503. std::string proto = findDataFile("dnn/efficientdet-d0.pbtxt");
  1504. std::string model = findDataFile("dnn/efficientdet-d0.pb", false);
  1505. Net net = readNetFromTensorflow(model, proto);
  1506. Mat img = imread(findDataFile("dnn/dog416.png"));
  1507. Mat blob = blobFromImage(img, 1.0/255, Size(512, 512), Scalar(123.675, 116.28, 103.53));
  1508. net.setPreferableBackend(backend);
  1509. net.setPreferableTarget(target);
  1510. net.setInput(blob);
  1511. // Output has shape 1x1xNx7 where N - number of detections.
  1512. // An every detection is a vector of values [id, classId, confidence, left, top, right, bottom]
  1513. Mat out = net.forward();
  1514. // References are from test for TensorFlow model.
  1515. Mat ref = (Mat_<float>(3, 7) << 0, 1, 0.8437444, 0.153996080160141, 0.20534580945968628, 0.7463544607162476, 0.7414066195487976,
  1516. 0, 17, 0.8245924, 0.16657517850399017, 0.3996818959712982, 0.4111558794975281, 0.9306337833404541,
  1517. 0, 7, 0.8039304, 0.6118435263633728, 0.13175517320632935, 0.9065558314323425, 0.2943994700908661);
  1518. double scoreDiff = (target == DNN_TARGET_OPENCL_FP16 || target == DNN_TARGET_MYRIAD) ? 4e-3 : 1e-5;
  1519. double iouDiff = (target == DNN_TARGET_OPENCL_FP16 || target == DNN_TARGET_MYRIAD) ? 2e-3 : 1e-4;
  1520. if (target == DNN_TARGET_CUDA_FP16)
  1521. {
  1522. scoreDiff = 0.002;
  1523. iouDiff = 0.005;
  1524. }
  1525. normAssertDetections(ref, out, "", 0.5, scoreDiff, iouDiff);
  1526. expectNoFallbacksFromIE(net);
  1527. }
  1528. }