prior_box_op.cc 6.7 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 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
/* 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"),
                   "Input(X) of SequenceSliceOp should not be null.");
    PADDLE_ENFORCE(ctx->HasInput("Image"),
                   "Input(Offset) of SequenceSliceOp should not be null.");

    auto image_dims = ctx->GetInputDim("Image");
    auto input_dims = ctx->GetInputDim("Input");
    PADDLE_ENFORCE(image_dims.size() == 4,
                   "The format of input tensor is NCHW.");

    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");
    auto input_aspect_ratio =
        ctx->Attrs().Get<std::vector<float>>("aspect_ratios");
    bool flip = ctx->Attrs().Get<bool>("flip");

    PADDLE_ENFORCE_GT(min_sizes.size(), 0, "must provide min_size.");
    for (size_t i = 0; i < min_sizes.size(); ++i) {
      PADDLE_ENFORCE_GT(min_sizes[i], 0, "min_sizes[%d] must be positive.", i);
    }

    std::vector<float> aspect_ratios;
    expand_aspect_ratios(input_aspect_ratio, flip, aspect_ratios);

    int num_priors = aspect_ratios.size() * min_sizes.size();
    if (max_sizes.size() > 0) {
      PADDLE_ENFORCE_EQ(max_sizes.size(), min_sizes.size(),
                        "The length of min_size and max_size must be equal.");
      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;
      }
    }

    if (variances.size() > 1) {
      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);
      }
    } else if (variances.size() == 1) {
      PADDLE_ENFORCE_GT(variances[0], 0.0,
                        "variance[0] must be greater than 0.");
    }

    const int img_h = ctx->Attrs().Get<int>("img_h");
    PADDLE_ENFORCE_GT(img_h, 0, "img_h should be larger than 0.");
    const int img_w = ctx->Attrs().Get<int>("img_w");
    PADDLE_ENFORCE_GT(img_w, 0, "img_w should be larger than 0.");

    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.");

    const int layer_height = input_dims[3];
    const int layer_width = input_dims[2];

    std::vector<int64_t> dim_vec(3);
    // Since all images in a batch has same height and width, we only need to
    // generate one set of priors which can be shared across all images.
    dim_vec[0] = 1;
    // 2 channels. First channel stores the mean of each prior coordinate.
    // Second channel stores the variance of each prior coordinate.
    dim_vec[1] = 2;
    dim_vec[2] = layer_width * layer_height * num_priors * 4;
    PADDLE_ENFORCE_GT(dim_vec[2], 0,
                      "output_dim[2] must larger than 0."
                      "check your data dims");
    auto output_dim = framework::make_ddim(dim_vec);
    ctx->SetOutputDim("Out", output_dim);
  }

 protected:
  framework::OpKernelType GetKernelType(
      const framework::ExecutionContext& ctx) const override {
    return framework::OpKernelType(
        framework::ToDataType(ctx.Input<framework::LoDTensor>("Image")->type()),
        ctx.device_context());
  }
};

class PriorBoxOpMaker : public framework::OpProtoAndCheckerMaker {
 public:
  PriorBoxOpMaker(framework::OpProto* proto,
                  framework::OpAttrChecker* op_checker)
      : OpProtoAndCheckerMaker(proto, op_checker) {
    AddInput("Input",
             "(Tensor), "
             "the input feature data of PriorBoxOp.");
    AddInput("Image",
             "(Tensor), "
             "the input image data of PriorBoxOp.");
    AddOutput("Out", "(Tensor), the output prior boxes of PriorBoxOp.");
    AddAttr<std::vector<int>>("min_sizes", "(vector<int>) ",
                              "List of min sizes of generated prior boxes.");
    AddAttr<std::vector<int>>("max_sizes", "(vector<int>) ",
                              "List of max sizes of generated prior boxes.");
    AddAttr<std::vector<float>>(
        "aspect_ratios", "(vector<float>) ",
        "List of aspect ratios of generated prior boxes.")
        .SetDefault({});
    AddAttr<std::vector<float>>(
        "variances", "(vector<float>) ",
        "List of variances to be encoded in prior boxes.")
        .SetDefault({0.1});
    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<int>("img_w", "").SetDefault(0);
    AddAttr<int>("img_h", "").SetDefault(0);
    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.
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>);