Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Tetragramm
GitHub Repository: Tetragramm/opencv
Path: blob/master/modules/dnn/test/test_googlenet.cpp
16339 views
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
Net net = readNetFromCaffe(findDataFile("dnn/bvlc_googlenet.prototxt", false),
59
findDataFile("dnn/bvlc_googlenet.caffemodel", false));
60
int targetId = GetParam();
61
net.setPreferableBackend(DNN_BACKEND_OPENCV);
62
net.setPreferableTarget(targetId);
63
64
if (targetId == DNN_TARGET_OPENCL)
65
{
66
// Initialize network for a single image in the batch but test with batch size=2.
67
Mat inp = Mat(224, 224, CV_8UC3);
68
randu(inp, -1, 1);
69
net.setInput(blobFromImage(inp));
70
net.forward();
71
}
72
73
std::vector<Mat> inpMats;
74
inpMats.push_back( imread(_tf("googlenet_0.png")) );
75
inpMats.push_back( imread(_tf("googlenet_1.png")) );
76
ASSERT_TRUE(!inpMats[0].empty() && !inpMats[1].empty());
77
78
net.setInput(blobFromImages(inpMats, 1.0f, Size(), Scalar(), false), "data");
79
Mat out = net.forward("prob");
80
81
Mat ref = blobFromNPY(_tf("googlenet_prob.npy"));
82
normAssert(out, ref);
83
}
84
85
TEST_P(Reproducibility_GoogLeNet, IntermediateBlobs)
86
{
87
Net net = readNetFromCaffe(findDataFile("dnn/bvlc_googlenet.prototxt", false),
88
findDataFile("dnn/bvlc_googlenet.caffemodel", false));
89
int targetId = GetParam();
90
net.setPreferableBackend(DNN_BACKEND_OPENCV);
91
net.setPreferableTarget(targetId);
92
93
std::vector<String> blobsNames;
94
blobsNames.push_back("conv1/7x7_s2");
95
blobsNames.push_back("conv1/relu_7x7");
96
blobsNames.push_back("inception_4c/1x1");
97
blobsNames.push_back("inception_4c/relu_1x1");
98
std::vector<Mat> outs;
99
Mat in = blobFromImage(imread(_tf("googlenet_0.png")), 1.0f, Size(), Scalar(), false);
100
net.setInput(in, "data");
101
net.forward(outs, blobsNames);
102
CV_Assert(outs.size() == blobsNames.size());
103
104
for (size_t i = 0; i < blobsNames.size(); i++)
105
{
106
std::string filename = blobsNames[i];
107
std::replace( filename.begin(), filename.end(), '/', '#');
108
Mat ref = blobFromNPY(_tf("googlenet_" + filename + ".npy"));
109
110
normAssert(outs[i], ref, "", 1E-4, 1E-2);
111
}
112
}
113
114
TEST_P(Reproducibility_GoogLeNet, SeveralCalls)
115
{
116
Net net = readNetFromCaffe(findDataFile("dnn/bvlc_googlenet.prototxt", false),
117
findDataFile("dnn/bvlc_googlenet.caffemodel", false));
118
int targetId = GetParam();
119
net.setPreferableBackend(DNN_BACKEND_OPENCV);
120
net.setPreferableTarget(targetId);
121
122
std::vector<Mat> inpMats;
123
inpMats.push_back( imread(_tf("googlenet_0.png")) );
124
inpMats.push_back( imread(_tf("googlenet_1.png")) );
125
ASSERT_TRUE(!inpMats[0].empty() && !inpMats[1].empty());
126
127
net.setInput(blobFromImages(inpMats, 1.0f, Size(), Scalar(), false), "data");
128
Mat out = net.forward();
129
130
Mat ref = blobFromNPY(_tf("googlenet_prob.npy"));
131
normAssert(out, ref);
132
133
std::vector<String> blobsNames;
134
blobsNames.push_back("conv1/7x7_s2");
135
std::vector<Mat> outs;
136
Mat in = blobFromImage(inpMats[0], 1.0f, Size(), Scalar(), false);
137
net.setInput(in, "data");
138
net.forward(outs, blobsNames);
139
CV_Assert(outs.size() == blobsNames.size());
140
141
ref = blobFromNPY(_tf("googlenet_conv1#7x7_s2.npy"));
142
143
normAssert(outs[0], ref, "", 1E-4, 1E-2);
144
}
145
146
INSTANTIATE_TEST_CASE_P(/**/, Reproducibility_GoogLeNet, availableDnnTargets());
147
148
}} // namespace
149
150