未验证 提交 93aa6690 编写于 作者: W wangguanzhong 提交者: GitHub

enhance the error message, test=develop (#23646)

* enhance the error message of box_coder, test=develop

* enhance error message in box_decoder_and_assign

* enhance error message of collect_fpn_proposals

* enhance errror message of distribute_fpn_proposals

* enhance error message of multiclass_nms, test=develop
上级 7de0a25b
......@@ -21,29 +21,47 @@ class BoxCoderOp : public framework::OperatorWithKernel {
protected:
void InferShape(framework::InferShapeContext *ctx) const override {
PADDLE_ENFORCE(ctx->HasInput("PriorBox"),
"Input(PriorBox) of BoxCoderOp should not be null.");
PADDLE_ENFORCE(ctx->HasInput("TargetBox"),
"Input(TargetBox) of BoxCoderOp should not be null.");
PADDLE_ENFORCE(ctx->HasOutput("OutputBox"),
"Output(OutputBox) of BoxCoderOp should not be null.");
PADDLE_ENFORCE_EQ(
ctx->HasInput("PriorBox"), true,
platform::errors::NotFound(
"Input(PriorBox) of BoxCoder operator is not found."));
PADDLE_ENFORCE_EQ(
ctx->HasInput("TargetBox"), true,
platform::errors::NotFound(
"Input(TargetBox) of BoxCoder operator is not found."));
PADDLE_ENFORCE_EQ(
ctx->HasOutput("OutputBox"), true,
platform::errors::NotFound(
"Output(OutputBox) of BoxCoder operator is not found."));
auto prior_box_dims = ctx->GetInputDim("PriorBox");
auto target_box_dims = ctx->GetInputDim("TargetBox");
if (ctx->IsRuntime()) {
PADDLE_ENFORCE_EQ(prior_box_dims.size(), 2,
"The rank of Input PriorBox must be 2");
platform::errors::InvalidArgument(
"The rank of Input PriorBox in BoxCoder operator "
"must be 2. But received rank = %d",
prior_box_dims.size()));
PADDLE_ENFORCE_EQ(prior_box_dims[1], 4,
"The shape of PriorBox is [N, 4]");
platform::errors::InvalidArgument(
"The second dimension of PriorBox in BoxCoder "
"operator must be 4. But received dimension = %d",
prior_box_dims[1]));
if (ctx->HasInput("PriorBoxVar")) {
auto prior_box_var_dims = ctx->GetInputDim("PriorBoxVar");
PADDLE_ENFORCE(prior_box_var_dims.size() == 2,
"Input(PriorBoxVar) of BoxCoderOp should be 2.");
PADDLE_ENFORCE_EQ(
prior_box_var_dims.size(), 2,
platform::errors::InvalidArgument(
"The rank of Input(PriorBoxVar) in BoxCoder operator"
" should be 2. But received rank = %d",
prior_box_var_dims.size()));
PADDLE_ENFORCE_EQ(
prior_box_dims, prior_box_var_dims,
"The dimension of Input(PriorBoxVar) should be equal to"
"the dimension of Input(PriorBox) when the rank is 2.");
platform::errors::InvalidArgument(
"The dimension of Input(PriorBoxVar) should be equal to"
"the dimension of Input(PriorBox) in BoxCoder operator "
"when the rank is 2."));
}
}
......@@ -51,23 +69,50 @@ class BoxCoderOp : public framework::OperatorWithKernel {
int axis = ctx->Attrs().Get<int>("axis");
if (code_type == BoxCodeType::kEncodeCenterSize) {
PADDLE_ENFORCE_EQ(target_box_dims.size(), 2,
"The rank of Input TargetBox must be 2");
platform::errors::InvalidArgument(
"The rank of Input TargetBox in BoxCoder operator "
"must be 2. But received rank is %d",
target_box_dims.size()));
PADDLE_ENFORCE_EQ(target_box_dims[1], 4,
"The shape of TargetBox is [M, 4]");
platform::errors::InvalidArgument(
"The second dimension of TargetBox in BoxCoder "
"operator is 4. But received dimension is %d",
target_box_dims[1]));
ctx->SetOutputDim(
"OutputBox",
framework::make_ddim({target_box_dims[0], prior_box_dims[0], 4}));
} else if (code_type == BoxCodeType::kDecodeCenterSize) {
PADDLE_ENFORCE_EQ(target_box_dims.size(), 3,
"The rank of Input TargetBox must be 3");
PADDLE_ENFORCE(axis == 0 || axis == 1, "axis must be 0 or 1");
platform::errors::InvalidArgument(
"The rank of Input TargetBox in BoxCoder "
"operator must be 3. But received rank is %d",
target_box_dims.size()));
PADDLE_ENFORCE_EQ(axis == 0 || axis == 1, true,
platform::errors::InvalidArgument(
"axis in BoxCoder operator must be 0 or 1."
"But received axis = %d",
axis));
if (ctx->IsRuntime()) {
if (axis == 0) {
PADDLE_ENFORCE_EQ(target_box_dims[1], prior_box_dims[0]);
PADDLE_ENFORCE_EQ(
target_box_dims[1], prior_box_dims[0],
platform::errors::InvalidArgument(
"When axis is 0, The second "
"dimension of TargetBox in BoxCoder "
"should be equal to the first dimension of PriorBox."));
} else if (axis == 1) {
PADDLE_ENFORCE_EQ(target_box_dims[0], prior_box_dims[0]);
PADDLE_ENFORCE_EQ(
target_box_dims[0], prior_box_dims[0],
platform::errors::InvalidArgument(
"When axis is 1, The first "
"dimension of TargetBox in BoxCoder "
"should be equal to the first dimension of PriorBox."));
}
PADDLE_ENFORCE_EQ(target_box_dims[2], prior_box_dims[1]);
PADDLE_ENFORCE_EQ(target_box_dims[2], prior_box_dims[1],
platform::errors::InvalidArgument(
"The third dimension of TargetBox"
" in BoxCoder should be equal to the "
"second dimension of PriorBox."));
}
ctx->ShareDim("TargetBox", /*->*/ "OutputBox");
}
......
......@@ -131,8 +131,6 @@ template <typename DeviceContext, typename T>
class BoxCoderCUDAKernel : public framework::OpKernel<T> {
public:
void Compute(const framework::ExecutionContext& context) const override {
PADDLE_ENFORCE(platform::is_gpu_place(context.GetPlace()),
"This kernel only runs on GPU device.");
auto* prior_box = context.Input<framework::Tensor>("PriorBox");
auto* prior_box_var = context.Input<framework::Tensor>("PriorBoxVar");
auto* target_box = context.Input<framework::LoDTensor>("TargetBox");
......@@ -143,20 +141,27 @@ class BoxCoderCUDAKernel : public framework::OpKernel<T> {
const T* prior_box_var_data = nullptr;
auto prior_box_var_size = 0;
if (prior_box_var) {
PADDLE_ENFORCE(variance.empty(),
"Input 'PriorBoxVar' and attribute 'variance' should not"
"be used at the same time.");
PADDLE_ENFORCE_EQ(variance.empty(), true,
platform::errors::InvalidArgument(
"Input 'PriorBoxVar' and attribute 'variance'"
" of BoxCoder operator should not be used at the "
"same time."));
prior_box_var_data = prior_box_var->data<T>();
prior_box_var_size = prior_box_var->dims().size();
}
if (!(variance.empty())) {
PADDLE_ENFORCE(static_cast<int>(variance.size()) == 4,
"Size of attribute 'variance' should be 4");
PADDLE_ENFORCE_EQ(static_cast<int>(variance.size()), 4,
platform::errors::InvalidArgument(
"Size of attribute 'variance' in BoxCoder operator"
" should be 4. But received size is %d",
variance.size()));
}
if (target_box->lod().size()) {
PADDLE_ENFORCE_EQ(target_box->lod().size(), 1,
"Only support 1 level of LoD.");
platform::errors::InvalidArgument(
"Input 'TargetBox' of BoxCoder operator only"
" supports LoD with one level."));
}
const int var_size = static_cast<int>(variance.size());
......
......@@ -186,16 +186,22 @@ class BoxCoderKernel : public framework::OpKernel<T> {
const int axis = context.Attr<int>("axis");
if (target_box->lod().size()) {
PADDLE_ENFORCE_EQ(target_box->lod().size(), 1UL,
"Only support 1 level of LoD.");
platform::errors::InvalidArgument(
"Input(TargetBox) of BoxCoder operator "
"supports LoD with only one level."));
}
if (prior_box_var) {
PADDLE_ENFORCE(variance.empty(),
"Input 'PriorBoxVar' and attribute 'variance' should not"
"be used at the same time.");
PADDLE_ENFORCE_EQ(variance.empty(), true,
platform::errors::InvalidArgument(
"Input 'PriorBoxVar' and attribute 'variance' "
"of BoxCoder operator should not be used at the "
"same time."));
}
if (!(variance.empty())) {
PADDLE_ENFORCE(static_cast<int>(variance.size()) == 4,
"Size of attribute 'variance' should be 4");
PADDLE_ENFORCE_EQ(static_cast<int>(variance.size()), 4,
platform::errors::InvalidArgument(
"Size of attribute 'variance' of BoxCoder "
"operator should be 4"));
}
auto code_type = GetBoxCodeType(context.Attr<std::string>("code_type"));
bool normalized = context.Attr<bool>("box_normalized");
......
......@@ -22,53 +22,93 @@ class BoxDecoderAndAssignOp : public framework::OperatorWithKernel {
protected:
void InferShape(framework::InferShapeContext *ctx) const override {
PADDLE_ENFORCE(
ctx->HasInput("PriorBox"),
"Input(PriorBox) of BoxDecoderAndAssignOp should not be null.");
PADDLE_ENFORCE(
ctx->HasInput("PriorBoxVar"),
"Input(PriorBoxVar) of BoxDecoderAndAssignOp should not be null.");
PADDLE_ENFORCE(
ctx->HasInput("TargetBox"),
"Input(TargetBox) of BoxDecoderAndAssignOp should not be null.");
PADDLE_ENFORCE(
ctx->HasInput("BoxScore"),
"Input(BoxScore) of BoxDecoderAndAssignOp should not be null.");
PADDLE_ENFORCE(
ctx->HasOutput("DecodeBox"),
"Output(DecodeBox) of BoxDecoderAndAssignOp should not be null.");
PADDLE_ENFORCE(
ctx->HasOutput("OutputAssignBox"),
"Output(OutputAssignBox) of BoxDecoderAndAssignOp should not be null.");
PADDLE_ENFORCE_EQ(
ctx->HasInput("PriorBox"), true,
platform::errors::NotFound("Input(PriorBox) of BoxDecoderAndAssignOp "
"is not found."));
PADDLE_ENFORCE_EQ(
ctx->HasInput("PriorBoxVar"), true,
platform::errors::NotFound("Input(PriorBoxVar) of BoxDecoderAndAssignOp"
" is not found."));
PADDLE_ENFORCE_EQ(
ctx->HasInput("TargetBox"), true,
platform::errors::NotFound("Input(TargetBox) of BoxDecoderAndAssignOp "
"is not found."));
PADDLE_ENFORCE_EQ(
ctx->HasInput("BoxScore"), true,
platform::errors::NotFound("Input(BoxScore) of BoxDecoderAndAssignOp "
"is not found."));
PADDLE_ENFORCE_EQ(
ctx->HasOutput("DecodeBox"), true,
platform::errors::NotFound("Output(DecodeBox) of BoxDecoderAndAssignOp"
" is not found."));
PADDLE_ENFORCE_EQ(
ctx->HasOutput("OutputAssignBox"), true,
platform::errors::NotFound("Output(OutputAssignBox) of "
"BoxDecoderAndAssignOp is not found."));
auto prior_box_dims = ctx->GetInputDim("PriorBox");
auto prior_box_var_dims = ctx->GetInputDim("PriorBoxVar");
auto target_box_dims = ctx->GetInputDim("TargetBox");
auto box_score_dims = ctx->GetInputDim("BoxScore");
PADDLE_ENFORCE_EQ(prior_box_dims.size(), 2,
"The rank of Input of PriorBox must be 2");
PADDLE_ENFORCE_EQ(prior_box_dims[1], 4, "The shape of PriorBox is [N, 4]");
PADDLE_ENFORCE_EQ(prior_box_var_dims.size(), 1,
"The rank of Input of PriorBoxVar must be 1");
PADDLE_ENFORCE_EQ(prior_box_var_dims[0], 4,
"The shape of PriorBoxVar is [4]");
PADDLE_ENFORCE_EQ(target_box_dims.size(), 2,
"The rank of Input of TargetBox must be 2");
PADDLE_ENFORCE_EQ(box_score_dims.size(), 2,
"The rank of Input of BoxScore must be 2");
PADDLE_ENFORCE_EQ(
prior_box_dims.size(), 2,
platform::errors::InvalidArgument("The rank of Input of PriorBox must"
" be 2. But received rank = %d",
prior_box_dims.size()));
PADDLE_ENFORCE_EQ(
prior_box_dims[1], 4,
platform::errors::InvalidArgument(
"The shape of PriorBox is [N, 4], "
"and the second dimension must be 4. But received dimension = %d",
prior_box_dims[1]));
PADDLE_ENFORCE_EQ(
prior_box_var_dims.size(), 1,
platform::errors::InvalidArgument("The rank of Input of PriorBoxVar "
"must be 1. But received rank = %d",
prior_box_var_dims.size()));
PADDLE_ENFORCE_EQ(
prior_box_var_dims[0], 4,
platform::errors::InvalidArgument("The shape of PriorBoxVar is [4]. "
"But received dimension = %d",
prior_box_var_dims[0]));
PADDLE_ENFORCE_EQ(
target_box_dims.size(), 2,
platform::errors::InvalidArgument("The rank of Input of TargetBox must "
"be 2. But received rank = %d",
target_box_dims.size()));
PADDLE_ENFORCE_EQ(
box_score_dims.size(), 2,
platform::errors::InvalidArgument("The rank of Input of BoxScore must "
"be 2. But received rank = %d",
box_score_dims.size()));
if (ctx->IsRuntime()) {
PADDLE_ENFORCE_EQ(prior_box_dims[0], target_box_dims[0],
"The first dim of prior_box and target_box is roi nums "
"and should be same!");
PADDLE_ENFORCE_EQ(prior_box_dims[0], box_score_dims[0],
"The first dim of prior_box and box_score is roi nums "
"and should be same!");
PADDLE_ENFORCE_EQ(
prior_box_dims[0], target_box_dims[0],
platform::errors::InvalidArgument(
"The first dimension of prior_box and "
"target_box is the number of box and should be same. But "
"received dimension of prior_box is %d, dimension of target_box "
"is %d",
prior_box_dims[0], target_box_dims[0]));
PADDLE_ENFORCE_EQ(
prior_box_dims[0], box_score_dims[0],
platform::errors::InvalidArgument(
"The first dimension of prior_box and "
"box_score is the number of box and should be same. But received "
"dimension of prior_box is %d, dimension of box_score is %d",
prior_box_dims[0], box_score_dims[0]));
PADDLE_ENFORCE_EQ(
target_box_dims[1], box_score_dims[1] * prior_box_dims[1],
"The shape of target_box is [N, classnum * 4], The shape "
"of box_score is [N, classnum], The shape of prior_box "
"is [N, 4]");
platform::errors::InvalidArgument(
"The shape of target_box is "
"[N, classnum * 4], The shape of box_score is [N, classnum], "
"The shape of prior_box is [N, 4]. But received second dimension "
"of "
"target_box is %d, second dimension of box_score_dims is %d, "
"and second dimension of prior_box_dims is %d",
target_box_dims[1], box_score_dims[1], prior_box_dims[1]));
}
ctx->SetOutputDim("DecodeBox", framework::make_ddim({target_box_dims[0],
target_box_dims[1]}));
......
......@@ -95,8 +95,6 @@ template <typename DeviceContext, typename T>
class BoxDecoderAndAssignCUDAKernel : public framework::OpKernel<T> {
public:
void Compute(const framework::ExecutionContext& context) const override {
PADDLE_ENFORCE(platform::is_gpu_place(context.GetPlace()),
"This kernel only runs on GPU device.");
auto* prior_box = context.Input<framework::LoDTensor>("PriorBox");
auto* prior_box_var = context.Input<framework::Tensor>("PriorBoxVar");
auto* target_box = context.Input<framework::LoDTensor>("TargetBox");
......
......@@ -21,24 +21,37 @@ class CollectFpnProposalsOp : public framework::OperatorWithKernel {
using framework::OperatorWithKernel::OperatorWithKernel;
void InferShape(framework::InferShapeContext *context) const override {
PADDLE_ENFORCE(context->HasInputs("MultiLevelRois"),
"Inputs(MultiLevelRois) shouldn't be null");
PADDLE_ENFORCE(context->HasInputs("MultiLevelScores"),
"Inputs(MultiLevelScores) shouldn't be null");
PADDLE_ENFORCE(context->HasOutput("FpnRois"),
"Outputs(MultiFpnRois) of DistributeOp should not be null");
PADDLE_ENFORCE_EQ(
context->HasInputs("MultiLevelRois"), true,
platform::errors::NotFound("Inputs(MultiLevelRois) of "
"CollectFpnProposalsOp is not found"));
PADDLE_ENFORCE_EQ(
context->HasInputs("MultiLevelScores"), true,
platform::errors::NotFound("Inputs(MultiLevelScores) of "
"CollectFpnProposalsOp is not found"));
PADDLE_ENFORCE_EQ(
context->HasOutput("FpnRois"), true,
platform::errors::NotFound("Outputs(MultiFpnRois) of "
"CollectFpnProposalsOp is not found"));
auto roi_dims = context->GetInputsDim("MultiLevelRois");
auto score_dims = context->GetInputsDim("MultiLevelScores");
auto post_nms_topN = context->Attrs().Get<int>("post_nms_topN");
std::vector<int64_t> out_dims;
for (auto &roi_dim : roi_dims) {
PADDLE_ENFORCE_EQ(roi_dim[1], 4,
"Second dimension of Input(MultiLevelRois) must be 4");
PADDLE_ENFORCE_EQ(
roi_dim[1], 4,
platform::errors::InvalidArgument(
"Second dimension of Input"
"(MultiLevelRois) must be 4. But received dimension = %d",
roi_dim[1]));
}
for (auto &score_dim : score_dims) {
PADDLE_ENFORCE_EQ(
score_dim[1], 1,
"Second dimension of Input(MultiLevelScores) must be 1");
platform::errors::InvalidArgument(
"Second dimension of Input"
"(MultiLevelScores) must be 1. But received dimension = %d",
score_dim[1]));
}
context->SetOutputDim("FpnRois", {post_nms_topN, 4});
if (!context->IsRuntime()) { // Runtime LoD infershape will be computed
......@@ -57,9 +70,11 @@ class CollectFpnProposalsOp : public framework::OperatorWithKernel {
boost::get<framework::Variable *>(score_inputs[i]);
auto &roi_lod = roi_var->Get<LoDTensor>().lod();
auto &score_lod = score_var->Get<LoDTensor>().lod();
PADDLE_ENFORCE_EQ(roi_lod, score_lod,
"Inputs(MultiLevelRois) and Inputs(MultiLevelScores) "
"should have same lod.");
PADDLE_ENFORCE_EQ(
roi_lod, score_lod,
platform::errors::InvalidArgument(
"Inputs(MultiLevelRois) and "
"Inputs(MultiLevelScores) should have same lod."));
}
}
}
......
......@@ -71,11 +71,19 @@ class CollectFpnProposalsOpKernel : public framework::OpKernel<T> {
int post_nms_topN = context.Attr<int>("post_nms_topN");
PADDLE_ENFORCE_GE(post_nms_topN, 0UL,
"The parameter post_nms_topN must be a positive integer");
platform::errors::InvalidArgument(
"The parameter post_nms_topN must be "
"a positive integer. But received post_nms_topN = %d",
post_nms_topN));
// assert that the length of Rois and scores are same
PADDLE_ENFORCE(multi_layer_rois.size() == multi_layer_scores.size(),
"DistributeFpnProposalsOp need 1 level of LoD");
PADDLE_ENFORCE_EQ(
multi_layer_rois.size(), multi_layer_scores.size(),
platform::errors::InvalidArgument(
"The number of RoIs and Scores should"
" be the same. But received number of RoIs is %d, number of Scores "
"is %d",
multi_layer_rois.size(), multi_layer_scores.size()));
// Check if the lod information of two LoDTensor is same
const int num_fpn_level = multi_layer_rois.size();
std::vector<int> integral_of_all_rois(num_fpn_level + 1, 0);
......
......@@ -22,15 +22,22 @@ class DistributeFpnProposalsOp : public framework::OperatorWithKernel {
using framework::OperatorWithKernel::OperatorWithKernel;
void InferShape(framework::InferShapeContext* ctx) const override {
PADDLE_ENFORCE(ctx->HasInput("FpnRois"),
"Input(FpnRois) shouldn't be null");
PADDLE_ENFORCE_GE(
ctx->Outputs("MultiFpnRois").size(), 1UL,
"Outputs(MultiFpnRois) of DistributeOp should not be empty");
PADDLE_ENFORCE_EQ(
ctx->HasInput("FpnRois"), true,
platform::errors::NotFound("Input(FpnRois) of DistributeFpnProposalsOp"
" is not found"));
PADDLE_ENFORCE_GE(ctx->Outputs("MultiFpnRois").size(), 1UL,
platform::errors::InvalidArgument(
"Outputs(MultiFpnRois) of "
"DistributeFpnProposalsOp should not be empty"));
size_t min_level = static_cast<size_t>(ctx->Attrs().Get<int>("min_level"));
size_t max_level = static_cast<size_t>(ctx->Attrs().Get<int>("max_level"));
PADDLE_ENFORCE_GE(max_level, min_level,
"max_level must not lower than min_level");
PADDLE_ENFORCE_GE(
max_level, min_level,
platform::errors::InvalidArgument(
"max_level must not lower than "
"min_level. But received max_level = %d, min_level = %d",
max_level, min_level));
// Set the output shape
size_t num_out_rois = max_level - min_level + 1;
std::vector<framework::DDim> outs_dims;
......
......@@ -80,8 +80,10 @@ class GPUDistributeFpnProposalsOpKernel : public framework::OpKernel<T> {
int num_level = max_level - min_level + 1;
// check that the fpn_rois is not empty
PADDLE_ENFORCE_EQ(fpn_rois->lod().size(), 1UL,
"DistributeFpnProposalsOp need 1 level of LoD");
PADDLE_ENFORCE_EQ(
fpn_rois->lod().size(), 1UL,
platform::errors::InvalidArgument("DistributeFpnProposalsOp needs LoD"
"with one level"));
auto fpn_rois_lod = fpn_rois->lod().back();
int lod_size = fpn_rois_lod.size() - 1;
......
......@@ -65,8 +65,10 @@ class DistributeFpnProposalsOpKernel : public framework::OpKernel<T> {
const int num_level = max_level - min_level + 1;
// check that the fpn_rois is not empty
PADDLE_ENFORCE_EQ(fpn_rois->lod().size(), 1UL,
"DistributeFpnProposalsOp need 1 level of LoD");
PADDLE_ENFORCE_EQ(
fpn_rois->lod().size(), 1UL,
platform::errors::InvalidArgument("DistributeFpnProposalsOp needs LoD "
"with one level."));
auto fpn_rois_lod = fpn_rois->lod().back();
int fpn_rois_num = fpn_rois_lod[fpn_rois_lod.size() - 1];
......
......@@ -29,30 +29,34 @@ class MultiClassNMSOp : public framework::OperatorWithKernel {
OP_INOUT_CHECK(ctx->HasInput("BBoxes"), "Input", "BBoxes", "MultiClassNMS");
OP_INOUT_CHECK(ctx->HasInput("Scores"), "Input", "Scores", "MultiClassNMS");
OP_INOUT_CHECK(ctx->HasOutput("Out"), "Output", "Out", "MultiClassNMS");
auto box_dims = ctx->GetInputDim("BBoxes");
auto score_dims = ctx->GetInputDim("Scores");
auto score_size = score_dims.size();
if (ctx->IsRuntime()) {
PADDLE_ENFORCE(score_size == 2 || score_size == 3,
"The rank of Input(Scores) must be 2 or 3");
PADDLE_ENFORCE_EQ(score_size == 2 || score_size == 3, true,
platform::errors::InvalidArgument(
"The rank of Input(Scores) must be 2 or 3"
". But received rank = %d",
score_size));
PADDLE_ENFORCE_EQ(box_dims.size(), 3,
platform::errors::InvalidArgument(
"The rank of Input(BBoxes) must be 3"
"But receive box_dims size(%s)",
". But received rank = %d",
box_dims.size()));
if (score_size == 3) {
PADDLE_ENFORCE(box_dims[2] == 4 || box_dims[2] == 8 ||
box_dims[2] == 16 || box_dims[2] == 24 ||
box_dims[2] == 32,
"The last dimension of Input(BBoxes) must be 4 or 8, "
"represents the layout of coordinate "
"[xmin, ymin, xmax, ymax] or "
"4 points: [x1, y1, x2, y2, x3, y3, x4, y4] or "
"8 points: [xi, yi] i= 1,2,...,8 or "
"12 points: [xi, yi] i= 1,2,...,12 or "
"16 points: [xi, yi] i= 1,2,...,16");
PADDLE_ENFORCE_EQ(
box_dims[2] == 4 || box_dims[2] == 8 || box_dims[2] == 16 ||
box_dims[2] == 24 || box_dims[2] == 32,
true, platform::errors::InvalidArgument(
"The last dimension of Input"
"(BBoxes) must be 4 or 8, "
"represents the layout of coordinate "
"[xmin, ymin, xmax, ymax] or "
"4 points: [x1, y1, x2, y2, x3, y3, x4, y4] or "
"8 points: [xi, yi] i= 1,2,...,8 or "
"12 points: [xi, yi] i= 1,2,...,12 or "
"16 points: [xi, yi] i= 1,2,...,16"));
PADDLE_ENFORCE_EQ(
box_dims[1], score_dims[2],
platform::errors::InvalidArgument(
......@@ -64,17 +68,16 @@ class MultiClassNMSOp : public framework::OperatorWithKernel {
} else {
PADDLE_ENFORCE_EQ(box_dims[2], 4,
platform::errors::InvalidArgument(
"The last dimension of Input(BBoxes) must be 4"
"But received box_dims[2](%s).",
"The last dimension of Input"
"(BBoxes) must be 4. But received dimension = %d",
box_dims[2]));
PADDLE_ENFORCE_EQ(box_dims[1], score_dims[1],
platform::errors::InvalidArgument(
"The 2nd dimension of Input(BBoxes)"
"must be equal to the 2nd dimension"
" of Input(Scores)"
"But received box_dims[1](%s) != "
"score_dims[1](%s)",
box_dims[1], score_dims[1]));
PADDLE_ENFORCE_EQ(
box_dims[1], score_dims[1],
platform::errors::InvalidArgument(
"The 2nd dimension of Input"
"(BBoxes) must be equal to the 2nd dimension of Input(Scores). "
"But received box dimension = %d, score dimension = %d",
box_dims[1], score_dims[1]));
}
}
// Here the box_dims[0] is not the real dimension of output.
......
......@@ -845,6 +845,10 @@ def box_coder(prior_box,
box_normalized=False,
axis=1)
"""
check_variable_and_dtype(prior_box, 'prior_box', ['float32', 'float64'],
'box_coder')
check_variable_and_dtype(target_box, 'target_box', ['float32', 'float64'],
'box_coder')
helper = LayerHelper("box_coder", **locals())
output_box = helper.create_variable_for_type_inference(
......@@ -3193,7 +3197,6 @@ def multiclass_nms(bboxes,
check_type(background_label, 'background_label', int, 'multiclass_nms')
helper = LayerHelper('multiclass_nms', **locals())
output = helper.create_variable_for_type_inference(dtype=bboxes.dtype)
helper.append_op(
type="multiclass_nms",
......@@ -3405,7 +3408,8 @@ def distribute_fpn_proposals(fpn_rois,
refer_level=4,
refer_scale=224)
"""
check_variable_and_dtype(fpn_rois, 'fpn_rois', ['float32', 'float64'],
'distribute_fpn_proposals')
helper = LayerHelper('distribute_fpn_proposals', **locals())
dtype = helper.input_dtype('fpn_rois')
num_lvl = max_level - min_level + 1
......@@ -3470,6 +3474,12 @@ def box_decoder_and_assign(prior_box,
pb, pbv, loc, scores, 4.135)
"""
check_variable_and_dtype(prior_box, 'prior_box', ['float32', 'float64'],
'box_decoder_and_assign')
check_variable_and_dtype(target_box, 'target_box', ['float32', 'float64'],
'box_decoder_and_assign')
check_variable_and_dtype(box_score, 'box_score', ['float32', 'float64'],
'box_decoder_and_assign')
helper = LayerHelper("box_decoder_and_assign", **locals())
decoded_box = helper.create_variable_for_type_inference(
......@@ -3551,9 +3561,12 @@ def collect_fpn_proposals(multi_rois,
max_level=5,
post_nms_top_n=2000)
"""
check_type(multi_rois, 'multi_rois', list, 'collect_fpn_proposals')
check_type(multi_scores, 'multi_scores', list, 'collect_fpn_proposals')
helper = LayerHelper('collect_fpn_proposals', **locals())
dtype = helper.input_dtype('multi_rois')
check_dtype(dtype, 'multi_rois', ['float32', 'float64'],
'collect_fpn_proposals')
num_lvl = max_level - min_level + 1
input_rois = multi_rois[:num_lvl]
input_scores = multi_scores[:num_lvl]
......
......@@ -72,6 +72,31 @@ class TestDetection(unittest.TestCase):
self.assertIsNotNone(bcoder)
print(str(program))
def test_box_coder_error(self):
program = Program()
with program_guard(program):
x1 = fluid.data(name='x1', shape=[10, 4], dtype='int32')
y1 = fluid.data(
name='y1', shape=[10, 4], dtype='float32', lod_level=1)
x2 = fluid.data(name='x2', shape=[10, 4], dtype='float32')
y2 = fluid.data(
name='y2', shape=[10, 4], dtype='int32', lod_level=1)
self.assertRaises(
TypeError,
layers.box_coder,
prior_box=x1,
prior_box_var=[0.1, 0.2, 0.1, 0.2],
target_box=y1,
code_type='encode_center_size')
self.assertRaises(
TypeError,
layers.box_coder,
prior_box=x2,
prior_box_var=[0.1, 0.2, 0.1, 0.2],
target_box=y2,
code_type='encode_center_size')
def test_detection_api(self):
program = Program()
with program_guard(program):
......@@ -549,6 +574,33 @@ class TestMulticlassNMS(unittest.TestCase):
output = layers.multiclass_nms(bboxes, scores, 0.3, 400, 200, 0.7)
self.assertIsNotNone(output)
def test_multiclass_nms_error(self):
program = Program()
with program_guard(program):
bboxes1 = fluid.data(
name='bboxes1', shape=[10, 10, 4], dtype='int32')
scores1 = fluid.data(
name='scores1', shape=[10, 10], dtype='float32')
bboxes2 = fluid.data(
name='bboxes2', shape=[10, 10, 4], dtype='float32')
scores2 = fluid.data(name='scores2', shape=[10, 10], dtype='int32')
self.assertRaises(
TypeError,
layers.multiclass_nms,
bboxes=bboxes1,
scores=scores1,
score_threshold=0.5,
nms_top_k=400,
keep_top_k=200)
self.assertRaises(
TypeError,
layers.multiclass_nms,
bboxes=bboxes2,
scores=scores2,
score_threshold=0.5,
nms_top_k=400,
keep_top_k=200)
class TestMulticlassNMS2(unittest.TestCase):
def test_multiclass_nms2(self):
......@@ -591,6 +643,49 @@ class TestCollectFpnPropsals(unittest.TestCase):
2, 5, 10)
self.assertIsNotNone(fpn_rois)
def test_collect_fpn_proposals_error(self):
def generate_input(bbox_type, score_type, name):
multi_bboxes = []
multi_scores = []
for i in range(4):
bboxes = fluid.data(
name='rois' + name + str(i),
shape=[10, 4],
dtype=bbox_type,
lod_level=1)
scores = fluid.data(
name='scores' + name + str(i),
shape=[10, 1],
dtype=score_type,
lod_level=1)
multi_bboxes.append(bboxes)
multi_scores.append(scores)
return multi_bboxes, multi_scores
program = Program()
with program_guard(program):
bbox1 = fluid.data(
name='rois', shape=[5, 10, 4], dtype='float32', lod_level=1)
score1 = fluid.data(
name='scores', shape=[5, 10, 1], dtype='float32', lod_level=1)
bbox2, score2 = generate_input('int32', 'float32', '2')
self.assertRaises(
TypeError,
layers.collect_fpn_proposals,
multi_rois=bbox1,
multi_scores=score1,
min_level=2,
max_level=5,
post_nms_top_n=2000)
self.assertRaises(
TypeError,
layers.collect_fpn_proposals,
multi_rois=bbox2,
multi_scores=score2,
min_level=2,
max_level=5,
post_nms_top_n=2000)
class TestDistributeFpnProposals(unittest.TestCase):
def test_distribute_fpn_proposals(self):
......@@ -607,6 +702,81 @@ class TestDistributeFpnProposals(unittest.TestCase):
self.assertIsNotNone(multi_rois)
self.assertIsNotNone(restore_ind)
def test_distribute_fpn_proposals_error(self):
program = Program()
with program_guard(program):
fpn_rois = fluid.data(
name='data_error', shape=[10, 4], dtype='int32', lod_level=1)
self.assertRaises(
TypeError,
layers.distribute_fpn_proposals,
fpn_rois=fpn_rois,
min_level=2,
max_level=5,
refer_level=4,
refer_scale=224)
class TestBoxDecoderAndAssign(unittest.TestCase):
def test_box_decoder_and_assign(self):
program = Program()
with program_guard(program):
pb = fluid.data(name='prior_box', shape=[None, 4], dtype='float32')
pbv = fluid.data(name='prior_box_var', shape=[4], dtype='float32')
loc = fluid.data(
name='target_box', shape=[None, 4 * 81], dtype='float32')
scores = fluid.data(
name='scores', shape=[None, 81], dtype='float32')
decoded_box, output_assign_box = fluid.layers.box_decoder_and_assign(
pb, pbv, loc, scores, 4.135)
self.assertIsNotNone(decoded_box)
self.assertIsNotNone(output_assign_box)
def test_box_decoder_and_assign_error(self):
def generate_input(pb_type, pbv_type, loc_type, score_type, name):
pb = fluid.data(
name='prior_box' + name, shape=[None, 4], dtype=pb_type)
pbv = fluid.data(
name='prior_box_var' + name, shape=[4], dtype=pbv_type)
loc = fluid.data(
name='target_box' + name, shape=[None, 4 * 81], dtype=loc_type)
scores = fluid.data(
name='scores' + name, shape=[None, 81], dtype=score_type)
return pb, pbv, loc, scores
program = Program()
with program_guard(program):
pb1, pbv1, loc1, scores1 = generate_input('int32', 'float32',
'float32', 'float32', '1')
pb2, pbv2, loc2, scores2 = generate_input('float32', 'float32',
'int32', 'float32', '2')
pb3, pbv3, loc3, scores3 = generate_input('float32', 'float32',
'float32', 'int32', '3')
self.assertRaises(
TypeError,
layers.box_decoder_and_assign,
prior_box=pb1,
prior_box_var=pbv1,
target_box=loc1,
box_score=scores1,
box_clip=4.0)
self.assertRaises(
TypeError,
layers.box_decoder_and_assign,
prior_box=pb2,
prior_box_var=pbv2,
target_box=loc2,
box_score=scores2,
box_clip=4.0)
self.assertRaises(
TypeError,
layers.box_decoder_and_assign,
prior_box=pb3,
prior_box_var=pbv3,
target_box=loc3,
box_score=scores3,
box_clip=4.0)
if __name__ == '__main__':
unittest.main()
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册