Skip to content

Commit 4196543

Browse files
committed
Merge pull request opencv#9313 from dkurt:dnn_perf_test
2 parents 99fae81 + 5c43a39 commit 4196543

File tree

7 files changed

+131
-26
lines changed

7 files changed

+131
-26
lines changed

doc/tutorials/dnn/dnn_halide/dnn_halide.markdown

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,7 @@ according to specific device and evaluate it with a quite good efficiency.
88

99
An official website of the Halide project: http://halide-lang.org/.
1010

11-
## Efficiency comparison
12-
Measured on Intel® Core™ i7-6700K CPU @ 4.00GHz x 8.
13-
14-
Single image forward pass (in milliseconds):
15-
16-
| Architecture | MKL backend | Halide backend | Speed Up ratio |
17-
|-----------------:|------------:|---------------:|---------------:|
18-
| AlexNet | 16.55 | 22.38 | x0.73 |
19-
| ResNet-50 | 63.69 | 73.91 | x0.86 |
20-
| SqueezeNet v1.1 | 10.11 | 8.21 | x1.23 |
21-
| Inception-5h | 35.38 | 37.06 | x0.95 |
22-
| ENet @ 3x512x256 | 82.26 | 41.21 | x1.99 |
23-
24-
Scheduling directives might be found @ [opencv_extra/testdata/dnn](https://github.com/opencv/opencv_extra/tree/master/testdata/dnn).
11+
An up to date efficiency comparison: https://github.com/opencv/opencv/wiki/DNN-Efficiency
2512

2613
## Requirements
2714
### LLVM compiler
@@ -81,6 +68,8 @@ MSBuild.exe /m:4 /t:Build /p:Configuration=Release .\\ALL_BUILD.vcxproj
8168
## Build OpenCV with Halide backend
8269
When you build OpenCV add the following configuration flags:
8370

71+
- `ENABLE_CXX11` - enable C++11 standard
72+
8473
- `WITH_HALIDE` - enable Halide linkage
8574

8675
- `HALIDE_ROOT_DIR` - path to Halide build directory

modules/dnn/CMakeLists.txt

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,24 @@ ocv_add_samples()
7777
ocv_add_accuracy_tests()
7878
ocv_add_perf_tests()
7979

80+
ocv_option(${the_module}_PERF_CAFFE "Add performance tests of Caffe framework" OFF)
81+
ocv_option(${the_module}_PERF_CLCAFFE "Add performance tests of clCaffe framework" OFF)
82+
if(BUILD_PERF_TESTS)
83+
if (${the_module}_PERF_CAFFE)
84+
find_package(Caffe QUIET)
85+
if (Caffe_FOUND)
86+
add_definitions(-DHAVE_CAFFE=1)
87+
ocv_target_link_libraries(opencv_perf_dnn caffe)
88+
endif()
89+
elseif(${the_module}_PERF_CLCAFFE)
90+
find_package(Caffe QUIET)
91+
if (Caffe_FOUND)
92+
add_definitions(-DHAVE_CLCAFFE=1)
93+
ocv_target_link_libraries(opencv_perf_dnn caffe)
94+
endif()
95+
endif()
96+
endif()
97+
8098
# ----------------------------------------------------------------------------
8199
# Torch7 importer of blobs and models, produced by Torch.nn module
82100
# ----------------------------------------------------------------------------

modules/dnn/include/opencv2/dnn/dnn.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -433,21 +433,21 @@ CV__DNN_EXPERIMENTAL_NS_BEGIN
433433
* specific target. For layers that not represented in scheduling file
434434
* or if no manual scheduling used at all, automatic scheduling will be applied.
435435
*/
436-
void setHalideScheduler(const String& scheduler);
436+
CV_WRAP void setHalideScheduler(const String& scheduler);
437437

438438
/**
439439
* @brief Ask network to use specific computation backend where it supported.
440440
* @param[in] backendId backend identifier.
441441
* @see Backend
442442
*/
443-
void setPreferableBackend(int backendId);
443+
CV_WRAP void setPreferableBackend(int backendId);
444444

445445
/**
446446
* @brief Ask network to make computations on specific target device.
447447
* @param[in] targetId target identifier.
448448
* @see Target
449449
*/
450-
void setPreferableTarget(int targetId);
450+
CV_WRAP void setPreferableTarget(int targetId);
451451

452452
/** @brief Sets the new value for the layer output blob
453453
* @param name descriptor of the updating layer output blob.

modules/dnn/perf/perf_caffe.cpp

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
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

modules/dnn/perf/perf_halide_net.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ PERF_TEST(GoogLeNet, HalidePerfTest)
5555
{
5656
Net net;
5757
loadNet("dnn/bvlc_googlenet.caffemodel", "dnn/bvlc_googlenet.prototxt",
58-
"", 227, 227, "prob", "caffe", DNN_TARGET_CPU, &net);
58+
"", 224, 224, "prob", "caffe", DNN_TARGET_CPU, &net);
5959
TEST_CYCLE() net.forward();
6060
SANITY_CHECK_NOTHING();
6161
}

modules/dnn/test/test_halide_nets.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ TEST(Reproducibility_GoogLeNet_Halide, Accuracy)
9999
{
100100
test(findDataFile("dnn/bvlc_googlenet.caffemodel", false),
101101
findDataFile("dnn/bvlc_googlenet.prototxt", false),
102-
"", 227, 227, "prob", "caffe", DNN_TARGET_CPU);
102+
"", 224, 224, "prob", "caffe", DNN_TARGET_CPU);
103103
};
104104

105105
TEST(Reproducibility_AlexNet_Halide, Accuracy)

samples/dnn/squeezenet_halide.cpp

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,3 @@
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-
81
// Sample of using Halide backend in OpenCV deep learning module.
92
// Based on caffe_googlenet.cpp.
103

0 commit comments

Comments
 (0)