From 9089841b6e7ec632bd5ed51ceb4fad8748e0aac3 Mon Sep 17 00:00:00 2001 From: LielinJiang <50691816+LielinJiang@users.noreply.github.com> Date: Fri, 9 Oct 2020 11:38:35 +0800 Subject: [PATCH] Fix bilateral inference shape bug (#26822) * fix bilateral bug --- paddle/fluid/operators/bilateral_slice_op.cc | 31 +++++++++++-------- python/paddle/fluid/contrib/layers/nn.py | 8 +++-- .../unittests/test_bilateral_slice_op.py | 17 +++++++--- 3 files changed, 36 insertions(+), 20 deletions(-) diff --git a/paddle/fluid/operators/bilateral_slice_op.cc b/paddle/fluid/operators/bilateral_slice_op.cc index b742b4c0dee..b00604155d6 100644 --- a/paddle/fluid/operators/bilateral_slice_op.cc +++ b/paddle/fluid/operators/bilateral_slice_op.cc @@ -50,20 +50,25 @@ class BilateralSliceOp : public framework::OperatorWithKernel { int64_t input_chans = input_dims[1]; int64_t output_chans; - if (has_offset) { - PADDLE_ENFORCE_EQ((coeffs_chans % (input_chans + 1)), 0, - platform::errors::InvalidArgument( - "Slicing with affine offset, coefficients grid " - "should have n_out*(n_in+1) channels, but got %d", - coeffs_chans)); - output_chans = coeffs_chans / (input_chans + 1); + if ((!ctx->IsRuntime()) && ((coeffs_chans < 0) || (input_chans < 0))) { + output_chans = -1; } else { - PADDLE_ENFORCE_EQ((coeffs_chans % input_chans), 0, - platform::errors::InvalidArgument( - "Slicing without affine offset, coefficients grid " - "should have n_out*n_in channels, but got %d .", - coeffs_chans)); - output_chans = coeffs_chans / input_chans; + if (has_offset) { + PADDLE_ENFORCE_EQ((coeffs_chans % (input_chans + 1)), 0, + platform::errors::InvalidArgument( + "Slicing with affine offset, coefficients grid " + "should have n_out*(n_in+1) channels, but got %d", + coeffs_chans)); + output_chans = coeffs_chans / (input_chans + 1); + } else { + PADDLE_ENFORCE_EQ( + (coeffs_chans % input_chans), 0, + platform::errors::InvalidArgument( + "Slicing without affine offset, coefficients grid " + "should have n_out*n_in channels, but got %d .", + coeffs_chans)); + output_chans = coeffs_chans / input_chans; + } } std::vector output_dims; diff --git a/python/paddle/fluid/contrib/layers/nn.py b/python/paddle/fluid/contrib/layers/nn.py index ac6493b1c29..d0543bb90dd 100644 --- a/python/paddle/fluid/contrib/layers/nn.py +++ b/python/paddle/fluid/contrib/layers/nn.py @@ -1525,10 +1525,10 @@ def bilateral_slice(x, guide, grid, has_offset, name=None): grid = fluid.data(name='grid', shape=[None, 12, 8, 10, 6], dtype='float32') # without offset - output = fluid.layers.bilateral_slice(x, guide, grid, has_offset=False) + output = fluid.contrib.bilateral_slice(x, guide, grid, has_offset=False) # has offset - output = fluid.layers.bilateral_slice(x, guide, grid, has_offset=True) + output = fluid.contrib.bilateral_slice(x, guide, grid, has_offset=True) """ helper = LayerHelper("bilateral_slice", **locals()) @@ -1541,7 +1541,9 @@ def bilateral_slice(x, guide, grid, has_offset, name=None): out = helper.create_variable_for_type_inference(x.dtype) inputs = {'X': x, 'Guide': guide, 'Grid': grid} - + if paddle.fluid.in_dygraph_mode(): + attrs = ('has_offset', has_offset) + return getattr(core.ops, "bilateral_slice")(x, grid, guide, *attrs) helper.append_op( type='bilateral_slice', inputs=inputs, diff --git a/python/paddle/fluid/tests/unittests/test_bilateral_slice_op.py b/python/paddle/fluid/tests/unittests/test_bilateral_slice_op.py index 51e447dba72..c0d622d7ea1 100644 --- a/python/paddle/fluid/tests/unittests/test_bilateral_slice_op.py +++ b/python/paddle/fluid/tests/unittests/test_bilateral_slice_op.py @@ -178,16 +178,25 @@ class TestBilateralSliceOp1(TestBilateralSliceOp): self.data_type = 'float32' -class TestBilateralSliceApi(TestBilateralSliceOp): +class TestBilateralSliceApi(unittest.TestCase): def test_api(self): x = paddle.fluid.data( name='x', shape=[None, 3, 25, 15], dtype='float32') guide = paddle.fluid.data( name='guide', shape=[None, 25, 15], dtype='float32') grid = paddle.fluid.data( - name='grid', shape=[None, 12, 8, 5, 3], dtype='float32') - paddle.fluid.contrib.layers.bilateral_slice(x, guide, grid, - self.has_offset) + name='grid', shape=[None, None, 8, 5, 3], dtype='float32') + paddle.fluid.contrib.layers.bilateral_slice(x, guide, grid, False) + + if not paddle.fluid.is_compiled_with_cuda(): + return + + with paddle.fluid.dygraph.guard(): + x1 = paddle.rand([3, 1, 50, 30]) + guide1 = paddle.rand([3, 50, 30]) + grid1 = paddle.rand([3, 2, 2, 5, 3]) + + paddle.fluid.contrib.bilateral_slice(x1, guide1, grid1, False) if __name__ == "__main__": -- GitLab