Merge pull request #10992 from dkurt:dnn_opencl_tests

This commit is contained in:
Alexander Alekhin 2018-03-09 10:06:39 +00:00
commit 5b868ccd82
11 changed files with 304 additions and 522 deletions

View File

@ -273,6 +273,9 @@ public:
for(int i = 0; i < outCn; i++ )
biasvec[i] = biasMat.at<float>(i);
}
#ifdef HAVE_OPENCL
convolutionOp.release();
#endif
}
bool setActivation(const Ptr<ActivationLayer>& layer)

View File

@ -267,6 +267,11 @@ public:
};
#ifdef HAVE_OPENCL
void finalize(const std::vector<Mat*> &inputs, std::vector<Mat> &outputs)
{
innerProductOp.release();
}
bool forward_ocl(InputArrayOfArrays inps, OutputArrayOfArrays outs, InputArrayOfArrays internals)
{
std::vector<UMat> inputs;

View File

@ -96,6 +96,11 @@ public:
}
#ifdef HAVE_OPENCL
void finalize(const std::vector<Mat*> &inputs, std::vector<Mat> &outputs)
{
lrnOp.release();
}
bool forward_ocl(InputArrayOfArrays inps, OutputArrayOfArrays outs, OutputArrayOfArrays internals)
{
std::vector<UMat> inputs;

View File

@ -127,6 +127,10 @@ public:
}
getConvPoolPaddings(inp, out, kernel, stride, padMode, Size(1, 1), pad);
#ifdef HAVE_OPENCL
poolOp.release();
#endif
}
virtual bool supportBackend(int backendId)

View File

@ -94,6 +94,11 @@ public:
}
#ifdef HAVE_OPENCL
virtual void finalize(const std::vector<Mat*> &inputs, std::vector<Mat> &outputs)
{
softmaxOp.release();
}
bool forward_ocl(InputArrayOfArrays inps, OutputArrayOfArrays outs, OutputArrayOfArrays itns)
{
std::vector<UMat> inputs;

View File

@ -10,9 +10,6 @@
namespace opencv_test { namespace {
CV_ENUM(DNNBackend, DNN_BACKEND_DEFAULT, DNN_BACKEND_HALIDE, DNN_BACKEND_INFERENCE_ENGINE)
CV_ENUM(DNNTarget, DNN_TARGET_CPU, DNN_TARGET_OPENCL)
static void loadNet(const std::string& weights, const std::string& proto,
const std::string& framework, Net* net)
{

View File

@ -42,8 +42,6 @@
#include "test_precomp.hpp"
#include "npy_blob.hpp"
#include <opencv2/dnn/shape_utils.hpp>
#include <opencv2/core/ocl.hpp>
#include <opencv2/ts/ocl_test.hpp>
namespace opencv_test { namespace {
@ -83,10 +81,10 @@ TEST(Test_Caffe, read_googlenet)
ASSERT_FALSE(net.empty());
}
typedef testing::TestWithParam<bool> Reproducibility_AlexNet;
typedef testing::TestWithParam<tuple<bool, DNNTarget> > Reproducibility_AlexNet;
TEST_P(Reproducibility_AlexNet, Accuracy)
{
bool readFromMemory = GetParam();
bool readFromMemory = get<0>(GetParam());
Net net;
{
const string proto = findDataFile("dnn/bvlc_alexnet.prototxt", false);
@ -106,42 +104,7 @@ TEST_P(Reproducibility_AlexNet, Accuracy)
ASSERT_FALSE(net.empty());
}
Mat sample = imread(_tf("grace_hopper_227.png"));
ASSERT_TRUE(!sample.empty());
net.setInput(blobFromImage(sample, 1.0f, Size(227, 227), Scalar(), false), "data");
Mat out = net.forward("prob");
Mat ref = blobFromNPY(_tf("caffe_alexnet_prob.npy"));
normAssert(ref, out);
}
INSTANTIATE_TEST_CASE_P(Test_Caffe, Reproducibility_AlexNet, testing::Bool());
typedef testing::TestWithParam<bool> Reproducibility_OCL_AlexNet;
OCL_TEST_P(Reproducibility_OCL_AlexNet, Accuracy)
{
bool readFromMemory = GetParam();
Net net;
{
const string proto = findDataFile("dnn/bvlc_alexnet.prototxt", false);
const string model = findDataFile("dnn/bvlc_alexnet.caffemodel", false);
if (readFromMemory)
{
string dataProto;
ASSERT_TRUE(readFileInMemory(proto, dataProto));
string dataModel;
ASSERT_TRUE(readFileInMemory(model, dataModel));
net = readNetFromCaffe(dataProto.c_str(), dataProto.size(),
dataModel.c_str(), dataModel.size());
}
else
net = readNetFromCaffe(proto, model);
ASSERT_FALSE(net.empty());
}
net.setPreferableBackend(DNN_BACKEND_DEFAULT);
net.setPreferableTarget(DNN_TARGET_OPENCL);
net.setPreferableTarget(get<1>(GetParam()));
Mat sample = imread(_tf("grace_hopper_227.png"));
ASSERT_TRUE(!sample.empty());
@ -152,7 +115,7 @@ OCL_TEST_P(Reproducibility_OCL_AlexNet, Accuracy)
normAssert(ref, out);
}
OCL_INSTANTIATE_TEST_CASE_P(Test_Caffe, Reproducibility_OCL_AlexNet, testing::Bool());
INSTANTIATE_TEST_CASE_P(/**/, Reproducibility_AlexNet, Combine(testing::Bool(), availableDnnTargets()));
#if !defined(_WIN32) || defined(_WIN64)
TEST(Reproducibility_FCN, Accuracy)
@ -207,12 +170,15 @@ TEST(Reproducibility_SSD, Accuracy)
normAssert(ref, out);
}
TEST(Reproducibility_MobileNet_SSD, Accuracy)
typedef testing::TestWithParam<DNNTarget> Reproducibility_MobileNet_SSD;
TEST_P(Reproducibility_MobileNet_SSD, Accuracy)
{
const string proto = findDataFile("dnn/MobileNetSSD_deploy.prototxt", false);
const string model = findDataFile("dnn/MobileNetSSD_deploy.caffemodel", false);
Net net = readNetFromCaffe(proto, model);
net.setPreferableTarget(GetParam());
Mat sample = imread(_tf("street.png"));
Mat inp = blobFromImage(sample, 1.0f / 127.5, Size(300, 300), Scalar(127.5, 127.5, 127.5), false);
@ -226,70 +192,39 @@ TEST(Reproducibility_MobileNet_SSD, Accuracy)
inp.setTo(0.0f);
net.setInput(inp);
out = net.forward();
out = out.reshape(1, out.total() / 7);
const int numDetections = out.size[2];
const int numDetections = out.rows;
ASSERT_NE(numDetections, 0);
for (int i = 0; i < numDetections; ++i)
{
float confidence = out.ptr<float>(0, 0, i)[2];
float confidence = out.ptr<float>(i)[2];
ASSERT_EQ(confidence, 0);
}
}
OCL_TEST(Reproducibility_MobileNet_SSD, Accuracy)
{
const string proto = findDataFile("dnn/MobileNetSSD_deploy.prototxt", false);
const string model = findDataFile("dnn/MobileNetSSD_deploy.caffemodel", false);
Net net = readNetFromCaffe(proto, model);
net.setPreferableBackend(DNN_BACKEND_DEFAULT);
net.setPreferableTarget(DNN_TARGET_OPENCL);
Mat sample = imread(_tf("street.png"));
Mat inp = blobFromImage(sample, 1.0f / 127.5, Size(300, 300), Scalar(127.5, 127.5, 127.5), false);
// Check batching mode.
ref = ref.reshape(1, numDetections);
inp = blobFromImages(std::vector<Mat>(2, sample), 1.0f / 127.5, Size(300, 300), Scalar(127.5, 127.5, 127.5), false);
net.setInput(inp);
Mat out = net.forward();
Mat outBatch = net.forward();
Mat ref = blobFromNPY(_tf("mobilenet_ssd_caffe_out.npy"));
normAssert(ref, out);
// Check that detections aren't preserved.
inp.setTo(0.0f);
net.setInput(inp);
out = net.forward();
const int numDetections = out.size[2];
ASSERT_NE(numDetections, 0);
for (int i = 0; i < numDetections; ++i)
{
float confidence = out.ptr<float>(0, 0, i)[2];
ASSERT_EQ(confidence, 0);
}
// Output blob has a shape 1x1x2Nx7 where N is a number of detection for
// a single sample in batch. The first numbers of detection vectors are batch id.
outBatch = outBatch.reshape(1, outBatch.total() / 7);
EXPECT_EQ(outBatch.rows, 2 * numDetections);
normAssert(outBatch.rowRange(0, numDetections), ref);
normAssert(outBatch.rowRange(numDetections, 2 * numDetections).colRange(1, 7), ref.colRange(1, 7));
}
INSTANTIATE_TEST_CASE_P(/**/, Reproducibility_MobileNet_SSD, availableDnnTargets());
TEST(Reproducibility_ResNet50, Accuracy)
typedef testing::TestWithParam<DNNTarget> Reproducibility_ResNet50;
TEST_P(Reproducibility_ResNet50, Accuracy)
{
Net net = readNetFromCaffe(findDataFile("dnn/ResNet-50-deploy.prototxt", false),
findDataFile("dnn/ResNet-50-model.caffemodel", false));
Mat input = blobFromImage(imread(_tf("googlenet_0.png")), 1.0f, Size(224,224), Scalar(), false);
ASSERT_TRUE(!input.empty());
net.setInput(input);
Mat out = net.forward();
Mat ref = blobFromNPY(_tf("resnet50_prob.npy"));
normAssert(ref, out);
}
OCL_TEST(Reproducibility_ResNet50, Accuracy)
{
Net net = readNetFromCaffe(findDataFile("dnn/ResNet-50-deploy.prototxt", false),
findDataFile("dnn/ResNet-50-model.caffemodel", false));
net.setPreferableBackend(DNN_BACKEND_DEFAULT);
net.setPreferableTarget(DNN_TARGET_OPENCL);
int targetId = GetParam();
net.setPreferableTarget(targetId);
Mat input = blobFromImage(imread(_tf("googlenet_0.png")), 1.0f, Size(224,224), Scalar(), false);
ASSERT_TRUE(!input.empty());
@ -300,52 +235,46 @@ OCL_TEST(Reproducibility_ResNet50, Accuracy)
Mat ref = blobFromNPY(_tf("resnet50_prob.npy"));
normAssert(ref, out);
UMat out_umat;
net.forward(out_umat);
normAssert(ref, out_umat, "out_umat");
if (targetId == DNN_TARGET_OPENCL)
{
UMat out_umat;
net.forward(out_umat);
normAssert(ref, out_umat, "out_umat");
std::vector<UMat> out_umats;
net.forward(out_umats);
normAssert(ref, out_umats[0], "out_umat_vector");
std::vector<UMat> out_umats;
net.forward(out_umats);
normAssert(ref, out_umats[0], "out_umat_vector");
}
}
INSTANTIATE_TEST_CASE_P(/**/, Reproducibility_ResNet50, availableDnnTargets());
TEST(Reproducibility_SqueezeNet_v1_1, Accuracy)
typedef testing::TestWithParam<DNNTarget> Reproducibility_SqueezeNet_v1_1;
TEST_P(Reproducibility_SqueezeNet_v1_1, Accuracy)
{
Net net = readNetFromCaffe(findDataFile("dnn/squeezenet_v1.1.prototxt", false),
findDataFile("dnn/squeezenet_v1.1.caffemodel", false));
Mat input = blobFromImage(imread(_tf("googlenet_0.png")), 1.0f, Size(227,227), Scalar(), false);
ASSERT_TRUE(!input.empty());
net.setInput(input);
Mat out = net.forward();
Mat ref = blobFromNPY(_tf("squeezenet_v1.1_prob.npy"));
normAssert(ref, out);
}
OCL_TEST(Reproducibility_SqueezeNet_v1_1, Accuracy)
{
Net net = readNetFromCaffe(findDataFile("dnn/squeezenet_v1.1.prototxt", false),
findDataFile("dnn/squeezenet_v1.1.caffemodel", false));
net.setPreferableBackend(DNN_BACKEND_DEFAULT);
net.setPreferableTarget(DNN_TARGET_OPENCL);
int targetId = GetParam();
net.setPreferableTarget(targetId);
Mat input = blobFromImage(imread(_tf("googlenet_0.png")), 1.0f, Size(227,227), Scalar(), false);
ASSERT_TRUE(!input.empty());
// Firstly set a wrong input blob and run the model to receive a wrong output.
net.setInput(input * 2.0f);
Mat out = net.forward();
// Then set a correct input blob to check CPU->GPU synchronization is working well.
Mat out;
if (targetId == DNN_TARGET_OPENCL)
{
// Firstly set a wrong input blob and run the model to receive a wrong output.
// Then set a correct input blob to check CPU->GPU synchronization is working well.
net.setInput(input * 2.0f);
out = net.forward();
}
net.setInput(input);
out = net.forward();
Mat ref = blobFromNPY(_tf("squeezenet_v1.1_prob.npy"));
normAssert(ref, out);
}
INSTANTIATE_TEST_CASE_P(/**/, Reproducibility_SqueezeNet_v1_1, availableDnnTargets());
TEST(Reproducibility_AlexNet_fp16, Accuracy)
{
@ -456,7 +385,6 @@ TEST(Test_Caffe, multiple_inputs)
normAssert(out, first_image + second_image);
}
CV_ENUM(DNNTarget, DNN_TARGET_CPU, DNN_TARGET_OPENCL)
typedef testing::TestWithParam<tuple<std::string, DNNTarget> > opencv_face_detector;
TEST_P(opencv_face_detector, Accuracy)
{

View File

@ -77,6 +77,10 @@ OCL_TEST(Reproducibility_GoogLeNet, Accuracy)
net.setPreferableBackend(DNN_BACKEND_DEFAULT);
net.setPreferableTarget(DNN_TARGET_OPENCL);
// Initialize network for a single image in the batch but test with batch size=2.
net.setInput(blobFromImage(Mat(224, 224, CV_8UC3)));
net.forward();
std::vector<Mat> inpMats;
inpMats.push_back( imread(_tf("googlenet_0.png")) );
inpMats.push_back( imread(_tf("googlenet_1.png")) );

View File

@ -44,12 +44,31 @@
#include "opencv2/ts.hpp"
#include "opencv2/ts/ts_perf.hpp"
#include "opencv2/core/utility.hpp"
#include "opencv2/core/ocl.hpp"
#include "opencv2/dnn.hpp"
#include "test_common.hpp"
namespace opencv_test {
using namespace cv::dnn;
CV_ENUM(DNNBackend, DNN_BACKEND_DEFAULT, DNN_BACKEND_HALIDE, DNN_BACKEND_INFERENCE_ENGINE)
CV_ENUM(DNNTarget, DNN_TARGET_CPU, DNN_TARGET_OPENCL)
static testing::internal::ParamGenerator<DNNTarget> availableDnnTargets()
{
static std::vector<DNNTarget> targets;
if (targets.empty())
{
targets.push_back(DNN_TARGET_CPU);
#ifdef HAVE_OPENCL
if (cv::ocl::useOpenCL())
targets.push_back(DNN_TARGET_OPENCL);
#endif
}
return testing::ValuesIn(targets);
}
}
#endif

View File

@ -11,8 +11,6 @@ Test for Tensorflow models loading
#include "test_precomp.hpp"
#include "npy_blob.hpp"
#include <opencv2/core/ocl.hpp>
#include <opencv2/ts/ocl_test.hpp>
namespace opencv_test
{
@ -115,97 +113,185 @@ static void runTensorFlowNet(const std::string& prefix, int targetId = DNN_TARGE
normAssert(target, output, "", l1, lInf);
}
TEST(Test_TensorFlow, conv)
typedef testing::TestWithParam<DNNTarget> Test_TensorFlow_layers;
TEST_P(Test_TensorFlow_layers, conv)
{
runTensorFlowNet("single_conv");
runTensorFlowNet("atrous_conv2d_valid");
runTensorFlowNet("atrous_conv2d_same");
runTensorFlowNet("depthwise_conv2d");
int targetId = GetParam();
runTensorFlowNet("single_conv", targetId);
runTensorFlowNet("atrous_conv2d_valid", targetId);
runTensorFlowNet("atrous_conv2d_same", targetId);
runTensorFlowNet("depthwise_conv2d", targetId);
}
TEST(Test_TensorFlow, padding)
TEST_P(Test_TensorFlow_layers, padding)
{
runTensorFlowNet("padding_same");
runTensorFlowNet("padding_valid");
runTensorFlowNet("spatial_padding");
int targetId = GetParam();
runTensorFlowNet("padding_same", targetId);
runTensorFlowNet("padding_valid", targetId);
runTensorFlowNet("spatial_padding", targetId);
}
TEST(Test_TensorFlow, eltwise_add_mul)
TEST_P(Test_TensorFlow_layers, eltwise_add_mul)
{
runTensorFlowNet("eltwise_add_mul");
runTensorFlowNet("eltwise_add_mul", GetParam());
}
OCL_TEST(Test_TensorFlow, eltwise_add_mul)
TEST_P(Test_TensorFlow_layers, pad_and_concat)
{
runTensorFlowNet("eltwise_add_mul", DNN_TARGET_OPENCL);
runTensorFlowNet("pad_and_concat", GetParam());
}
TEST(Test_TensorFlow, pad_and_concat)
TEST_P(Test_TensorFlow_layers, batch_norm)
{
runTensorFlowNet("pad_and_concat");
int targetId = GetParam();
runTensorFlowNet("batch_norm", targetId);
runTensorFlowNet("fused_batch_norm", targetId);
runTensorFlowNet("batch_norm_text", targetId, true);
runTensorFlowNet("mvn_batch_norm", targetId);
runTensorFlowNet("mvn_batch_norm_1x1", targetId);
}
TEST(Test_TensorFlow, batch_norm)
TEST_P(Test_TensorFlow_layers, pooling)
{
runTensorFlowNet("batch_norm");
runTensorFlowNet("fused_batch_norm");
runTensorFlowNet("batch_norm_text", DNN_TARGET_CPU, true);
runTensorFlowNet("mvn_batch_norm");
runTensorFlowNet("mvn_batch_norm_1x1");
int targetId = GetParam();
runTensorFlowNet("max_pool_even", targetId);
runTensorFlowNet("max_pool_odd_valid", targetId);
runTensorFlowNet("ave_pool_same", targetId);
runTensorFlowNet("max_pool_odd_same", targetId);
}
OCL_TEST(Test_TensorFlow, batch_norm)
TEST_P(Test_TensorFlow_layers, deconvolution)
{
runTensorFlowNet("batch_norm", DNN_TARGET_OPENCL);
runTensorFlowNet("fused_batch_norm", DNN_TARGET_OPENCL);
runTensorFlowNet("batch_norm_text", DNN_TARGET_OPENCL, true);
int targetId = GetParam();
runTensorFlowNet("deconvolution", targetId);
runTensorFlowNet("deconvolution_same", targetId);
runTensorFlowNet("deconvolution_stride_2_same", targetId);
runTensorFlowNet("deconvolution_adj_pad_valid", targetId);
runTensorFlowNet("deconvolution_adj_pad_same", targetId);
}
TEST(Test_TensorFlow, pooling)
TEST_P(Test_TensorFlow_layers, matmul)
{
runTensorFlowNet("max_pool_even");
runTensorFlowNet("max_pool_odd_valid");
runTensorFlowNet("max_pool_odd_same");
runTensorFlowNet("ave_pool_same");
int targetId = GetParam();
runTensorFlowNet("matmul", targetId);
runTensorFlowNet("nhwc_reshape_matmul", targetId);
runTensorFlowNet("nhwc_transpose_reshape_matmul", targetId);
}
TEST(Test_TensorFlow, deconvolution)
TEST_P(Test_TensorFlow_layers, reshape)
{
runTensorFlowNet("deconvolution");
runTensorFlowNet("deconvolution_same");
runTensorFlowNet("deconvolution_stride_2_same");
runTensorFlowNet("deconvolution_adj_pad_valid");
runTensorFlowNet("deconvolution_adj_pad_same");
int targetId = GetParam();
runTensorFlowNet("shift_reshape_no_reorder", targetId);
runTensorFlowNet("reshape_reduce", targetId);
runTensorFlowNet("flatten", targetId, true);
}
OCL_TEST(Test_TensorFlow, deconvolution)
INSTANTIATE_TEST_CASE_P(/**/, Test_TensorFlow_layers, availableDnnTargets());
typedef testing::TestWithParam<DNNTarget> Test_TensorFlow_nets;
TEST_P(Test_TensorFlow_nets, MobileNet_SSD)
{
runTensorFlowNet("deconvolution", DNN_TARGET_OPENCL);
runTensorFlowNet("deconvolution_same", DNN_TARGET_OPENCL);
runTensorFlowNet("deconvolution_stride_2_same", DNN_TARGET_OPENCL);
runTensorFlowNet("deconvolution_adj_pad_valid", DNN_TARGET_OPENCL);
runTensorFlowNet("deconvolution_adj_pad_same", DNN_TARGET_OPENCL);
std::string netPath = findDataFile("dnn/ssd_mobilenet_v1_coco.pb", false);
std::string netConfig = findDataFile("dnn/ssd_mobilenet_v1_coco.pbtxt", false);
std::string imgPath = findDataFile("dnn/street.png", false);
Mat inp;
resize(imread(imgPath), inp, Size(300, 300));
inp = blobFromImage(inp, 1.0f / 127.5, Size(), Scalar(127.5, 127.5, 127.5), true);
std::vector<String> outNames(3);
outNames[0] = "concat";
outNames[1] = "concat_1";
outNames[2] = "detection_out";
std::vector<Mat> target(outNames.size());
for (int i = 0; i < outNames.size(); ++i)
{
std::string path = findDataFile("dnn/tensorflow/ssd_mobilenet_v1_coco." + outNames[i] + ".npy", false);
target[i] = blobFromNPY(path);
}
Net net = readNetFromTensorflow(netPath, netConfig);
net.setPreferableTarget(GetParam());
net.setInput(inp);
std::vector<Mat> output;
net.forward(output, outNames);
normAssert(target[0].reshape(1, 1), output[0].reshape(1, 1), "", 1e-5, 1.5e-4);
normAssert(target[1].reshape(1, 1), output[1].reshape(1, 1), "", 1e-5, 3e-4);
normAssert(target[2].reshape(1, 1), output[2].reshape(1, 1), "", 4e-5, 1e-2);
}
TEST(Test_TensorFlow, matmul)
TEST_P(Test_TensorFlow_nets, Inception_v2_SSD)
{
runTensorFlowNet("matmul");
runTensorFlowNet("nhwc_reshape_matmul");
runTensorFlowNet("nhwc_transpose_reshape_matmul");
std::string proto = findDataFile("dnn/ssd_inception_v2_coco_2017_11_17.pbtxt", false);
std::string model = findDataFile("dnn/ssd_inception_v2_coco_2017_11_17.pb", false);
Net net = readNetFromTensorflow(model, proto);
Mat img = imread(findDataFile("dnn/street.png", false));
Mat blob = blobFromImage(img, 1.0f / 127.5, Size(300, 300), Scalar(127.5, 127.5, 127.5), true, false);
net.setPreferableTarget(GetParam());
net.setInput(blob);
// Output has shape 1x1xNx7 where N - number of detections.
// An every detection is a vector of values [id, classId, confidence, left, top, right, bottom]
Mat out = net.forward();
out = out.reshape(1, out.total() / 7);
Mat detections;
for (int i = 0; i < out.rows; ++i)
{
if (out.at<float>(i, 2) > 0.5)
detections.push_back(out.row(i).colRange(1, 7));
}
Mat ref = (Mat_<float>(5, 6) << 1, 0.90176028, 0.19872092, 0.36311883, 0.26461923, 0.63498729,
3, 0.93569964, 0.64865261, 0.45906419, 0.80675775, 0.65708131,
3, 0.75838411, 0.44668293, 0.45907149, 0.49459291, 0.52197015,
10, 0.95932811, 0.38349164, 0.32528657, 0.40387636, 0.39165527,
10, 0.93973452, 0.66561931, 0.37841269, 0.68074018, 0.42907384);
normAssert(detections, ref);
}
TEST_P(Test_TensorFlow_nets, opencv_face_detector_uint8)
{
std::string proto = findDataFile("dnn/opencv_face_detector.pbtxt", false);
std::string model = findDataFile("dnn/opencv_face_detector_uint8.pb", false);
Net net = readNetFromTensorflow(model, proto);
Mat img = imread(findDataFile("gpu/lbpcascade/er.png", false));
Mat blob = blobFromImage(img, 1.0, Size(), Scalar(104.0, 177.0, 123.0), false, false);
net.setPreferableTarget(GetParam());
net.setInput(blob);
// Output has shape 1x1xNx7 where N - number of detections.
// An every detection is a vector of values [id, classId, confidence, left, top, right, bottom]
Mat out = net.forward();
// References are from test for Caffe model.
Mat ref = (Mat_<float>(6, 5) << 0.99520785, 0.80997437, 0.16379407, 0.87996572, 0.26685631,
0.9934696, 0.2831718, 0.50738752, 0.345781, 0.5985168,
0.99096733, 0.13629119, 0.24892329, 0.19756334, 0.3310290,
0.98977017, 0.23901358, 0.09084064, 0.29902688, 0.1769477,
0.97203469, 0.67965847, 0.06876482, 0.73999709, 0.1513494,
0.95097077, 0.51901293, 0.45863652, 0.5777427, 0.5347801);
normAssert(out.reshape(1, out.total() / 7).rowRange(0, 6).colRange(2, 7), ref, "", 2.8e-4, 3.4e-3);
}
INSTANTIATE_TEST_CASE_P(/**/, Test_TensorFlow_nets, availableDnnTargets());
TEST(Test_TensorFlow, defun)
{
runTensorFlowNet("defun_dropout");
}
TEST(Test_TensorFlow, reshape)
{
runTensorFlowNet("shift_reshape_no_reorder");
runTensorFlowNet("reshape_reduce");
runTensorFlowNet("flatten", DNN_TARGET_CPU, true);
}
TEST(Test_TensorFlow, fp16)
{
const float l1 = 1e-3;
@ -226,139 +312,6 @@ TEST(Test_TensorFlow, quantized)
runTensorFlowNet("uint8_single_conv");
}
TEST(Test_TensorFlow, MobileNet_SSD)
{
std::string netPath = findDataFile("dnn/ssd_mobilenet_v1_coco.pb", false);
std::string netConfig = findDataFile("dnn/ssd_mobilenet_v1_coco.pbtxt", false);
std::string imgPath = findDataFile("dnn/street.png", false);
Mat inp;
resize(imread(imgPath), inp, Size(300, 300));
inp = blobFromImage(inp, 1.0f / 127.5, Size(), Scalar(127.5, 127.5, 127.5), true);
std::vector<String> outNames(3);
outNames[0] = "concat";
outNames[1] = "concat_1";
outNames[2] = "detection_out";
std::vector<Mat> target(outNames.size());
for (int i = 0; i < outNames.size(); ++i)
{
std::string path = findDataFile("dnn/tensorflow/ssd_mobilenet_v1_coco." + outNames[i] + ".npy", false);
target[i] = blobFromNPY(path);
}
Net net = readNetFromTensorflow(netPath, netConfig);
net.setInput(inp);
std::vector<Mat> output;
net.forward(output, outNames);
normAssert(target[0].reshape(1, 1), output[0].reshape(1, 1));
normAssert(target[1].reshape(1, 1), output[1].reshape(1, 1), "", 1e-5, 3e-4);
normAssert(target[2].reshape(1, 1), output[2].reshape(1, 1), "", 4e-5, 1e-2);
}
TEST(Test_TensorFlow, Inception_v2_SSD)
{
std::string proto = findDataFile("dnn/ssd_inception_v2_coco_2017_11_17.pbtxt", false);
std::string model = findDataFile("dnn/ssd_inception_v2_coco_2017_11_17.pb", false);
Net net = readNetFromTensorflow(model, proto);
Mat img = imread(findDataFile("dnn/street.png", false));
Mat blob = blobFromImage(img, 1.0f / 127.5, Size(300, 300), Scalar(127.5, 127.5, 127.5), true, false);
net.setInput(blob);
// Output has shape 1x1xNx7 where N - number of detections.
// An every detection is a vector of values [id, classId, confidence, left, top, right, bottom]
Mat out = net.forward();
out = out.reshape(1, out.total() / 7);
Mat detections;
for (int i = 0; i < out.rows; ++i)
{
if (out.at<float>(i, 2) > 0.5)
detections.push_back(out.row(i).colRange(1, 7));
}
Mat ref = (Mat_<float>(5, 6) << 1, 0.90176028, 0.19872092, 0.36311883, 0.26461923, 0.63498729,
3, 0.93569964, 0.64865261, 0.45906419, 0.80675775, 0.65708131,
3, 0.75838411, 0.44668293, 0.45907149, 0.49459291, 0.52197015,
10, 0.95932811, 0.38349164, 0.32528657, 0.40387636, 0.39165527,
10, 0.93973452, 0.66561931, 0.37841269, 0.68074018, 0.42907384);
normAssert(detections, ref);
}
OCL_TEST(Test_TensorFlow, MobileNet_SSD)
{
std::string netPath = findDataFile("dnn/ssd_mobilenet_v1_coco.pb", false);
std::string netConfig = findDataFile("dnn/ssd_mobilenet_v1_coco.pbtxt", false);
std::string imgPath = findDataFile("dnn/street.png", false);
Mat inp;
resize(imread(imgPath), inp, Size(300, 300));
inp = blobFromImage(inp, 1.0f / 127.5, Size(), Scalar(127.5, 127.5, 127.5), true);
std::vector<String> outNames(3);
outNames[0] = "concat";
outNames[1] = "concat_1";
outNames[2] = "detection_out";
std::vector<Mat> target(outNames.size());
for (int i = 0; i < outNames.size(); ++i)
{
std::string path = findDataFile("dnn/tensorflow/ssd_mobilenet_v1_coco." + outNames[i] + ".npy", false);
target[i] = blobFromNPY(path);
}
Net net = readNetFromTensorflow(netPath, netConfig);
net.setPreferableBackend(DNN_BACKEND_DEFAULT);
net.setPreferableTarget(DNN_TARGET_OPENCL);
net.setInput(inp);
std::vector<Mat> output;
net.forward(output, outNames);
normAssert(target[0].reshape(1, 1), output[0].reshape(1, 1), "", 1e-5, 1.5e-4);
normAssert(target[1].reshape(1, 1), output[1].reshape(1, 1), "", 1e-5, 3e-4);
normAssert(target[2].reshape(1, 1), output[2].reshape(1, 1), "", 4e-5, 1e-2);
}
OCL_TEST(Test_TensorFlow, Inception_v2_SSD)
{
std::string proto = findDataFile("dnn/ssd_inception_v2_coco_2017_11_17.pbtxt", false);
std::string model = findDataFile("dnn/ssd_inception_v2_coco_2017_11_17.pb", false);
Net net = readNetFromTensorflow(model, proto);
Mat img = imread(findDataFile("dnn/street.png", false));
Mat blob = blobFromImage(img, 1.0f / 127.5, Size(300, 300), Scalar(127.5, 127.5, 127.5), true, false);
net.setPreferableBackend(DNN_BACKEND_DEFAULT);
net.setPreferableTarget(DNN_TARGET_OPENCL);
net.setInput(blob);
// Output has shape 1x1xNx7 where N - number of detections.
// An every detection is a vector of values [id, classId, confidence, left, top, right, bottom]
Mat out = net.forward();
out = out.reshape(1, out.total() / 7);
Mat detections;
for (int i = 0; i < out.rows; ++i)
{
if (out.at<float>(i, 2) > 0.5)
detections.push_back(out.row(i).colRange(1, 7));
}
Mat ref = (Mat_<float>(5, 6) << 1, 0.90176028, 0.19872092, 0.36311883, 0.26461923, 0.63498729,
3, 0.93569964, 0.64865261, 0.45906419, 0.80675775, 0.65708131,
3, 0.75838411, 0.44668293, 0.45907149, 0.49459291, 0.52197015,
10, 0.95932811, 0.38349164, 0.32528657, 0.40387636, 0.39165527,
10, 0.93973452, 0.66561931, 0.37841269, 0.68074018, 0.42907384);
normAssert(detections, ref);
}
TEST(Test_TensorFlow, lstm)
{
runTensorFlowNet("lstm", DNN_TARGET_CPU, true);
@ -390,28 +343,4 @@ TEST(Test_TensorFlow, memory_read)
runTensorFlowNet("batch_norm_text", DNN_TARGET_CPU, true, l1, lInf, true);
}
TEST(Test_TensorFlow, opencv_face_detector_uint8)
{
std::string proto = findDataFile("dnn/opencv_face_detector.pbtxt", false);
std::string model = findDataFile("dnn/opencv_face_detector_uint8.pb", false);
Net net = readNetFromTensorflow(model, proto);
Mat img = imread(findDataFile("gpu/lbpcascade/er.png", false));
Mat blob = blobFromImage(img, 1.0, Size(), Scalar(104.0, 177.0, 123.0), false, false);
net.setInput(blob);
// Output has shape 1x1xNx7 where N - number of detections.
// An every detection is a vector of values [id, classId, confidence, left, top, right, bottom]
Mat out = net.forward();
// References are from test for Caffe model.
Mat ref = (Mat_<float>(6, 5) << 0.99520785, 0.80997437, 0.16379407, 0.87996572, 0.26685631,
0.9934696, 0.2831718, 0.50738752, 0.345781, 0.5985168,
0.99096733, 0.13629119, 0.24892329, 0.19756334, 0.3310290,
0.98977017, 0.23901358, 0.09084064, 0.29902688, 0.1769477,
0.97203469, 0.67965847, 0.06876482, 0.73999709, 0.1513494,
0.95097077, 0.51901293, 0.45863652, 0.5777427, 0.5347801);
normAssert(out.reshape(1, out.total() / 7).rowRange(0, 6).colRange(2, 7), ref, "", 2.8e-4, 3.4e-3);
}
}

View File

@ -42,7 +42,6 @@
#include "test_precomp.hpp"
#include "npy_blob.hpp"
#include <opencv2/dnn/shape_utils.hpp>
#include <opencv2/ts/ocl_test.hpp>
namespace opencv_test
{
@ -100,197 +99,123 @@ static void runTorchNet(String prefix, int targetId = DNN_TARGET_CPU, String out
}
}
TEST(Torch_Importer, run_convolution)
typedef testing::TestWithParam<DNNTarget> Test_Torch_layers;
TEST_P(Test_Torch_layers, run_convolution)
{
runTorchNet("net_conv");
runTorchNet("net_conv", GetParam());
}
OCL_TEST(Torch_Importer, run_convolution)
TEST_P(Test_Torch_layers, run_pool_max)
{
runTorchNet("net_conv", DNN_TARGET_OPENCL);
runTorchNet("net_pool_max", GetParam(), "", true);
}
TEST(Torch_Importer, run_pool_max)
TEST_P(Test_Torch_layers, run_pool_ave)
{
runTorchNet("net_pool_max", DNN_TARGET_CPU, "", true);
runTorchNet("net_pool_ave", GetParam());
}
OCL_TEST(Torch_Importer, run_pool_max)
TEST_P(Test_Torch_layers, run_reshape)
{
runTorchNet("net_pool_max", DNN_TARGET_OPENCL, "", true);
int targetId = GetParam();
runTorchNet("net_reshape", targetId);
runTorchNet("net_reshape_batch", targetId);
runTorchNet("net_reshape_single_sample", targetId);
runTorchNet("net_reshape_channels", targetId, "", false, true);
}
TEST(Torch_Importer, run_pool_ave)
TEST_P(Test_Torch_layers, run_linear)
{
runTorchNet("net_pool_ave");
runTorchNet("net_linear_2d", GetParam());
}
OCL_TEST(Torch_Importer, run_pool_ave)
TEST_P(Test_Torch_layers, run_concat)
{
runTorchNet("net_pool_ave", DNN_TARGET_OPENCL);
int targetId = GetParam();
runTorchNet("net_concat", targetId, "l5_torchMerge");
runTorchNet("net_depth_concat", targetId, "", false, true);
}
TEST(Torch_Importer, run_reshape)
TEST_P(Test_Torch_layers, run_deconv)
{
runTorchNet("net_reshape");
runTorchNet("net_reshape_batch");
runTorchNet("net_reshape_single_sample");
runTorchNet("net_reshape_channels", DNN_TARGET_CPU, "", false, true);
runTorchNet("net_deconv", GetParam());
}
TEST(Torch_Importer, run_linear)
TEST_P(Test_Torch_layers, run_batch_norm)
{
runTorchNet("net_linear_2d");
runTorchNet("net_batch_norm", GetParam(), "", false, true);
}
TEST(Torch_Importer, run_paralel)
TEST_P(Test_Torch_layers, net_prelu)
{
runTorchNet("net_parallel", DNN_TARGET_CPU, "l5_torchMerge");
runTorchNet("net_prelu", GetParam());
}
TEST(Torch_Importer, run_concat)
TEST_P(Test_Torch_layers, net_cadd_table)
{
runTorchNet("net_concat", DNN_TARGET_CPU, "l5_torchMerge");
runTorchNet("net_depth_concat", DNN_TARGET_CPU, "", false, true);
runTorchNet("net_cadd_table", GetParam());
}
OCL_TEST(Torch_Importer, run_concat)
TEST_P(Test_Torch_layers, net_softmax)
{
runTorchNet("net_concat", DNN_TARGET_OPENCL, "l5_torchMerge");
runTorchNet("net_depth_concat", DNN_TARGET_OPENCL, "", false, true);
int targetId = GetParam();
runTorchNet("net_softmax", targetId);
runTorchNet("net_softmax_spatial", targetId);
}
TEST(Torch_Importer, run_deconv)
{
runTorchNet("net_deconv");
}
OCL_TEST(Torch_Importer, run_deconv)
{
runTorchNet("net_deconv", DNN_TARGET_OPENCL);
}
TEST(Torch_Importer, run_batch_norm)
{
runTorchNet("net_batch_norm", DNN_TARGET_CPU, "", false, true);
}
OCL_TEST(Torch_Importer, run_batch_norm)
{
runTorchNet("net_batch_norm", DNN_TARGET_OPENCL, "", false, true);
}
TEST(Torch_Importer, net_prelu)
{
runTorchNet("net_prelu");
}
TEST(Torch_Importer, net_cadd_table)
{
runTorchNet("net_cadd_table");
}
TEST(Torch_Importer, net_softmax)
{
runTorchNet("net_softmax");
runTorchNet("net_softmax_spatial");
}
OCL_TEST(Torch_Importer, net_softmax)
{
runTorchNet("net_softmax", DNN_TARGET_OPENCL);
runTorchNet("net_softmax_spatial", DNN_TARGET_OPENCL);
}
TEST(Torch_Importer, net_logsoftmax)
TEST_P(Test_Torch_layers, net_logsoftmax)
{
runTorchNet("net_logsoftmax");
runTorchNet("net_logsoftmax_spatial");
}
OCL_TEST(Torch_Importer, net_logsoftmax)
TEST_P(Test_Torch_layers, net_lp_pooling)
{
runTorchNet("net_logsoftmax", DNN_TARGET_OPENCL);
runTorchNet("net_logsoftmax_spatial", DNN_TARGET_OPENCL);
int targetId = GetParam();
runTorchNet("net_lp_pooling_square", targetId, "", false, true);
runTorchNet("net_lp_pooling_power", targetId, "", false, true);
}
TEST(Torch_Importer, net_lp_pooling)
TEST_P(Test_Torch_layers, net_conv_gemm_lrn)
{
runTorchNet("net_lp_pooling_square", DNN_TARGET_CPU, "", false, true);
runTorchNet("net_lp_pooling_power", DNN_TARGET_CPU, "", false, true);
runTorchNet("net_conv_gemm_lrn", GetParam(), "", false, true);
}
TEST(Torch_Importer, net_conv_gemm_lrn)
TEST_P(Test_Torch_layers, net_inception_block)
{
runTorchNet("net_conv_gemm_lrn", DNN_TARGET_CPU, "", false, true);
runTorchNet("net_inception_block", GetParam(), "", false, true);
}
TEST(Torch_Importer, net_inception_block)
TEST_P(Test_Torch_layers, net_normalize)
{
runTorchNet("net_inception_block", DNN_TARGET_CPU, "", false, true);
runTorchNet("net_normalize", GetParam(), "", false, true);
}
TEST(Torch_Importer, net_normalize)
TEST_P(Test_Torch_layers, net_padding)
{
runTorchNet("net_normalize", DNN_TARGET_CPU, "", false, true);
int targetId = GetParam();
runTorchNet("net_padding", targetId, "", false, true);
runTorchNet("net_spatial_zero_padding", targetId, "", false, true);
runTorchNet("net_spatial_reflection_padding", targetId, "", false, true);
}
OCL_TEST(Torch_Importer, net_normalize)
TEST_P(Test_Torch_layers, net_non_spatial)
{
runTorchNet("net_normalize", DNN_TARGET_OPENCL, "", false, true);
runTorchNet("net_non_spatial", GetParam(), "", false, true);
}
TEST(Torch_Importer, net_padding)
{
runTorchNet("net_padding", DNN_TARGET_CPU, "", false, true);
runTorchNet("net_spatial_zero_padding", DNN_TARGET_CPU, "", false, true);
runTorchNet("net_spatial_reflection_padding", DNN_TARGET_CPU, "", false, true);
}
INSTANTIATE_TEST_CASE_P(/**/, Test_Torch_layers, availableDnnTargets());
TEST(Torch_Importer, net_non_spatial)
{
runTorchNet("net_non_spatial", DNN_TARGET_CPU, "", false, true);
}
typedef testing::TestWithParam<DNNTarget> Test_Torch_nets;
OCL_TEST(Torch_Importer, net_non_spatial)
{
runTorchNet("net_non_spatial", DNN_TARGET_OPENCL, "", false, true);
}
TEST(Torch_Importer, ENet_accuracy)
{
Net net;
{
const string model = findDataFile("dnn/Enet-model-best.net", false);
net = readNetFromTorch(model, true);
ASSERT_FALSE(net.empty());
}
Mat sample = imread(_tf("street.png", false));
Mat inputBlob = blobFromImage(sample, 1./255);
net.setInput(inputBlob, "");
Mat out = net.forward();
Mat ref = blobFromNPY(_tf("torch_enet_prob.npy", false));
// Due to numerical instability in Pooling-Unpooling layers (indexes jittering)
// thresholds for ENet must be changed. Accuracy of resuults was checked on
// Cityscapes dataset and difference in mIOU with Torch is 10E-4%
normAssert(ref, out, "", 0.00044, 0.44);
const int N = 3;
for (int i = 0; i < N; i++)
{
net.setInput(inputBlob, "");
Mat out = net.forward();
normAssert(ref, out, "", 0.00044, 0.44);
}
}
TEST(Torch_Importer, OpenFace_accuracy)
TEST_P(Test_Torch_nets, OpenFace_accuracy)
{
const string model = findDataFile("dnn/openface_nn4.small2.v1.t7", false);
Net net = readNetFromTorch(model);
net.setPreferableTarget(GetParam());
Mat sample = imread(findDataFile("cv/shared/lena.png", false));
Mat sampleF32(sample.size(), CV_32FC3);
sample.convertTo(sampleF32, sampleF32.type());
@ -306,30 +231,7 @@ TEST(Torch_Importer, OpenFace_accuracy)
normAssert(out, outRef);
}
OCL_TEST(Torch_Importer, OpenFace_accuracy)
{
const string model = findDataFile("dnn/openface_nn4.small2.v1.t7", false);
Net net = readNetFromTorch(model);
net.setPreferableBackend(DNN_BACKEND_DEFAULT);
net.setPreferableTarget(DNN_TARGET_OPENCL);
Mat sample = imread(findDataFile("cv/shared/lena.png", false));
Mat sampleF32(sample.size(), CV_32FC3);
sample.convertTo(sampleF32, sampleF32.type());
sampleF32 /= 255;
resize(sampleF32, sampleF32, Size(96, 96), 0, 0, INTER_NEAREST);
Mat inputBlob = blobFromImage(sampleF32);
net.setInput(inputBlob);
Mat out = net.forward();
Mat outRef = readTorchBlob(_tf("net_openface_output.dat"), true);
normAssert(out, outRef);
}
OCL_TEST(Torch_Importer, ENet_accuracy)
TEST_P(Test_Torch_nets, ENet_accuracy)
{
Net net;
{
@ -338,8 +240,7 @@ OCL_TEST(Torch_Importer, ENet_accuracy)
ASSERT_TRUE(!net.empty());
}
net.setPreferableBackend(DNN_BACKEND_DEFAULT);
net.setPreferableTarget(DNN_TARGET_OPENCL);
net.setPreferableTarget(GetParam());
Mat sample = imread(_tf("street.png", false));
Mat inputBlob = blobFromImage(sample, 1./255);
@ -374,7 +275,7 @@ OCL_TEST(Torch_Importer, ENet_accuracy)
// -median_filter 0 \
// -image_size 0 \
// -model models/instance_norm/feathers.t7
TEST(Torch_Importer, FastNeuralStyle_accuracy)
TEST_P(Test_Torch_nets, FastNeuralStyle_accuracy)
{
std::string models[] = {"dnn/fast_neural_style_eccv16_starry_night.t7",
"dnn/fast_neural_style_instance_norm_feathers.t7"};
@ -385,6 +286,8 @@ TEST(Torch_Importer, FastNeuralStyle_accuracy)
const string model = findDataFile(models[i], false);
Net net = readNetFromTorch(model);
net.setPreferableTarget(GetParam());
Mat img = imread(findDataFile("dnn/googlenet_1.png", false));
Mat inputBlob = blobFromImage(img, 1.0, Size(), Scalar(103.939, 116.779, 123.68), false);
@ -404,37 +307,17 @@ TEST(Torch_Importer, FastNeuralStyle_accuracy)
}
}
OCL_TEST(Torch_Importer, FastNeuralStyle_accuracy)
INSTANTIATE_TEST_CASE_P(/**/, Test_Torch_nets, availableDnnTargets());
// TODO: fix OpenCL and add to the rest of tests
TEST(Torch_Importer, run_paralel)
{
std::string models[] = {"dnn/fast_neural_style_eccv16_starry_night.t7",
"dnn/fast_neural_style_instance_norm_feathers.t7"};
std::string targets[] = {"dnn/lena_starry_night.png", "dnn/lena_feathers.png"};
runTorchNet("net_parallel", DNN_TARGET_CPU, "l5_torchMerge");
}
for (int i = 0; i < 2; ++i)
{
const string model = findDataFile(models[i], false);
Net net = readNetFromTorch(model);
net.setPreferableBackend(DNN_BACKEND_DEFAULT);
net.setPreferableTarget(DNN_TARGET_OPENCL);
Mat img = imread(findDataFile("dnn/googlenet_1.png", false));
Mat inputBlob = blobFromImage(img, 1.0, Size(), Scalar(103.939, 116.779, 123.68), false);
net.setInput(inputBlob);
Mat out = net.forward();
// Deprocessing.
getPlane(out, 0, 0) += 103.939;
getPlane(out, 0, 1) += 116.779;
getPlane(out, 0, 2) += 123.68;
out = cv::min(cv::max(0, out), 255);
Mat ref = imread(findDataFile(targets[i]));
Mat refBlob = blobFromImage(ref, 1.0, Size(), Scalar(), false);
normAssert(out, refBlob, "", 0.5, 1.1);
}
TEST(Torch_Importer, DISABLED_run_paralel)
{
runTorchNet("net_parallel", DNN_TARGET_OPENCL, "l5_torchMerge");
}
}