BlockExpandLayer.cpp 7.0 KB
Newer Older
1
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve.
Z
zhangjinchao01 已提交
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

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 "BlockExpandLayer.h"

#include "paddle/utils/Logging.h"

namespace paddle {

REGISTER_LAYER(blockexpand, BlockExpandLayer);

bool BlockExpandLayer::init(const LayerMap& layerMap,
                            const ParameterMap& parameterMap) {
  /* Initialize the basic parent class */
  Layer::init(layerMap, parameterMap);

  CHECK_EQ(config_.inputs_size(), 1);
  const BlockExpandConfig& blockConf = config_.inputs(0).block_expand_conf();
  blockH_ = blockConf.block_y();
  blockW_ = blockConf.block_x();
  strideH_ = blockConf.stride_y();
  strideW_ = blockConf.stride_x();
  paddingH_ = blockConf.padding_y();
  paddingW_ = blockConf.padding_x();
  channels_ = blockConf.channels();
  imgSizeH_ = blockConf.img_size_y();
  imgSizeW_ = blockConf.img_size_x();

40 41 42 43 44 45 46 47 48 49
  if (!useGpu_) {
    std::vector<size_t> strides = {(size_t)strideH_, (size_t)strideW_};
    std::vector<size_t> paddings = {(size_t)paddingH_, (size_t)paddingW_};
    std::vector<size_t> blocks = {(size_t)blockH_, (size_t)blockW_};
    createFunction(forward_,
                   "ImageExpand",
                   FuncConfig()
                       .set("strides", strides)
                       .set("paddings", paddings)
                       .set("blocks", blocks));
H
hedaoyuan 已提交
50 51 52 53 54 55
    createFunction(backward_,
                   "ImageExpandGrad",
                   FuncConfig()
                       .set("strides", strides)
                       .set("paddings", paddings)
                       .set("blocks", blocks));
56 57
  }

Z
zhangjinchao01 已提交
58 59 60 61 62 63 64 65 66 67 68 69 70 71
  return true;
}

size_t BlockExpandLayer::getBlockNum() {
  CHECK_EQ(inputLayers_.size(), 1UL);
  const BlockExpandConfig& blockConf = config_.inputs(0).block_expand_conf();
  imgSizeH_ = inputLayers_[0]->getOutput().getFrameHeight();
  imgSizeW_ = inputLayers_[0]->getOutput().getFrameWidth();
  if (imgSizeH_ == 0) {
    imgSizeH_ = blockConf.img_size_y();
  }
  if (imgSizeW_ == 0) {
    imgSizeW_ = blockConf.img_size_x();
  }
72
  size_t tmpH = 2 * paddingH_ + imgSizeH_ - blockH_;
Z
zhangjinchao01 已提交
73 74 75 76 77 78 79 80 81 82 83 84 85 86
  outputH_ = (int)tmpH < 0 ? 1 : 1 + (tmpH + strideH_ - 1) / strideH_;
  size_t tmpW = 2 * paddingW_ + imgSizeW_ - blockW_;
  outputW_ = (int)tmpW < 0 ? 1 : 1 + (tmpW + strideW_ - 1) / strideW_;

  return outputH_ * outputW_;
}

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

  size_t batchSize = inputLayers_[0]->getOutputValue()->getHeight();
  size_t blockNum = getBlockNum();
  size_t blockSize = blockH_ * blockW_ * channels_;
  resetOutput(blockNum * batchSize, blockSize);
87 88
  // TODO(hedaoyuan): After completing the GPU version of ImageExpand,
  // refactor the following code.
Z
zhangjinchao01 已提交
89 90 91 92 93
  Argument& out = getOutput();
  MatrixPtr outV = getOutputValue();

  MatrixPtr input = getPrev(0)->getOutputValue();
  Matrix::resizeOrCreate(outVTrans_, blockSize, blockNum, false, useGpu_);
94 95
  ICpuGpuVector::resizeOrCreate(
      out.sequenceStartPositions, batchSize + 1, false);
Z
zhangjinchao01 已提交
96 97 98 99
  IVector::resizeOrCreate(out.cpuSequenceDims, 2 * batchSize, false);
  int* start = out.sequenceStartPositions->getMutableData(false);
  int* dims = out.cpuSequenceDims->getData();
  for (size_t i = 0; i < batchSize; i++) {
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
    if (useGpu_) {
      outVTrans_->zeroMem();
      /* expand each block as one row */
      MatrixPtr inputTmp =
          Matrix::create(input->getData() + i * input->getWidth(),
                         1,
                         input->getWidth(),
                         false,
                         useGpu_);
      outVTrans_->convExpand(*inputTmp,
                             imgSizeH_,
                             imgSizeW_,
                             channels_,
                             blockH_,
                             blockW_,
                             strideH_,
                             strideW_,
                             paddingH_,
                             paddingW_,
                             outputH_,
                             outputW_);
      MatrixPtr outVTmp =
          Matrix::create(outV->getData() + i * blockNum * blockSize,
                         blockNum,
                         blockSize,
                         false,
                         useGpu_);
      outVTrans_->transpose(outVTmp, false);
    }
Z
zhangjinchao01 已提交
129 130 131 132 133
    start[i] = i * blockNum;
    dims[2 * i] = outputH_;
    dims[2 * i + 1] = outputW_;
  }
  start[batchSize] = batchSize * blockNum;
134
  if (!useGpu_) {
H
hedaoyuan 已提交
135 136
    inputShape_ = TensorShape({batchSize, channels_, imgSizeH_, imgSizeW_});
    outputShape_ = TensorShape({batchSize, blockNum, blockSize});
137 138
    BufferArgs inputs;
    BufferArgs outputs;
H
hedaoyuan 已提交
139 140
    inputs.addArg(*getInputValue(0), inputShape_);
    outputs.addArg(*getOutputValue(), outputShape_, ASSIGN_TO);
141 142
    forward_[0]->calc(inputs, outputs);
  }
Z
zhangjinchao01 已提交
143 144 145 146 147 148 149 150 151 152 153
}

void BlockExpandLayer::backward(const UpdateCallback& callback) {
  size_t blockNum = outputH_ * outputW_;
  size_t blockSize = blockH_ * blockW_ * channels_;
  /* Calculate the input layers error */
  MatrixPtr preGrad = inputLayers_[0]->getOutputGrad();
  if (!preGrad) {
    return;
  }

H
hedaoyuan 已提交
154 155 156 157
  if (useGpu_) {
    MatrixPtr grad = getOutputGrad();
    MatrixPtr gradTrans = Matrix::create(blockSize, blockNum, false, useGpu_);
    size_t batchSize = preGrad->getHeight();
Z
zhangjinchao01 已提交
158

H
hedaoyuan 已提交
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
    CHECK_EQ(batchSize * blockNum, grad->getHeight());
    CHECK_EQ(blockSize, grad->getWidth());

    for (size_t i = 0; i < batchSize; i++) {
      MatrixPtr gradTmp =
          Matrix::create(grad->getData() + i * blockNum * blockSize,
                         blockNum,
                         blockSize,
                         false,
                         useGpu_);
      gradTmp->transpose(gradTrans, false);
      MatrixPtr preGradTmp =
          Matrix::create(preGrad->getData() + i * preGrad->getWidth(),
                         1,
                         preGrad->getWidth(),
                         false,
                         useGpu_);
      preGradTmp->convShrink(*gradTrans,
                             imgSizeH_,
                             imgSizeW_,
                             channels_,
                             blockH_,
                             blockW_,
                             strideH_,
                             strideW_,
                             paddingH_,
                             paddingW_,
                             outputH_,
                             outputW_,
                             1.0,
                             1.0);
    }
  } else {
    BufferArgs inputs;
    BufferArgs outputs;
    inputs.addArg(*getOutputGrad(), outputShape_);
    outputs.addArg(*getInputGrad(0), inputShape_, ADD_TO);
    backward_[0]->calc(inputs, outputs);
Z
zhangjinchao01 已提交
197 198 199 200
  }
}

}  // namespace paddle