Pool3DLayer.cpp 6.5 KB
Newer Older
C
chengduoZH 已提交
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 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 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 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 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
/* Copyright (c) 2016 PaddlePaddle Authors. 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 "Pool3DLayer.h"
#include "PoolProjectionLayer.h"
#include "paddle/utils/Logging.h"

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;
  //  imgSizeD_ = inputLayers_[0]->getOutput().getFrameDepth();
  //  imgSizeH_ = inputLayers_[0]->getOutput().getFrameHeight();
  //  imgSizeW_ = inputLayers_[0]->getOutput().getFrameWidth();
  if (imgSizeH_ == 0) {
    //    imgSizeH_ = imgSizeY_;
  }
  if (imgSizeW_ == 0) {
    //    imgSizeW_ = imgSize_;
  }
  outputD_ = outputSize(imgSizeD_,
                        sizeZ_,
                        paddingD_,
                        strideD_,
                        /* caffeMode */ false);
  outputH_ = outputSize(imgSizeH_,
                        sizeY_,
                        paddingH_,
                        strideH_,
                        /* caffeMode */ false);
  outputW_ = outputSize(imgSizeW_,
                        sizeX_,
                        paddingW_,
                        strideW_,
                        /* caffeMode */ false);

  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();
  int batchSize = inMat->getHeight();
  int outWidth = getSize();
  resetOutput(batchSize, outWidth);
  const MatrixPtr outMat = getOutputValue();

  if (poolType_ == "avg") {
    outMat->avgPool3DForward(*inMat,
                             imgSizeD_,
                             imgSizeH_,
                             imgSizeW_,
                             channels_,
                             sizeZ_,
                             sizeY_,
                             sizeX_,
                             strideD_,
                             strideH_,
                             strideW_,
                             outputD_,
                             outputH_,
                             outputW_,
                             paddingD_,
                             paddingH_,
                             paddingW_);
  } else if (poolType_ == "max") {
    outMat->maxPool3DForward(*inMat,
                             imgSizeD_,
                             imgSizeH_,
                             imgSizeW_,
                             channels_,
                             sizeZ_,
                             sizeY_,
                             sizeX_,
                             strideD_,
                             strideH_,
                             strideW_,
                             outputD_,
                             outputH_,
                             outputW_,
                             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_,
                                 sizeZ_,
                                 sizeY_,
                                 sizeZ_,
                                 strideD_,
                                 strideH_,
                                 strideW_,
                                 outputD_,
                                 outputH_,
                                 outputW_,
                                 1,
                                 1,
                                 paddingD_,
                                 paddingH_,
                                 paddingW_);
  } else if (poolType_ == "max") {
    inGradMat->maxPool3DBackward(*inMat,
                                 imgSizeD_,
                                 imgSizeH_,
                                 imgSizeW_,
                                 *outGradMat,
                                 *outMat,
                                 sizeZ_,
                                 sizeY_,
                                 sizeZ_,
                                 strideD_,
                                 strideH_,
                                 strideW_,
                                 outputD_,
                                 outputH_,
                                 outputW_,
                                 1,
                                 1,
                                 paddingD_,
                                 paddingH_,
                                 paddingW_);
  } else {
    LOG(FATAL) << "Unknown pool type: " << poolType_;
  }
}

}  // namespace paddle