未验证 提交 636780b5 编写于 作者: R RedContritio 提交者: GitHub

Fix Python IndexError of case16: paddle.nn.functional.max_unpool2d/max_unpool3d (#49991)

* add input dimension check for max_unpool2d/max_unpool3d

* add unittest
上级 5db88d08
......@@ -195,6 +195,20 @@ class TestUnpool3DOpException(unittest.TestCase):
).astype("int32")
F.max_unpool3d(data, indices, kernel_size=2, stride=2)
def x_rank_error():
data = paddle.rand(shape=[1, 1, 3, 3])
indices = paddle.reshape(
paddle.arange(0, 27), shape=[1, 1, 3, 3, 3]
).astype("int32")
F.max_unpool3d(data, indices, kernel_size=2, stride=2)
def indices_rank_error():
data = paddle.rand(shape=[1, 1, 3, 3, 3])
indices = paddle.reshape(
paddle.arange(0, 27), shape=[1, 3, 3, 3]
).astype("int32")
F.max_unpool3d(data, indices, kernel_size=2, stride=2)
def indices_value_error():
data = paddle.rand(shape=[1, 1, 3, 3, 3])
indices = paddle.reshape(
......@@ -238,6 +252,16 @@ class TestUnpool3DOpException(unittest.TestCase):
r"The dimensions of Input\(X\) must equal to",
indices_size_error,
)
self.assertRaisesRegex(
ValueError,
r"The x should have \[N, C, D, H, W\] format",
x_rank_error,
)
self.assertRaisesRegex(
ValueError,
r"The indices should have \[N, C, D, H, W\] format",
indices_rank_error,
)
if not core.is_compiled_with_cuda():
self.assertRaisesRegex(
ValueError,
......
......@@ -193,6 +193,20 @@ class TestUnpoolOpException(unittest.TestCase):
).astype("int32")
F.max_unpool2d(data, indices, kernel_size=2, stride=2)
def x_rank_error():
data = paddle.rand(shape=[1, 1, 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)
def indices_rank_error():
data = paddle.rand(shape=[1, 1, 3, 3])
indices = paddle.reshape(
paddle.arange(0, 9), shape=[1, 3, 3]
).astype("int32")
F.max_unpool2d(data, indices, kernel_size=2, stride=2)
def indices_value_error():
data = paddle.rand(shape=[1, 1, 3, 3])
indices = paddle.reshape(
......@@ -232,6 +246,16 @@ class TestUnpoolOpException(unittest.TestCase):
r"The dimensions of Input\(X\) must equal to",
indices_size_error,
)
self.assertRaisesRegex(
ValueError,
r"The x should have \[N, C, H, W\] format",
x_rank_error,
)
self.assertRaisesRegex(
ValueError,
r"The indices should have \[N, C, H, W\] format",
indices_rank_error,
)
if not core.is_compiled_with_cuda():
self.assertRaisesRegex(
ValueError,
......
......@@ -927,6 +927,15 @@ def max_unpool2d(
# unpool_out shape: [1, 1, 7, 7]
"""
if x.ndim != 4:
raise ValueError(
f'The x should have [N, C, H, W] format, but received {x.shape}.'
)
if indices.ndim != 4:
raise ValueError(
f'The indices should have [N, C, H, W] format, but received {indices.shape}.'
)
kernel_size = utils.convert_to_list(kernel_size, 2, 'pool_size')
if stride is None:
stride = kernel_size
......@@ -1061,6 +1070,15 @@ def max_unpool3d(
# unpool_out shape: [1, 1, 4, 4, 6]
"""
if x.ndim != 5:
raise ValueError(
f'The x should have [N, C, D, H, W] format, but received {x.shape}.'
)
if indices.ndim != 5:
raise ValueError(
f'The indices should have [N, C, D, H, W] format, but received {indices.shape}.'
)
kernel_size = utils.convert_to_list(kernel_size, 3, 'pool_size')
if stride is None:
stride = kernel_size
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册