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/dnn/shape_utils.hpp>
50 using namespace cv::dnn;
52 template<typename TString>
53 static std::string _tf(TString filename)
55 return (getOpenCVExtraDir() + "/dnn/") + filename;
58 TEST(Test_Caffe, read_gtsrb)
60 Net net = readNetFromCaffe(_tf("gtsrb.prototxt"));
61 ASSERT_FALSE(net.empty());
64 TEST(Test_Caffe, read_googlenet)
66 Net net = readNetFromCaffe(_tf("bvlc_googlenet.prototxt"));
67 ASSERT_FALSE(net.empty());
70 TEST(Reproducibility_AlexNet, Accuracy)
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());
80 Mat sample = imread(_tf("grace_hopper_227.png"));
81 ASSERT_TRUE(!sample.empty());
83 Size inputSize(227, 227);
85 if (sample.size() != inputSize)
86 resize(sample, sample, inputSize);
88 net.setInput(blobFromImage(sample), "data");
89 Mat out = net.forward("prob");
90 Mat ref = blobFromNPY(_tf("caffe_alexnet_prob.npy"));
94 #if !defined(_WIN32) || defined(_WIN64)
95 TEST(Reproducibility_FCN, Accuracy)
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());
105 Mat sample = imread(_tf("street.png"));
106 ASSERT_TRUE(!sample.empty());
108 Size inputSize(500, 500);
109 if (sample.size() != inputSize)
110 resize(sample, sample, inputSize);
112 std::vector<int> layerIds;
113 std::vector<size_t> weights, blobs;
114 net.getMemoryConsumption(shape(1,3,227,227), layerIds, weights, blobs);
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);
123 TEST(Reproducibility_SSD, Accuracy)
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());
133 Mat sample = imread(_tf("street.png"));
134 ASSERT_TRUE(!sample.empty());
136 if (sample.channels() == 4)
137 cvtColor(sample, sample, COLOR_BGRA2BGR);
139 sample.convertTo(sample, CV_32F);
140 resize(sample, sample, Size(300, 300));
142 Mat in_blob = blobFromImage(sample);
143 net.setInput(in_blob, "data");
144 Mat out = net.forward("detection_out");
146 Mat ref = blobFromNPY(_tf("ssd_out.npy"));
147 normAssert(ref, out);
150 TEST(Reproducibility_ResNet50, Accuracy)
152 Net net = readNetFromCaffe(findDataFile("dnn/ResNet-50-deploy.prototxt", false),
153 findDataFile("dnn/ResNet-50-model.caffemodel", false));
155 Mat input = blobFromImage(imread(_tf("googlenet_0.png")), 1, Size(224,224));
156 ASSERT_TRUE(!input.empty());
159 Mat out = net.forward();
161 Mat ref = blobFromNPY(_tf("resnet50_prob.npy"));
162 normAssert(ref, out);
165 TEST(Reproducibility_SqueezeNet_v1_1, Accuracy)
167 Net net = readNetFromCaffe(findDataFile("dnn/squeezenet_v1.1.prototxt", false),
168 findDataFile("dnn/squeezenet_v1.1.caffemodel", false));
170 Mat input = blobFromImage(imread(_tf("googlenet_0.png")), 1, Size(227,227));
171 ASSERT_TRUE(!input.empty());
174 Mat out = net.forward();
176 Mat ref = blobFromNPY(_tf("squeezenet_v1.1_prob.npy"));
177 normAssert(ref, out);
180 TEST(Reproducibility_AlexNet_fp16, Accuracy)
182 const float l1 = 1e-5;
183 const float lInf = 2e-4;
185 const string proto = findDataFile("dnn/bvlc_alexnet.prototxt", false);
186 const string model = findDataFile("dnn/bvlc_alexnet.caffemodel", false);
188 shrinkCaffeModel(model, "bvlc_alexnet.caffemodel_fp16");
189 Net net = readNetFromCaffe(proto, "bvlc_alexnet.caffemodel_fp16");
191 Mat sample = imread(findDataFile("dnn/grace_hopper_227.png", false));
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);
199 TEST(Reproducibility_GoogLeNet_fp16, Accuracy)
201 const float l1 = 1e-5;
202 const float lInf = 3e-3;
204 const string proto = findDataFile("dnn/bvlc_googlenet.prototxt", false);
205 const string model = findDataFile("dnn/bvlc_googlenet.caffemodel", false);
207 shrinkCaffeModel(model, "bvlc_googlenet.caffemodel_fp16");
208 Net net = readNetFromCaffe(proto, "bvlc_googlenet.caffemodel_fp16");
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());
215 net.setInput(blobFromImages(inpMats), "data");
216 Mat out = net.forward("prob");
218 Mat ref = blobFromNPY(_tf("googlenet_prob.npy"));
219 normAssert(out, ref, "", l1, lInf);