Skip to content

Commit af7a50c

Browse files
author
wangyang59
committed
minor changes on deconv implementation and add protostr test for deconv layer
1 parent 4491209 commit af7a50c

File tree

7 files changed

+43
-22
lines changed

7 files changed

+43
-22
lines changed

paddle/gserver/layers/ExpandConvLayer.cpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,7 @@ void ExpandConvLayer::forward(PassType passType) {
3232
Layer::forward(passType);
3333

3434
/* malloc memory for the output_ if necessary */
35-
/* note: one sample correspond to one colum, and the
36-
* transOutValue correspond sample to one row */
37-
int batchSize = inputLayers_[0]->getOutputValue()->getWidth();
38-
batchSize = inputLayers_[0]->getOutputValue()->getHeight();
35+
int batchSize = inputLayers_[0]->getOutputValue()->getHeight();
3936
resetOutput(batchSize, getOutputSize());
4037

4138
MatrixPtr image = nullptr;

paddle/gserver/layers/ExpandConvTransLayer.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,6 @@ void ExpandConvTransLayer::forward(PassType passType) {
3838
Layer::forward(passType);
3939

4040
/* malloc memory for the output_ if necessary */
41-
/* note: one sample correspond to one colum, and the
42-
* transOutValue correspond sample to one row */
4341
int batchSize = inputLayers_[0]->getOutputValue()->getHeight();
4442
resetOutput(batchSize, getOutputSize());
4543

paddle/gserver/layers/ExpandConvTransLayer.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ namespace paddle {
2626
* This layer expands input and use matrix multiplication to
2727
* calculate convolution transpose (deconv) operation.
2828
*
29-
* The config file api is img_convTrans_layer.
29+
* The config file api is img_conv_layer with flag trans=True.
3030
*/
3131
class ExpandConvTransLayer : public ExpandConvBaseLayer {
3232
public:

paddle/math/MathUtils.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,10 @@ 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+
/**
64+
* Calculate image size based on output size and caffeMode_.
65+
* It is the reverse function of outputSize()
66+
*/
6367
int imageSize(int outputSize, int filterSize, int padding, int stride,
6468
bool caffeMode);
6569

python/paddle/trainer/config_parser.py

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1017,6 +1017,17 @@ def cnn_output_size(img_size, filter_size, padding, stride, caffe_mode):
10171017
else:
10181018
return 1 + int(math.ceil(output))
10191019

1020+
'''
1021+
calcualte image_size based on output_size for convolution.
1022+
It is the reverse function of cnn_output_size
1023+
'''
1024+
def cnn_image_size(output_size, filter_size, padding, stride, caffe_mode):
1025+
if caffe_mode:
1026+
img_size = (output_size - 1) * stride + filter_size - 2 * padding
1027+
else:
1028+
img_size = (output_size - 2) * stride + filter_size - 2 * padding + 1
1029+
return img_size
1030+
10201031
def parse_pool(pool, input_layer_name, pool_conf):
10211032
pool_conf.pool_type = pool.pool_type
10221033
config_assert(pool.pool_type in ['max-projection', 'avg-projection',
@@ -1120,14 +1131,9 @@ def parse_conv(conv, input_layer_name, conv_conf, trans=False):
11201131
("Input layer %s: Incorrect input image size %d for input "
11211132
+ "image pixels %d")
11221133
% (input_layer_name, conv_conf.output_x, outputSize))
1123-
if conv.caffe_mode:
1124-
conv_conf.img_size = \
1125-
(conv_conf.output_x - 1) * conv.stride \
1126-
+ conv.filter_size - 2 * conv.padding
1127-
else:
1128-
conv_conf.img_size = \
1129-
(conv_conf.output_x - 2) * conv.stride \
1130-
+ conv.filter_size - 2 * conv.padding + 1
1134+
conv_conf.img_size = cnn_image_size(
1135+
conv_conf.output_x, conv_conf.filter_size,
1136+
conv_conf.padding, conv_conf.stride, conv_conf.caffe_mode)
11311137

11321138
def parse_block_expand(block_expand, input_layer_name, block_expand_conf):
11331139
block_expand_conf.channels = block_expand.channels
@@ -1656,12 +1662,6 @@ def __init__(
16561662
use_gpu = int(g_command_config_args.get("use_gpu", 0))
16571663
parallel_nn = int(g_command_config_args.get("parallel_nn", 0))
16581664

1659-
# Automatically select cudnn_type for GPU and exconv for CPU
1660-
# if set type=conv, but still reserve the way user specify
1661-
# exconv or cudnn_conv manually.
1662-
if self.layer_type == "cudnn_convt":
1663-
config_assert(use_gpu, "cudnn_convt only support GPU")
1664-
16651665
# cudnn_convt has not been implemented so use exconvt only
16661666
self.layer_type = "exconvt"
16671667
# need to specify layer in config

python/paddle/trainer_config_helpers/tests/configs/generate_protostr.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ protostr=$PWD/protostr
99
configs=(test_fc layer_activations projections test_print_layer
1010
test_sequence_pooling test_lstmemory_layer test_grumemory_layer
1111
last_first_seq test_expand_layer test_ntm_layers test_hsigmoid
12-
img_layers util_layers simple_rnn_layers unused_layers test_cost_layers
12+
img_layers img_trans_layers util_layers simple_rnn_layers unused_layers test_cost_layers
1313
test_rnn_group shared_fc shared_lstm test_cost_layers_with_weight
1414
test_maxout test_bi_grumemory math_ops)
1515

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
from paddle.trainer_config_helpers import *
2+
3+
settings(
4+
learning_rate=1e-3,
5+
batch_size=1000
6+
)
7+
8+
img = data_layer(name='image', size=227*227)
9+
10+
# the parse_conv in config_parse.py is not strictly accurate when filter_size
11+
# is not square. So here set square filter_size.
12+
img_conv = img_conv_layer(input=img, num_channels=1, num_filters=64,
13+
filter_size=(32, 32), padding=(1, 1), stride=(1, 1),
14+
act=LinearActivation(), trans=True)
15+
img_bn = batch_norm_layer(input=img_conv, act=ReluActivation())
16+
17+
img_norm = img_cmrnorm_layer(input=img_bn, size=32)
18+
19+
img_pool = img_pool_layer(input=img_conv, pool_size=32, pool_type=MaxPooling())
20+
21+
22+
outputs(img_pool, img_norm)

0 commit comments

Comments
 (0)