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.
5 // Copyright (C) 2017, Intel Corporation, all rights reserved.
6 // Third party copyrights are property of their respective owners.
9 Test for Tensorflow models loading
12 #include "test_precomp.hpp"
13 #include "npy_blob.hpp"
14 #include <opencv2/core/ocl.hpp>
15 #include <opencv2/ts/ocl_test.hpp>
21 using namespace cv::dnn;
23 template<typename TString>
24 static std::string _tf(TString filename)
26 return (getOpenCVExtraDir() + "/dnn/") + filename;
29 TEST(Test_TensorFlow, read_inception)
33 const string model = findDataFile("dnn/tensorflow_inception_graph.pb", false);
34 net = readNetFromTensorflow(model);
35 ASSERT_FALSE(net.empty());
38 Mat sample = imread(_tf("grace_hopper_227.png"));
39 ASSERT_TRUE(!sample.empty());
41 resize(sample, input, Size(224, 224));
42 input -= 128; // mean sub
44 Mat inputBlob = blobFromImage(input);
46 net.setInput(inputBlob, "input");
47 Mat out = net.forward("softmax2");
49 std::cout << out.dims << std::endl;
52 TEST(Test_TensorFlow, inception_accuracy)
56 const string model = findDataFile("dnn/tensorflow_inception_graph.pb", false);
57 net = readNetFromTensorflow(model);
58 ASSERT_FALSE(net.empty());
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);
66 net.setInput(inputBlob, "input");
67 Mat out = net.forward("softmax2");
69 Mat ref = blobFromNPY(_tf("tf_inception_prob.npy"));
74 static std::string path(const std::string& file)
76 return findDataFile("dnn/tensorflow/" + file, false);
79 static void runTensorFlowNet(const std::string& prefix, bool hasText = false,
80 double l1 = 1e-5, double lInf = 1e-4,
81 bool memoryLoad = false)
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");
91 // Load files into a memory buffers
93 ASSERT_TRUE(readFileInMemory(netPath, dataModel));
97 ASSERT_TRUE(readFileInMemory(netConfig, dataConfig));
99 net = readNetFromTensorflow(dataModel.c_str(), dataModel.size(),
100 dataConfig.c_str(), dataConfig.size());
103 net = readNetFromTensorflow(netPath, netConfig);
105 ASSERT_FALSE(net.empty());
107 cv::Mat input = blobFromNPY(inpPath);
108 cv::Mat target = blobFromNPY(outPath);
111 cv::Mat output = net.forward();
112 normAssert(target, output, "", l1, lInf);
115 TEST(Test_TensorFlow, conv)
117 runTensorFlowNet("single_conv");
118 runTensorFlowNet("atrous_conv2d_valid");
119 runTensorFlowNet("atrous_conv2d_same");
120 runTensorFlowNet("depthwise_conv2d");
123 TEST(Test_TensorFlow, padding)
125 runTensorFlowNet("padding_same");
126 runTensorFlowNet("padding_valid");
127 runTensorFlowNet("spatial_padding");
130 TEST(Test_TensorFlow, eltwise_add_mul)
132 runTensorFlowNet("eltwise_add_mul");
135 TEST(Test_TensorFlow, pad_and_concat)
137 runTensorFlowNet("pad_and_concat");
140 TEST(Test_TensorFlow, batch_norm)
142 runTensorFlowNet("batch_norm");
143 runTensorFlowNet("fused_batch_norm");
144 runTensorFlowNet("batch_norm_text", true);
147 TEST(Test_TensorFlow, pooling)
149 runTensorFlowNet("max_pool_even");
150 runTensorFlowNet("max_pool_odd_valid");
151 runTensorFlowNet("max_pool_odd_same");
154 TEST(Test_TensorFlow, deconvolution)
156 runTensorFlowNet("deconvolution");
159 TEST(Test_TensorFlow, matmul)
161 runTensorFlowNet("matmul");
164 TEST(Test_TensorFlow, defun)
166 runTensorFlowNet("defun_dropout");
169 TEST(Test_TensorFlow, reshape)
171 runTensorFlowNet("shift_reshape_no_reorder");
172 runTensorFlowNet("reshape_reduce");
173 runTensorFlowNet("flatten", true);
176 TEST(Test_TensorFlow, fp16)
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);
191 TEST(Test_TensorFlow, MobileNet_SSD)
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);
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);
201 std::vector<String> outNames(3);
202 outNames[0] = "concat";
203 outNames[1] = "concat_1";
204 outNames[2] = "detection_out";
206 std::vector<Mat> target(outNames.size());
207 for (int i = 0; i < outNames.size(); ++i)
209 std::string path = findDataFile("dnn/tensorflow/ssd_mobilenet_v1_coco." + outNames[i] + ".npy", false);
210 target[i] = blobFromNPY(path);
213 Net net = readNetFromTensorflow(netPath, netConfig);
216 std::vector<Mat> output;
217 net.forward(output, outNames);
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);
224 OCL_TEST(Test_TensorFlow, MobileNet_SSD)
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);
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);
234 std::vector<String> outNames(3);
235 outNames[0] = "concat";
236 outNames[1] = "concat_1";
237 outNames[2] = "detection_out";
239 std::vector<Mat> target(outNames.size());
240 for (int i = 0; i < outNames.size(); ++i)
242 std::string path = findDataFile("dnn/tensorflow/ssd_mobilenet_v1_coco." + outNames[i] + ".npy", false);
243 target[i] = blobFromNPY(path);
246 Net net = readNetFromTensorflow(netPath, netConfig);
248 net.setPreferableBackend(DNN_BACKEND_DEFAULT);
249 net.setPreferableTarget(DNN_TARGET_OPENCL);
253 std::vector<Mat> output;
254 net.forward(output, outNames);
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);
261 TEST(Test_TensorFlow, lstm)
263 runTensorFlowNet("lstm", true);
266 TEST(Test_TensorFlow, split)
268 runTensorFlowNet("split_equals");
271 TEST(Test_TensorFlow, resize_nearest_neighbor)
273 runTensorFlowNet("resize_nearest_neighbor");
276 TEST(Test_TensorFlow, memory_read)
280 runTensorFlowNet("lstm", true, l1, lInf, true);
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);