From aa2cd2ce8f01072a2e604740bd350417c23485c4 Mon Sep 17 00:00:00 2001 From: wangyang59 Date: Tue, 1 Nov 2016 10:22:00 -0700 Subject: [PATCH] Refactor ExpandConvTransLayer to share codes with ExpandConvLayer --- paddle/gserver/layers/ConvBaseLayer.cpp | 7 ++ paddle/gserver/layers/ConvBaseLayer.h | 67 +++++++++++++++++++ paddle/gserver/layers/ExpandConvLayer.cpp | 18 ----- paddle/gserver/layers/ExpandConvLayer.h | 19 +----- .../gserver/layers/ExpandConvTransLayer.cpp | 40 ++--------- paddle/gserver/layers/ExpandConvTransLayer.h | 20 ++---- 6 files changed, 83 insertions(+), 88 deletions(-) diff --git a/paddle/gserver/layers/ConvBaseLayer.cpp b/paddle/gserver/layers/ConvBaseLayer.cpp index 42ff0b70d86..4346cb520ea 100644 --- a/paddle/gserver/layers/ConvBaseLayer.cpp +++ b/paddle/gserver/layers/ConvBaseLayer.cpp @@ -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 diff --git a/paddle/gserver/layers/ConvBaseLayer.h b/paddle/gserver/layers/ConvBaseLayer.h index e660a6d6f50..24927dec24d 100644 --- a/paddle/gserver/layers/ConvBaseLayer.h +++ b/paddle/gserver/layers/ConvBaseLayer.h @@ -28,6 +28,9 @@ class ConvBaseLayer : public Layer { protected: typedef std::vector 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 diff --git a/paddle/gserver/layers/ExpandConvLayer.cpp b/paddle/gserver/layers/ExpandConvLayer.cpp index 80a6a62b5c0..866cd33c118 100644 --- a/paddle/gserver/layers/ExpandConvLayer.cpp +++ b/paddle/gserver/layers/ExpandConvLayer.cpp @@ -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); diff --git a/paddle/gserver/layers/ExpandConvLayer.h b/paddle/gserver/layers/ExpandConvLayer.h index 030a3ba397f..f43b199498e 100644 --- a/paddle/gserver/layers/ExpandConvLayer.h +++ b/paddle/gserver/layers/ExpandConvLayer.h @@ -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); diff --git a/paddle/gserver/layers/ExpandConvTransLayer.cpp b/paddle/gserver/layers/ExpandConvTransLayer.cpp index 67c045821d1..fb2e7fc4bd6 100644 --- a/paddle/gserver/layers/ExpandConvTransLayer.cpp +++ b/paddle/gserver/layers/ExpandConvTransLayer.cpp @@ -29,14 +29,14 @@ REGISTER_LAYER(exconvt, ExpandConvTransLayer); bool ExpandConvTransLayer::init(const LayerMap &layerMap, const ParameterMap ¶meterMap) { /* 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) { diff --git a/paddle/gserver/layers/ExpandConvTransLayer.h b/paddle/gserver/layers/ExpandConvTransLayer.h index a6591fe1aa3..cbe4da8143c 100644 --- a/paddle/gserver/layers/ExpandConvTransLayer.h +++ b/paddle/gserver/layers/ExpandConvTransLayer.h @@ -15,7 +15,7 @@ limitations under the License. */ #pragma once -#include "ConvTransBaseLayer.h" +#include "ConvBaseLayer.h" #include "paddle/math/Matrix.h" #include @@ -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); -- GitLab