test_caffe_importer.cpp 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869
  1. /*M///////////////////////////////////////////////////////////////////////////////////////
  2. //
  3. // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
  4. //
  5. // By downloading, copying, installing or using the software you agree to this license.
  6. // If you do not agree to this license, do not download, install,
  7. // copy or use the software.
  8. //
  9. //
  10. // License Agreement
  11. // For Open Source Computer Vision Library
  12. //
  13. // Copyright (C) 2013, OpenCV Foundation, all rights reserved.
  14. // Third party copyrights are property of their respective owners.
  15. //
  16. // Redistribution and use in source and binary forms, with or without modification,
  17. // are permitted provided that the following conditions are met:
  18. //
  19. // * Redistribution's of source code must retain the above copyright notice,
  20. // this list of conditions and the following disclaimer.
  21. //
  22. // * Redistribution's in binary form must reproduce the above copyright notice,
  23. // this list of conditions and the following disclaimer in the documentation
  24. // and/or other materials provided with the distribution.
  25. //
  26. // * The name of the copyright holders may not be used to endorse or promote products
  27. // derived from this software without specific prior written permission.
  28. //
  29. // This software is provided by the copyright holders and contributors "as is" and
  30. // any express or implied warranties, including, but not limited to, the implied
  31. // warranties of merchantability and fitness for a particular purpose are disclaimed.
  32. // In no event shall the Intel Corporation or contributors be liable for any direct,
  33. // indirect, incidental, special, exemplary, or consequential damages
  34. // (including, but not limited to, procurement of substitute goods or services;
  35. // loss of use, data, or profits; or business interruption) however caused
  36. // and on any theory of liability, whether in contract, strict liability,
  37. // or tort (including negligence or otherwise) arising in any way out of
  38. // the use of this software, even if advised of the possibility of such damage.
  39. //
  40. //M*/
  41. #include "test_precomp.hpp"
  42. #include "npy_blob.hpp"
  43. #include <opencv2/dnn/shape_utils.hpp>
  44. namespace opencv_test { namespace {
  45. template<typename TString>
  46. static std::string _tf(TString filename)
  47. {
  48. return findDataFile(std::string("dnn/") + filename);
  49. }
  50. class Test_Caffe_nets : public DNNTestLayer
  51. {
  52. public:
  53. void testFaster(const std::string& proto, const std::string& model, const Mat& ref,
  54. double scoreDiff = 0.0, double iouDiff = 0.0)
  55. {
  56. checkBackend();
  57. Net net = readNetFromCaffe(findDataFile("dnn/" + proto),
  58. findDataFile("dnn/" + model, false));
  59. net.setPreferableBackend(backend);
  60. net.setPreferableTarget(target);
  61. if (target == DNN_TARGET_CPU_FP16)
  62. net.enableWinograd(false);
  63. Mat img = imread(findDataFile("dnn/dog416.png"));
  64. resize(img, img, Size(800, 600));
  65. Mat blob = blobFromImage(img, 1.0, Size(), Scalar(102.9801, 115.9465, 122.7717), false, false);
  66. Mat imInfo = (Mat_<float>(1, 3) << img.rows, img.cols, 1.6f);
  67. net.setInput(blob, "data");
  68. net.setInput(imInfo, "im_info");
  69. // Output has shape 1x1xNx7 where N - number of detections.
  70. // An every detection is a vector of values [id, classId, confidence, left, top, right, bottom]
  71. Mat out = net.forward();
  72. scoreDiff = scoreDiff ? scoreDiff : default_l1;
  73. iouDiff = iouDiff ? iouDiff : default_lInf;
  74. normAssertDetections(ref, out, ("model name: " + model).c_str(), 0.8, scoreDiff, iouDiff);
  75. }
  76. };
  77. TEST(Test_Caffe, memory_read)
  78. {
  79. const string proto = findDataFile("dnn/bvlc_googlenet.prototxt");
  80. const string model = findDataFile("dnn/bvlc_googlenet.caffemodel", false);
  81. std::vector<char> dataProto;
  82. readFileContent(proto, dataProto);
  83. std::vector<char> dataModel;
  84. readFileContent(model, dataModel);
  85. Net net = readNetFromCaffe(dataProto.data(), dataProto.size());
  86. net.setPreferableBackend(DNN_BACKEND_OPENCV);
  87. ASSERT_FALSE(net.empty());
  88. Net net2 = readNetFromCaffe(dataProto.data(), dataProto.size(),
  89. dataModel.data(), dataModel.size());
  90. ASSERT_FALSE(net2.empty());
  91. }
  92. TEST(Test_Caffe, read_gtsrb)
  93. {
  94. Net net = readNetFromCaffe(_tf("gtsrb.prototxt"));
  95. ASSERT_FALSE(net.empty());
  96. }
  97. TEST(Test_Caffe, read_googlenet)
  98. {
  99. Net net = readNetFromCaffe(_tf("bvlc_googlenet.prototxt"));
  100. ASSERT_FALSE(net.empty());
  101. }
  102. TEST_P(Test_Caffe_nets, Axpy)
  103. {
  104. #if defined(INF_ENGINE_RELEASE) && INF_ENGINE_VER_MAJOR_LT(2021040000)
  105. if (backend == DNN_BACKEND_INFERENCE_ENGINE_NN_BUILDER_2019)
  106. applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_NN_BUILDER);
  107. if (backend == DNN_BACKEND_INFERENCE_ENGINE_NGRAPH)
  108. applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_NGRAPH);
  109. #endif
  110. String proto = _tf("axpy.prototxt");
  111. Net net = readNetFromCaffe(proto);
  112. checkBackend();
  113. net.setPreferableBackend(backend);
  114. net.setPreferableTarget(target);
  115. int size[] = {1, 2, 3, 4};
  116. int scale_size[] = {1, 2, 1, 1};
  117. Mat scale(4, &scale_size[0], CV_32F);
  118. Mat shift(4, &size[0], CV_32F);
  119. Mat inp(4, &size[0], CV_32F);
  120. randu(scale, -1.0f, 1.0f);
  121. randu(shift, -1.0f, 1.0f);
  122. randu(inp, -1.0f, 1.0f);
  123. net.setInput(scale, "scale");
  124. net.setInput(shift, "shift");
  125. net.setInput(inp, "data");
  126. Mat out = net.forward();
  127. Mat ref(4, &size[0], inp.type());
  128. for (int i = 0; i < inp.size[1]; i++) {
  129. for (int h = 0; h < inp.size[2]; h++) {
  130. for (int w = 0; w < inp.size[3]; w++) {
  131. int idx[] = {0, i, h, w};
  132. int scale_idx[] = {0, i, 0, 0};
  133. ref.at<float>(idx) = inp.at<float>(idx) * scale.at<float>(scale_idx) +
  134. shift.at<float>(idx);
  135. }
  136. }
  137. }
  138. float l1 = 1e-5, lInf = 1e-4;
  139. if (target == DNN_TARGET_OPENCL_FP16 || target == DNN_TARGET_CPU_FP16)
  140. {
  141. l1 = 2e-4;
  142. lInf = 1e-3;
  143. }
  144. if (target == DNN_TARGET_MYRIAD)
  145. {
  146. l1 = 0.001;
  147. lInf = 0.001;
  148. }
  149. if(target == DNN_TARGET_CUDA_FP16)
  150. {
  151. l1 = 0.0002;
  152. lInf = 0.0007;
  153. }
  154. normAssert(ref, out, "", l1, lInf);
  155. }
  156. typedef testing::TestWithParam<tuple<bool, Target> > Reproducibility_AlexNet;
  157. TEST_P(Reproducibility_AlexNet, Accuracy)
  158. {
  159. Target targetId = get<1>(GetParam());
  160. #if defined(OPENCV_32BIT_CONFIGURATION) && defined(HAVE_OPENCL)
  161. applyTestTag(CV_TEST_TAG_MEMORY_2GB);
  162. #else
  163. applyTestTag(targetId == DNN_TARGET_CPU ? CV_TEST_TAG_MEMORY_512MB : CV_TEST_TAG_MEMORY_1GB);
  164. #endif
  165. ASSERT_TRUE(ocl::useOpenCL() || targetId == DNN_TARGET_CPU || targetId == DNN_TARGET_CPU_FP16);
  166. bool readFromMemory = get<0>(GetParam());
  167. Net net;
  168. {
  169. const string proto = findDataFile("dnn/bvlc_alexnet.prototxt");
  170. const string model = findDataFile("dnn/bvlc_alexnet.caffemodel", false);
  171. if (readFromMemory)
  172. {
  173. std::vector<char> dataProto;
  174. readFileContent(proto, dataProto);
  175. std::vector<char> dataModel;
  176. readFileContent(model, dataModel);
  177. net = readNetFromCaffe(dataProto.data(), dataProto.size(),
  178. dataModel.data(), dataModel.size());
  179. }
  180. else
  181. net = readNetFromCaffe(proto, model);
  182. ASSERT_FALSE(net.empty());
  183. }
  184. // Test input layer size
  185. std::vector<MatShape> inLayerShapes;
  186. std::vector<MatShape> outLayerShapes;
  187. net.getLayerShapes(MatShape(), 0, inLayerShapes, outLayerShapes);
  188. ASSERT_FALSE(inLayerShapes.empty());
  189. ASSERT_EQ(inLayerShapes[0].size(), 4);
  190. ASSERT_EQ(inLayerShapes[0][0], 1);
  191. ASSERT_EQ(inLayerShapes[0][1], 3);
  192. ASSERT_EQ(inLayerShapes[0][2], 227);
  193. ASSERT_EQ(inLayerShapes[0][3], 227);
  194. const float l1 = 1e-5;
  195. const float lInf = (targetId == DNN_TARGET_OPENCL_FP16 || targetId == DNN_TARGET_CPU_FP16) ? 4e-3 : 1e-4;
  196. net.setPreferableBackend(DNN_BACKEND_OPENCV);
  197. net.setPreferableTarget(targetId);
  198. if (targetId == DNN_TARGET_CPU_FP16)
  199. net.enableWinograd(false);
  200. Mat sample = imread(_tf("grace_hopper_227.png"));
  201. ASSERT_TRUE(!sample.empty());
  202. net.setInput(blobFromImage(sample, 1.0f, Size(227, 227), Scalar(), false), "data");
  203. Mat out = net.forward("prob");
  204. Mat ref = blobFromNPY(_tf("caffe_alexnet_prob.npy"));
  205. normAssert(ref, out, "", l1, lInf);
  206. }
  207. INSTANTIATE_TEST_CASE_P(/**/, Reproducibility_AlexNet, Combine(testing::Bool(),
  208. testing::ValuesIn(getAvailableTargets(DNN_BACKEND_OPENCV))));
  209. TEST(Reproducibility_FCN, Accuracy)
  210. {
  211. applyTestTag(CV_TEST_TAG_LONG, CV_TEST_TAG_DEBUG_VERYLONG, CV_TEST_TAG_MEMORY_2GB);
  212. Net net;
  213. {
  214. const string proto = findDataFile("dnn/fcn8s-heavy-pascal.prototxt");
  215. const string model = findDataFile("dnn/fcn8s-heavy-pascal.caffemodel", false);
  216. net = readNetFromCaffe(proto, model);
  217. ASSERT_FALSE(net.empty());
  218. }
  219. net.setPreferableBackend(DNN_BACKEND_OPENCV);
  220. Mat sample = imread(_tf("street.png"));
  221. ASSERT_TRUE(!sample.empty());
  222. std::vector<int> layerIds;
  223. std::vector<size_t> weights, blobs;
  224. net.getMemoryConsumption(shape(1,3,227,227), layerIds, weights, blobs);
  225. net.setInput(blobFromImage(sample, 1.0f, Size(500, 500), Scalar(), false), "data");
  226. Mat out = net.forward("score");
  227. Mat refData = imread(_tf("caffe_fcn8s_prob.png"), IMREAD_ANYDEPTH);
  228. int shape[] = {1, 21, 500, 500};
  229. Mat ref(4, shape, CV_32FC1, refData.data);
  230. normAssert(ref, out);
  231. }
  232. TEST(Reproducibility_SSD, Accuracy)
  233. {
  234. applyTestTag(
  235. CV_TEST_TAG_MEMORY_512MB,
  236. CV_TEST_TAG_DEBUG_VERYLONG
  237. );
  238. Net net;
  239. {
  240. const string proto = findDataFile("dnn/ssd_vgg16.prototxt");
  241. const string model = findDataFile("dnn/VGG_ILSVRC2016_SSD_300x300_iter_440000.caffemodel", false);
  242. net = readNetFromCaffe(proto, model);
  243. ASSERT_FALSE(net.empty());
  244. }
  245. net.setPreferableBackend(DNN_BACKEND_OPENCV);
  246. Mat sample = imread(_tf("street.png"));
  247. ASSERT_TRUE(!sample.empty());
  248. if (sample.channels() == 4)
  249. cvtColor(sample, sample, COLOR_BGRA2BGR);
  250. Mat in_blob = blobFromImage(sample, 1.0f, Size(300, 300), Scalar(), false);
  251. net.setInput(in_blob, "data");
  252. Mat out = net.forward("detection_out");
  253. Mat ref = blobFromNPY(_tf("ssd_out.npy"));
  254. normAssertDetections(ref, out, "", 0.06);
  255. }
  256. typedef testing::TestWithParam<tuple<Backend, Target> > Reproducibility_MobileNet_SSD;
  257. TEST_P(Reproducibility_MobileNet_SSD, Accuracy)
  258. {
  259. const string proto = findDataFile("dnn/MobileNetSSD_deploy_19e3ec3.prototxt", false);
  260. const string model = findDataFile("dnn/MobileNetSSD_deploy_19e3ec3.caffemodel", false);
  261. Net net = readNetFromCaffe(proto, model);
  262. int backendId = get<0>(GetParam());
  263. int targetId = get<1>(GetParam());
  264. net.setPreferableBackend(backendId);
  265. net.setPreferableTarget(targetId);
  266. Mat sample = imread(_tf("street.png"));
  267. Mat inp = blobFromImage(sample, 1.0f / 127.5, Size(300, 300), Scalar(127.5, 127.5, 127.5), false);
  268. net.setInput(inp);
  269. Mat out = net.forward().clone();
  270. ASSERT_EQ(out.size[2], 100);
  271. float scores_diff = 1e-5, boxes_iou_diff = 1e-4;
  272. if (targetId == DNN_TARGET_OPENCL_FP16 || targetId == DNN_TARGET_MYRIAD || targetId == DNN_TARGET_CPU_FP16)
  273. {
  274. scores_diff = 1.5e-2;
  275. boxes_iou_diff = 6.3e-2;
  276. }
  277. else if (targetId == DNN_TARGET_CUDA_FP16)
  278. {
  279. scores_diff = 0.015;
  280. boxes_iou_diff = 0.07;
  281. }
  282. Mat ref = blobFromNPY(_tf("mobilenet_ssd_caffe_out.npy"));
  283. normAssertDetections(ref, out, "", FLT_MIN, scores_diff, boxes_iou_diff);
  284. // Check that detections aren't preserved.
  285. inp.setTo(0.0f);
  286. net.setInput(inp);
  287. Mat zerosOut = net.forward();
  288. zerosOut = zerosOut.reshape(1, zerosOut.total() / 7);
  289. const int numDetections = zerosOut.rows;
  290. // TODO: fix it
  291. if (targetId != DNN_TARGET_MYRIAD ||
  292. getInferenceEngineVPUType() != CV_DNN_INFERENCE_ENGINE_VPU_TYPE_MYRIAD_X)
  293. {
  294. ASSERT_NE(numDetections, 0);
  295. for (int i = 0; i < numDetections; ++i)
  296. {
  297. float confidence = zerosOut.ptr<float>(i)[2];
  298. ASSERT_EQ(confidence, 0);
  299. }
  300. }
  301. // There is something wrong with Reshape layer in Myriad plugin.
  302. if (backendId == DNN_BACKEND_INFERENCE_ENGINE_NN_BUILDER_2019
  303. || backendId == DNN_BACKEND_INFERENCE_ENGINE_NGRAPH
  304. )
  305. {
  306. if (targetId == DNN_TARGET_MYRIAD || targetId == DNN_TARGET_OPENCL_FP16)
  307. return;
  308. }
  309. // Check batching mode.
  310. inp = blobFromImages(std::vector<Mat>(2, sample), 1.0f / 127.5, Size(300, 300), Scalar(127.5, 127.5, 127.5), false);
  311. net.setInput(inp);
  312. Mat outBatch = net.forward();
  313. // Output blob has a shape 1x1x2Nx7 where N is a number of detection for
  314. // a single sample in batch. The first numbers of detection vectors are batch id.
  315. // For Inference Engine backend there is -1 delimiter which points the end of detections.
  316. const int numRealDetections = ref.size[2];
  317. EXPECT_EQ(outBatch.size[2], 2 * numDetections);
  318. out = out.reshape(1, numDetections).rowRange(0, numRealDetections);
  319. outBatch = outBatch.reshape(1, 2 * numDetections);
  320. for (int i = 0; i < 2; ++i)
  321. {
  322. Mat pred = outBatch.rowRange(i * numRealDetections, (i + 1) * numRealDetections);
  323. EXPECT_EQ(countNonZero(pred.col(0) != i), 0);
  324. normAssert(pred.colRange(1, 7), out.colRange(1, 7));
  325. }
  326. }
  327. INSTANTIATE_TEST_CASE_P(/**/, Reproducibility_MobileNet_SSD, dnnBackendsAndTargets());
  328. typedef testing::TestWithParam<Target> Reproducibility_ResNet50;
  329. TEST_P(Reproducibility_ResNet50, Accuracy)
  330. {
  331. Target targetId = GetParam();
  332. applyTestTag(targetId == DNN_TARGET_CPU ? CV_TEST_TAG_MEMORY_512MB : CV_TEST_TAG_MEMORY_1GB);
  333. ASSERT_TRUE(ocl::useOpenCL() || targetId == DNN_TARGET_CPU || targetId == DNN_TARGET_CPU_FP16);
  334. Net net = readNetFromCaffe(findDataFile("dnn/ResNet-50-deploy.prototxt"),
  335. findDataFile("dnn/ResNet-50-model.caffemodel", false));
  336. net.setPreferableBackend(DNN_BACKEND_OPENCV);
  337. net.setPreferableTarget(targetId);
  338. if (targetId == DNN_TARGET_CPU_FP16)
  339. net.enableWinograd(false);
  340. float l1 = (targetId == DNN_TARGET_OPENCL_FP16 || targetId == DNN_TARGET_CPU_FP16) ? 3e-5 : 1e-5;
  341. float lInf = (targetId == DNN_TARGET_OPENCL_FP16 || targetId == DNN_TARGET_CPU_FP16) ? 6e-3 : 1e-4;
  342. Mat input = blobFromImage(imread(_tf("googlenet_0.png")), 1.0f, Size(224,224), Scalar(), false);
  343. ASSERT_TRUE(!input.empty());
  344. net.setInput(input);
  345. Mat out = net.forward();
  346. Mat ref = blobFromNPY(_tf("resnet50_prob.npy"));
  347. normAssert(ref, out, "", l1, lInf);
  348. if (targetId == DNN_TARGET_OPENCL || targetId == DNN_TARGET_OPENCL_FP16)
  349. {
  350. UMat out_umat;
  351. net.forward(out_umat);
  352. normAssert(ref, out_umat, "out_umat", l1, lInf);
  353. std::vector<UMat> out_umats;
  354. net.forward(out_umats);
  355. normAssert(ref, out_umats[0], "out_umat_vector", l1, lInf);
  356. }
  357. }
  358. INSTANTIATE_TEST_CASE_P(/**/, Reproducibility_ResNet50,
  359. testing::ValuesIn(getAvailableTargets(DNN_BACKEND_OPENCV)));
  360. typedef testing::TestWithParam<Target> Reproducibility_SqueezeNet_v1_1;
  361. TEST_P(Reproducibility_SqueezeNet_v1_1, Accuracy)
  362. {
  363. int targetId = GetParam();
  364. if(targetId == DNN_TARGET_OPENCL_FP16)
  365. applyTestTag(CV_TEST_TAG_DNN_SKIP_OPENCL_FP16);
  366. if(targetId == DNN_TARGET_CPU_FP16)
  367. applyTestTag(CV_TEST_TAG_DNN_SKIP_CPU_FP16);
  368. Net net = readNetFromCaffe(findDataFile("dnn/squeezenet_v1.1.prototxt"),
  369. findDataFile("dnn/squeezenet_v1.1.caffemodel", false));
  370. net.setPreferableBackend(DNN_BACKEND_OPENCV);
  371. net.setPreferableTarget(targetId);
  372. Mat input = blobFromImage(imread(_tf("googlenet_0.png")), 1.0f, Size(227,227), Scalar(), false, true);
  373. ASSERT_TRUE(!input.empty());
  374. Mat out;
  375. if (targetId == DNN_TARGET_OPENCL)
  376. {
  377. // Firstly set a wrong input blob and run the model to receive a wrong output.
  378. // Then set a correct input blob to check CPU->GPU synchronization is working well.
  379. net.setInput(input * 2.0f);
  380. out = net.forward();
  381. }
  382. net.setInput(input);
  383. out = net.forward();
  384. Mat ref = blobFromNPY(_tf("squeezenet_v1.1_prob.npy"));
  385. normAssert(ref, out);
  386. }
  387. INSTANTIATE_TEST_CASE_P(/**/, Reproducibility_SqueezeNet_v1_1,
  388. testing::ValuesIn(getAvailableTargets(DNN_BACKEND_OPENCV)));
  389. TEST(Reproducibility_AlexNet_fp16, Accuracy)
  390. {
  391. applyTestTag(CV_TEST_TAG_MEMORY_512MB);
  392. const float l1 = 1e-5;
  393. const float lInf = 3e-3;
  394. const string proto = findDataFile("dnn/bvlc_alexnet.prototxt");
  395. const string model = findDataFile("dnn/bvlc_alexnet.caffemodel", false);
  396. shrinkCaffeModel(model, "bvlc_alexnet.caffemodel_fp16");
  397. Net net = readNetFromCaffe(proto, "bvlc_alexnet.caffemodel_fp16");
  398. net.setPreferableBackend(DNN_BACKEND_OPENCV);
  399. Mat sample = imread(findDataFile("dnn/grace_hopper_227.png"));
  400. net.setInput(blobFromImage(sample, 1.0f, Size(227, 227), Scalar()));
  401. Mat out = net.forward();
  402. Mat ref = blobFromNPY(findDataFile("dnn/caffe_alexnet_prob.npy"));
  403. normAssert(ref, out, "", l1, lInf);
  404. }
  405. TEST(Reproducibility_GoogLeNet_fp16, Accuracy)
  406. {
  407. const float l1 = 1e-5;
  408. const float lInf = 3e-3;
  409. const string proto = findDataFile("dnn/bvlc_googlenet.prototxt");
  410. const string model = findDataFile("dnn/bvlc_googlenet.caffemodel", false);
  411. shrinkCaffeModel(model, "bvlc_googlenet.caffemodel_fp16");
  412. Net net = readNetFromCaffe(proto, "bvlc_googlenet.caffemodel_fp16");
  413. net.setPreferableBackend(DNN_BACKEND_OPENCV);
  414. std::vector<Mat> inpMats;
  415. inpMats.push_back( imread(_tf("googlenet_0.png")) );
  416. inpMats.push_back( imread(_tf("googlenet_1.png")) );
  417. ASSERT_TRUE(!inpMats[0].empty() && !inpMats[1].empty());
  418. net.setInput(blobFromImages(inpMats, 1.0f, Size(), Scalar(), false), "data");
  419. Mat out = net.forward("prob");
  420. Mat ref = blobFromNPY(_tf("googlenet_prob.npy"));
  421. normAssert(out, ref, "", l1, lInf);
  422. }
  423. // https://github.com/richzhang/colorization
  424. TEST_P(Test_Caffe_nets, Colorization)
  425. {
  426. applyTestTag(
  427. target == DNN_TARGET_CPU ? CV_TEST_TAG_MEMORY_512MB : CV_TEST_TAG_MEMORY_1GB,
  428. CV_TEST_TAG_DEBUG_VERYLONG
  429. );
  430. checkBackend();
  431. Mat inp = blobFromNPY(_tf("colorization_inp.npy"));
  432. Mat ref = blobFromNPY(_tf("colorization_out.npy"));
  433. Mat kernel = blobFromNPY(_tf("colorization_pts_in_hull.npy"));
  434. const string proto = findDataFile("dnn/colorization_deploy_v2.prototxt", false);
  435. const string model = findDataFile("dnn/colorization_release_v2.caffemodel", false);
  436. Net net = readNetFromCaffe(proto, model);
  437. net.setPreferableBackend(backend);
  438. net.setPreferableTarget(target);
  439. // This model has bad accuracy when the FP16 and Winograd are enable at same time.
  440. if (target == DNN_TARGET_CPU_FP16)
  441. net.enableWinograd(false);
  442. net.getLayer(net.getLayerId("class8_ab"))->blobs.push_back(kernel);
  443. net.getLayer(net.getLayerId("conv8_313_rh"))->blobs.push_back(Mat(1, 313, CV_32F, 2.606));
  444. net.setInput(inp);
  445. Mat out = net.forward();
  446. // Reference output values are in range [-29.1, 69.5]
  447. double l1 = 4e-4, lInf = 3e-3;
  448. if (target == DNN_TARGET_OPENCL_FP16 || target == DNN_TARGET_CPU_FP16)
  449. {
  450. l1 = 0.25;
  451. lInf = 5.3;
  452. }
  453. else if (target == DNN_TARGET_MYRIAD)
  454. {
  455. l1 = (getInferenceEngineVPUType() == CV_DNN_INFERENCE_ENGINE_VPU_TYPE_MYRIAD_X) ? 0.5 : 0.25;
  456. lInf = (getInferenceEngineVPUType() == CV_DNN_INFERENCE_ENGINE_VPU_TYPE_MYRIAD_X) ? 11 : 5.3;
  457. }
  458. else if(target == DNN_TARGET_CUDA_FP16)
  459. {
  460. l1 = 0.21;
  461. lInf = 4.5;
  462. }
  463. #if defined(INF_ENGINE_RELEASE)
  464. if (backend == DNN_BACKEND_INFERENCE_ENGINE_NGRAPH && target == DNN_TARGET_OPENCL_FP16)
  465. {
  466. l1 = 0.3; lInf = 10;
  467. }
  468. #endif
  469. normAssert(out, ref, "", l1, lInf);
  470. expectNoFallbacksFromIE(net);
  471. }
  472. TEST_P(Test_Caffe_nets, DenseNet_121)
  473. {
  474. applyTestTag(CV_TEST_TAG_MEMORY_512MB);
  475. checkBackend();
  476. const string proto = findDataFile("dnn/DenseNet_121.prototxt", false);
  477. const string weights = findDataFile("dnn/DenseNet_121.caffemodel", false);
  478. Mat inp = imread(_tf("dog416.png"));
  479. Model model(proto, weights);
  480. model.setInputScale(1.0 / 255).setInputSwapRB(true).setInputCrop(true);
  481. std::vector<Mat> outs;
  482. Mat ref = blobFromNPY(_tf("densenet_121_output.npy"));
  483. model.setPreferableBackend(backend);
  484. model.setPreferableTarget(target);
  485. model.predict(inp, outs);
  486. // Reference is an array of 1000 values from a range [-6.16, 7.9]
  487. float l1 = default_l1, lInf = default_lInf;
  488. if (target == DNN_TARGET_OPENCL_FP16)
  489. {
  490. #if defined(INF_ENGINE_RELEASE) && INF_ENGINE_VER_MAJOR_GE(2019020000)
  491. l1 = 0.05; lInf = 0.3;
  492. #else
  493. l1 = 0.017; lInf = 0.0795;
  494. #endif
  495. }
  496. else if (target == DNN_TARGET_MYRIAD)
  497. {
  498. l1 = 0.11; lInf = 0.5;
  499. }
  500. else if (target == DNN_TARGET_CUDA_FP16)
  501. {
  502. l1 = 0.04; lInf = 0.2;
  503. }
  504. else if (target == DNN_TARGET_CPU_FP16)
  505. {
  506. l1 = 0.06; lInf = 0.3;
  507. }
  508. normAssert(outs[0], ref, "", l1, lInf);
  509. if (target != DNN_TARGET_MYRIAD || getInferenceEngineVPUType() != CV_DNN_INFERENCE_ENGINE_VPU_TYPE_MYRIAD_X)
  510. expectNoFallbacksFromIE(model.getNetwork_());
  511. }
  512. TEST(Test_Caffe, multiple_inputs)
  513. {
  514. const string proto = findDataFile("dnn/layers/net_input.prototxt");
  515. Net net = readNetFromCaffe(proto);
  516. net.setPreferableBackend(DNN_BACKEND_OPENCV);
  517. Mat first_image(10, 11, CV_32FC3);
  518. Mat second_image(10, 11, CV_32FC3);
  519. randu(first_image, -1, 1);
  520. randu(second_image, -1, 1);
  521. first_image = blobFromImage(first_image);
  522. second_image = blobFromImage(second_image);
  523. Mat first_image_blue_green = slice(first_image, Range::all(), Range(0, 2), Range::all(), Range::all());
  524. Mat first_image_red = slice(first_image, Range::all(), Range(2, 3), Range::all(), Range::all());
  525. Mat second_image_blue_green = slice(second_image, Range::all(), Range(0, 2), Range::all(), Range::all());
  526. Mat second_image_red = slice(second_image, Range::all(), Range(2, 3), Range::all(), Range::all());
  527. net.setInput(first_image_blue_green, "old_style_input_blue_green");
  528. net.setInput(first_image_red, "different_name_for_red");
  529. net.setInput(second_image_blue_green, "input_layer_blue_green");
  530. net.setInput(second_image_red, "old_style_input_red");
  531. Mat out = net.forward();
  532. normAssert(out, first_image + second_image);
  533. }
  534. TEST(Test_Caffe, shared_weights)
  535. {
  536. const string proto = findDataFile("dnn/layers/shared_weights.prototxt");
  537. const string model = findDataFile("dnn/layers/shared_weights.caffemodel");
  538. Net net = readNetFromCaffe(proto, model);
  539. Mat input_1 = (Mat_<float>(2, 2) << 0., 2., 4., 6.);
  540. Mat input_2 = (Mat_<float>(2, 2) << 1., 3., 5., 7.);
  541. Mat blob_1 = blobFromImage(input_1);
  542. Mat blob_2 = blobFromImage(input_2);
  543. net.setInput(blob_1, "input_1");
  544. net.setInput(blob_2, "input_2");
  545. net.setPreferableBackend(DNN_BACKEND_OPENCV);
  546. Mat sum = net.forward();
  547. EXPECT_EQ(sum.at<float>(0,0), 12.);
  548. EXPECT_EQ(sum.at<float>(0,1), 16.);
  549. }
  550. typedef testing::TestWithParam<tuple<std::string, Target> > opencv_face_detector;
  551. TEST_P(opencv_face_detector, Accuracy)
  552. {
  553. std::string proto = findDataFile("dnn/opencv_face_detector.prototxt");
  554. std::string model = findDataFile(get<0>(GetParam()), false);
  555. dnn::Target targetId = (dnn::Target)(int)get<1>(GetParam());
  556. if (targetId == DNN_TARGET_OPENCL_FP16)
  557. applyTestTag(CV_TEST_TAG_DNN_SKIP_OPENCL_FP16);
  558. if (targetId == DNN_TARGET_CPU_FP16)
  559. applyTestTag(CV_TEST_TAG_DNN_SKIP_CPU_FP16);
  560. Net net = readNetFromCaffe(proto, model);
  561. Mat img = imread(findDataFile("gpu/lbpcascade/er.png"));
  562. Mat blob = blobFromImage(img, 1.0, Size(), Scalar(104.0, 177.0, 123.0), false, false);
  563. net.setPreferableBackend(DNN_BACKEND_OPENCV);
  564. net.setPreferableTarget(targetId);
  565. net.setInput(blob);
  566. // Output has shape 1x1xNx7 where N - number of detections.
  567. // An every detection is a vector of values [id, classId, confidence, left, top, right, bottom]
  568. Mat out = net.forward();
  569. Mat ref = (Mat_<float>(6, 7) << 0, 1, 0.99520785, 0.80997437, 0.16379407, 0.87996572, 0.26685631,
  570. 0, 1, 0.9934696, 0.2831718, 0.50738752, 0.345781, 0.5985168,
  571. 0, 1, 0.99096733, 0.13629119, 0.24892329, 0.19756334, 0.3310290,
  572. 0, 1, 0.98977017, 0.23901358, 0.09084064, 0.29902688, 0.1769477,
  573. 0, 1, 0.97203469, 0.67965847, 0.06876482, 0.73999709, 0.1513494,
  574. 0, 1, 0.95097077, 0.51901293, 0.45863652, 0.5777427, 0.5347801);
  575. normAssertDetections(ref, out, "", 0.5, 1e-4, 2e-4);
  576. }
  577. // False positives bug for large faces: https://github.com/opencv/opencv/issues/15106
  578. TEST_P(opencv_face_detector, issue_15106)
  579. {
  580. std::string proto = findDataFile("dnn/opencv_face_detector.prototxt");
  581. std::string model = findDataFile(get<0>(GetParam()), false);
  582. dnn::Target targetId = (dnn::Target)(int)get<1>(GetParam());
  583. if (targetId == DNN_TARGET_OPENCL_FP16)
  584. applyTestTag(CV_TEST_TAG_DNN_SKIP_OPENCL_FP16);
  585. if (targetId == DNN_TARGET_CPU_FP16)
  586. applyTestTag(CV_TEST_TAG_DNN_SKIP_CPU_FP16);
  587. Net net = readNetFromCaffe(proto, model);
  588. Mat img = imread(findDataFile("cv/shared/lena.png"));
  589. img = img.rowRange(img.rows / 4, 3 * img.rows / 4).colRange(img.cols / 4, 3 * img.cols / 4);
  590. Mat blob = blobFromImage(img, 1.0, Size(300, 300), Scalar(104.0, 177.0, 123.0), false, false);
  591. net.setPreferableBackend(DNN_BACKEND_OPENCV);
  592. net.setPreferableTarget(targetId);
  593. net.setInput(blob);
  594. // Output has shape 1x1xNx7 where N - number of detections.
  595. // An every detection is a vector of values [id, classId, confidence, left, top, right, bottom]
  596. Mat out = net.forward();
  597. Mat ref = (Mat_<float>(1, 7) << 0, 1, 0.9149431, 0.30424616, 0.26964942, 0.88733053, 0.99815309);
  598. normAssertDetections(ref, out, "", 0.89, 6e-5, 1e-4);
  599. }
  600. INSTANTIATE_TEST_CASE_P(Test_Caffe, opencv_face_detector,
  601. Combine(
  602. Values("dnn/opencv_face_detector.caffemodel",
  603. "dnn/opencv_face_detector_fp16.caffemodel"),
  604. testing::ValuesIn(getAvailableTargets(DNN_BACKEND_OPENCV))
  605. )
  606. );
  607. TEST_P(Test_Caffe_nets, FasterRCNN_vgg16)
  608. {
  609. applyTestTag(
  610. #if defined(OPENCV_32BIT_CONFIGURATION) && defined(HAVE_OPENCL)
  611. CV_TEST_TAG_MEMORY_2GB, // utilizes ~1Gb, but huge blobs may not be allocated on 32-bit systems due memory fragmentation
  612. #else
  613. CV_TEST_TAG_MEMORY_2GB,
  614. #endif
  615. CV_TEST_TAG_LONG,
  616. CV_TEST_TAG_DEBUG_VERYLONG
  617. );
  618. #if defined(INF_ENGINE_RELEASE) && INF_ENGINE_VER_MAJOR_LT(2021040000)
  619. if ((backend == DNN_BACKEND_INFERENCE_ENGINE_NN_BUILDER_2019 || backend == DNN_BACKEND_INFERENCE_ENGINE_NGRAPH) && (target == DNN_TARGET_OPENCL || target == DNN_TARGET_OPENCL_FP16))
  620. applyTestTag(target == DNN_TARGET_OPENCL ? CV_TEST_TAG_DNN_SKIP_IE_OPENCL : CV_TEST_TAG_DNN_SKIP_IE_OPENCL_FP16);
  621. if (backend == DNN_BACKEND_INFERENCE_ENGINE_NGRAPH)
  622. applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_NGRAPH, CV_TEST_TAG_DNN_SKIP_IE_VERSION);
  623. if (backend == DNN_BACKEND_INFERENCE_ENGINE_NN_BUILDER_2019 && target == DNN_TARGET_MYRIAD)
  624. applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_MYRIAD);
  625. #endif
  626. #if defined(INF_ENGINE_RELEASE) && INF_ENGINE_VER_MAJOR_EQ(2021040000)
  627. // IE exception: Ngraph operation Reshape with name rpn_cls_score_reshape has dynamic output shape on 0 port, but CPU plug-in supports only static shape
  628. if (backend == DNN_BACKEND_INFERENCE_ENGINE_NGRAPH && (target == DNN_TARGET_OPENCL || target == DNN_TARGET_OPENCL_FP16))
  629. applyTestTag(target == DNN_TARGET_OPENCL ? CV_TEST_TAG_DNN_SKIP_IE_OPENCL : CV_TEST_TAG_DNN_SKIP_IE_OPENCL_FP16,
  630. CV_TEST_TAG_DNN_SKIP_IE_NGRAPH, CV_TEST_TAG_DNN_SKIP_IE_VERSION
  631. );
  632. // Check 'backward_compatible_check || in_out_elements_equal' failed at core/src/op/reshape.cpp:390:
  633. // While validating node 'v1::Reshape bbox_pred_reshape (bbox_pred[0]:f32{1,84}, Constant_241202[0]:i64{4}) -> (f32{?,?,?,?})' with friendly_name 'bbox_pred_reshape':
  634. // Requested output shape {1,6300,4,1} is incompatible with input shape Shape{1, 84}
  635. if (target == DNN_TARGET_MYRIAD)
  636. applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_MYRIAD, CV_TEST_TAG_DNN_SKIP_IE_NGRAPH, CV_TEST_TAG_DNN_SKIP_IE_VERSION);
  637. #endif
  638. double scoreDiff = 0.0012, iouDiff = 0.03;
  639. #if defined(INF_ENGINE_RELEASE)
  640. if (target == DNN_TARGET_MYRIAD)
  641. applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_MYRIAD, CV_TEST_TAG_DNN_SKIP_IE_NGRAPH, CV_TEST_TAG_DNN_SKIP_IE_VERSION);
  642. if (backend == DNN_BACKEND_INFERENCE_ENGINE_NGRAPH) {
  643. iouDiff = 0.02;
  644. if (target == DNN_TARGET_OPENCL || target == DNN_TARGET_OPENCL_FP16) {
  645. scoreDiff = 0.04;
  646. iouDiff = 0.06;
  647. }
  648. }
  649. #endif
  650. static Mat ref = (Mat_<float>(3, 7) << 0, 2, 0.949398, 99.2454, 210.141, 601.205, 462.849,
  651. 0, 7, 0.997022, 481.841, 92.3218, 722.685, 175.953,
  652. 0, 12, 0.993028, 133.221, 189.377, 350.994, 563.166);
  653. testFaster("faster_rcnn_vgg16.prototxt", "VGG16_faster_rcnn_final.caffemodel", ref, scoreDiff, iouDiff);
  654. }
  655. TEST_P(Test_Caffe_nets, FasterRCNN_zf)
  656. {
  657. applyTestTag(
  658. #if defined(OPENCV_32BIT_CONFIGURATION) && defined(HAVE_OPENCL)
  659. CV_TEST_TAG_MEMORY_2GB,
  660. #else
  661. (target == DNN_TARGET_CPU ? CV_TEST_TAG_MEMORY_512MB : CV_TEST_TAG_MEMORY_1GB),
  662. #endif
  663. CV_TEST_TAG_DEBUG_VERYLONG
  664. );
  665. #if defined(INF_ENGINE_RELEASE) && INF_ENGINE_VER_MAJOR_EQ(2021040000)
  666. // IE exception: Ngraph operation Reshape with name rpn_cls_score_reshape has dynamic output shape on 0 port, but CPU plug-in supports only static shape
  667. if (backend == DNN_BACKEND_INFERENCE_ENGINE_NGRAPH && (target == DNN_TARGET_OPENCL || target == DNN_TARGET_OPENCL_FP16))
  668. applyTestTag(target == DNN_TARGET_OPENCL ? CV_TEST_TAG_DNN_SKIP_IE_OPENCL : CV_TEST_TAG_DNN_SKIP_IE_OPENCL_FP16,
  669. CV_TEST_TAG_DNN_SKIP_IE_NGRAPH, CV_TEST_TAG_DNN_SKIP_IE_VERSION
  670. );
  671. #endif
  672. if ((backend == DNN_BACKEND_INFERENCE_ENGINE_NN_BUILDER_2019 ||
  673. backend == DNN_BACKEND_INFERENCE_ENGINE_NGRAPH) && target == DNN_TARGET_MYRIAD)
  674. applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_MYRIAD);
  675. if (target == DNN_TARGET_CUDA_FP16)
  676. applyTestTag(CV_TEST_TAG_DNN_SKIP_CUDA_FP16);
  677. if (target == DNN_TARGET_CPU_FP16)
  678. applyTestTag(CV_TEST_TAG_DNN_SKIP_CPU_FP16);
  679. static Mat ref = (Mat_<float>(3, 7) << 0, 2, 0.90121, 120.407, 115.83, 570.586, 528.395,
  680. 0, 7, 0.988779, 469.849, 75.1756, 718.64, 186.762,
  681. 0, 12, 0.967198, 138.588, 206.843, 329.766, 553.176);
  682. double scoreDiff = 0.003, iouDiff = 0.07;
  683. if (backend == DNN_BACKEND_INFERENCE_ENGINE_NGRAPH) {
  684. scoreDiff = 0.02;
  685. iouDiff = 0.13;
  686. }
  687. testFaster("faster_rcnn_zf.prototxt", "ZF_faster_rcnn_final.caffemodel", ref, scoreDiff, iouDiff);
  688. }
  689. TEST_P(Test_Caffe_nets, RFCN)
  690. {
  691. applyTestTag(
  692. (target == DNN_TARGET_CPU ? CV_TEST_TAG_MEMORY_512MB : CV_TEST_TAG_MEMORY_2GB),
  693. CV_TEST_TAG_LONG,
  694. CV_TEST_TAG_DEBUG_VERYLONG
  695. );
  696. float scoreDiff = default_l1, iouDiff = default_lInf;
  697. if (backend == DNN_BACKEND_OPENCV && (target == DNN_TARGET_OPENCL_FP16 || target == DNN_TARGET_CPU_FP16))
  698. {
  699. scoreDiff = 4e-3;
  700. iouDiff = 8e-2;
  701. }
  702. if (target == DNN_TARGET_CUDA_FP16)
  703. {
  704. scoreDiff = 0.0034;
  705. iouDiff = 0.12;
  706. }
  707. #if defined(INF_ENGINE_RELEASE)
  708. if (backend == DNN_BACKEND_INFERENCE_ENGINE_NGRAPH)
  709. {
  710. scoreDiff = 0.1f;
  711. iouDiff = 0.2f;
  712. }
  713. // Check 'backward_compatible_check || in_out_elements_equal' failed at core/src/op/reshape.cpp:427:
  714. // While validating node 'v1::Reshape bbox_pred_reshape (ave_bbox_pred_rois[0]:f32{1,8,1,1}, Constant_388[0]:i64{4}) -> (f32{?,?,?,?})' with friendly_name 'bbox_pred_reshape':
  715. // Requested output shape {1,300,8,1} is incompatible with input shape {1, 8, 1, 1}
  716. if (backend == DNN_BACKEND_INFERENCE_ENGINE_NGRAPH && target == DNN_TARGET_MYRIAD)
  717. applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_MYRIAD, CV_TEST_TAG_DNN_SKIP_IE_NGRAPH, CV_TEST_TAG_DNN_SKIP_IE_VERSION);
  718. #elif defined(INF_ENGINE_RELEASE) && INF_ENGINE_VER_MAJOR_EQ(2021040000)
  719. // Exception: Function contains several inputs and outputs with one friendly name! (HETERO bug?)
  720. if (backend == DNN_BACKEND_INFERENCE_ENGINE_NGRAPH && target != DNN_TARGET_CPU)
  721. applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_NGRAPH, CV_TEST_TAG_DNN_SKIP_IE_VERSION);
  722. #elif defined(INF_ENGINE_RELEASE)
  723. if ((backend == DNN_BACKEND_INFERENCE_ENGINE_NN_BUILDER_2019 ||
  724. backend == DNN_BACKEND_INFERENCE_ENGINE_NGRAPH) && target == DNN_TARGET_OPENCL_FP16)
  725. applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_OPENCL_FP16);
  726. if ((backend == DNN_BACKEND_INFERENCE_ENGINE_NN_BUILDER_2019 ||
  727. backend == DNN_BACKEND_INFERENCE_ENGINE_NGRAPH) && target == DNN_TARGET_MYRIAD)
  728. applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_MYRIAD);
  729. #endif
  730. static Mat ref = (Mat_<float>(2, 7) << 0, 7, 0.991359, 491.822, 81.1668, 702.573, 178.234,
  731. 0, 12, 0.94786, 132.093, 223.903, 338.077, 566.16);
  732. testFaster("rfcn_pascal_voc_resnet50.prototxt", "resnet50_rfcn_final.caffemodel", ref, scoreDiff, iouDiff);
  733. }
  734. INSTANTIATE_TEST_CASE_P(/**/, Test_Caffe_nets, dnnBackendsAndTargets());
  735. }} // namespace