From 0652f15810b97e94e29d57131c2ca118f29cc092 Mon Sep 17 00:00:00 2001 From: Zhang Ting Date: Thu, 10 Oct 2019 16:47:20 +0800 Subject: [PATCH] fix Attr(scale)'s bug for image_resize and modified unittest, test=develop (#19983) --- python/paddle/fluid/layers/nn.py | 7 ++- .../tests/unittests/test_nearest_interp_op.py | 50 +++++++++---------- .../unittests/test_trilinear_interp_op.py | 9 ++-- 3 files changed, 35 insertions(+), 31 deletions(-) mode change 100644 => 100755 python/paddle/fluid/tests/unittests/test_nearest_interp_op.py mode change 100644 => 100755 python/paddle/fluid/tests/unittests/test_trilinear_interp_op.py diff --git a/python/paddle/fluid/layers/nn.py b/python/paddle/fluid/layers/nn.py index c6f7db00c7b..1854f4cdf84 100755 --- a/python/paddle/fluid/layers/nn.py +++ b/python/paddle/fluid/layers/nn.py @@ -9397,10 +9397,13 @@ def image_resize(input, if isinstance(scale, Variable): scale.stop_gradient = True inputs["Scale"] = scale - if isinstance(scale, float): + elif isinstance(scale, float) or isinstance(scale, int): if scale <= 0: - raise ValueError("scale should be greater than zero.") + raise ValueError("Attr(scale) should be greater than zero.") attrs['scale'] = float(scale) + else: + raise TypeError( + "Attr(scale)'s type should be float, int or Variable.") if isinstance(actual_shape, Variable): warnings.warn( diff --git a/python/paddle/fluid/tests/unittests/test_nearest_interp_op.py b/python/paddle/fluid/tests/unittests/test_nearest_interp_op.py old mode 100644 new mode 100755 index 6f0d85ed3ce..dbdca111da7 --- a/python/paddle/fluid/tests/unittests/test_nearest_interp_op.py +++ b/python/paddle/fluid/tests/unittests/test_nearest_interp_op.py @@ -425,26 +425,14 @@ class TestNearestInterp_attr_tensor_Case3(TestNearestInterpOp_attr_tensor): class TestNearestAPI(OpTest): def test_case(self): - x = fluid.layers.data(name="x", shape=[3, 6, 6], dtype="float32") - y = fluid.layers.data(name="y", shape=[6, 6, 3], dtype="float32") - - dim = fluid.layers.data( - name="dim", shape=[1], dtype="int32", append_batch_size=False) - shape_tensor = fluid.layers.data( - name="shape_tensor", - shape=[2], - dtype="int32", - append_batch_size=False) - actual_size = fluid.layers.data( - name="actual_size", - shape=[2], - dtype="int32", - append_batch_size=False) - scale_tensor = fluid.layers.data( - name="scale_tensor", - shape=[1], - dtype="float32", - append_batch_size=False) + x = fluid.data(name="x", shape=[1, 3, 6, 6], dtype="float32") + y = fluid.data(name="y", shape=[1, 6, 6, 3], dtype="float32") + + dim = fluid.data(name="dim", shape=[1], dtype="int32") + shape_tensor = fluid.data(name="shape_tensor", shape=[2], dtype="int32") + actual_size = fluid.data(name="actual_size", shape=[2], dtype="int32") + scale_tensor = fluid.data( + name="scale_tensor", shape=[1], dtype="float32") out1 = fluid.layers.resize_nearest( y, out_shape=[12, 12], data_format='NHWC') @@ -481,15 +469,25 @@ class TestNearestAPI(OpTest): for i in range(len(results) - 1): self.assertTrue(np.allclose(results[i + 1], expect_res)) + +class TestNearestInterpException(OpTest): def test_exception(self): - # for 4-D input, data_format can only be NCHW or NHWC - input = fluid.layers.data( - name="input", shape=[3, 6, 6], dtype="float32") - try: + input = fluid.data(name="input", shape=[1, 3, 6, 6], dtype="float32") + + def attr_data_format(): + # for 4-D input, data_format can only be NCHW or NHWC out = fluid.layers.resize_nearest( input, out_shape=[4, 8], data_format='NDHWC') - except: - pass + + def attr_scale_type(): + out = fluid.layers.resize_nearest(input, scale='scale') + + def attr_scale_value(): + out = fluid.layers.resize_nearest(input, scale=-0.3) + + self.assertRaises(ValueError, attr_data_format) + self.assertRaises(TypeError, attr_scale_type) + self.assertRaises(ValueError, attr_scale_value) if __name__ == "__main__": diff --git a/python/paddle/fluid/tests/unittests/test_trilinear_interp_op.py b/python/paddle/fluid/tests/unittests/test_trilinear_interp_op.py old mode 100644 new mode 100755 index dcd4c715273..899b4ba4341 --- a/python/paddle/fluid/tests/unittests/test_trilinear_interp_op.py +++ b/python/paddle/fluid/tests/unittests/test_trilinear_interp_op.py @@ -625,15 +625,18 @@ class TestTrilinearInterpAPI(OpTest): for i in range(len(results) - 1): self.assertTrue(np.allclose(results[i + 1], expect_res)) + +class TestTrilinearInterpOpException(OpTest): def test_exception(self): input = fluid.layers.data( name="input", shape=[3, 6, 9, 4], dtype="float32") - try: + + def attr_data_format(): # for 5-D input, data_format only can be NCDHW or NDHWC out = fluid.layers.resize_trilinear( input, out_shape=[4, 8, 4], data_format='NHWC') - except: - pass + + self.assertRaises(ValueError, attr_data_format) if __name__ == "__main__": -- GitLab