4db04e0b35fc065f5d3b63d550bb8ebd389e9d4d
[platform/upstream/opencv.git] / modules / dnn / test / test_googlenet.cpp
1 /*M///////////////////////////////////////////////////////////////////////////////////////
2 //
3 //  IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
4 //
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.
8 //
9 //
10 //                           License Agreement
11 //                For Open Source Computer Vision Library
12 //
13 // Copyright (C) 2013, OpenCV Foundation, all rights reserved.
14 // Third party copyrights are property of their respective owners.
15 //
16 // Redistribution and use in source and binary forms, with or without modification,
17 // are permitted provided that the following conditions are met:
18 //
19 //   * Redistribution's of source code must retain the above copyright notice,
20 //     this list of conditions and the following disclaimer.
21 //
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.
25 //
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.
28 //
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.
39 //
40 //M*/
41
42 #include "test_precomp.hpp"
43 #include "npy_blob.hpp"
44 #include <opencv2/core/ocl.hpp>
45 #include <opencv2/ts/ocl_test.hpp>
46
47 namespace opencv_test { namespace {
48
49 template<typename TString>
50 static std::string _tf(TString filename)
51 {
52     return (getOpenCVExtraDir() + "/dnn/") + filename;
53 }
54
55 typedef testing::TestWithParam<Target> Reproducibility_GoogLeNet;
56 TEST_P(Reproducibility_GoogLeNet, Batching)
57 {
58     const int targetId = GetParam();
59     if(targetId == DNN_TARGET_OPENCL_FP16)
60         throw SkipTestException("This test does not support 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);
65
66     if (targetId == DNN_TARGET_OPENCL)
67     {
68         // Initialize network for a single image in the batch but test with batch size=2.
69         Mat inp = Mat(224, 224, CV_8UC3);
70         randu(inp, -1, 1);
71         net.setInput(blobFromImage(inp));
72         net.forward();
73     }
74
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());
79
80     net.setInput(blobFromImages(inpMats, 1.0f, Size(), Scalar(), false), "data");
81     Mat out = net.forward("prob");
82
83     Mat ref = blobFromNPY(_tf("googlenet_prob.npy"));
84     normAssert(out, ref);
85 }
86
87 TEST_P(Reproducibility_GoogLeNet, IntermediateBlobs)
88 {
89     const int targetId = GetParam();
90     if(targetId == DNN_TARGET_OPENCL_FP16)
91         throw SkipTestException("This test does not support 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);
96
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());
107
108     for (size_t i = 0; i < blobsNames.size(); i++)
109     {
110         std::string filename = blobsNames[i];
111         std::replace( filename.begin(), filename.end(), '/', '#');
112         Mat ref = blobFromNPY(_tf("googlenet_" + filename + ".npy"));
113
114         normAssert(outs[i], ref, "", 1E-4, 1E-2);
115     }
116 }
117
118 TEST_P(Reproducibility_GoogLeNet, SeveralCalls)
119 {
120     const int targetId = GetParam();
121     if(targetId == DNN_TARGET_OPENCL_FP16)
122         throw SkipTestException("This test does not support 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);
127
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());
132
133     net.setInput(blobFromImages(inpMats, 1.0f, Size(), Scalar(), false), "data");
134     Mat out = net.forward();
135
136     Mat ref = blobFromNPY(_tf("googlenet_prob.npy"));
137     normAssert(out, ref);
138
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());
146
147     ref = blobFromNPY(_tf("googlenet_conv1#7x7_s2.npy"));
148
149     normAssert(outs[0], ref, "", 1E-4, 1E-2);
150 }
151
152 INSTANTIATE_TEST_CASE_P(/**/, Reproducibility_GoogLeNet,
153     testing::ValuesIn(getAvailableTargets(DNN_BACKEND_OPENCV)));
154
155 }} // namespace