perf_net.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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. //
  5. // Copyright (C) 2017, Intel Corporation, all rights reserved.
  6. // Third party copyrights are property of their respective owners.
  7. #include "perf_precomp.hpp"
  8. #include "opencv2/core/ocl.hpp"
  9. #include "opencv2/dnn/shape_utils.hpp"
  10. #include "../test/test_common.hpp"
  11. namespace opencv_test {
  12. class DNNTestNetwork : public ::perf::TestBaseWithParam< tuple<Backend, Target> >
  13. {
  14. public:
  15. dnn::Backend backend;
  16. dnn::Target target;
  17. dnn::Net net;
  18. DNNTestNetwork()
  19. {
  20. backend = (dnn::Backend)(int)get<0>(GetParam());
  21. target = (dnn::Target)(int)get<1>(GetParam());
  22. }
  23. void processNet(std::string weights, std::string proto, std::string halide_scheduler,
  24. const Mat& input, const std::string& outputLayer = "")
  25. {
  26. randu(input, 0.0f, 1.0f);
  27. weights = findDataFile(weights, false);
  28. if (!proto.empty())
  29. proto = findDataFile(proto);
  30. if (backend == DNN_BACKEND_HALIDE)
  31. {
  32. if (halide_scheduler == "disabled")
  33. throw cvtest::SkipTestException("Halide test is disabled");
  34. if (!halide_scheduler.empty())
  35. halide_scheduler = findDataFile(std::string("dnn/halide_scheduler_") + (target == DNN_TARGET_OPENCL ? "opencl_" : "") + halide_scheduler, true);
  36. }
  37. net = readNet(proto, weights);
  38. net.setInput(blobFromImage(input, 1.0, Size(), Scalar(), false));
  39. net.setPreferableBackend(backend);
  40. net.setPreferableTarget(target);
  41. if (backend == DNN_BACKEND_HALIDE)
  42. {
  43. net.setHalideScheduler(halide_scheduler);
  44. }
  45. MatShape netInputShape = shape(1, 3, input.rows, input.cols);
  46. size_t weightsMemory = 0, blobsMemory = 0;
  47. net.getMemoryConsumption(netInputShape, weightsMemory, blobsMemory);
  48. int64 flops = net.getFLOPS(netInputShape);
  49. CV_Assert(flops > 0);
  50. net.forward(outputLayer); // warmup
  51. std::cout << "Memory consumption:" << std::endl;
  52. std::cout << " Weights(parameters): " << divUp(weightsMemory, 1u<<20) << " Mb" << std::endl;
  53. std::cout << " Blobs: " << divUp(blobsMemory, 1u<<20) << " Mb" << std::endl;
  54. std::cout << "Calculation complexity: " << flops * 1e-9 << " GFlops" << std::endl;
  55. PERF_SAMPLE_BEGIN()
  56. net.forward();
  57. PERF_SAMPLE_END()
  58. SANITY_CHECK_NOTHING();
  59. }
  60. };
  61. PERF_TEST_P_(DNNTestNetwork, AlexNet)
  62. {
  63. processNet("dnn/bvlc_alexnet.caffemodel", "dnn/bvlc_alexnet.prototxt",
  64. "alexnet.yml", Mat(cv::Size(227, 227), CV_32FC3));
  65. }
  66. PERF_TEST_P_(DNNTestNetwork, GoogLeNet)
  67. {
  68. processNet("dnn/bvlc_googlenet.caffemodel", "dnn/bvlc_googlenet.prototxt",
  69. "", Mat(cv::Size(224, 224), CV_32FC3));
  70. }
  71. PERF_TEST_P_(DNNTestNetwork, ResNet_50)
  72. {
  73. processNet("dnn/ResNet-50-model.caffemodel", "dnn/ResNet-50-deploy.prototxt",
  74. "resnet_50.yml", Mat(cv::Size(224, 224), CV_32FC3));
  75. }
  76. PERF_TEST_P_(DNNTestNetwork, SqueezeNet_v1_1)
  77. {
  78. processNet("dnn/squeezenet_v1.1.caffemodel", "dnn/squeezenet_v1.1.prototxt",
  79. "squeezenet_v1_1.yml", Mat(cv::Size(227, 227), CV_32FC3));
  80. }
  81. PERF_TEST_P_(DNNTestNetwork, Inception_5h)
  82. {
  83. if (backend == DNN_BACKEND_INFERENCE_ENGINE_NN_BUILDER_2019) throw SkipTestException("");
  84. processNet("dnn/tensorflow_inception_graph.pb", "",
  85. "inception_5h.yml",
  86. Mat(cv::Size(224, 224), CV_32FC3), "softmax2");
  87. }
  88. PERF_TEST_P_(DNNTestNetwork, ENet)
  89. {
  90. if ((backend == DNN_BACKEND_INFERENCE_ENGINE_NN_BUILDER_2019 && target != DNN_TARGET_CPU) ||
  91. (backend == DNN_BACKEND_OPENCV && target == DNN_TARGET_OPENCL_FP16))
  92. throw SkipTestException("");
  93. #if defined(INF_ENGINE_RELEASE) && INF_ENGINE_VER_MAJOR_GE(2021010000)
  94. if (backend == DNN_BACKEND_INFERENCE_ENGINE_NGRAPH)
  95. throw SkipTestException("");
  96. #endif
  97. processNet("dnn/Enet-model-best.net", "", "enet.yml",
  98. Mat(cv::Size(512, 256), CV_32FC3));
  99. }
  100. PERF_TEST_P_(DNNTestNetwork, SSD)
  101. {
  102. processNet("dnn/VGG_ILSVRC2016_SSD_300x300_iter_440000.caffemodel", "dnn/ssd_vgg16.prototxt", "disabled",
  103. Mat(cv::Size(300, 300), CV_32FC3));
  104. }
  105. PERF_TEST_P_(DNNTestNetwork, OpenFace)
  106. {
  107. if (backend == DNN_BACKEND_HALIDE)
  108. throw SkipTestException("");
  109. #if defined(INF_ENGINE_RELEASE) && INF_ENGINE_VER_MAJOR_EQ(2018050000)
  110. if (backend == DNN_BACKEND_INFERENCE_ENGINE_NN_BUILDER_2019 && (target == DNN_TARGET_MYRIAD || target == DNN_TARGET_HDDL))
  111. throw SkipTestException("");
  112. #endif
  113. processNet("dnn/openface_nn4.small2.v1.t7", "", "",
  114. Mat(cv::Size(96, 96), CV_32FC3));
  115. }
  116. PERF_TEST_P_(DNNTestNetwork, MobileNet_SSD_Caffe)
  117. {
  118. if (backend == DNN_BACKEND_HALIDE)
  119. throw SkipTestException("");
  120. processNet("dnn/MobileNetSSD_deploy.caffemodel", "dnn/MobileNetSSD_deploy.prototxt", "",
  121. Mat(cv::Size(300, 300), CV_32FC3));
  122. }
  123. PERF_TEST_P_(DNNTestNetwork, MobileNet_SSD_v1_TensorFlow)
  124. {
  125. if (backend == DNN_BACKEND_HALIDE)
  126. throw SkipTestException("");
  127. processNet("dnn/ssd_mobilenet_v1_coco_2017_11_17.pb", "ssd_mobilenet_v1_coco_2017_11_17.pbtxt", "",
  128. Mat(cv::Size(300, 300), CV_32FC3));
  129. }
  130. PERF_TEST_P_(DNNTestNetwork, MobileNet_SSD_v2_TensorFlow)
  131. {
  132. if (backend == DNN_BACKEND_HALIDE)
  133. throw SkipTestException("");
  134. processNet("dnn/ssd_mobilenet_v2_coco_2018_03_29.pb", "ssd_mobilenet_v2_coco_2018_03_29.pbtxt", "",
  135. Mat(cv::Size(300, 300), CV_32FC3));
  136. }
  137. PERF_TEST_P_(DNNTestNetwork, DenseNet_121)
  138. {
  139. if (backend == DNN_BACKEND_HALIDE)
  140. throw SkipTestException("");
  141. processNet("dnn/DenseNet_121.caffemodel", "dnn/DenseNet_121.prototxt", "",
  142. Mat(cv::Size(224, 224), CV_32FC3));
  143. }
  144. PERF_TEST_P_(DNNTestNetwork, OpenPose_pose_mpi_faster_4_stages)
  145. {
  146. if (backend == DNN_BACKEND_HALIDE ||
  147. (backend == DNN_BACKEND_INFERENCE_ENGINE_NN_BUILDER_2019 && (target == DNN_TARGET_MYRIAD || target == DNN_TARGET_HDDL)))
  148. throw SkipTestException("");
  149. // The same .caffemodel but modified .prototxt
  150. // See https://github.com/CMU-Perceptual-Computing-Lab/openpose/blob/master/src/openpose/pose/poseParameters.cpp
  151. processNet("dnn/openpose_pose_mpi.caffemodel", "dnn/openpose_pose_mpi_faster_4_stages.prototxt", "",
  152. Mat(cv::Size(368, 368), CV_32FC3));
  153. }
  154. PERF_TEST_P_(DNNTestNetwork, opencv_face_detector)
  155. {
  156. if (backend == DNN_BACKEND_HALIDE)
  157. throw SkipTestException("");
  158. processNet("dnn/opencv_face_detector.caffemodel", "dnn/opencv_face_detector.prototxt", "",
  159. Mat(cv::Size(300, 300), CV_32FC3));
  160. }
  161. PERF_TEST_P_(DNNTestNetwork, Inception_v2_SSD_TensorFlow)
  162. {
  163. if (backend == DNN_BACKEND_HALIDE)
  164. throw SkipTestException("");
  165. processNet("dnn/ssd_inception_v2_coco_2017_11_17.pb", "ssd_inception_v2_coco_2017_11_17.pbtxt", "",
  166. Mat(cv::Size(300, 300), CV_32FC3));
  167. }
  168. PERF_TEST_P_(DNNTestNetwork, YOLOv3)
  169. {
  170. if (backend == DNN_BACKEND_HALIDE)
  171. throw SkipTestException("");
  172. #if defined(INF_ENGINE_RELEASE) && INF_ENGINE_VER_MAJOR_EQ(2020040000) // nGraph compilation failure
  173. if (backend == DNN_BACKEND_INFERENCE_ENGINE_NGRAPH && target == DNN_TARGET_OPENCL)
  174. throw SkipTestException("Test is disabled in OpenVINO 2020.4");
  175. if (backend == DNN_BACKEND_INFERENCE_ENGINE_NGRAPH && target == DNN_TARGET_OPENCL_FP16)
  176. throw SkipTestException("Test is disabled in OpenVINO 2020.4");
  177. #endif
  178. #if defined(INF_ENGINE_RELEASE) && INF_ENGINE_VER_MAJOR_GE(2021010000) // nGraph compilation failure
  179. if (target == DNN_TARGET_MYRIAD)
  180. throw SkipTestException("");
  181. #endif
  182. Mat sample = imread(findDataFile("dnn/dog416.png"));
  183. cvtColor(sample, sample, COLOR_BGR2RGB);
  184. Mat inp;
  185. sample.convertTo(inp, CV_32FC3, 1.0f / 255, 0);
  186. processNet("dnn/yolov3.weights", "dnn/yolov3.cfg", "", inp);
  187. }
  188. PERF_TEST_P_(DNNTestNetwork, YOLOv4)
  189. {
  190. if (backend == DNN_BACKEND_HALIDE)
  191. throw SkipTestException("");
  192. if (target == DNN_TARGET_MYRIAD) // not enough resources
  193. throw SkipTestException("");
  194. #if defined(INF_ENGINE_RELEASE) && INF_ENGINE_VER_MAJOR_EQ(2020040000) // nGraph compilation failure
  195. if (backend == DNN_BACKEND_INFERENCE_ENGINE_NGRAPH && target == DNN_TARGET_OPENCL)
  196. throw SkipTestException("Test is disabled in OpenVINO 2020.4");
  197. if (backend == DNN_BACKEND_INFERENCE_ENGINE_NGRAPH && target == DNN_TARGET_OPENCL_FP16)
  198. throw SkipTestException("Test is disabled in OpenVINO 2020.4");
  199. #endif
  200. Mat sample = imread(findDataFile("dnn/dog416.png"));
  201. cvtColor(sample, sample, COLOR_BGR2RGB);
  202. Mat inp;
  203. sample.convertTo(inp, CV_32FC3, 1.0f / 255, 0);
  204. processNet("dnn/yolov4.weights", "dnn/yolov4.cfg", "", inp);
  205. }
  206. PERF_TEST_P_(DNNTestNetwork, YOLOv4_tiny)
  207. {
  208. if (backend == DNN_BACKEND_HALIDE)
  209. throw SkipTestException("");
  210. #if defined(INF_ENGINE_RELEASE) && INF_ENGINE_VER_MAJOR_GE(2021010000) // nGraph compilation failure
  211. if (target == DNN_TARGET_MYRIAD)
  212. throw SkipTestException("");
  213. #endif
  214. Mat sample = imread(findDataFile("dnn/dog416.png"));
  215. cvtColor(sample, sample, COLOR_BGR2RGB);
  216. Mat inp;
  217. sample.convertTo(inp, CV_32FC3, 1.0f / 255, 0);
  218. processNet("dnn/yolov4-tiny.weights", "dnn/yolov4-tiny.cfg", "", inp);
  219. }
  220. PERF_TEST_P_(DNNTestNetwork, EAST_text_detection)
  221. {
  222. if (backend == DNN_BACKEND_HALIDE)
  223. throw SkipTestException("");
  224. processNet("dnn/frozen_east_text_detection.pb", "", "", Mat(cv::Size(320, 320), CV_32FC3));
  225. }
  226. PERF_TEST_P_(DNNTestNetwork, FastNeuralStyle_eccv16)
  227. {
  228. if (backend == DNN_BACKEND_HALIDE)
  229. throw SkipTestException("");
  230. processNet("dnn/fast_neural_style_eccv16_starry_night.t7", "", "", Mat(cv::Size(320, 240), CV_32FC3));
  231. }
  232. PERF_TEST_P_(DNNTestNetwork, Inception_v2_Faster_RCNN)
  233. {
  234. #if defined(INF_ENGINE_RELEASE) && INF_ENGINE_VER_MAJOR_EQ(2019010000)
  235. if (backend == DNN_BACKEND_INFERENCE_ENGINE_NN_BUILDER_2019)
  236. throw SkipTestException("Test is disabled in OpenVINO 2019R1");
  237. #endif
  238. #if defined(INF_ENGINE_RELEASE) && INF_ENGINE_VER_MAJOR_EQ(2019020000)
  239. if (backend == DNN_BACKEND_INFERENCE_ENGINE_NN_BUILDER_2019)
  240. throw SkipTestException("Test is disabled in OpenVINO 2019R2");
  241. #endif
  242. #if defined(INF_ENGINE_RELEASE) && INF_ENGINE_VER_MAJOR_GE(2021010000)
  243. if (target == DNN_TARGET_MYRIAD)
  244. throw SkipTestException("Test is disabled in OpenVINO 2021.1+ / MYRIAD");
  245. #endif
  246. if (backend == DNN_BACKEND_HALIDE ||
  247. (backend == DNN_BACKEND_INFERENCE_ENGINE_NN_BUILDER_2019 && target != DNN_TARGET_CPU) ||
  248. (backend == DNN_BACKEND_OPENCV && target == DNN_TARGET_OPENCL_FP16))
  249. throw SkipTestException("");
  250. processNet("dnn/faster_rcnn_inception_v2_coco_2018_01_28.pb",
  251. "dnn/faster_rcnn_inception_v2_coco_2018_01_28.pbtxt", "",
  252. Mat(cv::Size(800, 600), CV_32FC3));
  253. }
  254. PERF_TEST_P_(DNNTestNetwork, EfficientDet)
  255. {
  256. if (backend == DNN_BACKEND_HALIDE || target != DNN_TARGET_CPU)
  257. throw SkipTestException("");
  258. Mat sample = imread(findDataFile("dnn/dog416.png"));
  259. resize(sample, sample, Size(512, 512));
  260. Mat inp;
  261. sample.convertTo(inp, CV_32FC3, 1.0/255);
  262. processNet("dnn/efficientdet-d0.pb", "dnn/efficientdet-d0.pbtxt", "", inp);
  263. }
  264. INSTANTIATE_TEST_CASE_P(/*nothing*/, DNNTestNetwork, dnnBackendsAndTargets());
  265. } // namespace