BilinearInterpLayer.cpp 3.2 KB
Newer Older
1
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved.
L
liaogang 已提交
2 3 4 5 6 7 8 9 10 11 12 13 14 15

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 "BilinearInterpLayer.h"
X
Xin Pan 已提交
16 17
#include "paddle/legacy/utils/Logging.h"
#include "paddle/legacy/utils/Stat.h"
L
liaogang 已提交
18 19 20 21 22

namespace paddle {

REGISTER_LAYER(bilinear_interp, BilinearInterpLayer);

L
liaogang 已提交
23 24 25
size_t BilinearInterpLayer::getSize() {
  inImgH_ = inputLayers_[0]->getOutput().getFrameHeight();
  inImgW_ = inputLayers_[0]->getOutput().getFrameWidth();
L
liaogang 已提交
26 27 28

  const BilinearInterpConfig& conf = config_.inputs(0).bilinear_interp_conf();
  if (inImgH_ == 0) {
L
Luo Tao 已提交
29
    inImgH_ = conf.image_conf().img_size_y();
L
liaogang 已提交
30 31
  }
  if (inImgW_ == 0) {
L
Luo Tao 已提交
32
    inImgW_ = conf.image_conf().img_size();
L
liaogang 已提交
33 34 35 36
  }

  outImgH_ = conf.out_size_y();
  outImgW_ = conf.out_size_x();
L
Luo Tao 已提交
37
  numChannels_ = conf.image_conf().channels();
L
liaogang 已提交
38 39

  CHECK(outImgH_ > 0 && outImgW_ > 0);
L
liaogang 已提交
40
  CHECK(inImgH_ > 0 && inImgW_ > 0);
L
liaogang 已提交
41
  CHECK(numChannels_);
L
liaogang 已提交
42

43 44 45 46
  ratioH_ =
      (outImgH_ > 1) ? static_cast<real>(inImgH_ - 1) / (outImgH_ - 1) : 0.f;
  ratioW_ =
      (outImgW_ > 1) ? static_cast<real>(inImgW_ - 1) / (outImgW_ - 1) : 0.f;
L
liaogang 已提交
47

L
liaogang 已提交
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
  getOutput().setFrameHeight(outImgH_);
  getOutput().setFrameWidth(outImgW_);
  return outImgH_ * outImgW_ * numChannels_;
}

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

  CHECK_EQ(1, config_.inputs_size());

  return true;
}

void BilinearInterpLayer::forward(PassType passType) {
  Layer::forward(passType);
L
liaogang 已提交
65

L
liaogang 已提交
66
  size_t batchSize = getInput(0).getBatchSize();
L
liaogang 已提交
67
  size_t size = getSize();
L
liaogang 已提交
68 69 70 71 72 73 74 75 76
  {
    REGISTER_TIMER_INFO("FwResetTimer", getName().c_str());
    resetOutput(batchSize, size);
  }

  MatrixPtr inV = getInputValue(0);
  MatrixPtr outV = getOutputValue();
  {
    REGISTER_TIMER_INFO("FwBilinearInterpTimer", getName().c_str());
77 78 79 80 81 82 83 84
    outV->bilinearForward(*inV,
                          inImgH_,
                          inImgW_,
                          outImgH_,
                          outImgW_,
                          numChannels_,
                          ratioH_,
                          ratioW_);
L
liaogang 已提交
85 86 87 88
  }
}

void BilinearInterpLayer::backward(const UpdateCallback& callback) {
89
  (void)callback;
L
liaogang 已提交
90 91 92 93 94 95

  MatrixPtr inputG = getInputGrad(0);
  MatrixPtr outG = getOutputGrad();
  {
    REGISTER_TIMER_INFO("BwBilinearInterpTimer", getName().c_str());
    if (inputG) {
96 97 98 99 100 101 102 103
      inputG->bilinearBackward(*outG,
                               outImgH_,
                               outImgW_,
                               inImgH_,
                               inImgW_,
                               numChannels_,
                               ratioH_,
                               ratioW_);
L
liaogang 已提交
104 105 106 107
    }
  }
}
}  // namespace paddle