|
| 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 | +#include "perf_precomp.hpp" |
| 9 | +#include "opencv2/core/ocl.hpp" |
| 10 | + |
| 11 | +#include "opencv2/dnn/shape_utils.hpp" |
| 12 | + |
| 13 | +namespace |
| 14 | +{ |
| 15 | + |
| 16 | +#ifdef HAVE_HALIDE |
| 17 | +#define TEST_DNN_BACKEND DNN_BACKEND_DEFAULT, DNN_BACKEND_HALIDE |
| 18 | +#else |
| 19 | +#define TEST_DNN_BACKEND DNN_BACKEND_DEFAULT |
| 20 | +#endif |
| 21 | +#define TEST_DNN_TARGET DNN_TARGET_CPU, DNN_TARGET_OPENCL |
| 22 | + |
| 23 | +CV_ENUM(DNNBackend, DNN_BACKEND_DEFAULT, DNN_BACKEND_HALIDE) |
| 24 | +CV_ENUM(DNNTarget, DNN_TARGET_CPU, DNN_TARGET_OPENCL) |
| 25 | + |
| 26 | +class DNNTestNetwork : public ::perf::TestBaseWithParam< tuple<DNNBackend, DNNTarget> > |
| 27 | +{ |
| 28 | +public: |
| 29 | + dnn::Backend backend; |
| 30 | + dnn::Target target; |
| 31 | + |
| 32 | + dnn::Net net; |
| 33 | + |
| 34 | + void processNet(std::string weights, std::string proto, std::string halide_scheduler, |
| 35 | + int inWidth, int inHeight, const std::string& outputLayer, |
| 36 | + const std::string& framework) |
| 37 | + { |
| 38 | + backend = (dnn::Backend)(int)get<0>(GetParam()); |
| 39 | + target = (dnn::Target)(int)get<1>(GetParam()); |
| 40 | + |
| 41 | + if (backend == DNN_BACKEND_DEFAULT && target == DNN_TARGET_OPENCL) |
| 42 | + { |
| 43 | +#if 0 //defined(HAVE_OPENCL) |
| 44 | + if (!cv::ocl::useOpenCL()) |
| 45 | +#endif |
| 46 | + { |
| 47 | + throw ::SkipTestException("OpenCL is not available/disabled in OpenCV"); |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + Mat input(inHeight, inWidth, CV_32FC3); |
| 52 | + randu(input, 0.0f, 1.0f); |
| 53 | + |
| 54 | + |
| 55 | + weights = findDataFile(weights, false); |
| 56 | + if (!proto.empty()) |
| 57 | + proto = findDataFile(proto, false); |
| 58 | + if (!halide_scheduler.empty() && backend == DNN_BACKEND_HALIDE) |
| 59 | + halide_scheduler = findDataFile(std::string("dnn/halide_scheduler_") + (target == DNN_TARGET_OPENCL ? "opencl_" : "") + halide_scheduler, true); |
| 60 | + if (framework == "caffe") |
| 61 | + { |
| 62 | + net = cv::dnn::readNetFromCaffe(proto, weights); |
| 63 | + } |
| 64 | + else if (framework == "torch") |
| 65 | + { |
| 66 | + net = cv::dnn::readNetFromTorch(weights); |
| 67 | + } |
| 68 | + else if (framework == "tensorflow") |
| 69 | + { |
| 70 | + net = cv::dnn::readNetFromTensorflow(weights); |
| 71 | + } |
| 72 | + else |
| 73 | + CV_Error(Error::StsNotImplemented, "Unknown framework " + framework); |
| 74 | + |
| 75 | + net.setInput(blobFromImage(input, 1.0, Size(), Scalar(), false)); |
| 76 | + net.setPreferableBackend(backend); |
| 77 | + net.setPreferableTarget(target); |
| 78 | + if (backend == DNN_BACKEND_HALIDE) |
| 79 | + { |
| 80 | + net.setHalideScheduler(halide_scheduler); |
| 81 | + } |
| 82 | + |
| 83 | + MatShape netInputShape = shape(1, 3, inHeight, inWidth); |
| 84 | + size_t weightsMemory = 0, blobsMemory = 0; |
| 85 | + net.getMemoryConsumption(netInputShape, weightsMemory, blobsMemory); |
| 86 | + int64 flops = net.getFLOPS(netInputShape); |
| 87 | + |
| 88 | + net.forward(outputLayer); // warmup |
| 89 | + |
| 90 | + std::cout << "Memory consumption:" << std::endl; |
| 91 | + std::cout << " Weights(parameters): " << divUp(weightsMemory, 1u<<20) << " Mb" << std::endl; |
| 92 | + std::cout << " Blobs: " << divUp(blobsMemory, 1u<<20) << " Mb" << std::endl; |
| 93 | + std::cout << "Calculation complexity: " << flops * 1e-9 << " GFlops" << std::endl; |
| 94 | + |
| 95 | + PERF_SAMPLE_BEGIN() |
| 96 | + net.forward(); |
| 97 | + PERF_SAMPLE_END() |
| 98 | + |
| 99 | + SANITY_CHECK_NOTHING(); |
| 100 | + } |
| 101 | +}; |
| 102 | + |
| 103 | + |
| 104 | +PERF_TEST_P_(DNNTestNetwork, AlexNet) |
| 105 | +{ |
| 106 | + processNet("dnn/bvlc_alexnet.caffemodel", "dnn/bvlc_alexnet.prototxt", |
| 107 | + "alexnet.yml", 227, 227, "prob", "caffe"); |
| 108 | +} |
| 109 | + |
| 110 | +PERF_TEST_P_(DNNTestNetwork, GoogLeNet) |
| 111 | +{ |
| 112 | + processNet("dnn/bvlc_googlenet.caffemodel", "dnn/bvlc_googlenet.prototxt", |
| 113 | + "", 224, 224, "prob", "caffe"); |
| 114 | +} |
| 115 | + |
| 116 | +PERF_TEST_P_(DNNTestNetwork, ResNet50) |
| 117 | +{ |
| 118 | + processNet("dnn/ResNet-50-model.caffemodel", "dnn/ResNet-50-deploy.prototxt", |
| 119 | + "resnet_50.yml", 224, 224, "prob", "caffe"); |
| 120 | +} |
| 121 | + |
| 122 | +PERF_TEST_P_(DNNTestNetwork, SqueezeNet_v1_1) |
| 123 | +{ |
| 124 | + processNet("dnn/squeezenet_v1.1.caffemodel", "dnn/squeezenet_v1.1.prototxt", |
| 125 | + "squeezenet_v1_1.yml", 227, 227, "prob", "caffe"); |
| 126 | +} |
| 127 | + |
| 128 | +PERF_TEST_P_(DNNTestNetwork, Inception_5h) |
| 129 | +{ |
| 130 | + processNet("dnn/tensorflow_inception_graph.pb", "", |
| 131 | + "inception_5h.yml", |
| 132 | + 224, 224, "softmax2", "tensorflow"); |
| 133 | +} |
| 134 | + |
| 135 | +PERF_TEST_P_(DNNTestNetwork, ENet) |
| 136 | +{ |
| 137 | + processNet("dnn/Enet-model-best.net", "", "enet.yml", |
| 138 | + 512, 256, "l367_Deconvolution", "torch"); |
| 139 | +} |
| 140 | + |
| 141 | + |
| 142 | +INSTANTIATE_TEST_CASE_P(/*nothing*/, DNNTestNetwork, |
| 143 | + testing::Combine( |
| 144 | + ::testing::Values(TEST_DNN_BACKEND), |
| 145 | + DNNTarget::all() |
| 146 | + ) |
| 147 | +); |
| 148 | + |
| 149 | +} // namespace |
0 commit comments