Path: blob/master/modules/dnn/test/test_backends.cpp
16354 views
// This file is part of OpenCV project.1// It is subject to the license terms in the LICENSE file found in the top-level directory2// of this distribution and at http://opencv.org/license.html.3//4// Copyright (C) 2018, Intel Corporation, all rights reserved.5// Third party copyrights are property of their respective owners.67#include "test_precomp.hpp"8#include "opencv2/core/ocl.hpp"910namespace opencv_test { namespace {1112class DNNTestNetwork : public DNNTestLayer13{14public:15void processNet(const std::string& weights, const std::string& proto,16Size inpSize, const std::string& outputLayer = "",17const std::string& halideScheduler = "",18double l1 = 0.0, double lInf = 0.0)19{20// Create a common input blob.21int blobSize[] = {1, 3, inpSize.height, inpSize.width};22Mat inp(4, blobSize, CV_32FC1);23randu(inp, 0.0f, 1.0f);2425processNet(weights, proto, inp, outputLayer, halideScheduler, l1, lInf);26}2728void processNet(std::string weights, std::string proto,29Mat inp, const std::string& outputLayer = "",30std::string halideScheduler = "",31double l1 = 0.0, double lInf = 0.0, double detectionConfThresh = 0.2)32{33checkBackend();34l1 = l1 ? l1 : default_l1;35lInf = lInf ? lInf : default_lInf;3637weights = findDataFile(weights, false);38if (!proto.empty())39proto = findDataFile(proto, false);4041// Create two networks - with default backend and target and a tested one.42Net netDefault = readNet(weights, proto);43netDefault.setPreferableBackend(DNN_BACKEND_OPENCV);44netDefault.setInput(inp);45Mat outDefault = netDefault.forward(outputLayer).clone();4647Net net = readNet(weights, proto);48net.setInput(inp);49net.setPreferableBackend(backend);50net.setPreferableTarget(target);51if (backend == DNN_BACKEND_HALIDE && !halideScheduler.empty())52{53halideScheduler = findDataFile(halideScheduler, false);54net.setHalideScheduler(halideScheduler);55}56Mat out = net.forward(outputLayer).clone();5758check(outDefault, out, outputLayer, l1, lInf, detectionConfThresh, "First run");5960// Test 2: change input.61float* inpData = (float*)inp.data;62for (int i = 0; i < inp.size[0] * inp.size[1]; ++i)63{64Mat slice(inp.size[2], inp.size[3], CV_32F, inpData);65cv::flip(slice, slice, 1);66inpData += slice.total();67}68netDefault.setInput(inp);69net.setInput(inp);70outDefault = netDefault.forward(outputLayer).clone();71out = net.forward(outputLayer).clone();72check(outDefault, out, outputLayer, l1, lInf, detectionConfThresh, "Second run");73}7475void check(Mat& ref, Mat& out, const std::string& outputLayer, double l1, double lInf,76double detectionConfThresh, const char* msg)77{78if (outputLayer == "detection_out")79{80if (backend == DNN_BACKEND_INFERENCE_ENGINE)81{82// Inference Engine produces detections terminated by a row which starts from -1.83out = out.reshape(1, out.total() / 7);84int numDetections = 0;85while (numDetections < out.rows && out.at<float>(numDetections, 0) != -1)86{87numDetections += 1;88}89out = out.rowRange(0, numDetections);90}91normAssertDetections(ref, out, msg, detectionConfThresh, l1, lInf);92}93else94normAssert(ref, out, msg, l1, lInf);95}96};9798TEST_P(DNNTestNetwork, AlexNet)99{100processNet("dnn/bvlc_alexnet.caffemodel", "dnn/bvlc_alexnet.prototxt",101Size(227, 227), "prob",102target == DNN_TARGET_OPENCL ? "dnn/halide_scheduler_opencl_alexnet.yml" :103"dnn/halide_scheduler_alexnet.yml");104}105106TEST_P(DNNTestNetwork, ResNet_50)107{108processNet("dnn/ResNet-50-model.caffemodel", "dnn/ResNet-50-deploy.prototxt",109Size(224, 224), "prob",110target == DNN_TARGET_OPENCL ? "dnn/halide_scheduler_opencl_resnet_50.yml" :111"dnn/halide_scheduler_resnet_50.yml");112}113114TEST_P(DNNTestNetwork, SqueezeNet_v1_1)115{116processNet("dnn/squeezenet_v1.1.caffemodel", "dnn/squeezenet_v1.1.prototxt",117Size(227, 227), "prob",118target == DNN_TARGET_OPENCL ? "dnn/halide_scheduler_opencl_squeezenet_v1_1.yml" :119"dnn/halide_scheduler_squeezenet_v1_1.yml");120}121122TEST_P(DNNTestNetwork, GoogLeNet)123{124processNet("dnn/bvlc_googlenet.caffemodel", "dnn/bvlc_googlenet.prototxt",125Size(224, 224), "prob");126}127128TEST_P(DNNTestNetwork, Inception_5h)129{130if (backend == DNN_BACKEND_INFERENCE_ENGINE) throw SkipTestException("");131processNet("dnn/tensorflow_inception_graph.pb", "", Size(224, 224), "softmax2",132target == DNN_TARGET_OPENCL ? "dnn/halide_scheduler_opencl_inception_5h.yml" :133"dnn/halide_scheduler_inception_5h.yml");134}135136TEST_P(DNNTestNetwork, ENet)137{138if ((backend == DNN_BACKEND_INFERENCE_ENGINE) ||139(backend == DNN_BACKEND_OPENCV && target == DNN_TARGET_OPENCL_FP16))140throw SkipTestException("");141processNet("dnn/Enet-model-best.net", "", Size(512, 512), "l367_Deconvolution",142target == DNN_TARGET_OPENCL ? "dnn/halide_scheduler_opencl_enet.yml" :143"dnn/halide_scheduler_enet.yml",1442e-5, 0.15);145}146147TEST_P(DNNTestNetwork, MobileNet_SSD_Caffe)148{149if (backend == DNN_BACKEND_HALIDE)150throw SkipTestException("");151Mat sample = imread(findDataFile("dnn/street.png", false));152Mat inp = blobFromImage(sample, 1.0f / 127.5, Size(300, 300), Scalar(127.5, 127.5, 127.5), false);153float diffScores = (target == DNN_TARGET_OPENCL_FP16) ? 6e-3 : 0.0;154processNet("dnn/MobileNetSSD_deploy.caffemodel", "dnn/MobileNetSSD_deploy.prototxt",155inp, "detection_out", "", diffScores);156}157158TEST_P(DNNTestNetwork, MobileNet_SSD_v1_TensorFlow)159{160if (backend == DNN_BACKEND_HALIDE)161throw SkipTestException("");162Mat sample = imread(findDataFile("dnn/street.png", false));163Mat inp = blobFromImage(sample, 1.0f, Size(300, 300), Scalar(), false);164float l1 = (target == DNN_TARGET_OPENCL_FP16 || target == DNN_TARGET_MYRIAD) ? 0.011 : 0.0;165float lInf = (target == DNN_TARGET_OPENCL_FP16 || target == DNN_TARGET_MYRIAD) ? 0.06 : 0.0;166processNet("dnn/ssd_mobilenet_v1_coco_2017_11_17.pb", "dnn/ssd_mobilenet_v1_coco_2017_11_17.pbtxt",167inp, "detection_out", "", l1, lInf);168}169170TEST_P(DNNTestNetwork, MobileNet_SSD_v2_TensorFlow)171{172if (backend == DNN_BACKEND_HALIDE)173throw SkipTestException("");174Mat sample = imread(findDataFile("dnn/street.png", false));175Mat inp = blobFromImage(sample, 1.0f, Size(300, 300), Scalar(), false);176float l1 = (target == DNN_TARGET_OPENCL_FP16 || target == DNN_TARGET_MYRIAD) ? 0.011 : 0.0;177float lInf = (target == DNN_TARGET_OPENCL_FP16 || target == DNN_TARGET_MYRIAD) ? 0.062 : 0.0;178processNet("dnn/ssd_mobilenet_v2_coco_2018_03_29.pb", "dnn/ssd_mobilenet_v2_coco_2018_03_29.pbtxt",179inp, "detection_out", "", l1, lInf, 0.25);180}181182TEST_P(DNNTestNetwork, SSD_VGG16)183{184if (backend == DNN_BACKEND_HALIDE && target == DNN_TARGET_CPU)185throw SkipTestException("");186double scoreThreshold = (target == DNN_TARGET_OPENCL_FP16 || target == DNN_TARGET_MYRIAD) ? 0.0252 : 0.0;187Mat sample = imread(findDataFile("dnn/street.png", false));188Mat inp = blobFromImage(sample, 1.0f, Size(300, 300), Scalar(), false);189processNet("dnn/VGG_ILSVRC2016_SSD_300x300_iter_440000.caffemodel",190"dnn/ssd_vgg16.prototxt", inp, "detection_out", "", scoreThreshold);191}192193TEST_P(DNNTestNetwork, OpenPose_pose_coco)194{195if (backend == DNN_BACKEND_HALIDE ||196backend == DNN_BACKEND_INFERENCE_ENGINE && target == DNN_TARGET_MYRIAD)197throw SkipTestException("");198processNet("dnn/openpose_pose_coco.caffemodel", "dnn/openpose_pose_coco.prototxt",199Size(368, 368));200}201202TEST_P(DNNTestNetwork, OpenPose_pose_mpi)203{204if (backend == DNN_BACKEND_HALIDE ||205backend == DNN_BACKEND_INFERENCE_ENGINE && target == DNN_TARGET_MYRIAD)206throw SkipTestException("");207processNet("dnn/openpose_pose_mpi.caffemodel", "dnn/openpose_pose_mpi.prototxt",208Size(368, 368));209}210211TEST_P(DNNTestNetwork, OpenPose_pose_mpi_faster_4_stages)212{213if (backend == DNN_BACKEND_HALIDE ||214backend == DNN_BACKEND_INFERENCE_ENGINE && target == DNN_TARGET_MYRIAD)215throw SkipTestException("");216// The same .caffemodel but modified .prototxt217// See https://github.com/CMU-Perceptual-Computing-Lab/openpose/blob/master/src/openpose/pose/poseParameters.cpp218processNet("dnn/openpose_pose_mpi.caffemodel", "dnn/openpose_pose_mpi_faster_4_stages.prototxt",219Size(368, 368));220}221222TEST_P(DNNTestNetwork, OpenFace)223{224#if defined(INF_ENGINE_RELEASE) && INF_ENGINE_RELEASE < 2018030000225if (backend == DNN_BACKEND_INFERENCE_ENGINE && target == DNN_TARGET_MYRIAD)226throw SkipTestException("Test is enabled starts from OpenVINO 2018R3");227#endif228if (backend == DNN_BACKEND_HALIDE ||229(backend == DNN_BACKEND_INFERENCE_ENGINE && target == DNN_TARGET_OPENCL_FP16))230throw SkipTestException("");231processNet("dnn/openface_nn4.small2.v1.t7", "", Size(96, 96), "");232}233234TEST_P(DNNTestNetwork, opencv_face_detector)235{236if (backend == DNN_BACKEND_HALIDE)237throw SkipTestException("");238Mat img = imread(findDataFile("gpu/lbpcascade/er.png", false));239Mat inp = blobFromImage(img, 1.0, Size(), Scalar(104.0, 177.0, 123.0), false, false);240processNet("dnn/opencv_face_detector.caffemodel", "dnn/opencv_face_detector.prototxt",241inp, "detection_out");242}243244TEST_P(DNNTestNetwork, Inception_v2_SSD_TensorFlow)245{246if (backend == DNN_BACKEND_HALIDE)247throw SkipTestException("");248Mat sample = imread(findDataFile("dnn/street.png", false));249Mat inp = blobFromImage(sample, 1.0f, Size(300, 300), Scalar(), false);250float l1 = (target == DNN_TARGET_OPENCL_FP16 || target == DNN_TARGET_MYRIAD) ? 0.015 : 0.0;251float lInf = (target == DNN_TARGET_OPENCL_FP16 || target == DNN_TARGET_MYRIAD) ? 0.0731 : 0.0;252processNet("dnn/ssd_inception_v2_coco_2017_11_17.pb", "dnn/ssd_inception_v2_coco_2017_11_17.pbtxt",253inp, "detection_out", "", l1, lInf);254}255256TEST_P(DNNTestNetwork, DenseNet_121)257{258if (backend == DNN_BACKEND_HALIDE)259throw SkipTestException("");260261float l1 = 0.0, lInf = 0.0;262if (target == DNN_TARGET_OPENCL_FP16)263{264l1 = 9e-3; lInf = 5e-2;265}266else if (target == DNN_TARGET_MYRIAD)267{268l1 = 6e-2; lInf = 0.27;269}270processNet("dnn/DenseNet_121.caffemodel", "dnn/DenseNet_121.prototxt", Size(224, 224), "", "", l1, lInf);271}272273TEST_P(DNNTestNetwork, FastNeuralStyle_eccv16)274{275if (backend == DNN_BACKEND_HALIDE ||276(backend == DNN_BACKEND_OPENCV && target == DNN_TARGET_OPENCL_FP16) ||277(backend == DNN_BACKEND_INFERENCE_ENGINE && target == DNN_TARGET_MYRIAD))278throw SkipTestException("");279Mat img = imread(findDataFile("dnn/googlenet_1.png", false));280Mat inp = blobFromImage(img, 1.0, Size(320, 240), Scalar(103.939, 116.779, 123.68), false, false);281// Output image has values in range [-143.526, 148.539].282float l1 = (target == DNN_TARGET_OPENCL_FP16 || target == DNN_TARGET_MYRIAD) ? 0.3 : 4e-5;283float lInf = (target == DNN_TARGET_OPENCL_FP16 || target == DNN_TARGET_MYRIAD) ? 7.0 : 2e-3;284processNet("dnn/fast_neural_style_eccv16_starry_night.t7", "", inp, "", "", l1, lInf);285}286287INSTANTIATE_TEST_CASE_P(/*nothing*/, DNNTestNetwork, dnnBackendsAndTargets(true, true, false, true));288289}} // namespace290291292