From 5e1ee1064052ffe285bc18e3fdeb5667824f1c20 Mon Sep 17 00:00:00 2001 From: LoneRanger <836253168@qq.com> Date: Mon, 24 Apr 2023 11:44:36 +0800 Subject: [PATCH] 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 --- .../fluid/operators/detection/CMakeLists.txt | 7 - .../detection/generate_proposals_v2_op.cc | 126 ------------------ paddle/phi/api/yaml/legacy_ops.yaml | 8 -- paddle/phi/api/yaml/op_compat.yaml | 8 ++ paddle/phi/api/yaml/op_version.yaml | 8 ++ paddle/phi/api/yaml/ops.yaml | 10 ++ .../phi/ops/compat/generate_proposals_sig.cc | 19 --- 7 files changed, 26 insertions(+), 160 deletions(-) delete mode 100644 paddle/fluid/operators/detection/generate_proposals_v2_op.cc delete mode 100644 paddle/phi/ops/compat/generate_proposals_sig.cc diff --git a/paddle/fluid/operators/detection/CMakeLists.txt b/paddle/fluid/operators/detection/CMakeLists.txt index 6b4a6061cbb..8c5c1a5d8a2 100644 --- a/paddle/fluid/operators/detection/CMakeLists.txt +++ b/paddle/fluid/operators/detection/CMakeLists.txt @@ -36,12 +36,10 @@ if(WITH_XPU) detection_library(iou_similarity_op SRCS iou_similarity_op.cc iou_similarity_op_xpu.cc) detection_library(prior_box_op SRCS prior_box_op.cc) - detection_library(generate_proposals_v2_op SRCS generate_proposals_v2_op.cc) else() detection_library(iou_similarity_op SRCS iou_similarity_op.cc iou_similarity_op.cu) detection_library(prior_box_op SRCS prior_box_op.cc) - # detection_library(generate_proposals_v2_op SRCS generate_proposals_v2_op.cc) endif() detection_library(bipartite_match_op SRCS bipartite_match_op.cc) @@ -74,17 +72,12 @@ if(WITH_GPU OR WITH_ROCM) endif() detection_library(generate_proposals_op SRCS generate_proposals_op.cc 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 distribute_fpn_proposals_op.cc DEPS ${TMPDEPS}) detection_library(collect_fpn_proposals_op SRCS collect_fpn_proposals_op.cc collect_fpn_proposals_op.cu DEPS ${TMPDEPS}) else() 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 distribute_fpn_proposals_op.cc) detection_library(collect_fpn_proposals_op SRCS collect_fpn_proposals_op.cc) diff --git a/paddle/fluid/operators/detection/generate_proposals_v2_op.cc b/paddle/fluid/operators/detection/generate_proposals_v2_op.cc deleted file mode 100644 index 885a3575664..00000000000 --- a/paddle/fluid/operators/detection/generate_proposals_v2_op.cc +++ /dev/null @@ -1,126 +0,0 @@ -/* 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 -#include -#include -#include - -#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("pre_nms_topN", - "Number of top scoring RPN proposals to keep before " - "applying NMS."); - AddAttr("post_nms_topN", - "Number of top scoring RPN proposals to keep after " - "applying NMS"); - AddAttr("nms_thresh", "NMS threshold used on RPN proposals."); - AddAttr("min_size", - "Proposal height and width both need to be greater " - "than this min_size."); - AddAttr("eta", "The parameter for adaptive NMS."); - AddAttr("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::EmptyGradOpMaker, - 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)); diff --git a/paddle/phi/api/yaml/legacy_ops.yaml b/paddle/phi/api/yaml/legacy_ops.yaml index 789adede519..a3ff474a69e 100755 --- a/paddle/phi/api/yaml/legacy_ops.yaml +++ b/paddle/phi/api/yaml/legacy_ops.yaml @@ -567,14 +567,6 @@ data_type : dtype 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 args : (Tensor x, Tensor y) output : Tensor(out) diff --git a/paddle/phi/api/yaml/op_compat.yaml b/paddle/phi/api/yaml/op_compat.yaml index 490aff92c19..caf3194b899 100755 --- a/paddle/phi/api/yaml/op_compat.yaml +++ b/paddle/phi/api/yaml/op_compat.yaml @@ -990,6 +990,14 @@ extra : 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 extra : attrs : [bool use_mkldnn = false, str x_data_format = "", str y_data_format = "", str mkldnn_data_type = "float32", diff --git a/paddle/phi/api/yaml/op_version.yaml b/paddle/phi/api/yaml/op_version.yaml index ae7da60ac04..35ee674cd14 100644 --- a/paddle/phi/api/yaml/op_version.yaml +++ b/paddle/phi/api/yaml/op_version.yaml @@ -117,6 +117,14 @@ comment : "The arg 'default_value' of attr 'shape' is changed: from 'None' to '{}'." default : std::vector{} +- 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 version : - checkpoint : Upgrade compare ops, add a new attribute [force_cpu] diff --git a/paddle/phi/api/yaml/ops.yaml b/paddle/phi/api/yaml/ops.yaml index 860136f3192..b07bf0ecb99 100644 --- a/paddle/phi/api/yaml/ops.yaml +++ b/paddle/phi/api/yaml/ops.yaml @@ -805,6 +805,16 @@ func : gelu 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 args : (Tensor x, Tensor grid, str mode = "bilinear", str padding_mode = "zeros", bool align_corners = true) output : Tensor(out) diff --git a/paddle/phi/ops/compat/generate_proposals_sig.cc b/paddle/phi/ops/compat/generate_proposals_sig.cc deleted file mode 100644 index fc6696d4a27..00000000000 --- a/paddle/phi/ops/compat/generate_proposals_sig.cc +++ /dev/null @@ -1,19 +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 "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); -- GitLab