fbb90a7241c8963c76b27d470db1480b38a2b8de
[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
15 namespace cvtest
16 {
17
18 using namespace cv;
19 using namespace cv::dnn;
20
21 template<typename TString>
22 static std::string _tf(TString filename)
23 {
24     return (getOpenCVExtraDir() + "/dnn/") + filename;
25 }
26
27 TEST(Test_TensorFlow, read_inception)
28 {
29     Net net;
30     {
31         const string model = findDataFile("dnn/tensorflow_inception_graph.pb", false);
32         net = readNetFromTensorflow(model);
33         ASSERT_FALSE(net.empty());
34     }
35
36     Mat sample = imread(_tf("grace_hopper_227.png"));
37     ASSERT_TRUE(!sample.empty());
38     Mat input;
39     resize(sample, input, Size(224, 224));
40     input -= 128; // mean sub
41
42     Mat inputBlob = blobFromImage(input);
43
44     net.setInput(inputBlob, "input");
45     Mat out = net.forward("softmax2");
46
47     std::cout << out.dims << std::endl;
48 }
49
50 TEST(Test_TensorFlow, inception_accuracy)
51 {
52     Net net;
53     {
54         const string model = findDataFile("dnn/tensorflow_inception_graph.pb", false);
55         net = readNetFromTensorflow(model);
56         ASSERT_FALSE(net.empty());
57     }
58
59     Mat sample = imread(_tf("grace_hopper_227.png"));
60     ASSERT_TRUE(!sample.empty());
61     resize(sample, sample, Size(224, 224));
62     Mat inputBlob = blobFromImage(sample);
63
64     net.setInput(inputBlob, "input");
65     Mat out = net.forward("softmax2");
66
67     Mat ref = blobFromNPY(_tf("tf_inception_prob.npy"));
68
69     normAssert(ref, out);
70 }
71
72 static std::string path(const std::string& file)
73 {
74     return findDataFile("dnn/tensorflow/" + file, false);
75 }
76
77 static void runTensorFlowNet(const std::string& prefix, bool hasText = false,
78                              double l1 = 1e-5, double lInf = 1e-4,
79                              bool memoryLoad = false)
80 {
81     std::string netPath = path(prefix + "_net.pb");
82     std::string netConfig = (hasText ? path(prefix + "_net.pbtxt") : "");
83     std::string inpPath = path(prefix + "_in.npy");
84     std::string outPath = path(prefix + "_out.npy");
85
86     Net net;
87     if (memoryLoad)
88     {
89         // Load files into a memory buffers
90         string dataModel;
91         ASSERT_TRUE(readFileInMemory(netPath, dataModel));
92
93         string dataConfig;
94         if (hasText)
95             ASSERT_TRUE(readFileInMemory(netConfig, dataConfig));
96
97         net = readNetFromTensorflow(dataModel.c_str(), dataModel.size(),
98                                     dataConfig.c_str(), dataConfig.size());
99     }
100     else
101         net = readNetFromTensorflow(netPath, netConfig);
102
103     ASSERT_FALSE(net.empty());
104
105     cv::Mat input = blobFromNPY(inpPath);
106     cv::Mat target = blobFromNPY(outPath);
107
108     net.setInput(input);
109     cv::Mat output = net.forward();
110     normAssert(target, output, "", l1, lInf);
111 }
112
113 TEST(Test_TensorFlow, conv)
114 {
115     runTensorFlowNet("single_conv");
116     runTensorFlowNet("atrous_conv2d_valid");
117     runTensorFlowNet("atrous_conv2d_same");
118     runTensorFlowNet("depthwise_conv2d");
119 }
120
121 TEST(Test_TensorFlow, padding)
122 {
123     runTensorFlowNet("padding_same");
124     runTensorFlowNet("padding_valid");
125     runTensorFlowNet("spatial_padding");
126 }
127
128 TEST(Test_TensorFlow, eltwise_add_mul)
129 {
130     runTensorFlowNet("eltwise_add_mul");
131 }
132
133 TEST(Test_TensorFlow, pad_and_concat)
134 {
135     runTensorFlowNet("pad_and_concat");
136 }
137
138 TEST(Test_TensorFlow, batch_norm)
139 {
140     runTensorFlowNet("batch_norm");
141     runTensorFlowNet("fused_batch_norm");
142     runTensorFlowNet("batch_norm_text", true);
143 }
144
145 TEST(Test_TensorFlow, pooling)
146 {
147     runTensorFlowNet("max_pool_even");
148     runTensorFlowNet("max_pool_odd_valid");
149     runTensorFlowNet("max_pool_odd_same");
150 }
151
152 TEST(Test_TensorFlow, deconvolution)
153 {
154     runTensorFlowNet("deconvolution");
155 }
156
157 TEST(Test_TensorFlow, matmul)
158 {
159     runTensorFlowNet("matmul");
160 }
161
162 TEST(Test_TensorFlow, defun)
163 {
164     runTensorFlowNet("defun_dropout");
165 }
166
167 TEST(Test_TensorFlow, reshape)
168 {
169     runTensorFlowNet("shift_reshape_no_reorder");
170     runTensorFlowNet("reshape_reduce");
171     runTensorFlowNet("flatten", true);
172 }
173
174 TEST(Test_TensorFlow, fp16)
175 {
176     const float l1 = 1e-3;
177     const float lInf = 1e-2;
178     runTensorFlowNet("fp16_single_conv", false, l1, lInf);
179     runTensorFlowNet("fp16_deconvolution", false, l1, lInf);
180     runTensorFlowNet("fp16_max_pool_odd_same", false, l1, lInf);
181     runTensorFlowNet("fp16_padding_valid", false, l1, lInf);
182     runTensorFlowNet("fp16_eltwise_add_mul", false, l1, lInf);
183     runTensorFlowNet("fp16_max_pool_odd_valid", false, l1, lInf);
184     runTensorFlowNet("fp16_pad_and_concat", false, l1, lInf);
185     runTensorFlowNet("fp16_max_pool_even", false, l1, lInf);
186     runTensorFlowNet("fp16_padding_same", false, l1, lInf);
187 }
188
189 TEST(Test_TensorFlow, MobileNet_SSD)
190 {
191     std::string netPath = findDataFile("dnn/ssd_mobilenet_v1_coco.pb", false);
192     std::string netConfig = findDataFile("dnn/ssd_mobilenet_v1_coco.pbtxt", false);
193     std::string imgPath = findDataFile("dnn/street.png", false);
194
195     Mat inp;
196     resize(imread(imgPath), inp, Size(300, 300));
197     inp = blobFromImage(inp, 1.0f / 127.5, Size(), Scalar(127.5, 127.5, 127.5), true);
198
199     std::vector<String> outNames(3);
200     outNames[0] = "concat";
201     outNames[1] = "concat_1";
202     outNames[2] = "detection_out";
203
204     std::vector<Mat> target(outNames.size());
205     for (int i = 0; i < outNames.size(); ++i)
206     {
207         std::string path = findDataFile("dnn/tensorflow/ssd_mobilenet_v1_coco." + outNames[i] + ".npy", false);
208         target[i] = blobFromNPY(path);
209     }
210
211     Net net = readNetFromTensorflow(netPath, netConfig);
212     net.setInput(inp);
213
214     std::vector<Mat> output;
215     net.forward(output, outNames);
216
217     normAssert(target[0].reshape(1, 1), output[0].reshape(1, 1));
218     normAssert(target[1].reshape(1, 1), output[1].reshape(1, 1), "", 1e-5, 2e-4);
219     normAssert(target[2].reshape(1, 1), output[2].reshape(1, 1), "", 4e-5, 1e-2);
220 }
221
222 TEST(Test_TensorFlow, lstm)
223 {
224     runTensorFlowNet("lstm", true);
225 }
226
227 TEST(Test_TensorFlow, split)
228 {
229     runTensorFlowNet("split_equals");
230 }
231
232 TEST(Test_TensorFlow, resize_nearest_neighbor)
233 {
234     runTensorFlowNet("resize_nearest_neighbor");
235 }
236
237 TEST(Test_TensorFlow, memory_read)
238 {
239     double l1 = 1e-5;
240     double lInf = 1e-4;
241     runTensorFlowNet("lstm", true, l1, lInf, true);
242
243     runTensorFlowNet("batch_norm", false, l1, lInf, true);
244     runTensorFlowNet("fused_batch_norm", false, l1, lInf, true);
245     runTensorFlowNet("batch_norm_text", true, l1, lInf, true);
246 }
247
248 }