1 /*M///////////////////////////////////////////////////////////////////////////////////////
3 // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
5 // By downloading, copying, installing or using the software you agree to this license.
6 // If you do not agree to this license, do not download, install,
7 // copy or use the software.
11 // For Open Source Computer Vision Library
13 // Copyright (C) 2013, OpenCV Foundation, all rights reserved.
14 // Third party copyrights are property of their respective owners.
16 // Redistribution and use in source and binary forms, with or without modification,
17 // are permitted provided that the following conditions are met:
19 // * Redistribution's of source code must retain the above copyright notice,
20 // this list of conditions and the following disclaimer.
22 // * Redistribution's in binary form must reproduce the above copyright notice,
23 // this list of conditions and the following disclaimer in the documentation
24 // and/or other materials provided with the distribution.
26 // * The name of the copyright holders may not be used to endorse or promote products
27 // derived from this software without specific prior written permission.
29 // This software is provided by the copyright holders and contributors "as is" and
30 // any express or implied warranties, including, but not limited to, the implied
31 // warranties of merchantability and fitness for a particular purpose are disclaimed.
32 // In no event shall the Intel Corporation or contributors be liable for any direct,
33 // indirect, incidental, special, exemplary, or consequential damages
34 // (including, but not limited to, procurement of substitute goods or services;
35 // loss of use, data, or profits; or business interruption) however caused
36 // and on any theory of liability, whether in contract, strict liability,
37 // or tort (including negligence or otherwise) arising in any way out of
38 // the use of this software, even if advised of the possibility of such damage.
42 #include "test_precomp.hpp"
43 #include "npy_blob.hpp"
44 #include <opencv2/core/ocl.hpp>
45 #include <opencv2/ts/ocl_test.hpp>
47 namespace opencv_test { namespace {
49 template<typename TString>
50 static std::string _tf(TString filename)
52 return (getOpenCVExtraDir() + "/dnn/") + filename;
55 typedef testing::TestWithParam<Target> Reproducibility_GoogLeNet;
56 TEST_P(Reproducibility_GoogLeNet, Batching)
58 const int targetId = GetParam();
59 if (targetId == DNN_TARGET_OPENCL_FP16)
60 applyTestTag(CV_TEST_TAG_DNN_SKIP_OPENCL_FP16);
61 Net net = readNetFromCaffe(findDataFile("dnn/bvlc_googlenet.prototxt"),
62 findDataFile("dnn/bvlc_googlenet.caffemodel", false));
63 net.setPreferableBackend(DNN_BACKEND_OPENCV);
64 net.setPreferableTarget(targetId);
66 if (targetId == DNN_TARGET_OPENCL)
68 // Initialize network for a single image in the batch but test with batch size=2.
69 Mat inp = Mat(224, 224, CV_8UC3);
71 net.setInput(blobFromImage(inp));
75 std::vector<Mat> inpMats;
76 inpMats.push_back( imread(_tf("googlenet_0.png")) );
77 inpMats.push_back( imread(_tf("googlenet_1.png")) );
78 ASSERT_TRUE(!inpMats[0].empty() && !inpMats[1].empty());
80 net.setInput(blobFromImages(inpMats, 1.0f, Size(), Scalar(), false), "data");
81 Mat out = net.forward("prob");
83 Mat ref = blobFromNPY(_tf("googlenet_prob.npy"));
87 TEST_P(Reproducibility_GoogLeNet, IntermediateBlobs)
89 const int targetId = GetParam();
90 if (targetId == DNN_TARGET_OPENCL_FP16)
91 applyTestTag(CV_TEST_TAG_DNN_SKIP_OPENCL_FP16);
92 Net net = readNetFromCaffe(findDataFile("dnn/bvlc_googlenet.prototxt"),
93 findDataFile("dnn/bvlc_googlenet.caffemodel", false));
94 net.setPreferableBackend(DNN_BACKEND_OPENCV);
95 net.setPreferableTarget(targetId);
97 std::vector<String> blobsNames;
98 blobsNames.push_back("conv1/7x7_s2");
99 blobsNames.push_back("conv1/relu_7x7");
100 blobsNames.push_back("inception_4c/1x1");
101 blobsNames.push_back("inception_4c/relu_1x1");
102 std::vector<Mat> outs;
103 Mat in = blobFromImage(imread(_tf("googlenet_0.png")), 1.0f, Size(), Scalar(), false);
104 net.setInput(in, "data");
105 net.forward(outs, blobsNames);
106 CV_Assert(outs.size() == blobsNames.size());
108 for (size_t i = 0; i < blobsNames.size(); i++)
110 std::string filename = blobsNames[i];
111 std::replace( filename.begin(), filename.end(), '/', '#');
112 Mat ref = blobFromNPY(_tf("googlenet_" + filename + ".npy"));
114 normAssert(outs[i], ref, "", 1E-4, 1E-2);
118 TEST_P(Reproducibility_GoogLeNet, SeveralCalls)
120 const int targetId = GetParam();
121 if (targetId == DNN_TARGET_OPENCL_FP16)
122 applyTestTag(CV_TEST_TAG_DNN_SKIP_OPENCL_FP16);
123 Net net = readNetFromCaffe(findDataFile("dnn/bvlc_googlenet.prototxt"),
124 findDataFile("dnn/bvlc_googlenet.caffemodel", false));
125 net.setPreferableBackend(DNN_BACKEND_OPENCV);
126 net.setPreferableTarget(targetId);
128 std::vector<Mat> inpMats;
129 inpMats.push_back( imread(_tf("googlenet_0.png")) );
130 inpMats.push_back( imread(_tf("googlenet_1.png")) );
131 ASSERT_TRUE(!inpMats[0].empty() && !inpMats[1].empty());
133 net.setInput(blobFromImages(inpMats, 1.0f, Size(), Scalar(), false), "data");
134 Mat out = net.forward();
136 Mat ref = blobFromNPY(_tf("googlenet_prob.npy"));
137 normAssert(out, ref);
139 std::vector<String> blobsNames;
140 blobsNames.push_back("conv1/7x7_s2");
141 std::vector<Mat> outs;
142 Mat in = blobFromImage(inpMats[0], 1.0f, Size(), Scalar(), false);
143 net.setInput(in, "data");
144 net.forward(outs, blobsNames);
145 CV_Assert(outs.size() == blobsNames.size());
147 ref = blobFromNPY(_tf("googlenet_conv1#7x7_s2.npy"));
149 normAssert(outs[0], ref, "", 1E-4, 1E-2);
152 INSTANTIATE_TEST_CASE_P(/**/, Reproducibility_GoogLeNet,
153 testing::ValuesIn(getAvailableTargets(DNN_BACKEND_OPENCV)));