FeatureMapExpandLayer.cpp 4.9 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 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40

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 "Layer.h"
#include "paddle/math/Matrix.h"
#include "paddle/utils/Stat.h"

namespace paddle {

/**
 * @brief A layer for expanding a batch of images to feature maps.
 * Each data of the input is a 2 dimensional matrix. Each element of the matrix
 * is replicated num_filters times to create a feature map with num_filters
 * channels.
 * - Input: Input one should be dense image data.
 * - Output: expanded fature maps.
 * \f[
 *  y.row[i] = x.row[i \mod x.width], i = 0,1,..., (x.width * num\_filters - 1)
 * \f]
 * For example, num_filters = 4:
 * @code
 *   x = [a1,a2;
 *        b1,b2]
 *   y = [a1, a2, a1, a2, a1, a2, a1, a2;
 *        b1, b2, b1, b2, b1, b2, b1, b2;]
 * @endcode
 */

class FeatureMapExpandLayer : public Layer {
W
Wu Yi 已提交
41
 private:
Z
zhangjinchao01 已提交
42
  int numFilters_;
X
xuwei06 已提交
43
  bool asRowVector_;
Z
zhangjinchao01 已提交
44

W
Wu Yi 已提交
45
 public:
Z
zhangjinchao01 已提交
46 47 48 49
  explicit FeatureMapExpandLayer(const LayerConfig& config) : Layer(config) {}

  ~FeatureMapExpandLayer() {}

Y
Yu Yang 已提交
50 51
  bool init(const LayerMap& layerMap,
            const ParameterMap& parameterMap) override;
Z
zhangjinchao01 已提交
52

Y
Yu Yang 已提交
53 54
  void forward(PassType passType) override;
  void backward(const UpdateCallback& callback = nullptr) override;
Z
zhangjinchao01 已提交
55 56 57 58 59 60 61 62 63 64 65
};

REGISTER_LAYER(featmap_expand, FeatureMapExpandLayer);

bool FeatureMapExpandLayer::init(const LayerMap& layerMap,
                                 const ParameterMap& parameterMap) {
  /* Initialize the basic parent class */
  Layer::init(layerMap, parameterMap);

  CHECK_EQ(inputLayers_.size(), 1UL);
  numFilters_ = config_.num_filters();
X
xuwei06 已提交
66
  asRowVector_ = config_.user_arg() != "as_col_vec";
Z
zhangjinchao01 已提交
67 68 69 70 71 72 73 74 75 76 77 78 79 80
  return true;
}

void FeatureMapExpandLayer::forward(PassType passType) {
  Layer::forward(passType);
  MatrixPtr inputV = getInputValue(0);
  size_t batchSize = getInput(0).getBatchSize();
  int imgSize = inputV->getWidth();
  resetOutput(batchSize, imgSize * numFilters_);

  MatrixPtr outputV = getOutputValue();

  {
    AsyncGpuBlock asyncGpuBlock;
X
xuwei06 已提交
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
    if (asRowVector_) {
      for (size_t i = 0; i < batchSize; i++) {
        MatrixPtr outVTmp =
            Matrix::create(outputV->getData() + i * imgSize * numFilters_,
                           numFilters_,
                           imgSize,
                           false,
                           useGpu_);
        MatrixPtr inVTmp = Matrix::create(
            inputV->getData() + i * imgSize, 1, imgSize, false, useGpu_);
        outVTmp->addRowVector(*inVTmp);
      }
    } else {
      for (size_t i = 0; i < batchSize; i++) {
        MatrixPtr outVTmp =
            Matrix::create(outputV->getData() + i * imgSize * numFilters_,
                           imgSize,
                           numFilters_,
                           false,
                           useGpu_);
        MatrixPtr inVTmp = Matrix::create(
            inputV->getData() + i * imgSize, imgSize, 1, false, useGpu_);
        outVTmp->addColVector(*inVTmp);
      }
Z
zhangjinchao01 已提交
105 106 107 108 109 110 111 112 113 114
    }
  }
  /* activation */ {
    REGISTER_TIMER_INFO("FwAtvTimer", getName().c_str());
    forwardActivation();
  }
}

void FeatureMapExpandLayer::backward(const UpdateCallback& callback) {
  MatrixPtr inGrad = getInputGrad(0);
H
Haonan 已提交
115 116 117
  if (NULL == inGrad) {
    return;
  }
Z
zhangjinchao01 已提交
118 119 120
  MatrixPtr outGrad = getOutputGrad();
  size_t batchSize = getInput(0).getBatchSize();
  int imgSize = inGrad->getWidth();
X
xuwei06 已提交
121 122 123 124
  /* Do activation */ {
    REGISTER_TIMER_INFO("BpAvtTimer", getName().c_str());
    backwardActivation();
  }
Z
zhangjinchao01 已提交
125 126
  {
    AsyncGpuBlock asyncGpuBlock;
X
xuwei06 已提交
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
    if (asRowVector_) {
      for (size_t i = 0; i < batchSize; i++) {
        MatrixPtr outGradTmp =
            Matrix::create(outGrad->getData() + i * imgSize * numFilters_,
                           numFilters_,
                           imgSize,
                           false,
                           useGpu_);
        MatrixPtr inGradTmp = Matrix::create(
            inGrad->getData() + i * imgSize, 1, imgSize, false, useGpu_);
        inGradTmp->collectBias(*outGradTmp, 1);
      }
    } else {
      for (size_t i = 0; i < batchSize; i++) {
        MatrixPtr outGradTmp =
            Matrix::create(outGrad->getData() + i * imgSize * numFilters_,
                           imgSize,
                           numFilters_,
                           false,
                           useGpu_);
        MatrixPtr inGradTmp = Matrix::create(
            inGrad->getData() + i * imgSize, imgSize, 1, false, useGpu_);
        inGradTmp->sumRows(*outGradTmp, 1, 1);
      }
Z
zhangjinchao01 已提交
151 152 153 154 155
    }
  }
}

}  // namespace paddle.