PriorBox.cpp 5.8 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 20 21 22 23 24 25 26

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 {

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) {}
27
  void forwardImp(const Argument& featureMap, const Argument& imageShape);
Y
yuan 已提交
28 29 30 31 32
  int numPriors_;
  std::vector<int> minSize_;
  std::vector<int> maxSize_;
  std::vector<float> aspectRatio_;
  std::vector<float> variance_;
33
  std::vector<Argument> tmpCpuInput_;
Y
yuan 已提交
34 35 36 37
  MatrixPtr buffer_;
};

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

void PriorBoxLayer::forward(PassType passType) {
  Layer::forward(passType);
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
  if (useGpu_) {
    for (size_t i = 0; i < inputLayers_.size(); i++) {
      tmpCpuInput_[i].resizeAndCopyFrom(
          getInput(i), false, HPPL_STREAM_DEFAULT);
      hl_stream_synchronize(HPPL_STREAM_DEFAULT);
      forwardImp(tmpCpuInput_[0], tmpCpuInput_[1]);
    }
  } else {
    forwardImp(getInput(0), getInput(1));
  }
}

void PriorBoxLayer::forwardImp(const Argument& featureMap,
                               const Argument& imageShape) {
  int layer_width = featureMap.getFrameWidth();
  int layer_height = featureMap.getFrameHeight();
Y
yuan 已提交
88

89
  MatrixPtr inV1 = imageShape.value;
Y
yuan 已提交
90 91 92 93 94 95 96 97 98 99 100 101 102
  int image_width = inV1->getElement(0, 0);
  int image_height = inV1->getElement(0, 1);
  float step_w = static_cast<float>(image_width) / layer_width;
  float step_h = static_cast<float>(image_height) / layer_height;
  int dim = layer_height * layer_width * numPriors_ * 4;
  reserveOutput(1, dim * 2);
  // use a cpu buffer to compute
  Matrix::resizeOrCreate(buffer_, 1, dim * 2, false, false);
  auto* tmp_ptr = buffer_->getData();

  int idx = 0;
  for (int h = 0; h < layer_height; ++h) {
    for (int w = 0; w < layer_width; ++w) {
G
gaoyuan 已提交
103
      float center_x = (w + 0.5) * step_w;
Y
yuan 已提交
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
      float center_y = (h + 0.5) * step_h;
      int min_size = 0;
      for (size_t s = 0; s < minSize_.size(); s++) {
        // first prior.
        min_size = minSize_[s];
        int box_width = min_size;
        int box_height = min_size;
        // xmin, ymin, xmax, ymax.
        tmp_ptr[idx++] = (center_x - box_width / 2.) / image_width;
        tmp_ptr[idx++] = (center_y - box_height / 2.) / image_height;
        tmp_ptr[idx++] = (center_x + box_width / 2.) / image_width;
        tmp_ptr[idx++] = (center_y + box_height / 2.) / image_height;

        if (maxSize_.size() > 0) {
          CHECK_EQ(minSize_.size(), maxSize_.size());
          // second prior.
          for (size_t s = 0; s < maxSize_.size(); s++) {
            int max_size = maxSize_[s];
            box_width = box_height = sqrt(min_size * max_size);
            tmp_ptr[idx++] = (center_x - box_width / 2.) / image_width;
            tmp_ptr[idx++] = (center_y - box_height / 2.) / image_height;
            tmp_ptr[idx++] = (center_x + box_width / 2.) / image_width;
            tmp_ptr[idx++] = (center_y + box_height / 2.) / image_height;
          }
        }
      }
      // rest of priors.
      for (size_t r = 0; r < aspectRatio_.size(); r++) {
        float ar = aspectRatio_[r];
G
gaoyuan 已提交
133
        if (fabs(ar - 1.) < 1e-6) continue;
Y
yuan 已提交
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
        float box_width = min_size * sqrt(ar);
        float box_height = min_size / sqrt(ar);
        tmp_ptr[idx++] = (center_x - box_width / 2.) / image_width;
        tmp_ptr[idx++] = (center_y - box_height / 2.) / image_height;
        tmp_ptr[idx++] = (center_x + box_width / 2.) / image_width;
        tmp_ptr[idx++] = (center_y + box_height / 2.) / image_height;
      }
    }
  }
  // clip the prior's coordidate such that it is within [0, 1]
  for (int d = 0; d < dim; ++d)
    tmp_ptr[d] = std::min(std::max(tmp_ptr[d], (float)0.), (float)1.);
  // set the variance.
  for (int h = 0; h < layer_height; h++)
    for (int w = 0; w < layer_width; w++)
      for (int i = 0; i < numPriors_; i++)
G
gaoyuan 已提交
150
        for (int j = 0; j < 4; j++) tmp_ptr[idx++] = variance_[j];
Y
yuan 已提交
151 152 153
  MatrixPtr outV = getOutputValue();
  outV->copyFrom(buffer_->data_, dim * 2);
}
154

Y
yuan 已提交
155 156 157
REGISTER_LAYER(priorbox, PriorBoxLayer);

}  // namespace paddle