NormLayer.h 2.9 KB
Newer Older
1
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve.
Z
zhangjinchao01 已提交
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19

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 <vector>
#include "Layer.h"
#include "NormLayer.h"
Y
Yu Yang 已提交
20
#include "paddle/math/Matrix.h"
Z
zhangjinchao01 已提交
21 22 23 24

namespace paddle {

/**
Q
qijun 已提交
25 26 27
 * @brief Basic parent layer of normalization
 *
 * @note Normalize the input in local region
Z
zhangjinchao01 已提交
28 29 30 31 32
 */
class NormLayer : public Layer {
public:
  explicit NormLayer(const LayerConfig& config) : Layer(config) {}

Y
Yu Yang 已提交
33 34
  bool init(const LayerMap& layerMap,
            const ParameterMap& parameterMap) override {
Z
zhangjinchao01 已提交
35 36 37 38
    Layer::init(layerMap, parameterMap);
    return true;
  }

Q
qijun 已提交
39 40 41
  /**
   * @brief create norm layer by norm_type
   */
Z
zhangjinchao01 已提交
42 43 44 45 46
  static Layer* create(const LayerConfig& config);
};

/**
 * @brief response normalization within feature maps
47 48
 * namely normalize in independent channel
 * When code refactoring, we delete the original implementation.
Z
zhangjinchao01 已提交
49 50 51 52
 * Need to implement in the futrue.
 */
class ResponseNormLayer : public NormLayer {
protected:
L
Luo Tao 已提交
53
  size_t channels_, size_, outputX_, imgSize_, outputY_, imgSizeY_;
P
Peng Li 已提交
54
  real scale_, pow_;
Z
zhangjinchao01 已提交
55 56 57 58 59
  MatrixPtr denoms_;

public:
  explicit ResponseNormLayer(const LayerConfig& config) : NormLayer(config) {}

Y
Yu Yang 已提交
60 61 62 63
  bool init(const LayerMap& layerMap,
            const ParameterMap& parameterMap) override;
  void forward(PassType passType) override { LOG(FATAL) << "Not implemented"; }
  void backward(const UpdateCallback& callback = nullptr) override {
Z
zhangjinchao01 已提交
64 65 66 67
    LOG(FATAL) << "Not implemented";
  }
};

68
/**
G
gaoyuan 已提交
69 70
 * This layer applys normalization across the channels of each sample to a
 * conv layer's output, and scales the output by a group of trainable factors
G
gaoyuan 已提交
71
 * whose dimensions equal to the number of channels.
G
gaoyuan 已提交
72
 * - Input: One and only one input layer are accepted.
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
 * - Output: The normalized data of the input data.
 * Reference:
 *    Wei Liu, Dragomir Anguelov, Dumitru Erhan, Christian Szegedy, Scott Reed,
 *    Cheng-Yang Fu, Alexander C. Berg. SSD: Single Shot MultiBox Detector
 */
class CrossChannelNormLayer : public NormLayer {
public:
  explicit CrossChannelNormLayer(const LayerConfig& config)
      : NormLayer(config) {}
  bool init(const LayerMap& layerMap, const ParameterMap& parameterMap);

  void forward(PassType passType);
  void backward(const UpdateCallback& callback);

protected:
  size_t channels_;
  std::unique_ptr<Weight> scale_;
  MatrixPtr scaleDiff_;
  MatrixPtr normBuffer_;
  MatrixPtr dataBuffer_;
  MatrixPtr channelBuffer_;
  MatrixPtr spatialBuffer_;
  MatrixPtr sampleBuffer_;
};

Z
zhangjinchao01 已提交
98
}  // namespace paddle