From 6961a94e942796b8f32516897faf4fa95156ad66 Mon Sep 17 00:00:00 2001 From: tink2123 Date: Mon, 28 Jan 2019 22:33:37 -0800 Subject: [PATCH] avoid out_size less than 1 test=develop --- paddle/fluid/operators/interpolate_op.cu | 34 +++++++++++------- paddle/fluid/operators/interpolate_op.h | 36 ++++++++++++------- .../unittests/test_bilinear_interp_op.py | 18 +++++----- .../tests/unittests/test_nearest_interp_op.py | 18 +++++----- 4 files changed, 66 insertions(+), 40 deletions(-) diff --git a/paddle/fluid/operators/interpolate_op.cu b/paddle/fluid/operators/interpolate_op.cu index 1dfd4947c6..f86d2c4ab4 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 1ec0cb5025..acdebf73e0 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 2e3de58a3a..f60ed1d79a 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 9984a793ca..5bb2260ef7 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)) -- GitLab