add ocl accuracy test for tf mobilenet ssd
[platform/upstream/opencv.git] / modules / dnn / test / test_tf_importer.cpp
1 // This file is part of OpenCV project.
2 // It is subject to the license terms in the LICENSE file found in the top-level directory
3 // of this distribution and at http://opencv.org/license.html.
4
5 // Copyright (C) 2017, Intel Corporation, all rights reserved.
6 // Third party copyrights are property of their respective owners.
7
8 /*
9 Test for Tensorflow models loading
10 */
11
12 #include "test_precomp.hpp"
13 #include "npy_blob.hpp"
14 #include <opencv2/core/ocl.hpp>
15 #include <opencv2/ts/ocl_test.hpp>
16
17 namespace cvtest
18 {
19
20 using namespace cv;
21 using namespace cv::dnn;
22
23 template<typename TString>
24 static std::string _tf(TString filename)
25 {
26     return (getOpenCVExtraDir() + "/dnn/") + filename;
27 }
28
29 TEST(Test_TensorFlow, read_inception)
30 {
31     Net net;
32     {
33         const string model = findDataFile("dnn/tensorflow_inception_graph.pb", false);
34         net = readNetFromTensorflow(model);
35         ASSERT_FALSE(net.empty());
36     }
37
38     Mat sample = imread(_tf("grace_hopper_227.png"));
39     ASSERT_TRUE(!sample.empty());
40     Mat input;
41     resize(sample, input, Size(224, 224));
42     input -= 128; // mean sub
43
44     Mat inputBlob = blobFromImage(input);
45
46     net.setInput(inputBlob, "input");
47     Mat out = net.forward("softmax2");
48
49     std::cout << out.dims << std::endl;
50 }
51
52 TEST(Test_TensorFlow, inception_accuracy)
53 {
54     Net net;
55     {
56         const string model = findDataFile("dnn/tensorflow_inception_graph.pb", false);
57         net = readNetFromTensorflow(model);
58         ASSERT_FALSE(net.empty());
59     }
60
61     Mat sample = imread(_tf("grace_hopper_227.png"));
62     ASSERT_TRUE(!sample.empty());
63     resize(sample, sample, Size(224, 224));
64     Mat inputBlob = blobFromImage(sample);
65
66     net.setInput(inputBlob, "input");
67     Mat out = net.forward("softmax2");
68
69     Mat ref = blobFromNPY(_tf("tf_inception_prob.npy"));
70
71     normAssert(ref, out);
72 }
73
74 static std::string path(const std::string& file)
75 {
76     return findDataFile("dnn/tensorflow/" + file, false);
77 }
78
79 static void runTensorFlowNet(const std::string& prefix, bool hasText = false,
80                              double l1 = 1e-5, double lInf = 1e-4,
81                              bool memoryLoad = false)
82 {
83     std::string netPath = path(prefix + "_net.pb");
84     std::string netConfig = (hasText ? path(prefix + "_net.pbtxt") : "");
85     std::string inpPath = path(prefix + "_in.npy");
86     std::string outPath = path(prefix + "_out.npy");
87
88     Net net;
89     if (memoryLoad)
90     {
91         // Load files into a memory buffers
92         string dataModel;
93         ASSERT_TRUE(readFileInMemory(netPath, dataModel));
94
95         string dataConfig;
96         if (hasText)
97             ASSERT_TRUE(readFileInMemory(netConfig, dataConfig));
98
99         net = readNetFromTensorflow(dataModel.c_str(), dataModel.size(),
100                                     dataConfig.c_str(), dataConfig.size());
101     }
102     else
103         net = readNetFromTensorflow(netPath, netConfig);
104
105     ASSERT_FALSE(net.empty());
106
107     cv::Mat input = blobFromNPY(inpPath);
108     cv::Mat target = blobFromNPY(outPath);
109
110     net.setInput(input);
111     cv::Mat output = net.forward();
112     normAssert(target, output, "", l1, lInf);
113 }
114
115 TEST(Test_TensorFlow, conv)
116 {
117     runTensorFlowNet("single_conv");
118     runTensorFlowNet("atrous_conv2d_valid");
119     runTensorFlowNet("atrous_conv2d_same");
120     runTensorFlowNet("depthwise_conv2d");
121 }
122
123 TEST(Test_TensorFlow, padding)
124 {
125     runTensorFlowNet("padding_same");
126     runTensorFlowNet("padding_valid");
127     runTensorFlowNet("spatial_padding");
128 }
129
130 TEST(Test_TensorFlow, eltwise_add_mul)
131 {
132     runTensorFlowNet("eltwise_add_mul");
133 }
134
135 TEST(Test_TensorFlow, pad_and_concat)
136 {
137     runTensorFlowNet("pad_and_concat");
138 }
139
140 TEST(Test_TensorFlow, batch_norm)
141 {
142     runTensorFlowNet("batch_norm");
143     runTensorFlowNet("fused_batch_norm");
144     runTensorFlowNet("batch_norm_text", true);
145 }
146
147 TEST(Test_TensorFlow, pooling)
148 {
149     runTensorFlowNet("max_pool_even");
150     runTensorFlowNet("max_pool_odd_valid");
151     runTensorFlowNet("max_pool_odd_same");
152 }
153
154 TEST(Test_TensorFlow, deconvolution)
155 {
156     runTensorFlowNet("deconvolution");
157 }
158
159 TEST(Test_TensorFlow, matmul)
160 {
161     runTensorFlowNet("matmul");
162 }
163
164 TEST(Test_TensorFlow, defun)
165 {
166     runTensorFlowNet("defun_dropout");
167 }
168
169 TEST(Test_TensorFlow, reshape)
170 {
171     runTensorFlowNet("shift_reshape_no_reorder");
172     runTensorFlowNet("reshape_reduce");
173     runTensorFlowNet("flatten", true);
174 }
175
176 TEST(Test_TensorFlow, fp16)
177 {
178     const float l1 = 1e-3;
179     const float lInf = 1e-2;
180     runTensorFlowNet("fp16_single_conv", false, l1, lInf);
181     runTensorFlowNet("fp16_deconvolution", false, l1, lInf);
182     runTensorFlowNet("fp16_max_pool_odd_same", false, l1, lInf);
183     runTensorFlowNet("fp16_padding_valid", false, l1, lInf);
184     runTensorFlowNet("fp16_eltwise_add_mul", false, l1, lInf);
185     runTensorFlowNet("fp16_max_pool_odd_valid", false, l1, lInf);
186     runTensorFlowNet("fp16_pad_and_concat", false, l1, lInf);
187     runTensorFlowNet("fp16_max_pool_even", false, l1, lInf);
188     runTensorFlowNet("fp16_padding_same", false, l1, lInf);
189 }
190
191 TEST(Test_TensorFlow, MobileNet_SSD)
192 {
193     std::string netPath = findDataFile("dnn/ssd_mobilenet_v1_coco.pb", false);
194     std::string netConfig = findDataFile("dnn/ssd_mobilenet_v1_coco.pbtxt", false);
195     std::string imgPath = findDataFile("dnn/street.png", false);
196
197     Mat inp;
198     resize(imread(imgPath), inp, Size(300, 300));
199     inp = blobFromImage(inp, 1.0f / 127.5, Size(), Scalar(127.5, 127.5, 127.5), true);
200
201     std::vector<String> outNames(3);
202     outNames[0] = "concat";
203     outNames[1] = "concat_1";
204     outNames[2] = "detection_out";
205
206     std::vector<Mat> target(outNames.size());
207     for (int i = 0; i < outNames.size(); ++i)
208     {
209         std::string path = findDataFile("dnn/tensorflow/ssd_mobilenet_v1_coco." + outNames[i] + ".npy", false);
210         target[i] = blobFromNPY(path);
211     }
212
213     Net net = readNetFromTensorflow(netPath, netConfig);
214     net.setInput(inp);
215
216     std::vector<Mat> output;
217     net.forward(output, outNames);
218
219     normAssert(target[0].reshape(1, 1), output[0].reshape(1, 1));
220     normAssert(target[1].reshape(1, 1), output[1].reshape(1, 1), "", 1e-5, 2e-4);
221     normAssert(target[2].reshape(1, 1), output[2].reshape(1, 1), "", 4e-5, 1e-2);
222 }
223
224 OCL_TEST(Test_TensorFlow, MobileNet_SSD)
225 {
226     std::string netPath = findDataFile("dnn/ssd_mobilenet_v1_coco.pb", false);
227     std::string netConfig = findDataFile("dnn/ssd_mobilenet_v1_coco.pbtxt", false);
228     std::string imgPath = findDataFile("dnn/street.png", false);
229
230     Mat inp;
231     resize(imread(imgPath), inp, Size(300, 300));
232     inp = blobFromImage(inp, 1.0f / 127.5, Size(), Scalar(127.5, 127.5, 127.5), true);
233
234     std::vector<String> outNames(3);
235     outNames[0] = "concat";
236     outNames[1] = "concat_1";
237     outNames[2] = "detection_out";
238
239     std::vector<Mat> target(outNames.size());
240     for (int i = 0; i < outNames.size(); ++i)
241     {
242         std::string path = findDataFile("dnn/tensorflow/ssd_mobilenet_v1_coco." + outNames[i] + ".npy", false);
243         target[i] = blobFromNPY(path);
244     }
245
246     Net net = readNetFromTensorflow(netPath, netConfig);
247
248     net.setPreferableBackend(DNN_BACKEND_DEFAULT);
249     net.setPreferableTarget(DNN_TARGET_OPENCL);
250
251     net.setInput(inp);
252
253     std::vector<Mat> output;
254     net.forward(output, outNames);
255
256     normAssert(target[0].reshape(1, 1), output[0].reshape(1, 1));
257     normAssert(target[1].reshape(1, 1), output[1].reshape(1, 1), "", 1e-5, 2e-4);
258     normAssert(target[2].reshape(1, 1), output[2].reshape(1, 1), "", 4e-5, 1e-2);
259 }
260
261 TEST(Test_TensorFlow, lstm)
262 {
263     runTensorFlowNet("lstm", true);
264 }
265
266 TEST(Test_TensorFlow, split)
267 {
268     runTensorFlowNet("split_equals");
269 }
270
271 TEST(Test_TensorFlow, resize_nearest_neighbor)
272 {
273     runTensorFlowNet("resize_nearest_neighbor");
274 }
275
276 TEST(Test_TensorFlow, memory_read)
277 {
278     double l1 = 1e-5;
279     double lInf = 1e-4;
280     runTensorFlowNet("lstm", true, l1, lInf, true);
281
282     runTensorFlowNet("batch_norm", false, l1, lInf, true);
283     runTensorFlowNet("fused_batch_norm", false, l1, lInf, true);
284     runTensorFlowNet("batch_norm_text", true, l1, lInf, true);
285 }
286
287 }