提交 aa2cd2ce 编写于 作者: W wangyang59

Refactor ExpandConvTransLayer to share codes with ExpandConvLayer

上级 bda259bb
......@@ -21,6 +21,12 @@ bool ConvBaseLayer::init(const LayerMap& layerMap,
/* Initialize the basic parent class */
Layer::init(layerMap, parameterMap);
if (config_.type() == "exconv" || config_.type() == "cudnn_conv") {
isConv_ = true;
} else {
isConv_ = false;
}
/* Initialize the convolutional layer parameter */
numFilters_ = config_.num_filters();
sharedBiases_ = config_.shared_biases();
......@@ -88,6 +94,7 @@ size_t ConvBaseLayer::calOutputSize() {
getOutput().setFrameWidth(outputW_[0]);
layerSize = outputH_[0] * outputW_[0] * size_t(numFilters_);
return layerSize;
}
} // namespace paddle
......@@ -28,6 +28,9 @@ class ConvBaseLayer : public Layer {
protected:
typedef std::vector<int> IntV;
/// True if it's convolution layer, false if it's deconv layer
bool isConv_;
/// The number of filters.
int numFilters_;
/// The x dimension of the padding.
......@@ -75,6 +78,13 @@ protected:
/// of output size.
bool caffeMode_;
/*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_;
public:
explicit ConvBaseLayer(const LayerConfig& config) : Layer(config) {}
......@@ -88,6 +98,63 @@ public:
virtual size_t calOutputSize();
Weight& getWeight(int idx) { return *weights_[idx]; }
/**
* 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();
};
} // namespace paddle
......@@ -63,14 +63,6 @@ size_t ExpandConvLayer::getOutputSize() {
return layerSize;
}
void ExpandConvLayer::resetExpandInput(size_t height, size_t width) {
Matrix::resizeOrCreate(expandInput_, height, width, false, useGpu_);
}
void ExpandConvLayer::resetConvOutput(size_t batchSize, int inIdx) {
Matrix::resizeOrCreate(transOutValue_, batchSize * numFilters_, subN_[inIdx],
false, useGpu_);
}
void ExpandConvLayer::expandOneFrame(MatrixPtr image, size_t startIdx,
int inIdx) {
......@@ -135,17 +127,7 @@ void ExpandConvLayer::addSharedBias() {
transOutValue_->reshape(mapW, mapH);
transOutValue_->transpose(out, false); // false means no memory allocation
out->clear();
bias->clear();
}
void ExpandConvLayer::addUnsharedBias() {
MatrixPtr outValue = getOutputValue();
MatrixPtr bias =
Matrix::create(biases_->getW()->getData(), 1,
biases_->getW()->getElementCnt(), false, useGpu_);
outValue->addBias(*bias, 1.0f);
}
void ExpandConvLayer::forward(PassType passType) {
Layer::forward(passType);
......
......@@ -43,6 +43,7 @@ protected:
/// The transpose of output, which is an auxiliary matrix.
MatrixPtr transOutValue_;
public:
explicit ExpandConvLayer(const LayerConfig& config) : ConvBaseLayer(config) {}
......@@ -52,16 +53,6 @@ public:
size_t getOutputSize();
/**
* Create or resize expandInput_.
*/
void resetExpandInput(size_t height, size_t width);
/**
* Create or resize transOutValue_.
*/
void resetConvOutput(size_t batchSize, int inIdx);
/**
* Expand one input sample.
*/
......@@ -72,15 +63,7 @@ public:
*/
void expandFwdOnce(MatrixPtr image, int inIdx, int startIdx);
/**
* Add shared bias.
*/
void addSharedBias();
/**
* Add unshared bias.
*/
void addUnsharedBias();
void forward(PassType passType);
void bpropSharedBias(MatrixPtr biases, MatrixPtr v);
void bpropBiases(MatrixPtr v);
......
......@@ -29,14 +29,14 @@ REGISTER_LAYER(exconvt, ExpandConvTransLayer);
bool ExpandConvTransLayer::init(const LayerMap &layerMap,
const ParameterMap &parameterMap) {
/* Initialize the basic convolutional parent class */
ConvTransBaseLayer::init(layerMap, parameterMap);
ConvBaseLayer::init(layerMap, parameterMap);
/* Initialize the projection */
for (auto &inputConfig : config_.inputs()) {
const ConvConfig &conf = inputConfig.conv_conf();
subM_.push_back(conf.channels() / conf.groups());
subN_.push_back(conf.output_x() * conf.output_x());
subK_.push_back(channel_ * conf.filter_size() * conf.filter_size() /
subK_.push_back(numFilters_ * conf.filter_size() * conf.filter_size() /
conf.groups());
/* Consistent caffe mode for multiple input */
caffeMode_ = conf.caffe_mode();
......@@ -65,8 +65,8 @@ size_t ExpandConvTransLayer::getSize() {
imageSize(outputW_[i], filterSize_[i], padding_[i], stride_[i]));
subN_.push_back(outputH_[i] * outputW_[i]);
CHECK(layerSize == 0 ||
imgSizeH_[i] * imgSizeW_[i] * (size_t)channel_ == layerSize);
layerSize = imgSizeH_[i] * imgSizeW_[i] * channel_;
imgSizeH_[i] * imgSizeW_[i] * (size_t)numFilters_ == layerSize);
layerSize = imgSizeH_[i] * imgSizeW_[i] * numFilters_;
}
getOutput().setFrameHeight(imgSizeH_[0]);
getOutput().setFrameWidth(imgSizeW_[0]);
......@@ -83,38 +83,6 @@ void ExpandConvTransLayer::resetExpandInput(size_t height, size_t width) {
}*/
void ExpandConvTransLayer::addSharedBias() {
size_t mapW = getSize() / channel_;
size_t mapH = getOutputValue()->getElementCnt() / mapW;
MatrixPtr out =
Matrix::create(getOutputValue()->getData(), mapH, mapW, false, useGpu_);
Matrix::resizeOrCreate(transOutValue_, mapW, mapH, false, useGpu_);
out->transpose(transOutValue_, false); // false means no memory allocation
transOutValue_->reshape(transOutValue_->getElementCnt() / channel_,
channel_);
MatrixPtr bias =
Matrix::create(biases_->getW()->getData(), 1,
biases_->getW()->getElementCnt(), false, useGpu_);
transOutValue_->addBias(*bias, 1.0f);
transOutValue_->reshape(mapW, mapH);
transOutValue_->transpose(out, false); // false means no memory allocation
out->clear();
bias->clear();
}
void ExpandConvTransLayer::addUnsharedBias() {
MatrixPtr outValue = getOutputValue();
MatrixPtr bias =
Matrix::create(biases_->getW()->getData(), 1,
biases_->getW()->getElementCnt(), false, useGpu_);
outValue->addBias(*bias, 1.0f);
}
void ExpandConvTransLayer::expandOneFrame(MatrixPtr image, size_t startIdx,
int inIdx) {
......
......@@ -15,7 +15,7 @@ limitations under the License. */
#pragma once
#include "ConvTransBaseLayer.h"
#include "ConvBaseLayer.h"
#include "paddle/math/Matrix.h"
#include <vector>
......@@ -28,7 +28,7 @@ namespace paddle {
*
* The config file api is img_convTrans_layer.
*/
class ExpandConvTransLayer : public ConvTransBaseLayer {
class ExpandConvTransLayer : public ConvBaseLayer {
protected:
/// For expand convolution.
/// subM_ = numFilters_ / groups_.
......@@ -45,15 +45,11 @@ protected:
IntV outputH_;
/// The spatial dimensions of width of output feature map.
IntV outputW_;
/// Expand one sample at a time. shape:
/// (numChannels * filterPixels_, outputSizeH * outputSizeW)
MatrixPtr expandInput_;
/// The transpose of output, which is an auxiliary matrix.
MatrixPtr transOutValue_;
public:
explicit ExpandConvTransLayer(const LayerConfig& config) :
ConvTransBaseLayer(config) {}
ConvBaseLayer(config) {}
~ExpandConvTransLayer() {}
......@@ -86,15 +82,7 @@ public:
*/
void shrinkFwd(MatrixPtr output, int inpIdx);
/**
* Add shared bias.
*/
void addSharedBias();
/**
* Add unshared bias.
*/
void addUnsharedBias();
void forward(PassType passType);
void bpropSharedBias(MatrixPtr biases, MatrixPtr v);
void bpropBiases(MatrixPtr v);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册