diff --git a/python/paddle/fluid/tests/unittests/test_unpool3d_op.py b/python/paddle/fluid/tests/unittests/test_unpool3d_op.py index 3db2d1514caaf5e47bdd064557ec664da00d58c4..af17073758fdfd4056d6ac20ad5a35d4ccc8e359 100644 --- a/python/paddle/fluid/tests/unittests/test_unpool3d_op.py +++ b/python/paddle/fluid/tests/unittests/test_unpool3d_op.py @@ -17,6 +17,7 @@ import numpy as np from op_test import OpTest import paddle import paddle.nn.functional as F +from paddle.fluid import core paddle.enable_static() paddle.seed(2022) @@ -178,36 +179,42 @@ class TestUnpool3DOpOutput(TestUnpool3DOp): class TestUnpool3DOpException(unittest.TestCase): + def setUp(self): + paddle.disable_static() + + def tearDown(self): + paddle.enable_static() + def test_exception(self): def indices_size_error(): - data = paddle.randint(shape=[1, 1, 3, 3, 3]) + data = paddle.rand(shape=[1, 1, 3, 3, 3]) indices = paddle.reshape( paddle.arange(0, 36), shape=[1, 1, 3, 3, 4] - ) - MaxUnPool3D = F.maxunpool3d(data, indices, kernel_size=2, stride=2) + ).astype("int32") + F.max_unpool3d(data, indices, kernel_size=2, stride=2) def indices_value_error(): - data = paddle.randint(shape=[1, 1, 3, 3, 3]) + data = paddle.rand(shape=[1, 1, 3, 3, 3]) indices = paddle.reshape( - paddle.arange(4, 40), shape=[1, 1, 3, 3, 3] - ) - MaxUnPool3D = F.maxunpool3d(data, indices, kernel_size=2, stride=2) + paddle.arange(195, 222), shape=[1, 1, 3, 3, 3] + ).astype("int32") + F.max_unpool3d(data, indices, kernel_size=2, stride=2) def data_format_error(): - data = paddle.randint(shape=[1, 1, 3, 3, 3]) + data = paddle.rand(shape=[1, 1, 3, 3, 3]) indices = paddle.reshape( paddle.arange(0, 27), shape=[1, 1, 3, 3, 3] - ) - MaxUnPool3D = F.maxunpool3d( + ).astype("int32") + F.max_unpool3d( data, indices, kernel_size=2, stride=2, data_format="NDHWC" ) def data_outputsize_error(): - data = paddle.randint(shape=[1, 1, 3, 3, 3]) + data = paddle.rand(shape=[1, 1, 3, 3, 3]) indices = paddle.reshape( paddle.arange(0, 27), shape=[1, 1, 3, 3, 3] - ) - MaxUnPool3D = F.maxunpool3d( + ).astype("int32") + F.max_unpool3d( data, indices, kernel_size=2, @@ -216,19 +223,36 @@ class TestUnpool3DOpException(unittest.TestCase): ) def data_outputsize_error2(): - data = paddle.randint(shape=[1, 1, 3, 3, 3]) + data = paddle.rand(shape=[1, 1, 3, 3, 3]) indices = paddle.reshape( paddle.arange(0, 27), shape=[1, 1, 3, 3, 3] ) - MaxUnPool3D = F.maxunpool3d( + F.max_unpool3d( data, indices, kernel_size=2, stride=2, output_size=[10, 10, 10] ) - self.assertRaises(ValueError, indices_size_error) - self.assertRaises(ValueError, indices_value_error) - self.assertRaises(ValueError, data_format_error) - self.assertRaises(ValueError, data_outputsize_error) - self.assertRaises(ValueError, data_outputsize_error2) + self.assertRaisesRegex( + ValueError, + r"The dimensions of Input\(X\) must equal to", + indices_size_error, + ) + if not core.is_compiled_with_cuda(): + self.assertRaisesRegex( + ValueError, + r"index should less than output", + indices_value_error, + ) + self.assertRaisesRegex( + ValueError, + r"Attr\(data_format\) should be 'NCDHW'", + data_format_error, + ) + self.assertRaisesRegex( + ValueError, r"invalid output_size", data_outputsize_error + ) + self.assertRaisesRegex( + ValueError, r"invalid output_size", data_outputsize_error2 + ) class TestUnpool3DOpAPI_dygraph(unittest.TestCase): diff --git a/python/paddle/fluid/tests/unittests/test_unpool_op.py b/python/paddle/fluid/tests/unittests/test_unpool_op.py index 00c48e0872df825f145d7cb2ce8ecede8c7f6564..4ae6919552cadc2c68126a777c7c801423b4d688 100644 --- a/python/paddle/fluid/tests/unittests/test_unpool_op.py +++ b/python/paddle/fluid/tests/unittests/test_unpool_op.py @@ -18,7 +18,7 @@ import numpy as np from op_test import OpTest import paddle import paddle.nn.functional as F -from paddle.fluid import Program, program_guard +from paddle.fluid import Program, program_guard, core from test_attribute_var import UnittestBase @@ -178,46 +178,76 @@ class TestUnpoolOpOutput(TestUnpoolOp): class TestUnpoolOpException(unittest.TestCase): - def test_exception(self): - import paddle.nn.functional as F - import paddle + def setUp(self): + paddle.disable_static() + + def tearDown(self): + paddle.enable_static() + def test_exception(self): def indices_size_error(): - data = paddle.randint(shape=[1, 1, 3, 3]) - indices = paddle.reshape(paddle.arange(0, 12), shape[1, 1, 3, 4]) - MaxPool2D = F.maxunpool2d(data, indices, kernel_size=2, stride=2) + data = paddle.rand(shape=[1, 1, 3, 3]) + indices = paddle.reshape( + paddle.arange(0, 12), shape=[1, 1, 3, 4] + ).astype("int32") + F.max_unpool2d(data, indices, kernel_size=2, stride=2) def indices_value_error(): - data = paddle.randint(shape=[1, 1, 3, 3]) - indices = paddle.reshape(paddle.arange(4, 40), shape[1, 1, 3, 4]) - MaxPool2D = F.maxunpool2d(data, indices, kernel_size=2, stride=2) + data = paddle.rand(shape=[1, 1, 3, 3]) + indices = paddle.reshape( + paddle.arange(31, 40), shape=[1, 1, 3, 3] + ).astype("int32") + F.max_unpool2d(data, indices, kernel_size=2, stride=2) def data_format_error(): - data = paddle.randint(shape=[1, 1, 3, 3]) - indices = paddle.reshape(paddle.arange(4, 40), shape[1, 1, 3, 4]) - MaxPool2D = F.maxunpool2d( + data = paddle.rand(shape=[1, 1, 3, 3]) + indices = paddle.reshape( + paddle.arange(0, 9), shape=[1, 1, 3, 3] + ).astype("int32") + F.max_unpool2d( data, indices, kernel_size=2, stride=2, data_format="NHWC" ) def data_outputsize_error(): - data = paddle.randint(shape=[1, 1, 3, 3]) - indices = paddle.reshape(paddle.arange(4, 40), shape[1, 1, 3, 4]) - MaxPool2D = F.maxunpool2d( + data = paddle.rand(shape=[1, 1, 3, 3]) + indices = paddle.reshape( + paddle.arange(0, 9), shape=[1, 1, 3, 3] + ).astype("int32") + F.max_unpool2d( data, indices, kernel_size=2, stride=2, output_size=[5, 6, 7, 8] ) def data_outputsize_error2(): - data = paddle.randint(shape=[1, 1, 3, 3]) - indices = paddle.reshape(paddle.arange(4, 40), shape[1, 1, 3, 4]) - MaxPool2D = F.maxunpool2d( + data = paddle.rand(shape=[1, 1, 3, 3]) + indices = paddle.reshape( + paddle.arange(0, 9), shape=[1, 1, 3, 3] + ).astype("int32") + F.max_unpool2d( data, indices, kernel_size=2, stride=2, output_size=[100, 100] ) - self.assertRaises(ValueError, indices_size_error) - self.assertRaises(ValueError, indices_value_error) - self.assertRaises(ValueError, data_format_error) - self.assertRaises(ValueError, data_outputsize_error) - self.assertRaises(ValueError, data_outputsize_error2) + self.assertRaisesRegex( + ValueError, + r"The dimensions of Input\(X\) must equal to", + indices_size_error, + ) + if not core.is_compiled_with_cuda(): + self.assertRaisesRegex( + ValueError, + r"index should less than output", + indices_value_error, + ) + self.assertRaisesRegex( + ValueError, + r"Attr\(data_format\) should be 'NCHW'", + data_format_error, + ) + self.assertRaisesRegex( + ValueError, r"invalid output_size", data_outputsize_error + ) + self.assertRaisesRegex( + ValueError, r"invalid output_size", data_outputsize_error2 + ) class TestUnpoolOpAPI_dy(unittest.TestCase):