未验证 提交 5e1ee106 编写于 作者: L LoneRanger 提交者: GitHub

support the 'generate_proposals' for generating static graph ops (#52940)

* support the 'generate_proposals' for generating static graph ops

* fix bug

* fix bug

* Update ops.yaml

fix bug

* remove the generate_proposals_v2_op.cc from CMakeLists.txt

* fix bug

* fix bug
上级 4e5a14c1
...@@ -36,12 +36,10 @@ if(WITH_XPU) ...@@ -36,12 +36,10 @@ if(WITH_XPU)
detection_library(iou_similarity_op SRCS iou_similarity_op.cc detection_library(iou_similarity_op SRCS iou_similarity_op.cc
iou_similarity_op_xpu.cc) iou_similarity_op_xpu.cc)
detection_library(prior_box_op SRCS prior_box_op.cc) detection_library(prior_box_op SRCS prior_box_op.cc)
detection_library(generate_proposals_v2_op SRCS generate_proposals_v2_op.cc)
else() else()
detection_library(iou_similarity_op SRCS iou_similarity_op.cc detection_library(iou_similarity_op SRCS iou_similarity_op.cc
iou_similarity_op.cu) iou_similarity_op.cu)
detection_library(prior_box_op SRCS prior_box_op.cc) detection_library(prior_box_op SRCS prior_box_op.cc)
# detection_library(generate_proposals_v2_op SRCS generate_proposals_v2_op.cc)
endif() endif()
detection_library(bipartite_match_op SRCS bipartite_match_op.cc) detection_library(bipartite_match_op SRCS bipartite_match_op.cc)
...@@ -74,17 +72,12 @@ if(WITH_GPU OR WITH_ROCM) ...@@ -74,17 +72,12 @@ if(WITH_GPU OR WITH_ROCM)
endif() endif()
detection_library(generate_proposals_op SRCS generate_proposals_op.cc detection_library(generate_proposals_op SRCS generate_proposals_op.cc
generate_proposals_op.cu DEPS ${TMPDEPS}) generate_proposals_op.cu DEPS ${TMPDEPS})
detection_library(generate_proposals_v2_op SRCS generate_proposals_v2_op.cc
DEPS ${TMPDEPS})
detection_library(distribute_fpn_proposals_op SRCS detection_library(distribute_fpn_proposals_op SRCS
distribute_fpn_proposals_op.cc DEPS ${TMPDEPS}) distribute_fpn_proposals_op.cc DEPS ${TMPDEPS})
detection_library(collect_fpn_proposals_op SRCS collect_fpn_proposals_op.cc detection_library(collect_fpn_proposals_op SRCS collect_fpn_proposals_op.cc
collect_fpn_proposals_op.cu DEPS ${TMPDEPS}) collect_fpn_proposals_op.cu DEPS ${TMPDEPS})
else() else()
detection_library(generate_proposals_op SRCS generate_proposals_op.cc) detection_library(generate_proposals_op SRCS generate_proposals_op.cc)
if(NOT WITH_XPU)
detection_library(generate_proposals_v2_op SRCS generate_proposals_v2_op.cc)
endif()
detection_library(distribute_fpn_proposals_op SRCS detection_library(distribute_fpn_proposals_op SRCS
distribute_fpn_proposals_op.cc) distribute_fpn_proposals_op.cc)
detection_library(collect_fpn_proposals_op SRCS collect_fpn_proposals_op.cc) detection_library(collect_fpn_proposals_op SRCS collect_fpn_proposals_op.cc)
......
/* 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.
See the License for the specific language governing permissions and
limitations under the License. */
#include <cmath>
#include <cstring>
#include <string>
#include <vector>
#include "paddle/fluid/framework/infershape_utils.h"
#include "paddle/fluid/framework/op_registry.h"
#include "paddle/fluid/framework/op_version_registry.h"
#include "paddle/fluid/operators/detection/bbox_util.h"
#include "paddle/phi/infermeta/multiary.h"
#include "paddle/phi/kernels/funcs/detection/nms_util.h"
#include "paddle/phi/kernels/funcs/gather.h"
#include "paddle/phi/kernels/funcs/math_function.h"
namespace paddle {
namespace operators {
class GenerateProposalsV2Op : public framework::OperatorWithKernel {
public:
using framework::OperatorWithKernel::OperatorWithKernel;
protected:
phi::KernelKey GetExpectedKernelType(
const framework::ExecutionContext &ctx) const override {
return phi::KernelKey(
OperatorWithKernel::IndicateVarDataType(ctx, "Anchors"),
ctx.GetPlace());
}
};
class GenerateProposalsV2OpMaker : public framework::OpProtoAndCheckerMaker {
public:
void Make() override {
AddInput("Scores",
"(Tensor) The scores from conv is in shape (N, A, H, W), "
"N is batch size, A is number of anchors, "
"H and W are height and width of the feature map");
AddInput("BboxDeltas",
"(Tensor) Bounding box deltas from conv is in "
"shape (N, 4*A, H, W).");
AddInput("ImShape",
"(Tensor) Image shape in shape (N, 2), "
"in format (height, width)");
AddInput("Anchors",
"(Tensor) Bounding box anchors from anchor_generator_op "
"is in shape (A, H, W, 4).");
AddInput("Variances",
"(Tensor) Bounding box variances with same shape as `Anchors`.");
AddOutput("RpnRois",
"(phi::DenseTensor), Output proposals with shape (rois_num, 4).");
AddOutput(
"RpnRoiProbs",
"(phi::DenseTensor) Scores of proposals with shape (rois_num, 1).");
AddOutput("RpnRoisNum", "(Tensor), The number of Rpn RoIs in each image")
.AsDispensable();
AddAttr<int>("pre_nms_topN",
"Number of top scoring RPN proposals to keep before "
"applying NMS.");
AddAttr<int>("post_nms_topN",
"Number of top scoring RPN proposals to keep after "
"applying NMS");
AddAttr<float>("nms_thresh", "NMS threshold used on RPN proposals.");
AddAttr<float>("min_size",
"Proposal height and width both need to be greater "
"than this min_size.");
AddAttr<float>("eta", "The parameter for adaptive NMS.");
AddAttr<bool>("pixel_offset",
"(bool, default True),"
"If true, im_shape pixel offset is 1.")
.SetDefault(true);
AddComment(R"DOC(
This operator is the second version of generate_proposals op to generate
bounding box proposals for Faster RCNN.
The proposals are generated for a list of images based on image
score 'Scores', bounding box regression result 'BboxDeltas' as
well as predefined bounding box shapes 'anchors'. Greedy
non-maximum suppression is applied to generate the final bounding
boxes.
The difference between this version and the first version is that the image
scale is no long needed now, so the input requires im_shape instead of im_info.
The change aims to unify the input for all kinds of objective detection
such as YOLO-v3 and Faster R-CNN. As a result, the min_size represents the
size on input image instead of original image which is slightly different
to before and will not effect the result.
)DOC");
}
};
} // namespace operators
} // namespace paddle
DECLARE_INFER_SHAPE_FUNCTOR(generate_proposals_v2,
GenerateProposalsV2InferShapeFunctor,
PD_INFER_META(phi::GenerateProposalsV2InferMeta));
namespace ops = paddle::operators;
REGISTER_OPERATOR(
generate_proposals_v2,
ops::GenerateProposalsV2Op,
ops::GenerateProposalsV2OpMaker,
paddle::framework::EmptyGradOpMaker<paddle::framework::OpDesc>,
paddle::framework::EmptyGradOpMaker<paddle::imperative::OpBase>,
GenerateProposalsV2InferShapeFunctor);
REGISTER_OP_VERSION(generate_proposals_v2)
.AddCheckpoint(
R"ROC(Registe generate_proposals_v2 for adding the attribute of pixel_offset)ROC",
paddle::framework::compatible::OpVersionDesc().NewAttr(
"pixel_offset", "If true, im_shape pixel offset is 1.", true));
...@@ -567,14 +567,6 @@ ...@@ -567,14 +567,6 @@
data_type : dtype data_type : dtype
backend : place backend : place
- op : generate_proposals
args : (Tensor scores, Tensor bbox_deltas, Tensor im_shape, Tensor anchors, Tensor variances, int pre_nms_top_n, int post_nms_top_n, float nms_thresh, float min_size, float eta, bool pixel_offset=true)
output : Tensor(rpn_rois), Tensor(rpn_roi_probs), Tensor(rpn_rois_num)
infer_meta :
func : GenerateProposalsV2InferMeta
kernel :
func : generate_proposals
- op : greater_equal - op : greater_equal
args : (Tensor x, Tensor y) args : (Tensor x, Tensor y)
output : Tensor(out) output : Tensor(out)
......
...@@ -990,6 +990,14 @@ ...@@ -990,6 +990,14 @@
extra : extra :
attrs : [bool use_mkldnn = false, str mkldnn_data_type = "float32"] attrs : [bool use_mkldnn = false, str mkldnn_data_type = "float32"]
- op : generate_proposals(generate_proposals_v2)
inputs :
{scores : Scores, bbox_deltas : BboxDeltas, im_shape : ImShape, anchors : Anchors, variances : Variances}
outputs :
{rpn_rois : RpnRois, rpn_roi_probs : RpnRoiProbs, rpn_rois_num : RpnRoisNum}
attrs :
{pre_nms_top_n : pre_nms_topN, post_nms_top_n : post_nms_topN}
- op : grad_add - op : grad_add
extra : extra :
attrs : [bool use_mkldnn = false, str x_data_format = "", str y_data_format = "", str mkldnn_data_type = "float32", attrs : [bool use_mkldnn = false, str x_data_format = "", str y_data_format = "", str mkldnn_data_type = "float32",
......
...@@ -117,6 +117,14 @@ ...@@ -117,6 +117,14 @@
comment : "The arg 'default_value' of attr 'shape' is changed: from 'None' to '{}'." comment : "The arg 'default_value' of attr 'shape' is changed: from 'None' to '{}'."
default : std::vector<int64_t>{} default : std::vector<int64_t>{}
- op : generate_proposals
version :
- checkpoint : Registe generate_proposals_v2 for adding the attribute of pixel_offset
action :
- add_attr : pixel_offset
comment : If true, im_shape pixel offset is 1.
default : "true"
- op : greater_equal - op : greater_equal
version : version :
- checkpoint : Upgrade compare ops, add a new attribute [force_cpu] - checkpoint : Upgrade compare ops, add a new attribute [force_cpu]
......
...@@ -805,6 +805,16 @@ ...@@ -805,6 +805,16 @@
func : gelu func : gelu
backward : gelu_grad backward : gelu_grad
- op : generate_proposals
args : (Tensor scores, Tensor bbox_deltas, Tensor im_shape, Tensor anchors, Tensor variances, int pre_nms_top_n, int post_nms_top_n, float nms_thresh, float min_size, float eta, bool pixel_offset=true)
output : Tensor(rpn_rois), Tensor(rpn_roi_probs), Tensor(rpn_rois_num)
infer_meta :
func : GenerateProposalsV2InferMeta
kernel :
func : generate_proposals
data_type : anchors
optional : rpn_rois_num
- op : grid_sample - op : grid_sample
args : (Tensor x, Tensor grid, str mode = "bilinear", str padding_mode = "zeros", bool align_corners = true) args : (Tensor x, Tensor grid, str mode = "bilinear", str padding_mode = "zeros", bool align_corners = true)
output : Tensor(out) output : Tensor(out)
......
// 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 "paddle/phi/core/compat/op_utils.h"
PD_REGISTER_BASE_KERNEL_NAME(generate_proposals_v2, generate_proposals);
PD_REGISTER_BASE_KERNEL_NAME(generate_proposals_v2_grad,
generate_proposals_grad);
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册