Skip to content

Commit 2246759

Browse files
committed
Merge pull request opencv#9692 from alalek:dnn_perf_net
2 parents 68cc2e2 + 78788e1 commit 2246759

File tree

10 files changed

+243
-251
lines changed

10 files changed

+243
-251
lines changed

modules/dnn/perf/perf_convolution.cpp

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,15 @@
11
#include "perf_precomp.hpp"
22
#include <opencv2/dnn/shape_utils.hpp>
33

4-
namespace cvtest
4+
namespace
55
{
66

7-
using std::tr1::tuple;
8-
using std::tr1::get;
9-
using std::tr1::make_tuple;
10-
using std::make_pair;
11-
using namespace perf;
12-
using namespace testing;
13-
using namespace cv;
14-
using namespace cv::dnn;
15-
167
enum {STRIDE_OFF = 1, STRIDE_ON = 2};
178
CV_ENUM(StrideSize, STRIDE_OFF, STRIDE_ON);
189

1910
enum {GROUP_OFF = 1, GROUP_2 = 2};
2011
CV_ENUM(GroupSize, GROUP_OFF, GROUP_2);
2112

22-
//Squared Size
23-
#define SSZ(n) cv::Size(n, n)
24-
2513
typedef std::pair<MatShape, int> InpShapeNumOut;
2614
typedef tuple<Size, InpShapeNumOut, GroupSize, StrideSize> ConvParam; //kernel_size, inp shape, groups, stride
2715
typedef TestBaseWithParam<ConvParam> ConvolutionPerfTest;
@@ -77,11 +65,11 @@ PERF_TEST_P( ConvolutionPerfTest, perf, Combine(
7765
Ptr<Layer> layer = cv::dnn::LayerFactory::createLayerInstance("Convolution", lp);
7866
std::vector<MatShape> inputShapes(1, shape(inpBlob)), outShapes, internals;
7967
layer->getMemoryShapes(inputShapes, 0, outShapes, internals);
80-
for (int i = 0; i < outShapes.size(); i++)
68+
for (size_t i = 0; i < outShapes.size(); i++)
8169
{
8270
outBlobs.push_back(Mat(outShapes[i], CV_32F));
8371
}
84-
for (int i = 0; i < internals.size(); i++)
72+
for (size_t i = 0; i < internals.size(); i++)
8573
{
8674
internalBlobs.push_back(Mat());
8775
if (total(internals[i]))
@@ -95,12 +83,13 @@ PERF_TEST_P( ConvolutionPerfTest, perf, Combine(
9583
Mat outBlob2D = outBlobs[0].reshape(1, outBlobs[0].size[0]);
9684
declare.in(inpBlob2D, wgtBlob2D, WARMUP_RNG).out(outBlob2D).tbb_threads(cv::getNumThreads());
9785

98-
TEST_CYCLE_N(10)
99-
{
86+
layer->forward(inpBlobs, outBlobs, internalBlobs); /// warmup
87+
88+
PERF_SAMPLE_BEGIN()
10089
layer->forward(inpBlobs, outBlobs, internalBlobs);
101-
}
90+
PERF_SAMPLE_END()
10291

10392
SANITY_CHECK_NOTHING();
10493
}
10594

106-
}
95+
} // namespace

modules/dnn/perf/perf_halide_net.cpp

Lines changed: 0 additions & 174 deletions
This file was deleted.

modules/dnn/perf/perf_net.cpp

Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
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+
#include "perf_precomp.hpp"
9+
#include "opencv2/core/ocl.hpp"
10+
11+
#include "opencv2/dnn/shape_utils.hpp"
12+
13+
namespace
14+
{
15+
16+
#ifdef HAVE_HALIDE
17+
#define TEST_DNN_BACKEND DNN_BACKEND_DEFAULT, DNN_BACKEND_HALIDE
18+
#else
19+
#define TEST_DNN_BACKEND DNN_BACKEND_DEFAULT
20+
#endif
21+
#define TEST_DNN_TARGET DNN_TARGET_CPU, DNN_TARGET_OPENCL
22+
23+
CV_ENUM(DNNBackend, DNN_BACKEND_DEFAULT, DNN_BACKEND_HALIDE)
24+
CV_ENUM(DNNTarget, DNN_TARGET_CPU, DNN_TARGET_OPENCL)
25+
26+
class DNNTestNetwork : public ::perf::TestBaseWithParam< tuple<DNNBackend, DNNTarget> >
27+
{
28+
public:
29+
dnn::Backend backend;
30+
dnn::Target target;
31+
32+
dnn::Net net;
33+
34+
void processNet(std::string weights, std::string proto, std::string halide_scheduler,
35+
int inWidth, int inHeight, const std::string& outputLayer,
36+
const std::string& framework)
37+
{
38+
backend = (dnn::Backend)(int)get<0>(GetParam());
39+
target = (dnn::Target)(int)get<1>(GetParam());
40+
41+
if (backend == DNN_BACKEND_DEFAULT && target == DNN_TARGET_OPENCL)
42+
{
43+
#if 0 //defined(HAVE_OPENCL)
44+
if (!cv::ocl::useOpenCL())
45+
#endif
46+
{
47+
throw ::SkipTestException("OpenCL is not available/disabled in OpenCV");
48+
}
49+
}
50+
51+
Mat input(inHeight, inWidth, CV_32FC3);
52+
randu(input, 0.0f, 1.0f);
53+
54+
55+
weights = findDataFile(weights, false);
56+
if (!proto.empty())
57+
proto = findDataFile(proto, false);
58+
if (!halide_scheduler.empty() && backend == DNN_BACKEND_HALIDE)
59+
halide_scheduler = findDataFile(std::string("dnn/halide_scheduler_") + (target == DNN_TARGET_OPENCL ? "opencl_" : "") + halide_scheduler, true);
60+
if (framework == "caffe")
61+
{
62+
net = cv::dnn::readNetFromCaffe(proto, weights);
63+
}
64+
else if (framework == "torch")
65+
{
66+
net = cv::dnn::readNetFromTorch(weights);
67+
}
68+
else if (framework == "tensorflow")
69+
{
70+
net = cv::dnn::readNetFromTensorflow(weights);
71+
}
72+
else
73+
CV_Error(Error::StsNotImplemented, "Unknown framework " + framework);
74+
75+
net.setInput(blobFromImage(input, 1.0, Size(), Scalar(), false));
76+
net.setPreferableBackend(backend);
77+
net.setPreferableTarget(target);
78+
if (backend == DNN_BACKEND_HALIDE)
79+
{
80+
net.setHalideScheduler(halide_scheduler);
81+
}
82+
83+
MatShape netInputShape = shape(1, 3, inHeight, inWidth);
84+
size_t weightsMemory = 0, blobsMemory = 0;
85+
net.getMemoryConsumption(netInputShape, weightsMemory, blobsMemory);
86+
int64 flops = net.getFLOPS(netInputShape);
87+
88+
net.forward(outputLayer); // warmup
89+
90+
std::cout << "Memory consumption:" << std::endl;
91+
std::cout << " Weights(parameters): " << divUp(weightsMemory, 1u<<20) << " Mb" << std::endl;
92+
std::cout << " Blobs: " << divUp(blobsMemory, 1u<<20) << " Mb" << std::endl;
93+
std::cout << "Calculation complexity: " << flops * 1e-9 << " GFlops" << std::endl;
94+
95+
PERF_SAMPLE_BEGIN()
96+
net.forward();
97+
PERF_SAMPLE_END()
98+
99+
SANITY_CHECK_NOTHING();
100+
}
101+
};
102+
103+
104+
PERF_TEST_P_(DNNTestNetwork, AlexNet)
105+
{
106+
processNet("dnn/bvlc_alexnet.caffemodel", "dnn/bvlc_alexnet.prototxt",
107+
"alexnet.yml", 227, 227, "prob", "caffe");
108+
}
109+
110+
PERF_TEST_P_(DNNTestNetwork, GoogLeNet)
111+
{
112+
processNet("dnn/bvlc_googlenet.caffemodel", "dnn/bvlc_googlenet.prototxt",
113+
"", 224, 224, "prob", "caffe");
114+
}
115+
116+
PERF_TEST_P_(DNNTestNetwork, ResNet50)
117+
{
118+
processNet("dnn/ResNet-50-model.caffemodel", "dnn/ResNet-50-deploy.prototxt",
119+
"resnet_50.yml", 224, 224, "prob", "caffe");
120+
}
121+
122+
PERF_TEST_P_(DNNTestNetwork, SqueezeNet_v1_1)
123+
{
124+
processNet("dnn/squeezenet_v1.1.caffemodel", "dnn/squeezenet_v1.1.prototxt",
125+
"squeezenet_v1_1.yml", 227, 227, "prob", "caffe");
126+
}
127+
128+
PERF_TEST_P_(DNNTestNetwork, Inception_5h)
129+
{
130+
processNet("dnn/tensorflow_inception_graph.pb", "",
131+
"inception_5h.yml",
132+
224, 224, "softmax2", "tensorflow");
133+
}
134+
135+
PERF_TEST_P_(DNNTestNetwork, ENet)
136+
{
137+
processNet("dnn/Enet-model-best.net", "", "enet.yml",
138+
512, 256, "l367_Deconvolution", "torch");
139+
}
140+
141+
142+
INSTANTIATE_TEST_CASE_P(/*nothing*/, DNNTestNetwork,
143+
testing::Combine(
144+
::testing::Values(TEST_DNN_BACKEND),
145+
DNNTarget::all()
146+
)
147+
);
148+
149+
} // namespace

0 commit comments

Comments
 (0)