未验证 提交 8771fff3 编写于 作者: Z Zhenghai Zhang 提交者: GitHub

static graph autogen code for prior_box (#54508)

上级 ce0c5c27
...@@ -34,11 +34,9 @@ detection_library(density_prior_box_op SRCS density_prior_box_op.cc ...@@ -34,11 +34,9 @@ detection_library(density_prior_box_op SRCS density_prior_box_op.cc
if(WITH_XPU) 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)
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)
endif() endif()
detection_library(bipartite_match_op SRCS bipartite_match_op.cc) detection_library(bipartite_match_op SRCS bipartite_match_op.cc)
......
...@@ -268,3 +268,11 @@ PD_REGISTER_STRUCT_KERNEL(density_prior_box, ...@@ -268,3 +268,11 @@ PD_REGISTER_STRUCT_KERNEL(density_prior_box,
ops::DensityPriorBoxOpKernel, ops::DensityPriorBoxOpKernel,
float, float,
double) {} double) {}
REGISTER_OP_KERNEL(prior_box,
MKLDNN,
::paddle::platform::CPUPlace,
ops::PriorBoxOpKernel<float>,
ops::PriorBoxOpKernel<double>,
ops::PriorBoxOpKernel<uint8_t>,
ops::PriorBoxOpKernel<int8_t>);
/* Copyright (c) 2016 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/fluid/operators/detection/prior_box_op.h"
#include <string>
#include "paddle/fluid/framework/convert_utils.h"
#include "paddle/fluid/framework/infershape_utils.h"
#include "paddle/phi/infermeta/binary.h"
namespace paddle {
namespace operators {
class PriorBoxOp : public framework::OperatorWithKernel {
public:
using framework::OperatorWithKernel::OperatorWithKernel;
protected:
phi::KernelKey GetExpectedKernelType(
const framework::ExecutionContext& ctx) const override {
auto input_input_type =
OperatorWithKernel::IndicateVarDataType(ctx, "Input");
return phi::KernelKey(input_input_type, ctx.GetPlace());
}
};
class PriorBoxOpMaker : public framework::OpProtoAndCheckerMaker {
public:
void Make() override {
AddInput("Input",
"(Tensor, default Tensor<float>), "
"the input feature data of PriorBoxOp, The layout is NCHW.");
AddInput("Image",
"(Tensor, default Tensor<float>), "
"the input image data of PriorBoxOp, The layout is NCHW.");
AddOutput("Boxes",
"(Tensor, default Tensor<float>), the output prior boxes of "
"PriorBoxOp. The layout is [H, W, num_priors, 4]. "
"H is the height of input, W is the width of input, num_priors "
"is the box count of each position.");
AddOutput("Variances",
"(Tensor, default Tensor<float>), the expanded variances of "
"PriorBoxOp. The layout is [H, W, num_priors, 4]. "
"H is the height of input, W is the width of input, num_priors "
"is the box count of each position.");
AddAttr<std::vector<float>>("min_sizes",
"(vector<float>) List of min sizes "
"of generated prior boxes.")
.AddCustomChecker([](const std::vector<float>& min_sizes) {
PADDLE_ENFORCE_GT(
min_sizes.size(),
0,
platform::errors::InvalidArgument("Size of min_sizes must be "
"at least 1."));
for (size_t i = 0; i < min_sizes.size(); ++i) {
PADDLE_ENFORCE_GT(min_sizes[i],
0.0,
platform::errors::OutOfRange(
"min_sizes[%d] must be larger "
"than 0. But received: min_sizes[%d] is %f.",
i,
i,
min_sizes[i]));
}
});
AddAttr<std::vector<float>>(
"max_sizes",
"(vector<float>) List of max sizes of generated prior boxes.")
.SetDefault(std::vector<float>{});
AddAttr<std::vector<float>>(
"aspect_ratios",
"(vector<float>) List of aspect ratios of generated prior boxes.");
AddAttr<std::vector<float>>(
"variances",
"(vector<float>) List of variances to be encoded in prior boxes.")
.AddCustomChecker([](const std::vector<float>& variances) {
PADDLE_ENFORCE_EQ(variances.size(),
4,
platform::errors::InvalidArgument(
"The length of variance must "
"be 4. But received: variances' length is %d.",
variances.size()));
for (size_t i = 0; i < variances.size(); ++i) {
PADDLE_ENFORCE_GT(variances[i],
0.0,
platform::errors::OutOfRange(
"variance[%d] must be greater "
"than 0. But received: variance[%d] = %f",
i,
i,
variances[i]));
}
});
AddAttr<bool>("flip", "(bool) Whether to flip aspect ratios.")
.SetDefault(true);
AddAttr<bool>("clip", "(bool) Whether to clip out-of-boundary boxes.")
.SetDefault(true);
AddAttr<float>("step_w",
"Prior boxes step across width, 0.0 for auto calculation.")
.SetDefault(0.0)
.AddCustomChecker([](const float& step_w) {
PADDLE_ENFORCE_GE(step_w,
0.0,
platform::errors::InvalidArgument(
"step_w should be larger "
"than 0. But received: step_w = %f.",
step_w));
});
AddAttr<float>("step_h",
"Prior boxes step across height, 0.0 for auto calculation.")
.SetDefault(0.0)
.AddCustomChecker([](const float& step_h) {
PADDLE_ENFORCE_GE(step_h,
0.0,
platform::errors::InvalidArgument(
"step_h should be larger "
"than 0. But received: step_h = %f.",
step_h));
});
AddAttr<float>("offset",
"(float) "
"Prior boxes center offset.")
.SetDefault(0.5);
AddAttr<bool>(
"min_max_aspect_ratios_order",
"(bool) If set True, the output prior box is in order of"
"[min, max, aspect_ratios], which is consistent with Caffe."
"Please note, this order affects the weights order of convolution layer"
"followed by and does not affect the final detection results.")
.SetDefault(false);
AddAttr<bool>("use_mkldnn",
"(bool, default false) Only used in mkldnn kernel")
.SetDefault(false);
AddAttr<bool>(
"use_quantizer",
"(bool, default false) "
"This parameter is no longer used. Use 'mkldnn_data_type' instead.")
.SetDefault(false);
AddAttr<std::string>(
"mkldnn_data_type",
"(string, default \"float32\"). Data type of mkldnn kernel")
.SetDefault("float32")
.InEnum({"float32", "int8", "bfloat16"});
AddComment(R"DOC(
Prior box operator
Generate prior boxes for SSD(Single Shot MultiBox Detector) algorithm.
Each position of the input produce N prior boxes, N is determined by
the count of min_sizes, max_sizes and aspect_ratios, The size of the
box is in range(min_size, max_size) interval, which is generated in
sequence according to the aspect_ratios.
Please get more information from the following papers:
https://arxiv.org/abs/1512.02325.
)DOC");
}
};
} // namespace operators
} // namespace paddle
DECLARE_INFER_SHAPE_FUNCTOR(prior_box,
PriorBoxInferShapeFunctor,
PD_INFER_META(phi::PriorBoxInferMeta));
namespace ops = paddle::operators;
REGISTER_OPERATOR(
prior_box,
ops::PriorBoxOp,
ops::PriorBoxOpMaker,
paddle::framework::EmptyGradOpMaker<paddle::framework::OpDesc>,
paddle::framework::EmptyGradOpMaker<paddle::imperative::OpBase>,
PriorBoxInferShapeFunctor);
REGISTER_OP_KERNEL(prior_box,
MKLDNN,
::paddle::platform::CPUPlace,
ops::PriorBoxOpKernel<float>,
ops::PriorBoxOpKernel<double>,
ops::PriorBoxOpKernel<uint8_t>,
ops::PriorBoxOpKernel<int8_t>);
...@@ -727,14 +727,6 @@ ...@@ -727,14 +727,6 @@
param : [x, kernel_size, strides, paddings, ceil_mode, exclusive, data_format, pooling_type, global_pooling, adaptive, padding_algorithm] param : [x, kernel_size, strides, paddings, ceil_mode, exclusive, data_format, pooling_type, global_pooling, adaptive, padding_algorithm]
backward : pool3d_grad backward : pool3d_grad
- op : prior_box
args : (Tensor input, Tensor image, float[] min_sizes, float[] max_sizes = {}, float[] aspect_ratios = {}, float[] variances = {}, bool flip=true, bool clip=true, float step_w=0.0, float step_h=0.0, float offset=0.5, bool min_max_aspect_ratios_order=false)
output : Tensor(out), Tensor(var)
infer_meta :
func : PriorBoxInferMeta
kernel :
func : prior_box
- op : prod - op : prod
args : (Tensor x, IntArray dims, bool keep_dim, bool reduce_all) args : (Tensor x, IntArray dims, bool keep_dim, bool reduce_all)
output : Tensor output : Tensor
......
...@@ -1984,6 +1984,14 @@ ...@@ -1984,6 +1984,14 @@
extra : extra :
attrs : [bool use_mkldnn = false, str mkldnn_data_type = "float32", bool is_test = false] attrs : [bool use_mkldnn = false, str mkldnn_data_type = "float32", bool is_test = false]
- op : prior_box
inputs :
{input: Input, image: Image}
outputs :
{out: Boxes, var: Variances}
extra :
attrs : [bool use_mkldnn = false, bool use_quantizer = false, str mkldnn_data_type = "float32"]
- op : prod (reduce_prod) - op : prod (reduce_prod)
backward : prod_grad (reduce_prod_grad) backward : prod_grad (reduce_prod_grad)
inputs: inputs:
......
...@@ -1803,6 +1803,15 @@ ...@@ -1803,6 +1803,15 @@
data_type : x data_type : x
backward : prelu_grad backward : prelu_grad
- op : prior_box
args : (Tensor input, Tensor image, float[] min_sizes, float[] max_sizes = {}, float[] aspect_ratios = {}, float[] variances = {}, bool flip=true, bool clip=true, float step_w=0.0, float step_h=0.0, float offset=0.5, bool min_max_aspect_ratios_order=false)
output : Tensor(out), Tensor(var)
infer_meta :
func : PriorBoxInferMeta
kernel :
func : prior_box
data_type : input
- op : put_along_axis - op : put_along_axis
args : (Tensor arr, Tensor indices, Tensor values, int axis, str reduce = "assign") args : (Tensor arr, Tensor indices, Tensor values, int axis, str reduce = "assign")
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"
namespace phi {
KernelSignature PriorBoxOpArgumentMapping(
const ArgumentMappingContext& ctx UNUSED) {
return KernelSignature("prior_box",
{"Input", "Image"},
{"min_sizes",
"max_sizes",
"aspect_ratios",
"variances",
"flip",
"clip",
"step_w",
"step_h",
"offset",
"min_max_aspect_ratios_order"},
{"Boxes", "Variances"});
}
} // namespace phi
PD_REGISTER_ARG_MAPPING_FN(prior_box, phi::PriorBoxOpArgumentMapping);
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册