ConvBaseLayer.cpp 4.7 KB
Newer Older
Z
zhangjinchao01 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/* Copyright (c) 2016 Baidu, Inc. 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 "paddle/utils/Logging.h"
#include "ConvBaseLayer.h"
17
#include "paddle/math/MathUtils.h"
Z
zhangjinchao01 已提交
18 19 20 21 22 23
namespace paddle {

bool ConvBaseLayer::init(const LayerMap& layerMap,
                         const ParameterMap& parameterMap) {
  /* Initialize the basic parent class */
  Layer::init(layerMap, parameterMap);
24 25
  isDeconv_ = (config_.type() == "exconv" || config_.type() == "cudnn_conv")
              ? false : true;
26

Z
zhangjinchao01 已提交
27 28 29 30 31 32 33 34 35 36 37 38 39
  /* Initialize the convolutional layer parameter */
  numFilters_ = config_.num_filters();
  sharedBiases_ = config_.shared_biases();
  for (auto& inputConfig : config_.inputs()) {
    const ConvConfig& conf = inputConfig.conv_conf();
    padding_.push_back(conf.padding());
    stride_.push_back(conf.stride());
    filterSize_.push_back(conf.filter_size());
    paddingY_.push_back(conf.padding_y());
    strideY_.push_back(conf.stride_y());
    filterSizeY_.push_back(conf.filter_size_y());
    filterPixels_.push_back(filterSize_.back() * filterSizeY_.back());
    channels_.push_back(conf.channels());
L
Luo Tao 已提交
40 41
    imgSizeH_.push_back(conf.has_img_size_y() ? conf.img_size_y() :
                        conf.img_size());
42
    imgSizeW_.push_back(conf.img_size());
Z
zhangjinchao01 已提交
43 44
    groups_.push_back(conf.groups());
    filterChannels_.push_back(conf.filter_channels());
L
Luo Tao 已提交
45 46
    outputH_.push_back(conf.has_output_y() ? conf.output_y() :
                       conf.output_x());
47
    outputW_.push_back(conf.output_x());
Z
zhangjinchao01 已提交
48 49
  }

50 51 52 53 54 55 56 57 58 59 60 61
  CHECK(inputLayers_.size() == parameters_.size());
  for (size_t i = 0; i < inputLayers_.size(); i++) {
    size_t height, width;
    height = filterPixels_[i] * filterChannels_[i];
    width = (!isDeconv_) ? numFilters_ : channels_[i];

    // create a new weight
    CHECK_EQ(parameters_[i]->getSize(), width * height);
    Weight* w = new Weight(height, width, parameters_[i]);
    weights_.emplace_back(w);
  }

Z
zhangjinchao01 已提交
62
  /* initialize the biases_ */
63
  if (biasParameter_.get()) {
Z
zhangjinchao01 已提交
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
    if (sharedBiases_) {
      CHECK_EQ((size_t)numFilters_, biasParameter_->getSize());
      biases_ =
          std::unique_ptr<Weight>(new Weight(numFilters_, 1, biasParameter_));
    } else {
      biases_ =
          std::unique_ptr<Weight>(new Weight(getSize(), 1, biasParameter_));
    }
  }

  // default caffe model
  caffeMode_ = true;

  return true;
}

80 81 82 83 84 85 86 87 88 89
size_t ConvBaseLayer::calOutputSize() {
  auto clearAndReserve = [this](IntV* vec) {
    vec->clear();
    vec->reserve(this->inputLayers_.size());
  };
  clearAndReserve(&imgSizeH_);
  clearAndReserve(&imgSizeW_);
  clearAndReserve(&outputH_);
  clearAndReserve(&outputW_);
  size_t layerSize = 0;
90

91
  auto setLayerSize = [&](IntV& inH, IntV& inW, IntV& outH, IntV& outW) {
92
    for (size_t i = 0; i < inputLayers_.size(); i++) {
93 94
       inH.push_back(inputLayers_[i]->getOutput().getFrameHeight());
       inW.push_back(inputLayers_[i]->getOutput().getFrameWidth());
L
Luo Tao 已提交
95
       const ConvConfig& conf = config_.inputs(i).conv_conf();
96 97
       if (isDeconv_) {
         if (inH[i] == 0)
L
Luo Tao 已提交
98
           inH[i] = conf.has_output_y() ? conf.output_y() : conf.output_x();
99
         if (inW[i] == 0)
L
Luo Tao 已提交
100
           inW[i] = conf.output_x();
101
         outH.push_back(
102 103
             imageSize(inH[i], filterSizeY_[i], paddingY_[i], strideY_[i],
                       caffeMode_));
104
         outW.push_back(
105 106
             imageSize(inW[i], filterSize_[i], padding_[i], stride_[i],
                       caffeMode_));
107 108
       } else {
         if (inH[i] == 0)
L
Luo Tao 已提交
109
           inH[i] = conf.has_img_size_y() ? conf.img_size_y() : conf.img_size();
110
         if (inW[i] == 0)
L
Luo Tao 已提交
111
           inW[i] = conf.img_size();
112
         outH.push_back(
113 114
             outputSize(inH[i], filterSizeY_[i], paddingY_[i], strideY_[i],
                        caffeMode_));
115
         outW.push_back(
116 117
             outputSize(inW[i], filterSize_[i], padding_[i], stride_[i],
                        caffeMode_));
118
       }
119 120
       CHECK_EQ(outH[i], outH[0]);
       CHECK_EQ(outW[i], outW[0]);
121
    }
122 123 124 125 126 127 128
    getOutput().setFrameHeight(outH[0]);
    getOutput().setFrameWidth(outW[0]);
    layerSize = outH[0] * outW[0] * size_t(numFilters_);
  };

  if (isDeconv_) {
    setLayerSize(outputH_, outputW_, imgSizeH_, imgSizeW_);
129
  } else {
130
    setLayerSize(imgSizeH_, imgSizeW_, outputH_, outputW_);
131
  }
132

133
  return layerSize;
134 135
}

Z
zhangjinchao01 已提交
136
}  // namespace paddle