提交 aa2cd2ce 编写于 作者: W wangyang59

Refactor ExpandConvTransLayer to share codes with ExpandConvLayer

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