From ed040a17dba36ed12328102bef595fe8ae3bbf6d Mon Sep 17 00:00:00 2001 From: Wang Xin Date: Wed, 26 Apr 2023 11:03:22 +0800 Subject: [PATCH] add autogen code support for box_coder op (#53309) --- .../fluid/operators/detection/CMakeLists.txt | 1 - .../fluid/operators/detection/box_coder_op.cc | 144 ------------------ paddle/phi/api/yaml/legacy_ops.yaml | 9 -- paddle/phi/api/yaml/op_compat.yaml | 6 + paddle/phi/api/yaml/ops.yaml | 9 ++ paddle/phi/ops/compat/box_coder_sig.cc | 28 ---- 6 files changed, 15 insertions(+), 182 deletions(-) delete mode 100644 paddle/fluid/operators/detection/box_coder_op.cc delete mode 100644 paddle/phi/ops/compat/box_coder_sig.cc diff --git a/paddle/fluid/operators/detection/CMakeLists.txt b/paddle/fluid/operators/detection/CMakeLists.txt index 8c5c1a5d8a2..64f9e03d7e0 100644 --- a/paddle/fluid/operators/detection/CMakeLists.txt +++ b/paddle/fluid/operators/detection/CMakeLists.txt @@ -28,7 +28,6 @@ function(detection_library TARGET_NAME) PARENT_SCOPE) endfunction() -detection_library(box_coder_op SRCS box_coder_op.cc) detection_library(density_prior_box_op SRCS density_prior_box_op.cc density_prior_box_op.cu) diff --git a/paddle/fluid/operators/detection/box_coder_op.cc b/paddle/fluid/operators/detection/box_coder_op.cc deleted file mode 100644 index aafe040991e..00000000000 --- a/paddle/fluid/operators/detection/box_coder_op.cc +++ /dev/null @@ -1,144 +0,0 @@ -/* Copyright (c) 2018 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/phi/core/infermeta_utils.h" -#include "paddle/phi/infermeta/ternary.h" - -namespace paddle { -namespace operators { - -class BoxCoderOp : public framework::OperatorWithKernel { - public: - using framework::OperatorWithKernel::OperatorWithKernel; -}; - -class BoxCoderOpMaker : public framework::OpProtoAndCheckerMaker { - public: - void Make() override { - AddInput( - "PriorBox", - "(Tensor, default Tensor) " - "Box list PriorBox is a 2-D Tensor with shape [M, 4] holds M boxes, " - "each box is represented as [xmin, ymin, xmax, ymax], " - "[xmin, ymin] is the left top coordinate of the anchor box, " - "if the input is image feature map, they are close to the origin " - "of the coordinate system. [xmax, ymax] is the right bottom " - "coordinate of the anchor box."); - AddInput("PriorBoxVar", - "(Tensor, default Tensor, optional) " - "PriorBoxVar is a 2-D Tensor with shape [M, 4] holds M group " - "of variance. PriorBoxVar will set all elements to 1 by " - "default.") - .AsDispensable(); - AddInput( - "TargetBox", - "(phi::DenseTensor or Tensor) This input can be a 2-D phi::DenseTensor " - "with shape " - "[N, 4] when code_type is 'encode_center_size'. This input also can " - "be a 3-D Tensor with shape [N, M, 4] when code_type is " - "'decode_center_size'. [N, 4], each box is represented as " - "[xmin, ymin, xmax, ymax], [xmin, ymin] is the left top coordinate " - "of the box if the input is image feature map, they are close to " - "the origin of the coordinate system. [xmax, ymax] is the right " - "bottom coordinate of the box. This tensor can contain LoD " - "information to represent a batch of inputs. One instance of this " - "batch can contain different numbers of entities."); - AddAttr("code_type", - "(string, default encode_center_size) " - "the code type used with the target box") - .SetDefault("encode_center_size") - .InEnum({"encode_center_size", "decode_center_size"}); - AddAttr("box_normalized", - "(bool, default true) " - "whether treat the priorbox as a normalized box") - .SetDefault(true); - AddAttr("axis", - "(int, default 0)" - "which axis in PriorBox to broadcast for box decode," - "for example, if axis is 0 and TargetBox has shape" - "[N, M, 4] and PriorBox has shape [M, 4], then PriorBox " - "will broadcast to [N, M, 4] for decoding. It is only valid" - "when code type is decode_center_size") - .SetDefault(0) - .InEnum({0, 1}); - AddAttr>( - "variance", - "(vector, default {})," - "variance of prior box with shape [4]. PriorBoxVar and variance can" - "not be provided at the same time.") - .SetDefault(std::vector{}); - AddOutput("OutputBox", - "(phi::DenseTensor or Tensor) " - "When code_type is 'encode_center_size', the output tensor of " - "box_coder_op with shape [N, M, 4] representing the result of N " - "target boxes encoded with M Prior boxes and variances. When " - "code_type is 'decode_center_size', N represents the batch size " - "and M represents the number of decoded boxes."); - - AddComment(R"DOC( - -Bounding Box Coder. - -Encode/Decode the target bounding box with the priorbox information. - -The Encoding schema described below: - - ox = (tx - px) / pw / pxv - - oy = (ty - py) / ph / pyv - - ow = log(abs(tw / pw)) / pwv - - oh = log(abs(th / ph)) / phv - -The Decoding schema described below: - - ox = (pw * pxv * tx * + px) - tw / 2 - - oy = (ph * pyv * ty * + py) - th / 2 - - ow = exp(pwv * tw) * pw + tw / 2 - - oh = exp(phv * th) * ph + th / 2 - -where `tx`, `ty`, `tw`, `th` denote the target box's center coordinates, width -and height respectively. Similarly, `px`, `py`, `pw`, `ph` denote the -priorbox's (anchor) center coordinates, width and height. `pxv`, `pyv`, `pwv`, -`phv` denote the variance of the priorbox and `ox`, `oy`, `ow`, `oh` denote the -encoded/decoded coordinates, width and height. - -During Box Decoding, two modes for broadcast are supported. Say target box has -shape [N, M, 4], and the shape of prior box can be [N, 4] or [M, 4]. Then prior -box will broadcast to target box along the assigned axis. -)DOC"); - } -}; - -} // namespace operators -} // namespace paddle - -namespace ops = paddle::operators; - -DECLARE_INFER_SHAPE_FUNCTOR(box_coder, - BoxCoderInferShapeFunctor, - PD_INFER_META(phi::BoxCoderInferMeta)); - -REGISTER_OPERATOR( - box_coder, - ops::BoxCoderOp, - ops::BoxCoderOpMaker, - paddle::framework::EmptyGradOpMaker, - paddle::framework::EmptyGradOpMaker, - BoxCoderInferShapeFunctor); diff --git a/paddle/phi/api/yaml/legacy_ops.yaml b/paddle/phi/api/yaml/legacy_ops.yaml index 61a8cb76e5c..5bb3e8d0d73 100755 --- a/paddle/phi/api/yaml/legacy_ops.yaml +++ b/paddle/phi/api/yaml/legacy_ops.yaml @@ -139,15 +139,6 @@ func: bincount optional: weights -- op : box_coder - args : (Tensor prior_box, Tensor prior_box_var, Tensor target_box, str code_type, bool box_normalized, int axis, float[] variance) - output : Tensor(output_box) - infer_meta : - func : BoxCoderInferMeta - kernel : - func : box_coder - optional : prior_box_var - - op : cast args : (Tensor x, DataType dtype) output : Tensor diff --git a/paddle/phi/api/yaml/op_compat.yaml b/paddle/phi/api/yaml/op_compat.yaml index 5585d0c2d3b..8d29a773373 100755 --- a/paddle/phi/api/yaml/op_compat.yaml +++ b/paddle/phi/api/yaml/op_compat.yaml @@ -343,6 +343,12 @@ outputs : out : Out +- op : box_coder + inputs : + {prior_box : PriorBox , prior_box_var : PriorBoxVar, target_box: TargetBox} + outputs : + output_box : OutputBox + - op : broadcast_tensors backward : broadcast_tensors_grad inputs : diff --git a/paddle/phi/api/yaml/ops.yaml b/paddle/phi/api/yaml/ops.yaml index c228a0506e8..507fef33096 100644 --- a/paddle/phi/api/yaml/ops.yaml +++ b/paddle/phi/api/yaml/ops.yaml @@ -329,6 +329,15 @@ func : bmm backward : bmm_grad +- op : box_coder + args : (Tensor prior_box, Tensor prior_box_var, Tensor target_box, str code_type = "encode_center_size", bool box_normalized = true, int axis = 0, float[] variance = {}) + output : Tensor(output_box) + infer_meta : + func : BoxCoderInferMeta + kernel : + func : box_coder + optional : prior_box_var + - op : broadcast_tensors args: (Tensor[] input) output: Tensor[]{input.size()} diff --git a/paddle/phi/ops/compat/box_coder_sig.cc b/paddle/phi/ops/compat/box_coder_sig.cc deleted file mode 100644 index 5b674f3dcd2..00000000000 --- a/paddle/phi/ops/compat/box_coder_sig.cc +++ /dev/null @@ -1,28 +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" - -namespace phi { - -KernelSignature BoxCoderOpArgumentMapping(const ArgumentMappingContext& ctx) { - return KernelSignature("box_coder", - {"PriorBox", "PriorBoxVar", "TargetBox"}, - {"code_type", "box_normalized", "axis", "variance"}, - {"OutputBox"}); -} - -} // namespace phi - -PD_REGISTER_ARG_MAPPING_FN(box_coder, phi::BoxCoderOpArgumentMapping); -- GitLab