From 5014b24db5615cdc4e52a70546dd18c20f2ffbda Mon Sep 17 00:00:00 2001 From: Shiven Solanki Date: Tue, 24 Nov 2026 15:34:50 +0800 Subject: [PATCH] dnn: keep CV_64S precision in Resize2 nearest mode (#31036) * dnn: keep CV_64S precision in Resize2 nearest mode Fixes #31035 * dnn: use ptr<>() in Resize2 int64 test to fix Debug assert on 4D Mat --- modules/dnn/src/layers/resize2_layer.cpp | 7 ++++- modules/dnn/test/test_layers.cpp | 36 ++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 1 deletion(-) diff --git a/modules/dnn/src/layers/resize2_layer.cpp b/modules/dnn/src/layers/resize2_layer.cpp index 24555a04b3..f71c9caf85 100644 --- a/modules/dnn/src/layers/resize2_layer.cpp +++ b/modules/dnn/src/layers/resize2_layer.cpp @@ -1295,8 +1295,10 @@ public: } Mat inp, out; + // Nearest only copies values, so CV_64S can stay int64 instead of going through float. + const bool nearestInt64 = depth == CV_64S && interpolation == "nearest"; if (depth != CV_32F && depth != CV_8S && depth != CV_8U && depth != CV_16F && depth != CV_16BF && - depth != CV_32S) { + depth != CV_32S && !nearestInt64) { inp_.convertTo(inp, CV_32F); out.fit(outShape, CV_32F); depth = CV_32F; @@ -1341,6 +1343,9 @@ public: case CV_32S: resizeNearest(inp,out,scaleHeight,scaleWidth,length_resized_y,length_resized_x,nearestModeE,coordTransMode,halfPixelCenters,roi_start_y,roi_end_y,roi_start_x,roi_end_x,extrapolation_value); break; + case CV_64S: + resizeNearest(inp,out,scaleHeight,scaleWidth,length_resized_y,length_resized_x,nearestModeE,coordTransMode,halfPixelCenters,roi_start_y,roi_end_y,roi_start_x,roi_end_x,extrapolation_value); + break; default: CV_Error(Error::StsUnsupportedFormat,"Unsupported depth"); } } diff --git a/modules/dnn/test/test_layers.cpp b/modules/dnn/test/test_layers.cpp index 3ccb604f90..f54d3b1e8e 100644 --- a/modules/dnn/test/test_layers.cpp +++ b/modules/dnn/test/test_layers.cpp @@ -2890,6 +2890,42 @@ TEST_P(Layer_Test_Resize, align_corners_downscale) normAssert(out2d, ref, "", 5e-5, 1e-4); } +// Nearest only copies values, so CV_64S input must come out unchanged. +// It used to go through CV_32F, which rounds values above 2^24. +TEST_P(Layer_Test_Resize, nearest_int64_keeps_precision) +{ + int backendId = get<0>(GetParam()); + int targetId = get<1>(GetParam()); + if (backendId != DNN_BACKEND_OPENCV) + throw SkipTestException("Resize2 layer is implemented for the OpenCV backend only"); + + const int64_t vals[] = {16777217, 123456789012LL}; + Mat inp(std::vector{1, 1, 1, 2}, CV_64S); + int64_t* inpData = inp.ptr(); + inpData[0] = vals[0]; + inpData[1] = vals[1]; + + LayerParams lp; + lp.type = "Resize2"; + lp.name = "testLayer"; + lp.set("zoom_factor_y", 1); + lp.set("zoom_factor_x", 2); + lp.set("interpolation", "nearest"); + + Net net; + net.addLayerToPrev(lp.name, lp.type, lp); + net.setInput(inp); + net.setPreferableBackend(backendId); + net.setPreferableTarget(targetId); + Mat out = net.forward(); + + ASSERT_EQ(CV_64S, out.depth()); + ASSERT_EQ(4, (int)out.total()); + const int64_t* outData = out.ptr(); + for (int i = 0; i < 4; ++i) + EXPECT_EQ(vals[i / 2], outData[i]) << "i=" << i; +} + INSTANTIATE_TEST_CASE_P(/**/, Layer_Test_Resize, dnnBackendsAndTargets()); struct Layer_Test_Slice : public testing::TestWithParam >