Skip to content

Commit 53e1629

Browse files
author
wangyang59
committed
Refactored imageSize in ConvBaseLayer to MathUtil
1 parent 03f4b1d commit 53e1629

File tree

7 files changed

+39
-62
lines changed

7 files changed

+39
-62
lines changed

paddle/gserver/layers/ConvBaseLayer.cpp

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ limitations under the License. */
1414

1515
#include "paddle/utils/Logging.h"
1616
#include "ConvBaseLayer.h"
17+
#include "paddle/math/MathUtils.h"
1718
namespace paddle {
1819

1920
bool ConvBaseLayer::init(const LayerMap& layerMap,
@@ -95,18 +96,22 @@ size_t ConvBaseLayer::calOutputSize() {
9596
if (inW[i] == 0)
9697
inW[i] = config_.inputs(i).conv_conf().output_x();
9798
outH.push_back(
98-
imageSize(inH[i], filterSizeY_[i], paddingY_[i], strideY_[i]));
99+
imageSize(inH[i], filterSizeY_[i], paddingY_[i], strideY_[i],
100+
caffeMode_));
99101
outW.push_back(
100-
imageSize(inW[i], filterSize_[i], padding_[i], stride_[i]));
102+
imageSize(inW[i], filterSize_[i], padding_[i], stride_[i],
103+
caffeMode_));
101104
} else {
102105
if (inH[i] == 0)
103106
inH[i] = config_.inputs(i).conv_conf().img_size();
104107
if (inW[i] == 0)
105108
inW[i] = config_.inputs(i).conv_conf().img_size();
106109
outH.push_back(
107-
outputSize(inH[i], filterSizeY_[i], paddingY_[i], strideY_[i]));
110+
outputSize(inH[i], filterSizeY_[i], paddingY_[i], strideY_[i],
111+
caffeMode_));
108112
outW.push_back(
109-
outputSize(inW[i], filterSize_[i], padding_[i], stride_[i]));
113+
outputSize(inW[i], filterSize_[i], padding_[i], stride_[i],
114+
caffeMode_));
110115
}
111116
CHECK_EQ(outH[i], outH[0]);
112117
CHECK_EQ(outW[i], outW[0]);

paddle/gserver/layers/ConvBaseLayer.h

Lines changed: 0 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -91,43 +91,6 @@ class ConvBaseLayer : public Layer {
9191
virtual size_t calOutputSize();
9292

9393
Weight& getWeight(int idx) { return *weights_[idx]; }
94-
95-
/**
96-
* Calculate output size based on caffeMode_.
97-
* - input(+padding): 0123456789
98-
* - imageSize(+padding) = 10;
99-
* - filterSize = 3;
100-
* - stride = 2;
101-
* - caffeMode_ is true:
102-
- output: (012), (234), (456), (678)
103-
- outputSize = 4;
104-
* - caffeMode_ is false:
105-
* - output: (012), (234), (456), (678), (9)
106-
* - outputSize = 5;
107-
*/
108-
int outputSize(int imageSize, int filterSize, int padding, int stride) {
109-
int outputSize;
110-
if (!caffeMode_) {
111-
outputSize =
112-
(imageSize - filterSize + 2 * padding + stride - 1) / stride + 1;
113-
} else {
114-
outputSize = (imageSize - filterSize + 2 * padding) / stride + 1;
115-
}
116-
CHECK_GE(outputSize, 1);
117-
return outputSize;
118-
}
119-
120-
int imageSize(int outputSize, int filterSize, int padding, int stride) {
121-
int imageSize;
122-
if (!caffeMode_) {
123-
imageSize =
124-
(outputSize - 1) * stride + filterSize - 2 * padding - stride + 1;
125-
} else {
126-
imageSize = (outputSize - 1) * stride + filterSize - 2 * padding;
127-
}
128-
CHECK_GE(imageSize, 1);
129-
return imageSize;
130-
}
13194
};
13295

13396
} // namespace paddle

paddle/gserver/tests/test_ConvTrans.cpp

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ limitations under the License. */
2020
#include "paddle/trainer/Trainer.h"
2121
#include "paddle/utils/GlobalConstants.h"
2222
#include "paddle/gserver/layers/ExpandConvTransLayer.h"
23+
#include "paddle/math/MathUtils.h"
2324

2425
#include "TestUtil.h"
2526
#include "LayerGradUtil.h"
@@ -56,11 +57,9 @@ TEST(Layer, convTransLayerFwd) {
5657
conv->set_groups(1);
5758
conv->set_filter_channels(3 / conv->groups());
5859
conv->set_img_size(16);
59-
conv->set_output_x(
60-
(2 * conv->padding() + conv->img_size() - conv->filter_size()) /
61-
((float)conv->stride()) +
62-
1.5);
63-
60+
conv->set_output_x(outputSize(conv->img_size(), conv->filter_size(),
61+
conv->padding(), conv->stride(),
62+
/* caffeMode */ true));
6463
configt.layerConfig.set_size(conv->img_size() * conv->img_size() *
6564
configt.layerConfig.num_filters());
6665
configt.layerConfig.set_name("convTrans");
@@ -99,10 +98,9 @@ TEST(Layer, convTransLayerFwd) {
9998
conv->set_groups(1);
10099
conv->set_filter_channels(conv->channels() / conv->groups());
101100
conv->set_img_size(16);
102-
conv->set_output_x(
103-
(2 * conv->padding() + conv->img_size() - conv->filter_size()) /
104-
((float)conv->stride()) +
105-
1.5);
101+
conv->set_output_x(outputSize(conv->img_size(), conv->filter_size(),
102+
conv->padding(), conv->stride(),
103+
/* caffeMode */ true));
106104
config.layerConfig.set_size(conv->output_x() * conv->output_x() *
107105
config.layerConfig.num_filters());
108106
config.layerConfig.set_name("conv");

paddle/gserver/tests/test_LayerGrad.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -336,10 +336,9 @@ void testConvTransLayer(const string& type, bool trans, bool useGpu) {
336336
conv->set_groups(1);
337337
conv->set_filter_channels(3 / conv->groups());
338338
conv->set_img_size(16);
339-
conv->set_output_x(
340-
(2 * conv->padding() + conv->img_size() - conv->filter_size()) /
341-
((float)conv->stride()) +
342-
1.5);
339+
conv->set_output_x(outputSize(conv->img_size(), conv->filter_size(),
340+
conv->padding(), conv->stride(),
341+
/* caffeMode */ true));
343342

344343
config.layerConfig.set_size(conv->img_size() * conv->img_size() *
345344
config.layerConfig.num_filters());

paddle/math/MathUtils.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,4 +80,17 @@ int outputSize(int imageSize, int filterSize, int padding, int stride,
8080
return outputSize;
8181
}
8282

83+
int imageSize(int outputSize, int filterSize, int padding, int stride,
84+
bool caffeMode) {
85+
int imageSize;
86+
if (!caffeMode) {
87+
imageSize =
88+
(outputSize - 1) * stride + filterSize - 2 * padding - stride + 1;
89+
} else {
90+
imageSize = (outputSize - 1) * stride + filterSize - 2 * padding;
91+
}
92+
CHECK_GE(imageSize, 1);
93+
return imageSize;
94+
}
95+
8396
} // namespace paddle

paddle/math/MathUtils.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,4 +60,7 @@ void sparseRand(int* major, int* minor, int nnz, int majorLen, int minorMax,
6060
int outputSize(int imageSize, int filterSize, int padding, int stride,
6161
bool caffeMode);
6262

63+
int imageSize(int outputSize, int filterSize, int padding, int stride,
64+
bool caffeMode);
65+
6366
} // namespace paddle

python/paddle/trainer/config_parser.py

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1107,14 +1107,10 @@ def parse_conv(conv, input_layer_name, conv_conf, trans=False):
11071107
("Input layer %s: Incorrect input image size %d for input "
11081108
+ "image pixels %d")
11091109
% (input_layer_name, conv_conf.img_size, img_pixels))
1110-
if conv.caffe_mode:
1111-
conv_conf.output_x = \
1112-
1 + int(math.floor((2 * conv.padding + conv_conf.img_size \
1113-
- conv.filter_size) / float(conv.stride)))
1114-
else:
1115-
conv_conf.output_x = \
1116-
1 + int(math.ceil((2 * conv.padding + conv_conf.img_size \
1117-
- conv.filter_size) / float(conv.stride)))
1110+
1111+
conv_conf.output_x = cnn_output_size(
1112+
conv_conf.img_size, conv_conf.filter_size,
1113+
conv_conf.padding, conv_conf.stride, conv_conf.caffe_mode)
11181114
else:
11191115
outputSize = g_layer_map[input_layer_name].size / conv.channels
11201116
print('channels=%d size=%d'%(conv.channels,

0 commit comments

Comments
 (0)