opencv/modules/dnn/test/test_tf_importer.cpp

184 lines
4.3 KiB
C++
Raw Normal View History

// This file is part of OpenCV project.
// It is subject to the license terms in the LICENSE file found in the top-level directory
// of this distribution and at http://opencv.org/license.html.
2017-08-25 19:45:03 +08:00
// Copyright (C) 2017, Intel Corporation, all rights reserved.
// Third party copyrights are property of their respective owners.
/*
Test for Tensorflow models loading
*/
#include "test_precomp.hpp"
#include "npy_blob.hpp"
namespace cvtest
{
using namespace cv;
using namespace cv::dnn;
template<typename TString>
static std::string _tf(TString filename)
{
return (getOpenCVExtraDir() + "/dnn/") + filename;
}
TEST(Test_TensorFlow, read_inception)
{
Net net;
{
const string model = findDataFile("dnn/tensorflow_inception_graph.pb", false);
net = readNetFromTensorflow(model);
ASSERT_FALSE(net.empty());
}
Mat sample = imread(_tf("grace_hopper_227.png"));
ASSERT_TRUE(!sample.empty());
Mat input;
resize(sample, input, Size(224, 224));
input -= 128; // mean sub
Mat inputBlob = blobFromImage(input);
net.setInput(inputBlob, "input");
Mat out = net.forward("softmax2");
std::cout << out.dims << std::endl;
}
TEST(Test_TensorFlow, inception_accuracy)
{
Net net;
{
const string model = findDataFile("dnn/tensorflow_inception_graph.pb", false);
net = readNetFromTensorflow(model);
ASSERT_FALSE(net.empty());
}
Mat sample = imread(_tf("grace_hopper_227.png"));
ASSERT_TRUE(!sample.empty());
resize(sample, sample, Size(224, 224));
Mat inputBlob = blobFromImage(sample);
net.setInput(inputBlob, "input");
Mat out = net.forward("softmax2");
Mat ref = blobFromNPY(_tf("tf_inception_prob.npy"));
normAssert(ref, out);
}
2017-08-01 23:21:47 +08:00
static std::string path(const std::string& file)
{
return findDataFile("dnn/tensorflow/" + file, false);
}
static void runTensorFlowNet(const std::string& prefix,
double l1 = 1e-5, double lInf = 1e-4)
2017-08-01 23:21:47 +08:00
{
std::string netPath = path(prefix + "_net.pb");
std::string inpPath = path(prefix + "_in.npy");
std::string outPath = path(prefix + "_out.npy");
Net net = readNetFromTensorflow(netPath);
cv::Mat input = blobFromNPY(inpPath);
cv::Mat target = blobFromNPY(outPath);
net.setInput(input);
cv::Mat output = net.forward();
normAssert(target, output, "", l1, lInf);
2017-08-01 23:21:47 +08:00
}
2017-09-14 03:18:02 +08:00
TEST(Test_TensorFlow, conv)
2017-08-01 23:21:47 +08:00
{
runTensorFlowNet("single_conv");
runTensorFlowNet("atrous_conv2d_valid");
runTensorFlowNet("atrous_conv2d_same");
2017-09-14 03:18:02 +08:00
runTensorFlowNet("depthwise_conv2d");
2017-08-01 23:21:47 +08:00
}
TEST(Test_TensorFlow, padding)
{
runTensorFlowNet("padding_same");
runTensorFlowNet("padding_valid");
2017-09-22 17:12:03 +08:00
runTensorFlowNet("spatial_padding");
2017-08-01 23:21:47 +08:00
}
TEST(Test_TensorFlow, eltwise_add_mul)
{
runTensorFlowNet("eltwise_add_mul");
}
TEST(Test_TensorFlow, pad_and_concat)
{
runTensorFlowNet("pad_and_concat");
}
2017-09-14 03:18:02 +08:00
TEST(Test_TensorFlow, batch_norm)
2017-08-01 23:21:47 +08:00
{
2017-09-14 03:18:02 +08:00
runTensorFlowNet("batch_norm");
2017-08-01 23:21:47 +08:00
runTensorFlowNet("fused_batch_norm");
}
TEST(Test_TensorFlow, pooling)
{
runTensorFlowNet("max_pool_even");
runTensorFlowNet("max_pool_odd_valid");
runTensorFlowNet("max_pool_odd_same");
}
2017-08-11 21:23:41 +08:00
TEST(Test_TensorFlow, deconvolution)
{
runTensorFlowNet("deconvolution");
}
2017-09-14 03:18:02 +08:00
TEST(Test_TensorFlow, matmul)
{
runTensorFlowNet("matmul");
}
TEST(Test_TensorFlow, defun)
{
runTensorFlowNet("defun_dropout");
}
TEST(Test_TensorFlow, reshape)
{
runTensorFlowNet("shift_reshape_no_reorder");
2017-08-25 19:45:03 +08:00
runTensorFlowNet("reshape_reduce");
}
TEST(Test_TensorFlow, fp16)
{
const float l1 = 1e-3;
const float lInf = 1e-2;
runTensorFlowNet("fp16_single_conv", l1, lInf);
runTensorFlowNet("fp16_deconvolution", l1, lInf);
runTensorFlowNet("fp16_max_pool_odd_same", l1, lInf);
runTensorFlowNet("fp16_padding_valid", l1, lInf);
runTensorFlowNet("fp16_eltwise_add_mul", l1, lInf);
runTensorFlowNet("fp16_max_pool_odd_valid", l1, lInf);
runTensorFlowNet("fp16_pad_and_concat", l1, lInf);
runTensorFlowNet("fp16_max_pool_even", l1, lInf);
runTensorFlowNet("fp16_padding_same", l1, lInf);
}
2017-08-25 19:45:03 +08:00
TEST(Test_TensorFlow, lstm)
{
runTensorFlowNet("lstm");
}
2017-10-03 03:44:42 +08:00
TEST(Test_TensorFlow, split)
{
runTensorFlowNet("split_equals");
}
2017-10-06 19:24:01 +08:00
TEST(Test_TensorFlow, resize_nearest_neighbor)
{
runTensorFlowNet("resize_nearest_neighbor");
}
}