Pool3DLayer.cpp 5.9 KB
Newer Older
1
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved.
C
chengduoZH 已提交
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 "Pool3DLayer.h"
#include "PoolProjectionLayer.h"
X
Xin Pan 已提交
17
#include "paddle/legacy/utils/Logging.h"
C
chengduoZH 已提交
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60

namespace paddle {

REGISTER_LAYER(pool3d, Pool3DLayer);

bool Pool3DLayer::init(const LayerMap& layerMap,
                       const ParameterMap& parameterMap) {
  Layer::init(layerMap, parameterMap);

  /* the size of inputs for pool-layer is 1 */
  CHECK_EQ(config_.inputs_size(), 1);

  const PoolConfig& conf = config_.inputs(0).pool_conf();
  poolType_ = conf.pool_type();
  channels_ = conf.channels();

  sizeX_ = conf.size_x();
  sizeY_ = conf.size_y();
  sizeZ_ = conf.size_z();

  strideW_ = conf.stride();
  strideH_ = conf.stride_y();
  strideD_ = conf.stride_z();

  imgSizeW_ = conf.img_size();
  imgSizeH_ = conf.img_size_y();
  imgSizeD_ = conf.img_size_z();

  paddingW_ = conf.padding();
  paddingH_ = conf.padding_y();
  paddingD_ = conf.padding_z();

  outputW_ = conf.output_x();
  outputH_ = conf.output_y();
  outputD_ = conf.output_z();

  return true;
}

size_t Pool3DLayer::getSize() {
  CHECK_EQ(inputLayers_.size(), 1UL);

  size_t layerSize = 0;
C
chengduoZH 已提交
61 62 63
  outputD_ = outputSize(imgSizeD_, sizeZ_, paddingD_, strideD_, false);
  outputH_ = outputSize(imgSizeH_, sizeY_, paddingH_, strideH_, false);
  outputW_ = outputSize(imgSizeW_, sizeX_, paddingW_, strideW_, false);
C
chengduoZH 已提交
64 65 66 67 68 69 70 71 72 73 74

  layerSize = outputD_ * outputH_ * outputW_ * channels_;
  getOutput().setFrameHeight(outputH_);
  getOutput().setFrameWidth(outputW_);
  getOutput().setFrameDepth(outputD_);
  return layerSize;
}

void Pool3DLayer::forward(PassType passType) {
  Layer::forward(passType);
  const MatrixPtr& inMat = inputLayers_[0]->getOutputValue();
C
chengduoZH 已提交
75 76
  size_t batchSize = inMat->getHeight();
  size_t outWidth = getSize();
C
chengduoZH 已提交
77
  resetOutput(batchSize, outWidth);
C
chengduoZH 已提交
78
  Matrix::resizeOrCreate(maxPoolIdx_, batchSize, outWidth, false, useGpu_);
C
chengduoZH 已提交
79 80 81 82
  const MatrixPtr outMat = getOutputValue();

  if (poolType_ == "avg") {
    outMat->avgPool3DForward(*inMat,
C
chengduoZH 已提交
83
                             channels_,
C
chengduoZH 已提交
84 85 86
                             imgSizeD_,
                             imgSizeH_,
                             imgSizeW_,
C
chengduoZH 已提交
87 88 89
                             outputD_,
                             outputH_,
                             outputW_,
C
chengduoZH 已提交
90 91 92 93 94 95 96 97 98 99 100
                             sizeZ_,
                             sizeY_,
                             sizeX_,
                             strideD_,
                             strideH_,
                             strideW_,
                             paddingD_,
                             paddingH_,
                             paddingW_);
  } else if (poolType_ == "max") {
    outMat->maxPool3DForward(*inMat,
C
chengduoZH 已提交
101
                             *maxPoolIdx_,
C
chengduoZH 已提交
102
                             channels_,
C
chengduoZH 已提交
103 104 105
                             imgSizeD_,
                             imgSizeH_,
                             imgSizeW_,
C
chengduoZH 已提交
106 107 108
                             outputD_,
                             outputH_,
                             outputW_,
C
chengduoZH 已提交
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
                             sizeZ_,
                             sizeY_,
                             sizeX_,
                             strideD_,
                             strideH_,
                             strideW_,
                             paddingD_,
                             paddingH_,
                             paddingW_);
  } else {
    LOG(FATAL) << "Unknown pool type: " << poolType_;
  }
  forwardActivation();
}

void Pool3DLayer::backward(const UpdateCallback& callback) {
  backwardActivation();

  (void)callback;
  if (NULL == getInputGrad(0)) return;
  MatrixPtr inMat = inputLayers_[0]->getOutputValue();
  MatrixPtr inGradMat = inputLayers_[0]->getOutputGrad();
  MatrixPtr outMat = getOutputValue();
  MatrixPtr outGradMat = getOutputGrad();

  if (poolType_ == "avg") {
    inGradMat->avgPool3DBackward(*outGradMat,
                                 imgSizeD_,
                                 imgSizeH_,
                                 imgSizeW_,
C
chengduoZH 已提交
139 140 141
                                 outputD_,
                                 outputH_,
                                 outputW_,
C
chengduoZH 已提交
142 143 144 145 146 147 148 149
                                 sizeZ_,
                                 sizeY_,
                                 sizeZ_,
                                 strideD_,
                                 strideH_,
                                 strideW_,
                                 paddingD_,
                                 paddingH_,
C
chengduoZH 已提交
150 151 152
                                 paddingW_,
                                 1.0,
                                 1.0);
C
chengduoZH 已提交
153
  } else if (poolType_ == "max") {
C
chengduoZH 已提交
154 155
    inGradMat->maxPool3DBackward(*outGradMat,
                                 *maxPoolIdx_,
C
chengduoZH 已提交
156 157 158
                                 imgSizeD_,
                                 imgSizeH_,
                                 imgSizeW_,
C
chengduoZH 已提交
159 160 161
                                 outputD_,
                                 outputH_,
                                 outputW_,
C
chengduoZH 已提交
162 163 164 165 166 167 168 169
                                 sizeZ_,
                                 sizeY_,
                                 sizeZ_,
                                 strideD_,
                                 strideH_,
                                 strideW_,
                                 paddingD_,
                                 paddingH_,
C
chengduoZH 已提交
170 171 172
                                 paddingW_,
                                 1.0,
                                 1.0);
C
chengduoZH 已提交
173 174 175 176 177 178
  } else {
    LOG(FATAL) << "Unknown pool type: " << poolType_;
  }
}

}  // namespace paddle