Path: blob/master/modules/dnn/test/test_googlenet.cpp
16339 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/core/ocl.hpp>44#include <opencv2/ts/ocl_test.hpp>4546namespace opencv_test { namespace {4748template<typename TString>49static std::string _tf(TString filename)50{51return (getOpenCVExtraDir() + "/dnn/") + filename;52}5354typedef testing::TestWithParam<Target> Reproducibility_GoogLeNet;55TEST_P(Reproducibility_GoogLeNet, Batching)56{57Net net = readNetFromCaffe(findDataFile("dnn/bvlc_googlenet.prototxt", false),58findDataFile("dnn/bvlc_googlenet.caffemodel", false));59int targetId = GetParam();60net.setPreferableBackend(DNN_BACKEND_OPENCV);61net.setPreferableTarget(targetId);6263if (targetId == DNN_TARGET_OPENCL)64{65// Initialize network for a single image in the batch but test with batch size=2.66Mat inp = Mat(224, 224, CV_8UC3);67randu(inp, -1, 1);68net.setInput(blobFromImage(inp));69net.forward();70}7172std::vector<Mat> inpMats;73inpMats.push_back( imread(_tf("googlenet_0.png")) );74inpMats.push_back( imread(_tf("googlenet_1.png")) );75ASSERT_TRUE(!inpMats[0].empty() && !inpMats[1].empty());7677net.setInput(blobFromImages(inpMats, 1.0f, Size(), Scalar(), false), "data");78Mat out = net.forward("prob");7980Mat ref = blobFromNPY(_tf("googlenet_prob.npy"));81normAssert(out, ref);82}8384TEST_P(Reproducibility_GoogLeNet, IntermediateBlobs)85{86Net net = readNetFromCaffe(findDataFile("dnn/bvlc_googlenet.prototxt", false),87findDataFile("dnn/bvlc_googlenet.caffemodel", false));88int targetId = GetParam();89net.setPreferableBackend(DNN_BACKEND_OPENCV);90net.setPreferableTarget(targetId);9192std::vector<String> blobsNames;93blobsNames.push_back("conv1/7x7_s2");94blobsNames.push_back("conv1/relu_7x7");95blobsNames.push_back("inception_4c/1x1");96blobsNames.push_back("inception_4c/relu_1x1");97std::vector<Mat> outs;98Mat in = blobFromImage(imread(_tf("googlenet_0.png")), 1.0f, Size(), Scalar(), false);99net.setInput(in, "data");100net.forward(outs, blobsNames);101CV_Assert(outs.size() == blobsNames.size());102103for (size_t i = 0; i < blobsNames.size(); i++)104{105std::string filename = blobsNames[i];106std::replace( filename.begin(), filename.end(), '/', '#');107Mat ref = blobFromNPY(_tf("googlenet_" + filename + ".npy"));108109normAssert(outs[i], ref, "", 1E-4, 1E-2);110}111}112113TEST_P(Reproducibility_GoogLeNet, SeveralCalls)114{115Net net = readNetFromCaffe(findDataFile("dnn/bvlc_googlenet.prototxt", false),116findDataFile("dnn/bvlc_googlenet.caffemodel", false));117int targetId = GetParam();118net.setPreferableBackend(DNN_BACKEND_OPENCV);119net.setPreferableTarget(targetId);120121std::vector<Mat> inpMats;122inpMats.push_back( imread(_tf("googlenet_0.png")) );123inpMats.push_back( imread(_tf("googlenet_1.png")) );124ASSERT_TRUE(!inpMats[0].empty() && !inpMats[1].empty());125126net.setInput(blobFromImages(inpMats, 1.0f, Size(), Scalar(), false), "data");127Mat out = net.forward();128129Mat ref = blobFromNPY(_tf("googlenet_prob.npy"));130normAssert(out, ref);131132std::vector<String> blobsNames;133blobsNames.push_back("conv1/7x7_s2");134std::vector<Mat> outs;135Mat in = blobFromImage(inpMats[0], 1.0f, Size(), Scalar(), false);136net.setInput(in, "data");137net.forward(outs, blobsNames);138CV_Assert(outs.size() == blobsNames.size());139140ref = blobFromNPY(_tf("googlenet_conv1#7x7_s2.npy"));141142normAssert(outs[0], ref, "", 1E-4, 1E-2);143}144145INSTANTIATE_TEST_CASE_P(/**/, Reproducibility_GoogLeNet, availableDnnTargets());146147}} // namespace148149150