|
| 1 | +/* Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserve. |
| 2 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | +you may not use this file except in compliance with the License. |
| 4 | +You may obtain a copy of the License at |
| 5 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 6 | +Unless required by applicable law or agreed to in writing, software |
| 7 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 8 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 9 | +See the License for the specific language governing permissions and |
| 10 | +limitations under the License. */ |
| 11 | + |
| 12 | +#include "paddle/operators/box_coder_op.h" |
| 13 | + |
| 14 | +namespace paddle { |
| 15 | +namespace operators { |
| 16 | + |
| 17 | +class BoxCoderOp : public framework::OperatorWithKernel { |
| 18 | + public: |
| 19 | + using framework::OperatorWithKernel::OperatorWithKernel; |
| 20 | + |
| 21 | + protected: |
| 22 | + void InferShape(framework::InferShapeContext *ctx) const override { |
| 23 | + PADDLE_ENFORCE(ctx->HasInput("PriorBox"), |
| 24 | + "Input(PriorBox) of BoxCoderOp should not be null."); |
| 25 | + PADDLE_ENFORCE(ctx->HasInput("PriorBoxVar"), |
| 26 | + "Input(PriorBoxVar) of BoxCoderOp should not be null."); |
| 27 | + PADDLE_ENFORCE(ctx->HasInput("TargetBox"), |
| 28 | + "Input(TargetBox) of BoxCoderOp should not be null."); |
| 29 | + PADDLE_ENFORCE(ctx->HasOutput("OutputBox"), |
| 30 | + "Output(OutputBox) of BoxCoderOp should not be null."); |
| 31 | + |
| 32 | + auto prior_box_dims = ctx->GetInputDim("PriorBox"); |
| 33 | + auto prior_box_var_dims = ctx->GetInputDim("PriorBoxVar"); |
| 34 | + auto target_box_dims = ctx->GetInputDim("TargetBox"); |
| 35 | + |
| 36 | + PADDLE_ENFORCE_EQ(prior_box_dims.size(), 2, |
| 37 | + "The rank of Input of PriorBoxVar must be 2"); |
| 38 | + PADDLE_ENFORCE_EQ(prior_box_dims[1], 4, "The shape of PriorBox is [N, 4]"); |
| 39 | + PADDLE_ENFORCE_EQ(prior_box_dims, prior_box_var_dims); |
| 40 | + PADDLE_ENFORCE_EQ(target_box_dims.size(), 2, |
| 41 | + "The rank of Input of TargetBox must be 2"); |
| 42 | + PADDLE_ENFORCE_EQ(target_box_dims[1], 4, |
| 43 | + "The shape of TargetBox is [M, 4]"); |
| 44 | + |
| 45 | + GetBoxCodeType(ctx->Attrs().Get<std::string>("code_type")); |
| 46 | + |
| 47 | + ctx->SetOutputDim( |
| 48 | + "OutputBox", |
| 49 | + framework::make_ddim({target_box_dims[0], prior_box_dims[0], 4})); |
| 50 | + ctx->ShareLoD("TargetBox", /*->*/ "OutputBox"); |
| 51 | + } |
| 52 | +}; |
| 53 | + |
| 54 | +class BoxCoderOpMaker : public framework::OpProtoAndCheckerMaker { |
| 55 | + public: |
| 56 | + BoxCoderOpMaker(OpProto *proto, OpAttrChecker *op_checker) |
| 57 | + : OpProtoAndCheckerMaker(proto, op_checker) { |
| 58 | + AddInput( |
| 59 | + "PriorBox", |
| 60 | + "(Tensor, default Tensor<float>) " |
| 61 | + "Box list PriorBox is a 2-D Tensor with shape [M, 4] holds M boxes, " |
| 62 | + "each box is represented as [xmin, ymin, xmax, ymax], " |
| 63 | + "[xmin, ymin] is the left top coordinate of the anchor box, " |
| 64 | + "if the input is image feature map, they are close to the origin " |
| 65 | + "of the coordinate system. [xmax, ymax] is the right bottom " |
| 66 | + "coordinate of the anchor box."); |
| 67 | + AddInput("PriorBoxVar", |
| 68 | + "(Tensor, default Tensor<float>) " |
| 69 | + "PriorBoxVar is a 2-D Tensor with shape [M, 4] holds M group " |
| 70 | + "of variance."); |
| 71 | + AddInput( |
| 72 | + "TargetBox", |
| 73 | + "(LoDTensor or Tensor) this input is a 2-D LoDTensor with shape " |
| 74 | + "[N, 4], each box is represented as [xmin, ymin, xmax, ymax], " |
| 75 | + "[xmin, ymin] is the left top coordinate of the box if the input " |
| 76 | + "is image feature map, they are close to the origin of the coordinate " |
| 77 | + "system. [xmax, ymax] is the right bottom coordinate of the box. " |
| 78 | + "This tensor can contain LoD information to represent a batch " |
| 79 | + "of inputs. One instance of this batch can contain different " |
| 80 | + "numbers of entities."); |
| 81 | + AddAttr<std::string>("code_type", |
| 82 | + "(string, default encode_center_size) " |
| 83 | + "the code type used with the target box") |
| 84 | + .SetDefault("encode_center_size") |
| 85 | + .InEnum({"encode_center_size", "decode_center_size"}); |
| 86 | + AddOutput( |
| 87 | + "OutputBox", |
| 88 | + "(LoDTensor or Tensor) " |
| 89 | + "(Tensor) The output of box_coder_op, a tensor with shape [N, M, 4] " |
| 90 | + "representing the result of N target boxes encoded/decoded with " |
| 91 | + "M Prior boxes and variances."); |
| 92 | + |
| 93 | + AddComment(R"DOC( |
| 94 | +Bounding Box Coder Operator. |
| 95 | +Encode/Decode the target bounding box with the priorbox information. |
| 96 | +The Encoding schema described below: |
| 97 | +ox = (tx - px) / pw / pxv |
| 98 | +oy = (ty - py) / ph / pyv |
| 99 | +ow = log(abs(tw / pw)) / pwv |
| 100 | +oh = log(abs(th / ph)) / phv |
| 101 | +The Decoding schema described below: |
| 102 | +ox = (pw * pxv * tx * + px) - tw / 2 |
| 103 | +oy = (ph * pyv * ty * + py) - th / 2 |
| 104 | +ow = exp(pwv * tw) * pw + tw / 2 |
| 105 | +oh = exp(phv * th) * ph + th / 2 |
| 106 | +where tx, ty, tw, th denote the target box's center coordinates, width and |
| 107 | +height respectively. Similarly, px, py, pw, ph denote the priorbox's(anchor) |
| 108 | +center coordinates, width and height. pxv, pyv, pwv, phv denote the variance |
| 109 | +of the priorbox and ox, oy, ow, oh denote the encoded/decoded coordinates, |
| 110 | +width and height. |
| 111 | +)DOC"); |
| 112 | + } |
| 113 | +}; |
| 114 | + |
| 115 | +} // namespace operators |
| 116 | +} // namespace paddle |
| 117 | + |
| 118 | +namespace ops = paddle::operators; |
| 119 | +REGISTER_OP_WITHOUT_GRADIENT(box_coder, ops::BoxCoderOp, ops::BoxCoderOpMaker); |
| 120 | +REGISTER_OP_CPU_KERNEL(box_coder, ops::BoxCoderKernel<float>, |
| 121 | + ops::BoxCoderKernel<double>); |
0 commit comments