test_int8_layers.cpp 48 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211
  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. #include "test_precomp.hpp"
  5. #include "npy_blob.hpp"
  6. #include <opencv2/dnn/shape_utils.hpp>
  7. #include <opencv2/dnn/all_layers.hpp>
  8. namespace opencv_test { namespace {
  9. testing::internal::ParamGenerator< tuple<Backend, Target> > dnnBackendsAndTargetsInt8()
  10. {
  11. std::vector< tuple<Backend, Target> > targets;
  12. targets.push_back(make_tuple(DNN_BACKEND_OPENCV, DNN_TARGET_CPU));
  13. return testing::ValuesIn(targets);
  14. }
  15. template<typename TString>
  16. static std::string _tf(TString filename)
  17. {
  18. return (getOpenCVExtraDir() + "dnn/") + filename;
  19. }
  20. class Test_Int8_layers : public DNNTestLayer
  21. {
  22. public:
  23. void testLayer(const String& basename, const String& importer, double l1, double lInf,
  24. int numInps = 1, int numOuts = 1, bool useCaffeModel = false,
  25. bool useCommonInputBlob = true, bool hasText = false)
  26. {
  27. CV_Assert_N(numInps >= 1, numInps <= 10, numOuts >= 1, numOuts <= 10);
  28. std::vector<Mat> inps(numInps), inps_int8(numInps);
  29. std::vector<Mat> refs(numOuts), outs_int8(numOuts), outs_dequantized(numOuts);
  30. std::vector<float> inputScale, outputScale;
  31. std::vector<int> inputZp, outputZp;
  32. String inpPath, outPath;
  33. Net net, qnet;
  34. if (importer == "Caffe")
  35. {
  36. String prototxt = _tf("layers/" + basename + ".prototxt");
  37. String caffemodel = _tf("layers/" + basename + ".caffemodel");
  38. net = readNetFromCaffe(prototxt, useCaffeModel ? caffemodel : String());
  39. inpPath = _tf("layers/" + (useCommonInputBlob ? "blob" : basename + ".input"));
  40. outPath = _tf("layers/" + basename);
  41. }
  42. else if (importer == "TensorFlow")
  43. {
  44. String netPath = _tf("tensorflow/" + basename + "_net.pb");
  45. String netConfig = hasText ? _tf("tensorflow/" + basename + "_net.pbtxt") : "";
  46. net = readNetFromTensorflow(netPath, netConfig);
  47. inpPath = _tf("tensorflow/" + basename + "_in");
  48. outPath = _tf("tensorflow/" + basename + "_out");
  49. }
  50. else if (importer == "ONNX")
  51. {
  52. String onnxmodel = _tf("onnx/models/" + basename + ".onnx");
  53. net = readNetFromONNX(onnxmodel);
  54. inpPath = _tf("onnx/data/input_" + basename);
  55. outPath = _tf("onnx/data/output_" + basename);
  56. }
  57. ASSERT_FALSE(net.empty());
  58. net.setPreferableBackend(backend);
  59. net.setPreferableTarget(target);
  60. for (int i = 0; i < numInps; i++)
  61. inps[i] = blobFromNPY(inpPath + ((numInps > 1) ? cv::format("_%d.npy", i) : ".npy"));
  62. for (int i = 0; i < numOuts; i++)
  63. refs[i] = blobFromNPY(outPath + ((numOuts > 1) ? cv::format("_%d.npy", i) : ".npy"));
  64. qnet = net.quantize(inps, CV_8S, CV_8S);
  65. qnet.getInputDetails(inputScale, inputZp);
  66. qnet.getOutputDetails(outputScale, outputZp);
  67. // Quantize inputs to int8
  68. // int8_value = float_value/scale + zero-point
  69. for (int i = 0; i < numInps; i++)
  70. {
  71. inps[i].convertTo(inps_int8[i], CV_8S, 1.f/inputScale[i], inputZp[i]);
  72. String inp_name = numInps > 1 ? (importer == "Caffe" ? cv::format("input_%d", i) : cv::format("%d", i)) : "";
  73. qnet.setInput(inps_int8[i], inp_name);
  74. }
  75. qnet.forward(outs_int8);
  76. // Dequantize outputs and compare with reference outputs
  77. // float_value = scale*(int8_value - zero-point)
  78. for (int i = 0; i < numOuts; i++)
  79. {
  80. outs_int8[i].convertTo(outs_dequantized[i], CV_32F, outputScale[i], -(outputScale[i] * outputZp[i]));
  81. normAssert(refs[i], outs_dequantized[i], "", l1, lInf);
  82. }
  83. }
  84. };
  85. TEST_P(Test_Int8_layers, Convolution1D)
  86. {
  87. testLayer("conv1d", "ONNX", 0.00302, 0.00909);
  88. testLayer("conv1d_bias", "ONNX", 0.00306, 0.00948);
  89. }
  90. TEST_P(Test_Int8_layers, Convolution2D)
  91. {
  92. testLayer("layer_convolution", "Caffe", 0.0174, 0.0758, 1, 1, true);
  93. testLayer("single_conv", "TensorFlow", 0.00413, 0.02201);
  94. testLayer("depthwise_conv2d", "TensorFlow", 0.0388, 0.169);
  95. testLayer("atrous_conv2d_valid", "TensorFlow", 0.0193, 0.0633);
  96. testLayer("atrous_conv2d_same", "TensorFlow", 0.0185, 0.1322);
  97. testLayer("keras_atrous_conv2d_same", "TensorFlow", 0.0056, 0.0244);
  98. testLayer("convolution", "ONNX", 0.0052, 0.01516);
  99. testLayer("two_convolution", "ONNX", 0.00295, 0.00840);
  100. }
  101. TEST_P(Test_Int8_layers, Convolution3D)
  102. {
  103. testLayer("conv3d", "TensorFlow", 0.00734, 0.02434);
  104. testLayer("conv3d", "ONNX", 0.00353, 0.00941);
  105. testLayer("conv3d_bias", "ONNX", 0.00129, 0.00249);
  106. }
  107. TEST_P(Test_Int8_layers, Flatten)
  108. {
  109. testLayer("flatten", "TensorFlow", 0.0036, 0.0069, 1, 1, false, true, true);
  110. testLayer("unfused_flatten", "TensorFlow", 0.0014, 0.0028);
  111. testLayer("unfused_flatten_unknown_batch", "TensorFlow", 0.0043, 0.0051);
  112. }
  113. TEST_P(Test_Int8_layers, Padding)
  114. {
  115. testLayer("padding_valid", "TensorFlow", 0.0026, 0.0064);
  116. testLayer("padding_same", "TensorFlow", 0.0081, 0.032);
  117. testLayer("spatial_padding", "TensorFlow", 0.0078, 0.028);
  118. testLayer("mirror_pad", "TensorFlow", 0.0064, 0.013);
  119. testLayer("pad_and_concat", "TensorFlow", 0.0021, 0.0098);
  120. testLayer("padding", "ONNX", 0.0005, 0.0069);
  121. testLayer("ReflectionPad2d", "ONNX", 0.00062, 0.0018);
  122. testLayer("ZeroPad2d", "ONNX", 0.00037, 0.0018);
  123. }
  124. TEST_P(Test_Int8_layers, AvePooling)
  125. {
  126. testLayer("layer_pooling_ave", "Caffe", 0.0021, 0.0075);
  127. testLayer("ave_pool_same", "TensorFlow", 0.00153, 0.0041);
  128. testLayer("average_pooling_1d", "ONNX", 0.002, 0.0048);
  129. testLayer("average_pooling", "ONNX", 0.0014, 0.0032);
  130. testLayer("average_pooling_dynamic_axes", "ONNX", 0.0014, 0.006);
  131. if (target != DNN_TARGET_CPU)
  132. throw SkipTestException("Only CPU is supported");
  133. testLayer("ave_pool3d", "TensorFlow", 0.00175, 0.0047);
  134. testLayer("ave_pool3d", "ONNX", 0.00063, 0.0016);
  135. }
  136. TEST_P(Test_Int8_layers, MaxPooling)
  137. {
  138. testLayer("pool_conv_1d", "ONNX", 0.0006, 0.0015);
  139. if (target != DNN_TARGET_CPU)
  140. throw SkipTestException("Only CPU is supported");
  141. testLayer("pool_conv_3d", "ONNX", 0.0033, 0.0124);
  142. /* All the below tests have MaxPooling as last layer, so computeMaxIdx is set to true
  143. which is not supported by int8 maxpooling
  144. testLayer("layer_pooling_max", "Caffe", 0.0021, 0.004);
  145. testLayer("max_pool_even", "TensorFlow", 0.0048, 0.0139);
  146. testLayer("max_pool_odd_valid", "TensorFlow", 0.0043, 0.012);
  147. testLayer("conv_pool_nchw", "TensorFlow", 0.007, 0.025);
  148. testLayer("max_pool3d", "TensorFlow", 0.0025, 0.0058);
  149. testLayer("maxpooling_1d", "ONNX", 0.0018, 0.0037);
  150. testLayer("two_maxpooling_1d", "ONNX", 0.0037, 0.0052);
  151. testLayer("maxpooling", "ONNX", 0.0034, 0.0065);
  152. testLayer("two_maxpooling", "ONNX", 0.0025, 0.0052);
  153. testLayer("max_pool3d", "ONNX", 0.0028, 0.0069);*/
  154. }
  155. TEST_P(Test_Int8_layers, Reduce)
  156. {
  157. testLayer("reduce_mean", "TensorFlow", 0.0005, 0.0014);
  158. testLayer("reduce_mean", "ONNX", 0.00062, 0.0014);
  159. testLayer("reduce_mean_axis1", "ONNX", 0.00032, 0.0007);
  160. testLayer("reduce_mean_axis2", "ONNX", 0.00033, 0.001);
  161. testLayer("reduce_sum", "TensorFlow", 0.015, 0.031);
  162. testLayer("reduce_sum_channel", "TensorFlow", 0.008, 0.019);
  163. testLayer("sum_pool_by_axis", "TensorFlow", 0.012, 0.032);
  164. testLayer("reduce_sum", "ONNX", 0.0025, 0.0048);
  165. testLayer("reduce_max", "ONNX", 0, 0);
  166. testLayer("reduce_max_axis_0", "ONNX", 0.0042, 0.007);
  167. testLayer("reduce_max_axis_1", "ONNX", 0.0018, 0.0036);
  168. if (target != DNN_TARGET_CPU)
  169. throw SkipTestException("Only CPU is supported");
  170. testLayer("reduce_mean3d", "ONNX", 0.00048, 0.0016);
  171. }
  172. TEST_P(Test_Int8_layers, ReLU)
  173. {
  174. testLayer("layer_relu", "Caffe", 0.0005, 0.002);
  175. testLayer("ReLU", "ONNX", 0.0012, 0.0047);
  176. }
  177. TEST_P(Test_Int8_layers, LeakyReLU)
  178. {
  179. testLayer("leaky_relu", "TensorFlow", 0.0002, 0.0004);
  180. }
  181. TEST_P(Test_Int8_layers, ReLU6)
  182. {
  183. testLayer("keras_relu6", "TensorFlow", 0.0018, 0.0062);
  184. testLayer("keras_relu6", "TensorFlow", 0.0018, 0.0062, 1, 1, false, true, true);
  185. testLayer("clip_by_value", "TensorFlow", 0.0009, 0.002);
  186. testLayer("clip", "ONNX", 0.00006, 0.00037);
  187. }
  188. TEST_P(Test_Int8_layers, Sigmoid)
  189. {
  190. testLayer("maxpooling_sigmoid", "ONNX", 0.0011, 0.0032);
  191. }
  192. TEST_P(Test_Int8_layers, Sigmoid_dynamic_axes)
  193. {
  194. testLayer("maxpooling_sigmoid_dynamic_axes", "ONNX", 0.002, 0.0032);
  195. }
  196. TEST_P(Test_Int8_layers, Sigmoid_1d)
  197. {
  198. testLayer("maxpooling_sigmoid_1d", "ONNX", 0.002, 0.0037);
  199. }
  200. TEST_P(Test_Int8_layers, Mish)
  201. {
  202. testLayer("mish", "ONNX", 0.0015, 0.0025);
  203. }
  204. TEST_P(Test_Int8_layers, Softmax)
  205. {
  206. testLayer("layer_softmax", "Caffe", 0.0011, 0.0036);
  207. testLayer("keras_softmax", "TensorFlow", 0.00093, 0.0027);
  208. testLayer("slim_softmax", "TensorFlow", 0.0016, 0.0034);
  209. testLayer("slim_softmax_v2", "TensorFlow", 0.0029, 0.017);
  210. testLayer("softmax", "ONNX", 0.0016, 0.0028);
  211. testLayer("log_softmax", "ONNX", 0.014, 0.025);
  212. testLayer("softmax_unfused", "ONNX", 0.0009, 0.0021);
  213. }
  214. TEST_P(Test_Int8_layers, Concat)
  215. {
  216. testLayer("layer_concat_shared_input", "Caffe", 0.0076, 0.029, 1, 1, true, false);
  217. testLayer("concat_axis_1", "TensorFlow", 0.0056, 0.017);
  218. testLayer("keras_pad_concat", "TensorFlow", 0.0032, 0.0089);
  219. testLayer("concat_3d", "TensorFlow", 0.005, 0.014);
  220. testLayer("concatenation", "ONNX", 0.0032, 0.009);
  221. }
  222. TEST_P(Test_Int8_layers, BatchNorm)
  223. {
  224. testLayer("layer_batch_norm", "Caffe", 0.0061, 0.019, 1, 1, true);
  225. testLayer("fused_batch_norm", "TensorFlow", 0.0063, 0.02);
  226. testLayer("batch_norm_text", "TensorFlow", 0.0048, 0.013, 1, 1, false, true, true);
  227. testLayer("unfused_batch_norm", "TensorFlow", 0.0076, 0.019);
  228. testLayer("fused_batch_norm_no_gamma", "TensorFlow", 0.0067, 0.015);
  229. testLayer("unfused_batch_norm_no_gamma", "TensorFlow", 0.0123, 0.044);
  230. testLayer("switch_identity", "TensorFlow", 0.0035, 0.011);
  231. testLayer("batch_norm3d", "TensorFlow", 0.0077, 0.02);
  232. testLayer("batch_norm", "ONNX", 0.0012, 0.0049);
  233. testLayer("batch_norm_3d", "ONNX", 0.0039, 0.012);
  234. testLayer("frozenBatchNorm2d", "ONNX", 0.001, 0.0018);
  235. testLayer("batch_norm_subgraph", "ONNX", 0.0049, 0.0098);
  236. }
  237. TEST_P(Test_Int8_layers, Scale)
  238. {
  239. testLayer("batch_norm", "TensorFlow", 0.0028, 0.0098);
  240. testLayer("scale", "ONNX", 0.0025, 0.0071);
  241. testLayer("expand_hw", "ONNX", 0.0012, 0.0012);
  242. testLayer("flatten_const", "ONNX", 0.0024, 0.0048);
  243. }
  244. TEST_P(Test_Int8_layers, InnerProduct)
  245. {
  246. testLayer("layer_inner_product", "Caffe", 0.005, 0.02, 1, 1, true);
  247. testLayer("matmul", "TensorFlow", 0.0061, 0.019);
  248. testLayer("nhwc_transpose_reshape_matmul", "TensorFlow", 0.0009, 0.0091);
  249. testLayer("nhwc_reshape_matmul", "TensorFlow", 0.03, 0.071);
  250. testLayer("matmul_layout", "TensorFlow", 0.035, 0.06);
  251. testLayer("tf2_dense", "TensorFlow", 0, 0);
  252. testLayer("matmul_add", "ONNX", 0.041, 0.082);
  253. testLayer("linear", "ONNX", 0.0018, 0.0029);
  254. testLayer("constant", "ONNX", 0.00021, 0.0006);
  255. testLayer("lin_with_constant", "ONNX", 0.0011, 0.0016);
  256. }
  257. TEST_P(Test_Int8_layers, Reshape)
  258. {
  259. testLayer("reshape_layer", "TensorFlow", 0.0032, 0.0082);
  260. testLayer("reshape_nchw", "TensorFlow", 0.0089, 0.029);
  261. testLayer("reshape_conv", "TensorFlow", 0.035, 0.054);
  262. testLayer("reshape_reduce", "TensorFlow", 0.0042, 0.0078);
  263. testLayer("reshape_as_shape", "TensorFlow", 0.0014, 0.0028);
  264. testLayer("reshape_no_reorder", "TensorFlow", 0.0014, 0.0028);
  265. testLayer("shift_reshape_no_reorder", "TensorFlow", 0.0063, 0.014);
  266. testLayer("dynamic_reshape", "ONNX", 0.0047, 0.0079);
  267. testLayer("dynamic_reshape_opset_11", "ONNX", 0.0048, 0.0081);
  268. testLayer("flatten_by_prod", "ONNX", 0.0048, 0.0081);
  269. testLayer("squeeze", "ONNX", 0.0048, 0.0081);
  270. testLayer("unsqueeze", "ONNX", 0.0033, 0.0053);
  271. testLayer("squeeze_and_conv_dynamic_axes", "ONNX", 0.0054, 0.0154);
  272. testLayer("unsqueeze_and_conv_dynamic_axes", "ONNX", 0.0037, 0.0151);
  273. }
  274. TEST_P(Test_Int8_layers, Permute)
  275. {
  276. testLayer("tf2_permute_nhwc_ncwh", "TensorFlow", 0.0028, 0.006);
  277. testLayer("transpose", "ONNX", 0.0015, 0.0046);
  278. }
  279. TEST_P(Test_Int8_layers, Identity)
  280. {
  281. testLayer("expand_batch", "ONNX", 0.0027, 0.0036);
  282. testLayer("expand_channels", "ONNX", 0.0013, 0.0019);
  283. testLayer("expand_neg_batch", "ONNX", 0.00071, 0.0019);
  284. }
  285. TEST_P(Test_Int8_layers, Slice_split_tf)
  286. {
  287. testLayer("split", "TensorFlow", 0.0033, 0.0056);
  288. }
  289. TEST_P(Test_Int8_layers, Slice_4d_tf)
  290. {
  291. testLayer("slice_4d", "TensorFlow", 0.003, 0.0073);
  292. }
  293. TEST_P(Test_Int8_layers, Slice_strided_tf)
  294. {
  295. testLayer("strided_slice", "TensorFlow", 0.008, 0.0142);
  296. }
  297. TEST_P(Test_Int8_layers, Slice_onnx)
  298. {
  299. testLayer("slice", "ONNX", 0.0046, 0.0077);
  300. }
  301. TEST_P(Test_Int8_layers, Slice_dynamic_axes_onnx)
  302. {
  303. testLayer("slice_dynamic_axes", "ONNX", 0.0039, 0.02);
  304. }
  305. TEST_P(Test_Int8_layers, Slice_steps_2d_onnx11)
  306. {
  307. testLayer("slice_opset_11_steps_2d", "ONNX", 0.01, 0.0124);
  308. }
  309. TEST_P(Test_Int8_layers, Slice_steps_3d_onnx11)
  310. {
  311. testLayer("slice_opset_11_steps_3d", "ONNX", 0.0068, 0.014);
  312. }
  313. TEST_P(Test_Int8_layers, Slice_steps_4d_onnx11)
  314. {
  315. testLayer("slice_opset_11_steps_4d", "ONNX", 0.0041, 0.008);
  316. }
  317. TEST_P(Test_Int8_layers, Slice_steps_5d_onnx11)
  318. {
  319. testLayer("slice_opset_11_steps_5d", "ONNX", 0.0085, 0.021);
  320. }
  321. TEST_P(Test_Int8_layers, Dropout)
  322. {
  323. testLayer("layer_dropout", "Caffe", 0.0021, 0.004);
  324. testLayer("dropout", "ONNX", 0.0029, 0.004);
  325. }
  326. TEST_P(Test_Int8_layers, Eltwise)
  327. {
  328. testLayer("layer_eltwise", "Caffe", 0.062, 0.15);
  329. testLayer("conv_2_inps", "Caffe", 0.0086, 0.0232, 2, 1, true, false);
  330. testLayer("eltwise_sub", "TensorFlow", 0.015, 0.047);
  331. testLayer("eltwise_add_vec", "TensorFlow", 0.037, 0.21); // tflite 0.0095, 0.0365
  332. testLayer("eltwise_mul_vec", "TensorFlow", 0.173, 1.14); // tflite 0.0028, 0.017
  333. testLayer("channel_broadcast", "TensorFlow", 0.0025, 0.0063);
  334. testLayer("split_equals", "TensorFlow", 0.02, 0.065);
  335. testLayer("mul", "ONNX", 0.0039, 0.014);
  336. testLayer("split_max", "ONNX", 0.004, 0.012);
  337. }
  338. INSTANTIATE_TEST_CASE_P(/**/, Test_Int8_layers, dnnBackendsAndTargetsInt8());
  339. class Test_Int8_nets : public DNNTestLayer
  340. {
  341. public:
  342. void testClassificationNet(Net baseNet, const Mat& blob, const Mat& ref, double l1, double lInf)
  343. {
  344. Net qnet = baseNet.quantize(blob, CV_32F, CV_32F);
  345. qnet.setPreferableBackend(backend);
  346. qnet.setPreferableTarget(target);
  347. qnet.setInput(blob);
  348. Mat out = qnet.forward();
  349. normAssert(ref, out, "", l1, lInf);
  350. }
  351. void testDetectionNet(Net baseNet, const Mat& blob, const Mat& ref,
  352. double confThreshold, double scoreDiff, double iouDiff)
  353. {
  354. Net qnet = baseNet.quantize(blob, CV_32F, CV_32F);
  355. qnet.setPreferableBackend(backend);
  356. qnet.setPreferableTarget(target);
  357. qnet.setInput(blob);
  358. Mat out = qnet.forward();
  359. normAssertDetections(ref, out, "", confThreshold, scoreDiff, iouDiff);
  360. }
  361. void testFaster(Net baseNet, const Mat& ref, double confThreshold, double scoreDiff, double iouDiff)
  362. {
  363. Mat inp = imread(_tf("dog416.png"));
  364. resize(inp, inp, Size(800, 600));
  365. Mat blob = blobFromImage(inp, 1.0, Size(), Scalar(102.9801, 115.9465, 122.7717), false, false);
  366. Mat imInfo = (Mat_<float>(1, 3) << inp.rows, inp.cols, 1.6f);
  367. Net qnet = baseNet.quantize(std::vector<Mat>{blob, imInfo}, CV_32F, CV_32F);
  368. qnet.setPreferableBackend(backend);
  369. qnet.setPreferableTarget(target);
  370. qnet.setInput(blob, "data");
  371. qnet.setInput(imInfo, "im_info");
  372. Mat out = qnet.forward();
  373. normAssertDetections(ref, out, "", confThreshold, scoreDiff, iouDiff);
  374. }
  375. void testONNXNet(const String& basename, double l1, double lInf, bool useSoftmax = false)
  376. {
  377. String onnxmodel = findDataFile("dnn/onnx/models/" + basename + ".onnx", false);
  378. Mat blob = readTensorFromONNX(findDataFile("dnn/onnx/data/input_" + basename + ".pb"));
  379. Mat ref = readTensorFromONNX(findDataFile("dnn/onnx/data/output_" + basename + ".pb"));
  380. Net baseNet = readNetFromONNX(onnxmodel);
  381. baseNet.setPreferableBackend(backend);
  382. baseNet.setPreferableTarget(target);
  383. Net qnet = baseNet.quantize(blob, CV_32F, CV_32F);
  384. qnet.setInput(blob);
  385. Mat out = qnet.forward();
  386. if (useSoftmax)
  387. {
  388. LayerParams lp;
  389. Net netSoftmax;
  390. netSoftmax.addLayerToPrev("softmaxLayer", "Softmax", lp);
  391. netSoftmax.setPreferableBackend(DNN_BACKEND_OPENCV);
  392. netSoftmax.setInput(out);
  393. out = netSoftmax.forward();
  394. netSoftmax.setInput(ref);
  395. ref = netSoftmax.forward();
  396. }
  397. normAssert(ref, out, "", l1, lInf);
  398. }
  399. void testDarknetModel(const std::string& cfg, const std::string& weights,
  400. const cv::Mat& ref, double scoreDiff, double iouDiff,
  401. float confThreshold = 0.24, float nmsThreshold = 0.4)
  402. {
  403. CV_Assert(ref.cols == 7);
  404. std::vector<std::vector<int> > refClassIds;
  405. std::vector<std::vector<float> > refScores;
  406. std::vector<std::vector<Rect2d> > refBoxes;
  407. for (int i = 0; i < ref.rows; ++i)
  408. {
  409. int batchId = static_cast<int>(ref.at<float>(i, 0));
  410. int classId = static_cast<int>(ref.at<float>(i, 1));
  411. float score = ref.at<float>(i, 2);
  412. float left = ref.at<float>(i, 3);
  413. float top = ref.at<float>(i, 4);
  414. float right = ref.at<float>(i, 5);
  415. float bottom = ref.at<float>(i, 6);
  416. Rect2d box(left, top, right - left, bottom - top);
  417. if (batchId >= refClassIds.size())
  418. {
  419. refClassIds.resize(batchId + 1);
  420. refScores.resize(batchId + 1);
  421. refBoxes.resize(batchId + 1);
  422. }
  423. refClassIds[batchId].push_back(classId);
  424. refScores[batchId].push_back(score);
  425. refBoxes[batchId].push_back(box);
  426. }
  427. Mat img1 = imread(_tf("dog416.png"));
  428. Mat img2 = imread(_tf("street.png"));
  429. std::vector<Mat> samples(2);
  430. samples[0] = img1; samples[1] = img2;
  431. // determine test type, whether batch or single img
  432. int batch_size = refClassIds.size();
  433. CV_Assert(batch_size == 1 || batch_size == 2);
  434. samples.resize(batch_size);
  435. Mat inp = blobFromImages(samples, 1.0/255, Size(416, 416), Scalar(), true, false);
  436. Net baseNet = readNetFromDarknet(findDataFile("dnn/" + cfg), findDataFile("dnn/" + weights, false));
  437. Net qnet = baseNet.quantize(inp, CV_32F, CV_32F);
  438. qnet.setPreferableBackend(backend);
  439. qnet.setPreferableTarget(target);
  440. qnet.setInput(inp);
  441. std::vector<Mat> outs;
  442. qnet.forward(outs, qnet.getUnconnectedOutLayersNames());
  443. for (int b = 0; b < batch_size; ++b)
  444. {
  445. std::vector<int> classIds;
  446. std::vector<float> confidences;
  447. std::vector<Rect2d> boxes;
  448. for (int i = 0; i < outs.size(); ++i)
  449. {
  450. Mat out;
  451. if (batch_size > 1){
  452. // get the sample slice from 3D matrix (batch, box, classes+5)
  453. Range ranges[3] = {Range(b, b+1), Range::all(), Range::all()};
  454. out = outs[i](ranges).reshape(1, outs[i].size[1]);
  455. }else{
  456. out = outs[i];
  457. }
  458. for (int j = 0; j < out.rows; ++j)
  459. {
  460. Mat scores = out.row(j).colRange(5, out.cols);
  461. double confidence;
  462. Point maxLoc;
  463. minMaxLoc(scores, 0, &confidence, 0, &maxLoc);
  464. if (confidence > confThreshold) {
  465. float* detection = out.ptr<float>(j);
  466. double centerX = detection[0];
  467. double centerY = detection[1];
  468. double width = detection[2];
  469. double height = detection[3];
  470. boxes.push_back(Rect2d(centerX - 0.5 * width, centerY - 0.5 * height,
  471. width, height));
  472. confidences.push_back(confidence);
  473. classIds.push_back(maxLoc.x);
  474. }
  475. }
  476. }
  477. // here we need NMS of boxes
  478. std::vector<int> indices;
  479. NMSBoxes(boxes, confidences, confThreshold, nmsThreshold, indices);
  480. std::vector<int> nms_classIds;
  481. std::vector<float> nms_confidences;
  482. std::vector<Rect2d> nms_boxes;
  483. for (size_t i = 0; i < indices.size(); ++i)
  484. {
  485. int idx = indices[i];
  486. Rect2d box = boxes[idx];
  487. float conf = confidences[idx];
  488. int class_id = classIds[idx];
  489. nms_boxes.push_back(box);
  490. nms_confidences.push_back(conf);
  491. nms_classIds.push_back(class_id);
  492. }
  493. if (cvIsNaN(iouDiff))
  494. {
  495. if (b == 0)
  496. std::cout << "Skip accuracy checks" << std::endl;
  497. continue;
  498. }
  499. normAssertDetections(refClassIds[b], refScores[b], refBoxes[b], nms_classIds, nms_confidences, nms_boxes,
  500. format("batch size %d, sample %d\n", batch_size, b).c_str(), confThreshold, scoreDiff, iouDiff);
  501. }
  502. }
  503. };
  504. TEST_P(Test_Int8_nets, AlexNet)
  505. {
  506. #if defined(OPENCV_32BIT_CONFIGURATION) && defined(HAVE_OPENCL)
  507. applyTestTag(CV_TEST_TAG_MEMORY_2GB);
  508. #else
  509. applyTestTag(target == DNN_TARGET_CPU ? CV_TEST_TAG_MEMORY_512MB : CV_TEST_TAG_MEMORY_1GB);
  510. #endif
  511. if (backend != DNN_BACKEND_OPENCV)
  512. throw SkipTestException("Only OpenCV backend is supported");
  513. if (target == DNN_TARGET_OPENCL_FP16 && !ocl::Device::getDefault().isIntel())
  514. applyTestTag(CV_TEST_TAG_DNN_SKIP_OPENCL_FP16);
  515. if (target == DNN_TARGET_OPENCL && !ocl::Device::getDefault().isIntel())
  516. applyTestTag(CV_TEST_TAG_DNN_SKIP_OPENCL);
  517. Net net = readNetFromCaffe(findDataFile("dnn/bvlc_alexnet.prototxt"),
  518. findDataFile("dnn/bvlc_alexnet.caffemodel", false));
  519. Mat inp = imread(_tf("grace_hopper_227.png"));
  520. Mat blob = blobFromImage(inp, 1.0, Size(227, 227), Scalar(), false);
  521. Mat ref = blobFromNPY(_tf("caffe_alexnet_prob.npy"));
  522. float l1 = 1e-4, lInf = 0.003;
  523. testClassificationNet(net, blob, ref, l1, lInf);
  524. }
  525. TEST_P(Test_Int8_nets, GoogLeNet)
  526. {
  527. if (target == DNN_TARGET_OPENCL_FP16 && !ocl::Device::getDefault().isIntel())
  528. applyTestTag(CV_TEST_TAG_DNN_SKIP_OPENCL_FP16);
  529. if (target == DNN_TARGET_OPENCL && !ocl::Device::getDefault().isIntel())
  530. applyTestTag(CV_TEST_TAG_DNN_SKIP_OPENCL);
  531. Net net = readNetFromCaffe(findDataFile("dnn/bvlc_googlenet.prototxt"),
  532. findDataFile("dnn/bvlc_googlenet.caffemodel", false));
  533. std::vector<Mat> inpMats;
  534. inpMats.push_back( imread(_tf("googlenet_0.png")) );
  535. inpMats.push_back( imread(_tf("googlenet_1.png")) );
  536. Mat blob = blobFromImages(inpMats, 1.0, Size(224, 224), Scalar(), false);
  537. Mat ref = blobFromNPY(_tf("googlenet_prob.npy"));
  538. float l1 = 2e-4, lInf = 0.06;
  539. testClassificationNet(net, blob, ref, l1, lInf);
  540. }
  541. TEST_P(Test_Int8_nets, ResNet50)
  542. {
  543. applyTestTag(target == DNN_TARGET_CPU ? CV_TEST_TAG_MEMORY_512MB : CV_TEST_TAG_MEMORY_1GB);
  544. if (backend != DNN_BACKEND_OPENCV)
  545. throw SkipTestException("Only OpenCV backend is supported");
  546. if (target == DNN_TARGET_OPENCL_FP16 && !ocl::Device::getDefault().isIntel())
  547. applyTestTag(CV_TEST_TAG_DNN_SKIP_OPENCL_FP16);
  548. if (target == DNN_TARGET_OPENCL && !ocl::Device::getDefault().isIntel())
  549. applyTestTag(CV_TEST_TAG_DNN_SKIP_OPENCL);
  550. Net net = readNetFromCaffe(findDataFile("dnn/ResNet-50-deploy.prototxt"),
  551. findDataFile("dnn/ResNet-50-model.caffemodel", false));
  552. Mat inp = imread(_tf("googlenet_0.png"));
  553. Mat blob = blobFromImage(inp, 1.0, Size(224, 224), Scalar(), false);
  554. Mat ref = blobFromNPY(_tf("resnet50_prob.npy"));
  555. float l1 = 3e-4, lInf = 0.04;
  556. testClassificationNet(net, blob, ref, l1, lInf);
  557. }
  558. TEST_P(Test_Int8_nets, DenseNet121)
  559. {
  560. applyTestTag(CV_TEST_TAG_MEMORY_512MB);
  561. if (target == DNN_TARGET_OPENCL_FP16 && !ocl::Device::getDefault().isIntel())
  562. applyTestTag(CV_TEST_TAG_DNN_SKIP_OPENCL_FP16);
  563. if (target == DNN_TARGET_OPENCL && !ocl::Device::getDefault().isIntel())
  564. applyTestTag(CV_TEST_TAG_DNN_SKIP_OPENCL);
  565. Net net = readNetFromCaffe(findDataFile("dnn/DenseNet_121.prototxt", false),
  566. findDataFile("dnn/DenseNet_121.caffemodel", false));
  567. Mat inp = imread(_tf("dog416.png"));
  568. Mat blob = blobFromImage(inp, 1.0 / 255.0, Size(224, 224), Scalar(), true, true);
  569. Mat ref = blobFromNPY(_tf("densenet_121_output.npy"));
  570. float l1 = 0.76, lInf = 3.31; // seems wrong
  571. testClassificationNet(net, blob, ref, l1, lInf);
  572. }
  573. TEST_P(Test_Int8_nets, SqueezeNet_v1_1)
  574. {
  575. if(target == DNN_TARGET_OPENCL_FP16)
  576. applyTestTag(CV_TEST_TAG_DNN_SKIP_OPENCL_FP16);
  577. if (target == DNN_TARGET_OPENCL_FP16 && !ocl::Device::getDefault().isIntel())
  578. applyTestTag(CV_TEST_TAG_DNN_SKIP_OPENCL_FP16);
  579. if (target == DNN_TARGET_OPENCL && !ocl::Device::getDefault().isIntel())
  580. applyTestTag(CV_TEST_TAG_DNN_SKIP_OPENCL);
  581. Net net = readNetFromCaffe(findDataFile("dnn/squeezenet_v1.1.prototxt"),
  582. findDataFile("dnn/squeezenet_v1.1.caffemodel", false));
  583. Mat inp = imread(_tf("googlenet_0.png"));
  584. Mat blob = blobFromImage(inp, 1.0, Size(227, 227), Scalar(), false, true);
  585. Mat ref = blobFromNPY(_tf("squeezenet_v1.1_prob.npy"));
  586. float l1 = 3e-4, lInf = 0.056;
  587. testClassificationNet(net, blob, ref, l1, lInf);
  588. }
  589. TEST_P(Test_Int8_nets, CaffeNet)
  590. {
  591. #if defined(OPENCV_32BIT_CONFIGURATION) && (defined(HAVE_OPENCL) || defined(_WIN32))
  592. applyTestTag(CV_TEST_TAG_MEMORY_2GB);
  593. #else
  594. applyTestTag(target == DNN_TARGET_CPU ? CV_TEST_TAG_MEMORY_512MB : CV_TEST_TAG_MEMORY_1GB);
  595. #endif
  596. if (target == DNN_TARGET_OPENCL_FP16 && !ocl::Device::getDefault().isIntel())
  597. applyTestTag(CV_TEST_TAG_DNN_SKIP_OPENCL_FP16);
  598. if (target == DNN_TARGET_OPENCL && !ocl::Device::getDefault().isIntel())
  599. applyTestTag(CV_TEST_TAG_DNN_SKIP_OPENCL);
  600. float l1 = 4e-5, lInf = 0.0025;
  601. testONNXNet("caffenet", l1, lInf);
  602. }
  603. TEST_P(Test_Int8_nets, RCNN_ILSVRC13)
  604. {
  605. #if defined(OPENCV_32BIT_CONFIGURATION) && (defined(HAVE_OPENCL) || defined(_WIN32))
  606. applyTestTag(CV_TEST_TAG_MEMORY_2GB);
  607. #else
  608. applyTestTag(target == DNN_TARGET_CPU ? CV_TEST_TAG_MEMORY_512MB : CV_TEST_TAG_MEMORY_1GB);
  609. #endif
  610. if (target == DNN_TARGET_OPENCL_FP16 && !ocl::Device::getDefault().isIntel())
  611. applyTestTag(CV_TEST_TAG_DNN_SKIP_OPENCL_FP16);
  612. if (target == DNN_TARGET_OPENCL && !ocl::Device::getDefault().isIntel())
  613. applyTestTag(CV_TEST_TAG_DNN_SKIP_OPENCL);
  614. float l1 = 0.02, lInf = 0.042;
  615. testONNXNet("rcnn_ilsvrc13", l1, lInf);
  616. }
  617. TEST_P(Test_Int8_nets, Inception_v2)
  618. {
  619. if (target == DNN_TARGET_OPENCL_FP16 && !ocl::Device::getDefault().isIntel())
  620. applyTestTag(CV_TEST_TAG_DNN_SKIP_OPENCL_FP16);
  621. if (target == DNN_TARGET_OPENCL && !ocl::Device::getDefault().isIntel())
  622. applyTestTag(CV_TEST_TAG_DNN_SKIP_OPENCL);
  623. testONNXNet("inception_v2", default_l1, default_lInf, true);
  624. }
  625. TEST_P(Test_Int8_nets, MobileNet_v2)
  626. {
  627. if (target == DNN_TARGET_OPENCL_FP16 && !ocl::Device::getDefault().isIntel())
  628. applyTestTag(CV_TEST_TAG_DNN_SKIP_OPENCL_FP16);
  629. if (target == DNN_TARGET_OPENCL && !ocl::Device::getDefault().isIntel())
  630. applyTestTag(CV_TEST_TAG_DNN_SKIP_OPENCL);
  631. testONNXNet("mobilenetv2", default_l1, default_lInf, true);
  632. }
  633. TEST_P(Test_Int8_nets, Shufflenet)
  634. {
  635. if (target == DNN_TARGET_OPENCL_FP16 && !ocl::Device::getDefault().isIntel())
  636. applyTestTag(CV_TEST_TAG_DNN_SKIP_OPENCL_FP16);
  637. if (target == DNN_TARGET_OPENCL && !ocl::Device::getDefault().isIntel())
  638. applyTestTag(CV_TEST_TAG_DNN_SKIP_OPENCL);
  639. testONNXNet("shufflenet", default_l1, default_lInf);
  640. }
  641. TEST_P(Test_Int8_nets, MobileNet_SSD)
  642. {
  643. if (target == DNN_TARGET_OPENCL_FP16 && !ocl::Device::getDefault().isIntel())
  644. applyTestTag(CV_TEST_TAG_DNN_SKIP_OPENCL_FP16);
  645. if (target == DNN_TARGET_OPENCL && !ocl::Device::getDefault().isIntel())
  646. applyTestTag(CV_TEST_TAG_DNN_SKIP_OPENCL);
  647. Net net = readNetFromCaffe(findDataFile("dnn/MobileNetSSD_deploy.prototxt", false),
  648. findDataFile("dnn/MobileNetSSD_deploy.caffemodel", false));
  649. Mat inp = imread(_tf("street.png"));
  650. Mat blob = blobFromImage(inp, 1.0 / 127.5, Size(300, 300), Scalar(127.5, 127.5, 127.5), false);
  651. Mat ref = blobFromNPY(_tf("mobilenet_ssd_caffe_out.npy"));
  652. float confThreshold = FLT_MIN, scoreDiff = 0.059, iouDiff = 0.11;
  653. testDetectionNet(net, blob, ref, confThreshold, scoreDiff, iouDiff);
  654. }
  655. TEST_P(Test_Int8_nets, MobileNet_v1_SSD)
  656. {
  657. if (target == DNN_TARGET_OPENCL_FP16 && !ocl::Device::getDefault().isIntel())
  658. applyTestTag(CV_TEST_TAG_DNN_SKIP_OPENCL_FP16);
  659. if (target == DNN_TARGET_OPENCL && !ocl::Device::getDefault().isIntel())
  660. applyTestTag(CV_TEST_TAG_DNN_SKIP_OPENCL);
  661. Net net = readNetFromTensorflow(findDataFile("dnn/ssd_mobilenet_v1_coco_2017_11_17.pb", false),
  662. findDataFile("dnn/ssd_mobilenet_v1_coco_2017_11_17.pbtxt"));
  663. Mat inp = imread(_tf("dog416.png"));
  664. Mat blob = blobFromImage(inp, 1.0, Size(300, 300), Scalar(), true, false);
  665. Mat ref = blobFromNPY(_tf("tensorflow/ssd_mobilenet_v1_coco_2017_11_17.detection_out.npy"));
  666. float confThreshold = 0.5, scoreDiff = 0.034, iouDiff = 0.13;
  667. testDetectionNet(net, blob, ref, confThreshold, scoreDiff, iouDiff);
  668. }
  669. TEST_P(Test_Int8_nets, MobileNet_v1_SSD_PPN)
  670. {
  671. if (target == DNN_TARGET_OPENCL_FP16 && !ocl::Device::getDefault().isIntel())
  672. applyTestTag(CV_TEST_TAG_DNN_SKIP_OPENCL_FP16);
  673. if (target == DNN_TARGET_OPENCL && !ocl::Device::getDefault().isIntel())
  674. applyTestTag(CV_TEST_TAG_DNN_SKIP_OPENCL);
  675. Net net = readNetFromTensorflow(findDataFile("dnn/ssd_mobilenet_v1_ppn_coco.pb", false),
  676. findDataFile("dnn/ssd_mobilenet_v1_ppn_coco.pbtxt"));
  677. Mat inp = imread(_tf("dog416.png"));
  678. Mat blob = blobFromImage(inp, 1.0, Size(300, 300), Scalar(), true, false);
  679. Mat ref = blobFromNPY(_tf("tensorflow/ssd_mobilenet_v1_ppn_coco.detection_out.npy"));
  680. float confThreshold = 0.51, scoreDiff = 0.05, iouDiff = 0.06;
  681. testDetectionNet(net, blob, ref, confThreshold, scoreDiff, iouDiff);
  682. }
  683. TEST_P(Test_Int8_nets, Inception_v2_SSD)
  684. {
  685. if (target == DNN_TARGET_OPENCL_FP16 && !ocl::Device::getDefault().isIntel())
  686. applyTestTag(CV_TEST_TAG_DNN_SKIP_OPENCL_FP16);
  687. if (target == DNN_TARGET_OPENCL && !ocl::Device::getDefault().isIntel())
  688. applyTestTag(CV_TEST_TAG_DNN_SKIP_OPENCL);
  689. applyTestTag(target == DNN_TARGET_CPU ? CV_TEST_TAG_MEMORY_512MB : CV_TEST_TAG_MEMORY_1GB);
  690. Net net = readNetFromTensorflow(findDataFile("dnn/ssd_inception_v2_coco_2017_11_17.pb", false),
  691. findDataFile("dnn/ssd_inception_v2_coco_2017_11_17.pbtxt"));
  692. Mat inp = imread(_tf("street.png"));
  693. Mat blob = blobFromImage(inp, 1.0, Size(300, 300), Scalar(), true, false);
  694. Mat ref = (Mat_<float>(5, 7) << 0, 1, 0.90176028, 0.19872092, 0.36311883, 0.26461923, 0.63498729,
  695. 0, 3, 0.93569964, 0.64865261, 0.45906419, 0.80675775, 0.65708131,
  696. 0, 3, 0.75838411, 0.44668293, 0.45907149, 0.49459291, 0.52197015,
  697. 0, 10, 0.95932811, 0.38349164, 0.32528657, 0.40387636, 0.39165527,
  698. 0, 10, 0.93973452, 0.66561931, 0.37841269, 0.68074018, 0.42907384);
  699. float confThreshold = 0.5, scoreDiff = 0.0114, iouDiff = 0.22;
  700. testDetectionNet(net, blob, ref, confThreshold, scoreDiff, iouDiff);
  701. }
  702. TEST_P(Test_Int8_nets, opencv_face_detector)
  703. {
  704. if (target == DNN_TARGET_OPENCL_FP16 && !ocl::Device::getDefault().isIntel())
  705. applyTestTag(CV_TEST_TAG_DNN_SKIP_OPENCL_FP16);
  706. if (target == DNN_TARGET_OPENCL && !ocl::Device::getDefault().isIntel())
  707. applyTestTag(CV_TEST_TAG_DNN_SKIP_OPENCL);
  708. Net net = readNetFromCaffe(findDataFile("dnn/opencv_face_detector.prototxt"),
  709. findDataFile("dnn/opencv_face_detector.caffemodel", false));
  710. Mat inp = imread(findDataFile("gpu/lbpcascade/er.png"));
  711. Mat blob = blobFromImage(inp, 1.0, Size(), Scalar(104.0, 177.0, 123.0), false, false);
  712. Mat ref = (Mat_<float>(6, 7) << 0, 1, 0.99520785, 0.80997437, 0.16379407, 0.87996572, 0.26685631,
  713. 0, 1, 0.9934696, 0.2831718, 0.50738752, 0.345781, 0.5985168,
  714. 0, 1, 0.99096733, 0.13629119, 0.24892329, 0.19756334, 0.3310290,
  715. 0, 1, 0.98977017, 0.23901358, 0.09084064, 0.29902688, 0.1769477,
  716. 0, 1, 0.97203469, 0.67965847, 0.06876482, 0.73999709, 0.1513494,
  717. 0, 1, 0.95097077, 0.51901293, 0.45863652, 0.5777427, 0.5347801);
  718. float confThreshold = 0.5, scoreDiff = 0.002, iouDiff = 0.21;
  719. testDetectionNet(net, blob, ref, confThreshold, scoreDiff, iouDiff);
  720. }
  721. TEST_P(Test_Int8_nets, EfficientDet)
  722. {
  723. if (target == DNN_TARGET_OPENCL_FP16 && !ocl::Device::getDefault().isIntel())
  724. applyTestTag(CV_TEST_TAG_DNN_SKIP_OPENCL_FP16);
  725. if (target == DNN_TARGET_OPENCL && !ocl::Device::getDefault().isIntel())
  726. applyTestTag(CV_TEST_TAG_DNN_SKIP_OPENCL);
  727. if (target != DNN_TARGET_CPU)
  728. {
  729. if (target == DNN_TARGET_OPENCL_FP16) applyTestTag(CV_TEST_TAG_DNN_SKIP_OPENCL_FP16);
  730. if (target == DNN_TARGET_OPENCL) applyTestTag(CV_TEST_TAG_DNN_SKIP_OPENCL);
  731. if (target == DNN_TARGET_MYRIAD) applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_MYRIAD);
  732. }
  733. Net net = readNetFromTensorflow(findDataFile("dnn/efficientdet-d0.pb", false),
  734. findDataFile("dnn/efficientdet-d0.pbtxt"));
  735. Mat inp = imread(_tf("dog416.png"));
  736. Mat blob = blobFromImage(inp, 1.0/255, Size(512, 512), Scalar(123.675, 116.28, 103.53));
  737. Mat ref = (Mat_<float>(3, 7) << 0, 1, 0.8437444, 0.153996080160141, 0.20534580945968628, 0.7463544607162476, 0.7414066195487976,
  738. 0, 17, 0.8245924, 0.16657517850399017, 0.3996818959712982, 0.4111558794975281, 0.9306337833404541,
  739. 0, 7, 0.8039304, 0.6118435263633728, 0.13175517320632935, 0.9065558314323425, 0.2943994700908661);
  740. float confThreshold = 0.65, scoreDiff = 0.17, iouDiff = 0.18;
  741. testDetectionNet(net, blob, ref, confThreshold, scoreDiff, iouDiff);
  742. }
  743. TEST_P(Test_Int8_nets, FasterRCNN_resnet50)
  744. {
  745. applyTestTag(
  746. (target == DNN_TARGET_CPU ? CV_TEST_TAG_MEMORY_1GB : CV_TEST_TAG_MEMORY_2GB),
  747. CV_TEST_TAG_LONG,
  748. CV_TEST_TAG_DEBUG_VERYLONG
  749. );
  750. if (target == DNN_TARGET_OPENCL_FP16 && !ocl::Device::getDefault().isIntel())
  751. applyTestTag(CV_TEST_TAG_DNN_SKIP_OPENCL_FP16);
  752. if (target == DNN_TARGET_OPENCL && !ocl::Device::getDefault().isIntel())
  753. applyTestTag(CV_TEST_TAG_DNN_SKIP_OPENCL);
  754. if (backend == DNN_BACKEND_OPENCV && target == DNN_TARGET_OPENCL_FP16)
  755. applyTestTag(CV_TEST_TAG_DNN_SKIP_OPENCL_FP16);
  756. Net net = readNetFromTensorflow(findDataFile("dnn/faster_rcnn_resnet50_coco_2018_01_28.pb", false),
  757. findDataFile("dnn/faster_rcnn_resnet50_coco_2018_01_28.pbtxt"));
  758. Mat inp = imread(_tf("dog416.png"));
  759. Mat blob = blobFromImage(inp, 1.0, Size(800, 600), Scalar(), true, false);
  760. Mat ref = blobFromNPY(_tf("tensorflow/faster_rcnn_resnet50_coco_2018_01_28.detection_out.npy"));
  761. float confThreshold = 0.5, scoreDiff = 0.05, iouDiff = 0.15;
  762. testDetectionNet(net, blob, ref, confThreshold, scoreDiff, iouDiff);
  763. }
  764. TEST_P(Test_Int8_nets, FasterRCNN_inceptionv2)
  765. {
  766. applyTestTag(
  767. (target == DNN_TARGET_CPU ? CV_TEST_TAG_MEMORY_1GB : CV_TEST_TAG_MEMORY_2GB),
  768. CV_TEST_TAG_LONG,
  769. CV_TEST_TAG_DEBUG_VERYLONG
  770. );
  771. if (target == DNN_TARGET_OPENCL_FP16 && !ocl::Device::getDefault().isIntel())
  772. applyTestTag(CV_TEST_TAG_DNN_SKIP_OPENCL_FP16);
  773. if (target == DNN_TARGET_OPENCL && !ocl::Device::getDefault().isIntel())
  774. applyTestTag(CV_TEST_TAG_DNN_SKIP_OPENCL);
  775. if (backend == DNN_BACKEND_OPENCV && target == DNN_TARGET_OPENCL_FP16)
  776. applyTestTag(CV_TEST_TAG_DNN_SKIP_OPENCL_FP16);
  777. Net net = readNetFromTensorflow(findDataFile("dnn/faster_rcnn_inception_v2_coco_2018_01_28.pb", false),
  778. findDataFile("dnn/faster_rcnn_inception_v2_coco_2018_01_28.pbtxt"));
  779. Mat inp = imread(_tf("dog416.png"));
  780. Mat blob = blobFromImage(inp, 1.0, Size(800, 600), Scalar(), true, false);
  781. Mat ref = blobFromNPY(_tf("tensorflow/faster_rcnn_inception_v2_coco_2018_01_28.detection_out.npy"));
  782. float confThreshold = 0.5, scoreDiff = 0.21, iouDiff = 0.1;
  783. testDetectionNet(net, blob, ref, confThreshold, scoreDiff, iouDiff);
  784. }
  785. TEST_P(Test_Int8_nets, FasterRCNN_vgg16)
  786. {
  787. applyTestTag(
  788. #if defined(OPENCV_32BIT_CONFIGURATION) && defined(HAVE_OPENCL)
  789. CV_TEST_TAG_MEMORY_2GB,
  790. #else
  791. (target == DNN_TARGET_CPU ? CV_TEST_TAG_MEMORY_1GB : CV_TEST_TAG_MEMORY_2GB),
  792. #endif
  793. CV_TEST_TAG_LONG,
  794. CV_TEST_TAG_DEBUG_VERYLONG
  795. );
  796. if (target == DNN_TARGET_OPENCL_FP16 && !ocl::Device::getDefault().isIntel())
  797. applyTestTag(CV_TEST_TAG_DNN_SKIP_OPENCL_FP16);
  798. if (target == DNN_TARGET_OPENCL && !ocl::Device::getDefault().isIntel())
  799. applyTestTag(CV_TEST_TAG_DNN_SKIP_OPENCL);
  800. Net net = readNetFromCaffe(findDataFile("dnn/faster_rcnn_vgg16.prototxt"),
  801. findDataFile("dnn/VGG16_faster_rcnn_final.caffemodel", false));
  802. Mat ref = (Mat_<float>(3, 7) << 0, 2, 0.949398, 99.2454, 210.141, 601.205, 462.849,
  803. 0, 7, 0.997022, 481.841, 92.3218, 722.685, 175.953,
  804. 0, 12, 0.993028, 133.221, 189.377, 350.994, 563.166);
  805. float confThreshold = 0.8, scoreDiff = 0.024, iouDiff = 0.35;
  806. testFaster(net, ref, confThreshold, scoreDiff, iouDiff);
  807. }
  808. TEST_P(Test_Int8_nets, FasterRCNN_zf)
  809. {
  810. applyTestTag(
  811. #if defined(OPENCV_32BIT_CONFIGURATION) && defined(HAVE_OPENCL)
  812. CV_TEST_TAG_MEMORY_2GB,
  813. #else
  814. (target == DNN_TARGET_CPU ? CV_TEST_TAG_MEMORY_512MB : CV_TEST_TAG_MEMORY_1GB),
  815. #endif
  816. CV_TEST_TAG_DEBUG_LONG
  817. );
  818. if (target == DNN_TARGET_OPENCL_FP16 && !ocl::Device::getDefault().isIntel())
  819. applyTestTag(CV_TEST_TAG_DNN_SKIP_OPENCL_FP16);
  820. if (target == DNN_TARGET_OPENCL && !ocl::Device::getDefault().isIntel())
  821. applyTestTag(CV_TEST_TAG_DNN_SKIP_OPENCL);
  822. Net net = readNetFromCaffe(findDataFile("dnn/faster_rcnn_zf.prototxt"),
  823. findDataFile("dnn/ZF_faster_rcnn_final.caffemodel", false));
  824. Mat ref = (Mat_<float>(3, 7) << 0, 2, 0.90121, 120.407, 115.83, 570.586, 528.395,
  825. 0, 7, 0.988779, 469.849, 75.1756, 718.64, 186.762,
  826. 0, 12, 0.967198, 138.588, 206.843, 329.766, 553.176);
  827. float confThreshold = 0.8, scoreDiff = 0.021, iouDiff = 0.1;
  828. testFaster(net, ref, confThreshold, scoreDiff, iouDiff);
  829. }
  830. TEST_P(Test_Int8_nets, RFCN)
  831. {
  832. applyTestTag(
  833. (target == DNN_TARGET_CPU ? CV_TEST_TAG_MEMORY_512MB : CV_TEST_TAG_MEMORY_2GB),
  834. CV_TEST_TAG_LONG,
  835. CV_TEST_TAG_DEBUG_VERYLONG
  836. );
  837. if (target == DNN_TARGET_OPENCL_FP16 && !ocl::Device::getDefault().isIntel())
  838. applyTestTag(CV_TEST_TAG_DNN_SKIP_OPENCL_FP16);
  839. if (target == DNN_TARGET_OPENCL && !ocl::Device::getDefault().isIntel())
  840. applyTestTag(CV_TEST_TAG_DNN_SKIP_OPENCL);
  841. Net net = readNetFromCaffe(findDataFile("dnn/rfcn_pascal_voc_resnet50.prototxt"),
  842. findDataFile("dnn/resnet50_rfcn_final.caffemodel", false));
  843. Mat ref = (Mat_<float>(2, 7) << 0, 7, 0.991359, 491.822, 81.1668, 702.573, 178.234,
  844. 0, 12, 0.94786, 132.093, 223.903, 338.077, 566.16);
  845. float confThreshold = 0.8, scoreDiff = 0.017, iouDiff = 0.11;
  846. testFaster(net, ref, confThreshold, scoreDiff, iouDiff);
  847. }
  848. TEST_P(Test_Int8_nets, YoloVoc)
  849. {
  850. applyTestTag(
  851. #if defined(OPENCV_32BIT_CONFIGURATION) && defined(HAVE_OPENCL)
  852. CV_TEST_TAG_MEMORY_2GB,
  853. #else
  854. CV_TEST_TAG_MEMORY_1GB,
  855. #endif
  856. CV_TEST_TAG_LONG
  857. );
  858. if (target == DNN_TARGET_OPENCL_FP16 && !ocl::Device::getDefault().isIntel())
  859. applyTestTag(CV_TEST_TAG_DNN_SKIP_OPENCL_FP16);
  860. if (target == DNN_TARGET_OPENCL && !ocl::Device::getDefault().isIntel())
  861. applyTestTag(CV_TEST_TAG_DNN_SKIP_OPENCL);
  862. Mat ref = (Mat_<float>(6, 7) << 0, 6, 0.750469f, 0.577374f, 0.127391f, 0.902949f, 0.300809f,
  863. 0, 1, 0.780879f, 0.270762f, 0.264102f, 0.732475f, 0.745412f,
  864. 0, 11, 0.901615f, 0.1386f, 0.338509f, 0.421337f, 0.938789f,
  865. 1, 14, 0.623813f, 0.183179f, 0.381921f, 0.247726f, 0.625847f,
  866. 1, 6, 0.667770f, 0.446555f, 0.453578f, 0.499986f, 0.519167f,
  867. 1, 6, 0.844947f, 0.637058f, 0.460398f, 0.828508f, 0.66427f);
  868. std::string config_file = "yolo-voc.cfg";
  869. std::string weights_file = "yolo-voc.weights";
  870. double scoreDiff = 0.1, iouDiff = 0.3;
  871. {
  872. SCOPED_TRACE("batch size 1");
  873. testDarknetModel(config_file, weights_file, ref.rowRange(0, 3), scoreDiff, iouDiff);
  874. }
  875. {
  876. SCOPED_TRACE("batch size 2");
  877. testDarknetModel(config_file, weights_file, ref, scoreDiff, iouDiff);
  878. }
  879. }
  880. TEST_P(Test_Int8_nets, TinyYoloVoc)
  881. {
  882. applyTestTag(CV_TEST_TAG_MEMORY_512MB);
  883. if (target == DNN_TARGET_OPENCL_FP16 && !ocl::Device::getDefault().isIntel())
  884. applyTestTag(CV_TEST_TAG_DNN_SKIP_OPENCL_FP16);
  885. if (target == DNN_TARGET_OPENCL && !ocl::Device::getDefault().isIntel())
  886. applyTestTag(CV_TEST_TAG_DNN_SKIP_OPENCL);
  887. Mat ref = (Mat_<float>(4, 7) << 0, 6, 0.761967f, 0.579042f, 0.159161f, 0.894482f, 0.31994f,
  888. 0, 11, 0.780595f, 0.129696f, 0.386467f, 0.445275f, 0.920994f,
  889. 1, 6, 0.651450f, 0.460526f, 0.458019f, 0.522527f, 0.5341f,
  890. 1, 6, 0.928758f, 0.651024f, 0.463539f, 0.823784f, 0.654998f);
  891. std::string config_file = "tiny-yolo-voc.cfg";
  892. std::string weights_file = "tiny-yolo-voc.weights";
  893. double scoreDiff = 0.043, iouDiff = 0.12;
  894. {
  895. SCOPED_TRACE("batch size 1");
  896. testDarknetModel(config_file, weights_file, ref.rowRange(0, 2), scoreDiff, iouDiff);
  897. }
  898. {
  899. SCOPED_TRACE("batch size 2");
  900. testDarknetModel(config_file, weights_file, ref, scoreDiff, iouDiff);
  901. }
  902. }
  903. TEST_P(Test_Int8_nets, YOLOv3)
  904. {
  905. applyTestTag(CV_TEST_TAG_LONG, (target == DNN_TARGET_CPU ? CV_TEST_TAG_MEMORY_1GB : CV_TEST_TAG_MEMORY_2GB));
  906. if (target == DNN_TARGET_OPENCL_FP16 && !ocl::Device::getDefault().isIntel())
  907. applyTestTag(CV_TEST_TAG_DNN_SKIP_OPENCL_FP16);
  908. if (target == DNN_TARGET_OPENCL && !ocl::Device::getDefault().isIntel())
  909. applyTestTag(CV_TEST_TAG_DNN_SKIP_OPENCL);
  910. const int N0 = 3;
  911. const int N1 = 6;
  912. static const float ref_[/* (N0 + N1) * 7 */] = {
  913. 0, 16, 0.998836f, 0.160024f, 0.389964f, 0.417885f, 0.943716f,
  914. 0, 1, 0.987908f, 0.150913f, 0.221933f, 0.742255f, 0.746261f,
  915. 0, 7, 0.952983f, 0.614621f, 0.150257f, 0.901368f, 0.289251f,
  916. 1, 2, 0.997412f, 0.647584f, 0.459939f, 0.821037f, 0.663947f,
  917. 1, 2, 0.989633f, 0.450719f, 0.463353f, 0.496306f, 0.522258f,
  918. 1, 0, 0.980053f, 0.195856f, 0.378454f, 0.258626f, 0.629257f,
  919. 1, 9, 0.785341f, 0.665503f, 0.373543f, 0.688893f, 0.439244f,
  920. 1, 9, 0.733275f, 0.376029f, 0.315694f, 0.401776f, 0.395165f,
  921. 1, 9, 0.384815f, 0.659824f, 0.372389f, 0.673927f, 0.429412f,
  922. };
  923. Mat ref(N0 + N1, 7, CV_32FC1, (void*)ref_);
  924. std::string config_file = "yolov3.cfg";
  925. std::string weights_file = "yolov3.weights";
  926. double scoreDiff = 0.08, iouDiff = 0.21, confThreshold = 0.25;
  927. {
  928. SCOPED_TRACE("batch size 1");
  929. testDarknetModel(config_file, weights_file, ref.rowRange(0, N0), scoreDiff, iouDiff, confThreshold);
  930. }
  931. {
  932. SCOPED_TRACE("batch size 2");
  933. testDarknetModel(config_file, weights_file, ref, scoreDiff, iouDiff, confThreshold);
  934. }
  935. }
  936. TEST_P(Test_Int8_nets, YOLOv4)
  937. {
  938. applyTestTag(CV_TEST_TAG_LONG, (target == DNN_TARGET_CPU ? CV_TEST_TAG_MEMORY_1GB : CV_TEST_TAG_MEMORY_2GB));
  939. if (target == DNN_TARGET_OPENCL_FP16 && !ocl::Device::getDefault().isIntel())
  940. applyTestTag(CV_TEST_TAG_DNN_SKIP_OPENCL_FP16);
  941. if (target == DNN_TARGET_OPENCL && !ocl::Device::getDefault().isIntel())
  942. applyTestTag(CV_TEST_TAG_DNN_SKIP_OPENCL);
  943. const int N0 = 3;
  944. const int N1 = 7;
  945. static const float ref_[/* (N0 + N1) * 7 */] = {
  946. 0, 16, 0.992194f, 0.172375f, 0.402458f, 0.403918f, 0.932801f,
  947. 0, 1, 0.988326f, 0.166708f, 0.228236f, 0.737208f, 0.735803f,
  948. 0, 7, 0.94639f, 0.602523f, 0.130399f, 0.901623f, 0.298452f,
  949. 1, 2, 0.99761f, 0.646556f, 0.45985f, 0.816041f, 0.659067f,
  950. 1, 0, 0.988913f, 0.201726f, 0.360282f, 0.266181f, 0.631728f,
  951. 1, 2, 0.98233f, 0.452007f, 0.462217f, 0.495612f, 0.521687f,
  952. 1, 9, 0.919195f, 0.374642f, 0.316524f, 0.398126f, 0.393714f,
  953. 1, 9, 0.856303f, 0.666842f, 0.372215f, 0.685539f, 0.44141f,
  954. 1, 9, 0.313516f, 0.656791f, 0.374734f, 0.671959f, 0.438371f,
  955. 1, 9, 0.256625f, 0.940232f, 0.326931f, 0.967586f, 0.374002f,
  956. };
  957. Mat ref(N0 + N1, 7, CV_32FC1, (void*)ref_);
  958. std::string config_file = "yolov4.cfg";
  959. std::string weights_file = "yolov4.weights";
  960. double scoreDiff = 0.15, iouDiff = 0.2;
  961. {
  962. SCOPED_TRACE("batch size 1");
  963. testDarknetModel(config_file, weights_file, ref.rowRange(0, N0), scoreDiff, iouDiff);
  964. }
  965. {
  966. SCOPED_TRACE("batch size 2");
  967. testDarknetModel(config_file, weights_file, ref, scoreDiff, iouDiff);
  968. }
  969. }
  970. TEST_P(Test_Int8_nets, YOLOv4_tiny)
  971. {
  972. applyTestTag(
  973. target == DNN_TARGET_CPU ? CV_TEST_TAG_MEMORY_512MB : CV_TEST_TAG_MEMORY_1GB
  974. );
  975. if (target == DNN_TARGET_OPENCL_FP16 && !ocl::Device::getDefault().isIntel())
  976. applyTestTag(CV_TEST_TAG_DNN_SKIP_OPENCL_FP16);
  977. if (target == DNN_TARGET_OPENCL && !ocl::Device::getDefault().isIntel())
  978. applyTestTag(CV_TEST_TAG_DNN_SKIP_OPENCL);
  979. const float confThreshold = 0.6;
  980. const int N0 = 2;
  981. const int N1 = 3;
  982. static const float ref_[/* (N0 + N1) * 7 */] = {
  983. 0, 7, 0.85935f, 0.593484f, 0.141211f, 0.920356f, 0.291593f,
  984. 0, 16, 0.795188f, 0.169207f, 0.386886f, 0.423753f, 0.933004f,
  985. 1, 2, 0.996832f, 0.653802f, 0.464573f, 0.815193f, 0.653292f,
  986. 1, 2, 0.963325f, 0.451151f, 0.458915f, 0.496255f, 0.52241f,
  987. 1, 0, 0.926244f, 0.194851f, 0.361743f, 0.260277f, 0.632364f,
  988. };
  989. Mat ref(N0 + N1, 7, CV_32FC1, (void*)ref_);
  990. std::string config_file = "yolov4-tiny.cfg";
  991. std::string weights_file = "yolov4-tiny.weights";
  992. double scoreDiff = 0.12;
  993. double iouDiff = target == DNN_TARGET_OPENCL_FP16 ? 0.2 : 0.082;
  994. {
  995. SCOPED_TRACE("batch size 1");
  996. testDarknetModel(config_file, weights_file, ref.rowRange(0, N0), scoreDiff, iouDiff, confThreshold);
  997. }
  998. throw SkipTestException("batch2: bad accuracy on second image");
  999. /* bad accuracy on second image
  1000. {
  1001. SCOPED_TRACE("batch size 2");
  1002. testDarknetModel(config_file, weights_file, ref, scoreDiff, iouDiff, confThreshold);
  1003. }
  1004. */
  1005. }
  1006. INSTANTIATE_TEST_CASE_P(/**/, Test_Int8_nets, dnnBackendsAndTargetsInt8());
  1007. }} // namespace