mirror of
https://github.com/opencv/opencv.git
synced 2026-12-02 13:16:32 -06:00
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
This commit is contained in:
@@ -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<int32_t>(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<int64_t>(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");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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<int>{1, 1, 1, 2}, CV_64S);
|
||||
int64_t* inpData = inp.ptr<int64_t>();
|
||||
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<int64_t>();
|
||||
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<tuple<Backend, Target> >
|
||||
|
||||
Reference in New Issue
Block a user