diff --git a/modules/dnn/src/layers/resize_layer.cpp b/modules/dnn/src/layers/resize_layer.cpp index 370679c780..792d84d357 100644 --- a/modules/dnn/src/layers/resize_layer.cpp +++ b/modules/dnn/src/layers/resize_layer.cpp @@ -42,6 +42,7 @@ public: CV_Check(interpolation, interpolation == "nearest" || interpolation == "opencv_linear" || interpolation == "bilinear", ""); alignCorners = params.get("align_corners", false); + halfPixelCenters = params.get("half_pixel_centers", false); } bool getMemoryShapes(const std::vector &inputs, @@ -114,7 +115,7 @@ public: Mat& inp = inputs[0]; Mat& out = outputs[0]; - if (interpolation == "nearest" || interpolation == "opencv_linear") + if (interpolation == "nearest" || interpolation == "opencv_linear" || (interpolation == "bilinear" && halfPixelCenters)) { InterpolationFlags mode = interpolation == "nearest" ? INTER_NEAREST : INTER_LINEAR; for (size_t n = 0; n < inputs[0].size[0]; ++n) @@ -236,6 +237,7 @@ protected: String interpolation; float scaleWidth, scaleHeight; bool alignCorners; + bool halfPixelCenters; }; diff --git a/modules/dnn/src/tensorflow/tf_importer.cpp b/modules/dnn/src/tensorflow/tf_importer.cpp index 9083a4d4f9..13e1f40cd6 100644 --- a/modules/dnn/src/tensorflow/tf_importer.cpp +++ b/modules/dnn/src/tensorflow/tf_importer.cpp @@ -1962,6 +1962,9 @@ void TFImporter::populateNet(Net dstNet) if (hasLayerAttr(layer, "align_corners")) layerParams.set("align_corners", getLayerAttr(layer, "align_corners").b()); + if (hasLayerAttr(layer, "half_pixel_centers")) + layerParams.set("half_pixel_centers", getLayerAttr(layer, "half_pixel_centers").b()); + int id = dstNet.addLayer(name, "Resize", layerParams); layer_id[name] = id; diff --git a/modules/dnn/test/test_tf_importer.cpp b/modules/dnn/test/test_tf_importer.cpp index e9c1562b4c..a5a736d08f 100644 --- a/modules/dnn/test/test_tf_importer.cpp +++ b/modules/dnn/test/test_tf_importer.cpp @@ -81,12 +81,12 @@ class Test_TensorFlow_layers : public DNNTestLayer { public: void runTensorFlowNet(const std::string& prefix, bool hasText = false, - double l1 = 0.0, double lInf = 0.0, bool memoryLoad = false) + double l1 = 0.0, double lInf = 0.0, bool memoryLoad = false, const std::string& groupPrefix = "") { - std::string netPath = path(prefix + "_net.pb"); - std::string netConfig = (hasText ? path(prefix + "_net.pbtxt") : ""); + std::string netPath = path(prefix + groupPrefix + "_net.pb"); + std::string netConfig = (hasText ? path(prefix + groupPrefix + "_net.pbtxt") : ""); std::string inpPath = path(prefix + "_in.npy"); - std::string outPath = path(prefix + "_out.npy"); + std::string outPath = path(prefix + groupPrefix + "_out.npy"); cv::Mat input = blobFromNPY(inpPath); cv::Mat ref = blobFromNPY(outPath); @@ -975,10 +975,53 @@ TEST_P(Test_TensorFlow_layers, keras_mobilenet_head) runTensorFlowNet("keras_learning_phase"); } +// TF case: align_corners=False, half_pixel_centers=False TEST_P(Test_TensorFlow_layers, resize_bilinear) { runTensorFlowNet("resize_bilinear"); +} + +// TF case: align_corners=True, half_pixel_centers=False +TEST_P(Test_TensorFlow_layers, resize_bilinear_align_corners) +{ + runTensorFlowNet("resize_bilinear", + false, 0.0, 0.0, false, // default parameters + "_align_corners"); +} + +// TF case: align_corners=False, half_pixel_centers=True +TEST_P(Test_TensorFlow_layers, resize_bilinear_half_pixel) +{ + if (backend == DNN_BACKEND_INFERENCE_ENGINE_NGRAPH) + applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_NGRAPH); + + runTensorFlowNet("resize_bilinear", false, 0.0, 0.0, false, "_half_pixel"); +} + +// TF case: align_corners=False, half_pixel_centers=False +TEST_P(Test_TensorFlow_layers, resize_bilinear_factor) +{ runTensorFlowNet("resize_bilinear_factor"); +} + +// TF case: align_corners=False, half_pixel_centers=True +TEST_P(Test_TensorFlow_layers, resize_bilinear_factor_half_pixel) +{ + if (backend == DNN_BACKEND_INFERENCE_ENGINE_NGRAPH) + applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_NGRAPH); + + runTensorFlowNet("resize_bilinear_factor", false, 0.0, 0.0, false, "_half_pixel"); +} + +// TF case: align_corners=True, half_pixel_centers=False +TEST_P(Test_TensorFlow_layers, resize_bilinear_factor_align_corners) +{ + runTensorFlowNet("resize_bilinear_factor", false, 0.0, 0.0, false, "_align_corners"); +} + +// TF case: align_corners=False, half_pixel_centers=False +TEST_P(Test_TensorFlow_layers, resize_bilinear_down) +{ runTensorFlowNet("resize_bilinear_down"); }