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 b5d1a7d0dfd5c0cffe66a7e17abfd5930a62aa54..6482a5fddf9a8687a30fbddb779946c99240cefa 100644 --- a/python/paddle/fluid/tests/unittests/test_bicubic_interp_op.py +++ b/python/paddle/fluid/tests/unittests/test_bicubic_interp_op.py @@ -390,6 +390,12 @@ class TestBicubicOpError(unittest.TestCase): x, size=[12, 12], mode='BICUBIC', align_corners=False ) + def test_size_shape(): + x = fluid.data(name="x", shape=[2, 3, 6, 6], dtype="float32") + out = interpolate( + x, size=[12], mode='BICUBIC', align_corners=False + ) + def test_align_corcers(): x = fluid.data(name="x", shape=[2, 3, 6, 6], dtype="float32") interpolate(x, size=[12, 12], mode='BICUBIC', align_corners=3) @@ -481,6 +487,7 @@ class TestBicubicOpError(unittest.TestCase): self.assertRaises(ValueError, test_mode_type) self.assertRaises(ValueError, test_input_shape) + self.assertRaises(ValueError, test_size_shape) self.assertRaises(TypeError, test_align_corcers) self.assertRaises(ValueError, test_attr_data_format) self.assertRaises(TypeError, test_actual_shape) diff --git a/python/paddle/fluid/tests/unittests/test_bicubic_interp_v2_op.py b/python/paddle/fluid/tests/unittests/test_bicubic_interp_v2_op.py index fed25ad18d2586054f880561998352bcee4ff603..a52a5b3f36d13eb1ab9f44df9278ba3ef93097cf 100644 --- a/python/paddle/fluid/tests/unittests/test_bicubic_interp_v2_op.py +++ b/python/paddle/fluid/tests/unittests/test_bicubic_interp_v2_op.py @@ -610,6 +610,20 @@ class TestBicubicOpError(unittest.TestCase): x, size={2, 2}, mode='bicubic', align_corners=False ) + def test_size_length(): + x = fluid.data(name="x", shape=[2, 3, 6, 6], dtype="float32") + out = interpolate(x, size=[2], mode='bicubic', align_corners=False) + + def test_size_tensor_ndim(): + x = fluid.data(name="x", shape=[2, 3, 6, 6], dtype="float32") + size = paddle.to_tensor(np.array([[2, 2]])) + out = interpolate(x, size=size, mode='bicubic', align_corners=False) + + def test_size_tensor_length(): + x = fluid.data(name="x", shape=[2, 3, 6, 6], dtype="float32") + size = paddle.to_tensor(np.array([2])) + out = interpolate(x, size=size, mode='bicubic', align_corners=False) + def test_input_shape_1(): x = fluid.data(name="x", shape=[2, 1, 0, 0], dtype="float32") out = interpolate( @@ -633,6 +647,9 @@ class TestBicubicOpError(unittest.TestCase): self.assertRaises(ValueError, test_size_and_scale) self.assertRaises(ValueError, test_size_and_scale2) self.assertRaises(TypeError, test_size_type) + self.assertRaises(ValueError, test_size_length) + self.assertRaises(ValueError, test_size_tensor_ndim) + self.assertRaises(ValueError, test_size_tensor_length) self.assertRaises(ValueError, test_input_shape_1) def test_errors(self): diff --git a/python/paddle/nn/functional/common.py b/python/paddle/nn/functional/common.py index 6631682d0e949d295750b43ef8259209cb6a6d2e..d9f5b0b160dc0e98b81accf95630b4953e0e2aab 100644 --- a/python/paddle/nn/functional/common.py +++ b/python/paddle/nn/functional/common.py @@ -397,6 +397,23 @@ def interpolate( if size is None and scale_factor is None: raise ValueError("One of size and scale_factor must not be None.") + if (isinstance(size, list) or isinstance(size, tuple)) and len( + size + ) != x.ndim - 2: + raise ValueError( + 'The x and size should satisfy rank(x) - 2 == len(size).' + ) + + if isinstance(size, Variable): + if size.ndim != 1: + raise ValueError( + f"If size is a tensor, it's rank must be 1, but received {size.ndim}." + ) + if size.shape[0] != x.ndim - 2: + raise ValueError( + 'The x and size should satisfy rank(x) - 2 == size.shape[0].' + ) + if not isinstance(align_corners, bool): raise TypeError("Attr align_corners should be a bool value")