Merge pull request #9305 from dkurt:public_dnn_importer_is_deprecated
[platform/upstream/opencv.git] / modules / dnn / test / test_caffe_importer.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/dnn/shape_utils.hpp>
45
46 namespace cvtest
47 {
48
49 using namespace cv;
50 using namespace cv::dnn;
51
52 template<typename TString>
53 static std::string _tf(TString filename)
54 {
55     return (getOpenCVExtraDir() + "/dnn/") + filename;
56 }
57
58 TEST(Test_Caffe, read_gtsrb)
59 {
60     Net net = readNetFromCaffe(_tf("gtsrb.prototxt"));
61     ASSERT_FALSE(net.empty());
62 }
63
64 TEST(Test_Caffe, read_googlenet)
65 {
66     Net net = readNetFromCaffe(_tf("bvlc_googlenet.prototxt"));
67     ASSERT_FALSE(net.empty());
68 }
69
70 TEST(Reproducibility_AlexNet, Accuracy)
71 {
72     Net net;
73     {
74         const string proto = findDataFile("dnn/bvlc_alexnet.prototxt", false);
75         const string model = findDataFile("dnn/bvlc_alexnet.caffemodel", false);
76         net = readNetFromCaffe(proto, model);
77         ASSERT_FALSE(net.empty());
78     }
79
80     Mat sample = imread(_tf("grace_hopper_227.png"));
81     ASSERT_TRUE(!sample.empty());
82
83     Size inputSize(227, 227);
84
85     if (sample.size() != inputSize)
86         resize(sample, sample, inputSize);
87
88     net.setInput(blobFromImage(sample), "data");
89     Mat out = net.forward("prob");
90     Mat ref = blobFromNPY(_tf("caffe_alexnet_prob.npy"));
91     normAssert(ref, out);
92 }
93
94 #if !defined(_WIN32) || defined(_WIN64)
95 TEST(Reproducibility_FCN, Accuracy)
96 {
97     Net net;
98     {
99         const string proto = findDataFile("dnn/fcn8s-heavy-pascal.prototxt", false);
100         const string model = findDataFile("dnn/fcn8s-heavy-pascal.caffemodel", false);
101         net = readNetFromCaffe(proto, model);
102         ASSERT_FALSE(net.empty());
103     }
104
105     Mat sample = imread(_tf("street.png"));
106     ASSERT_TRUE(!sample.empty());
107
108     Size inputSize(500, 500);
109     if (sample.size() != inputSize)
110         resize(sample, sample, inputSize);
111
112     std::vector<int> layerIds;
113     std::vector<size_t> weights, blobs;
114     net.getMemoryConsumption(shape(1,3,227,227), layerIds, weights, blobs);
115
116     net.setInput(blobFromImage(sample), "data");
117     Mat out = net.forward("score");
118     Mat ref = blobFromNPY(_tf("caffe_fcn8s_prob.npy"));
119     normAssert(ref, out);
120 }
121 #endif
122
123 TEST(Reproducibility_SSD, Accuracy)
124 {
125     Net net;
126     {
127         const string proto = findDataFile("dnn/ssd_vgg16.prototxt", false);
128         const string model = findDataFile("dnn/VGG_ILSVRC2016_SSD_300x300_iter_440000.caffemodel", false);
129         net = readNetFromCaffe(proto, model);
130         ASSERT_FALSE(net.empty());
131     }
132
133     Mat sample = imread(_tf("street.png"));
134     ASSERT_TRUE(!sample.empty());
135
136     if (sample.channels() == 4)
137         cvtColor(sample, sample, COLOR_BGRA2BGR);
138
139     sample.convertTo(sample, CV_32F);
140     resize(sample, sample, Size(300, 300));
141
142     Mat in_blob = blobFromImage(sample);
143     net.setInput(in_blob, "data");
144     Mat out = net.forward("detection_out");
145
146     Mat ref = blobFromNPY(_tf("ssd_out.npy"));
147     normAssert(ref, out);
148 }
149
150 TEST(Reproducibility_ResNet50, Accuracy)
151 {
152     Net net = readNetFromCaffe(findDataFile("dnn/ResNet-50-deploy.prototxt", false),
153                                findDataFile("dnn/ResNet-50-model.caffemodel", false));
154
155     Mat input = blobFromImage(imread(_tf("googlenet_0.png")), 1, Size(224,224));
156     ASSERT_TRUE(!input.empty());
157
158     net.setInput(input);
159     Mat out = net.forward();
160
161     Mat ref = blobFromNPY(_tf("resnet50_prob.npy"));
162     normAssert(ref, out);
163 }
164
165 TEST(Reproducibility_SqueezeNet_v1_1, Accuracy)
166 {
167     Net net = readNetFromCaffe(findDataFile("dnn/squeezenet_v1.1.prototxt", false),
168                                findDataFile("dnn/squeezenet_v1.1.caffemodel", false));
169
170     Mat input = blobFromImage(imread(_tf("googlenet_0.png")), 1, Size(227,227));
171     ASSERT_TRUE(!input.empty());
172
173     net.setInput(input);
174     Mat out = net.forward();
175
176     Mat ref = blobFromNPY(_tf("squeezenet_v1.1_prob.npy"));
177     normAssert(ref, out);
178 }
179
180 TEST(Reproducibility_AlexNet_fp16, Accuracy)
181 {
182     const float l1 = 1e-5;
183     const float lInf = 2e-4;
184
185     const string proto = findDataFile("dnn/bvlc_alexnet.prototxt", false);
186     const string model = findDataFile("dnn/bvlc_alexnet.caffemodel", false);
187
188     shrinkCaffeModel(model, "bvlc_alexnet.caffemodel_fp16");
189     Net net = readNetFromCaffe(proto, "bvlc_alexnet.caffemodel_fp16");
190
191     Mat sample = imread(findDataFile("dnn/grace_hopper_227.png", false));
192
193     net.setInput(blobFromImage(sample, 1, Size(227, 227)));
194     Mat out = net.forward();
195     Mat ref = blobFromNPY(findDataFile("dnn/caffe_alexnet_prob.npy", false));
196     normAssert(ref, out, "", l1, lInf);
197 }
198
199 TEST(Reproducibility_GoogLeNet_fp16, Accuracy)
200 {
201     const float l1 = 1e-5;
202     const float lInf = 3e-3;
203
204     const string proto = findDataFile("dnn/bvlc_googlenet.prototxt", false);
205     const string model = findDataFile("dnn/bvlc_googlenet.caffemodel", false);
206
207     shrinkCaffeModel(model, "bvlc_googlenet.caffemodel_fp16");
208     Net net = readNetFromCaffe(proto, "bvlc_googlenet.caffemodel_fp16");
209
210     std::vector<Mat> inpMats;
211     inpMats.push_back( imread(_tf("googlenet_0.png")) );
212     inpMats.push_back( imread(_tf("googlenet_1.png")) );
213     ASSERT_TRUE(!inpMats[0].empty() && !inpMats[1].empty());
214
215     net.setInput(blobFromImages(inpMats), "data");
216     Mat out = net.forward("prob");
217
218     Mat ref = blobFromNPY(_tf("googlenet_prob.npy"));
219     normAssert(out, ref, "", l1, lInf);
220 }
221
222 }