SpatialPyramidPoolLayer.cpp 4.5 KB
Newer Older
Q
qijun 已提交
1 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
/* Copyright (c) 2016 Baidu, Inc. 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. */

#include "SpatialPyramidPoolLayer.h"

namespace paddle {

REGISTER_LAYER(spp, SpatialPyramidPoolLayer);

ProjectionConfig SpatialPyramidPoolLayer::getConfig(size_t imgSizeW,
                                                    size_t imgSizeH,
                                                    size_t channels,
                                                    size_t pyramidLevel,
                                                    std::string& poolType) {
  ProjectionConfig config;
Q
qijun 已提交
27
  config.set_type("pool");
Q
qijun 已提交
28 29 30 31 32 33 34 35 36
  PoolConfig* conf = config.mutable_pool_conf();
  conf->set_channels(channels);
  conf->set_img_size(imgSizeW);
  conf->set_img_size_y(imgSizeH);
  conf->set_pool_type(poolType);

  int numBins = std::pow(2, pyramidLevel);

  int sizeH = std::ceil(imgSizeH / static_cast<double>(numBins));
Q
qijun 已提交
37 38
  int paddingH = (sizeH * numBins - imgSizeH + 1) / 2;
  int outSizeH = outputSize(imgSizeH, sizeH, paddingH, sizeH, true);
Q
qijun 已提交
39 40

  int sizeW = std::ceil(imgSizeW / static_cast<double>(numBins));
Q
qijun 已提交
41 42
  int paddingW = (sizeW * numBins - imgSizeW + 1) / 2;
  int outSizeW = outputSize(imgSizeW, sizeW, paddingW, sizeW, true);
Q
qijun 已提交
43 44 45 46 47 48 49 50 51 52 53 54 55

  conf->set_stride(sizeW);
  conf->set_stride_y(sizeH);
  conf->set_size_x(sizeW);
  conf->set_size_y(sizeH);
  conf->set_padding(paddingW);
  conf->set_padding_y(paddingH);
  conf->set_output_x(outSizeW);
  conf->set_output_y(outSizeH);
  config.set_output_size(outSizeH * outSizeW * channels);
  return config;
}

Q
qijun 已提交
56 57 58
size_t SpatialPyramidPoolLayer::getSize() {
  CHECK_EQ(inputLayers_.size(), 1UL);
  size_t layerSize = 0;
L
Luo Tao 已提交
59
  const ImageConfig& conf = config_.inputs(0).spp_conf().image_conf();
Q
qijun 已提交
60 61
  imgSizeH_ = inputLayers_[0]->getOutput().getFrameHeight();
  imgSizeW_ = inputLayers_[0]->getOutput().getFrameWidth();
Q
qijun 已提交
62
  if (imgSizeH_ == 0) {
L
Luo Tao 已提交
63
    imgSizeH_ = conf.has_img_size_y() ? conf.img_size_y() : conf.img_size();
Q
qijun 已提交
64 65
  }
  if (imgSizeW_ == 0) {
L
Luo Tao 已提交
66
    imgSizeW_ = conf.img_size();
Q
qijun 已提交
67
  }
Q
qijun 已提交
68 69 70 71 72 73

  size_t outputH = 1;
  size_t outputW = (std::pow(4, pyramidHeight_) - 1) / (4 - 1);

  layerSize = outputH * outputW * channels_;
  return layerSize;
Q
qijun 已提交
74 75 76 77 78 79 80 81 82 83 84
}

bool SpatialPyramidPoolLayer::init(const LayerMap& layerMap,
                                   const ParameterMap& parameterMap) {
  Layer::init(layerMap, parameterMap);
  CHECK_EQ(config_.inputs_size(), 1);

  const SppConfig& sppConf = config_.inputs(0).spp_conf();
  pyramidHeight_ = sppConf.pyramid_height();
  poolType_ = sppConf.pool_type();

L
Luo Tao 已提交
85 86 87 88
  const ImageConfig& imageConf = sppConf.image_conf();
  channels_ = imageConf.channels();
  imgSizeW_ = imageConf.img_size();
  imgSizeH_ = imageConf.has_img_size_y() ? imageConf.img_size_y() : imgSizeW_;
Q
qijun 已提交
89 90 91 92 93 94 95 96
  poolProjections_.reserve(pyramidHeight_);
  projCol_.reserve(pyramidHeight_);
  projOutput_.resize(pyramidHeight_);

  size_t startCol = 0;
  size_t endCol = 0;
  for (size_t i = 0; i < pyramidHeight_; i++) {
    poolProjections_.emplace_back(PoolProjection::create(
97 98
        getConfig(imgSizeW_, imgSizeH_, channels_, i, poolType_),
        nullptr,
Q
qijun 已提交
99
        useGpu_));
Q
qijun 已提交
100 101 102 103
    endCol += poolProjections_[i]->getOutputSize();
    projCol_.push_back(std::make_pair(startCol, endCol));
    startCol = endCol;
  }
Q
qijun 已提交
104
  CHECK_EQ(endCol, getSize());
Q
qijun 已提交
105 106 107 108 109 110 111
  return true;
}

void SpatialPyramidPoolLayer::forward(PassType passType) {
  Layer::forward(passType);

  int batchSize = getInput(0).getBatchSize();
Q
qijun 已提交
112
  resetOutput(batchSize, getSize());
Q
qijun 已提交
113 114 115 116
  for (size_t i = 0; i < pyramidHeight_; i++) {
    size_t startCol = projCol_[i].first;
    size_t endCol = projCol_[i].second;
    projOutput_[i].value = output_.value->subColMatrix(startCol, endCol);
D
dangqingqing 已提交
117 118 119
    if (output_.grad) {
      projOutput_[i].grad = output_.grad->subColMatrix(startCol, endCol);
    }
Q
qijun 已提交
120 121
  }
  for (size_t i = 0; i < pyramidHeight_; i++) {
Q
qijun 已提交
122
    poolProjections_[i]->forward(&getInput(0), &projOutput_[i], passType);
Q
qijun 已提交
123 124 125 126 127 128 129 130 131 132 133 134
  }
}

void SpatialPyramidPoolLayer::backward(const UpdateCallback& callback) {
  for (size_t i = 0; i < pyramidHeight_; i++) {
    if (poolProjections_[i]) {
      poolProjections_[i]->backward(callback);
    }
  }
}

}  // namespace paddle