ConvBaseLayer.cpp 4.4 KB
Newer Older
1
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved.
Z
zhangjinchao01 已提交
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 "ConvBaseLayer.h"
X
Xin Pan 已提交
16
#include "paddle/legacy/math/MathUtils.h"
Y
Yu Yang 已提交
17
#include "paddle/utils/Logging.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);
C
chengduoZH 已提交
24
  isDeconv_ = (config_.type() == "exconv" || config_.type() == "cudnn_conv")
C
chengduoZH 已提交
25 26
                  ? false
                  : true;
27

Z
zhangjinchao01 已提交
28 29 30 31 32 33 34
  /* 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());
35
    dilation_.push_back(conf.dilation());
Z
zhangjinchao01 已提交
36 37 38
    filterSize_.push_back(conf.filter_size());
    paddingY_.push_back(conf.padding_y());
    strideY_.push_back(conf.stride_y());
39
    dilationY_.push_back(conf.dilation_y());
Z
zhangjinchao01 已提交
40 41
    filterSizeY_.push_back(conf.filter_size_y());
    channels_.push_back(conf.channels());
L
Luo Tao 已提交
42 43
    imgSizeH_.push_back(conf.has_img_size_y() ? conf.img_size_y()
                                              : conf.img_size());
44
    imgSizeW_.push_back(conf.img_size());
Z
zhangjinchao01 已提交
45 46
    groups_.push_back(conf.groups());
    filterChannels_.push_back(conf.filter_channels());
L
Luo Tao 已提交
47
    outputH_.push_back(conf.has_output_y() ? conf.output_y() : conf.output_x());
48
    outputW_.push_back(conf.output_x());
49 50 51 52 53 54

    paddingZ_.push_back(conf.padding_z());
    strideZ_.push_back(conf.stride_z());
    filterSizeZ_.push_back(conf.filter_size_z());
    imgSizeD_.push_back(conf.img_size_z());
    outputD_.push_back(conf.output_z());
C
chengduoZH 已提交
55 56
    filterPixels_.push_back(filterSize_.back() * filterSizeY_.back() *
                            filterSizeZ_.back());
Z
zhangjinchao01 已提交
57 58
  }

59 60
  CHECK(inputLayers_.size() == parameters_.size());

C
chengduoZH 已提交
61 62
  // create new weights_ in derived class
  // create new biases_ in derived class
Z
zhangjinchao01 已提交
63 64 65 66 67 68 69

  // default caffe model
  caffeMode_ = true;

  return true;
}

70 71 72 73 74 75 76 77 78 79
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;
80

81
  auto setLayerSize = [&](IntV& inH, IntV& inW, IntV& outH, IntV& outW) {
82 83
    size_t filterSizeY;
    size_t filterSize;
84
    for (size_t i = 0; i < inputLayers_.size(); i++) {
85 86
      filterSizeY = (filterSizeY_[i] - 1) * dilationY_[i] + 1;
      filterSize = (filterSize_[i] - 1) * dilation_[i] + 1;
87 88
      inH.push_back(inputLayers_[i]->getOutput().getFrameHeight());
      inW.push_back(inputLayers_[i]->getOutput().getFrameWidth());
L
Luo Tao 已提交
89
      const ConvConfig& conf = config_.inputs(i).conv_conf();
90
      if (isDeconv_) {
L
Luo Tao 已提交
91 92 93
        if (inH[i] == 0)
          inH[i] = conf.has_output_y() ? conf.output_y() : conf.output_x();
        if (inW[i] == 0) inW[i] = conf.output_x();
94
        outH.push_back(imageSize(
95 96 97
            inH[i], filterSizeY, paddingY_[i], strideY_[i], caffeMode_));
        outW.push_back(
            imageSize(inW[i], filterSize, padding_[i], stride_[i], caffeMode_));
98
      } else {
L
Luo Tao 已提交
99 100 101
        if (inH[i] == 0)
          inH[i] = conf.has_img_size_y() ? conf.img_size_y() : conf.img_size();
        if (inW[i] == 0) inW[i] = conf.img_size();
102
        outH.push_back(outputSize(
103
            inH[i], filterSizeY, paddingY_[i], strideY_[i], caffeMode_));
104
        outW.push_back(outputSize(
105
            inW[i], filterSize, padding_[i], stride_[i], caffeMode_));
106 107 108
      }
      CHECK_EQ(outH[i], outH[0]);
      CHECK_EQ(outW[i], outW[0]);
109
    }
110 111 112 113 114
    getOutput().setFrameHeight(outH[0]);
    getOutput().setFrameWidth(outW[0]);
    layerSize = outH[0] * outW[0] * size_t(numFilters_);
  };

115
  setLayerSize(imgSizeH_, imgSizeW_, outputH_, outputW_);
116

117
  return layerSize;
118 119
}

Z
zhangjinchao01 已提交
120
}  // namespace paddle