未验证 提交 4bf1c163 编写于 作者: I Infinity_lee 提交者: GitHub

support auto generate for nms (#51891)

上级 5bcdfbb0
...@@ -83,7 +83,6 @@ detection_library(sigmoid_focal_loss_op SRCS sigmoid_focal_loss_op.cc ...@@ -83,7 +83,6 @@ detection_library(sigmoid_focal_loss_op SRCS sigmoid_focal_loss_op.cc
sigmoid_focal_loss_op.cu) sigmoid_focal_loss_op.cu)
detection_library(retinanet_detection_output_op SRCS detection_library(retinanet_detection_output_op SRCS
retinanet_detection_output_op.cc) retinanet_detection_output_op.cc)
detection_library(nms_op SRCS nms_op.cc)
if(WITH_GPU OR WITH_ROCM) if(WITH_GPU OR WITH_ROCM)
set(TMPDEPS memory) set(TMPDEPS memory)
......
/* 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 <vector>
#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<float>(
"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 <typename T>
class NMSKernel : public framework::OpKernel<T> {};
} // 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::OpDesc>,
paddle::framework::EmptyGradOpMaker<paddle::imperative::OpBase>,
NMSInferMetaFunctor);
...@@ -1168,15 +1168,6 @@ ...@@ -1168,15 +1168,6 @@
inplace : (x -> out) inplace : (x -> out)
backward : multiply_grad 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 - op : norm
args : (Tensor x, int axis, float epsilon, bool is_test) args : (Tensor x, int axis, float epsilon, bool is_test)
output : Tensor(out), Tensor(norm) output : Tensor(out), Tensor(norm)
......
...@@ -1281,6 +1281,14 @@ ...@@ -1281,6 +1281,14 @@
outputs : outputs :
{out : Out, total_weight : Total_weight} {out : Out, total_weight : Total_weight}
- op : nms
inputs :
x : Boxes
outputs :
out : KeepBoxesIdxs
attrs :
threshold : iou_threshold
- op : nonzero (where_index) - op : nonzero (where_index)
inputs : inputs :
condition : Condition condition : Condition
......
...@@ -1071,6 +1071,15 @@ ...@@ -1071,6 +1071,15 @@
optional : weight optional : weight
backward : nll_loss_grad 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 - op : nonzero
args : (Tensor condition) args : (Tensor condition)
output : Tensor(out) output : Tensor(out)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册