UpsampleLayer.cpp 3.5 KB
Newer Older
X
xzl 已提交
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
/* 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 "UpsampleLayer.h"
#include "iostream"

namespace paddle {

REGISTER_LAYER(upsample, UpsampleLayer);

size_t UpsampleLayer::getOutputSize() {
  if (upsampleSize_ == 0) {
    upsampleSize_ = imgSize_ * scale_ - static_cast<int>(padOutX_);
    upsampleSizeY_ = imgSizeY_ * scaleY_ - static_cast<int>(padOutY_);
  }
  return upsampleSize_ * upsampleSizeY_ * channels_;
}

bool UpsampleLayer::init(const LayerMap& layerMap,
                         const ParameterMap& parameterMap) {
  Layer::init(layerMap, parameterMap);
X
xzl 已提交
33

X
xzl 已提交
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
  CHECK_EQ(inputLayers_.size(), 2U);
  CHECK_EQ(config_.inputs_size(), 2);
  const auto& conf = config_.inputs(0).upsample_conf();
  const auto& img_conf = conf.image_conf();

  imgSizeY_ =
      img_conf.has_img_size_y() ? img_conf.img_size_y() : img_conf.img_size();
  imgSize_ = img_conf.img_size();
  channels_ = img_conf.channels();

  CHECK((conf.has_upsample_size()) || (conf.has_scale()))
      << "scale or upsample_size is required.";

  if (conf.has_upsample_size()) {
    upsampleSize_ = conf.upsample_size();
    upsampleSizeY_ = upsampleSize_;
    if (conf.has_upsample_size_y()) {
      upsampleSizeY_ = conf.upsample_size_y();
    }
  } else {
    if (!conf.has_scale_y()) {
      scale_ = scaleY_ = conf.scale_y();
      CHECK_GT(static_cast<int>(scale_), 1);
    } else {
      scale_ = conf.scale();
      scaleY_ = conf.scale_y();
    }
    padOutX_ = conf.pad_out_x();
    padOutY_ = conf.pad_out_y();
    CHECK(!padOutX_ || scale_ == 2)
        << "Output height padding compensation requires scale_ == 2";
    CHECK(!padOutY_ || scaleY_ == 2)
        << "Output width padding compensation requires scaleY_ == 2";
    upsampleSize_ = upsampleSizeY_ = 0;
  }
  return true;
}

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

  MatrixPtr input = getInputValue(0);
  MatrixPtr mask = inputLayers_[1]->getOutput("mask").value;

  size_t batchSize = input->getHeight();
  size_t outSize = getOutputSize();

  CHECK_EQ(input->getWidth(), mask->getWidth());
  CHECK_EQ(mask->getHeight(), batchSize);
  resetOutput(batchSize, outSize);

  MatrixPtr output = getOutputValue();
  output->upsampleForward(*input,
                          *mask,
                          imgSize_,
                          imgSizeY_,
                          channels_,
                          upsampleSize_,
                          upsampleSizeY_);
}

void UpsampleLayer::backward(const UpdateCallback& callback) {
  MatrixPtr mask = inputLayers_[1]->getOutput("mask").value;
  MatrixPtr inputGrad = getInputGrad(0);
  MatrixPtr outputGrad = getOutputGrad();
  inputGrad->upsampleBackward(*outputGrad,
                              *mask,
                              imgSize_,
                              imgSizeY_,
                              channels_,
                              upsampleSize_,
                              upsampleSizeY_);
}

}  // namespace paddle