ConvBaseLayer.h 3.2 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 16 17

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. */

#pragma once

#include "Layer.h"
18
#include "paddle/math/MathUtils.h"
Z
zhangjinchao01 已提交
19 20 21 22 23 24 25 26
namespace paddle {

/**
 * @brief A Base Convolution Layer, which convolves the input image
 * with learned filters and (optionally) adds biases.
 */

class ConvBaseLayer : public Layer {
W
Wu Yi 已提交
27
 protected:
Z
zhangjinchao01 已提交
28 29
  typedef std::vector<int> IntV;

30 31
  /// True if it's deconv layer, false if it's convolution layer
  bool isDeconv_;
32

Z
zhangjinchao01 已提交
33 34 35 36 37 38 39 40 41 42
  /// The number of filters.
  int numFilters_;
  /// The x dimension of the padding.
  IntV padding_;
  /// The y dimension of the padding.
  IntV paddingY_;
  /// The x dimension of the stride.
  IntV stride_;
  /// The y dimension of the stride.
  IntV strideY_;
43 44 45 46
  /// The x dimension of the dilation.
  IntV dilation_;
  /// The y dimension of the dilation.
  IntV dilationY_;
Z
zhangjinchao01 已提交
47 48 49 50 51 52
  /// The x dimension of a filter kernel.
  IntV filterSize_;
  /// The y dimension of a filter kernel.
  IntV filterSizeY_;
  /// The spatial dimensions of the convolution input.
  IntV channels_;
53 54 55 56
  /// The spatial dimensions of input feature map height.
  IntV imgSizeH_;
  /// The spatial dimensions of input feature map width.
  IntV imgSizeW_;
Z
zhangjinchao01 已提交
57 58 59 60
  /// filterPixels_ = filterSizeX_ * filterSizeY_.
  IntV filterPixels_;
  /// filterChannels_ = channels_/groups_.
  IntV filterChannels_;
61 62 63 64
  /// The spatial dimensions of output feature map height.
  IntV outputH_;
  /// The spatial dimensions of output feature map width.
  IntV outputW_;
65 66 67 68 69 70 71

  IntV outputD_;
  IntV imgSizeD_;
  IntV filterSizeZ_;
  IntV strideZ_;
  IntV paddingZ_;

Z
zhangjinchao01 已提交
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
  /// Group size, refer to grouped convolution in
  /// Alex Krizhevsky's paper: when group=2, the first half of the
  /// filters are only connected to the first half of the input channels,
  /// and the second half only connected to the second half.
  IntV groups_;
  /// Whether the bias is shared for feature in each channel.
  bool sharedBiases_;

  /// shape of weight: (numChannels * filterPixels_, numFilters)
  WeightList weights_;
  /// If shared_biases is false shape of bias: (numFilters_, 1)
  /// If shared_biases is ture shape of bias:
  /// (numFilters_ * outputX * outputY, 1)
  std::unique_ptr<Weight> biases_;

  /// True by default. The only difference is the calculation
  /// of output size.
  bool caffeMode_;

W
Wu Yi 已提交
91
 public:
Z
zhangjinchao01 已提交
92 93
  explicit ConvBaseLayer(const LayerConfig& config) : Layer(config) {}

Y
Yu Yang 已提交
94 95
  bool init(const LayerMap& layerMap,
            const ParameterMap& parameterMap) override;
Z
zhangjinchao01 已提交
96

97 98 99 100 101 102 103
  /**
   * imgSizeH_ and imgSizeW_ will be set according to the previous input layers
   * in this function. Then it will calculate outputH_ and outputW_ and set them
   * into output argument.
   */
  virtual size_t calOutputSize();

Z
zhangjinchao01 已提交
104 105 106 107
  Weight& getWeight(int idx) { return *weights_[idx]; }
};

}  // namespace paddle