prior_box_op.cc 6.6 KB
Newer Older
W
wanghaox 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve.

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/operators/prior_box_op.h"

namespace paddle {
namespace operators {

class PriorBoxOp : public framework::OperatorWithKernel {
 public:
  using framework::OperatorWithKernel::OperatorWithKernel;

  void InferShape(framework::InferShapeContext* ctx) const override {
    PADDLE_ENFORCE(ctx->HasInput("Input"),
W
wanghaox 已提交
26
                   "Input(Input) of PriorBoxOp should not be null.");
W
wanghaox 已提交
27
    PADDLE_ENFORCE(ctx->HasInput("Image"),
W
wanghaox 已提交
28
                   "Input(Image) of PriorBoxOp should not be null.");
W
wanghaox 已提交
29 30 31

    auto image_dims = ctx->GetInputDim("Image");
    auto input_dims = ctx->GetInputDim("Input");
W
wanghaox 已提交
32 33
    PADDLE_ENFORCE(image_dims.size() == 4, "The layout of image is NCHW.");
    PADDLE_ENFORCE(input_dims.size() == 4, "The layout of input is NCHW.");
W
wanghaox 已提交
34 35 36 37 38 39

    PADDLE_ENFORCE_LT(input_dims[2], image_dims[2],
                      "The height of input must smaller than image.");

    PADDLE_ENFORCE_LT(input_dims[3], image_dims[3],
                      "The width of input must smaller than image.");
W
wanghaox 已提交
40 41 42 43

    auto min_sizes = ctx->Attrs().Get<std::vector<int>>("min_sizes");
    auto max_sizes = ctx->Attrs().Get<std::vector<int>>("max_sizes");
    auto variances = ctx->Attrs().Get<std::vector<float>>("variances");
W
wanghaox 已提交
44
    auto aspect_ratios = ctx->Attrs().Get<std::vector<float>>("aspect_ratios");
W
wanghaox 已提交
45 46
    bool flip = ctx->Attrs().Get<bool>("flip");

W
wanghaox 已提交
47
    PADDLE_ENFORCE_GT(min_sizes.size(), 0,
W
wanghaox 已提交
48
                      "Size of min_sizes must be at least 1.");
W
wanghaox 已提交
49 50 51 52
    for (size_t i = 0; i < min_sizes.size(); ++i) {
      PADDLE_ENFORCE_GT(min_sizes[i], 0, "min_sizes[%d] must be positive.", i);
    }

W
wanghaox 已提交
53 54
    std::vector<float> aspect_ratios_vec;
    ExpandAspectRatios(aspect_ratios, flip, aspect_ratios_vec);
W
wanghaox 已提交
55

W
wanghaox 已提交
56
    int num_priors = aspect_ratios_vec.size() * min_sizes.size();
W
wanghaox 已提交
57 58
    if (max_sizes.size() > 0) {
      PADDLE_ENFORCE_EQ(max_sizes.size(), min_sizes.size(),
W
wanghaox 已提交
59
                        "The number of min_size and max_size must be equal.");
W
wanghaox 已提交
60 61 62 63 64 65 66 67
      for (size_t i = 0; i < min_sizes.size(); ++i) {
        PADDLE_ENFORCE_GT(max_sizes[i], min_sizes[i],
                          "max_size[%d] must be greater than min_size[%d].", i,
                          i);
        num_priors += 1;
      }
    }

W
wanghaox 已提交
68 69 70 71
    PADDLE_ENFORCE_EQ(variances.size(), 4, "Must and only provide 4 variance.");
    for (size_t i = 0; i < variances.size(); ++i) {
      PADDLE_ENFORCE_GT(variances[i], 0.0,
                        "variance[%d] must be greater than 0.", i);
W
wanghaox 已提交
72 73 74 75 76 77 78
    }

    const float step_h = ctx->Attrs().Get<float>("step_h");
    PADDLE_ENFORCE_GT(step_h, 0.0, "step_h should be larger than 0.");
    const float step_w = ctx->Attrs().Get<float>("step_w");
    PADDLE_ENFORCE_GT(step_w, 0.0, "step_w should be larger than 0.");

W
wanghaox 已提交
79 80 81 82 83 84 85
    std::vector<int64_t> dim_vec(4);
    dim_vec[0] = input_dims[2];
    dim_vec[1] = input_dims[3];
    dim_vec[2] = num_priors;
    dim_vec[3] = 4;
    ctx->SetOutputDim("Boxes", framework::make_ddim(dim_vec));
    ctx->SetOutputDim("Variances", framework::make_ddim(dim_vec));
W
wanghaox 已提交
86 87 88 89 90
  }
};

class PriorBoxOpMaker : public framework::OpProtoAndCheckerMaker {
 public:
W
wanghaox 已提交
91
  PriorBoxOpMaker(OpProto* proto, OpAttrChecker* op_checker)
W
wanghaox 已提交
92 93
      : OpProtoAndCheckerMaker(proto, op_checker) {
    AddInput("Input",
W
wanghaox 已提交
94
             "(Tensor, default Tensor<float>), "
W
wanghaox 已提交
95
             "the input feature data of PriorBoxOp, The layout is NCHW.");
W
wanghaox 已提交
96
    AddInput("Image",
W
wanghaox 已提交
97
             "(Tensor, default Tensor<float>), "
W
wanghaox 已提交
98
             "the input image data of PriorBoxOp, The layout is NCHW.");
W
wanghaox 已提交
99
    AddOutput("Boxes",
W
wanghaox 已提交
100
              "(Tensor, default Tensor<float>), the output prior boxes of "
W
wanghaox 已提交
101
              "PriorBoxOp. The layout is [layer_height, layer_width, "
W
wanghaox 已提交
102
              "num_priors, 4]. layer_height is the height of input, "
103
              "layer_width is the width of input, num_priors is the box "
W
wanghaox 已提交
104 105 106
              "count of each position.");
    AddOutput("Variances",
              "(Tensor, default Tensor<float>), the expanded variances of "
W
wanghaox 已提交
107
              "PriorBoxOp. The layout is [layer_height, layer_width, "
W
wanghaox 已提交
108
              "num_priors, 4]. layer_height is the height of input, "
109
              "layer_width is the width of input, num_priors is the box "
W
wanghaox 已提交
110
              "count of each position.");
W
wanghaox 已提交
111 112 113
    AddAttr<std::vector<int>>("min_sizes", "(vector<int>) ",
                              "List of min sizes of generated prior boxes.");
    AddAttr<std::vector<int>>("max_sizes", "(vector<int>) ",
W
wanghaox 已提交
114
                              "List of max sizes of generated prior boxes.");
W
wanghaox 已提交
115 116
    AddAttr<std::vector<float>>(
        "aspect_ratios", "(vector<float>) ",
W
wanghaox 已提交
117
        "List of aspect ratios of generated prior boxes.");
W
wanghaox 已提交
118 119
    AddAttr<std::vector<float>>(
        "variances", "(vector<float>) ",
W
wanghaox 已提交
120
        "List of variances to be encoded in prior boxes.");
W
wanghaox 已提交
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
    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 for auto calculation.")
        .SetDefault(0.0);
    AddAttr<float>("step_h",
                   "Prior boxes step across height, 0 for auto calculation.")
        .SetDefault(0.0);
    AddAttr<float>("offset",
                   "(float) "
                   "Prior boxes center offset.")
        .SetDefault(0.5);
    AddComment(R"DOC(
Prior box operator
Generate prior boxes for SSD(Single Shot MultiBox Detector) algorithm.
W
wanghaox 已提交
138 139 140 141 142
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.

W
wanghaox 已提交
143 144 145 146 147 148 149 150 151 152 153 154 155 156
Please get more information from the following papers:
https://arxiv.org/abs/1512.02325.
)DOC");
  }
};

}  // namespace operators
}  // namespace paddle

namespace ops = paddle::operators;
REGISTER_OP_WITHOUT_GRADIENT(prior_box, ops::PriorBoxOp, ops::PriorBoxOpMaker);
REGISTER_OP_CPU_KERNEL(
    prior_box, ops::PriorBoxOpKernel<paddle::platform::CPUPlace, float>,
    ops::PriorBoxOpKernel<paddle::platform::CPUPlace, double>);