diff --git a/paddle/fluid/operators/interpolate_v2_op.cc b/paddle/fluid/operators/interpolate_v2_op.cc index a4353420c84a9aebb15e6b9d37f52e47eabd8b31..97e39e71a556971fb16e3f2abce7a3bf93f17137 100644 --- a/paddle/fluid/operators/interpolate_v2_op.cc +++ b/paddle/fluid/operators/interpolate_v2_op.cc @@ -35,7 +35,12 @@ static void Interpolate1DInferShapeCheck(framework::InferShapeContext* ctx) { interp_method)); const DataLayout data_layout = framework::StringToDataLayout( ctx->Attrs().Get("data_layout")); - + for (int i = 0; i < dim_x.size(); ++i) { + PADDLE_ENFORCE_NE(dim_x[i], 0, platform::errors::InvalidArgument( + "The shape of input(x) should be larged " + "than 0, bug received shape[%d] is %d ", + i, dim_x[i])); + } if (ctx->HasInputs("SizeTensor")) { // top prority size auto inputs_name = ctx->Inputs("SizeTensor"); @@ -134,6 +139,13 @@ static void Interpolate2DInferShapeCheck(framework::InferShapeContext* ctx) { const DataLayout data_layout = framework::StringToDataLayout( ctx->Attrs().Get("data_layout")); + for (int i = 0; i < dim_x.size(); ++i) { + PADDLE_ENFORCE_NE(dim_x[i], 0, platform::errors::InvalidArgument( + "The shape of input(x) should be larged " + "than 0, bug received shape[%d] is %d ", + i, dim_x[i])); + } + if (ctx->HasInputs("SizeTensor")) { // top prority size auto inputs_name = ctx->Inputs("SizeTensor"); @@ -246,6 +258,13 @@ static void Interpolate3DInferShapeCheck(framework::InferShapeContext* ctx) { const DataLayout data_layout = framework::StringToDataLayout( ctx->Attrs().Get("data_layout")); + for (int i = 0; i < dim_x.size(); ++i) { + PADDLE_ENFORCE_NE(dim_x[i], 0, platform::errors::InvalidArgument( + "The shape of input(x) should be larged " + "than 0, bug received shape[%d] is %d ", + i, dim_x[i])); + } + if (ctx->HasInputs("SizeTensor")) { // top prority size auto inputs_name = ctx->Inputs("SizeTensor"); diff --git a/python/paddle/fluid/tests/unittests/test_bicubic_interp_v2_op.py b/python/paddle/fluid/tests/unittests/test_bicubic_interp_v2_op.py index b1ec74411987a73cf2e6a7d60aecce6c87ed598e..58d8d0f53d07bb71c58686b680281c5ff2045d71 100644 --- a/python/paddle/fluid/tests/unittests/test_bicubic_interp_v2_op.py +++ b/python/paddle/fluid/tests/unittests/test_bicubic_interp_v2_op.py @@ -517,6 +517,11 @@ class TestBicubicOpError(unittest.TestCase): out = interpolate( x, size={2, 2}, mode='bicubic', align_corners=False) + def test_input_shape(): + x = fluid.data(name="x", shape=[2, 1, 0, 0], dtype="float32") + out = interpolate( + x, size=[3, 3], mode="bicubic", align_corners=False) + self.assertRaises(ValueError, test_mode_type) self.assertRaises(ValueError, test_input_shape) self.assertRaises(TypeError, test_align_corcers) @@ -534,6 +539,7 @@ class TestBicubicOpError(unittest.TestCase): self.assertRaises(ValueError, test_size_and_scale) self.assertRaises(ValueError, test_size_and_scale2) self.assertRaises(TypeError, test_size_type) + self.assertRaises(ValueError, test_input_shape) if __name__ == "__main__": diff --git a/python/paddle/fluid/tests/unittests/test_trilinear_interp_v2_op.py b/python/paddle/fluid/tests/unittests/test_trilinear_interp_v2_op.py index 1f8ff4963ec3dc60402f18f332d3402444f4379a..9f46b539a04b6104bb9a363bed5d74af3fa27f00 100755 --- a/python/paddle/fluid/tests/unittests/test_trilinear_interp_v2_op.py +++ b/python/paddle/fluid/tests/unittests/test_trilinear_interp_v2_op.py @@ -21,6 +21,8 @@ import paddle.fluid.core as core import paddle.fluid as fluid from paddle.nn.functional import interpolate +np.random.seed(123) + def trilinear_interp_np(input, out_d,