PriorBox.cpp 5.7 KB
Newer Older
1
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved.
Y
yuan 已提交
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

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 "Layer.h"
#include "paddle/math/BaseMatrix.h"
G
gaoyuan 已提交
17
#include "paddle/math/Matrix.h"
Y
yuan 已提交
18 19

namespace paddle {
G
gaoyuan 已提交
20
/**
21
 * @brief A layer for generating priorbox locations and variances.
G
gaoyuan 已提交
22
 * - Input: Two and only two input layer are accepted. The input layer must be
G
gaoyuan 已提交
23
 *          be a data output layer and a convolution output layer.
24
 * - Output: The priorbox locations and variances of the input data.
G
gaoyuan 已提交
25 26 27 28
 * Reference:
 *    Wei Liu, Dragomir Anguelov, Dumitru Erhan, Christian Szegedy, Scott Reed,
 *    Cheng-Yang Fu, Alexander C. Berg. SSD: Single Shot MultiBox Detector
 */
Y
yuan 已提交
29 30

class PriorBoxLayer : public Layer {
W
Wu Yi 已提交
31
 public:  // NOLINT
Y
yuan 已提交
32
  explicit PriorBoxLayer(const LayerConfig& config) : Layer(config) {}
Y
Yu Yang 已提交
33 34
  bool init(const LayerMap& layerMap,
            const ParameterMap& parameterMap) override;
35

Y
Yu Yang 已提交
36 37
  void forward(PassType passType) override;
  void backward(const UpdateCallback& callback) override {}
38

W
Wu Yi 已提交
39
 protected:  // NOLINT
Y
yuan 已提交
40 41 42
  int numPriors_;
  std::vector<int> minSize_;
  std::vector<int> maxSize_;
G
gaoyuan 已提交
43 44
  std::vector<real> aspectRatio_;
  std::vector<real> variance_;
Y
yuan 已提交
45 46 47
  MatrixPtr buffer_;
};

G
gaoyuan 已提交
48 49
REGISTER_LAYER(priorbox, PriorBoxLayer);

Y
yuan 已提交
50
bool PriorBoxLayer::init(const LayerMap& layerMap,
G
gaoyuan 已提交
51
                         const ParameterMap& parameterMap) {
Y
yuan 已提交
52
  Layer::init(layerMap, parameterMap);
G
gaoyuan 已提交
53
  auto pbConf = config_.inputs(0).priorbox_conf();
G
gaoyuan 已提交
54 55
  std::vector<real> tmp;
  aspectRatio_.push_back(1.);
G
gaoyuan 已提交
56 57
  std::copy(pbConf.min_size().begin(),
            pbConf.min_size().end(),
Y
yuan 已提交
58
            std::back_inserter(minSize_));
G
gaoyuan 已提交
59 60
  std::copy(pbConf.max_size().begin(),
            pbConf.max_size().end(),
Y
yuan 已提交
61
            std::back_inserter(maxSize_));
G
gaoyuan 已提交
62 63
  std::copy(pbConf.variance().begin(),
            pbConf.variance().end(),
Y
yuan 已提交
64
            std::back_inserter(variance_));
G
gaoyuan 已提交
65 66 67
  std::copy(pbConf.aspect_ratio().begin(),
            pbConf.aspect_ratio().end(),
            std::back_inserter(tmp));
W
wangmeng28 已提交
68 69 70

  if (maxSize_.size() > 0) CHECK_EQ(minSize_.size(), maxSize_.size());

71
  // flip aspect ratios
72
  for (unsigned index = 0; index < tmp.size(); index++) {
73 74 75 76
    real ar = tmp[index];
    if (fabs(ar - 1.) < 1e-6) continue;
    aspectRatio_.push_back(ar);
    aspectRatio_.push_back(1. / ar);
G
gaoyuan 已提交
77
  }
W
wangmeng28 已提交
78 79 80

  numPriors_ = aspectRatio_.size() * minSize_.size() + maxSize_.size();

Y
yuan 已提交
81 82 83 84 85
  return true;
}

void PriorBoxLayer::forward(PassType passType) {
  Layer::forward(passType);
G
gaoyuan 已提交
86 87 88
  auto input = getInput(0);
  int layerWidth = input.getFrameWidth();
  int layerHeight = input.getFrameHeight();
Y
yuan 已提交
89

G
gaoyuan 已提交
90 91 92
  auto image = getInput(1);
  int imageWidth = image.getFrameWidth();
  int imageHeight = image.getFrameHeight();
G
gaoyuan 已提交
93

G
gaoyuan 已提交
94 95
  real stepW = static_cast<real>(imageWidth) / layerWidth;
  real stepH = static_cast<real>(imageHeight) / layerHeight;
G
gaoyuan 已提交
96
  int dim = layerHeight * layerWidth * numPriors_ * 4;
Y
yuan 已提交
97 98 99
  reserveOutput(1, dim * 2);
  // use a cpu buffer to compute
  Matrix::resizeOrCreate(buffer_, 1, dim * 2, false, false);
G
gaoyuan 已提交
100
  auto* tmpPtr = buffer_->getData();
Y
yuan 已提交
101 102

  int idx = 0;
G
gaoyuan 已提交
103 104
  for (int h = 0; h < layerHeight; ++h) {
    for (int w = 0; w < layerWidth; ++w) {
G
gaoyuan 已提交
105 106
      real centerX = (w + 0.5) * stepW;
      real centerY = (h + 0.5) * stepH;
Y
yuan 已提交
107
      for (size_t s = 0; s < minSize_.size(); s++) {
108
        real minSize = minSize_[s];
G
gaoyuan 已提交
109 110
        real boxWidth = minSize;
        real boxHeight = minSize;
111

Y
yangyaming 已提交
112 113 114 115 116 117 118 119 120 121 122 123
        // first prior: aspect_ratio == 1.0, compatible to old logic
        tmpPtr[idx++] = (centerX - boxWidth / 2.) / imageWidth;
        tmpPtr[idx++] = (centerY - boxHeight / 2.) / imageHeight;
        tmpPtr[idx++] = (centerX + boxWidth / 2.) / imageWidth;
        tmpPtr[idx++] = (centerY + boxHeight / 2.) / imageHeight;
        // set the variance.
        for (int t = 0; t < 4; t++) tmpPtr[idx++] = variance_[t];

        if (maxSize_.size() > 0) {
          // square prior with size sqrt(minSize * maxSize)
          real maxSize = maxSize_[s];
          boxWidth = boxHeight = sqrt(minSize * maxSize);
124 125 126 127 128 129 130
          tmpPtr[idx++] = (centerX - boxWidth / 2.) / imageWidth;
          tmpPtr[idx++] = (centerY - boxHeight / 2.) / imageHeight;
          tmpPtr[idx++] = (centerX + boxWidth / 2.) / imageWidth;
          tmpPtr[idx++] = (centerY + boxHeight / 2.) / imageHeight;
          // set the variance.
          for (int t = 0; t < 4; t++) tmpPtr[idx++] = variance_[t];
        }
W
wangmeng28 已提交
131

Y
yangyaming 已提交
132 133 134 135 136 137 138 139
        // priors with different aspect ratios
        for (size_t r = 0; r < aspectRatio_.size(); r++) {
          real ar = aspectRatio_[r];
          if (fabs(ar - 1.0) < 1e-6) {
            continue;
          }
          boxWidth = minSize * sqrt(ar);
          boxHeight = minSize / sqrt(ar);
W
wangmeng28 已提交
140 141 142 143 144 145 146
          tmpPtr[idx++] = (centerX - boxWidth / 2.) / imageWidth;
          tmpPtr[idx++] = (centerY - boxHeight / 2.) / imageHeight;
          tmpPtr[idx++] = (centerX + boxWidth / 2.) / imageWidth;
          tmpPtr[idx++] = (centerY + boxHeight / 2.) / imageHeight;
          // set the variance.
          for (int t = 0; t < 4; t++) tmpPtr[idx++] = variance_[t];
        }
Y
yuan 已提交
147 148 149
      }
    }
  }
W
wangmeng28 已提交
150

Y
yuan 已提交
151
  // clip the prior's coordidate such that it is within [0, 1]
G
gaoyuan 已提交
152 153
  for (int d = 0; d < dim * 2; ++d)
    if ((d % 8) < 4)
G
gaoyuan 已提交
154
      tmpPtr[d] = std::min(std::max(tmpPtr[d], (real)0.), (real)1.);
Y
yuan 已提交
155 156 157 158 159
  MatrixPtr outV = getOutputValue();
  outV->copyFrom(buffer_->data_, dim * 2);
}

}  // namespace paddle