prior_box_op.h 7.3 KB
Newer Older
1
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved.
W
wanghaox 已提交
2 3 4 5 6 7 8 9 10 11 12 13 14 15

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
16 17
#include <algorithm>
#include <vector>
18

Y
Yi Wang 已提交
19 20
#include "paddle/fluid/framework/op_registry.h"
#include "paddle/fluid/platform/transform.h"
21
#include "paddle/phi/kernels/funcs/math_function.h"
W
wanghaox 已提交
22 23 24 25

namespace paddle {
namespace operators {

26 27 28
constexpr int kPriorBoxFLOAT = 1;
constexpr int kPriorBoxDOUBLE = 2;

W
wanghaox 已提交
29 30
inline void ExpandAspectRatios(const std::vector<float>& input_aspect_ratior,
                               bool flip,
31
                               std::vector<float>* output_aspect_ratior) {
32
  constexpr float epsilon = 1e-6;
33 34
  output_aspect_ratior->clear();
  output_aspect_ratior->push_back(1.0f);
W
wanghaox 已提交
35 36 37
  for (size_t i = 0; i < input_aspect_ratior.size(); ++i) {
    float ar = input_aspect_ratior[i];
    bool already_exist = false;
38 39
    for (size_t j = 0; j < output_aspect_ratior->size(); ++j) {
      if (fabs(ar - output_aspect_ratior->at(j)) < epsilon) {
W
wanghaox 已提交
40 41 42 43 44
        already_exist = true;
        break;
      }
    }
    if (!already_exist) {
45
      output_aspect_ratior->push_back(ar);
W
wanghaox 已提交
46
      if (flip) {
47
        output_aspect_ratior->push_back(1.0f / ar);
W
wanghaox 已提交
48 49 50 51 52
      }
    }
  }
}

53
template <typename T, typename K>
W
wanghaox 已提交
54 55 56
class PriorBoxOpKernel : public framework::OpKernel<T> {
 public:
  void Compute(const framework::ExecutionContext& ctx) const override {
57 58 59 60
    auto* input = ctx.Input<phi::DenseTensor>("Input");
    auto* image = ctx.Input<phi::DenseTensor>("Image");
    auto* boxes = ctx.Output<phi::DenseTensor>("Boxes");
    auto* vars = ctx.Output<phi::DenseTensor>("Variances");
W
wanghaox 已提交
61

C
chengduoZH 已提交
62 63
    auto min_sizes = ctx.Attr<std::vector<float>>("min_sizes");
    auto max_sizes = ctx.Attr<std::vector<float>>("max_sizes");
W
wanghaox 已提交
64 65 66 67
    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");
68 69
    auto min_max_aspect_ratios_order =
        ctx.Attr<bool>("min_max_aspect_ratios_order");
W
wanghaox 已提交
70 71

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

74 75 76
    K step_w = static_cast<K>(ctx.Attr<float>("step_w"));
    K step_h = static_cast<K>(ctx.Attr<float>("step_h"));
    K offset = static_cast<K>(ctx.Attr<float>("offset"));
W
wanghaox 已提交
77

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

W
wanghaox 已提交
81 82
    auto feature_width = input->dims()[3];
    auto feature_height = input->dims()[2];
W
wanghaox 已提交
83

84
    K step_width, step_height;
W
wanghaox 已提交
85
    if (step_w == 0 || step_h == 0) {
86 87
      step_width = static_cast<K>(img_width) / feature_width;
      step_height = static_cast<K>(img_height) / feature_height;
W
wanghaox 已提交
88 89 90 91 92 93 94 95 96 97
    } 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();
    }

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

101
    K* b_t = boxes->data<K>();
W
wanghaox 已提交
102 103
    for (int h = 0; h < feature_height; ++h) {
      for (int w = 0; w < feature_width; ++w) {
104 105 106
        K center_x = (w + offset) * step_width;
        K center_y = (h + offset) * step_height;
        K box_width, box_height;
W
wanghaox 已提交
107
        for (size_t s = 0; s < min_sizes.size(); ++s) {
C
chengduoZH 已提交
108
          auto min_size = min_sizes[s];
109 110
          if (min_max_aspect_ratios_order) {
            box_width = box_height = min_size / 2.;
111 112 113 114 115
            b_t[0] = (center_x - box_width) / img_width;
            b_t[1] = (center_y - box_height) / img_height;
            b_t[2] = (center_x + box_width) / img_width;
            b_t[3] = (center_y + box_height) / img_height;
            b_t += 4;
116 117 118 119
            if (max_sizes.size() > 0) {
              auto max_size = max_sizes[s];
              // square prior with size sqrt(minSize * maxSize)
              box_width = box_height = sqrt(min_size * max_size) / 2.;
120 121 122 123 124
              b_t[0] = (center_x - box_width) / img_width;
              b_t[1] = (center_y - box_height) / img_height;
              b_t[2] = (center_x + box_width) / img_width;
              b_t[3] = (center_y + box_height) / img_height;
              b_t += 4;
125 126 127 128 129 130 131 132 133
            }
            // priors with different aspect ratios
            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) / 2.;
              box_height = min_size / sqrt(ar) / 2.;
134 135 136 137 138
              b_t[0] = (center_x - box_width) / img_width;
              b_t[1] = (center_y - box_height) / img_height;
              b_t[2] = (center_x + box_width) / img_width;
              b_t[3] = (center_y + box_height) / img_height;
              b_t += 4;
139 140 141 142 143 144 145
            }
          } else {
            // priors with different aspect ratios
            for (size_t r = 0; r < aspect_ratios.size(); ++r) {
              float ar = aspect_ratios[r];
              box_width = min_size * sqrt(ar) / 2.;
              box_height = min_size / sqrt(ar) / 2.;
146 147 148 149 150
              b_t[0] = (center_x - box_width) / img_width;
              b_t[1] = (center_y - box_height) / img_height;
              b_t[2] = (center_x + box_width) / img_width;
              b_t[3] = (center_y + box_height) / img_height;
              b_t += 4;
151 152 153 154 155
            }
            if (max_sizes.size() > 0) {
              auto max_size = max_sizes[s];
              // square prior with size sqrt(minSize * maxSize)
              box_width = box_height = sqrt(min_size * max_size) / 2.;
156 157 158 159 160
              b_t[0] = (center_x - box_width) / img_width;
              b_t[1] = (center_y - box_height) / img_height;
              b_t[2] = (center_x + box_width) / img_width;
              b_t[3] = (center_y + box_height) / img_height;
              b_t += 4;
161
            }
W
wanghaox 已提交
162 163 164 165 166 167
          }
        }
      }
    }

    if (clip) {
168 169 170
      K* dt = boxes->data<K>();
      std::transform(dt, dt + boxes->numel(), dt, [](K v) -> K {
        return std::min<K>(std::max<K>(v, 0.), 1.);
171
      });
W
wanghaox 已提交
172
    }
W
wanghaox 已提交
173

174
    phi::DenseTensor var_t;
175
    var_t.mutable_data<K>(
176
        phi::make_ddim({1, static_cast<int>(variances.size())}),
W
wanghaox 已提交
177
        ctx.GetPlace());
178
    auto var_et = framework::EigenTensor<K, 2>::From(var_t);
179 180 181 182

#ifdef PADDLE_WITH_MKLML
#pragma omp parallel for
#endif
W
wanghaox 已提交
183
    for (size_t i = 0; i < variances.size(); ++i) {
W
wanghaox 已提交
184
      var_et(0, i) = variances[i];
W
wanghaox 已提交
185
    }
W
wanghaox 已提交
186

W
wanghaox 已提交
187
    int box_num = feature_height * feature_width * num_priors;
W
wanghaox 已提交
188 189 190
    auto var_dim = vars->dims();
    vars->Resize({box_num, static_cast<int>(variances.size())});

191
    auto e_vars = framework::EigenMatrix<K, Eigen::RowMajor>::From(*vars);
W
wanghaox 已提交
192

193 194 195 196
#ifdef PADDLE_WITH_MKLML
#pragma omp parallel for collapse(2)
#endif
    for (int i = 0; i < box_num; ++i) {
197
      for (size_t j = 0; j < variances.size(); ++j) {
198 199 200
        e_vars(i, j) = variances[j];
      }
    }
W
wanghaox 已提交
201
    vars->Resize(var_dim);
W
wanghaox 已提交
202
  }
W
wanghaox 已提交
203
};  // namespace operators
W
wanghaox 已提交
204 205 206

}  // namespace operators
}  // namespace paddle