Path: blob/master/modules/dnn/test/test_caffe_importer.cpp
16344 views
/*M///////////////////////////////////////////////////////////////////////////////////////1//2// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.3//4// By downloading, copying, installing or using the software you agree to this license.5// If you do not agree to this license, do not download, install,6// copy or use the software.7//8//9// License Agreement10// For Open Source Computer Vision Library11//12// Copyright (C) 2013, OpenCV Foundation, all rights reserved.13// Third party copyrights are property of their respective owners.14//15// Redistribution and use in source and binary forms, with or without modification,16// are permitted provided that the following conditions are met:17//18// * Redistribution's of source code must retain the above copyright notice,19// this list of conditions and the following disclaimer.20//21// * Redistribution's in binary form must reproduce the above copyright notice,22// this list of conditions and the following disclaimer in the documentation23// and/or other materials provided with the distribution.24//25// * The name of the copyright holders may not be used to endorse or promote products26// derived from this software without specific prior written permission.27//28// This software is provided by the copyright holders and contributors "as is" and29// any express or implied warranties, including, but not limited to, the implied30// warranties of merchantability and fitness for a particular purpose are disclaimed.31// In no event shall the Intel Corporation or contributors be liable for any direct,32// indirect, incidental, special, exemplary, or consequential damages33// (including, but not limited to, procurement of substitute goods or services;34// loss of use, data, or profits; or business interruption) however caused35// and on any theory of liability, whether in contract, strict liability,36// or tort (including negligence or otherwise) arising in any way out of37// the use of this software, even if advised of the possibility of such damage.38//39//M*/4041#include "test_precomp.hpp"42#include "npy_blob.hpp"43#include <opencv2/dnn/shape_utils.hpp>4445namespace opencv_test { namespace {4647template<typename TString>48static std::string _tf(TString filename)49{50return (getOpenCVExtraDir() + "/dnn/") + filename;51}5253class Test_Caffe_nets : public DNNTestLayer54{55public:56void testFaster(const std::string& proto, const std::string& model, const Mat& ref,57double scoreDiff = 0.0, double iouDiff = 0.0)58{59checkBackend();60Net net = readNetFromCaffe(findDataFile("dnn/" + proto, false),61findDataFile("dnn/" + model, false));62net.setPreferableBackend(backend);63net.setPreferableTarget(target);64Mat img = imread(findDataFile("dnn/dog416.png", false));65resize(img, img, Size(800, 600));66Mat blob = blobFromImage(img, 1.0, Size(), Scalar(102.9801, 115.9465, 122.7717), false, false);67Mat imInfo = (Mat_<float>(1, 3) << img.rows, img.cols, 1.6f);6869net.setInput(blob, "data");70net.setInput(imInfo, "im_info");71// Output has shape 1x1xNx7 where N - number of detections.72// An every detection is a vector of values [id, classId, confidence, left, top, right, bottom]73Mat out = net.forward();74scoreDiff = scoreDiff ? scoreDiff : default_l1;75iouDiff = iouDiff ? iouDiff : default_lInf;76normAssertDetections(ref, out, ("model name: " + model).c_str(), 0.8, scoreDiff, iouDiff);77}78};7980TEST(Test_Caffe, memory_read)81{82const string proto = findDataFile("dnn/bvlc_googlenet.prototxt", false);83const string model = findDataFile("dnn/bvlc_googlenet.caffemodel", false);8485string dataProto;86ASSERT_TRUE(readFileInMemory(proto, dataProto));87string dataModel;88ASSERT_TRUE(readFileInMemory(model, dataModel));8990Net net = readNetFromCaffe(dataProto.c_str(), dataProto.size());91net.setPreferableBackend(DNN_BACKEND_OPENCV);92ASSERT_FALSE(net.empty());9394Net net2 = readNetFromCaffe(dataProto.c_str(), dataProto.size(),95dataModel.c_str(), dataModel.size());96ASSERT_FALSE(net2.empty());97}9899TEST(Test_Caffe, read_gtsrb)100{101Net net = readNetFromCaffe(_tf("gtsrb.prototxt"));102ASSERT_FALSE(net.empty());103}104105TEST(Test_Caffe, read_googlenet)106{107Net net = readNetFromCaffe(_tf("bvlc_googlenet.prototxt"));108ASSERT_FALSE(net.empty());109}110111typedef testing::TestWithParam<tuple<bool, Target> > Reproducibility_AlexNet;112TEST_P(Reproducibility_AlexNet, Accuracy)113{114bool readFromMemory = get<0>(GetParam());115Net net;116{117const string proto = findDataFile("dnn/bvlc_alexnet.prototxt", false);118const string model = findDataFile("dnn/bvlc_alexnet.caffemodel", false);119if (readFromMemory)120{121string dataProto;122ASSERT_TRUE(readFileInMemory(proto, dataProto));123string dataModel;124ASSERT_TRUE(readFileInMemory(model, dataModel));125126net = readNetFromCaffe(dataProto.c_str(), dataProto.size(),127dataModel.c_str(), dataModel.size());128}129else130net = readNetFromCaffe(proto, model);131ASSERT_FALSE(net.empty());132}133134int targetId = get<1>(GetParam());135const float l1 = 1e-5;136const float lInf = (targetId == DNN_TARGET_OPENCL_FP16) ? 3e-3 : 1e-4;137138net.setPreferableBackend(DNN_BACKEND_OPENCV);139net.setPreferableTarget(targetId);140141Mat sample = imread(_tf("grace_hopper_227.png"));142ASSERT_TRUE(!sample.empty());143144net.setInput(blobFromImage(sample, 1.0f, Size(227, 227), Scalar(), false), "data");145Mat out = net.forward("prob");146Mat ref = blobFromNPY(_tf("caffe_alexnet_prob.npy"));147normAssert(ref, out, "", l1, lInf);148}149150INSTANTIATE_TEST_CASE_P(/**/, Reproducibility_AlexNet, Combine(testing::Bool(),151Values(DNN_TARGET_CPU, DNN_TARGET_OPENCL, DNN_TARGET_OPENCL_FP16)));152153#if !defined(_WIN32) || defined(_WIN64)154TEST(Reproducibility_FCN, Accuracy)155{156Net net;157{158const string proto = findDataFile("dnn/fcn8s-heavy-pascal.prototxt", false);159const string model = findDataFile("dnn/fcn8s-heavy-pascal.caffemodel", false);160net = readNetFromCaffe(proto, model);161ASSERT_FALSE(net.empty());162}163net.setPreferableBackend(DNN_BACKEND_OPENCV);164165Mat sample = imread(_tf("street.png"));166ASSERT_TRUE(!sample.empty());167168std::vector<int> layerIds;169std::vector<size_t> weights, blobs;170net.getMemoryConsumption(shape(1,3,227,227), layerIds, weights, blobs);171172net.setInput(blobFromImage(sample, 1.0f, Size(500, 500), Scalar(), false), "data");173Mat out = net.forward("score");174175Mat refData = imread(_tf("caffe_fcn8s_prob.png"), IMREAD_ANYDEPTH);176int shape[] = {1, 21, 500, 500};177Mat ref(4, shape, CV_32FC1, refData.data);178179normAssert(ref, out);180}181#endif182183TEST(Reproducibility_SSD, Accuracy)184{185Net net;186{187const string proto = findDataFile("dnn/ssd_vgg16.prototxt", false);188const string model = findDataFile("dnn/VGG_ILSVRC2016_SSD_300x300_iter_440000.caffemodel", false);189net = readNetFromCaffe(proto, model);190ASSERT_FALSE(net.empty());191}192net.setPreferableBackend(DNN_BACKEND_OPENCV);193194Mat sample = imread(_tf("street.png"));195ASSERT_TRUE(!sample.empty());196197if (sample.channels() == 4)198cvtColor(sample, sample, COLOR_BGRA2BGR);199200Mat in_blob = blobFromImage(sample, 1.0f, Size(300, 300), Scalar(), false);201net.setInput(in_blob, "data");202Mat out = net.forward("detection_out");203204Mat ref = blobFromNPY(_tf("ssd_out.npy"));205normAssertDetections(ref, out);206}207208typedef testing::TestWithParam<Target> Reproducibility_MobileNet_SSD;209TEST_P(Reproducibility_MobileNet_SSD, Accuracy)210{211const string proto = findDataFile("dnn/MobileNetSSD_deploy.prototxt", false);212const string model = findDataFile("dnn/MobileNetSSD_deploy.caffemodel", false);213Net net = readNetFromCaffe(proto, model);214int targetId = GetParam();215const float l1 = (targetId == DNN_TARGET_OPENCL_FP16) ? 1.5e-4 : 1e-5;216const float lInf = (targetId == DNN_TARGET_OPENCL_FP16) ? 4e-4 : 1e-4;217218net.setPreferableBackend(DNN_BACKEND_OPENCV);219net.setPreferableTarget(targetId);220221Mat sample = imread(_tf("street.png"));222223Mat inp = blobFromImage(sample, 1.0f / 127.5, Size(300, 300), Scalar(127.5, 127.5, 127.5), false);224net.setInput(inp);225Mat out = net.forward();226227const float scores_diff = (targetId == DNN_TARGET_OPENCL_FP16) ? 4e-4 : 1e-5;228const float boxes_iou_diff = (targetId == DNN_TARGET_OPENCL_FP16) ? 5e-3 : 1e-4;229Mat ref = blobFromNPY(_tf("mobilenet_ssd_caffe_out.npy"));230normAssertDetections(ref, out, "", 0.0, scores_diff, boxes_iou_diff);231232// Check that detections aren't preserved.233inp.setTo(0.0f);234net.setInput(inp);235out = net.forward();236out = out.reshape(1, out.total() / 7);237238const int numDetections = out.rows;239ASSERT_NE(numDetections, 0);240for (int i = 0; i < numDetections; ++i)241{242float confidence = out.ptr<float>(i)[2];243ASSERT_EQ(confidence, 0);244}245246// Check batching mode.247ref = ref.reshape(1, numDetections);248inp = blobFromImages(std::vector<Mat>(2, sample), 1.0f / 127.5, Size(300, 300), Scalar(127.5, 127.5, 127.5), false);249net.setInput(inp);250Mat outBatch = net.forward();251252// Output blob has a shape 1x1x2Nx7 where N is a number of detection for253// a single sample in batch. The first numbers of detection vectors are batch id.254outBatch = outBatch.reshape(1, outBatch.total() / 7);255EXPECT_EQ(outBatch.rows, 2 * numDetections);256normAssert(outBatch.rowRange(0, numDetections), ref, "", l1, lInf);257normAssert(outBatch.rowRange(numDetections, 2 * numDetections).colRange(1, 7), ref.colRange(1, 7),258"", l1, lInf);259}260INSTANTIATE_TEST_CASE_P(/**/, Reproducibility_MobileNet_SSD,261Values(DNN_TARGET_CPU, DNN_TARGET_OPENCL, DNN_TARGET_OPENCL_FP16));262263typedef testing::TestWithParam<Target> Reproducibility_ResNet50;264TEST_P(Reproducibility_ResNet50, Accuracy)265{266Net net = readNetFromCaffe(findDataFile("dnn/ResNet-50-deploy.prototxt", false),267findDataFile("dnn/ResNet-50-model.caffemodel", false));268269int targetId = GetParam();270net.setPreferableBackend(DNN_BACKEND_OPENCV);271net.setPreferableTarget(targetId);272273float l1 = (targetId == DNN_TARGET_OPENCL_FP16) ? 3e-5 : 1e-5;274float lInf = (targetId == DNN_TARGET_OPENCL_FP16) ? 6e-3 : 1e-4;275276Mat input = blobFromImage(imread(_tf("googlenet_0.png")), 1.0f, Size(224,224), Scalar(), false);277ASSERT_TRUE(!input.empty());278279net.setInput(input);280Mat out = net.forward();281282Mat ref = blobFromNPY(_tf("resnet50_prob.npy"));283normAssert(ref, out, "", l1, lInf);284285if (targetId == DNN_TARGET_OPENCL || targetId == DNN_TARGET_OPENCL_FP16)286{287UMat out_umat;288net.forward(out_umat);289normAssert(ref, out_umat, "out_umat", l1, lInf);290291std::vector<UMat> out_umats;292net.forward(out_umats);293normAssert(ref, out_umats[0], "out_umat_vector", l1, lInf);294}295}296INSTANTIATE_TEST_CASE_P(/**/, Reproducibility_ResNet50,297Values(DNN_TARGET_CPU, DNN_TARGET_OPENCL, DNN_TARGET_OPENCL_FP16));298299typedef testing::TestWithParam<Target> Reproducibility_SqueezeNet_v1_1;300TEST_P(Reproducibility_SqueezeNet_v1_1, Accuracy)301{302Net net = readNetFromCaffe(findDataFile("dnn/squeezenet_v1.1.prototxt", false),303findDataFile("dnn/squeezenet_v1.1.caffemodel", false));304305int targetId = GetParam();306net.setPreferableBackend(DNN_BACKEND_OPENCV);307net.setPreferableTarget(targetId);308309Mat input = blobFromImage(imread(_tf("googlenet_0.png")), 1.0f, Size(227,227), Scalar(), false, true);310ASSERT_TRUE(!input.empty());311312Mat out;313if (targetId == DNN_TARGET_OPENCL)314{315// Firstly set a wrong input blob and run the model to receive a wrong output.316// Then set a correct input blob to check CPU->GPU synchronization is working well.317net.setInput(input * 2.0f);318out = net.forward();319}320net.setInput(input);321out = net.forward();322323Mat ref = blobFromNPY(_tf("squeezenet_v1.1_prob.npy"));324normAssert(ref, out);325}326INSTANTIATE_TEST_CASE_P(/**/, Reproducibility_SqueezeNet_v1_1, availableDnnTargets());327328TEST(Reproducibility_AlexNet_fp16, Accuracy)329{330const float l1 = 1e-5;331const float lInf = 3e-3;332333const string proto = findDataFile("dnn/bvlc_alexnet.prototxt", false);334const string model = findDataFile("dnn/bvlc_alexnet.caffemodel", false);335336shrinkCaffeModel(model, "bvlc_alexnet.caffemodel_fp16");337Net net = readNetFromCaffe(proto, "bvlc_alexnet.caffemodel_fp16");338net.setPreferableBackend(DNN_BACKEND_OPENCV);339340Mat sample = imread(findDataFile("dnn/grace_hopper_227.png", false));341342net.setInput(blobFromImage(sample, 1.0f, Size(227, 227), Scalar(), false));343Mat out = net.forward();344Mat ref = blobFromNPY(findDataFile("dnn/caffe_alexnet_prob.npy", false));345normAssert(ref, out, "", l1, lInf);346}347348TEST(Reproducibility_GoogLeNet_fp16, Accuracy)349{350const float l1 = 1e-5;351const float lInf = 3e-3;352353const string proto = findDataFile("dnn/bvlc_googlenet.prototxt", false);354const string model = findDataFile("dnn/bvlc_googlenet.caffemodel", false);355356shrinkCaffeModel(model, "bvlc_googlenet.caffemodel_fp16");357Net net = readNetFromCaffe(proto, "bvlc_googlenet.caffemodel_fp16");358net.setPreferableBackend(DNN_BACKEND_OPENCV);359360std::vector<Mat> inpMats;361inpMats.push_back( imread(_tf("googlenet_0.png")) );362inpMats.push_back( imread(_tf("googlenet_1.png")) );363ASSERT_TRUE(!inpMats[0].empty() && !inpMats[1].empty());364365net.setInput(blobFromImages(inpMats, 1.0f, Size(), Scalar(), false), "data");366Mat out = net.forward("prob");367368Mat ref = blobFromNPY(_tf("googlenet_prob.npy"));369normAssert(out, ref, "", l1, lInf);370}371372// https://github.com/richzhang/colorization373TEST_P(Test_Caffe_nets, Colorization)374{375checkBackend();376Mat inp = blobFromNPY(_tf("colorization_inp.npy"));377Mat ref = blobFromNPY(_tf("colorization_out.npy"));378Mat kernel = blobFromNPY(_tf("colorization_pts_in_hull.npy"));379380const string proto = findDataFile("dnn/colorization_deploy_v2.prototxt", false);381const string model = findDataFile("dnn/colorization_release_v2.caffemodel", false);382Net net = readNetFromCaffe(proto, model);383net.setPreferableBackend(backend);384net.setPreferableTarget(target);385386net.getLayer(net.getLayerId("class8_ab"))->blobs.push_back(kernel);387net.getLayer(net.getLayerId("conv8_313_rh"))->blobs.push_back(Mat(1, 313, CV_32F, 2.606));388389net.setInput(inp);390Mat out = net.forward();391392// Reference output values are in range [-29.1, 69.5]393const double l1 = (target == DNN_TARGET_OPENCL_FP16 || target == DNN_TARGET_MYRIAD) ? 0.25 : 4e-4;394const double lInf = (target == DNN_TARGET_OPENCL_FP16 || target == DNN_TARGET_MYRIAD) ? 5.3 : 3e-3;395normAssert(out, ref, "", l1, lInf);396}397398TEST_P(Test_Caffe_nets, DenseNet_121)399{400checkBackend();401const string proto = findDataFile("dnn/DenseNet_121.prototxt", false);402const string model = findDataFile("dnn/DenseNet_121.caffemodel", false);403404Mat inp = imread(_tf("dog416.png"));405inp = blobFromImage(inp, 1.0 / 255, Size(224, 224), Scalar(), true, true);406Mat ref = blobFromNPY(_tf("densenet_121_output.npy"));407408Net net = readNetFromCaffe(proto, model);409net.setPreferableBackend(backend);410net.setPreferableTarget(target);411412net.setInput(inp);413Mat out = net.forward();414415// Reference is an array of 1000 values from a range [-6.16, 7.9]416float l1 = default_l1, lInf = default_lInf;417if (target == DNN_TARGET_OPENCL_FP16)418{419l1 = 0.017; lInf = 0.0795;420}421else if (target == DNN_TARGET_MYRIAD)422{423l1 = 0.097; lInf = 0.52;424}425normAssert(out, ref, "", l1, lInf);426}427428TEST(Test_Caffe, multiple_inputs)429{430const string proto = findDataFile("dnn/layers/net_input.prototxt", false);431Net net = readNetFromCaffe(proto);432net.setPreferableBackend(DNN_BACKEND_OPENCV);433434Mat first_image(10, 11, CV_32FC3);435Mat second_image(10, 11, CV_32FC3);436randu(first_image, -1, 1);437randu(second_image, -1, 1);438439first_image = blobFromImage(first_image);440second_image = blobFromImage(second_image);441442Mat first_image_blue_green = slice(first_image, Range::all(), Range(0, 2), Range::all(), Range::all());443Mat first_image_red = slice(first_image, Range::all(), Range(2, 3), Range::all(), Range::all());444Mat second_image_blue_green = slice(second_image, Range::all(), Range(0, 2), Range::all(), Range::all());445Mat second_image_red = slice(second_image, Range::all(), Range(2, 3), Range::all(), Range::all());446447net.setInput(first_image_blue_green, "old_style_input_blue_green");448net.setInput(first_image_red, "different_name_for_red");449net.setInput(second_image_blue_green, "input_layer_blue_green");450net.setInput(second_image_red, "old_style_input_red");451Mat out = net.forward();452453normAssert(out, first_image + second_image);454}455456TEST(Test_Caffe, shared_weights)457{458const string proto = findDataFile("dnn/layers/shared_weights.prototxt", false);459const string model = findDataFile("dnn/layers/shared_weights.caffemodel", false);460461Net net = readNetFromCaffe(proto, model);462463Mat input_1 = (Mat_<float>(2, 2) << 0., 2., 4., 6.);464Mat input_2 = (Mat_<float>(2, 2) << 1., 3., 5., 7.);465466Mat blob_1 = blobFromImage(input_1);467Mat blob_2 = blobFromImage(input_2);468469net.setInput(blob_1, "input_1");470net.setInput(blob_2, "input_2");471472Mat sum = net.forward();473474EXPECT_EQ(sum.at<float>(0,0), 12.);475EXPECT_EQ(sum.at<float>(0,1), 16.);476}477478typedef testing::TestWithParam<tuple<std::string, Target> > opencv_face_detector;479TEST_P(opencv_face_detector, Accuracy)480{481std::string proto = findDataFile("dnn/opencv_face_detector.prototxt", false);482std::string model = findDataFile(get<0>(GetParam()), false);483dnn::Target targetId = (dnn::Target)(int)get<1>(GetParam());484485Net net = readNetFromCaffe(proto, model);486Mat img = imread(findDataFile("gpu/lbpcascade/er.png", false));487Mat blob = blobFromImage(img, 1.0, Size(), Scalar(104.0, 177.0, 123.0), false, false);488489net.setPreferableBackend(DNN_BACKEND_OPENCV);490net.setPreferableTarget(targetId);491492net.setInput(blob);493// Output has shape 1x1xNx7 where N - number of detections.494// An every detection is a vector of values [id, classId, confidence, left, top, right, bottom]495Mat out = net.forward();496Mat ref = (Mat_<float>(6, 7) << 0, 1, 0.99520785, 0.80997437, 0.16379407, 0.87996572, 0.26685631,4970, 1, 0.9934696, 0.2831718, 0.50738752, 0.345781, 0.5985168,4980, 1, 0.99096733, 0.13629119, 0.24892329, 0.19756334, 0.3310290,4990, 1, 0.98977017, 0.23901358, 0.09084064, 0.29902688, 0.1769477,5000, 1, 0.97203469, 0.67965847, 0.06876482, 0.73999709, 0.1513494,5010, 1, 0.95097077, 0.51901293, 0.45863652, 0.5777427, 0.5347801);502normAssertDetections(ref, out, "", 0.5, 1e-5, 2e-4);503}504INSTANTIATE_TEST_CASE_P(Test_Caffe, opencv_face_detector,505Combine(506Values("dnn/opencv_face_detector.caffemodel",507"dnn/opencv_face_detector_fp16.caffemodel"),508Values(DNN_TARGET_CPU, DNN_TARGET_OPENCL)509)510);511512TEST_P(Test_Caffe_nets, FasterRCNN_vgg16)513{514if (backend == DNN_BACKEND_INFERENCE_ENGINE && target == DNN_TARGET_MYRIAD)515throw SkipTestException("");516static Mat ref = (Mat_<float>(3, 7) << 0, 2, 0.949398, 99.2454, 210.141, 601.205, 462.849,5170, 7, 0.997022, 481.841, 92.3218, 722.685, 175.953,5180, 12, 0.993028, 133.221, 189.377, 350.994, 563.166);519testFaster("faster_rcnn_vgg16.prototxt", "VGG16_faster_rcnn_final.caffemodel", ref);520}521522TEST_P(Test_Caffe_nets, FasterRCNN_zf)523{524if ((backend == DNN_BACKEND_INFERENCE_ENGINE && target == DNN_TARGET_OPENCL_FP16) ||525(backend == DNN_BACKEND_INFERENCE_ENGINE && target == DNN_TARGET_MYRIAD))526throw SkipTestException("");527static Mat ref = (Mat_<float>(3, 7) << 0, 2, 0.90121, 120.407, 115.83, 570.586, 528.395,5280, 7, 0.988779, 469.849, 75.1756, 718.64, 186.762,5290, 12, 0.967198, 138.588, 206.843, 329.766, 553.176);530testFaster("faster_rcnn_zf.prototxt", "ZF_faster_rcnn_final.caffemodel", ref);531}532533TEST_P(Test_Caffe_nets, RFCN)534{535if ((backend == DNN_BACKEND_INFERENCE_ENGINE && target == DNN_TARGET_OPENCL_FP16) ||536(backend == DNN_BACKEND_INFERENCE_ENGINE && target == DNN_TARGET_MYRIAD))537throw SkipTestException("");538double scoreDiff = (backend == DNN_BACKEND_OPENCV && target == DNN_TARGET_OPENCL_FP16) ? 4e-3 : default_l1;539double iouDiff = (backend == DNN_BACKEND_OPENCV && target == DNN_TARGET_OPENCL_FP16) ? 8e-2 : default_lInf;540static Mat ref = (Mat_<float>(2, 7) << 0, 7, 0.991359, 491.822, 81.1668, 702.573, 178.234,5410, 12, 0.94786, 132.093, 223.903, 338.077, 566.16);542testFaster("rfcn_pascal_voc_resnet50.prototxt", "resnet50_rfcn_final.caffemodel", ref, scoreDiff, iouDiff);543}544545INSTANTIATE_TEST_CASE_P(/**/, Test_Caffe_nets, dnnBackendsAndTargets());546547}} // namespace548549550