From 4bf1c163444d7f718efdd6eb3e7a5f7aa0557f04 Mon Sep 17 00:00:00 2001 From: Infinity_lee Date: Thu, 23 Mar 2023 10:54:11 +0800 Subject: [PATCH] support auto generate for nms (#51891) --- .../fluid/operators/detection/CMakeLists.txt | 1 - paddle/fluid/operators/detection/nms_op.cc | 96 ------------------- paddle/phi/api/yaml/legacy_ops.yaml | 9 -- paddle/phi/api/yaml/op_compat.yaml | 8 ++ paddle/phi/api/yaml/ops.yaml | 9 ++ 5 files changed, 17 insertions(+), 106 deletions(-) delete mode 100644 paddle/fluid/operators/detection/nms_op.cc diff --git a/paddle/fluid/operators/detection/CMakeLists.txt b/paddle/fluid/operators/detection/CMakeLists.txt index 81860c6049203..639d376485b4b 100644 --- a/paddle/fluid/operators/detection/CMakeLists.txt +++ b/paddle/fluid/operators/detection/CMakeLists.txt @@ -83,7 +83,6 @@ detection_library(sigmoid_focal_loss_op SRCS sigmoid_focal_loss_op.cc sigmoid_focal_loss_op.cu) detection_library(retinanet_detection_output_op SRCS retinanet_detection_output_op.cc) -detection_library(nms_op SRCS nms_op.cc) if(WITH_GPU OR WITH_ROCM) set(TMPDEPS memory) diff --git a/paddle/fluid/operators/detection/nms_op.cc b/paddle/fluid/operators/detection/nms_op.cc deleted file mode 100644 index 9171b9ab25ea4..0000000000000 --- a/paddle/fluid/operators/detection/nms_op.cc +++ /dev/null @@ -1,96 +0,0 @@ -/* Copyright (c) 2022 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. -See the License for the specific language governing permissions and -limitations under the License. */ - -#include - -#include "paddle/fluid/framework/infershape_utils.h" -#include "paddle/fluid/framework/op_registry.h" -#include "paddle/fluid/platform/for_range.h" -#include "paddle/phi/core/infermeta_utils.h" -#include "paddle/phi/infermeta/unary.h" - -namespace paddle { -namespace operators { - -class NMSOpMaker : public framework::OpProtoAndCheckerMaker { - public: - void Make() override { - AddInput("Boxes", - "(Tensor) " - "Boxes is a Tensor with shape [N, 4] " - "N is the number of boxes " - "in last dimension in format [x1, x2, y1, y2] " - "the relation should be ``0 <= x1 < x2 && 0 <= y1 < y2``."); - - AddOutput("KeepBoxesIdxs", - "(Tensor) " - "KeepBoxesIdxs is a Tensor with shape [N] "); - AddAttr( - "iou_threshold", - "iou_threshold is a threshold value used to compress similar boxes " - "boxes with IoU > iou_threshold will be considered as overlapping " - "and just one of them can be kept.") - .SetDefault(1.0f) - .AddCustomChecker([](const float& iou_threshold) { - PADDLE_ENFORCE_LE(iou_threshold, - 1.0f, - platform::errors::InvalidArgument( - "iou_threshold should less equal than 1.0 " - "but got %f", - iou_threshold)); - PADDLE_ENFORCE_GE(iou_threshold, - 0.0f, - platform::errors::InvalidArgument( - "iou_threshold should greater equal than 0.0 " - "but got %f", - iou_threshold)); - }); - AddComment(R"DOC( - NMS Operator. - This Operator is used to perform Non-Maximum Compress for input boxes. - Indices of boxes kept by NMS will be sorted by scores and output. - )DOC"); - } -}; - -class NMSOp : public framework::OperatorWithKernel { - public: - using framework::OperatorWithKernel::OperatorWithKernel; - - protected: - phi::KernelKey GetExpectedKernelType( - const framework::ExecutionContext& ctx) const override { - return phi::KernelKey(OperatorWithKernel::IndicateVarDataType(ctx, "Boxes"), - ctx.GetPlace()); - } -}; - -template -class NMSKernel : public framework::OpKernel {}; - -} // namespace operators -} // namespace paddle - -namespace ops = paddle::operators; -DECLARE_INFER_SHAPE_FUNCTOR(nms, - NMSInferMetaFunctor, - PD_INFER_META(phi::NMSInferMeta)); - -REGISTER_OPERATOR( - nms, - ops::NMSOp, - ops::NMSOpMaker, - paddle::framework::EmptyGradOpMaker, - paddle::framework::EmptyGradOpMaker, - NMSInferMetaFunctor); diff --git a/paddle/phi/api/yaml/legacy_ops.yaml b/paddle/phi/api/yaml/legacy_ops.yaml index 46a1e965e21c3..54329cef0ad7a 100755 --- a/paddle/phi/api/yaml/legacy_ops.yaml +++ b/paddle/phi/api/yaml/legacy_ops.yaml @@ -1168,15 +1168,6 @@ inplace : (x -> out) backward : multiply_grad -- op : nms - args : (Tensor x, float threshold) - output : Tensor(out) - infer_meta : - func : NMSInferMeta - kernel : - func : nms - data_type : x - - op : norm args : (Tensor x, int axis, float epsilon, bool is_test) output : Tensor(out), Tensor(norm) diff --git a/paddle/phi/api/yaml/op_compat.yaml b/paddle/phi/api/yaml/op_compat.yaml index 7409fca6980fc..302d0ce4025b9 100644 --- a/paddle/phi/api/yaml/op_compat.yaml +++ b/paddle/phi/api/yaml/op_compat.yaml @@ -1281,6 +1281,14 @@ outputs : {out : Out, total_weight : Total_weight} +- op : nms + inputs : + x : Boxes + outputs : + out : KeepBoxesIdxs + attrs : + threshold : iou_threshold + - op : nonzero (where_index) inputs : condition : Condition diff --git a/paddle/phi/api/yaml/ops.yaml b/paddle/phi/api/yaml/ops.yaml index 0afba610c5cda..9b9fe6afb3496 100644 --- a/paddle/phi/api/yaml/ops.yaml +++ b/paddle/phi/api/yaml/ops.yaml @@ -1071,6 +1071,15 @@ optional : weight backward : nll_loss_grad +- op : nms + args : (Tensor x, float threshold = 1.0f) + output : Tensor(out) + infer_meta : + func : NMSInferMeta + kernel : + func : nms + data_type : x + - op : nonzero args : (Tensor condition) output : Tensor(out)