Skip to content

Commit 3d72e94

Browse files
author
wangyang59
committed
rebase deconv implementation with develop branch and resolve conflicts with pull#218 commit 45c81a4
1 parent 5e4cc24 commit 3d72e94

File tree

9 files changed

+78
-80
lines changed

9 files changed

+78
-80
lines changed

paddle/gserver/layers/ConvBaseLayer.cpp

Lines changed: 51 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,20 @@ bool ConvBaseLayer::init(const LayerMap& layerMap,
4848
outputW_.push_back(conf.output_x());
4949
}
5050

51+
CHECK(inputLayers_.size() == parameters_.size());
52+
for (size_t i = 0; i < inputLayers_.size(); i++) {
53+
size_t height, width;
54+
height = filterPixels_[i] * filterChannels_[i];
55+
width = (!isDeconv_) ? numFilters_ : channels_[i];
56+
57+
// create a new weight
58+
CHECK_EQ(parameters_[i]->getSize(), width * height);
59+
Weight* w = new Weight(height, width, parameters_[i]);
60+
weights_.emplace_back(w);
61+
}
62+
5163
/* initialize the biases_ */
52-
if (biasParameter_.get() != NULL) {
64+
if (biasParameter_.get()) {
5365
if (sharedBiases_) {
5466
CHECK_EQ((size_t)numFilters_, biasParameter_->getSize());
5567
biases_ =
@@ -76,25 +88,46 @@ size_t ConvBaseLayer::calOutputSize() {
7688
clearAndReserve(&outputH_);
7789
clearAndReserve(&outputW_);
7890
size_t layerSize = 0;
79-
for (size_t i = 0; i < inputLayers_.size(); i++) {
80-
imgSizeH_.push_back(inputLayers_[i]->getOutput().getFrameHeight());
81-
imgSizeW_.push_back(inputLayers_[i]->getOutput().getFrameWidth());
82-
if (imgSizeH_[i] == 0)
83-
imgSizeH_[i] = config_.inputs(i).conv_conf().img_size();
84-
if (imgSizeW_[i] == 0)
85-
imgSizeW_[i] = config_.inputs(i).conv_conf().img_size();
86-
outputH_.push_back(outputSize(imgSizeH_[i], filterSizeY_[i], paddingY_[i],
87-
strideY_[i], caffeMode_));
88-
outputW_.push_back(outputSize(imgSizeW_[i], filterSize_[i], padding_[i],
89-
stride_[i], caffeMode_));
90-
CHECK_EQ(outputH_[i], outputH_[0]);
91-
CHECK_EQ(outputW_[i], outputW_[0]);
91+
92+
if (!isDeconv_) {
93+
for (size_t i = 0; i < inputLayers_.size(); i++) {
94+
imgSizeH_.push_back(inputLayers_[i]->getOutput().getFrameHeight());
95+
imgSizeW_.push_back(inputLayers_[i]->getOutput().getFrameWidth());
96+
if (imgSizeH_[i] == 0)
97+
imgSizeH_[i] = config_.inputs(i).conv_conf().img_size();
98+
if (imgSizeW_[i] == 0)
99+
imgSizeW_[i] = config_.inputs(i).conv_conf().img_size();
100+
outputH_.push_back(
101+
outputSize(imgSizeH_[i], filterSizeY_[i], paddingY_[i], strideY_[i]));
102+
outputW_.push_back(
103+
outputSize(imgSizeW_[i], filterSize_[i], padding_[i], stride_[i]));
104+
CHECK_EQ(outputH_[i], outputH_[0]);
105+
CHECK_EQ(outputW_[i], outputW_[0]);
106+
}
107+
getOutput().setFrameHeight(outputH_[0]);
108+
getOutput().setFrameWidth(outputW_[0]);
109+
layerSize = outputH_[0] * outputW_[0] * size_t(numFilters_);
110+
} else {
111+
for (size_t i = 0; i < inputLayers_.size(); i++) {
112+
outputH_.push_back(inputLayers_[i]->getOutput().getFrameHeight());
113+
outputW_.push_back(inputLayers_[i]->getOutput().getFrameWidth());
114+
if (outputH_[i] == 0)
115+
outputH_[i] = config_.inputs(i).conv_conf().output_x();
116+
if (outputW_[i] == 0)
117+
outputW_[i] = config_.inputs(i).conv_conf().output_x();
118+
imgSizeH_.push_back(
119+
imageSize(outputH_[i], filterSizeY_[i], paddingY_[i], strideY_[i]));
120+
imgSizeW_.push_back(
121+
imageSize(outputW_[i], filterSize_[i], padding_[i], stride_[i]));
122+
CHECK_EQ(imgSizeH_[i], imgSizeH_[0]);
123+
CHECK_EQ(imgSizeW_[i], imgSizeW_[0]);
124+
}
125+
getOutput().setFrameHeight(imgSizeH_[0]);
126+
getOutput().setFrameWidth(imgSizeW_[0]);
127+
layerSize = imgSizeH_[0] * imgSizeW_[0] * size_t(numFilters_);
92128
}
93-
getOutput().setFrameHeight(outputH_[0]);
94-
getOutput().setFrameWidth(outputW_[0]);
95-
layerSize = outputH_[0] * outputW_[0] * size_t(numFilters_);
96-
return layerSize;
97129

130+
return layerSize;
98131
}
99132

100133
} // namespace paddle

paddle/gserver/layers/ExpandConvBaseLayer.cpp

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,15 +45,27 @@ bool ExpandConvBaseLayer::init(const LayerMap &layerMap,
4545
caffeMode_ = conf.caffe_mode();
4646
}
4747

48+
getOutputSize();
49+
4850
return true;
4951
}
5052

53+
size_t ExpandConvBaseLayer::getOutputSize() {
54+
CHECK_NE(inputLayers_.size(), 0UL);
55+
size_t layerSize = ConvBaseLayer::calOutputSize();
56+
subN_.clear();
57+
for (size_t i = 0; i < inputLayers_.size(); i++) {
58+
subN_.push_back(outputH_[i] * outputW_[i]);
59+
}
60+
return layerSize;
61+
}
62+
5163
void ExpandConvBaseLayer::resetExpandInput(size_t height, size_t width) {
5264
Matrix::resizeOrCreate(expandInput_, height, width, false, useGpu_);
5365
}
5466

5567
void ExpandConvBaseLayer::addSharedBias() {
56-
size_t mapW = getSize() / numFilters_;
68+
size_t mapW = getOutputSize() / numFilters_;
5769
size_t mapH = getOutputValue()->getElementCnt() / mapW;
5870
MatrixPtr out =
5971
Matrix::create(getOutputValue()->getData(), mapH, mapW, false, useGpu_);
@@ -224,7 +236,7 @@ void ExpandConvBaseLayer::bpropWeights(MatrixPtr image, MatrixPtr out,
224236
}
225237

226238
void ExpandConvBaseLayer::bpropSharedBias(MatrixPtr biases, MatrixPtr v) {
227-
size_t mapW = getSize() / numFilters_;
239+
size_t mapW = getOutputSize() / numFilters_;
228240
size_t mapH = v->getElementCnt() / mapW;
229241
MatrixPtr vTmp = Matrix::create(v->getData(), mapH, mapW, false, useGpu_);
230242

paddle/gserver/layers/ExpandConvBaseLayer.h

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,6 @@ class ExpandConvBaseLayer : public ConvBaseLayer {
3434
IntV subN_;
3535
/// subK_ = channels_ * filterPixels_ * groups_.
3636
IntV subK_;
37-
/// The spatial dimensions of height of input feature map.
38-
IntV imgSizeH_;
39-
/// The spatial dimensions of width of input feature map.
40-
IntV imgSizeW_;
41-
/// The spatial dimensions of height of output feature map.
42-
IntV outputH_;
43-
/// The spatial dimensions of width of output feature map.
44-
IntV outputW_;
4537

4638
/*The expandInput_ and transOutValue_ are used for CPU expand conv calc
4739
* Expand one sample at a time. shape:
@@ -59,6 +51,7 @@ class ExpandConvBaseLayer : public ConvBaseLayer {
5951

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

54+
size_t getOutputSize();
6255
/**
6356
* Create or resize expandInput_.
6457
*/

paddle/gserver/layers/ExpandConvLayer.cpp

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,6 @@ bool ExpandConvLayer::init(const LayerMap &layerMap,
2828
return true;
2929
}
3030

31-
size_t ExpandConvLayer::getOutputSize() {
32-
CHECK_NE(inputLayers_.size(), 0UL);
33-
size_t layerSize = ConvBaseLayer::calOutputSize();
34-
subN_.clear();
35-
for (size_t i = 0; i < inputLayers_.size(); i++) {
36-
subN_.push_back(outputH_[i] * outputW_[i]);
37-
}
38-
return layerSize;
39-
}
40-
4131
void ExpandConvLayer::forward(PassType passType) {
4232
Layer::forward(passType);
4333

paddle/gserver/layers/ExpandConvLayer.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,6 @@ class ExpandConvLayer : public ExpandConvBaseLayer {
3838

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

41-
size_t getOutputSize();
42-
4341
void forward(PassType passType);
4442
void backward(const UpdateCallback& callback);
4543
};

paddle/gserver/layers/ExpandConvTransLayer.cpp

Lines changed: 1 addition & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -34,42 +34,14 @@ bool ExpandConvTransLayer::init(const LayerMap &layerMap,
3434
return true;
3535
}
3636

37-
// Why this is necessary after calling init?
38-
size_t ExpandConvTransLayer::getSize() {
39-
CHECK_NE(inputLayers_.size(), 0UL);
40-
imgSizeH_.clear();
41-
imgSizeW_.clear();
42-
outputH_.clear();
43-
outputW_.clear();
44-
subN_.clear();
45-
size_t layerSize = 0;
46-
for (size_t i = 0; i < inputLayers_.size(); i++) {
47-
outputH_.push_back(inputLayers_[i]->getOutput().getFrameHeight());
48-
outputW_.push_back(inputLayers_[i]->getOutput().getFrameWidth());
49-
if (outputH_[i] == 0) outputH_[i] = outputX_[i];
50-
if (outputW_[i] == 0) outputW_[i] = outputX_[i];
51-
imgSizeH_.push_back(
52-
imageSize(outputH_[i], filterSize_[i], padding_[i], stride_[i]));
53-
imgSizeW_.push_back(
54-
imageSize(outputW_[i], filterSize_[i], padding_[i], stride_[i]));
55-
subN_.push_back(outputH_[i] * outputW_[i]);
56-
CHECK(layerSize == 0 ||
57-
imgSizeH_[i] * imgSizeW_[i] * (size_t)numFilters_ == layerSize);
58-
layerSize = imgSizeH_[i] * imgSizeW_[i] * numFilters_;
59-
}
60-
getOutput().setFrameHeight(imgSizeH_[0]);
61-
getOutput().setFrameWidth(imgSizeW_[0]);
62-
return layerSize;
63-
}
64-
6537
void ExpandConvTransLayer::forward(PassType passType) {
6638
Layer::forward(passType);
6739

6840
/* malloc memory for the output_ if necessary */
6941
/* note: one sample correspond to one colum, and the
7042
* transOutValue correspond sample to one row */
7143
int batchSize = inputLayers_[0]->getOutputValue()->getHeight();
72-
resetOutput(batchSize, getSize());
44+
resetOutput(batchSize, getOutputSize());
7345

7446
MatrixPtr output = nullptr;
7547
for (size_t i = 0; i < inputLayers_.size(); ++i) {

paddle/gserver/layers/ExpandConvTransLayer.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,6 @@ class ExpandConvTransLayer : public ExpandConvBaseLayer {
3737

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

40-
size_t getSize();
41-
4240
void forward(PassType passType);
4341
void backward(const UpdateCallback& callback);
4442
};

paddle/gserver/tests/test_ConvTrans.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,11 @@ TEST(Layer, convTransLayerFwd) {
4343
configt.layerConfig.set_partial_sum(1);
4444
configt.layerConfig.set_shared_biases(true);
4545

46-
configt.inputDefs.push_back({INPUT_DATA, "layer_0", 1024, 288});
46+
configt.inputDefs.push_back({INPUT_DATA, "layer_0", 1024, 384});
4747
LayerInputConfig* input = configt.layerConfig.add_inputs();
4848
ConvConfig* conv = input->mutable_conv_conf();
4949
conv->set_filter_size(2);
50-
conv->set_filter_size_y(3);
50+
conv->set_filter_size_y(4);
5151
conv->set_channels(16);
5252
conv->set_padding(0);
5353
conv->set_padding_y(1);
@@ -86,11 +86,11 @@ TEST(Layer, convTransLayerFwd) {
8686
config.layerConfig.set_partial_sum(1);
8787
config.layerConfig.set_shared_biases(true);
8888

89-
config.inputDefs.push_back({INPUT_DATA, "layer_1", 768, 288});
89+
config.inputDefs.push_back({INPUT_DATA, "layer_1", 768, 384});
9090
input = config.layerConfig.add_inputs();
9191
conv = input->mutable_conv_conf();
9292
conv->set_filter_size(2);
93-
conv->set_filter_size_y(3);
93+
conv->set_filter_size_y(4);
9494
conv->set_channels(3);
9595
conv->set_padding(0);
9696
conv->set_padding_y(1);

python/paddle/trainer/config_parser.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1670,11 +1670,13 @@ def __init__(
16701670
if self.layer_type == "cudnn_convt":
16711671
config_assert(use_gpu, "cudnn_convt only support GPU")
16721672

1673-
if (use_gpu == 1 and self.layer_type != "exconvt" and
1674-
(parallel_nn == 0 or self.config.device > -1)):
1675-
self.layer_type = "cudnn_convt"
1676-
else:
1677-
self.layer_type = "exconvt"
1673+
# if (use_gpu == 1 and self.layer_type != "exconvt" and
1674+
# (parallel_nn == 0 or self.config.device > -1)):
1675+
# self.layer_type = "cudnn_convt"
1676+
# else:
1677+
# self.layer_type = "exconvt"
1678+
# cudnn_convt has not been implemented so use exconvt only
1679+
self.layer_type = "exconvt"
16781680
# need to specify layer in config
16791681
self.config.type = self.layer_type
16801682

0 commit comments

Comments
 (0)