Path: blob/master/modules/dnn/test/test_tf_importer.cpp
16339 views
// This file is part of OpenCV project.1// It is subject to the license terms in the LICENSE file found in the top-level directory2// of this distribution and at http://opencv.org/license.html.34// Copyright (C) 2017, Intel Corporation, all rights reserved.5// Third party copyrights are property of their respective owners.67/*8Test for Tensorflow models loading9*/1011#include "test_precomp.hpp"12#include "npy_blob.hpp"1314#include <opencv2/dnn/layer.details.hpp> // CV_DNN_REGISTER_LAYER_CLASS1516namespace opencv_test17{1819using namespace cv;20using namespace cv::dnn;2122template<typename TString>23static std::string _tf(TString filename)24{25return (getOpenCVExtraDir() + "/dnn/") + filename;26}2728TEST(Test_TensorFlow, read_inception)29{30Net net;31{32const string model = findDataFile("dnn/tensorflow_inception_graph.pb", false);33net = readNetFromTensorflow(model);34ASSERT_FALSE(net.empty());35}36net.setPreferableBackend(DNN_BACKEND_OPENCV);3738Mat sample = imread(_tf("grace_hopper_227.png"));39ASSERT_TRUE(!sample.empty());40Mat input;41resize(sample, input, Size(224, 224));42input -= 128; // mean sub4344Mat inputBlob = blobFromImage(input);4546net.setInput(inputBlob, "input");47Mat out = net.forward("softmax2");4849std::cout << out.dims << std::endl;50}5152TEST(Test_TensorFlow, inception_accuracy)53{54Net net;55{56const string model = findDataFile("dnn/tensorflow_inception_graph.pb", false);57net = readNetFromTensorflow(model);58ASSERT_FALSE(net.empty());59}60net.setPreferableBackend(DNN_BACKEND_OPENCV);6162Mat sample = imread(_tf("grace_hopper_227.png"));63ASSERT_TRUE(!sample.empty());64Mat inputBlob = blobFromImage(sample, 1.0, Size(224, 224), Scalar(), /*swapRB*/true);6566net.setInput(inputBlob, "input");67Mat out = net.forward("softmax2");6869Mat ref = blobFromNPY(_tf("tf_inception_prob.npy"));7071normAssert(ref, out);72}7374static std::string path(const std::string& file)75{76return findDataFile("dnn/tensorflow/" + file, false);77}7879class Test_TensorFlow_layers : public DNNTestLayer80{81public:82void runTensorFlowNet(const std::string& prefix, bool hasText = false,83double l1 = 0.0, double lInf = 0.0, bool memoryLoad = false)84{85std::string netPath = path(prefix + "_net.pb");86std::string netConfig = (hasText ? path(prefix + "_net.pbtxt") : "");87std::string inpPath = path(prefix + "_in.npy");88std::string outPath = path(prefix + "_out.npy");8990cv::Mat input = blobFromNPY(inpPath);91cv::Mat ref = blobFromNPY(outPath);92checkBackend(&input, &ref);9394Net net;95if (memoryLoad)96{97// Load files into a memory buffers98string dataModel;99ASSERT_TRUE(readFileInMemory(netPath, dataModel));100101string dataConfig;102if (hasText)103ASSERT_TRUE(readFileInMemory(netConfig, dataConfig));104105net = readNetFromTensorflow(dataModel.c_str(), dataModel.size(),106dataConfig.c_str(), dataConfig.size());107}108else109net = readNetFromTensorflow(netPath, netConfig);110111ASSERT_FALSE(net.empty());112113net.setPreferableBackend(backend);114net.setPreferableTarget(target);115net.setInput(input);116cv::Mat output = net.forward();117normAssert(ref, output, "", l1 ? l1 : default_l1, lInf ? lInf : default_lInf);118}119};120121TEST_P(Test_TensorFlow_layers, conv)122{123runTensorFlowNet("single_conv");124runTensorFlowNet("atrous_conv2d_valid");125runTensorFlowNet("atrous_conv2d_same");126runTensorFlowNet("depthwise_conv2d");127runTensorFlowNet("keras_atrous_conv2d_same");128runTensorFlowNet("conv_pool_nchw");129}130131TEST_P(Test_TensorFlow_layers, padding)132{133runTensorFlowNet("padding_same");134runTensorFlowNet("padding_valid");135runTensorFlowNet("spatial_padding");136}137138TEST_P(Test_TensorFlow_layers, eltwise_add_mul)139{140runTensorFlowNet("eltwise_add_mul");141}142143TEST_P(Test_TensorFlow_layers, pad_and_concat)144{145#if defined(INF_ENGINE_RELEASE) && INF_ENGINE_RELEASE < 2018030000146if (backend == DNN_BACKEND_INFERENCE_ENGINE && target == DNN_TARGET_MYRIAD)147throw SkipTestException("Test is enabled starts from OpenVINO 2018R3");148#endif149runTensorFlowNet("pad_and_concat");150}151152TEST_P(Test_TensorFlow_layers, concat_axis_1)153{154runTensorFlowNet("concat_axis_1");155}156157TEST_P(Test_TensorFlow_layers, batch_norm)158{159runTensorFlowNet("batch_norm");160runTensorFlowNet("batch_norm", false, 0.0, 0.0, true);161runTensorFlowNet("fused_batch_norm");162runTensorFlowNet("fused_batch_norm", false, 0.0, 0.0, true);163runTensorFlowNet("batch_norm_text", true);164runTensorFlowNet("batch_norm_text", true, 0.0, 0.0, true);165runTensorFlowNet("unfused_batch_norm");166runTensorFlowNet("fused_batch_norm_no_gamma");167runTensorFlowNet("unfused_batch_norm_no_gamma");168runTensorFlowNet("mvn_batch_norm");169runTensorFlowNet("mvn_batch_norm_1x1");170}171172TEST_P(Test_TensorFlow_layers, pooling)173{174runTensorFlowNet("max_pool_even");175runTensorFlowNet("max_pool_odd_valid");176runTensorFlowNet("max_pool_odd_same");177runTensorFlowNet("reduce_mean"); // an average pooling over all spatial dimensions.178}179180// TODO: fix tests and replace to pooling181TEST_P(Test_TensorFlow_layers, ave_pool_same)182{183#if defined(INF_ENGINE_RELEASE) && INF_ENGINE_RELEASE < 2018030000184if (backend == DNN_BACKEND_INFERENCE_ENGINE && target == DNN_TARGET_MYRIAD)185throw SkipTestException("Test is enabled starts from OpenVINO 2018R3");186#endif187runTensorFlowNet("ave_pool_same");188}189190TEST_P(Test_TensorFlow_layers, deconvolution)191{192runTensorFlowNet("deconvolution");193runTensorFlowNet("deconvolution_same");194runTensorFlowNet("deconvolution_stride_2_same");195runTensorFlowNet("deconvolution_adj_pad_valid");196runTensorFlowNet("deconvolution_adj_pad_same");197runTensorFlowNet("keras_deconv_valid");198runTensorFlowNet("keras_deconv_same");199}200201TEST_P(Test_TensorFlow_layers, matmul)202{203if (backend == DNN_BACKEND_OPENCV && target == DNN_TARGET_OPENCL_FP16)204throw SkipTestException("");205runTensorFlowNet("matmul");206runTensorFlowNet("nhwc_reshape_matmul");207runTensorFlowNet("nhwc_transpose_reshape_matmul");208}209210TEST_P(Test_TensorFlow_layers, reshape)211{212if (backend == DNN_BACKEND_INFERENCE_ENGINE)213throw SkipTestException("");214runTensorFlowNet("shift_reshape_no_reorder");215runTensorFlowNet("reshape_no_reorder");216runTensorFlowNet("reshape_reduce");217runTensorFlowNet("reshape_as_shape");218}219220TEST_P(Test_TensorFlow_layers, flatten)221{222if (backend == DNN_BACKEND_INFERENCE_ENGINE &&223(target == DNN_TARGET_OPENCL || target == DNN_TARGET_OPENCL_FP16 || target == DNN_TARGET_MYRIAD))224throw SkipTestException("");225runTensorFlowNet("flatten", true);226}227228TEST_P(Test_TensorFlow_layers, unfused_flatten)229{230if (backend == DNN_BACKEND_INFERENCE_ENGINE &&231(target == DNN_TARGET_OPENCL || target == DNN_TARGET_OPENCL_FP16))232throw SkipTestException("");233runTensorFlowNet("unfused_flatten");234runTensorFlowNet("unfused_flatten_unknown_batch");235}236237TEST_P(Test_TensorFlow_layers, leaky_relu)238{239runTensorFlowNet("leaky_relu_order1");240runTensorFlowNet("leaky_relu_order2");241runTensorFlowNet("leaky_relu_order3");242}243244TEST_P(Test_TensorFlow_layers, l2_normalize)245{246runTensorFlowNet("l2_normalize");247}248249// TODO: fix it and add to l2_normalize250TEST_P(Test_TensorFlow_layers, l2_normalize_3d)251{252if (backend == DNN_BACKEND_INFERENCE_ENGINE && target != DNN_TARGET_CPU)253throw SkipTestException("");254runTensorFlowNet("l2_normalize_3d");255}256257class Test_TensorFlow_nets : public DNNTestLayer {};258259TEST_P(Test_TensorFlow_nets, MobileNet_SSD)260{261checkBackend();262if ((backend == DNN_BACKEND_INFERENCE_ENGINE && target != DNN_TARGET_CPU) ||263(backend == DNN_BACKEND_OPENCV && target == DNN_TARGET_OPENCL_FP16))264throw SkipTestException("");265266std::string netPath = findDataFile("dnn/ssd_mobilenet_v1_coco.pb", false);267std::string netConfig = findDataFile("dnn/ssd_mobilenet_v1_coco.pbtxt", false);268std::string imgPath = findDataFile("dnn/street.png", false);269270Mat inp;271resize(imread(imgPath), inp, Size(300, 300));272inp = blobFromImage(inp, 1.0f / 127.5, Size(), Scalar(127.5, 127.5, 127.5), true);273274std::vector<String> outNames(3);275outNames[0] = "concat";276outNames[1] = "concat_1";277outNames[2] = "detection_out";278279std::vector<Mat> refs(outNames.size());280for (int i = 0; i < outNames.size(); ++i)281{282std::string path = findDataFile("dnn/tensorflow/ssd_mobilenet_v1_coco." + outNames[i] + ".npy", false);283refs[i] = blobFromNPY(path);284}285286Net net = readNetFromTensorflow(netPath, netConfig);287net.setPreferableBackend(backend);288net.setPreferableTarget(target);289290net.setInput(inp);291292std::vector<Mat> output;293net.forward(output, outNames);294295normAssert(refs[0].reshape(1, 1), output[0].reshape(1, 1), "", 1e-5, 1.5e-4);296normAssert(refs[1].reshape(1, 1), output[1].reshape(1, 1), "", 1e-5, 3e-4);297normAssertDetections(refs[2], output[2], "", 0.2);298}299300TEST_P(Test_TensorFlow_nets, Inception_v2_SSD)301{302checkBackend();303std::string proto = findDataFile("dnn/ssd_inception_v2_coco_2017_11_17.pbtxt", false);304std::string model = findDataFile("dnn/ssd_inception_v2_coco_2017_11_17.pb", false);305306Net net = readNetFromTensorflow(model, proto);307Mat img = imread(findDataFile("dnn/street.png", false));308Mat blob = blobFromImage(img, 1.0f, Size(300, 300), Scalar(), true, false);309310net.setPreferableBackend(backend);311net.setPreferableTarget(target);312313net.setInput(blob);314// Output has shape 1x1xNx7 where N - number of detections.315// An every detection is a vector of values [id, classId, confidence, left, top, right, bottom]316Mat out = net.forward();317Mat ref = (Mat_<float>(5, 7) << 0, 1, 0.90176028, 0.19872092, 0.36311883, 0.26461923, 0.63498729,3180, 3, 0.93569964, 0.64865261, 0.45906419, 0.80675775, 0.65708131,3190, 3, 0.75838411, 0.44668293, 0.45907149, 0.49459291, 0.52197015,3200, 10, 0.95932811, 0.38349164, 0.32528657, 0.40387636, 0.39165527,3210, 10, 0.93973452, 0.66561931, 0.37841269, 0.68074018, 0.42907384);322double scoreDiff = (target == DNN_TARGET_OPENCL_FP16 || target == DNN_TARGET_MYRIAD) ? 0.0097 : default_l1;323double iouDiff = (target == DNN_TARGET_OPENCL_FP16 || target == DNN_TARGET_MYRIAD) ? 0.09 : default_lInf;324normAssertDetections(ref, out, "", 0.5, scoreDiff, iouDiff);325}326327TEST_P(Test_TensorFlow_nets, MobileNet_v1_SSD)328{329checkBackend();330331std::string model = findDataFile("dnn/ssd_mobilenet_v1_coco_2017_11_17.pb", false);332std::string proto = findDataFile("dnn/ssd_mobilenet_v1_coco_2017_11_17.pbtxt", false);333334Net net = readNetFromTensorflow(model, proto);335Mat img = imread(findDataFile("dnn/dog416.png", false));336Mat blob = blobFromImage(img, 1.0f, Size(300, 300), Scalar(), true, false);337338net.setPreferableBackend(backend);339net.setPreferableTarget(target);340341net.setInput(blob);342Mat out = net.forward();343344Mat ref = blobFromNPY(findDataFile("dnn/tensorflow/ssd_mobilenet_v1_coco_2017_11_17.detection_out.npy"));345float scoreDiff = (target == DNN_TARGET_OPENCL_FP16 || target == DNN_TARGET_MYRIAD) ? 7e-3 : 1e-5;346float iouDiff = (target == DNN_TARGET_OPENCL_FP16 || target == DNN_TARGET_MYRIAD) ? 0.0098 : 1e-3;347normAssertDetections(ref, out, "", 0.3, scoreDiff, iouDiff);348}349350TEST_P(Test_TensorFlow_nets, Faster_RCNN)351{352static std::string names[] = {"faster_rcnn_inception_v2_coco_2018_01_28",353"faster_rcnn_resnet50_coco_2018_01_28"};354355checkBackend();356if ((backend == DNN_BACKEND_INFERENCE_ENGINE && target != DNN_TARGET_CPU) ||357(backend == DNN_BACKEND_OPENCV && target == DNN_TARGET_OPENCL_FP16))358throw SkipTestException("");359360for (int i = 1; i < 2; ++i)361{362std::string proto = findDataFile("dnn/" + names[i] + ".pbtxt", false);363std::string model = findDataFile("dnn/" + names[i] + ".pb", false);364365Net net = readNetFromTensorflow(model, proto);366net.setPreferableBackend(backend);367net.setPreferableTarget(target);368Mat img = imread(findDataFile("dnn/dog416.png", false));369Mat blob = blobFromImage(img, 1.0f, Size(800, 600), Scalar(), true, false);370371net.setInput(blob);372Mat out = net.forward();373374Mat ref = blobFromNPY(findDataFile("dnn/tensorflow/" + names[i] + ".detection_out.npy"));375normAssertDetections(ref, out, names[i].c_str(), 0.3);376}377}378379TEST_P(Test_TensorFlow_nets, MobileNet_v1_SSD_PPN)380{381checkBackend();382std::string proto = findDataFile("dnn/ssd_mobilenet_v1_ppn_coco.pbtxt", false);383std::string model = findDataFile("dnn/ssd_mobilenet_v1_ppn_coco.pb", false);384385Net net = readNetFromTensorflow(model, proto);386Mat img = imread(findDataFile("dnn/dog416.png", false));387Mat ref = blobFromNPY(findDataFile("dnn/tensorflow/ssd_mobilenet_v1_ppn_coco.detection_out.npy", false));388Mat blob = blobFromImage(img, 1.0f, Size(300, 300), Scalar(), true, false);389390net.setPreferableBackend(backend);391net.setPreferableTarget(target);392393net.setInput(blob);394Mat out = net.forward();395396double scoreDiff = (target == DNN_TARGET_OPENCL_FP16 || target == DNN_TARGET_MYRIAD) ? 0.011 : default_l1;397double iouDiff = (target == DNN_TARGET_OPENCL_FP16 || target == DNN_TARGET_MYRIAD) ? 0.021 : default_lInf;398normAssertDetections(ref, out, "", 0.4, scoreDiff, iouDiff);399}400401TEST_P(Test_TensorFlow_nets, opencv_face_detector_uint8)402{403checkBackend();404std::string proto = findDataFile("dnn/opencv_face_detector.pbtxt", false);405std::string model = findDataFile("dnn/opencv_face_detector_uint8.pb", false);406407Net net = readNetFromTensorflow(model, proto);408Mat img = imread(findDataFile("gpu/lbpcascade/er.png", false));409Mat blob = blobFromImage(img, 1.0, Size(), Scalar(104.0, 177.0, 123.0), false, false);410411net.setPreferableBackend(backend);412net.setPreferableTarget(target);413net.setInput(blob);414// Output has shape 1x1xNx7 where N - number of detections.415// An every detection is a vector of values [id, classId, confidence, left, top, right, bottom]416Mat out = net.forward();417418// References are from test for Caffe model.419Mat ref = (Mat_<float>(6, 7) << 0, 1, 0.99520785, 0.80997437, 0.16379407, 0.87996572, 0.26685631,4200, 1, 0.9934696, 0.2831718, 0.50738752, 0.345781, 0.5985168,4210, 1, 0.99096733, 0.13629119, 0.24892329, 0.19756334, 0.3310290,4220, 1, 0.98977017, 0.23901358, 0.09084064, 0.29902688, 0.1769477,4230, 1, 0.97203469, 0.67965847, 0.06876482, 0.73999709, 0.1513494,4240, 1, 0.95097077, 0.51901293, 0.45863652, 0.5777427, 0.5347801);425double scoreDiff = (target == DNN_TARGET_OPENCL_FP16 || target == DNN_TARGET_MYRIAD) ? 4e-3 : 3.4e-3;426double iouDiff = (target == DNN_TARGET_OPENCL_FP16 || target == DNN_TARGET_MYRIAD) ? 0.024 : 1e-2;427normAssertDetections(ref, out, "", 0.9, scoreDiff, iouDiff);428}429430// inp = cv.imread('opencv_extra/testdata/cv/ximgproc/sources/08.png')431// inp = inp[:,:,[2, 1, 0]].astype(np.float32).reshape(1, 512, 512, 3)432// outs = sess.run([sess.graph.get_tensor_by_name('feature_fusion/Conv_7/Sigmoid:0'),433// sess.graph.get_tensor_by_name('feature_fusion/concat_3:0')],434// feed_dict={'input_images:0': inp})435// scores = np.ascontiguousarray(outs[0].transpose(0, 3, 1, 2))436// geometry = np.ascontiguousarray(outs[1].transpose(0, 3, 1, 2))437// np.save('east_text_detection.scores.npy', scores)438// np.save('east_text_detection.geometry.npy', geometry)439TEST_P(Test_TensorFlow_nets, EAST_text_detection)440{441checkBackend();442#if defined(INF_ENGINE_RELEASE) && INF_ENGINE_RELEASE < 2018030000443if (backend == DNN_BACKEND_INFERENCE_ENGINE && target == DNN_TARGET_MYRIAD)444throw SkipTestException("Test is enabled starts from OpenVINO 2018R3");445#endif446447std::string netPath = findDataFile("dnn/frozen_east_text_detection.pb", false);448std::string imgPath = findDataFile("cv/ximgproc/sources/08.png", false);449std::string refScoresPath = findDataFile("dnn/east_text_detection.scores.npy", false);450std::string refGeometryPath = findDataFile("dnn/east_text_detection.geometry.npy", false);451452Net net = readNet(findDataFile("dnn/frozen_east_text_detection.pb", false));453454net.setPreferableBackend(backend);455net.setPreferableTarget(target);456457Mat img = imread(imgPath);458Mat inp = blobFromImage(img, 1.0, Size(), Scalar(123.68, 116.78, 103.94), true, false);459net.setInput(inp);460461std::vector<Mat> outs;462std::vector<String> outNames(2);463outNames[0] = "feature_fusion/Conv_7/Sigmoid";464outNames[1] = "feature_fusion/concat_3";465net.forward(outs, outNames);466467Mat scores = outs[0];468Mat geometry = outs[1];469470// Scores are in range [0, 1]. Geometry values are in range [-0.23, 290]471double l1_scores = default_l1, lInf_scores = default_lInf;472double l1_geometry = default_l1, lInf_geometry = default_lInf;473if (target == DNN_TARGET_OPENCL_FP16)474{475lInf_scores = 0.11;476l1_geometry = 0.28; lInf_geometry = 5.94;477}478else if (target == DNN_TARGET_MYRIAD)479{480lInf_scores = 0.214;481l1_geometry = 0.47; lInf_geometry = 15.34;482}483else484{485l1_geometry = 1e-4, lInf_geometry = 3e-3;486}487normAssert(scores, blobFromNPY(refScoresPath), "scores", l1_scores, lInf_scores);488normAssert(geometry, blobFromNPY(refGeometryPath), "geometry", l1_geometry, lInf_geometry);489}490491INSTANTIATE_TEST_CASE_P(/**/, Test_TensorFlow_nets, dnnBackendsAndTargets());492493TEST_P(Test_TensorFlow_layers, fp16_weights)494{495const float l1 = 0.00071;496const float lInf = 0.012;497runTensorFlowNet("fp16_single_conv", false, l1, lInf);498runTensorFlowNet("fp16_deconvolution", false, l1, lInf);499runTensorFlowNet("fp16_max_pool_odd_same", false, l1, lInf);500runTensorFlowNet("fp16_padding_valid", false, l1, lInf);501runTensorFlowNet("fp16_eltwise_add_mul", false, l1, lInf);502runTensorFlowNet("fp16_max_pool_odd_valid", false, l1, lInf);503runTensorFlowNet("fp16_max_pool_even", false, l1, lInf);504runTensorFlowNet("fp16_padding_same", false, l1, lInf);505}506507// TODO: fix pad_and_concat and add this test case to fp16_weights508TEST_P(Test_TensorFlow_layers, fp16_pad_and_concat)509{510const float l1 = 0.00071;511const float lInf = 0.012;512#if defined(INF_ENGINE_RELEASE) && INF_ENGINE_RELEASE < 2018030000513if (backend == DNN_BACKEND_INFERENCE_ENGINE && target == DNN_TARGET_MYRIAD)514throw SkipTestException("Test is enabled starts from OpenVINO 2018R3");515#endif516runTensorFlowNet("fp16_pad_and_concat", false, l1, lInf);517}518519TEST_P(Test_TensorFlow_layers, defun)520{521runTensorFlowNet("defun_dropout");522}523524TEST_P(Test_TensorFlow_layers, quantized)525{526runTensorFlowNet("uint8_single_conv");527}528529TEST_P(Test_TensorFlow_layers, lstm)530{531if (backend == DNN_BACKEND_INFERENCE_ENGINE ||532(backend == DNN_BACKEND_OPENCV && target == DNN_TARGET_OPENCL_FP16))533throw SkipTestException("");534runTensorFlowNet("lstm", true);535runTensorFlowNet("lstm", true, 0.0, 0.0, true);536}537538TEST_P(Test_TensorFlow_layers, split)539{540if (backend == DNN_BACKEND_INFERENCE_ENGINE)541throw SkipTestException("");542runTensorFlowNet("split_equals");543}544545TEST_P(Test_TensorFlow_layers, resize_nearest_neighbor)546{547if (backend == DNN_BACKEND_INFERENCE_ENGINE && target != DNN_TARGET_MYRIAD)548throw SkipTestException("");549runTensorFlowNet("resize_nearest_neighbor");550runTensorFlowNet("keras_upsampling2d");551}552553TEST_P(Test_TensorFlow_layers, slice)554{555if (backend == DNN_BACKEND_INFERENCE_ENGINE &&556(target == DNN_TARGET_OPENCL || target == DNN_TARGET_OPENCL_FP16))557throw SkipTestException("");558runTensorFlowNet("slice_4d");559}560561TEST_P(Test_TensorFlow_layers, softmax)562{563runTensorFlowNet("keras_softmax");564}565566TEST_P(Test_TensorFlow_layers, relu6)567{568runTensorFlowNet("keras_relu6");569runTensorFlowNet("keras_relu6", /*hasText*/ true);570}571572TEST_P(Test_TensorFlow_layers, keras_mobilenet_head)573{574runTensorFlowNet("keras_mobilenet_head");575}576577TEST_P(Test_TensorFlow_layers, resize_bilinear)578{579runTensorFlowNet("resize_bilinear");580runTensorFlowNet("resize_bilinear_factor");581}582583INSTANTIATE_TEST_CASE_P(/**/, Test_TensorFlow_layers, dnnBackendsAndTargets());584585TEST(Test_TensorFlow, two_inputs)586{587Net net = readNet(path("two_inputs_net.pbtxt"));588net.setPreferableBackend(DNN_BACKEND_OPENCV);589590Mat firstInput(2, 3, CV_32FC1), secondInput(2, 3, CV_32FC1);591randu(firstInput, -1, 1);592randu(secondInput, -1, 1);593594net.setInput(firstInput, "first_input");595net.setInput(secondInput, "second_input");596Mat out = net.forward();597598normAssert(out, firstInput + secondInput);599}600601TEST(Test_TensorFlow, Mask_RCNN)602{603std::string proto = findDataFile("dnn/mask_rcnn_inception_v2_coco_2018_01_28.pbtxt", false);604std::string model = findDataFile("dnn/mask_rcnn_inception_v2_coco_2018_01_28.pb", false);605606Net net = readNetFromTensorflow(model, proto);607Mat img = imread(findDataFile("dnn/street.png", false));608Mat refDetections = blobFromNPY(path("mask_rcnn_inception_v2_coco_2018_01_28.detection_out.npy"));609Mat refMasks = blobFromNPY(path("mask_rcnn_inception_v2_coco_2018_01_28.detection_masks.npy"));610Mat blob = blobFromImage(img, 1.0f, Size(800, 800), Scalar(), true, false);611612net.setPreferableBackend(DNN_BACKEND_OPENCV);613614net.setInput(blob);615616// Mask-RCNN predicts bounding boxes and segmentation masks.617std::vector<String> outNames(2);618outNames[0] = "detection_out_final";619outNames[1] = "detection_masks";620621std::vector<Mat> outs;622net.forward(outs, outNames);623624Mat outDetections = outs[0];625Mat outMasks = outs[1];626normAssertDetections(refDetections, outDetections, "", /*threshold for zero confidence*/1e-5);627628// Output size of masks is NxCxHxW where629// N - number of detected boxes630// C - number of classes (excluding background)631// HxW - segmentation shape632const int numDetections = outDetections.size[2];633634int masksSize[] = {1, numDetections, outMasks.size[2], outMasks.size[3]};635Mat masks(4, &masksSize[0], CV_32F);636637std::vector<cv::Range> srcRanges(4, cv::Range::all());638std::vector<cv::Range> dstRanges(4, cv::Range::all());639640outDetections = outDetections.reshape(1, outDetections.total() / 7);641for (int i = 0; i < numDetections; ++i)642{643// Get a class id for this bounding box and copy mask only for that class.644int classId = static_cast<int>(outDetections.at<float>(i, 1));645srcRanges[0] = dstRanges[1] = cv::Range(i, i + 1);646srcRanges[1] = cv::Range(classId, classId + 1);647outMasks(srcRanges).copyTo(masks(dstRanges));648}649cv::Range topRefMasks[] = {Range::all(), Range(0, numDetections), Range::all(), Range::all()};650normAssert(masks, refMasks(&topRefMasks[0]));651}652653}654655656