perf_net.cpp 9.8 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"

A
Alexander Alekhin 已提交
13
namespace opencv_test {
A
Alexander Alekhin 已提交
14

15
CV_ENUM(DNNBackend, DNN_BACKEND_DEFAULT, DNN_BACKEND_HALIDE, DNN_BACKEND_INFERENCE_ENGINE)
16
CV_ENUM(DNNTarget, DNN_TARGET_CPU, DNN_TARGET_OPENCL, DNN_TARGET_OPENCL_FP16, DNN_TARGET_MYRIAD)
A
Alexander Alekhin 已提交
17 18 19 20 21 22 23 24 25

class DNNTestNetwork : public ::perf::TestBaseWithParam< tuple<DNNBackend, DNNTarget> >
{
public:
    dnn::Backend backend;
    dnn::Target target;

    dnn::Net net;

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

32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
    static bool checkMyriadTarget()
    {
#ifndef HAVE_INF_ENGINE
        return false;
#endif
        cv::dnn::Net net;
        cv::dnn::LayerParams lp;
        net.addLayerToPrev("testLayer", "Identity", lp);
        net.setPreferableBackend(cv::dnn::DNN_BACKEND_INFERENCE_ENGINE);
        net.setPreferableTarget(cv::dnn::DNN_TARGET_MYRIAD);
        net.setInput(cv::Mat::zeros(1, 1, CV_32FC1));
        try
        {
            net.forward();
        }
        catch(...)
        {
            return false;
        }
        return true;
    }

54
    void processNet(std::string weights, std::string proto, std::string halide_scheduler,
55
                    const Mat& input, const std::string& outputLayer = "")
56
    {
A
Alexander Alekhin 已提交
57 58
        if (backend == DNN_BACKEND_DEFAULT && target == DNN_TARGET_OPENCL)
        {
59
#if defined(HAVE_OPENCL)
A
Alexander Alekhin 已提交
60 61 62
            if (!cv::ocl::useOpenCL())
#endif
            {
A
Alexander Alekhin 已提交
63
                throw cvtest::SkipTestException("OpenCL is not available/disabled in OpenCV");
A
Alexander Alekhin 已提交
64 65
            }
        }
66 67 68 69 70 71 72
        if (backend == DNN_BACKEND_INFERENCE_ENGINE && target == DNN_TARGET_MYRIAD)
        {
            if (!checkMyriadTarget())
            {
                throw SkipTestException("Myriad is not available/disabled in OpenCV");
            }
        }
A
Alexander Alekhin 已提交
73 74 75 76 77 78

        randu(input, 0.0f, 1.0f);

        weights = findDataFile(weights, false);
        if (!proto.empty())
            proto = findDataFile(proto, false);
A
Alexander Alekhin 已提交
79 80 81
        if (backend == DNN_BACKEND_HALIDE)
        {
            if (halide_scheduler == "disabled")
A
Alexander Alekhin 已提交
82
                throw cvtest::SkipTestException("Halide test is disabled");
A
Alexander Alekhin 已提交
83 84 85
            if (!halide_scheduler.empty())
                halide_scheduler = findDataFile(std::string("dnn/halide_scheduler_") + (target == DNN_TARGET_OPENCL ? "opencl_" : "") + halide_scheduler, true);
        }
86
        net = readNet(proto, weights);
A
Alexander Alekhin 已提交
87 88 89 90 91 92 93 94
        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 已提交
95
        MatShape netInputShape = shape(1, 3, input.rows, input.cols);
A
Alexander Alekhin 已提交
96 97 98
        size_t weightsMemory = 0, blobsMemory = 0;
        net.getMemoryConsumption(netInputShape, weightsMemory, blobsMemory);
        int64 flops = net.getFLOPS(netInputShape);
99
        CV_Assert(flops > 0);
A
Alexander Alekhin 已提交
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119

        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",
120
            "alexnet.yml", Mat(cv::Size(227, 227), CV_32FC3));
A
Alexander Alekhin 已提交
121 122 123 124 125
}

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

129
PERF_TEST_P_(DNNTestNetwork, ResNet_50)
A
Alexander Alekhin 已提交
130 131
{
    processNet("dnn/ResNet-50-model.caffemodel", "dnn/ResNet-50-deploy.prototxt",
132
            "resnet_50.yml", Mat(cv::Size(224, 224), CV_32FC3));
A
Alexander Alekhin 已提交
133 134 135 136 137
}

PERF_TEST_P_(DNNTestNetwork, SqueezeNet_v1_1)
{
    processNet("dnn/squeezenet_v1.1.caffemodel", "dnn/squeezenet_v1.1.prototxt",
138
            "squeezenet_v1_1.yml", Mat(cv::Size(227, 227), CV_32FC3));
A
Alexander Alekhin 已提交
139 140 141 142
}

PERF_TEST_P_(DNNTestNetwork, Inception_5h)
{
143
    if (backend == DNN_BACKEND_INFERENCE_ENGINE) throw SkipTestException("");
A
Alexander Alekhin 已提交
144 145
    processNet("dnn/tensorflow_inception_graph.pb", "",
            "inception_5h.yml",
146
            Mat(cv::Size(224, 224), CV_32FC3), "softmax2");
A
Alexander Alekhin 已提交
147 148 149 150
}

PERF_TEST_P_(DNNTestNetwork, ENet)
{
L
Li Peng 已提交
151 152 153
    if ((backend == DNN_BACKEND_INFERENCE_ENGINE) ||
        (backend == DNN_BACKEND_DEFAULT && target == DNN_TARGET_OPENCL_FP16))
        throw SkipTestException("");
A
Alexander Alekhin 已提交
154
    processNet("dnn/Enet-model-best.net", "", "enet.yml",
155
            Mat(cv::Size(512, 256), CV_32FC3));
A
Alexander Alekhin 已提交
156 157
}

A
Alexander Alekhin 已提交
158 159 160
PERF_TEST_P_(DNNTestNetwork, SSD)
{
    processNet("dnn/VGG_ILSVRC2016_SSD_300x300_iter_440000.caffemodel", "dnn/ssd_vgg16.prototxt", "disabled",
161
            Mat(cv::Size(300, 300), CV_32FC3));
A
Alexander Alekhin 已提交
162
}
A
Alexander Alekhin 已提交
163

D
Dmitry Kurtaev 已提交
164 165
PERF_TEST_P_(DNNTestNetwork, OpenFace)
{
166 167 168
    if (backend == DNN_BACKEND_HALIDE ||
        backend == DNN_BACKEND_INFERENCE_ENGINE && target != DNN_TARGET_CPU)
        throw SkipTestException("");
D
Dmitry Kurtaev 已提交
169
    processNet("dnn/openface_nn4.small2.v1.t7", "", "",
170
            Mat(cv::Size(96, 96), CV_32FC3));
D
Dmitry Kurtaev 已提交
171 172 173 174
}

PERF_TEST_P_(DNNTestNetwork, MobileNet_SSD_Caffe)
{
175
    if (backend == DNN_BACKEND_HALIDE)
176
        throw SkipTestException("");
D
Dmitry Kurtaev 已提交
177
    processNet("dnn/MobileNetSSD_deploy.caffemodel", "dnn/MobileNetSSD_deploy.prototxt", "",
178
            Mat(cv::Size(300, 300), CV_32FC3));
D
Dmitry Kurtaev 已提交
179 180
}

181
// TODO: update MobileNet model.
D
Dmitry Kurtaev 已提交
182 183
PERF_TEST_P_(DNNTestNetwork, MobileNet_SSD_TensorFlow)
{
184 185
    if (backend == DNN_BACKEND_HALIDE ||
        backend == DNN_BACKEND_INFERENCE_ENGINE)
186
        throw SkipTestException("");
D
Dmitry Kurtaev 已提交
187
    processNet("dnn/ssd_mobilenet_v1_coco.pb", "ssd_mobilenet_v1_coco.pbtxt", "",
188
            Mat(cv::Size(300, 300), CV_32FC3));
D
Dmitry Kurtaev 已提交
189 190
}

191 192
PERF_TEST_P_(DNNTestNetwork, DenseNet_121)
{
193
    if (backend == DNN_BACKEND_HALIDE ||
194 195
        backend == DNN_BACKEND_INFERENCE_ENGINE && (target == DNN_TARGET_OPENCL_FP16 ||
                                                    target == DNN_TARGET_MYRIAD))
196
        throw SkipTestException("");
197
    processNet("dnn/DenseNet_121.caffemodel", "dnn/DenseNet_121.prototxt", "",
198
               Mat(cv::Size(224, 224), CV_32FC3));
199 200 201 202
}

PERF_TEST_P_(DNNTestNetwork, OpenPose_pose_coco)
{
203 204 205
    if (backend == DNN_BACKEND_HALIDE ||
        backend == DNN_BACKEND_INFERENCE_ENGINE && target == DNN_TARGET_MYRIAD)
        throw SkipTestException("");
206
    processNet("dnn/openpose_pose_coco.caffemodel", "dnn/openpose_pose_coco.prototxt", "",
207
               Mat(cv::Size(368, 368), CV_32FC3));
208 209 210 211
}

PERF_TEST_P_(DNNTestNetwork, OpenPose_pose_mpi)
{
212 213 214
    if (backend == DNN_BACKEND_HALIDE ||
        backend == DNN_BACKEND_INFERENCE_ENGINE && target == DNN_TARGET_MYRIAD)
        throw SkipTestException("");
215
    processNet("dnn/openpose_pose_mpi.caffemodel", "dnn/openpose_pose_mpi.prototxt", "",
216
               Mat(cv::Size(368, 368), CV_32FC3));
217 218 219 220
}

PERF_TEST_P_(DNNTestNetwork, OpenPose_pose_mpi_faster_4_stages)
{
221 222 223
    if (backend == DNN_BACKEND_HALIDE ||
        backend == DNN_BACKEND_INFERENCE_ENGINE && target == DNN_TARGET_MYRIAD)
        throw SkipTestException("");
224 225 226
    // 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", "",
227
               Mat(cv::Size(368, 368), CV_32FC3));
228 229
}

230 231
PERF_TEST_P_(DNNTestNetwork, opencv_face_detector)
{
232
    if (backend == DNN_BACKEND_HALIDE)
233 234
        throw SkipTestException("");
    processNet("dnn/opencv_face_detector.caffemodel", "dnn/opencv_face_detector.prototxt", "",
235
               Mat(cv::Size(300, 300), CV_32FC3));
236 237
}

238 239
PERF_TEST_P_(DNNTestNetwork, Inception_v2_SSD_TensorFlow)
{
240
    if (backend == DNN_BACKEND_HALIDE ||
241 242
        (backend == DNN_BACKEND_INFERENCE_ENGINE && target == DNN_TARGET_OPENCL) ||
        (backend == DNN_BACKEND_INFERENCE_ENGINE && target == DNN_TARGET_OPENCL_FP16))
243
        throw SkipTestException("");
244
    processNet("dnn/ssd_inception_v2_coco_2017_11_17.pb", "ssd_inception_v2_coco_2017_11_17.pbtxt", "",
245
            Mat(cv::Size(300, 300), CV_32FC3));
246 247
}

248 249
PERF_TEST_P_(DNNTestNetwork, YOLOv3)
{
250 251
    if (backend == DNN_BACKEND_HALIDE ||
        backend == DNN_BACKEND_INFERENCE_ENGINE && target == DNN_TARGET_MYRIAD)
252 253 254 255 256 257 258
        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);
}

259 260 261 262 263 264 265
const tuple<DNNBackend, DNNTarget> testCases[] = {
#ifdef HAVE_HALIDE
    tuple<DNNBackend, DNNTarget>(DNN_BACKEND_HALIDE, DNN_TARGET_CPU),
    tuple<DNNBackend, DNNTarget>(DNN_BACKEND_HALIDE, DNN_TARGET_OPENCL),
#endif
#ifdef HAVE_INF_ENGINE
    tuple<DNNBackend, DNNTarget>(DNN_BACKEND_INFERENCE_ENGINE, DNN_TARGET_CPU),
266 267
    tuple<DNNBackend, DNNTarget>(DNN_BACKEND_INFERENCE_ENGINE, DNN_TARGET_OPENCL),
    tuple<DNNBackend, DNNTarget>(DNN_BACKEND_INFERENCE_ENGINE, DNN_TARGET_OPENCL_FP16),
268
    tuple<DNNBackend, DNNTarget>(DNN_BACKEND_INFERENCE_ENGINE, DNN_TARGET_MYRIAD),
269 270
#endif
    tuple<DNNBackend, DNNTarget>(DNN_BACKEND_DEFAULT, DNN_TARGET_CPU),
L
Li Peng 已提交
271 272
    tuple<DNNBackend, DNNTarget>(DNN_BACKEND_DEFAULT, DNN_TARGET_OPENCL),
    tuple<DNNBackend, DNNTarget>(DNN_BACKEND_DEFAULT, DNN_TARGET_OPENCL_FP16)
273 274 275
};

INSTANTIATE_TEST_CASE_P(/*nothing*/, DNNTestNetwork, testing::ValuesIn(testCases));
A
Alexander Alekhin 已提交
276 277

} // namespace