Skip to content

[cherry-pick/release/1.8]Enhance checking in some operator. #24935

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 5, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 43 additions & 15 deletions paddle/fluid/operators/affine_grid_op.cc
Original file line number Diff line number Diff line change
Expand Up @@ -42,29 +42,57 @@ class AffineGridOp : public framework::OperatorWithKernel {
public:
using framework::OperatorWithKernel::OperatorWithKernel;
void InferShape(framework::InferShapeContext* ctx) const override {
PADDLE_ENFORCE(ctx->HasInput("Theta"),
"Input(Theta) of AffineGridOp should not be null.");
PADDLE_ENFORCE(ctx->HasOutput("Output"),
"Output(Output) of AffineGridOp should not be null.");
PADDLE_ENFORCE_EQ(ctx->HasInput("Theta"), true,
platform::errors::NotFound(
"The input 'Theta' of AffineGridOp is not found."));
PADDLE_ENFORCE_EQ(ctx->HasOutput("Output"), true,
platform::errors::NotFound(
"The output 'Output' of AffineGridOp is not found."));
auto theta_dims = ctx->GetInputDim("Theta");
PADDLE_ENFORCE(theta_dims.size() == 3,
"AffineGrid's Input(Theta) should be 3-D tensor.");
PADDLE_ENFORCE_EQ(
theta_dims.size(), 3,
platform::errors::InvalidArgument(
"The input Theta's dimensions size should be 3. But received "
"Theta's demensions size=[%d], Theta's dimensions=[%s].",
theta_dims.size(), theta_dims));

auto output_shape = ctx->Attrs().Get<std::vector<int>>("output_shape");
if (output_shape.size() == 0) {
PADDLE_ENFORCE(ctx->HasInput("OutputShape"),
"Input(OutputShape) of AffineGridOp should not be null if "
"attr(output_shape) is not configured.");
PADDLE_ENFORCE_EQ(
ctx->HasInput("OutputShape"), true,
platform::errors::NotFound(
"The input 'OutputShape' of AffineGridOp should not be null if "
"'output_shape' is not configured."));
auto output_shape_dims = ctx->GetInputDim("OutputShape");
PADDLE_ENFORCE(output_shape_dims.size() == 1,
"AffineGrid's Input(OutputShape) should be 1-D tensor.");
PADDLE_ENFORCE_EQ(
output_shape_dims.size(), 1,
platform::errors::InvalidArgument(
"The dimesions size of input OutputShape in AffineGridOp should "
"be 1. But received OutputShape's dimesions size=[%d], "
"OutputShape's dimesions=[%s]",
output_shape_dims.size(), output_shape_dims));
} else {
PADDLE_ENFORCE(output_shape.size() == 4,
"The size of attr(output_shape) should be 4.");
PADDLE_ENFORCE_EQ(
output_shape.size(), 4,
platform::errors::InvalidArgument(
"The size of attribute 'output_shape' in AffineGridOp should be "
"4. But received output_shape's size=[%d].",
output_shape.size()));
}

PADDLE_ENFORCE(theta_dims[1] == 2, "Input(theta) dims[1] should be 2.");
PADDLE_ENFORCE(theta_dims[2] == 3, "Input(theta) dims[2] should be 3.");
PADDLE_ENFORCE_EQ(
theta_dims[1], 2,
platform::errors::InvalidArgument(
"The second dimesion of input 'theta' in AffineGridOp should be 2. "
"But received second dimesion=[%d], dimesions=[%s]",
theta_dims[1], theta_dims));
PADDLE_ENFORCE_EQ(
theta_dims[2], 3,
platform::errors::InvalidArgument(
"The third dimesion of input 'theta' in AffineGridOp should be 3. "
"But received third dimesion=[%d], dimesions=[%s]",
theta_dims[2], theta_dims));

// N * H * W * 2
ctx->SetOutputDim("Output",
framework::make_ddim({theta_dims[0], -1, -1, 2}));
Expand Down
107 changes: 72 additions & 35 deletions paddle/fluid/operators/detection/generate_proposal_labels_op.cc
Original file line number Diff line number Diff line change
Expand Up @@ -38,42 +38,64 @@ class GenerateProposalLabelsOp : public framework::OperatorWithKernel {
using framework::OperatorWithKernel::OperatorWithKernel;

void InferShape(framework::InferShapeContext* ctx) const override {
PADDLE_ENFORCE(ctx->HasInput("RpnRois"),
"Input(RpnRois) shouldn't be null.");
PADDLE_ENFORCE(ctx->HasInput("GtClasses"),
"Input(GtClasses) shouldn't be null.");
PADDLE_ENFORCE(ctx->HasInput("IsCrowd"),
"Input(IsCrowd) shouldn't be null.");
PADDLE_ENFORCE(ctx->HasInput("GtBoxes"),
"Input(GtBoxes) shouldn't be null.");
PADDLE_ENFORCE(ctx->HasInput("ImInfo"), "Input(ImInfo) shouldn't be null.");

PADDLE_ENFORCE(
ctx->HasOutput("Rois"),
"Output(Rois) of GenerateProposalLabelsOp should not be null");
PADDLE_ENFORCE(
ctx->HasOutput("LabelsInt32"),
"Output(LabelsInt32) of GenerateProposalLabelsOp should not be null");
PADDLE_ENFORCE(
ctx->HasOutput("BboxTargets"),
"Output(BboxTargets) of GenerateProposalLabelsOp should not be null");
PADDLE_ENFORCE(ctx->HasOutput("BboxInsideWeights"),
"Output(BboxInsideWeights) of GenerateProposalLabelsOp "
"should not be null");
PADDLE_ENFORCE(ctx->HasOutput("BboxOutsideWeights"),
"Output(BboxOutsideWeights) of GenerateProposalLabelsOp "
"should not be null");
PADDLE_ENFORCE_EQ(
ctx->HasInput("RpnRois"), true,
platform::errors::NotFound("Input(RpnRois) shouldn't be null."));
PADDLE_ENFORCE_EQ(
ctx->HasInput("GtClasses"), true,
platform::errors::NotFound("Input(GtClasses) shouldn't be null."));
PADDLE_ENFORCE_EQ(
ctx->HasInput("IsCrowd"), true,
platform::errors::NotFound("Input(IsCrowd) shouldn't be null."));
PADDLE_ENFORCE_EQ(
ctx->HasInput("GtBoxes"), true,
platform::errors::NotFound("Input(GtBoxes) shouldn't be null."));
PADDLE_ENFORCE_EQ(
ctx->HasInput("ImInfo"), true,
platform::errors::NotFound("Input(ImInfo) shouldn't be null."));

PADDLE_ENFORCE_EQ(
ctx->HasOutput("Rois"), true,
platform::errors::NotFound(
"Output(Rois) of GenerateProposalLabelsOp should not be null"));
PADDLE_ENFORCE_EQ(ctx->HasOutput("LabelsInt32"), true,
platform::errors::NotFound("Output(LabelsInt32) of "
"GenerateProposalLabelsOp "
"should not be null"));
PADDLE_ENFORCE_EQ(ctx->HasOutput("BboxTargets"), true,
platform::errors::NotFound("Output(BboxTargets) of "
"GenerateProposalLabelsOp "
"should not be null"));
PADDLE_ENFORCE_EQ(
ctx->HasOutput("BboxInsideWeights"), true,
platform::errors::NotFound(
"Output(BboxInsideWeights) of GenerateProposalLabelsOp "
"should not be null"));
PADDLE_ENFORCE_EQ(
ctx->HasOutput("BboxOutsideWeights"), true,
platform::errors::NotFound(
"Output(BboxOutsideWeights) of GenerateProposalLabelsOp "
"should not be null"));

auto rpn_rois_dims = ctx->GetInputDim("RpnRois");
auto gt_boxes_dims = ctx->GetInputDim("GtBoxes");
auto im_info_dims = ctx->GetInputDim("ImInfo");

PADDLE_ENFORCE_EQ(rpn_rois_dims.size(), 2,
"The rank of Input(RpnRois) must be 2.");
platform::errors::InvalidArgument(
"The dimensions size of Input(RpnRois) must be 2. "
"But received dimensions size=[%d], dimensions=[%s].",
rpn_rois_dims.size(), rpn_rois_dims));
PADDLE_ENFORCE_EQ(gt_boxes_dims.size(), 2,
"The rank of Input(GtBoxes) must be 2.");
platform::errors::InvalidArgument(
"The dimensions size of Input(GtBoxes) must be 2. "
"But received dimensions size=[%d], dimensions=[%s].",
gt_boxes_dims.size(), gt_boxes_dims));
PADDLE_ENFORCE_EQ(im_info_dims.size(), 2,
"The rank of Input(ImInfo) must be 2.");
platform::errors::InvalidArgument(
"The dimensions size of Input(ImInfo) must be 2. But "
"received dimensions size=[%d], dimensions=[%s].",
im_info_dims.size(), im_info_dims));

int class_nums = ctx->Attrs().Get<int>("class_nums");

Expand Down Expand Up @@ -399,15 +421,30 @@ class GenerateProposalLabelsKernel : public framework::OpKernel<T> {
bool use_random = context.Attr<bool>("use_random");
bool is_cascade_rcnn = context.Attr<bool>("is_cascade_rcnn");
bool is_cls_agnostic = context.Attr<bool>("is_cls_agnostic");
PADDLE_ENFORCE_EQ(rpn_rois->lod().size(), 1UL,
"GenerateProposalLabelsOp rpn_rois needs 1 level of LoD");
PADDLE_ENFORCE_EQ(
rpn_rois->lod().size(), 1UL,
platform::errors::InvalidArgument(
"GenerateProposalLabelsOp rpn_rois needs 1 level of LoD. But "
"received level of LoD is [%d], LoD is [%s].",
rpn_rois->lod().size(), rpn_rois->lod()));
PADDLE_ENFORCE_EQ(
gt_classes->lod().size(), 1UL,
"GenerateProposalLabelsOp gt_classes needs 1 level of LoD");
PADDLE_ENFORCE_EQ(is_crowd->lod().size(), 1UL,
"GenerateProposalLabelsOp is_crowd needs 1 level of LoD");
PADDLE_ENFORCE_EQ(gt_boxes->lod().size(), 1UL,
"GenerateProposalLabelsOp gt_boxes needs 1 level of LoD");
platform::errors::InvalidArgument(
"GenerateProposalLabelsOp gt_classes needs 1 level of LoD. But "
"received level of LoD is [%d], LoD is [%s].",
gt_classes->lod().size(), gt_classes->lod()));
PADDLE_ENFORCE_EQ(
is_crowd->lod().size(), 1UL,
platform::errors::InvalidArgument(
"GenerateProposalLabelsOp is_crowd needs 1 level of LoD. But "
"received level of LoD is [%d], LoD is [%s].",
is_crowd->lod().size(), is_crowd->lod()));
PADDLE_ENFORCE_EQ(
gt_boxes->lod().size(), 1UL,
platform::errors::InvalidArgument(
"GenerateProposalLabelsOp gt_boxes needs 1 level of LoD. But "
"received level of LoD is [%d], LoD is [%s].",
gt_boxes->lod().size(), gt_boxes->lod()));
int64_t n = static_cast<int64_t>(rpn_rois->lod().back().size() - 1);

rois->mutable_data<T>({n * batch_size_per_im, kBoxDim}, context.GetPlace());
Expand Down
24 changes: 15 additions & 9 deletions paddle/fluid/operators/detection/generate_proposals_op.cc
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,21 @@ class GenerateProposalsOp : public framework::OperatorWithKernel {
using framework::OperatorWithKernel::OperatorWithKernel;

void InferShape(framework::InferShapeContext *ctx) const override {
PADDLE_ENFORCE(ctx->HasInput("Scores"), "Input(Scores) shouldn't be null.");
PADDLE_ENFORCE(ctx->HasInput("BboxDeltas"),
"Input(BboxDeltas) shouldn't be null.");
PADDLE_ENFORCE(ctx->HasInput("ImInfo"), "Input(ImInfo) shouldn't be null.");
PADDLE_ENFORCE(ctx->HasInput("Anchors"),
"Input(Anchors) shouldn't be null.");
PADDLE_ENFORCE(ctx->HasInput("Variances"),
"Input(Variances) shouldn't be null.");
PADDLE_ENFORCE_EQ(
ctx->HasInput("Scores"), true,
platform::errors::NotFound("Input(Scores) shouldn't be null."));
PADDLE_ENFORCE_EQ(
ctx->HasInput("BboxDeltas"), true,
platform::errors::NotFound("Input(BboxDeltas) shouldn't be null."));
PADDLE_ENFORCE_EQ(
ctx->HasInput("ImInfo"), true,
platform::errors::NotFound("Input(ImInfo) shouldn't be null."));
PADDLE_ENFORCE_EQ(
ctx->HasInput("Anchors"), true,
platform::errors::NotFound("Input(Anchors) shouldn't be null."));
PADDLE_ENFORCE_EQ(
ctx->HasInput("Variances"), true,
platform::errors::NotFound("Input(Variances) shouldn't be null."));

ctx->SetOutputDim("RpnRois", {-1, 4});
ctx->SetOutputDim("RpnRoiProbs", {-1, 1});
Expand Down Expand Up @@ -247,7 +254,6 @@ static inline Tensor VectorToTensor(const std::vector<T> &selected_indices,
template <class T>
static inline Tensor NMS(const platform::DeviceContext &ctx, Tensor *bbox,
Tensor *scores, T nms_threshold, float eta) {
PADDLE_ENFORCE_NOT_NULL(bbox);
int64_t num_boxes = bbox->dims()[0];
// 4: [xmin ymin xmax ymax]
int64_t box_size = bbox->dims()[1];
Expand Down
6 changes: 5 additions & 1 deletion paddle/fluid/operators/detection/generate_proposals_op.cu
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,11 @@ class CUDAGenerateProposalsKernel : public framework::OpKernel<T> {
float nms_thresh = context.Attr<float>("nms_thresh");
float min_size = context.Attr<float>("min_size");
float eta = context.Attr<float>("eta");
PADDLE_ENFORCE_GE(eta, 1., "Not support adaptive NMS.");
PADDLE_ENFORCE_GE(eta, 1.,
platform::errors::InvalidArgument(
"Not support adaptive NMS. The attribute 'eta' "
"should not less than 1. But received eta=[%d]",
eta));

auto &dev_ctx = context.template device_context<DeviceContext>();

Expand Down
84 changes: 53 additions & 31 deletions paddle/fluid/operators/detection/rpn_target_assign_op.cc
Original file line number Diff line number Diff line change
Expand Up @@ -31,40 +31,44 @@ class RpnTargetAssignOp : public framework::OperatorWithKernel {
using framework::OperatorWithKernel::OperatorWithKernel;

void InferShape(framework::InferShapeContext* ctx) const override {
PADDLE_ENFORCE(ctx->HasInput("Anchor"),
"Input(Anchor) of RpnTargetAssignOp should not be null");
PADDLE_ENFORCE(ctx->HasInput("GtBoxes"),
"Input(GtBoxes) of RpnTargetAssignOp should not be null");
PADDLE_ENFORCE(ctx->HasInput("IsCrowd"),
"Input(Anchor) of RpnTargetAssignOp should not be null");
PADDLE_ENFORCE(ctx->HasInput("ImInfo"),
"Input(ImInfo) of RpnTargetAssignOp should not be null");

PADDLE_ENFORCE(
ctx->HasOutput("LocationIndex"),
"Output(LocationIndex) of RpnTargetAssignOp should not be null");
PADDLE_ENFORCE(
ctx->HasOutput("ScoreIndex"),
"Output(ScoreIndex) of RpnTargetAssignOp should not be null");
PADDLE_ENFORCE(
ctx->HasOutput("TargetLabel"),
"Output(TargetLabel) of RpnTargetAssignOp should not be null");
PADDLE_ENFORCE(
ctx->HasOutput("TargetBBox"),
"Output(TargetBBox) of RpnTargetAssignOp should not be null");
PADDLE_ENFORCE(
ctx->HasOutput("BBoxInsideWeight"),
"Output(BBoxInsideWeight) of RpnTargetAssignOp should not be null");
OP_INOUT_CHECK(ctx->HasInput("Anchor"), "Input", "Anchor",
"rpn_target_assign");
OP_INOUT_CHECK(ctx->HasInput("GtBoxes"), "Input", "GtBoxes",
"rpn_target_assign");
OP_INOUT_CHECK(ctx->HasInput("IsCrowd"), "Input", "IsCrowd",
"rpn_target_assign");
OP_INOUT_CHECK(ctx->HasInput("ImInfo"), "Input", "ImInfo",
"rpn_target_assign");

OP_INOUT_CHECK(ctx->HasOutput("LocationIndex"), "Output", "LocationIndex",
"rpn_target_assign");
OP_INOUT_CHECK(ctx->HasOutput("ScoreIndex"), "Output", "ScoreIndex",
"rpn_target_assign");
OP_INOUT_CHECK(ctx->HasOutput("TargetLabel"), "Output", "TargetLabel",
"rpn_target_assign");
OP_INOUT_CHECK(ctx->HasOutput("TargetBBox"), "Output", "TargetBBox",
"rpn_target_assign");
OP_INOUT_CHECK(ctx->HasOutput("BBoxInsideWeight"), "Output",
"BBoxInsideWeight", "rpn_target_assign");

auto anchor_dims = ctx->GetInputDim("Anchor");
auto gt_boxes_dims = ctx->GetInputDim("GtBoxes");
auto im_info_dims = ctx->GetInputDim("ImInfo");
PADDLE_ENFORCE_EQ(anchor_dims.size(), 2,
"The rank of Input(Anchor) must be 2.");
platform::errors::InvalidArgument(
"The dimensions size of Input(Anchor) must be 2. But "
"received dimensions size=[%d], dimensions=[%s].",
anchor_dims.size(), anchor_dims));
PADDLE_ENFORCE_EQ(gt_boxes_dims.size(), 2,
"The rank of Input(GtBoxes) must be 2.");
platform::errors::InvalidArgument(
"The dimensions size of Input(GtBoxes) must be 2. "
"But received dimensions size=[%d], dimensions=[%s].",
gt_boxes_dims.size(), gt_boxes_dims));
PADDLE_ENFORCE_EQ(im_info_dims.size(), 2,
"The rank of Input(ImInfo) must be 2.");
platform::errors::InvalidArgument(
"The dimensions size of Input(ImInfo) must be 2. But "
"received dimensions size=[%d], dimensions=[%s].",
im_info_dims.size(), im_info_dims));

ctx->SetOutputDim("LocationIndex", {-1});
ctx->SetOutputDim("ScoreIndex", {-1});
Expand Down Expand Up @@ -357,9 +361,15 @@ class RpnTargetAssignKernel : public framework::OpKernel<T> {
auto* bbox_inside_weight = context.Output<LoDTensor>("BBoxInsideWeight");

PADDLE_ENFORCE_EQ(gt_boxes->lod().size(), 1UL,
"RpnTargetAssignOp gt_boxes needs 1 level of LoD");
platform::errors::InvalidArgument(
"RpnTargetAssignOp gt_boxes needs 1 level of LoD. "
"But received level of LoD is [%d], LoD is [%s].",
gt_boxes->lod().size(), gt_boxes->lod()));
PADDLE_ENFORCE_EQ(is_crowd->lod().size(), 1UL,
"RpnTargetAssignOp is_crowd needs 1 level of LoD");
platform::errors::InvalidArgument(
"RpnTargetAssignOp is_crowd needs 1 level of LoD. "
"But received level of LoD is [%d], LoD is [%s].",
is_crowd->lod().size(), is_crowd->lod()));
int64_t anchor_num = static_cast<int64_t>(anchor->dims()[0]);
int64_t batch_num = static_cast<int64_t>(gt_boxes->lod().back().size() - 1);

Expand Down Expand Up @@ -479,8 +489,20 @@ class RpnTargetAssignKernel : public framework::OpKernel<T> {
lod0_score.emplace_back(total_score_num);
}

PADDLE_ENFORCE_LE(total_loc_num, max_num);
PADDLE_ENFORCE_LE(total_score_num, max_num);
PADDLE_ENFORCE_LE(
total_loc_num, max_num,
platform::errors::InvalidArgument(
"The number of sampled bboxes should not be greater than the "
"number of all anchor boxes(%d), but the number of sampled "
"bboxes is :%d.",
max_num, total_loc_num));
PADDLE_ENFORCE_LE(
total_score_num, max_num,
platform::errors::InvalidArgument(
"The number of sampled scores should not be greater than the "
"number of all anchor boxes(%d), but the number of sampled "
"scores is :%d.",
max_num, total_score_num));

lod_loc.emplace_back(lod0_loc);
loc_score.emplace_back(lod0_score);
Expand Down
Loading