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
}

PERF_TEST_P_(DNNTestNetwork, GoogLeNet)
{
98 99
    if (backend == DNN_BACKEND_INFERENCE_ENGINE && target == DNN_TARGET_OPENCL_FP16)
        throw SkipTestException("");
A
Alexander Alekhin 已提交
100
    processNet("dnn/bvlc_googlenet.caffemodel", "dnn/bvlc_googlenet.prototxt",
101
            "", Mat(cv::Size(224, 224), CV_32FC3));
A
Alexander Alekhin 已提交
102 103
}

104
PERF_TEST_P_(DNNTestNetwork, ResNet_50)
A
Alexander Alekhin 已提交
105
{
106 107
    if (backend == DNN_BACKEND_INFERENCE_ENGINE && target == DNN_TARGET_OPENCL_FP16)
        throw SkipTestException("");
A
Alexander Alekhin 已提交
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
}

PERF_TEST_P_(DNNTestNetwork, SqueezeNet_v1_1)
{
114 115
    if (backend == DNN_BACKEND_INFERENCE_ENGINE && target == DNN_TARGET_OPENCL_FP16)
        throw SkipTestException("");
A
Alexander Alekhin 已提交
116
    processNet("dnn/squeezenet_v1.1.caffemodel", "dnn/squeezenet_v1.1.prototxt",
117
            "squeezenet_v1_1.yml", Mat(cv::Size(227, 227), CV_32FC3));
A
Alexander Alekhin 已提交
118 119 120 121
}

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

PERF_TEST_P_(DNNTestNetwork, ENet)
{
130
    if (backend == DNN_BACKEND_INFERENCE_ENGINE) 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
PERF_TEST_P_(DNNTestNetwork, SSD)
{
137
    if (backend == DNN_BACKEND_INFERENCE_ENGINE) throw SkipTestException("");
A
Alexander Alekhin 已提交
138
    processNet("dnn/VGG_ILSVRC2016_SSD_300x300_iter_440000.caffemodel", "dnn/ssd_vgg16.prototxt", "disabled",
139
            Mat(cv::Size(300, 300), CV_32FC3));
A
Alexander Alekhin 已提交
140
}
A
Alexander Alekhin 已提交
141

D
Dmitry Kurtaev 已提交
142 143
PERF_TEST_P_(DNNTestNetwork, OpenFace)
{
144 145 146
    if (backend == DNN_BACKEND_HALIDE ||
        backend == DNN_BACKEND_INFERENCE_ENGINE && target != DNN_TARGET_CPU)
        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 154 155
    if (backend == DNN_BACKEND_HALIDE ||
        backend == DNN_BACKEND_INFERENCE_ENGINE && target != DNN_TARGET_CPU)
        throw SkipTestException("");
D
Dmitry Kurtaev 已提交
156
    processNet("dnn/MobileNetSSD_deploy.caffemodel", "dnn/MobileNetSSD_deploy.prototxt", "",
157
            Mat(cv::Size(300, 300), CV_32FC3));
D
Dmitry Kurtaev 已提交
158 159 160 161
}

PERF_TEST_P_(DNNTestNetwork, MobileNet_SSD_TensorFlow)
{
162
    if (backend == DNN_BACKEND_DEFAULT && target == DNN_TARGET_OPENCL ||
163 164
        backend == DNN_BACKEND_HALIDE ||
        backend == DNN_BACKEND_INFERENCE_ENGINE && target != DNN_TARGET_CPU)
165
        throw SkipTestException("");
D
Dmitry Kurtaev 已提交
166
    processNet("dnn/ssd_mobilenet_v1_coco.pb", "ssd_mobilenet_v1_coco.pbtxt", "",
167
            Mat(cv::Size(300, 300), CV_32FC3));
D
Dmitry Kurtaev 已提交
168 169
}

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

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", "",
183
               Mat(cv::Size(368, 368), CV_32FC3));
184 185 186 187 188 189
}

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", "",
190
               Mat(cv::Size(368, 368), CV_32FC3));
191 192 193 194 195 196 197 198
}

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", "",
199
               Mat(cv::Size(368, 368), CV_32FC3));
200 201
}

202 203 204
PERF_TEST_P_(DNNTestNetwork, opencv_face_detector)
{
    if (backend == DNN_BACKEND_HALIDE ||
205
        backend == DNN_BACKEND_INFERENCE_ENGINE && target != DNN_TARGET_CPU)
206 207
        throw SkipTestException("");
    processNet("dnn/opencv_face_detector.caffemodel", "dnn/opencv_face_detector.prototxt", "",
208
               Mat(cv::Size(300, 300), CV_32FC3));
209 210
}

211 212
PERF_TEST_P_(DNNTestNetwork, Inception_v2_SSD_TensorFlow)
{
213 214 215
    if (backend == DNN_BACKEND_HALIDE ||
        backend == DNN_BACKEND_INFERENCE_ENGINE && target != DNN_TARGET_CPU)
        throw SkipTestException("");
216
    processNet("dnn/ssd_inception_v2_coco_2017_11_17.pb", "ssd_inception_v2_coco_2017_11_17.pbtxt", "",
217
            Mat(cv::Size(300, 300), CV_32FC3));
218 219
}

220 221 222 223 224 225 226
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),
227 228
    tuple<DNNBackend, DNNTarget>(DNN_BACKEND_INFERENCE_ENGINE, DNN_TARGET_OPENCL),
    tuple<DNNBackend, DNNTarget>(DNN_BACKEND_INFERENCE_ENGINE, DNN_TARGET_OPENCL_FP16),
229 230 231 232 233 234
#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 已提交
235 236

} // namespace