FeatureMapExpandLayer.cpp 3.7 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 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80

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 {
private:
  int numFilters_;

public:
  explicit FeatureMapExpandLayer(const LayerConfig& config) : Layer(config) {}

  ~FeatureMapExpandLayer() {}

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

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

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();
  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;
    for (size_t i = 0; i < batchSize; i++) {
      MatrixPtr outVTmp =
          Matrix::create(outputV->getData() + i * imgSize * numFilters_,
81 82 83 84 85 86
                         numFilters_,
                         imgSize,
                         false,
                         useGpu_);
      MatrixPtr inVTmp = Matrix::create(
          inputV->getData() + i * imgSize, 1, imgSize, false, useGpu_);
Z
zhangjinchao01 已提交
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
      outVTmp->addRowVector(*inVTmp);
    }
  }
  /* activation */ {
    REGISTER_TIMER_INFO("FwAtvTimer", getName().c_str());
    forwardActivation();
  }
}

void FeatureMapExpandLayer::backward(const UpdateCallback& callback) {
  MatrixPtr inGrad = getInputGrad(0);
  MatrixPtr outGrad = getOutputGrad();
  size_t batchSize = getInput(0).getBatchSize();
  int imgSize = inGrad->getWidth();
  {
    AsyncGpuBlock asyncGpuBlock;
    for (size_t i = 0; i < batchSize; i++) {
      MatrixPtr outGradTmp =
          Matrix::create(outGrad->getData() + i * imgSize * numFilters_,
106 107 108 109 110 111
                         numFilters_,
                         imgSize,
                         false,
                         useGpu_);
      MatrixPtr inGradTmp = Matrix::create(
          inGrad->getData() + i * imgSize, 1, imgSize, false, useGpu_);
Z
zhangjinchao01 已提交
112 113 114 115 116 117 118 119 120 121
      inGradTmp->collectBias(*outGradTmp, 1);
    }
  }
  /* Do derivation */ {
    REGISTER_TIMER_INFO("BpAvtTimer", getName().c_str());
    backwardActivation();
  }
}

}  // namespace paddle.