box_coder_op.cc 5.7 KB
Newer Older
1
/* Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
G
gaoyuan 已提交
2 3 4 5 6 7 8 9 10 11
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. */

12
#include <vector>
G
gaoyuan 已提交
13

L
lyq 已提交
14 15 16 17 18
#include "paddle/fluid/framework/infershape_utils.h"
#include "paddle/fluid/framework/op_registry.h"
#include "paddle/phi/core/infermeta_utils.h"
#include "paddle/phi/infermeta/ternary.h"

G
gaoyuan 已提交
19 20 21 22 23 24 25 26 27 28
namespace paddle {
namespace operators {

class BoxCoderOp : public framework::OperatorWithKernel {
 public:
  using framework::OperatorWithKernel::OperatorWithKernel;
};

class BoxCoderOpMaker : public framework::OpProtoAndCheckerMaker {
 public:
Y
Yu Yang 已提交
29
  void Make() override {
G
gaoyuan 已提交
30 31 32
    AddInput(
        "PriorBox",
        "(Tensor, default Tensor<float>) "
G
gaoyuan 已提交
33
        "Box list PriorBox is a 2-D Tensor with shape [M, 4] holds M boxes, "
G
gaoyuan 已提交
34 35 36 37 38 39
        "each box is represented as [xmin, ymin, xmax, ymax], "
        "[xmin, ymin] is the left top coordinate of the anchor box, "
        "if the input is image feature map, they are close to the origin "
        "of the coordinate system. [xmax, ymax] is the right bottom "
        "coordinate of the anchor box.");
    AddInput("PriorBoxVar",
40
             "(Tensor, default Tensor<float>, optional) "
G
gaoyuan 已提交
41
             "PriorBoxVar is a 2-D Tensor with shape [M, 4] holds M group "
42 43 44
             "of variance. PriorBoxVar will set all elements to 1 by "
             "default.")
        .AsDispensable();
G
gaoyuan 已提交
45 46
    AddInput(
        "TargetBox",
47 48
        "(phi::DenseTensor or Tensor) This input can be a 2-D phi::DenseTensor "
        "with shape "
Y
Yuan Gao 已提交
49 50 51 52 53 54 55 56 57
        "[N, 4] when code_type is 'encode_center_size'. This input also can "
        "be a 3-D Tensor with shape [N, M, 4] when code_type is "
        "'decode_center_size'. [N, 4], each box is represented as "
        "[xmin, ymin, xmax, ymax], [xmin, ymin] is the left top coordinate "
        "of the box if the input is image feature map, they are close to "
        "the origin of the coordinate system. [xmax, ymax] is the right "
        "bottom coordinate of the box. This tensor can contain LoD "
        "information to represent a batch of inputs. One instance of this "
        "batch can contain different numbers of entities.");
G
gaoyuan 已提交
58 59 60 61 62
    AddAttr<std::string>("code_type",
                         "(string, default encode_center_size) "
                         "the code type used with the target box")
        .SetDefault("encode_center_size")
        .InEnum({"encode_center_size", "decode_center_size"});
63 64
    AddAttr<bool>("box_normalized",
                  "(bool, default true) "
T
tianshuo78520a 已提交
65
                  "whether treat the priorbox as a normalized box")
66
        .SetDefault(true);
J
jerrywgz 已提交
67
    AddAttr<int>("axis",
68 69 70 71 72
                 "(int, default 0)"
                 "which axis in PriorBox to broadcast for box decode,"
                 "for example, if axis is 0 and TargetBox has shape"
                 "[N, M, 4] and PriorBox has shape [M, 4], then PriorBox "
                 "will broadcast to [N, M, 4] for decoding. It is only valid"
J
jerrywgz 已提交
73 74 75
                 "when code type is decode_center_size")
        .SetDefault(0)
        .InEnum({0, 1});
76 77 78 79 80 81
    AddAttr<std::vector<float>>(
        "variance",
        "(vector<float>, default {}),"
        "variance of prior box with shape [4]. PriorBoxVar and variance can"
        "not be provided at the same time.")
        .SetDefault(std::vector<float>{});
Y
Yuan Gao 已提交
82
    AddOutput("OutputBox",
83
              "(phi::DenseTensor or Tensor) "
Y
Yuan Gao 已提交
84 85 86 87
              "When code_type is 'encode_center_size', the output tensor of "
              "box_coder_op with shape [N, M, 4] representing the result of N "
              "target boxes encoded with M Prior boxes and variances. When "
              "code_type is 'decode_center_size', N represents the batch size "
T
tianshuo78520a 已提交
88
              "and M represents the number of decoded boxes.");
G
gaoyuan 已提交
89 90

    AddComment(R"DOC(
91 92 93

Bounding Box Coder.

G
gaoyuan 已提交
94
Encode/Decode the target bounding box with the priorbox information.
95

G
gaoyuan 已提交
96
The Encoding schema described below:
97 98 99 100 101

    ox = (tx - px) / pw / pxv

    oy = (ty - py) / ph / pyv

102
    ow = log(abs(tw / pw)) / pwv
103

104
    oh = log(abs(th / ph)) / phv
105

G
gaoyuan 已提交
106
The Decoding schema described below:
107 108 109 110 111 112 113 114 115 116 117 118 119

    ox = (pw * pxv * tx * + px) - tw / 2

    oy = (ph * pyv * ty * + py) - th / 2

    ow = exp(pwv * tw) * pw + tw / 2

    oh = exp(phv * th) * ph + th / 2

where `tx`, `ty`, `tw`, `th` denote the target box's center coordinates, width
and height respectively. Similarly, `px`, `py`, `pw`, `ph` denote the
priorbox's (anchor) center coordinates, width and height. `pxv`, `pyv`, `pwv`,
`phv` denote the variance of the priorbox and `ox`, `oy`, `ow`, `oh` denote the
120
encoded/decoded coordinates, width and height.
J
jerrywgz 已提交
121

122
During Box Decoding, two modes for broadcast are supported. Say target box has
J
jerrywgz 已提交
123
shape [N, M, 4], and the shape of prior box can be [N, 4] or [M, 4]. Then prior
124
box will broadcast to target box along the assigned axis.
G
gaoyuan 已提交
125 126 127 128 129 130 131 132
)DOC");
  }
};

}  // namespace operators
}  // namespace paddle

namespace ops = paddle::operators;
L
lyq 已提交
133 134 135 136 137

DECLARE_INFER_SHAPE_FUNCTOR(box_coder,
                            BoxCoderInferShapeFunctor,
                            PD_INFER_META(phi::BoxCoderInferMeta));

H
hong 已提交
138
REGISTER_OPERATOR(
139 140 141
    box_coder,
    ops::BoxCoderOp,
    ops::BoxCoderOpMaker,
H
hong 已提交
142
    paddle::framework::EmptyGradOpMaker<paddle::framework::OpDesc>,
L
lyq 已提交
143 144
    paddle::framework::EmptyGradOpMaker<paddle::imperative::OpBase>,
    BoxCoderInferShapeFunctor);