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

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/Matrix.h"
#include "paddle/math/BaseMatrix.h"

namespace paddle {
G
gaoyuan 已提交
20 21 22 23 24 25 26 27 28
/**
 * @brief A layer for generate prior box locations and variances.
 * - Input: Two and only two input layer are accepted. The input layer must be
 *        be a data output layer and a convolution output layer.
 * - Output: The prior box locations and variances of the input data.
 * 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 31 32 33 34 35 36 37 38 39 40 41 42 43 44

class PriorBoxLayer : public Layer {
public:
  explicit PriorBoxLayer(const LayerConfig& config) : Layer(config) {}
  bool init(const LayerMap& layerMap, const ParameterMap& parameterMap);
  void forward(PassType passType);
  void backward(const UpdateCallback& callback) {}
  int numPriors_;
  std::vector<int> minSize_;
  std::vector<int> maxSize_;
  std::vector<float> aspectRatio_;
  std::vector<float> variance_;
  MatrixPtr buffer_;
};

bool PriorBoxLayer::init(const LayerMap& layerMap,
G
gaoyuan 已提交
45
                         const ParameterMap& parameterMap) {
Y
yuan 已提交
46
  Layer::init(layerMap, parameterMap);
G
gaoyuan 已提交
47 48 49
  auto pbConf = config_.inputs(0).priorbox_conf();
  std::copy(pbConf.min_size().begin(),
            pbConf.min_size().end(),
Y
yuan 已提交
50
            std::back_inserter(minSize_));
G
gaoyuan 已提交
51 52
  std::copy(pbConf.max_size().begin(),
            pbConf.max_size().end(),
Y
yuan 已提交
53
            std::back_inserter(maxSize_));
G
gaoyuan 已提交
54 55
  std::copy(pbConf.aspect_ratio().begin(),
            pbConf.aspect_ratio().end(),
Y
yuan 已提交
56
            std::back_inserter(aspectRatio_));
G
gaoyuan 已提交
57 58
  std::copy(pbConf.variance().begin(),
            pbConf.variance().end(),
Y
yuan 已提交
59 60
            std::back_inserter(variance_));
  // flip
G
gaoyuan 已提交
61 62
  int inputRatioLength = aspectRatio_.size();
  for (int index = 0; index < inputRatioLength; index++)
G
gaoyuan 已提交
63
    aspectRatio_.push_back(1 / aspectRatio_[index]);
Y
yuan 已提交
64 65
  aspectRatio_.push_back(1.);
  numPriors_ = aspectRatio_.size();
G
gaoyuan 已提交
66
  if (maxSize_.size() > 0) numPriors_++;
Y
yuan 已提交
67 68 69 70 71
  return true;
}

void PriorBoxLayer::forward(PassType passType) {
  Layer::forward(passType);
G
gaoyuan 已提交
72 73 74
  auto input = getInput(0);
  int layerWidth = input.getFrameWidth();
  int layerHeight = input.getFrameHeight();
Y
yuan 已提交
75

G
gaoyuan 已提交
76 77 78 79 80 81
  auto image = getInput(1);
  int imageWidth = image.getFrameWidth();
  int imageHeight = image.getFrameHeight();
  float stepW = static_cast<float>(imageWidth) / layerWidth;
  float stepH = static_cast<float>(imageHeight) / layerHeight;
  int dim = layerHeight * layerWidth * numPriors_ * 4;
Y
yuan 已提交
82 83 84
  reserveOutput(1, dim * 2);
  // use a cpu buffer to compute
  Matrix::resizeOrCreate(buffer_, 1, dim * 2, false, false);
G
gaoyuan 已提交
85
  auto* tmpPtr = buffer_->getData();
Y
yuan 已提交
86 87

  int idx = 0;
G
gaoyuan 已提交
88 89 90 91 92
  for (int h = 0; h < layerHeight; ++h) {
    for (int w = 0; w < layerWidth; ++w) {
      float centerX = (w + 0.5) * stepW;
      float centerY = (h + 0.5) * stepH;
      int minSize = 0;
Y
yuan 已提交
93 94
      for (size_t s = 0; s < minSize_.size(); s++) {
        // first prior.
G
gaoyuan 已提交
95 96 97
        minSize = minSize_[s];
        int boxWidth = minSize;
        int boxHeight = minSize;
Y
yuan 已提交
98
        // xmin, ymin, xmax, ymax.
G
gaoyuan 已提交
99 100 101 102
        tmpPtr[idx++] = (centerX - boxWidth / 2.) / imageWidth;
        tmpPtr[idx++] = (centerY - boxHeight / 2.) / imageHeight;
        tmpPtr[idx++] = (centerX + boxWidth / 2.) / imageWidth;
        tmpPtr[idx++] = (centerY + boxHeight / 2.) / imageHeight;
Y
yuan 已提交
103 104 105 106 107

        if (maxSize_.size() > 0) {
          CHECK_EQ(minSize_.size(), maxSize_.size());
          // second prior.
          for (size_t s = 0; s < maxSize_.size(); s++) {
G
gaoyuan 已提交
108 109 110 111 112 113
            int maxSize = maxSize_[s];
            boxWidth = boxHeight = sqrt(minSize * maxSize);
            tmpPtr[idx++] = (centerX - boxWidth / 2.) / imageWidth;
            tmpPtr[idx++] = (centerY - boxHeight / 2.) / imageHeight;
            tmpPtr[idx++] = (centerX + boxWidth / 2.) / imageWidth;
            tmpPtr[idx++] = (centerY + boxHeight / 2.) / imageHeight;
Y
yuan 已提交
114 115 116 117 118 119
          }
        }
      }
      // rest of priors.
      for (size_t r = 0; r < aspectRatio_.size(); r++) {
        float ar = aspectRatio_[r];
G
gaoyuan 已提交
120
        if (fabs(ar - 1.) < 1e-6) continue;
G
gaoyuan 已提交
121 122 123 124 125 126
        float boxWidth = minSize * sqrt(ar);
        float boxHeight = minSize / sqrt(ar);
        tmpPtr[idx++] = (centerX - boxWidth / 2.) / imageWidth;
        tmpPtr[idx++] = (centerY - boxHeight / 2.) / imageHeight;
        tmpPtr[idx++] = (centerX + boxWidth / 2.) / imageWidth;
        tmpPtr[idx++] = (centerY + boxHeight / 2.) / imageHeight;
Y
yuan 已提交
127 128 129 130 131
      }
    }
  }
  // clip the prior's coordidate such that it is within [0, 1]
  for (int d = 0; d < dim; ++d)
G
gaoyuan 已提交
132
    tmpPtr[d] = std::min(std::max(tmpPtr[d], (float)0.), (float)1.);
Y
yuan 已提交
133
  // set the variance.
G
gaoyuan 已提交
134 135
  for (int h = 0; h < layerHeight; h++)
    for (int w = 0; w < layerWidth; w++)
Y
yuan 已提交
136
      for (int i = 0; i < numPriors_; i++)
G
gaoyuan 已提交
137
        for (int j = 0; j < 4; j++) tmpPtr[idx++] = variance_[j];
Y
yuan 已提交
138 139 140 141 142 143
  MatrixPtr outV = getOutputValue();
  outV->copyFrom(buffer_->data_, dim * 2);
}
REGISTER_LAYER(priorbox, PriorBoxLayer);

}  // namespace paddle