From 96ffebef55f3af6831e840d625f9fc491bb0965c Mon Sep 17 00:00:00 2001 From: xiaoting <31891223+tink2123@users.noreply.github.com> Date: Fri, 24 Apr 2020 10:32:37 +0800 Subject: [PATCH] fix bicubic, change int to floor (#24063) * change int to floor, test=develop * fix unittest, test=develop --- paddle/fluid/operators/interpolate_op.cu | 8 ++++---- paddle/fluid/operators/interpolate_op.h | 8 ++++---- .../fluid/tests/unittests/test_bicubic_interp_op.py | 6 +++--- 3 files changed, 11 insertions(+), 11 deletions(-) diff --git a/paddle/fluid/operators/interpolate_op.cu b/paddle/fluid/operators/interpolate_op.cu index 1effcf751f1..5beaa64664f 100644 --- a/paddle/fluid/operators/interpolate_op.cu +++ b/paddle/fluid/operators/interpolate_op.cu @@ -553,13 +553,13 @@ __global__ void KeBicubicInterpFw( T in_img_idy = align_corners ? static_cast(ratio_h * out_img_idy) : static_cast(ratio_h * (out_img_idy + 0.5) - 0.5); - int input_y = static_cast(in_img_idy); + int input_y = floorf(in_img_idy); const T y_t = in_img_idy - input_y; T in_img_idx = align_corners ? static_cast(ratio_w * out_img_idx) : static_cast(ratio_w * (out_img_idx + 0.5) - 0.5); - int input_x = static_cast(in_img_idx); + int input_x = floorf(in_img_idx); const T x_t = in_img_idx - input_x; T coefficients[4]; @@ -665,13 +665,13 @@ __global__ void KeBicubicInterpBw( T in_img_idy = align_corners ? static_cast(ratio_h * out_img_idy) : static_cast(ratio_h * (out_img_idy + 0.5) - 0.5); - int input_y = static_cast(in_img_idy); + int input_y = floorf(in_img_idy); const T y_t = in_img_idy - input_y; T in_img_idx = align_corners ? static_cast(ratio_w * out_img_idx) : static_cast(ratio_w * (out_img_idx + 0.5) - 0.5); - int input_x = static_cast(in_img_idx); + int input_x = floorf(in_img_idx); const T x_t = in_img_idx - input_x; diff --git a/paddle/fluid/operators/interpolate_op.h b/paddle/fluid/operators/interpolate_op.h index 11c4e9f385e..8acd54200ec 100644 --- a/paddle/fluid/operators/interpolate_op.h +++ b/paddle/fluid/operators/interpolate_op.h @@ -389,13 +389,13 @@ static void BicubicInterpolation(const Tensor& input, Tensor* output, for (int k = 0; k < out_h; k++) { // loop for images T y_n = align_corners ? static_cast(ratio_h * k) : static_cast(ratio_h * (k + 0.5) - 0.5); - int input_y = static_cast(y_n); + int input_y = floorf(y_n); const T y_t = y_n - input_y; for (int l = 0; l < out_w; l++) { T x_n = align_corners ? static_cast(ratio_w * l) : static_cast(ratio_w * (l + 0.5) - 0.5); - int input_x = static_cast(x_n); + int input_x = floorf(x_n); const T x_t = x_n - input_x; for (int i = 0; i < n; i++) { // loop for batches @@ -625,13 +625,13 @@ static void BicubicInterpolationGrad(const Tensor& output_grad, for (int k = 0; k < out_h; k++) { // loop for images T y_n = align_corners ? static_cast(ratio_h * k) : static_cast(ratio_h * (k + 0.5) - 0.5); - int input_y = static_cast(y_n); + int input_y = floorf(y_n); T y_t = y_n - input_y; for (int l = 0; l < out_w; l++) { T x_n = align_corners ? static_cast(ratio_w * l) : static_cast(ratio_w * (l + 0.5) - 0.5); - int input_x = static_cast(x_n); + int input_x = floorf(x_n); T x_t = x_n - input_x; T x_coeffs[4]; diff --git a/python/paddle/fluid/tests/unittests/test_bicubic_interp_op.py b/python/paddle/fluid/tests/unittests/test_bicubic_interp_op.py index 68f98da8119..616f9c5a392 100644 --- a/python/paddle/fluid/tests/unittests/test_bicubic_interp_op.py +++ b/python/paddle/fluid/tests/unittests/test_bicubic_interp_op.py @@ -88,14 +88,14 @@ def bicubic_interp_np(input, h = ratio_h * k else: h = ratio_h * (k + 0.5) - 0.5 - input_y = int(h) + input_y = np.floor(h) y_t = h - input_y for l in range(out_w): if (align_corners): w = ratio_w * l else: w = ratio_w * (l + 0.5) - 0.5 - input_x = int(w) + input_x = np.floor(w) x_t = w - input_x for i in range(batch_size): for j in range(channel): @@ -265,7 +265,7 @@ class TestBicubicInterpDataLayout(TestBicubicInterpOp): class TestBicubicInterpOpAPI(unittest.TestCase): def test_case(self): - + np.random.seed(200) x_data = np.random.random((2, 3, 6, 6)).astype("float32") dim_data = np.array([12]).astype("int32") shape_data = np.array([12, 12]).astype("int32") -- GitLab