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