Skip to content

Commit 40304ab

Browse files
authored
Refine error message in some OPs (#24443) (#24497)
test=develop
1 parent ebb3697 commit 40304ab

File tree

3 files changed

+126
-42
lines changed

3 files changed

+126
-42
lines changed

paddle/fluid/operators/detection/density_prior_box_op.cc

Lines changed: 61 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,27 @@ class DensityPriorBoxOp : public framework::OperatorWithKernel {
1919
using framework::OperatorWithKernel::OperatorWithKernel;
2020

2121
void InferShape(framework::InferShapeContext* ctx) const override {
22-
PADDLE_ENFORCE(ctx->HasInput("Input"),
23-
"Input(Input) of DensityPriorBoxOp should not be null.");
24-
PADDLE_ENFORCE(ctx->HasInput("Image"),
25-
"Input(Image) of DensityPriorBoxOp should not be null.");
22+
OP_INOUT_CHECK(ctx->HasInput("Input"), "Input", "Input",
23+
"DensityPriorBoxOp");
24+
OP_INOUT_CHECK(ctx->HasInput("Image"), "Input", "Image",
25+
"DensityPriorBoxOp");
2626

2727
auto image_dims = ctx->GetInputDim("Image");
2828
auto input_dims = ctx->GetInputDim("Input");
29-
PADDLE_ENFORCE(image_dims.size() == 4, "The layout of image is NCHW.");
30-
PADDLE_ENFORCE(input_dims.size() == 4, "The layout of input is NCHW.");
29+
PADDLE_ENFORCE_EQ(
30+
image_dims.size(), 4,
31+
platform::errors::InvalidArgument(
32+
"The Input(Image) of Op(density_prior_box) should be a 4-D Tensor "
33+
"and data format is NCHW. But received Image's dimensions = %d, "
34+
"shape = [%s].",
35+
image_dims.size(), image_dims));
36+
PADDLE_ENFORCE_EQ(
37+
input_dims.size(), 4,
38+
platform::errors::InvalidArgument(
39+
"The Input(Input) of Op(density_prior_box) should be a 4-D Tensor "
40+
"and data format is NCHW. But received Input's dimensions = %d, "
41+
"shape = [%s].",
42+
input_dims.size(), input_dims));
3143

3244
if (ctx->IsRuntime()) {
3345
PADDLE_ENFORCE_LT(
@@ -53,8 +65,13 @@ class DensityPriorBoxOp : public framework::OperatorWithKernel {
5365
auto densities = ctx->Attrs().Get<std::vector<int>>("densities");
5466
bool flatten = ctx->Attrs().Get<bool>("flatten_to_2d");
5567

56-
PADDLE_ENFORCE_EQ(fixed_sizes.size(), densities.size(),
57-
"The number of fixed_sizes and densities must be equal.");
68+
PADDLE_ENFORCE_EQ(
69+
fixed_sizes.size(), densities.size(),
70+
platform::errors::InvalidArgument(
71+
"The length of fixed_sizes and densities must be equal. "
72+
"But received: fixed_sizes's length is %d, densities's length "
73+
"is %d",
74+
fixed_sizes.size(), densities.size()));
5875
size_t num_priors = 0;
5976
for (size_t i = 0; i < densities.size(); ++i) {
6077
num_priors += (fixed_ratios.size()) * (pow(densities[i], 2));
@@ -110,10 +127,16 @@ class DensityPriorBoxOpMaker : public framework::OpProtoAndCheckerMaker {
110127
"encoded in density prior boxes.")
111128
.AddCustomChecker([](const std::vector<float>& variances) {
112129
PADDLE_ENFORCE_EQ(variances.size(), 4,
113-
"Must and only provide 4 variance.");
130+
platform::errors::InvalidArgument(
131+
"The length of variance must "
132+
"be 4. But received: variances' length is %d.",
133+
variances.size()));
114134
for (size_t i = 0; i < variances.size(); ++i) {
115135
PADDLE_ENFORCE_GT(variances[i], 0.0,
116-
"variance[%d] must be greater than 0.", i);
136+
platform::errors::OutOfRange(
137+
"variance[%d] must be greater "
138+
"than 0. But received: variance[%d] = %f",
139+
i, i, variances[i]));
117140
}
118141
});
119142
AddAttr<bool>("clip", "(bool) Whether to clip out-of-boundary boxes.")
@@ -127,14 +150,22 @@ class DensityPriorBoxOpMaker : public framework::OpProtoAndCheckerMaker {
127150
"Density prior boxes step across width, 0.0 for auto calculation.")
128151
.SetDefault(0.0)
129152
.AddCustomChecker([](const float& step_w) {
130-
PADDLE_ENFORCE_GE(step_w, 0.0, "step_w should be larger than 0.");
153+
PADDLE_ENFORCE_GE(step_w, 0.0,
154+
platform::errors::InvalidArgument(
155+
"step_w should be larger "
156+
"than 0. But received: step_w = %f.",
157+
step_w));
131158
});
132159
AddAttr<float>(
133160
"step_h",
134161
"Density prior boxes step across height, 0.0 for auto calculation.")
135162
.SetDefault(0.0)
136163
.AddCustomChecker([](const float& step_h) {
137-
PADDLE_ENFORCE_GE(step_h, 0.0, "step_h should be larger than 0.");
164+
PADDLE_ENFORCE_GE(step_h, 0.0,
165+
platform::errors::InvalidArgument(
166+
"step_h should be larger "
167+
"than 0. But received: step_h = %f.",
168+
step_h));
138169
});
139170

140171
AddAttr<float>("offset",
@@ -147,8 +178,12 @@ class DensityPriorBoxOpMaker : public framework::OpProtoAndCheckerMaker {
147178
.SetDefault(std::vector<float>{})
148179
.AddCustomChecker([](const std::vector<float>& fixed_sizes) {
149180
for (size_t i = 0; i < fixed_sizes.size(); ++i) {
150-
PADDLE_ENFORCE_GT(fixed_sizes[i], 0.0,
151-
"fixed_sizes[%d] should be larger than 0.", i);
181+
PADDLE_ENFORCE_GT(
182+
fixed_sizes[i], 0.0,
183+
platform::errors::OutOfRange(
184+
"fixed_sizes[%d] should be "
185+
"larger than 0. But received: fixed_sizes[%d] = %f",
186+
i, i, fixed_sizes[i]));
152187
}
153188
});
154189

@@ -158,8 +193,12 @@ class DensityPriorBoxOpMaker : public framework::OpProtoAndCheckerMaker {
158193
.SetDefault(std::vector<float>{})
159194
.AddCustomChecker([](const std::vector<float>& fixed_ratios) {
160195
for (size_t i = 0; i < fixed_ratios.size(); ++i) {
161-
PADDLE_ENFORCE_GT(fixed_ratios[i], 0.0,
162-
"fixed_ratios[%d] should be larger than 0.", i);
196+
PADDLE_ENFORCE_GT(
197+
fixed_ratios[i], 0.0,
198+
platform::errors::OutOfRange(
199+
"fixed_ratios[%d] should be "
200+
"larger than 0. But received: fixed_ratios[%d] = %f",
201+
i, i, fixed_ratios[i]));
163202
}
164203
});
165204

@@ -169,8 +208,12 @@ class DensityPriorBoxOpMaker : public framework::OpProtoAndCheckerMaker {
169208
.SetDefault(std::vector<int>{})
170209
.AddCustomChecker([](const std::vector<int>& densities) {
171210
for (size_t i = 0; i < densities.size(); ++i) {
172-
PADDLE_ENFORCE_GT(densities[i], 0,
173-
"densities[%d] should be larger than 0.", i);
211+
PADDLE_ENFORCE_GT(
212+
densities[i], 0,
213+
platform::errors::OutOfRange(
214+
"densities[%d] should be "
215+
"larger than 0. But received: densities[%d] = %f.",
216+
i, i, densities[i]));
174217
}
175218
});
176219
AddComment(R"DOC(

paddle/fluid/operators/detection/prior_box_op.cc

Lines changed: 57 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,26 @@ class PriorBoxOp : public framework::OperatorWithKernel {
2626
using framework::OperatorWithKernel::OperatorWithKernel;
2727

2828
void InferShape(framework::InferShapeContext* ctx) const override {
29-
PADDLE_ENFORCE(ctx->HasInput("Input"),
30-
"Input(Input) of PriorBoxOp should not be null.");
31-
PADDLE_ENFORCE(ctx->HasInput("Image"),
32-
"Input(Image) of PriorBoxOp should not be null.");
29+
OP_INOUT_CHECK(ctx->HasInput("Input"), "Input", "Input", "PriorBoxOp");
30+
OP_INOUT_CHECK(ctx->HasInput("Image"), "Input", "Image", "PriorBoxOp");
3331

3432
auto image_dims = ctx->GetInputDim("Image");
3533
auto input_dims = ctx->GetInputDim("Input");
36-
PADDLE_ENFORCE(image_dims.size() == 4, "The layout of image is NCHW.");
37-
PADDLE_ENFORCE(input_dims.size() == 4, "The layout of input is NCHW.");
34+
35+
PADDLE_ENFORCE_EQ(
36+
image_dims.size(), 4,
37+
platform::errors::InvalidArgument(
38+
"The Input(Image) of Op(PriorBoxOp) should be a 4-D Tensor "
39+
"and data format is NCHW. But received Image's dimensions = %d, "
40+
"shape = [%s].",
41+
image_dims.size(), image_dims));
42+
PADDLE_ENFORCE_EQ(
43+
input_dims.size(), 4,
44+
platform::errors::InvalidArgument(
45+
"The Input(Input) of Op(PriorBoxOp) should be a 4-D Tensor "
46+
"and data format is NCHW. But received Input's dimensions = %d, "
47+
"shape = [%s].",
48+
input_dims.size(), input_dims));
3849

3950
auto min_sizes = ctx->Attrs().Get<std::vector<float>>("min_sizes");
4051
auto max_sizes = ctx->Attrs().Get<std::vector<float>>("max_sizes");
@@ -47,13 +58,22 @@ class PriorBoxOp : public framework::OperatorWithKernel {
4758

4859
size_t num_priors = aspect_ratios_vec.size() * min_sizes.size();
4960
if (max_sizes.size() > 0) {
50-
PADDLE_ENFORCE_EQ(max_sizes.size(), min_sizes.size(),
51-
"The number of min_size and max_size must be equal.");
61+
PADDLE_ENFORCE_EQ(
62+
max_sizes.size(), min_sizes.size(),
63+
platform::errors::InvalidArgument(
64+
"The length of min_size and "
65+
"max_size must be equal. But received: min_size's length is %d, "
66+
"max_size's length is %d.",
67+
min_sizes.size(), max_sizes.size()));
5268
num_priors += max_sizes.size();
5369
for (size_t i = 0; i < max_sizes.size(); ++i) {
54-
PADDLE_ENFORCE_GT(max_sizes[i], min_sizes[i],
55-
"max_size[%d] must be greater than min_size[%d].", i,
56-
i);
70+
PADDLE_ENFORCE_GT(
71+
max_sizes[i], min_sizes[i],
72+
platform::errors::InvalidArgument(
73+
"max_size[%d] must be greater "
74+
"than min_size[%d]. But received: max_size[%d] is %f, "
75+
"min_size[%d] is %f.",
76+
i, i, i, max_sizes[i], i, min_sizes[i]));
5777
}
5878
}
5979

@@ -121,11 +141,16 @@ class PriorBoxOpMaker : public framework::OpProtoAndCheckerMaker {
121141
"(vector<float>) List of min sizes "
122142
"of generated prior boxes.")
123143
.AddCustomChecker([](const std::vector<float>& min_sizes) {
124-
PADDLE_ENFORCE_GT(min_sizes.size(), 0,
125-
"Size of min_sizes must be at least 1.");
144+
PADDLE_ENFORCE_GT(
145+
min_sizes.size(), 0,
146+
platform::errors::InvalidArgument("Size of min_sizes must be "
147+
"at least 1."));
126148
for (size_t i = 0; i < min_sizes.size(); ++i) {
127149
PADDLE_ENFORCE_GT(min_sizes[i], 0.0,
128-
"min_sizes[%d] must be positive.", i);
150+
platform::errors::OutOfRange(
151+
"min_sizes[%d] must be larger "
152+
"than 0. But received: min_sizes[%d] is %f.",
153+
i, i, min_sizes[i]));
129154
}
130155
});
131156
AddAttr<std::vector<float>>(
@@ -141,10 +166,16 @@ class PriorBoxOpMaker : public framework::OpProtoAndCheckerMaker {
141166
"(vector<float>) List of variances to be encoded in prior boxes.")
142167
.AddCustomChecker([](const std::vector<float>& variances) {
143168
PADDLE_ENFORCE_EQ(variances.size(), 4,
144-
"Must and only provide 4 variance.");
169+
platform::errors::InvalidArgument(
170+
"The length of variance must "
171+
"be 4. But received: variances' length is %d.",
172+
variances.size()));
145173
for (size_t i = 0; i < variances.size(); ++i) {
146174
PADDLE_ENFORCE_GT(variances[i], 0.0,
147-
"variance[%d] must be greater than 0.", i);
175+
platform::errors::OutOfRange(
176+
"variance[%d] must be greater "
177+
"than 0. But received: variance[%d] = %f",
178+
i, i, variances[i]));
148179
}
149180
});
150181
AddAttr<bool>("flip", "(bool) Whether to flip aspect ratios.")
@@ -156,13 +187,21 @@ class PriorBoxOpMaker : public framework::OpProtoAndCheckerMaker {
156187
"Prior boxes step across width, 0.0 for auto calculation.")
157188
.SetDefault(0.0)
158189
.AddCustomChecker([](const float& step_w) {
159-
PADDLE_ENFORCE_GE(step_w, 0.0, "step_w should be larger than 0.");
190+
PADDLE_ENFORCE_GE(step_w, 0.0,
191+
platform::errors::InvalidArgument(
192+
"step_w should be larger "
193+
"than 0. But received: step_w = %f.",
194+
step_w));
160195
});
161196
AddAttr<float>("step_h",
162197
"Prior boxes step across height, 0.0 for auto calculation.")
163198
.SetDefault(0.0)
164199
.AddCustomChecker([](const float& step_h) {
165-
PADDLE_ENFORCE_GE(step_h, 0.0, "step_h should be larger than 0.");
200+
PADDLE_ENFORCE_GE(step_h, 0.0,
201+
platform::errors::InvalidArgument(
202+
"step_h should be larger "
203+
"than 0. But received: step_h = %f.",
204+
step_h));
166205
});
167206

168207
AddAttr<float>("offset",

python/paddle/fluid/layers/detection.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1754,6 +1754,8 @@ def prior_box(input,
17541754
"""
17551755
helper = LayerHelper("prior_box", **locals())
17561756
dtype = helper.input_dtype()
1757+
check_variable_and_dtype(
1758+
input, 'input', ['uint8', 'int8', 'float32', 'float64'], 'prior_box')
17571759

17581760
def _is_list_or_tuple_(data):
17591761
return (isinstance(data, list) or isinstance(data, tuple))
@@ -1932,18 +1934,18 @@ def density_prior_box(input,
19321934
"""
19331935
helper = LayerHelper("density_prior_box", **locals())
19341936
dtype = helper.input_dtype()
1937+
check_variable_and_dtype(input, 'input', ['float32', 'float64'],
1938+
'density_prior_box')
19351939

19361940
def _is_list_or_tuple_(data):
19371941
return (isinstance(data, list) or isinstance(data, tuple))
19381942

1939-
if not _is_list_or_tuple_(densities):
1940-
raise TypeError('densities should be a list or a tuple or None.')
1941-
if not _is_list_or_tuple_(fixed_sizes):
1942-
raise TypeError('fixed_sizes should be a list or a tuple or None.')
1943-
if not _is_list_or_tuple_(fixed_ratios):
1944-
raise TypeError('fixed_ratios should be a list or a tuple or None.')
1943+
check_type(densities, 'densities', (list, tuple), 'density_prior_box')
1944+
check_type(fixed_sizes, 'fixed_sizes', (list, tuple), 'density_prior_box')
1945+
check_type(fixed_ratios, 'fixed_ratios', (list, tuple), 'density_prior_box')
19451946
if len(densities) != len(fixed_sizes):
19461947
raise ValueError('densities and fixed_sizes length should be euqal.')
1948+
19471949
if not (_is_list_or_tuple_(steps) and len(steps) == 2):
19481950
raise ValueError('steps should be a list or tuple ',
19491951
'with length 2, (step_width, step_height).')

0 commit comments

Comments
 (0)