ConvBaseLayer.h 4.7 KB
Newer Older
Z
zhangjinchao01 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/* 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. */


#pragma once

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

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

class ConvBaseLayer : public Layer {
protected:
  typedef std::vector<int> IntV;

31 32 33
  /// True if it's convolution layer, false if it's deconv layer
  bool isConv_;

Z
zhangjinchao01 已提交
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
  /// 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_;
  /// 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_;
50 51 52 53
  /// The spatial dimensions of input feature map height.
  IntV imgSizeH_;
  /// The spatial dimensions of input feature map width.
  IntV imgSizeW_;
Z
zhangjinchao01 已提交
54 55 56 57
  /// filterPixels_ = filterSizeX_ * filterSizeY_.
  IntV filterPixels_;
  /// filterChannels_ = channels_/groups_.
  IntV filterChannels_;
58 59 60 61
  /// The spatial dimensions of output feature map height.
  IntV outputH_;
  /// The spatial dimensions of output feature map width.
  IntV outputW_;
Z
zhangjinchao01 已提交
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
  /// 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_;

81 82 83 84 85 86 87
  /*The expandInput_ and transOutValue_ are used for CPU expand conv calc*/
  /// Expand one sample at a time. shape:
  /// (numChannels * filterPixels_, outputSizeH * outputSizeW)
  MatrixPtr expandInput_;
  /// The transpose of output, which is an auxiliary matrix.
  MatrixPtr transOutValue_;

Z
zhangjinchao01 已提交
88 89 90 91 92
public:
  explicit ConvBaseLayer(const LayerConfig& config) : Layer(config) {}

  virtual bool init(const LayerMap& layerMap, const ParameterMap& parameterMap);

93 94 95 96 97 98 99
  /**
   * 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 已提交
100
  Weight& getWeight(int idx) { return *weights_[idx]; }
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157

  /**
   * Calculate output size based on caffeMode_.
   * - input(+padding): 0123456789
   * - imageSize(+padding) = 10;
   * - filterSize = 3;
   * - stride = 2;
   * - caffeMode_ is true:
       - output: (012), (234), (456), (678)
       - outputSize = 4;
   * - caffeMode_ is false:
   *   - output: (012), (234), (456), (678), (9)
   *   - outputSize = 5;
   */
  int outputSize(int imageSize, int filterSize, int padding, int stride) {
    int outputSize;
    if (!caffeMode_) {
     outputSize =
          (imageSize - filterSize + 2 * padding + stride - 1) / stride + 1;
    } else {
      outputSize = (imageSize - filterSize + 2 * padding) / stride + 1;
    }
    CHECK_GE(outputSize, 1);
    return outputSize;
  }

  int imageSize(int outputSize, int filterSize, int padding, int stride) {
    int imageSize;
    if (!caffeMode_) {
     imageSize =
         (outputSize - 1) * stride + filterSize - 2 * padding - stride + 1;
    } else {
     imageSize = (outputSize - 1) * stride + filterSize - 2 * padding;
    }
    CHECK_GE(imageSize, 1);
    return imageSize;
  }

  /**
   * Create or resize expandInput_.
   */
  void resetExpandInput(size_t height, size_t width);

  /**
   * Create or resize transOutValue_.
   */
  void resetConvOutput(size_t batchSize, int inIdx);

  /**
   * Add shared bias.
   */
  void addSharedBias();

  /**
   * Add unshared bias.
   */
  void addUnsharedBias();
Z
zhangjinchao01 已提交
158 159 160
};

}  // namespace paddle