|
| 1 | +/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved. |
| 2 | +
|
| 3 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +you may not use this file except in compliance with the License. |
| 5 | +You may obtain a copy of the License at |
| 6 | +
|
| 7 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +
|
| 9 | +Unless required by applicable law or agreed to in writing, software |
| 10 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +See the License for the specific language governing permissions and |
| 13 | +limitations under the License. */ |
| 14 | + |
| 15 | +#include "paddle/fluid/operators/detection/anchor_generator_op.h" |
| 16 | + |
| 17 | +namespace paddle { |
| 18 | +namespace operators { |
| 19 | + |
| 20 | +class AnchorGeneratorOp : public framework::OperatorWithKernel { |
| 21 | + public: |
| 22 | + using framework::OperatorWithKernel::OperatorWithKernel; |
| 23 | + |
| 24 | + void InferShape(framework::InferShapeContext* ctx) const override { |
| 25 | + PADDLE_ENFORCE(ctx->HasInput("Input"), |
| 26 | + "Input(Input) of AnchorGeneratorOp should not be null."); |
| 27 | + PADDLE_ENFORCE(ctx->HasOutput("Anchors"), |
| 28 | + "Output(Anchors) of AnchorGeneratorOp should not be null."); |
| 29 | + PADDLE_ENFORCE( |
| 30 | + ctx->HasOutput("Variances"), |
| 31 | + "Output(Variances) of AnchorGeneratorOp should not be null."); |
| 32 | + |
| 33 | + auto input_dims = ctx->GetInputDim("Input"); |
| 34 | + PADDLE_ENFORCE(input_dims.size() == 4, "The layout of input is NCHW."); |
| 35 | + |
| 36 | + auto anchor_sizes = ctx->Attrs().Get<std::vector<float>>("anchor_sizes"); |
| 37 | + auto aspect_ratios = ctx->Attrs().Get<std::vector<float>>("aspect_ratios"); |
| 38 | + auto stride = ctx->Attrs().Get<std::vector<float>>("stride"); |
| 39 | + auto variances = ctx->Attrs().Get<std::vector<float>>("variances"); |
| 40 | + |
| 41 | + size_t num_anchors = aspect_ratios.size() * anchor_sizes.size(); |
| 42 | + |
| 43 | + std::vector<int64_t> dim_vec(4); |
| 44 | + dim_vec[0] = input_dims[2]; |
| 45 | + dim_vec[1] = input_dims[3]; |
| 46 | + dim_vec[2] = num_anchors; |
| 47 | + dim_vec[3] = 4; |
| 48 | + ctx->SetOutputDim("Anchors", framework::make_ddim(dim_vec)); |
| 49 | + ctx->SetOutputDim("Variances", framework::make_ddim(dim_vec)); |
| 50 | + } |
| 51 | + |
| 52 | + protected: |
| 53 | + framework::OpKernelType GetExpectedKernelType( |
| 54 | + const framework::ExecutionContext& ctx) const override { |
| 55 | + return framework::OpKernelType( |
| 56 | + framework::ToDataType(ctx.Input<framework::Tensor>("Input")->type()), |
| 57 | + ctx.device_context()); |
| 58 | + } |
| 59 | +}; |
| 60 | + |
| 61 | +class AnchorGeneratorOpMaker : public framework::OpProtoAndCheckerMaker { |
| 62 | + public: |
| 63 | + void Make() override { |
| 64 | + AddInput("Input", |
| 65 | + "(Tensor, default Tensor<float>), " |
| 66 | + "the input feature is a tensor with a rank of 4. " |
| 67 | + "The layout is NCHW."); |
| 68 | + AddOutput("Anchors", |
| 69 | + "(Tensor, default Tensor<float>), the output is a " |
| 70 | + "tensor with a rank of 4. The layout is [H, W, num_anchors, 4]. " |
| 71 | + "H is the height of input, W is the width of input, num_anchors " |
| 72 | + "is the box count of each position. " |
| 73 | + "Each anchor is in (xmin, ymin, xmax, ymax) format"); |
| 74 | + AddOutput("Variances", |
| 75 | + "(Tensor, default Tensor<float>), the expanded variances for " |
| 76 | + "normalizing bbox regression targets. The layout is [H, W, " |
| 77 | + "num_anchors, 4]. " |
| 78 | + "H is the height of input, W is the width of input, num_anchors " |
| 79 | + "is the box count of each position. " |
| 80 | + "Each variance is in (xcenter, ycenter, w, h) format"); |
| 81 | + |
| 82 | + AddAttr<std::vector<float>>( |
| 83 | + "anchor_sizes", |
| 84 | + "(vector<float>) List of Region Proposal Network(RPN) anchor sizes " |
| 85 | + " given in absolute pixels e.g. (64, 128, 256, 512)." |
| 86 | + " For instance, the anchor size of 64 means the area of this anchor " |
| 87 | + "equals to 64**2.") |
| 88 | + .AddCustomChecker([](const std::vector<float>& anchor_sizes) { |
| 89 | + PADDLE_ENFORCE_GT(anchor_sizes.size(), 0, |
| 90 | + "Size of anchor_sizes must be at least 1."); |
| 91 | + for (size_t i = 0; i < anchor_sizes.size(); ++i) { |
| 92 | + PADDLE_ENFORCE_GT(anchor_sizes[i], 0.0, |
| 93 | + "anchor_sizes[%d] must be positive.", i); |
| 94 | + } |
| 95 | + }); |
| 96 | + AddAttr<std::vector<float>>( |
| 97 | + "aspect_ratios", |
| 98 | + "(vector<float>) List of Region Proposal Network(RPN) anchor aspect " |
| 99 | + "ratios, e.g. (0.5, 1, 2)." |
| 100 | + "For instacne, the aspect ratio of 0.5 means the height / width of " |
| 101 | + "this anchor equals 0.5."); |
| 102 | + |
| 103 | + AddAttr<std::vector<float>>("variances", |
| 104 | + "(vector<float>) List of variances to be used " |
| 105 | + "in box regression deltas") |
| 106 | + .AddCustomChecker([](const std::vector<float>& variances) { |
| 107 | + PADDLE_ENFORCE_EQ(variances.size(), 4, |
| 108 | + "Must and only provide 4 variance."); |
| 109 | + for (size_t i = 0; i < variances.size(); ++i) { |
| 110 | + PADDLE_ENFORCE_GT(variances[i], 0.0, |
| 111 | + "variance[%d] must be greater than 0.", i); |
| 112 | + } |
| 113 | + }); |
| 114 | + |
| 115 | + AddAttr<std::vector<float>>("stride", |
| 116 | + "Anchors stride across width and height, " |
| 117 | + "with a default of (16, 16)") |
| 118 | + .SetDefault(std::vector<float>(2, 16.0)) |
| 119 | + .AddCustomChecker([](const std::vector<float>& stride) { |
| 120 | + PADDLE_ENFORCE_EQ( |
| 121 | + stride.size(), 2, |
| 122 | + "Must and only provide 2 stride for width and height."); |
| 123 | + for (size_t i = 0; i < stride.size(); ++i) { |
| 124 | + PADDLE_ENFORCE_GT(stride[i], 0.0, |
| 125 | + "stride[%d] should be larger than 0.", i); |
| 126 | + } |
| 127 | + }); |
| 128 | + |
| 129 | + AddAttr<float>("offset", |
| 130 | + "(float) " |
| 131 | + "Anchor center offset, with a default of 0.5") |
| 132 | + .SetDefault(0.5); |
| 133 | + AddComment(R"DOC( |
| 134 | +AnchorGenerator operator |
| 135 | +Generates anchors for Faster RCNN, FPN etc. algorithm. |
| 136 | +Each position of the input produce N anchors, N = |
| 137 | + size(anchor_sizes) * size(aspect_ratios). |
| 138 | +
|
| 139 | +Please get more information from the following papers: |
| 140 | +https://arxiv.org/abs/1506.01497. |
| 141 | +)DOC"); |
| 142 | + } |
| 143 | +}; |
| 144 | + |
| 145 | +} // namespace operators |
| 146 | +} // namespace paddle |
| 147 | + |
| 148 | +namespace ops = paddle::operators; |
| 149 | +REGISTER_OPERATOR(anchor_generator, ops::AnchorGeneratorOp, |
| 150 | + ops::AnchorGeneratorOpMaker, |
| 151 | + paddle::framework::EmptyGradOpMaker); |
| 152 | + |
| 153 | +REGISTER_OP_CPU_KERNEL(anchor_generator, ops::AnchorGeneratorOpKernel<float>, |
| 154 | + ops::AnchorGeneratorOpKernel<double>); |
0 commit comments