|
| 1 | +/* Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved. |
| 2 | +
|
| 3 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +you may not use this file except in compliance with the License. |
| 5 | +You may obtain a copy of the License at |
| 6 | +
|
| 7 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +
|
| 9 | +Unless required by applicable law or agreed to in writing, software |
| 10 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +See the License for the specific language governing permissions and |
| 13 | +limitations under the License. */ |
| 14 | + |
| 15 | +#include "paddle/fluid/operators/detection/nms_op.h" |
| 16 | +#include <vector> |
| 17 | + |
| 18 | +namespace paddle { |
| 19 | +namespace operators { |
| 20 | + |
| 21 | +using framework::Tensor; |
| 22 | + |
| 23 | +class NMSOpMaker : public framework::OpProtoAndCheckerMaker { |
| 24 | + public: |
| 25 | + void Make() override { |
| 26 | + AddInput("Boxes", |
| 27 | + "(Tensor) " |
| 28 | + "Boxes is a Tensor with shape [N, 4] " |
| 29 | + "N is the number of boxes " |
| 30 | + "in last dimension in format [x1, x2, y1, y2] " |
| 31 | + "the relation should be ``0 <= x1 < x2 && 0 <= y1 < y2``."); |
| 32 | + |
| 33 | + AddOutput("KeepBoxesIdxs", |
| 34 | + "(Tensor) " |
| 35 | + "KeepBoxesIdxs is a Tensor with shape [N] "); |
| 36 | + AddAttr<float>( |
| 37 | + "iou_threshold", |
| 38 | + "iou_threshold is a threshold value used to compress similar boxes " |
| 39 | + "boxes with IoU > iou_threshold will be considered as overlapping " |
| 40 | + "and just one of them can be kept.") |
| 41 | + .SetDefault(1.0f) |
| 42 | + .AddCustomChecker([](const float& iou_threshold) { |
| 43 | + PADDLE_ENFORCE_LE(iou_threshold, 1.0f, |
| 44 | + platform::errors::InvalidArgument( |
| 45 | + "iou_threshold should less equal than 1.0 " |
| 46 | + "but got %f", |
| 47 | + iou_threshold)); |
| 48 | + PADDLE_ENFORCE_GE(iou_threshold, 0.0f, |
| 49 | + platform::errors::InvalidArgument( |
| 50 | + "iou_threshold should greater equal than 0.0 " |
| 51 | + "but got %f", |
| 52 | + iou_threshold)); |
| 53 | + }); |
| 54 | + AddComment(R"DOC( |
| 55 | + NMS Operator. |
| 56 | + This Operator is used to perform Non-Maximum Compress for input boxes. |
| 57 | + Indices of boxes kept by NMS will be sorted by scores and output. |
| 58 | + )DOC"); |
| 59 | + } |
| 60 | +}; |
| 61 | + |
| 62 | +class NMSOp : public framework::OperatorWithKernel { |
| 63 | + public: |
| 64 | + using framework::OperatorWithKernel::OperatorWithKernel; |
| 65 | + void InferShape(framework::InferShapeContext* ctx) const override { |
| 66 | + OP_INOUT_CHECK(ctx->HasInput("Boxes"), "Input", "Boxes", "NMS"); |
| 67 | + OP_INOUT_CHECK(ctx->HasOutput("KeepBoxesIdxs"), "Output", "KeepBoxesIdxs", |
| 68 | + "NMS"); |
| 69 | + |
| 70 | + auto boxes_dim = ctx->GetInputDim("Boxes"); |
| 71 | + PADDLE_ENFORCE_EQ(boxes_dim.size(), 2, |
| 72 | + platform::errors::InvalidArgument( |
| 73 | + "The Input Boxes must be 2-dimention " |
| 74 | + "whose shape must be [N, 4] " |
| 75 | + "N is the number of boxes " |
| 76 | + "in last dimension in format [x1, x2, y1, y2]. ")); |
| 77 | + auto num_boxes = boxes_dim[0]; |
| 78 | + |
| 79 | + ctx->SetOutputDim("KeepBoxesIdxs", {num_boxes}); |
| 80 | + } |
| 81 | + |
| 82 | + protected: |
| 83 | + framework::OpKernelType GetExpectedKernelType( |
| 84 | + const framework::ExecutionContext& ctx) const override { |
| 85 | + return framework::OpKernelType( |
| 86 | + OperatorWithKernel::IndicateVarDataType(ctx, "Boxes"), ctx.GetPlace()); |
| 87 | + } |
| 88 | +}; |
| 89 | + |
| 90 | +template <typename T> |
| 91 | +static void NMS(const T* boxes_data, int64_t* output_data, float threshold, |
| 92 | + int64_t num_boxes) { |
| 93 | + auto num_masks = CeilDivide(num_boxes, 64); |
| 94 | + std::vector<uint64_t> masks(num_masks, 0); |
| 95 | + |
| 96 | + for (int64_t i = 0; i < num_boxes; ++i) { |
| 97 | + if (masks[i / 64] & 1ULL << (i % 64)) continue; |
| 98 | + T box_1[4]; |
| 99 | + for (int k = 0; k < 4; ++k) { |
| 100 | + box_1[k] = boxes_data[i * 4 + k]; |
| 101 | + } |
| 102 | + for (int64_t j = i + 1; j < num_boxes; ++j) { |
| 103 | + if (masks[j / 64] & 1ULL << (j % 64)) continue; |
| 104 | + T box_2[4]; |
| 105 | + for (int k = 0; k < 4; ++k) { |
| 106 | + box_2[k] = boxes_data[j * 4 + k]; |
| 107 | + } |
| 108 | + bool is_overlap = CalculateIoU<T>(box_1, box_2, threshold); |
| 109 | + if (is_overlap) { |
| 110 | + masks[j / 64] |= 1ULL << (j % 64); |
| 111 | + } |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + int64_t output_data_idx = 0; |
| 116 | + for (int64_t i = 0; i < num_boxes; ++i) { |
| 117 | + if (masks[i / 64] & 1ULL << (i % 64)) continue; |
| 118 | + output_data[output_data_idx++] = i; |
| 119 | + } |
| 120 | + |
| 121 | + for (; output_data_idx < num_boxes; ++output_data_idx) { |
| 122 | + output_data[output_data_idx] = 0; |
| 123 | + } |
| 124 | +} |
| 125 | + |
| 126 | +template <typename T> |
| 127 | +class NMSKernel : public framework::OpKernel<T> { |
| 128 | + public: |
| 129 | + void Compute(const framework::ExecutionContext& context) const override { |
| 130 | + const Tensor* boxes = context.Input<Tensor>("Boxes"); |
| 131 | + Tensor* output = context.Output<Tensor>("KeepBoxesIdxs"); |
| 132 | + int64_t* output_data = output->mutable_data<int64_t>(context.GetPlace()); |
| 133 | + auto threshold = context.template Attr<float>("iou_threshold"); |
| 134 | + NMS<T>(boxes->data<T>(), output_data, threshold, boxes->dims()[0]); |
| 135 | + } |
| 136 | +}; |
| 137 | + |
| 138 | +} // namespace operators |
| 139 | +} // namespace paddle |
| 140 | + |
| 141 | +namespace ops = paddle::operators; |
| 142 | + |
| 143 | +REGISTER_OPERATOR( |
| 144 | + nms, ops::NMSOp, ops::NMSOpMaker, |
| 145 | + paddle::framework::EmptyGradOpMaker<paddle::framework::OpDesc>, |
| 146 | + paddle::framework::EmptyGradOpMaker<paddle::imperative::OpBase>); |
| 147 | +REGISTER_OP_CPU_KERNEL(nms, ops::NMSKernel<float>, ops::NMSKernel<double>); |
0 commit comments