perf_net.cpp 9.7 KB
Newer Older
A
Alexander Alekhin 已提交
1 2 3 4 5 6 7 8 9 10 11 12
// This file is part of OpenCV project.
// It is subject to the license terms in the LICENSE file found in the top-level directory
// of this distribution and at http://opencv.org/license.html.
//
// Copyright (C) 2017, Intel Corporation, all rights reserved.
// Third party copyrights are property of their respective owners.

#include "perf_precomp.hpp"
#include "opencv2/core/ocl.hpp"

#include "opencv2/dnn/shape_utils.hpp"

13 14
#include "../test/test_common.hpp"

A
Alexander Alekhin 已提交
15
namespace opencv_test {
A
Alexander Alekhin 已提交
16

17
class DNNTestNetwork : public ::perf::TestBaseWithParam< tuple<Backend, Target> >
A
Alexander Alekhin 已提交
18 19 20 21 22 23 24
{
public:
    dnn::Backend backend;
    dnn::Target target;

    dnn::Net net;

25
    DNNTestNetwork()
A
Alexander Alekhin 已提交
26 27 28
    {
        backend = (dnn::Backend)(int)get<0>(GetParam());
        target = (dnn::Target)(int)get<1>(GetParam());
29
    }
A
Alexander Alekhin 已提交
30

31
    void processNet(std::string weights, std::string proto, std::string halide_scheduler,
32
                    const Mat& input, const std::string& outputLayer = "")
33
    {
34
        if (backend == DNN_BACKEND_OPENCV && (target == DNN_TARGET_OPENCL || target == DNN_TARGET_OPENCL_FP16))
A
Alexander Alekhin 已提交
35
        {
36
#if defined(HAVE_OPENCL)
A
Alexander Alekhin 已提交
37 38 39
            if (!cv::ocl::useOpenCL())
#endif
            {
A
Alexander Alekhin 已提交
40
                throw cvtest::SkipTestException("OpenCL is not available/disabled in OpenCV");
A
Alexander Alekhin 已提交
41 42
            }
        }
43 44 45 46 47 48 49
        if (backend == DNN_BACKEND_INFERENCE_ENGINE && target == DNN_TARGET_MYRIAD)
        {
            if (!checkMyriadTarget())
            {
                throw SkipTestException("Myriad is not available/disabled in OpenCV");
            }
        }
A
Alexander Alekhin 已提交
50 51 52 53 54 55

        randu(input, 0.0f, 1.0f);

        weights = findDataFile(weights, false);
        if (!proto.empty())
            proto = findDataFile(proto, false);
A
Alexander Alekhin 已提交
56 57 58
        if (backend == DNN_BACKEND_HALIDE)
        {
            if (halide_scheduler == "disabled")
A
Alexander Alekhin 已提交
59
                throw cvtest::SkipTestException("Halide test is disabled");
A
Alexander Alekhin 已提交
60 61 62
            if (!halide_scheduler.empty())
                halide_scheduler = findDataFile(std::string("dnn/halide_scheduler_") + (target == DNN_TARGET_OPENCL ? "opencl_" : "") + halide_scheduler, true);
        }
63
        net = readNet(proto, weights);
A
Alexander Alekhin 已提交
64 65 66 67 68 69 70 71
        net.setInput(blobFromImage(input, 1.0, Size(), Scalar(), false));
        net.setPreferableBackend(backend);
        net.setPreferableTarget(target);
        if (backend == DNN_BACKEND_HALIDE)
        {
            net.setHalideScheduler(halide_scheduler);
        }

A
Alexander Alekhin 已提交
72
        MatShape netInputShape = shape(1, 3, input.rows, input.cols);
A
Alexander Alekhin 已提交
73 74 75
        size_t weightsMemory = 0, blobsMemory = 0;
        net.getMemoryConsumption(netInputShape, weightsMemory, blobsMemory);
        int64 flops = net.getFLOPS(netInputShape);
76
        CV_Assert(flops > 0);
A
Alexander Alekhin 已提交
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96

        net.forward(outputLayer); // warmup

        std::cout << "Memory consumption:" << std::endl;
        std::cout << "    Weights(parameters): " << divUp(weightsMemory, 1u<<20) << " Mb" << std::endl;
        std::cout << "    Blobs: " << divUp(blobsMemory, 1u<<20) << " Mb" << std::endl;
        std::cout << "Calculation complexity: " << flops * 1e-9 << " GFlops" << std::endl;

        PERF_SAMPLE_BEGIN()
            net.forward();
        PERF_SAMPLE_END()

        SANITY_CHECK_NOTHING();
    }
};


PERF_TEST_P_(DNNTestNetwork, AlexNet)
{
    processNet("dnn/bvlc_alexnet.caffemodel", "dnn/bvlc_alexnet.prototxt",
97
            "alexnet.yml", Mat(cv::Size(227, 227), CV_32FC3));
A
Alexander Alekhin 已提交
98 99 100 101 102
}

PERF_TEST_P_(DNNTestNetwork, GoogLeNet)
{
    processNet("dnn/bvlc_googlenet.caffemodel", "dnn/bvlc_googlenet.prototxt",
103
            "", Mat(cv::Size(224, 224), CV_32FC3));
A
Alexander Alekhin 已提交
104 105
}

106
PERF_TEST_P_(DNNTestNetwork, ResNet_50)
A
Alexander Alekhin 已提交
107 108
{
    processNet("dnn/ResNet-50-model.caffemodel", "dnn/ResNet-50-deploy.prototxt",
109
            "resnet_50.yml", Mat(cv::Size(224, 224), CV_32FC3));
A
Alexander Alekhin 已提交
110 111 112 113 114
}

PERF_TEST_P_(DNNTestNetwork, SqueezeNet_v1_1)
{
    processNet("dnn/squeezenet_v1.1.caffemodel", "dnn/squeezenet_v1.1.prototxt",
115
            "squeezenet_v1_1.yml", Mat(cv::Size(227, 227), CV_32FC3));
A
Alexander Alekhin 已提交
116 117 118 119
}

PERF_TEST_P_(DNNTestNetwork, Inception_5h)
{
120
    if (backend == DNN_BACKEND_INFERENCE_ENGINE) throw SkipTestException("");
A
Alexander Alekhin 已提交
121 122
    processNet("dnn/tensorflow_inception_graph.pb", "",
            "inception_5h.yml",
123
            Mat(cv::Size(224, 224), CV_32FC3), "softmax2");
A
Alexander Alekhin 已提交
124 125 126 127
}

PERF_TEST_P_(DNNTestNetwork, ENet)
{
L
Li Peng 已提交
128
    if ((backend == DNN_BACKEND_INFERENCE_ENGINE) ||
129
        (backend == DNN_BACKEND_OPENCV && target == DNN_TARGET_OPENCL_FP16))
L
Li Peng 已提交
130
        throw SkipTestException("");
A
Alexander Alekhin 已提交
131
    processNet("dnn/Enet-model-best.net", "", "enet.yml",
132
            Mat(cv::Size(512, 256), CV_32FC3));
A
Alexander Alekhin 已提交
133 134
}

A
Alexander Alekhin 已提交
135 136 137
PERF_TEST_P_(DNNTestNetwork, SSD)
{
    processNet("dnn/VGG_ILSVRC2016_SSD_300x300_iter_440000.caffemodel", "dnn/ssd_vgg16.prototxt", "disabled",
138
            Mat(cv::Size(300, 300), CV_32FC3));
A
Alexander Alekhin 已提交
139
}
A
Alexander Alekhin 已提交
140

D
Dmitry Kurtaev 已提交
141 142
PERF_TEST_P_(DNNTestNetwork, OpenFace)
{
143
    if (backend == DNN_BACKEND_HALIDE ||
144 145
        (backend == DNN_BACKEND_INFERENCE_ENGINE && target == DNN_TARGET_OPENCL_FP16) ||
        (backend == DNN_BACKEND_INFERENCE_ENGINE && target == DNN_TARGET_MYRIAD))
146
        throw SkipTestException("");
D
Dmitry Kurtaev 已提交
147
    processNet("dnn/openface_nn4.small2.v1.t7", "", "",
148
            Mat(cv::Size(96, 96), CV_32FC3));
D
Dmitry Kurtaev 已提交
149 150 151 152
}

PERF_TEST_P_(DNNTestNetwork, MobileNet_SSD_Caffe)
{
153
    if (backend == DNN_BACKEND_HALIDE)
154
        throw SkipTestException("");
D
Dmitry Kurtaev 已提交
155
    processNet("dnn/MobileNetSSD_deploy.caffemodel", "dnn/MobileNetSSD_deploy.prototxt", "",
156
            Mat(cv::Size(300, 300), CV_32FC3));
D
Dmitry Kurtaev 已提交
157 158
}

159
PERF_TEST_P_(DNNTestNetwork, MobileNet_SSD_v1_TensorFlow)
D
Dmitry Kurtaev 已提交
160
{
161 162 163 164 165 166 167 168 169
    if (backend == DNN_BACKEND_HALIDE)
        throw SkipTestException("");
    processNet("dnn/ssd_mobilenet_v1_coco_2017_11_17.pb", "ssd_mobilenet_v1_coco_2017_11_17.pbtxt", "",
            Mat(cv::Size(300, 300), CV_32FC3));
}

PERF_TEST_P_(DNNTestNetwork, MobileNet_SSD_v2_TensorFlow)
{
    if (backend == DNN_BACKEND_HALIDE)
170
        throw SkipTestException("");
171
    processNet("dnn/ssd_mobilenet_v2_coco_2018_03_29.pb", "ssd_mobilenet_v2_coco_2018_03_29.pbtxt", "",
172
            Mat(cv::Size(300, 300), CV_32FC3));
D
Dmitry Kurtaev 已提交
173 174
}

175 176
PERF_TEST_P_(DNNTestNetwork, DenseNet_121)
{
177
    if (backend == DNN_BACKEND_HALIDE ||
178
        (backend == DNN_BACKEND_INFERENCE_ENGINE && (target == DNN_TARGET_OPENCL_FP16 || target == DNN_TARGET_MYRIAD)))
179
        throw SkipTestException("");
180
    processNet("dnn/DenseNet_121.caffemodel", "dnn/DenseNet_121.prototxt", "",
181
               Mat(cv::Size(224, 224), CV_32FC3));
182 183 184 185
}

PERF_TEST_P_(DNNTestNetwork, OpenPose_pose_coco)
{
186
    if (backend == DNN_BACKEND_HALIDE ||
187
        (backend == DNN_BACKEND_INFERENCE_ENGINE && target == DNN_TARGET_MYRIAD))
188
        throw SkipTestException("");
189
    processNet("dnn/openpose_pose_coco.caffemodel", "dnn/openpose_pose_coco.prototxt", "",
190
               Mat(cv::Size(368, 368), CV_32FC3));
191 192 193 194
}

PERF_TEST_P_(DNNTestNetwork, OpenPose_pose_mpi)
{
195
    if (backend == DNN_BACKEND_HALIDE ||
196
        (backend == DNN_BACKEND_INFERENCE_ENGINE && target == DNN_TARGET_MYRIAD))
197
        throw SkipTestException("");
198
    processNet("dnn/openpose_pose_mpi.caffemodel", "dnn/openpose_pose_mpi.prototxt", "",
199
               Mat(cv::Size(368, 368), CV_32FC3));
200 201 202 203
}

PERF_TEST_P_(DNNTestNetwork, OpenPose_pose_mpi_faster_4_stages)
{
204
    if (backend == DNN_BACKEND_HALIDE ||
205
        (backend == DNN_BACKEND_INFERENCE_ENGINE && target == DNN_TARGET_MYRIAD))
206
        throw SkipTestException("");
207 208 209
    // The same .caffemodel but modified .prototxt
    // See https://github.com/CMU-Perceptual-Computing-Lab/openpose/blob/master/src/openpose/pose/poseParameters.cpp
    processNet("dnn/openpose_pose_mpi.caffemodel", "dnn/openpose_pose_mpi_faster_4_stages.prototxt", "",
210
               Mat(cv::Size(368, 368), CV_32FC3));
211 212
}

213 214
PERF_TEST_P_(DNNTestNetwork, opencv_face_detector)
{
215
    if (backend == DNN_BACKEND_HALIDE)
216 217
        throw SkipTestException("");
    processNet("dnn/opencv_face_detector.caffemodel", "dnn/opencv_face_detector.prototxt", "",
218
               Mat(cv::Size(300, 300), CV_32FC3));
219 220
}

221 222
PERF_TEST_P_(DNNTestNetwork, Inception_v2_SSD_TensorFlow)
{
223
    if (backend == DNN_BACKEND_HALIDE)
224
        throw SkipTestException("");
225
    processNet("dnn/ssd_inception_v2_coco_2017_11_17.pb", "ssd_inception_v2_coco_2017_11_17.pbtxt", "",
226
            Mat(cv::Size(300, 300), CV_32FC3));
227 228
}

229 230
PERF_TEST_P_(DNNTestNetwork, YOLOv3)
{
231
    if (backend == DNN_BACKEND_HALIDE ||
232
        (backend == DNN_BACKEND_INFERENCE_ENGINE && target == DNN_TARGET_MYRIAD))
233 234 235 236 237 238 239
        throw SkipTestException("");
    Mat sample = imread(findDataFile("dnn/dog416.png", false));
    Mat inp;
    sample.convertTo(inp, CV_32FC3);
    processNet("dnn/yolov3.cfg", "dnn/yolov3.weights", "", inp / 255);
}

D
David 已提交
240 241 242
PERF_TEST_P_(DNNTestNetwork, EAST_text_detection)
{
    if (backend == DNN_BACKEND_HALIDE ||
243
        (backend == DNN_BACKEND_INFERENCE_ENGINE && target == DNN_TARGET_MYRIAD))
D
David 已提交
244 245 246 247
        throw SkipTestException("");
    processNet("dnn/frozen_east_text_detection.pb", "", "", Mat(cv::Size(320, 320), CV_32FC3));
}

248 249 250 251 252 253 254 255 256
PERF_TEST_P_(DNNTestNetwork, FastNeuralStyle_eccv16)
{
    if (backend == DNN_BACKEND_HALIDE ||
        (backend == DNN_BACKEND_OPENCV && target == DNN_TARGET_OPENCL_FP16) ||
        (backend == DNN_BACKEND_INFERENCE_ENGINE && target == DNN_TARGET_MYRIAD))
        throw SkipTestException("");
    processNet("dnn/fast_neural_style_eccv16_starry_night.t7", "", "", Mat(cv::Size(320, 240), CV_32FC3));
}

257 258 259 260 261 262 263 264 265 266 267
PERF_TEST_P_(DNNTestNetwork, Inception_v2_Faster_RCNN)
{
    if (backend == DNN_BACKEND_HALIDE ||
        (backend == DNN_BACKEND_INFERENCE_ENGINE && target != DNN_TARGET_CPU) ||
        (backend == DNN_BACKEND_OPENCV && target == DNN_TARGET_OPENCL_FP16))
        throw SkipTestException("");
    processNet("dnn/faster_rcnn_inception_v2_coco_2018_01_28.pb",
               "dnn/faster_rcnn_inception_v2_coco_2018_01_28.pbtxt", "",
               Mat(cv::Size(800, 600), CV_32FC3));
}

268
INSTANTIATE_TEST_CASE_P(/*nothing*/, DNNTestNetwork, dnnBackendsAndTargets());
A
Alexander Alekhin 已提交
269 270

} // namespace