diff --git a/paddle/fluid/operators/interpolate_op.cu b/paddle/fluid/operators/interpolate_op.cu index 1dfd4947c6054c3da9a8e1c79542169b1727e9ad..f86d2c4ab4e4d3aaa28a37f9d53d9a1224166581 100644 --- a/paddle/fluid/operators/interpolate_op.cu +++ b/paddle/fluid/operators/interpolate_op.cu @@ -220,12 +220,17 @@ class InterpolateOpCUDAKernel : public framework::OpKernel { int in_chw = c * in_hw; int out_chw = c * out_hw; - float ratio_h = (align_corners && out_h > 1) - ? static_cast(in_h - 1) / (out_h - 1) - : static_cast(in_h) / out_h; - float ratio_w = (align_corners && out_w > 1) - ? static_cast(in_w - 1) / (out_w - 1) - : static_cast(in_w) / out_w; + float ratio_h = 0.f; + float ratio_w = 0.f; + if (out_h > 1) { + ratio_h = (align_corners) ? static_cast(in_h - 1) / (out_h - 1) + : static_cast(in_h) / out_h; + } + if (out_w > 1) { + ratio_w = (align_corners && out_w > 1) + ? static_cast(in_w - 1) / (out_w - 1) + : static_cast(in_w) / out_w; + } if (in_h == out_h && in_w == out_w) { framework::TensorCopy(*input, ctx.GetPlace(), output); @@ -290,12 +295,17 @@ class InterpolateGradOpCUDAKernel : public framework::OpKernel { int in_chw = c * in_hw; int out_chw = c * out_hw; - float ratio_h = (align_corners && out_h > 1) - ? static_cast(in_h - 1) / (out_h - 1) - : static_cast(in_h) / out_h; - float ratio_w = (align_corners && out_w > 1) - ? static_cast(in_w - 1) / (out_w - 1) - : static_cast(in_w) / out_w; + float ratio_h = 0.f; + float ratio_w = 0.f; + if (out_h > 1) { + ratio_h = (align_corners) ? static_cast(in_h - 1) / (out_h - 1) + : static_cast(in_h) / out_h; + } + if (out_w > 1) { + ratio_w = (align_corners && out_w > 1) + ? static_cast(in_w - 1) / (out_w - 1) + : static_cast(in_w) / out_w; + } if (in_h == out_h && in_w == out_w) { framework::TensorCopy(*output_grad, ctx.GetPlace(), input_grad); diff --git a/paddle/fluid/operators/interpolate_op.h b/paddle/fluid/operators/interpolate_op.h index 1ec0cb5025b343117b803dc4a0f8b03be57b31ac..acdebf73e0db917c688f5b38f9b76bfc420330f0 100644 --- a/paddle/fluid/operators/interpolate_op.h +++ b/paddle/fluid/operators/interpolate_op.h @@ -191,12 +191,18 @@ class InterpolateKernel : public framework::OpKernel { return; } - float ratio_h = (align_corners && out_h > 1) - ? static_cast(in_h - 1) / (out_h - 1) - : static_cast(in_h) / out_h; - float ratio_w = (align_corners && out_w > 1) - ? static_cast(in_w - 1) / (out_w - 1) - : static_cast(in_w) / out_w; + float ratio_h = 0.f; + float ratio_w = 0.f; + + if (out_h > 1) { + ratio_h = (align_corners) ? static_cast(in_h - 1) / (out_h - 1) + : static_cast(in_h) / out_h; + } + if (out_w > 1) { + ratio_w = (align_corners && out_w > 1) + ? static_cast(in_w - 1) / (out_w - 1) + : static_cast(in_w) / out_w; + } if ("bilinear" == interp_method) { BilinearInterpolation(*input, output, ratio_h, ratio_w, in_h, in_w, n, @@ -244,12 +250,18 @@ class InterpolateGradKernel : public framework::OpKernel { return; } - float ratio_h = (align_corners && out_h > 1) - ? static_cast(in_h - 1) / (out_h - 1) - : static_cast(in_h) / out_h; - float ratio_w = (align_corners && out_w > 1) - ? static_cast(in_w - 1) / (out_w - 1) - : static_cast(in_w) / out_w; + float ratio_h = 0.f; + float ratio_w = 0.f; + + if (out_h > 1) { + ratio_h = (align_corners) ? static_cast(in_h - 1) / (out_h - 1) + : static_cast(in_h) / out_h; + } + if (out_w > 1) { + ratio_w = (align_corners && out_w > 1) + ? static_cast(in_w - 1) / (out_w - 1) + : static_cast(in_w) / out_w; + } if ("bilinear" == interp_method) { BilinearInterpolationGrad(*output_grad, input_grad, ratio_h, ratio_w, diff --git a/python/paddle/fluid/tests/unittests/test_bilinear_interp_op.py b/python/paddle/fluid/tests/unittests/test_bilinear_interp_op.py index 2e3de58a3abcb694b1d392d9e49165bb56bd9ae4..f60ed1d79ae5778f751d6101fde386ae3a90c0f7 100644 --- a/python/paddle/fluid/tests/unittests/test_bilinear_interp_op.py +++ b/python/paddle/fluid/tests/unittests/test_bilinear_interp_op.py @@ -37,14 +37,16 @@ def bilinear_interp_np(input, batch_size, channel, in_h, in_w = input.shape ratio_h = ratio_w = 0.0 - if (align_corners and out_h > 1): - ratio_h = (in_h - 1.0) / (out_h - 1.0) - else: - ratio_h = 1.0 * in_h / out_h - if (align_corners and out_w > 1): - ratio_w = (in_w - 1.0) / (out_w - 1.0) - else: - ratio_w = 1.0 * in_w / out_w + if out_h > 1: + if (align_corners): + ratio_h = (in_h - 1.0) / (out_h - 1.0) + else: + ratio_h = 1.0 * in_h / out_h + if out_w > 1: + if (align_corners): + ratio_w = (in_w - 1.0) / (out_w - 1.0) + else: + ratio_w = 1.0 * in_w / out_w out = np.zeros((batch_size, channel, out_h, out_w)) diff --git a/python/paddle/fluid/tests/unittests/test_nearest_interp_op.py b/python/paddle/fluid/tests/unittests/test_nearest_interp_op.py index 9984a793cae5774f122db05c30c7f1e24e1d78bc..5bb2260ef7a143670dd75fc88769603d1437173d 100644 --- a/python/paddle/fluid/tests/unittests/test_nearest_interp_op.py +++ b/python/paddle/fluid/tests/unittests/test_nearest_interp_op.py @@ -36,14 +36,16 @@ def nearest_neighbor_interp_np(X, n, c, in_h, in_w = X.shape ratio_h = ratio_w = 0.0 - if (align_corners and out_h > 1): - ratio_h = (in_h - 1.0) / (out_h - 1.0) - else: - ratio_h = 1.0 * in_h / out_h - if (align_corners and out_w > 1): - ratio_w = (in_w - 1.0) / (out_w - 1.0) - else: - ratio_w = 1.0 * in_w / out_w + if (out_h > 1): + if (align_corners): + ratio_h = (in_h - 1.0) / (out_h - 1.0) + else: + ratio_h = 1.0 * in_h / out_h + if (out_w > 1): + if (align_corners): + ratio_w = (in_w - 1.0) / (out_w - 1.0) + else: + ratio_w = 1.0 * in_w / out_w out = np.zeros((n, c, out_h, out_w))