diff --git a/python/paddle/fluid/tests/unittests/test_psroi_pool_op.py b/python/paddle/fluid/tests/unittests/test_psroi_pool_op.py index 40f3c52d4fc030fc9001038afb73fffb8f6f47c7..c33d218cd862126360d25955430b18c289d41844 100644 --- a/python/paddle/fluid/tests/unittests/test_psroi_pool_op.py +++ b/python/paddle/fluid/tests/unittests/test_psroi_pool_op.py @@ -339,6 +339,22 @@ class TestPSROIPoolChannelError(unittest.TestCase): self.assertRaises(ValueError, test_channel_error) +class TestPSROIPoolZeroDivError(unittest.TestCase): + def setUp(self): + paddle.disable_static() + self.x = paddle.uniform([2, 490, 28, 28], dtype='float32') + self.boxes = paddle.to_tensor( + [[1, 5, 8, 10], [4, 2, 6, 7], [12, 12, 19, 21]], dtype='float32' + ) + self.boxes_num = paddle.to_tensor([1, 2], dtype='int32') + + def test_errors(self): + def test_zero_div_error(): + paddle.vision.ops.psroi_pool(self.x, self.boxes, self.boxes_num, 0) + + self.assertRaises(ValueError, test_zero_div_error) + + class TestPSROIPoolStaticAPI(unittest.TestCase): def setUp(self): paddle.enable_static() diff --git a/python/paddle/vision/ops.py b/python/paddle/vision/ops.py index 0696b5f7cc6a1ade175ec5e537f2d865437a2d0e..0d43bd0fc54ce894aa9de9ae2c0f79bd24c6d5a0 100755 --- a/python/paddle/vision/ops.py +++ b/python/paddle/vision/ops.py @@ -1424,6 +1424,8 @@ def psroi_pool(x, boxes, boxes_num, output_size, spatial_scale=1.0, name=None): output_size = (output_size, output_size) pooled_height, pooled_width = output_size assert len(x.shape) == 4, "Input features with shape should be (N, C, H, W)" + if pooled_height * pooled_width == 0: + raise ValueError('output_size should not contain 0.') output_channels = int(x.shape[1] / (pooled_height * pooled_width)) if in_dygraph_mode(): return _C_ops.psroi_pool(