Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CodeStyle][F821] fix test_exception in test_unpool3d_op and test_unpool_op #47756

Merged
merged 10 commits into from
Nov 10, 2022
64 changes: 44 additions & 20 deletions python/paddle/fluid/tests/unittests/test_unpool3d_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from op_test import OpTest
import paddle
import paddle.nn.functional as F
from paddle.fluid import core

paddle.enable_static()
paddle.seed(2022)
Expand Down Expand Up @@ -178,36 +179,42 @@ def init_test_case(self):


class TestUnpool3DOpException(unittest.TestCase):
def setUp(self):
paddle.disable_static()

def tearDown(self):
paddle.enable_static()
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

test_exception 使用动态图进行测试


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,
Expand All @@ -216,19 +223,36 @@ def data_outputsize_error():
)

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,
)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

GPU 是没有这个报错的,因此这里仅在 CPU assert

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):
Expand Down
78 changes: 54 additions & 24 deletions python/paddle/fluid/tests/unittests/test_unpool_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
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

Expand Down Expand Up @@ -178,46 +178,76 @@ def init_test_case(self):


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):
Expand Down