From f0e95a60491c6e26d8fd96bec5d16591d9531560 Mon Sep 17 00:00:00 2001 From: liym27 <33742067+liym27@users.noreply.github.com> Date: Wed, 6 Nov 2019 14:42:11 +0800 Subject: [PATCH] Polish error messages of pool_2d/3d and add Raises in English document. test=develop (#21017) --- paddle/fluid/operators/pool_op.cc | 37 ++++++++++++++----- paddle/fluid/operators/pool_op.h | 4 +- python/paddle/fluid/layers/nn.py | 36 ++++++++++++++---- .../fluid/tests/unittests/test_pool2d_op.py | 4 +- .../fluid/tests/unittests/test_pool3d_op.py | 4 +- 5 files changed, 62 insertions(+), 23 deletions(-) diff --git a/paddle/fluid/operators/pool_op.cc b/paddle/fluid/operators/pool_op.cc index 817ad336cf6..469e123d9b0 100644 --- a/paddle/fluid/operators/pool_op.cc +++ b/paddle/fluid/operators/pool_op.cc @@ -38,10 +38,10 @@ int PoolOutputSize(int input_size, int filter_size, int padding_1, } PADDLE_ENFORCE_GT( output_size, 0, - "Due to the settings of padding(%d,%d), filter_size(%d) and " - "stride(%d), the output size is less than 0, please check " - "again. Input_size:%d", - padding_1, padding_2, filter_size, stride, input_size); + "ShapeError: the output size must be greater than 0. But received: " + "output_size = %d due to the settings of input_size(%d), padding(%d,%d), " + "k_size(%d) and stride(%d). Please check again!", + output_size, input_size, padding_1, padding_2, filter_size, stride); return output_size; } @@ -63,13 +63,30 @@ void PoolOp::InferShape(framework::InferShapeContext* ctx) const { ctx->Attrs().Get("padding_algorithm"); auto in_x_dims = ctx->GetInputDim("X"); - PADDLE_ENFORCE_EQ(in_x_dims.size() == 4 || in_x_dims.size() == 5, true, - "Pooling intput should be 4-D or 5-D tensor."); + PADDLE_ENFORCE_EQ( + in_x_dims.size() == 4 || in_x_dims.size() == 5, true, + "ShapeError: the input of Op(pool) should be 4-D or 5-D Tensor. But " + "received: %u-D Tensor and it's shape is [%s].", + in_x_dims.size(), in_x_dims); + + PADDLE_ENFORCE_EQ( + in_x_dims.size() - ksize.size(), 2U, + "ShapeError: the dimension of input minus the size of " + "Attr(ksize) must be euqal to 2 in Op(pool). " + "But received: the dimension of input minus the size " + "of Attr(ksize) is %d, the " + "input's dimension is %d, the shape of input " + "is [%s], the Attr(ksize)'s size is %d, the Attr(ksize) is [%s].", + in_x_dims.size() - ksize.size(), in_x_dims.size(), in_x_dims, + ksize.size(), framework::make_ddim(ksize)); - PADDLE_ENFORCE_EQ(in_x_dims.size() - ksize.size(), 2U, - "Input size and pooling size should be consistent."); PADDLE_ENFORCE_EQ(ksize.size(), strides.size(), - "Strides size and pooling size should be the same."); + "ShapeError: the size of Attr(ksize) and Attr(strides) in " + "Op(pool) must be equal. " + "But received: Attr(ksize)'s size is %d, Attr(strides)'s " + "size is %d, Attr(ksize) is [%s], Attr(strides)is [%s].", + ksize.size(), strides.size(), framework::make_ddim(ksize), + framework::make_ddim(strides)); const bool channel_last = (data_format == "NHWC" || data_format == "NDHWC"); @@ -91,7 +108,7 @@ void PoolOp::InferShape(framework::InferShapeContext* ctx) const { if (adaptive) { output_shape.insert(output_shape.end(), ksize.begin(), ksize.end()); } else { - for (size_t i = 0; i < data_dims.size(); ++i) { + for (int i = 0; i < data_dims.size(); ++i) { if ((!ctx->IsRuntime()) && (data_dims[i] < 0)) { output_shape.push_back(data_dims[i]); } else { diff --git a/paddle/fluid/operators/pool_op.h b/paddle/fluid/operators/pool_op.h index d7c6c6230a8..eec989611ce 100644 --- a/paddle/fluid/operators/pool_op.h +++ b/paddle/fluid/operators/pool_op.h @@ -66,7 +66,7 @@ inline void UpdatePadding(std::vector* paddings, const bool global_pooling, // set padding size == data_dims.size() * 2 auto data_shape = framework::vectorize(data_dims); if (paddings->size() == data_dims.size()) { - for (size_t i = 0; i < data_dims.size(); ++i) { + for (int i = 0; i < data_dims.size(); ++i) { int copy_pad = *(paddings->begin() + 2 * i); paddings->insert(paddings->begin() + 2 * i + 1, copy_pad); } @@ -78,7 +78,7 @@ inline void UpdatePadding(std::vector* paddings, const bool global_pooling, // when padding_desc is "VALID" or "SAME" if (padding_algorithm == "SAME") { - for (size_t i = 0; i < data_dims.size(); ++i) { + for (int i = 0; i < data_dims.size(); ++i) { int out_size = (data_dims[i] + strides[i] - 1) / strides[0]; int pad_sum = std::max((out_size - 1) * strides[i] + ksize[i] - data_shape[i], 0); diff --git a/python/paddle/fluid/layers/nn.py b/python/paddle/fluid/layers/nn.py index 85c9e982534..ed35916c34e 100755 --- a/python/paddle/fluid/layers/nn.py +++ b/python/paddle/fluid/layers/nn.py @@ -3458,9 +3458,18 @@ def pool2d(input, Variable: The output tensor of pooling result. The data type is same as input tensor. Raises: - ValueError: If `pool_type` is not "max" nor "avg" - ValueError: If `global_pooling` is False and `pool_size` is -1 - ValueError: If `use_cudnn` is not a bool value. + ValueError: If `pool_type` is not "max" nor "avg". + ValueError: If `global_pooling` is False and `pool_size` is -1. + TypeError: If `use_cudnn` is not a bool value. + ValueError: If `data_format` is not "NCHW" or "NHWC". + ValueError: If `pool_padding` is a string, but not "SAME" or "VALID". + ValueError: If `pool_padding` is "VALID", but `ceil_mode` is True. + ValueError: If `pool_padding` is a list or tuple, but the elements in the batch or channel dimensions are non-zero. + ShapeError: If the input is not a 4-D or 5-D Tensor. + ShapeError: If the dimension of input minus the size of `pool_stride` is not 2. + ShapeError: If the size of `pool_size` and `pool_stride` is not equal. + ShapeError: If the output's shape calculated is not greater than 0. + Examples: @@ -3523,8 +3532,8 @@ def pool2d(input, "and be a valid value. Received pool_size: %s." % str(pool_size)) if not isinstance(use_cudnn, bool): - raise ValueError("Attr(use_cudnn) should be True or False. Received " - "Attr(use_cudnn): %s." % str(use_cudnn)) + raise TypeError("Attr(use_cudnn) should be True or False. Received " + "Attr(use_cudnn): %s." % str(use_cudnn)) if data_format not in ["NCHW", "NHWC"]: raise ValueError( @@ -3663,6 +3672,19 @@ def pool3d(input, Returns: Variable: The output tensor of pooling result. The data type is same as input tensor. + Raises: + ValueError: If `pool_type` is not "max" nor "avg". + ValueError: If `global_pooling` is False and `pool_size` is -1. + TypeError: If `use_cudnn` is not a bool value. + ValueError: If `data_format` is not "NCDHW" or "NDHWC". + ValueError: If `pool_padding` is a string, but not "SAME" or "VALID". + ValueError: If `pool_padding` is "VALID", but `ceil_mode` is True. + ValueError: If `pool_padding` is a list or tuple, but the elements in the batch or channel dimensions are non-zero. + ShapeError: If the input is not a 4-D or 5-D Tensor. + ShapeError: If the dimension of input minus the size of `pool_stride` is not 2. + ShapeError: If the size of `pool_size` and `pool_stride` is not equal. + ShapeError: If the output's shape calculated is not greater than 0. + Examples: .. code-block:: python @@ -3730,8 +3752,8 @@ def pool3d(input, str(pool_size)) if not isinstance(use_cudnn, bool): - raise ValueError("Attr(use_cudnn) should be True or False. Received " - "Attr(use_cudnn): %s. " % str(use_cudnn)) + raise TypeError("Attr(use_cudnn) should be True or False. Received " + "Attr(use_cudnn): %s. " % str(use_cudnn)) if data_format not in ["NCDHW", "NDHWC"]: raise ValueError( diff --git a/python/paddle/fluid/tests/unittests/test_pool2d_op.py b/python/paddle/fluid/tests/unittests/test_pool2d_op.py index d5cc142b2a8..3f66d41244e 100644 --- a/python/paddle/fluid/tests/unittests/test_pool2d_op.py +++ b/python/paddle/fluid/tests/unittests/test_pool2d_op.py @@ -1179,7 +1179,7 @@ class TestPool2dAPI_Error(OpTest): dtype="float32") ksize = [3, 3] - # cudnn value error + # cudnn type error def run_1(): out_1 = fluid.layers.pool2d( input=input_NHWC, @@ -1189,7 +1189,7 @@ class TestPool2dAPI_Error(OpTest): use_cudnn=[0], data_format="NHWC") - self.assertRaises(ValueError, run_1) + self.assertRaises(TypeError, run_1) # data_format value error def run_2(): diff --git a/python/paddle/fluid/tests/unittests/test_pool3d_op.py b/python/paddle/fluid/tests/unittests/test_pool3d_op.py index 6bbc3981b1c..4865e9d5ab1 100644 --- a/python/paddle/fluid/tests/unittests/test_pool3d_op.py +++ b/python/paddle/fluid/tests/unittests/test_pool3d_op.py @@ -1086,7 +1086,7 @@ class TestPool3dAPI_Error(OpTest): dtype="float32") ksize = [3, 3, 3] - # cudnn value error + # cudnn type error def run_1(): out_1 = fluid.layers.pool3d( input=input_NDHWC, @@ -1096,7 +1096,7 @@ class TestPool3dAPI_Error(OpTest): use_cudnn=[0], data_format="NDHWC") - self.assertRaises(ValueError, run_1) + self.assertRaises(TypeError, run_1) # data_format value error def run_2(): -- GitLab