|
| 1 | +/*Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved. |
| 2 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | +you may not use this file except in compliance with the License. |
| 4 | +You may obtain a copy of the License at |
| 5 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 6 | +Unless required by applicable law or agreed to in writing, software |
| 7 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 8 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 9 | +See the License for the specific language governing permissions and |
| 10 | +limitations under the License. */ |
| 11 | + |
| 12 | +#include "paddle/fluid/operators/detection/density_prior_box_op.h" |
| 13 | + |
| 14 | +namespace paddle { |
| 15 | +namespace operators { |
| 16 | + |
| 17 | +class DensityPriorBoxOp : public framework::OperatorWithKernel { |
| 18 | + public: |
| 19 | + using framework::OperatorWithKernel::OperatorWithKernel; |
| 20 | + |
| 21 | + void InferShape(framework::InferShapeContext* ctx) const override { |
| 22 | + PADDLE_ENFORCE(ctx->HasInput("Input"), |
| 23 | + "Input(Input) of DensityPriorBoxOp should not be null."); |
| 24 | + PADDLE_ENFORCE(ctx->HasInput("Image"), |
| 25 | + "Input(Image) of DensityPriorBoxOp should not be null."); |
| 26 | + |
| 27 | + auto image_dims = ctx->GetInputDim("Image"); |
| 28 | + auto input_dims = ctx->GetInputDim("Input"); |
| 29 | + PADDLE_ENFORCE(image_dims.size() == 4, "The layout of image is NCHW."); |
| 30 | + PADDLE_ENFORCE(input_dims.size() == 4, "The layout of input is NCHW."); |
| 31 | + |
| 32 | + PADDLE_ENFORCE_LT(input_dims[2], image_dims[2], |
| 33 | + "The height of input must smaller than image."); |
| 34 | + |
| 35 | + PADDLE_ENFORCE_LT(input_dims[3], image_dims[3], |
| 36 | + "The width of input must smaller than image."); |
| 37 | + auto variances = ctx->Attrs().Get<std::vector<float>>("variances"); |
| 38 | + |
| 39 | + auto fixed_sizes = ctx->Attrs().Get<std::vector<float>>("fixed_sizes"); |
| 40 | + auto fixed_ratios = ctx->Attrs().Get<std::vector<float>>("fixed_ratios"); |
| 41 | + auto densities = ctx->Attrs().Get<std::vector<int>>("densities"); |
| 42 | + |
| 43 | + PADDLE_ENFORCE_EQ(fixed_sizes.size(), densities.size(), |
| 44 | + "The number of fixed_sizes and densities must be equal."); |
| 45 | + size_t num_priors = 0; |
| 46 | + if ((fixed_sizes.size() > 0) && (densities.size() > 0)) { |
| 47 | + for (size_t i = 0; i < densities.size(); ++i) { |
| 48 | + if (fixed_ratios.size() > 0) { |
| 49 | + num_priors += (fixed_ratios.size()) * (pow(densities[i], 2)); |
| 50 | + } |
| 51 | + } |
| 52 | + } |
| 53 | + std::vector<int64_t> dim_vec(4); |
| 54 | + dim_vec[0] = input_dims[2]; |
| 55 | + dim_vec[1] = input_dims[3]; |
| 56 | + dim_vec[2] = num_priors; |
| 57 | + dim_vec[3] = 4; |
| 58 | + ctx->SetOutputDim("Boxes", framework::make_ddim(dim_vec)); |
| 59 | + ctx->SetOutputDim("Variances", framework::make_ddim(dim_vec)); |
| 60 | + } |
| 61 | + |
| 62 | + protected: |
| 63 | + framework::OpKernelType GetExpectedKernelType( |
| 64 | + const framework::ExecutionContext& ctx) const override { |
| 65 | + return framework::OpKernelType( |
| 66 | + framework::ToDataType(ctx.Input<framework::Tensor>("Input")->type()), |
| 67 | + platform::CPUPlace()); |
| 68 | + } |
| 69 | +}; |
| 70 | + |
| 71 | +class DensityPriorBoxOpMaker : public framework::OpProtoAndCheckerMaker { |
| 72 | + public: |
| 73 | + void Make() override { |
| 74 | + AddInput( |
| 75 | + "Input", |
| 76 | + "(Tensor, default Tensor<float>), " |
| 77 | + "the input feature data of DensityPriorBoxOp, the layout is NCHW."); |
| 78 | + AddInput("Image", |
| 79 | + "(Tensor, default Tensor<float>), " |
| 80 | + "the input image data of DensityPriorBoxOp, the layout is NCHW."); |
| 81 | + AddOutput("Boxes", |
| 82 | + "(Tensor, default Tensor<float>), the output prior boxes of " |
| 83 | + "DensityPriorBoxOp. The layout is [H, W, num_priors, 4]. " |
| 84 | + "H is the height of input, W is the width of input, num_priors " |
| 85 | + "is the box count of each position."); |
| 86 | + AddOutput("Variances", |
| 87 | + "(Tensor, default Tensor<float>), the expanded variances of " |
| 88 | + "DensityPriorBoxOp. The layout is [H, W, num_priors, 4]. " |
| 89 | + "H is the height of input, W is the width of input, num_priors " |
| 90 | + "is the box count of each position."); |
| 91 | + AddAttr<std::vector<float>>("variances", |
| 92 | + "(vector<float>) List of variances to be " |
| 93 | + "encoded in density prior boxes.") |
| 94 | + .AddCustomChecker([](const std::vector<float>& variances) { |
| 95 | + PADDLE_ENFORCE_EQ(variances.size(), 4, |
| 96 | + "Must and only provide 4 variance."); |
| 97 | + for (size_t i = 0; i < variances.size(); ++i) { |
| 98 | + PADDLE_ENFORCE_GT(variances[i], 0.0, |
| 99 | + "variance[%d] must be greater than 0.", i); |
| 100 | + } |
| 101 | + }); |
| 102 | + AddAttr<bool>("clip", "(bool) Whether to clip out-of-boundary boxes.") |
| 103 | + .SetDefault(true); |
| 104 | + |
| 105 | + AddAttr<float>( |
| 106 | + "step_w", |
| 107 | + "Density prior boxes step across width, 0.0 for auto calculation.") |
| 108 | + .SetDefault(0.0) |
| 109 | + .AddCustomChecker([](const float& step_w) { |
| 110 | + PADDLE_ENFORCE_GE(step_w, 0.0, "step_w should be larger than 0."); |
| 111 | + }); |
| 112 | + AddAttr<float>( |
| 113 | + "step_h", |
| 114 | + "Density prior boxes step across height, 0.0 for auto calculation.") |
| 115 | + .SetDefault(0.0) |
| 116 | + .AddCustomChecker([](const float& step_h) { |
| 117 | + PADDLE_ENFORCE_GE(step_h, 0.0, "step_h should be larger than 0."); |
| 118 | + }); |
| 119 | + |
| 120 | + AddAttr<float>("offset", |
| 121 | + "(float) " |
| 122 | + "Density prior boxes center offset.") |
| 123 | + .SetDefault(0.5); |
| 124 | + AddAttr<std::vector<float>>("fixed_sizes", |
| 125 | + "(vector<float>) List of fixed sizes " |
| 126 | + "of generated density prior boxes.") |
| 127 | + .SetDefault(std::vector<float>{}) |
| 128 | + .AddCustomChecker([](const std::vector<float>& fixed_sizes) { |
| 129 | + for (size_t i = 0; i < fixed_sizes.size(); ++i) { |
| 130 | + PADDLE_ENFORCE_GT(fixed_sizes[i], 0.0, |
| 131 | + "fixed_sizes[%d] should be larger than 0.", i); |
| 132 | + } |
| 133 | + }); |
| 134 | + |
| 135 | + AddAttr<std::vector<float>>("fixed_ratios", |
| 136 | + "(vector<float>) List of fixed ratios " |
| 137 | + "of generated density prior boxes.") |
| 138 | + .SetDefault(std::vector<float>{}) |
| 139 | + .AddCustomChecker([](const std::vector<float>& fixed_ratios) { |
| 140 | + for (size_t i = 0; i < fixed_ratios.size(); ++i) { |
| 141 | + PADDLE_ENFORCE_GT(fixed_ratios[i], 0.0, |
| 142 | + "fixed_ratios[%d] should be larger than 0.", i); |
| 143 | + } |
| 144 | + }); |
| 145 | + |
| 146 | + AddAttr<std::vector<int>>("densities", |
| 147 | + "(vector<float>) List of densities " |
| 148 | + "of generated density prior boxes.") |
| 149 | + .SetDefault(std::vector<int>{}) |
| 150 | + .AddCustomChecker([](const std::vector<int>& densities) { |
| 151 | + for (size_t i = 0; i < densities.size(); ++i) { |
| 152 | + PADDLE_ENFORCE_GT(densities[i], 0, |
| 153 | + "densities[%d] should be larger than 0.", i); |
| 154 | + } |
| 155 | + }); |
| 156 | + AddComment(R"DOC( |
| 157 | + Density Prior box operator |
| 158 | + Each position of the input produce N density prior boxes, N is determined by |
| 159 | + the count of fixed_ratios, densities, the calculation of N is as follows: |
| 160 | + for density in densities: |
| 161 | + N += size(fixed_ratios)*density^2 |
| 162 | + )DOC"); |
| 163 | + } |
| 164 | +}; |
| 165 | + |
| 166 | +} // namespace operators |
| 167 | +} // namespace paddle |
| 168 | + |
| 169 | +namespace ops = paddle::operators; |
| 170 | +REGISTER_OPERATOR(density_prior_box, ops::DensityPriorBoxOp, |
| 171 | + ops::DensityPriorBoxOpMaker, |
| 172 | + paddle::framework::EmptyGradOpMaker); |
| 173 | + |
| 174 | +REGISTER_OP_CPU_KERNEL(density_prior_box, ops::DensityPriorBoxOpKernel<float>, |
| 175 | + ops::DensityPriorBoxOpKernel<double>); |
0 commit comments