Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Tetragramm
GitHub Repository: Tetragramm/opencv
Path: blob/master/modules/dnn/perf/perf_caffe.cpp
16354 views
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 opencv_test {
38
39
static caffe::Net<float>* initNet(std::string proto, std::string weights)
40
{
41
proto = findDataFile(proto, false);
42
weights = findDataFile(weights, false);
43
44
#ifdef HAVE_CLCAFFE
45
caffe::Caffe::set_mode(caffe::Caffe::GPU);
46
caffe::Caffe::SetDevice(0);
47
48
caffe::Net<float>* net =
49
new caffe::Net<float>(proto, caffe::TEST, caffe::Caffe::GetDefaultDevice());
50
#else
51
caffe::Caffe::set_mode(caffe::Caffe::CPU);
52
53
caffe::Net<float>* net = new caffe::Net<float>(proto, caffe::TEST);
54
#endif
55
56
net->CopyTrainedLayersFrom(weights);
57
58
caffe::Blob<float>* input = net->input_blobs()[0];
59
60
CV_Assert(input->num() == 1);
61
CV_Assert(input->channels() == 3);
62
63
Mat inputMat(input->height(), input->width(), CV_32FC3, (char*)input->cpu_data());
64
randu(inputMat, 0.0f, 1.0f);
65
66
net->Forward();
67
return net;
68
}
69
70
PERF_TEST(AlexNet_caffe, CaffePerfTest)
71
{
72
caffe::Net<float>* net = initNet("dnn/bvlc_alexnet.prototxt",
73
"dnn/bvlc_alexnet.caffemodel");
74
TEST_CYCLE() net->Forward();
75
SANITY_CHECK_NOTHING();
76
}
77
78
PERF_TEST(GoogLeNet_caffe, CaffePerfTest)
79
{
80
caffe::Net<float>* net = initNet("dnn/bvlc_googlenet.prototxt",
81
"dnn/bvlc_googlenet.caffemodel");
82
TEST_CYCLE() net->Forward();
83
SANITY_CHECK_NOTHING();
84
}
85
86
PERF_TEST(ResNet50_caffe, CaffePerfTest)
87
{
88
caffe::Net<float>* net = initNet("dnn/ResNet-50-deploy.prototxt",
89
"dnn/ResNet-50-model.caffemodel");
90
TEST_CYCLE() net->Forward();
91
SANITY_CHECK_NOTHING();
92
}
93
94
PERF_TEST(SqueezeNet_v1_1_caffe, CaffePerfTest)
95
{
96
caffe::Net<float>* net = initNet("dnn/squeezenet_v1.1.prototxt",
97
"dnn/squeezenet_v1.1.caffemodel");
98
TEST_CYCLE() net->Forward();
99
SANITY_CHECK_NOTHING();
100
}
101
102
PERF_TEST(MobileNet_SSD, CaffePerfTest)
103
{
104
caffe::Net<float>* net = initNet("dnn/MobileNetSSD_deploy.prototxt",
105
"dnn/MobileNetSSD_deploy.caffemodel");
106
TEST_CYCLE() net->Forward();
107
SANITY_CHECK_NOTHING();
108
}
109
110
} // namespace
111
#endif // HAVE_CAFFE
112
113