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

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)
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
    void processNet(std::string weights, std::string proto, std::string halide_scheduler,
33
                    const Mat& input, const std::string& outputLayer = "")
34
    {
A
Alexander Alekhin 已提交
35 36
        if (backend == DNN_BACKEND_DEFAULT && target == DNN_TARGET_OPENCL)
        {
37
#if defined(HAVE_OPENCL)
A
Alexander Alekhin 已提交
38 39 40
            if (!cv::ocl::useOpenCL())
#endif
            {
A
Alexander Alekhin 已提交
41
                throw cvtest::SkipTestException("OpenCL is not available/disabled in OpenCV");
A
Alexander Alekhin 已提交
42 43 44 45 46 47 48 49
            }
        }

        randu(input, 0.0f, 1.0f);

        weights = findDataFile(weights, false);
        if (!proto.empty())
            proto = findDataFile(proto, false);
A
Alexander Alekhin 已提交
50 51 52
        if (backend == DNN_BACKEND_HALIDE)
        {
            if (halide_scheduler == "disabled")
A
Alexander Alekhin 已提交
53
                throw cvtest::SkipTestException("Halide test is disabled");
A
Alexander Alekhin 已提交
54 55 56
            if (!halide_scheduler.empty())
                halide_scheduler = findDataFile(std::string("dnn/halide_scheduler_") + (target == DNN_TARGET_OPENCL ? "opencl_" : "") + halide_scheduler, true);
        }
57
        net = readNet(proto, weights);
A
Alexander Alekhin 已提交
58 59 60 61 62 63 64 65
        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 已提交
66
        MatShape netInputShape = shape(1, 3, input.rows, input.cols);
A
Alexander Alekhin 已提交
67 68 69
        size_t weightsMemory = 0, blobsMemory = 0;
        net.getMemoryConsumption(netInputShape, weightsMemory, blobsMemory);
        int64 flops = net.getFLOPS(netInputShape);
70
        CV_Assert(flops > 0);
A
Alexander Alekhin 已提交
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89

        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)
{
90 91
    if (backend == DNN_BACKEND_INFERENCE_ENGINE && target != DNN_TARGET_CPU)
        throw SkipTestException("");
A
Alexander Alekhin 已提交
92
    processNet("dnn/bvlc_alexnet.caffemodel", "dnn/bvlc_alexnet.prototxt",
93
            "alexnet.yml", Mat(cv::Size(227, 227), CV_32FC3));
A
Alexander Alekhin 已提交
94 95 96 97 98
}

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

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

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

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

PERF_TEST_P_(DNNTestNetwork, ENet)
{
124
    if (backend == DNN_BACKEND_INFERENCE_ENGINE) throw SkipTestException("");
A
Alexander Alekhin 已提交
125
    processNet("dnn/Enet-model-best.net", "", "enet.yml",
126
            Mat(cv::Size(512, 256), CV_32FC3));
A
Alexander Alekhin 已提交
127 128
}

A
Alexander Alekhin 已提交
129 130
PERF_TEST_P_(DNNTestNetwork, SSD)
{
131
    if (backend == DNN_BACKEND_INFERENCE_ENGINE) throw SkipTestException("");
A
Alexander Alekhin 已提交
132
    processNet("dnn/VGG_ILSVRC2016_SSD_300x300_iter_440000.caffemodel", "dnn/ssd_vgg16.prototxt", "disabled",
133
            Mat(cv::Size(300, 300), CV_32FC3));
A
Alexander Alekhin 已提交
134
}
A
Alexander Alekhin 已提交
135

D
Dmitry Kurtaev 已提交
136 137
PERF_TEST_P_(DNNTestNetwork, OpenFace)
{
138 139 140
    if (backend == DNN_BACKEND_HALIDE ||
        backend == DNN_BACKEND_INFERENCE_ENGINE && target != DNN_TARGET_CPU)
        throw SkipTestException("");
D
Dmitry Kurtaev 已提交
141
    processNet("dnn/openface_nn4.small2.v1.t7", "", "",
142
            Mat(cv::Size(96, 96), CV_32FC3));
D
Dmitry Kurtaev 已提交
143 144 145 146
}

PERF_TEST_P_(DNNTestNetwork, MobileNet_SSD_Caffe)
{
147 148 149
    if (backend == DNN_BACKEND_HALIDE ||
        backend == DNN_BACKEND_INFERENCE_ENGINE && target != DNN_TARGET_CPU)
        throw SkipTestException("");
D
Dmitry Kurtaev 已提交
150
    processNet("dnn/MobileNetSSD_deploy.caffemodel", "dnn/MobileNetSSD_deploy.prototxt", "",
151
            Mat(cv::Size(300, 300), CV_32FC3));
D
Dmitry Kurtaev 已提交
152 153 154 155
}

PERF_TEST_P_(DNNTestNetwork, MobileNet_SSD_TensorFlow)
{
156
    if (backend == DNN_BACKEND_DEFAULT && target == DNN_TARGET_OPENCL ||
157 158
        backend == DNN_BACKEND_HALIDE ||
        backend == DNN_BACKEND_INFERENCE_ENGINE && target != DNN_TARGET_CPU)
159
        throw SkipTestException("");
D
Dmitry Kurtaev 已提交
160
    processNet("dnn/ssd_mobilenet_v1_coco.pb", "ssd_mobilenet_v1_coco.pbtxt", "",
161
            Mat(cv::Size(300, 300), CV_32FC3));
D
Dmitry Kurtaev 已提交
162 163
}

164 165
PERF_TEST_P_(DNNTestNetwork, DenseNet_121)
{
166 167 168
    if (backend == DNN_BACKEND_HALIDE ||
        backend == DNN_BACKEND_INFERENCE_ENGINE && target == DNN_TARGET_OPENCL_FP16)
        throw SkipTestException("");
169
    processNet("dnn/DenseNet_121.caffemodel", "dnn/DenseNet_121.prototxt", "",
170
               Mat(cv::Size(224, 224), CV_32FC3));
171 172 173 174 175 176
}

PERF_TEST_P_(DNNTestNetwork, OpenPose_pose_coco)
{
    if (backend == DNN_BACKEND_HALIDE) throw SkipTestException("");
    processNet("dnn/openpose_pose_coco.caffemodel", "dnn/openpose_pose_coco.prototxt", "",
177
               Mat(cv::Size(368, 368), CV_32FC3));
178 179 180 181 182 183
}

PERF_TEST_P_(DNNTestNetwork, OpenPose_pose_mpi)
{
    if (backend == DNN_BACKEND_HALIDE) throw SkipTestException("");
    processNet("dnn/openpose_pose_mpi.caffemodel", "dnn/openpose_pose_mpi.prototxt", "",
184
               Mat(cv::Size(368, 368), CV_32FC3));
185 186 187 188 189 190 191 192
}

PERF_TEST_P_(DNNTestNetwork, OpenPose_pose_mpi_faster_4_stages)
{
    if (backend == DNN_BACKEND_HALIDE) throw SkipTestException("");
    // 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", "",
193
               Mat(cv::Size(368, 368), CV_32FC3));
194 195
}

196 197 198
PERF_TEST_P_(DNNTestNetwork, opencv_face_detector)
{
    if (backend == DNN_BACKEND_HALIDE ||
199
        backend == DNN_BACKEND_INFERENCE_ENGINE && target != DNN_TARGET_CPU)
200 201
        throw SkipTestException("");
    processNet("dnn/opencv_face_detector.caffemodel", "dnn/opencv_face_detector.prototxt", "",
202
               Mat(cv::Size(300, 300), CV_32FC3));
203 204
}

205 206
PERF_TEST_P_(DNNTestNetwork, Inception_v2_SSD_TensorFlow)
{
207 208 209
    if (backend == DNN_BACKEND_HALIDE ||
        backend == DNN_BACKEND_INFERENCE_ENGINE && target != DNN_TARGET_CPU)
        throw SkipTestException("");
210
    processNet("dnn/ssd_inception_v2_coco_2017_11_17.pb", "ssd_inception_v2_coco_2017_11_17.pbtxt", "",
211
            Mat(cv::Size(300, 300), CV_32FC3));
212 213
}

214 215 216 217 218 219 220 221 222 223
PERF_TEST_P_(DNNTestNetwork, YOLOv3)
{
    if (backend != DNN_BACKEND_DEFAULT)
        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);
}

224 225 226 227 228 229 230
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),
231 232
    tuple<DNNBackend, DNNTarget>(DNN_BACKEND_INFERENCE_ENGINE, DNN_TARGET_OPENCL),
    tuple<DNNBackend, DNNTarget>(DNN_BACKEND_INFERENCE_ENGINE, DNN_TARGET_OPENCL_FP16),
233 234 235 236 237 238
#endif
    tuple<DNNBackend, DNNTarget>(DNN_BACKEND_DEFAULT, DNN_TARGET_CPU),
    tuple<DNNBackend, DNNTarget>(DNN_BACKEND_DEFAULT, DNN_TARGET_OPENCL)
};

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

} // namespace