iou_similarity_op.cc 4.4 KB
Newer Older
1
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved.
W
wanghaox 已提交
2 3 4 5 6 7 8 9 10 11 12 13 14

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. */

B
baiyf 已提交
15
#include "paddle/fluid/operators/detection/iou_similarity_op.h"
W
wanghaox 已提交
16 17 18 19 20 21 22 23 24 25

namespace paddle {
namespace operators {

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

 protected:
  void InferShape(framework::InferShapeContext *ctx) const override {
F
FDInSky 已提交
26 27
    OP_INOUT_CHECK(ctx->HasInput("X"), "Input", "X", "iou_similarity");
    OP_INOUT_CHECK(ctx->HasInput("Y"), "Input", "Y", "iou_similarity");
W
wanghaox 已提交
28 29 30
    auto x_dims = ctx->GetInputDim("X");
    auto y_dims = ctx->GetInputDim("Y");

F
FDInSky 已提交
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
    PADDLE_ENFORCE_EQ(
        x_dims.size(), 2UL,
        platform::errors::InvalidArgument(
            "The rank of Input(X) must be 2, but got dimension = %d.",
            x_dims.size()));
    PADDLE_ENFORCE_EQ(
        x_dims[1], 4UL,
        platform::errors::InvalidArgument(
            "The shape of X is [N, 4], bug got dimension = %d.", x_dims[1]));
    PADDLE_ENFORCE_EQ(
        y_dims.size(), 2UL,
        platform::errors::InvalidArgument(
            "The rank of Input(Y) must be 2, but got dimension = %d.",
            y_dims.size()));
    PADDLE_ENFORCE_EQ(
        y_dims[1], 4UL,
        platform::errors::InvalidArgument(
            "The shape of Y is [M, 4], but got dimension = %d.", y_dims[1]));
W
wanghaox 已提交
49

W
wanghaox 已提交
50
    ctx->ShareLoD("X", /*->*/ "Out");
W
wanghaox 已提交
51 52 53 54 55 56
    ctx->SetOutputDim("Out", framework::make_ddim({x_dims[0], y_dims[0]}));
  }
};

class IOUSimilarityOpMaker : public framework::OpProtoAndCheckerMaker {
 public:
Y
Yu Yang 已提交
57
  void Make() override {
W
wanghaox 已提交
58
    AddInput("X",
59 60 61
             "(LoDTensor, default LoDTensor<float>) "
             "Box list X is a 2-D LoDTensor with shape [N, 4] holds N boxes, "
             "each box is represented as [xmin, ymin, xmax, ymax], "
W
wanghaox 已提交
62 63 64 65 66 67 68
             "the shape of X is [N, 4]. [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.");
W
wanghaox 已提交
69 70
    AddInput("Y",
             "(Tensor, default Tensor<float>) "
W
wanghaox 已提交
71 72 73 74 75
             "Box list Y holds M boxes, each box is represented as "
             "[xmin, ymin, xmax, ymax], the shape of X is [N, 4]. "
             "[xmin, ymin] is the left top coordinate of the box if the "
             "input is image feature map, and [xmax, ymax] is the right "
             "bottom coordinate of the box.");
76 77
    AddAttr<bool>("box_normalized",
                  "(bool, default true) "
T
tianshuo78520a 已提交
78
                  "whether treat the priorbox as a normalized box")
79
        .SetDefault(true);
80
    AddOutput("Out",
W
wanghaox 已提交
81
              "(LoDTensor, the lod is same as input X) The output of "
82 83
              "iou_similarity op, a tensor with shape [N, M] "
              "representing pairwise iou scores.");
W
wanghaox 已提交
84 85

    AddComment(R"DOC(
Y
yi.wu 已提交
86
**IOU Similarity Operator**
Y
yi.wu 已提交
87

W
wanghaox 已提交
88
Computes intersection-over-union (IOU) between two box lists.
Y
yi.wu 已提交
89 90 91
Box list 'X' should be a LoDTensor and 'Y' is a common Tensor,
boxes in 'Y' are shared by all instance of the batched inputs of X.
Given two boxes A and B, the calculation of IOU is as follows:
92 93 94

$$
IOU(A, B) = 
Y
yi.wu 已提交
95
\\frac{area(A\\cap B)}{area(A)+area(B)-area(A\\cap B)}
96 97
$$

W
wanghaox 已提交
98 99 100 101 102 103 104
)DOC");
  }
};
}  // namespace operators
}  // namespace paddle

namespace ops = paddle::operators;
H
hong 已提交
105 106 107 108
REGISTER_OPERATOR(
    iou_similarity, ops::IOUSimilarityOp, ops::IOUSimilarityOpMaker,
    paddle::framework::EmptyGradOpMaker<paddle::framework::OpDesc>,
    paddle::framework::EmptyGradOpMaker<paddle::imperative::OpBase>);
W
wanghaox 已提交
109 110 111 112 113

REGISTER_OP_CPU_KERNEL(
    iou_similarity,
    ops::IOUSimilarityKernel<paddle::platform::CPUDeviceContext, float>,
    ops::IOUSimilarityKernel<paddle::platform::CPUDeviceContext, double>);