prior_box_op.h 6.4 KB
Newer Older
W
wanghaox 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/* 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. */

#pragma once
#include "paddle/framework/op_registry.h"
#include "paddle/operators/math/math_function.h"
W
wanghaox 已提交
18
#include "paddle/platform/transform.h"
W
wanghaox 已提交
19 20 21 22

namespace paddle {
namespace operators {

W
wanghaox 已提交
23 24 25
inline void ExpandAspectRatios(const std::vector<float>& input_aspect_ratior,
                               bool flip,
                               std::vector<float>& output_aspect_ratior) {
W
wanghaox 已提交
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
  constexpr float eps = 1e-6;
  output_aspect_ratior.clear();
  output_aspect_ratior.push_back(1.);
  for (size_t i = 0; i < input_aspect_ratior.size(); ++i) {
    float ar = input_aspect_ratior[i];
    bool already_exist = false;
    for (size_t j = 0; j < output_aspect_ratior.size(); ++j) {
      if (fabs(ar - output_aspect_ratior[j]) < eps) {
        already_exist = true;
        break;
      }
    }
    if (!already_exist) {
      output_aspect_ratior.push_back(ar);
      if (flip) {
        output_aspect_ratior.push_back(1. / ar);
      }
    }
  }
}

W
wanghaox 已提交
47 48 49 50 51 52 53
template <typename T>
struct ClipFunctor {
  HOSTDEVICE T operator()(T in) const {
    return std::min<T>(std::max<T>(in, 0.), 1.);
  }
};

W
wanghaox 已提交
54 55 56 57 58 59
template <typename Place, typename T>
class PriorBoxOpKernel : public framework::OpKernel<T> {
 public:
  void Compute(const framework::ExecutionContext& ctx) const override {
    auto* input = ctx.Input<paddle::framework::Tensor>("Input");
    auto* image = ctx.Input<paddle::framework::Tensor>("Image");
W
wanghaox 已提交
60 61
    auto* boxes = ctx.Output<paddle::framework::Tensor>("Boxes");
    auto* vars = ctx.Output<paddle::framework::Tensor>("Variances");
W
wanghaox 已提交
62 63 64 65 66 67 68 69 70

    auto min_sizes = ctx.Attr<std::vector<int>>("min_sizes");
    auto max_sizes = ctx.Attr<std::vector<int>>("max_sizes");
    auto input_aspect_ratio = ctx.Attr<std::vector<float>>("aspect_ratios");
    auto variances = ctx.Attr<std::vector<float>>("variances");
    auto flip = ctx.Attr<bool>("flip");
    auto clip = ctx.Attr<bool>("clip");

    std::vector<float> aspect_ratios;
W
wanghaox 已提交
71
    ExpandAspectRatios(input_aspect_ratio, flip, aspect_ratios);
W
wanghaox 已提交
72 73 74 75 76

    auto step_w = ctx.Attr<float>("step_w");
    auto step_h = ctx.Attr<float>("step_h");
    auto offset = ctx.Attr<float>("offset");

W
wanghaox 已提交
77 78
    auto img_width = image->dims()[3];
    auto img_height = image->dims()[2];
W
wanghaox 已提交
79

W
wanghaox 已提交
80 81
    auto layer_width = input->dims()[3];
    auto layer_height = input->dims()[2];
W
wanghaox 已提交
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96

    float step_width, step_height;
    if (step_w == 0 || step_h == 0) {
      step_width = static_cast<float>(img_width) / layer_width;
      step_height = static_cast<float>(img_height) / layer_height;
    } else {
      step_width = step_w;
      step_height = step_h;
    }

    int num_priors = aspect_ratios.size() * min_sizes.size();
    if (max_sizes.size() > 0) {
      num_priors += max_sizes.size();
    }

W
wanghaox 已提交
97 98
    boxes->mutable_data<T>(ctx.GetPlace());
    vars->mutable_data<T>(ctx.GetPlace());
W
wanghaox 已提交
99

W
wanghaox 已提交
100
    auto e_boxes = framework::EigenTensor<T, 4>::From(*boxes);
W
wanghaox 已提交
101 102 103 104 105
    for (int h = 0; h < layer_height; ++h) {
      for (int w = 0; w < layer_width; ++w) {
        float center_x = (w + offset) * step_width;
        float center_y = (h + offset) * step_height;
        float box_width, box_height;
106
        int idx = 0;
W
wanghaox 已提交
107 108 109 110 111
        for (size_t s = 0; s < min_sizes.size(); ++s) {
          int min_size = min_sizes[s];
          // first prior: aspect_ratio = 1, size = min_size
          box_width = box_height = min_size;
          // xmin
W
wanghaox 已提交
112
          e_boxes(h, w, idx, 0) = (center_x - box_width / 2.) / img_width;
W
wanghaox 已提交
113
          // ymin
W
wanghaox 已提交
114
          e_boxes(h, w, idx, 1) = (center_y - box_height / 2.) / img_height;
W
wanghaox 已提交
115
          // xmax
W
wanghaox 已提交
116
          e_boxes(h, w, idx, 2) = (center_x + box_width / 2.) / img_width;
W
wanghaox 已提交
117
          // ymax
W
wanghaox 已提交
118
          e_boxes(h, w, idx, 3) = (center_y + box_height / 2.) / img_height;
W
wanghaox 已提交
119

120
          idx++;
W
wanghaox 已提交
121 122 123 124 125 126
          if (max_sizes.size() > 0) {
            int max_size = max_sizes[s];
            // second prior: aspect_ratio = 1,
            // size = sqrt(min_size * max_size)
            box_width = box_height = sqrt(min_size * max_size);
            // xmin
W
wanghaox 已提交
127
            e_boxes(h, w, idx, 0) = (center_x - box_width / 2.) / img_width;
W
wanghaox 已提交
128
            // ymin
W
wanghaox 已提交
129
            e_boxes(h, w, idx, 1) = (center_y - box_height / 2.) / img_height;
W
wanghaox 已提交
130
            // xmax
W
wanghaox 已提交
131
            e_boxes(h, w, idx, 2) = (center_x + box_width / 2.) / img_width;
W
wanghaox 已提交
132
            // ymax
W
wanghaox 已提交
133
            e_boxes(h, w, idx, 3) = (center_y + box_height / 2.) / img_height;
134
            idx++;
W
wanghaox 已提交
135 136 137 138 139 140 141 142 143 144 145
          }

          // rest of priors
          for (size_t r = 0; r < aspect_ratios.size(); ++r) {
            float ar = aspect_ratios[r];
            if (fabs(ar - 1.) < 1e-6) {
              continue;
            }
            box_width = min_size * sqrt(ar);
            box_height = min_size / sqrt(ar);
            // xmin
W
wanghaox 已提交
146
            e_boxes(h, w, idx, 0) = (center_x - box_width / 2.) / img_width;
W
wanghaox 已提交
147
            // ymin
W
wanghaox 已提交
148
            e_boxes(h, w, idx, 1) = (center_y - box_height / 2.) / img_height;
W
wanghaox 已提交
149
            // xmax
W
wanghaox 已提交
150
            e_boxes(h, w, idx, 2) = (center_x + box_width / 2.) / img_width;
W
wanghaox 已提交
151
            // ymax
W
wanghaox 已提交
152
            e_boxes(h, w, idx, 3) = (center_y + box_height / 2.) / img_height;
153
            idx++;
W
wanghaox 已提交
154 155 156 157 158 159
          }
        }
      }
    }

    if (clip) {
W
wanghaox 已提交
160 161 162 163 164
      platform::Transform<platform::CPUDeviceContext> trans;
      ClipFunctor<T> clip_func;
      trans(ctx.template device_context<platform::CPUDeviceContext>(),
            boxes->data<T>(), boxes->data<T>() + boxes->numel(),
            boxes->data<T>(), clip_func);
W
wanghaox 已提交
165
    }
W
wanghaox 已提交
166 167 168 169

    Eigen::Tensor<T, 2, Eigen::RowMajor> var_et(1, variances.size());
    for (int i = 0; i < variances.size(); ++i) {
      var_et(0, i) = variances[i];
W
wanghaox 已提交
170
    }
W
wanghaox 已提交
171 172 173 174 175 176 177 178 179

    int box_num = layer_height * layer_width * num_priors;
    auto var_dim = vars->dims();
    vars->Resize({box_num, static_cast<int>(variances.size())});

    auto e_vars = framework::EigenMatrix<T, Eigen::RowMajor>::From(*vars);
    e_vars = var_et.broadcast(Eigen::DSizes<int, 2>(box_num, 1));

    vars->Resize(var_dim);
W
wanghaox 已提交
180
  }
W
wanghaox 已提交
181
};  // namespace operators
W
wanghaox 已提交
182 183 184

}  // namespace operators
}  // namespace paddle