提交 f0e95a60 编写于 作者: L liym27 提交者: Aurelius84

Polish error messages of pool_2d/3d and add Raises in English document. test=develop (#21017)

上级 fa522dff
...@@ -38,10 +38,10 @@ int PoolOutputSize(int input_size, int filter_size, int padding_1, ...@@ -38,10 +38,10 @@ int PoolOutputSize(int input_size, int filter_size, int padding_1,
} }
PADDLE_ENFORCE_GT( PADDLE_ENFORCE_GT(
output_size, 0, output_size, 0,
"Due to the settings of padding(%d,%d), filter_size(%d) and " "ShapeError: the output size must be greater than 0. But received: "
"stride(%d), the output size is less than 0, please check " "output_size = %d due to the settings of input_size(%d), padding(%d,%d), "
"again. Input_size:%d", "k_size(%d) and stride(%d). Please check again!",
padding_1, padding_2, filter_size, stride, input_size); output_size, input_size, padding_1, padding_2, filter_size, stride);
return output_size; return output_size;
} }
...@@ -63,13 +63,30 @@ void PoolOp::InferShape(framework::InferShapeContext* ctx) const { ...@@ -63,13 +63,30 @@ void PoolOp::InferShape(framework::InferShapeContext* ctx) const {
ctx->Attrs().Get<std::string>("padding_algorithm"); ctx->Attrs().Get<std::string>("padding_algorithm");
auto in_x_dims = ctx->GetInputDim("X"); auto in_x_dims = ctx->GetInputDim("X");
PADDLE_ENFORCE_EQ(in_x_dims.size() == 4 || in_x_dims.size() == 5, true, PADDLE_ENFORCE_EQ(
"Pooling intput should be 4-D or 5-D tensor."); 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(), 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"); const bool channel_last = (data_format == "NHWC" || data_format == "NDHWC");
...@@ -91,7 +108,7 @@ void PoolOp::InferShape(framework::InferShapeContext* ctx) const { ...@@ -91,7 +108,7 @@ void PoolOp::InferShape(framework::InferShapeContext* ctx) const {
if (adaptive) { if (adaptive) {
output_shape.insert(output_shape.end(), ksize.begin(), ksize.end()); output_shape.insert(output_shape.end(), ksize.begin(), ksize.end());
} else { } 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)) { if ((!ctx->IsRuntime()) && (data_dims[i] < 0)) {
output_shape.push_back(data_dims[i]); output_shape.push_back(data_dims[i]);
} else { } else {
......
...@@ -66,7 +66,7 @@ inline void UpdatePadding(std::vector<int>* paddings, const bool global_pooling, ...@@ -66,7 +66,7 @@ inline void UpdatePadding(std::vector<int>* paddings, const bool global_pooling,
// set padding size == data_dims.size() * 2 // set padding size == data_dims.size() * 2
auto data_shape = framework::vectorize<int>(data_dims); auto data_shape = framework::vectorize<int>(data_dims);
if (paddings->size() == data_dims.size()) { 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); int copy_pad = *(paddings->begin() + 2 * i);
paddings->insert(paddings->begin() + 2 * i + 1, copy_pad); paddings->insert(paddings->begin() + 2 * i + 1, copy_pad);
} }
...@@ -78,7 +78,7 @@ inline void UpdatePadding(std::vector<int>* paddings, const bool global_pooling, ...@@ -78,7 +78,7 @@ inline void UpdatePadding(std::vector<int>* paddings, const bool global_pooling,
// when padding_desc is "VALID" or "SAME" // when padding_desc is "VALID" or "SAME"
if (padding_algorithm == "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 out_size = (data_dims[i] + strides[i] - 1) / strides[0];
int pad_sum = int pad_sum =
std::max((out_size - 1) * strides[i] + ksize[i] - data_shape[i], 0); std::max((out_size - 1) * strides[i] + ksize[i] - data_shape[i], 0);
......
...@@ -3458,9 +3458,18 @@ def pool2d(input, ...@@ -3458,9 +3458,18 @@ def pool2d(input,
Variable: The output tensor of pooling result. The data type is same as input tensor. Variable: The output tensor of pooling result. The data type is same as input tensor.
Raises: Raises:
ValueError: If `pool_type` is not "max" nor "avg" ValueError: If `pool_type` is not "max" nor "avg".
ValueError: If `global_pooling` is False and `pool_size` is -1 ValueError: If `global_pooling` is False and `pool_size` is -1.
ValueError: If `use_cudnn` is not a bool value. 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: Examples:
...@@ -3523,8 +3532,8 @@ def pool2d(input, ...@@ -3523,8 +3532,8 @@ def pool2d(input,
"and be a valid value. Received pool_size: %s." % str(pool_size)) "and be a valid value. Received pool_size: %s." % str(pool_size))
if not isinstance(use_cudnn, bool): if not isinstance(use_cudnn, bool):
raise ValueError("Attr(use_cudnn) should be True or False. Received " raise TypeError("Attr(use_cudnn) should be True or False. Received "
"Attr(use_cudnn): %s." % str(use_cudnn)) "Attr(use_cudnn): %s." % str(use_cudnn))
if data_format not in ["NCHW", "NHWC"]: if data_format not in ["NCHW", "NHWC"]:
raise ValueError( raise ValueError(
...@@ -3663,6 +3672,19 @@ def pool3d(input, ...@@ -3663,6 +3672,19 @@ def pool3d(input,
Returns: Returns:
Variable: The output tensor of pooling result. The data type is same as input tensor. 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: Examples:
.. code-block:: python .. code-block:: python
...@@ -3730,8 +3752,8 @@ def pool3d(input, ...@@ -3730,8 +3752,8 @@ def pool3d(input,
str(pool_size)) str(pool_size))
if not isinstance(use_cudnn, bool): if not isinstance(use_cudnn, bool):
raise ValueError("Attr(use_cudnn) should be True or False. Received " raise TypeError("Attr(use_cudnn) should be True or False. Received "
"Attr(use_cudnn): %s. " % str(use_cudnn)) "Attr(use_cudnn): %s. " % str(use_cudnn))
if data_format not in ["NCDHW", "NDHWC"]: if data_format not in ["NCDHW", "NDHWC"]:
raise ValueError( raise ValueError(
......
...@@ -1179,7 +1179,7 @@ class TestPool2dAPI_Error(OpTest): ...@@ -1179,7 +1179,7 @@ class TestPool2dAPI_Error(OpTest):
dtype="float32") dtype="float32")
ksize = [3, 3] ksize = [3, 3]
# cudnn value error # cudnn type error
def run_1(): def run_1():
out_1 = fluid.layers.pool2d( out_1 = fluid.layers.pool2d(
input=input_NHWC, input=input_NHWC,
...@@ -1189,7 +1189,7 @@ class TestPool2dAPI_Error(OpTest): ...@@ -1189,7 +1189,7 @@ class TestPool2dAPI_Error(OpTest):
use_cudnn=[0], use_cudnn=[0],
data_format="NHWC") data_format="NHWC")
self.assertRaises(ValueError, run_1) self.assertRaises(TypeError, run_1)
# data_format value error # data_format value error
def run_2(): def run_2():
......
...@@ -1086,7 +1086,7 @@ class TestPool3dAPI_Error(OpTest): ...@@ -1086,7 +1086,7 @@ class TestPool3dAPI_Error(OpTest):
dtype="float32") dtype="float32")
ksize = [3, 3, 3] ksize = [3, 3, 3]
# cudnn value error # cudnn type error
def run_1(): def run_1():
out_1 = fluid.layers.pool3d( out_1 = fluid.layers.pool3d(
input=input_NDHWC, input=input_NDHWC,
...@@ -1096,7 +1096,7 @@ class TestPool3dAPI_Error(OpTest): ...@@ -1096,7 +1096,7 @@ class TestPool3dAPI_Error(OpTest):
use_cudnn=[0], use_cudnn=[0],
data_format="NDHWC") data_format="NDHWC")
self.assertRaises(ValueError, run_1) self.assertRaises(TypeError, run_1)
# data_format value error # data_format value error
def run_2(): def run_2():
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册