From 6909fffde1effe0b39e9f6023940e93b9858ed15 Mon Sep 17 00:00:00 2001 From: Dmitry Kurtaev Date: Wed, 14 Jun 2023 14:21:28 +0300 Subject: [PATCH 1/2] Consider half pixel mode in ONNX resize --- modules/dnn/src/onnx/onnx_importer.cpp | 16 ++++++++++------ modules/dnn/test/test_onnx_importer.cpp | 1 + 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/modules/dnn/src/onnx/onnx_importer.cpp b/modules/dnn/src/onnx/onnx_importer.cpp index dd1559b386..b087a2cee1 100644 --- a/modules/dnn/src/onnx/onnx_importer.cpp +++ b/modules/dnn/src/onnx/onnx_importer.cpp @@ -2779,13 +2779,15 @@ void ONNXImporter::parseResize(LayerParams& layerParams, const opencv_onnx::Node if (layerParams.has("coordinate_transformation_mode")) { String interp_mode = layerParams.get("coordinate_transformation_mode"); - CV_Assert_N(interp_mode != "tf_crop_and_resize", interp_mode != "tf_half_pixel_for_nn"); + CV_Assert_N(interp_mode != "tf_crop_and_resize"); + + bool halfPixel = interp_mode == "tf_half_pixel_for_nn" || interp_mode == "half_pixel" || interp_mode == "pytorch_half_pixel"; layerParams.set("align_corners", interp_mode == "align_corners"); + layerParams.set("half_pixel_centers", halfPixel); if (layerParams.get("mode") == "linear") { - layerParams.set("mode", interp_mode == "pytorch_half_pixel" || interp_mode == "half_pixel" ? - "opencv_linear" : "bilinear"); + layerParams.set("mode", halfPixel ? "opencv_linear" : "bilinear"); } } if (layerParams.get("mode") == "linear" && framework_name == "pytorch") @@ -2838,13 +2840,15 @@ void ONNXImporter::parseUpsample(LayerParams& layerParams, const opencv_onnx::No if (layerParams.has("coordinate_transformation_mode")) { String interp_mode = layerParams.get("coordinate_transformation_mode"); - CV_Assert_N(interp_mode != "tf_crop_and_resize", interp_mode != "tf_half_pixel_for_nn"); + CV_Assert_N(interp_mode != "tf_crop_and_resize"); + + bool halfPixel = interp_mode == "tf_half_pixel_for_nn" || interp_mode == "half_pixel" || interp_mode == "pytorch_half_pixel"; layerParams.set("align_corners", interp_mode == "align_corners"); + layerParams.set("half_pixel_centers", halfPixel); if (layerParams.get("mode") == "linear") { - layerParams.set("mode", interp_mode == "pytorch_half_pixel" ? - "opencv_linear" : "bilinear"); + layerParams.set("mode", halfPixel ? "opencv_linear" : "bilinear"); } } if (layerParams.get("mode") == "linear" && framework_name == "pytorch") diff --git a/modules/dnn/test/test_onnx_importer.cpp b/modules/dnn/test/test_onnx_importer.cpp index b996c631ac..49908e7ff1 100644 --- a/modules/dnn/test/test_onnx_importer.cpp +++ b/modules/dnn/test/test_onnx_importer.cpp @@ -1017,6 +1017,7 @@ TEST_P(Test_ONNX_layers, Padding) TEST_P(Test_ONNX_layers, Resize) { testONNXModels("resize_nearest"); + testONNXModels("tf_half_pixel_for_nn"); if (backend == DNN_BACKEND_INFERENCE_ENGINE_NN_BUILDER_2019) applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_NN_BUILDER); testONNXModels("resize_bilinear"); From 924c01dbec19f04d7249a6be4aed1ab50944cea1 Mon Sep 17 00:00:00 2001 From: Dmitry Kurtaev Date: Thu, 15 Jun 2023 17:30:33 +0300 Subject: [PATCH 2/2] Replace CV_Assert_N --- modules/dnn/src/onnx/onnx_importer.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/modules/dnn/src/onnx/onnx_importer.cpp b/modules/dnn/src/onnx/onnx_importer.cpp index b087a2cee1..5cd22057ad 100644 --- a/modules/dnn/src/onnx/onnx_importer.cpp +++ b/modules/dnn/src/onnx/onnx_importer.cpp @@ -2779,7 +2779,7 @@ void ONNXImporter::parseResize(LayerParams& layerParams, const opencv_onnx::Node if (layerParams.has("coordinate_transformation_mode")) { String interp_mode = layerParams.get("coordinate_transformation_mode"); - CV_Assert_N(interp_mode != "tf_crop_and_resize"); + CV_Assert(interp_mode != "tf_crop_and_resize"); bool halfPixel = interp_mode == "tf_half_pixel_for_nn" || interp_mode == "half_pixel" || interp_mode == "pytorch_half_pixel"; @@ -2840,7 +2840,7 @@ void ONNXImporter::parseUpsample(LayerParams& layerParams, const opencv_onnx::No if (layerParams.has("coordinate_transformation_mode")) { String interp_mode = layerParams.get("coordinate_transformation_mode"); - CV_Assert_N(interp_mode != "tf_crop_and_resize"); + CV_Assert(interp_mode != "tf_crop_and_resize"); bool halfPixel = interp_mode == "tf_half_pixel_for_nn" || interp_mode == "half_pixel" || interp_mode == "pytorch_half_pixel";