box_clip_op.cc 4.0 KB
Newer Older
J
jerrywgz 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
/* Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
    http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */

#include "paddle/fluid/operators/detection/box_clip_op.h"
#include "paddle/fluid/framework/op_registry.h"

namespace paddle {
namespace operators {

class BoxClipOp : public framework::OperatorWithKernel {
 public:
  using framework::OperatorWithKernel::OperatorWithKernel;

 protected:
J
jerrywgz 已提交
23
  void InferShape(framework::InferShapeContext* ctx) const override {
24 25 26 27 28 29
    PADDLE_ENFORCE_EQ(ctx->HasInput("Input"), true,
                      platform::errors::NotFound("Input(Input) of BoxClipOp "
                                                 "is not found."));
    PADDLE_ENFORCE_EQ(ctx->HasInput("ImInfo"), true,
                      platform::errors::NotFound("Input(ImInfo) of BoxClipOp "
                                                 "is not found."));
J
jerrywgz 已提交
30

31
    auto input_box_dims = ctx->GetInputDim("Input");
J
jerrywgz 已提交
32 33 34 35
    auto im_info_dims = ctx->GetInputDim("ImInfo");

    if (ctx->IsRuntime()) {
      auto input_box_size = input_box_dims.size();
36 37 38
      PADDLE_ENFORCE_EQ(
          input_box_dims[input_box_size - 1], 4,
          platform::errors::InvalidArgument(
39 40
              "The last dimension of Input(Input) in BoxClipOp must be 4. "
              "But received last dimension = %d",
41
              input_box_dims[input_box_size - 1]));
J
jerrywgz 已提交
42
      PADDLE_ENFORCE_EQ(im_info_dims.size(), 2,
43
                        platform::errors::InvalidArgument(
44 45
                            "The rank of Input(Input) in BoxClipOp must be 2."
                            " But received rank = %d",
46 47 48 49
                            im_info_dims.size()));
      PADDLE_ENFORCE_EQ(
          im_info_dims[1], 3,
          platform::errors::InvalidArgument(
50 51
              "The last dimension of Input(ImInfo) of BoxClipOp must be 3. "
              "But received last dimension = %d",
52
              im_info_dims[1]));
J
jerrywgz 已提交
53
    }
54 55
    ctx->ShareDim("Input", /*->*/ "Output");
    ctx->ShareLoD("Input", /*->*/ "Output");
J
jerrywgz 已提交
56
  }
J
jerrywgz 已提交
57 58 59 60 61
};

class BoxClipOpMaker : public framework::OpProtoAndCheckerMaker {
 public:
  void Make() override {
62
    AddInput("Input",
J
jerrywgz 已提交
63
             "(LoDTensor) "
64
             "Input is a LoDTensor with shape [..., 4] holds 4 points"
J
jerrywgz 已提交
65 66
             "in last dimension in format [xmin, ymin, xmax, ymax]");
    AddInput("ImInfo",
J
jerrywgz 已提交
67 68
             "(Tensor) Information for image reshape is in shape (N, 3), "
             "in format (height, width, im_scale)");
69
    AddOutput("Output",
J
jerrywgz 已提交
70
              "(LoDTensor) "
71
              "Output is a LoDTensor with the same shape as Input"
J
jerrywgz 已提交
72 73
              "and it is the result after clip");
    AddComment(R"DOC(
74 75
This operator clips input boxes to original input images.

J
jerrywgz 已提交
76
For each input box, The formula is given as follows:
77

J
jerrywgz 已提交
78 79 80 81
       $$xmin = \max(\min(xmin, im_w - 1), 0)$$
       $$ymin = \max(\min(ymin, im_h - 1), 0)$$     
       $$xmax = \max(\min(xmax, im_w - 1), 0)$$
       $$ymax = \max(\min(ymax, im_h - 1), 0)$$
82

J
jerrywgz 已提交
83 84 85 86
where im_w and im_h are computed from ImInfo, the formula is given as follows:

       $$im_w = \round(width / im_scale)$$
       $$im_h = \round(height / im_scale)$$ 
J
jerrywgz 已提交
87 88 89 90 91 92 93 94
)DOC");
  }
};

}  // namespace operators
}  // namespace paddle

namespace ops = paddle::operators;
H
hong 已提交
95 96 97 98
REGISTER_OPERATOR(
    box_clip, ops::BoxClipOp, ops::BoxClipOpMaker,
    paddle::framework::EmptyGradOpMaker<paddle::framework::OpDesc>,
    paddle::framework::EmptyGradOpMaker<paddle::imperative::OpBase>);
J
jerrywgz 已提交
99 100 101
REGISTER_OP_CPU_KERNEL(
    box_clip, ops::BoxClipKernel<paddle::platform::CPUDeviceContext, float>,
    ops::BoxClipKernel<paddle::platform::CPUDeviceContext, double>);