Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 18 additions & 13 deletions paddle/fluid/operators/bilateral_slice_op.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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<int64_t> output_dims;
Expand Down
8 changes: 5 additions & 3 deletions python/paddle/fluid/contrib/layers/nn.py
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand All @@ -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,
Expand Down
17 changes: 13 additions & 4 deletions python/paddle/fluid/tests/unittests/test_bilateral_slice_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,16 +178,25 @@ def initTestCase(self):
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__":
Expand Down