matrix_nms_op.cc 6.3 KB
Newer Older
Y
Yang Zhang 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13
/* Copyright (c) 2020 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.
limitations under the License. */

Z
zhiboniu 已提交
14
#include "paddle/fluid/framework/infershape_utils.h"
Y
Yang Zhang 已提交
15
#include "paddle/fluid/framework/op_registry.h"
16
#include "paddle/fluid/framework/op_version_registry.h"
Z
zhiboniu 已提交
17
#include "paddle/phi/infermeta/binary.h"
Z
zhiboniu 已提交
18
#include "paddle/phi/kernels/funcs/detection/nms_util.h"
Y
Yang Zhang 已提交
19 20 21 22 23 24 25 26 27

namespace paddle {
namespace operators {

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

 protected:
28
  phi::KernelKey GetExpectedKernelType(
Y
Yang Zhang 已提交
29
      const framework::ExecutionContext& ctx) const override {
30
    return phi::KernelKey(
Y
Yang Zhang 已提交
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
        OperatorWithKernel::IndicateVarDataType(ctx, "Scores"),
        platform::CPUPlace());
  }
};

class MatrixNMSOpMaker : public framework::OpProtoAndCheckerMaker {
 public:
  void Make() override {
    AddInput("BBoxes",
             "(Tensor) A 3-D Tensor with shape "
             "[N, M, 4] represents the predicted locations of M bounding boxes"
             ", N is the batch size. "
             "Each bounding box has four coordinate values and the layout is "
             "[xmin, ymin, xmax, ymax], when box size equals to 4.");
    AddInput("Scores",
             "(Tensor) A 3-D Tensor with shape [N, C, M] represents the "
             "predicted confidence predictions. N is the batch size, C is the "
             "class number, M is number of bounding boxes. For each category "
             "there are total M scores which corresponding M bounding boxes. "
             " Please note, M is equal to the 2nd dimension of BBoxes. ");
    AddAttr<int>(
        "background_label",
        "(int, default: 0) "
        "The index of background label, the background label will be ignored. "
        "If set to -1, then all categories will be considered.")
        .SetDefault(0);
    AddAttr<float>("score_threshold",
                   "(float) "
                   "Threshold to filter out bounding boxes with low "
                   "confidence score.");
    AddAttr<float>("post_threshold",
                   "(float, default 0.) "
                   "Threshold to filter out bounding boxes with low "
                   "confidence score AFTER decaying.")
        .SetDefault(0.);
    AddAttr<int>("nms_top_k",
                 "(int64_t) "
                 "Maximum number of detections to be kept according to the "
                 "confidences after the filtering detections based on "
                 "score_threshold");
    AddAttr<int>("keep_top_k",
                 "(int64_t) "
                 "Number of total bboxes to be kept per image after NMS "
                 "step. -1 means keeping all bboxes after NMS step.");
    AddAttr<bool>("normalized",
                  "(bool, default true) "
                  "Whether detections are normalized.")
        .SetDefault(true);
    AddAttr<bool>("use_gaussian",
                  "(bool, default false) "
                  "Whether to use Gaussian as decreasing function.")
        .SetDefault(false);
    AddAttr<float>("gaussian_sigma",
                   "(float) "
85
                   "Sigma for Gaussian decreasing function, only takes effect "
Y
Yang Zhang 已提交
86 87 88
                   "when 'use_gaussian' is enabled.")
        .SetDefault(2.);
    AddOutput("Out",
89 90
              "(phi::DenseTensor) A 2-D phi::DenseTensor with shape [No, 6] "
              "represents the "
Y
Yang Zhang 已提交
91 92 93 94 95 96
              "detections. Each row has 6 values: "
              "[label, confidence, xmin, ymin, xmax, ymax]. "
              "the offsets in first dimension are called LoD, the number of "
              "offset is N + 1, if LoD[i + 1] - LoD[i] == 0, means there is "
              "no detected bbox.");
    AddOutput("Index",
97 98
              "(phi::DenseTensor) A 2-D phi::DenseTensor with shape [No, 1] "
              "represents the "
Y
Yang Zhang 已提交
99 100
              "index of selected bbox. The index is the absolute index cross "
              "batches.");
101 102
    AddOutput("RoisNum", "(Tensor), Number of RoIs in each images.")
        .AsDispensable();
Y
Yang Zhang 已提交
103 104 105 106 107 108 109 110 111 112 113 114
    AddComment(R"DOC(
This operator does multi-class matrix non maximum suppression (NMS) on batched
boxes and scores.
In the NMS step, this operator greedily selects a subset of detection bounding
boxes that have high scores larger than score_threshold, if providing this
threshold, then selects the largest nms_top_k confidences scores if nms_top_k
is larger than -1. Then this operator decays boxes score according to the
Matrix NMS scheme.
Aftern NMS step, at most keep_top_k number of total bboxes are to be kept
per image if keep_top_k is larger than -1.
This operator support multi-class and batched inputs. It applying NMS
independently for each class. The outputs is a 2-D LoDTenosr, for each
115
image, the offsets in first dimension of phi::DenseTensor are called LoD, the number
Y
Yang Zhang 已提交
116
of offset is N + 1, where N is the batch size. If LoD[i + 1] - LoD[i] == 0,
117
means there is no detected bbox for this image. Now this operator has one more
118
output, which is RoisNum. The size of RoisNum is N, RoisNum[i] means the number of
119
detected bbox for this image.
Y
Yang Zhang 已提交
120 121 122 123 124 125 126 127 128 129

For more information on Matrix NMS, please refer to:
https://arxiv.org/abs/2003.10152
)DOC");
  }
};

}  // namespace operators
}  // namespace paddle

Z
zhiboniu 已提交
130 131 132 133
DECLARE_INFER_SHAPE_FUNCTOR(matrix_nms,
                            MatrixNMSInferShapeFunctor,
                            PD_INFER_META(phi::MatrixNMSInferMeta));

Y
Yang Zhang 已提交
134 135
namespace ops = paddle::operators;
REGISTER_OPERATOR(
136 137 138
    matrix_nms,
    ops::MatrixNMSOp,
    ops::MatrixNMSOpMaker,
Y
Yang Zhang 已提交
139
    paddle::framework::EmptyGradOpMaker<paddle::framework::OpDesc>,
Z
zhiboniu 已提交
140 141 142
    paddle::framework::EmptyGradOpMaker<paddle::imperative::OpBase>,
    MatrixNMSInferShapeFunctor);

143
REGISTER_OP_VERSION(matrix_nms)
144 145 146
    .AddCheckpoint(R"ROC(Upgrade matrix_nms: add a new output [RoisNum].)ROC",
                   paddle::framework::compatible::OpVersionDesc().NewOutput(
                       "RoisNum", "The number of RoIs in each image."));