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

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

Y
Yi Wang 已提交
15
#include "paddle/fluid/operators/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 {
W
wanghaox 已提交
26 27 28 29
    PADDLE_ENFORCE(ctx->HasInput("X"),
                   "Input(X) of IOUSimilarityOp should not be null.");
    PADDLE_ENFORCE(ctx->HasInput("Y"),
                   "Input(Y) of IOUSimilarityOp should not be null.");
W
wanghaox 已提交
30 31 32
    auto x_dims = ctx->GetInputDim("X");
    auto y_dims = ctx->GetInputDim("Y");

W
wanghaox 已提交
33
    PADDLE_ENFORCE_EQ(x_dims.size(), 2UL, "The rank of Input(X) must be 2.");
W
wanghaox 已提交
34
    PADDLE_ENFORCE_EQ(x_dims[1], 4UL, "The shape of X is [N, 4]");
W
wanghaox 已提交
35
    PADDLE_ENFORCE_EQ(y_dims.size(), 2UL, "The rank of Input(Y) must be 2.");
W
wanghaox 已提交
36 37
    PADDLE_ENFORCE_EQ(y_dims[1], 4UL, "The shape of Y is [M, 4]");

W
wanghaox 已提交
38
    ctx->ShareLoD("X", /*->*/ "Out");
W
wanghaox 已提交
39 40 41 42 43 44 45 46
    ctx->SetOutputDim("Out", framework::make_ddim({x_dims[0], y_dims[0]}));
  }
};

class IOUSimilarityOpMaker : public framework::OpProtoAndCheckerMaker {
 public:
  IOUSimilarityOpMaker(OpProto *proto, OpAttrChecker *op_checker)
      : OpProtoAndCheckerMaker(proto, op_checker) {
W
wanghaox 已提交
47
    AddInput("X",
48 49 50
             "(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 已提交
51 52 53 54 55 56 57
             "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 已提交
58 59
    AddInput("Y",
             "(Tensor, default Tensor<float>) "
W
wanghaox 已提交
60 61 62 63 64
             "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.");
W
wanghaox 已提交
65

66
    AddOutput("Out",
W
wanghaox 已提交
67
              "(LoDTensor, the lod is same as input X) The output of "
68 69
              "iou_similarity op, a tensor with shape [N, M] "
              "representing pairwise iou scores.");
W
wanghaox 已提交
70 71 72

    AddComment(R"DOC(
IOU Similarity Operator.
W
wanghaox 已提交
73
Computes intersection-over-union (IOU) between two box lists.
74
 Box list 'X' should be a LoDTensor and 'Y' is a common Tensor,
W
wanghaox 已提交
75 76
 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:
77 78 79 80 81 82

$$
IOU(A, B) = 
\frac{area(A\cap B)}{area(A)+area(B)-area(A\cap B)}
$$

W
wanghaox 已提交
83 84 85 86 87 88 89 90 91 92 93 94 95 96
)DOC");
  }
};
}  // namespace operators
}  // namespace paddle

namespace ops = paddle::operators;
REGISTER_OP_WITHOUT_GRADIENT(iou_similarity, ops::IOUSimilarityOp,
                             ops::IOUSimilarityOpMaker);

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