perf_caffe.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. // Recommends run this performance test via
  8. // ./bin/opencv_perf_dnn 2> /dev/null | grep "PERFSTAT" -A 3
  9. // because whole output includes Caffe's logs.
  10. //
  11. // Note: Be sure that interesting version of Caffe was linked.
  12. // Note: There is an impact on Halide performance. Comment this tests if you
  13. // want to run the last one.
  14. //
  15. // How to build Intel-Caffe with MKLDNN backend
  16. // ============================================
  17. // mkdir build && cd build
  18. // cmake -DCMAKE_BUILD_TYPE=Release \
  19. // -DUSE_MKLDNN_AS_DEFAULT_ENGINE=ON \
  20. // -DUSE_MKL2017_AS_DEFAULT_ENGINE=OFF \
  21. // -DCPU_ONLY=ON \
  22. // -DCMAKE_INSTALL_PREFIX=/usr/local .. && make -j8
  23. // sudo make install
  24. //
  25. // In case of problems with cublas_v2.h at include/caffe/util/device_alternate.hpp: add line
  26. // #define CPU_ONLY
  27. // before the first line
  28. // #ifdef CPU_ONLY // CPU-only Caffe.
  29. #if defined(HAVE_CAFFE) || defined(HAVE_CLCAFFE)
  30. #include "perf_precomp.hpp"
  31. #include <iostream>
  32. #include <caffe/caffe.hpp>
  33. namespace opencv_test {
  34. static caffe::Net<float>* initNet(std::string proto, std::string weights)
  35. {
  36. proto = findDataFile(proto);
  37. weights = findDataFile(weights, false);
  38. #ifdef HAVE_CLCAFFE
  39. caffe::Caffe::set_mode(caffe::Caffe::GPU);
  40. caffe::Caffe::SetDevice(0);
  41. caffe::Net<float>* net =
  42. new caffe::Net<float>(proto, caffe::TEST, caffe::Caffe::GetDefaultDevice());
  43. #else
  44. caffe::Caffe::set_mode(caffe::Caffe::CPU);
  45. caffe::Net<float>* net = new caffe::Net<float>(proto, caffe::TEST);
  46. #endif
  47. net->CopyTrainedLayersFrom(weights);
  48. caffe::Blob<float>* input = net->input_blobs()[0];
  49. CV_Assert(input->num() == 1);
  50. CV_Assert(input->channels() == 3);
  51. Mat inputMat(input->height(), input->width(), CV_32FC3, (char*)input->cpu_data());
  52. randu(inputMat, 0.0f, 1.0f);
  53. net->Forward();
  54. return net;
  55. }
  56. PERF_TEST(AlexNet_caffe, CaffePerfTest)
  57. {
  58. caffe::Net<float>* net = initNet("dnn/bvlc_alexnet.prototxt",
  59. "dnn/bvlc_alexnet.caffemodel");
  60. TEST_CYCLE() net->Forward();
  61. SANITY_CHECK_NOTHING();
  62. }
  63. PERF_TEST(GoogLeNet_caffe, CaffePerfTest)
  64. {
  65. caffe::Net<float>* net = initNet("dnn/bvlc_googlenet.prototxt",
  66. "dnn/bvlc_googlenet.caffemodel");
  67. TEST_CYCLE() net->Forward();
  68. SANITY_CHECK_NOTHING();
  69. }
  70. PERF_TEST(ResNet50_caffe, CaffePerfTest)
  71. {
  72. caffe::Net<float>* net = initNet("dnn/ResNet-50-deploy.prototxt",
  73. "dnn/ResNet-50-model.caffemodel");
  74. TEST_CYCLE() net->Forward();
  75. SANITY_CHECK_NOTHING();
  76. }
  77. PERF_TEST(SqueezeNet_v1_1_caffe, CaffePerfTest)
  78. {
  79. caffe::Net<float>* net = initNet("dnn/squeezenet_v1.1.prototxt",
  80. "dnn/squeezenet_v1.1.caffemodel");
  81. TEST_CYCLE() net->Forward();
  82. SANITY_CHECK_NOTHING();
  83. }
  84. PERF_TEST(MobileNet_SSD, CaffePerfTest)
  85. {
  86. caffe::Net<float>* net = initNet("dnn/MobileNetSSD_deploy.prototxt",
  87. "dnn/MobileNetSSD_deploy.caffemodel");
  88. TEST_CYCLE() net->Forward();
  89. SANITY_CHECK_NOTHING();
  90. }
  91. } // namespace
  92. #endif // HAVE_CAFFE