Skip to content

Commit 05e563b

Browse files
committed
Add unitest
1 parent 91b83d0 commit 05e563b

File tree

2 files changed

+55
-34
lines changed

2 files changed

+55
-34
lines changed

python/paddle/v2/fluid/layers/nn.py

Lines changed: 45 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1974,16 +1974,13 @@ def sequence_reshape(input, new_dim):
19741974

19751975

19761976
def im2sequence(input,
1977-
block_x=1,
1978-
block_y=1,
1979-
stride_x=1,
1980-
stride_y=1,
1981-
padding_x=0,
1982-
padding_y=0,
1977+
filter_size=1,
1978+
stride=1,
1979+
padding=0,
19831980
name=None,
19841981
layer_attr=None):
19851982
"""
1986-
This op use block to scan images and convert these images to sequences.
1983+
This op use filter to scan images and convert these images to sequences.
19871984
After expanding, the number of time step are output_height * output_width
19881985
for an image, in which output_height and output_width are calculated
19891986
by below equation:
@@ -1995,23 +1992,34 @@ def im2sequence(input,
19951992
19961993
And the dimension of each time step is block_y * block_x * input.channels.
19971994
1998-
1999-
20001995
Args:
20011996
input (Variable): The input should be a tensor in NCHW format.
2002-
block_x (int): The width of sub block.
2003-
block_y (int): The width of sub block.
2004-
stride_x (int): The stride size in horizontal direction.
2005-
stride_y (int): The stride size in vertical direction.
2006-
padding_x (int): The padding size in horizontal direction.
2007-
padding_y (int): The padding size in vertical direction.
1997+
1998+
filter_size(int|tuple|None): The filter size. If filter_size is a tuple,
1999+
it must contain two integers, (filter_size_H, filter_size_W).
2000+
Otherwise, the filter will be a square.
2001+
2002+
stride(int|tuple): The stride size. If stride is a tuple, it must
2003+
contain two integers, (stride_H, stride_W). Otherwise, the
2004+
stride_H = stride_W = stride. Default: stride = 1.
2005+
2006+
padding(int|tuple): The padding size. If padding is a tuple, it can
2007+
contain two integers like (padding_H, padding_W) which means
2008+
padding_up = padding_down = padding_H and
2009+
padding_left = padding_right = padding_W. Or it can use
2010+
(padding_up, padding_left, padding_down, padding_right) to indicate
2011+
paddings of four direction. Otherwise, a scalar padding means
2012+
padding_up = padding_down = padding_left = padding_right = padding
2013+
Default: padding = 0.
2014+
20082015
name (int): The name of this layer. It is optional.
20092016
20102017
Returns:
2011-
output: The output is a LoDTensor woth shape
2012-
{input.batch_size * output_y * x,
2013-
block_y * block_x * input.channels}.
2014-
If we regard output as matrix, each row of this matrix is a step of sequence.
2018+
output: The output is a LoDTensor with shape
2019+
{input.batch_size * output_height * output_width,
2020+
filter_size_H * filter_size_W * input.channels}.
2021+
If we regard output as a matrix, each row of this matrix is
2022+
a step of a sequence.
20152023
20162024
Examples:
20172025
@@ -2041,12 +2049,9 @@ def im2sequence(input,
20412049
20422050
And:
20432051
2044-
block_height = 2
2045-
block_width = 2
2046-
stride_height = 1
2047-
stride_width = 1
2048-
padding_height = 0
2049-
padding_width = 0
2052+
filter = [2, 2]
2053+
stride = [1, 1]
2054+
padding = [0, 0]
20502055
20512056
Then:
20522057
@@ -2063,27 +2068,33 @@ def im2sequence(input,
20632068
20642069
output.lod = [[0, 4, 8]]
20652070
2066-
2067-
20682071
The simple usage is:
20692072
20702073
.. code-block:: python
20712074
2072-
output = fluid.layers.im2sequence(input=layer, stride_x=1, stride_y=1, block_x=2, block_y=2)
2075+
output = fluid.layers.im2sequence(input=layer, stride=[1, 1], filter=[2, 2])
20732076
20742077
"""
2078+
2079+
if isinstance(filter_size, int):
2080+
filter_size = [filter_size, filter_size]
2081+
if isinstance(stride, int):
2082+
stride = [stride, stride]
2083+
if isinstance(padding, int):
2084+
padding = [padding, padding]
2085+
if len(padding) == 2:
2086+
padding.append(padding[0])
2087+
padding.append(padding[1])
2088+
20752089
helper = LayerHelper('im2sequence', **locals())
20762090
out = helper.create_tmp_variable(dtype=helper.input_dtype())
20772091
helper.append_op(
20782092
type='im2sequence',
20792093
inputs={'X': input},
20802094
outputs={'Out': out},
20812095
attrs={
2082-
'block_height': block_y,
2083-
'block_width': block_x,
2084-
'stride_height': stride_y,
2085-
'stride_width': stride_x,
2086-
'padding_height': padding_y,
2087-
'padding_width': padding_x
2096+
'kernels': filter_size,
2097+
'strides': stride,
2098+
'paddings': padding,
20882099
})
20892100
return out

python/paddle/v2/fluid/tests/test_layers.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,16 @@ def test_sequence_reshape(self):
225225
self.assertIsNotNone(out)
226226
print(str(program))
227227

228+
def test_im2sequence(self):
229+
print("test_im2sequence")
230+
program = Program()
231+
with program_guard(program):
232+
x = layers.data(name='x', shape=[3, 128, 128], dtype='float32')
233+
output = layers.im2sequence(
234+
input=x, stride=[1, 1], filter_size=[2, 2])
235+
self.assertIsNotNone(output)
236+
print(str(program))
237+
228238

229239
if __name__ == '__main__':
230240
unittest.main()

0 commit comments

Comments
 (0)