提交 05e563b9 编写于 作者: W wanghaoshuang

Add unitest

上级 91b83d0a
...@@ -1974,16 +1974,13 @@ def sequence_reshape(input, new_dim): ...@@ -1974,16 +1974,13 @@ def sequence_reshape(input, new_dim):
def im2sequence(input, def im2sequence(input,
block_x=1, filter_size=1,
block_y=1, stride=1,
stride_x=1, padding=0,
stride_y=1,
padding_x=0,
padding_y=0,
name=None, name=None,
layer_attr=None): layer_attr=None):
""" """
This op use block to scan images and convert these images to sequences. This op use filter to scan images and convert these images to sequences.
After expanding, the number of time step are output_height * output_width After expanding, the number of time step are output_height * output_width
for an image, in which output_height and output_width are calculated for an image, in which output_height and output_width are calculated
by below equation: by below equation:
...@@ -1995,23 +1992,34 @@ def im2sequence(input, ...@@ -1995,23 +1992,34 @@ def im2sequence(input,
And the dimension of each time step is block_y * block_x * input.channels. And the dimension of each time step is block_y * block_x * input.channels.
Args: Args:
input (Variable): The input should be a tensor in NCHW format. input (Variable): The input should be a tensor in NCHW format.
block_x (int): The width of sub block.
block_y (int): The width of sub block. filter_size(int|tuple|None): The filter size. If filter_size is a tuple,
stride_x (int): The stride size in horizontal direction. it must contain two integers, (filter_size_H, filter_size_W).
stride_y (int): The stride size in vertical direction. Otherwise, the filter will be a square.
padding_x (int): The padding size in horizontal direction.
padding_y (int): The padding size in vertical direction. stride(int|tuple): The stride size. If stride is a tuple, it must
contain two integers, (stride_H, stride_W). Otherwise, the
stride_H = stride_W = stride. Default: stride = 1.
padding(int|tuple): The padding size. If padding is a tuple, it can
contain two integers like (padding_H, padding_W) which means
padding_up = padding_down = padding_H and
padding_left = padding_right = padding_W. Or it can use
(padding_up, padding_left, padding_down, padding_right) to indicate
paddings of four direction. Otherwise, a scalar padding means
padding_up = padding_down = padding_left = padding_right = padding
Default: padding = 0.
name (int): The name of this layer. It is optional. name (int): The name of this layer. It is optional.
Returns: Returns:
output: The output is a LoDTensor woth shape output: The output is a LoDTensor with shape
{input.batch_size * output_y * x, {input.batch_size * output_height * output_width,
block_y * block_x * input.channels}. filter_size_H * filter_size_W * input.channels}.
If we regard output as matrix, each row of this matrix is a step of sequence. If we regard output as a matrix, each row of this matrix is
a step of a sequence.
Examples: Examples:
...@@ -2041,12 +2049,9 @@ def im2sequence(input, ...@@ -2041,12 +2049,9 @@ def im2sequence(input,
And: And:
block_height = 2 filter = [2, 2]
block_width = 2 stride = [1, 1]
stride_height = 1 padding = [0, 0]
stride_width = 1
padding_height = 0
padding_width = 0
Then: Then:
...@@ -2063,15 +2068,24 @@ def im2sequence(input, ...@@ -2063,15 +2068,24 @@ def im2sequence(input,
output.lod = [[0, 4, 8]] output.lod = [[0, 4, 8]]
The simple usage is: The simple usage is:
.. code-block:: python .. code-block:: python
output = fluid.layers.im2sequence(input=layer, stride_x=1, stride_y=1, block_x=2, block_y=2) output = fluid.layers.im2sequence(input=layer, stride=[1, 1], filter=[2, 2])
""" """
if isinstance(filter_size, int):
filter_size = [filter_size, filter_size]
if isinstance(stride, int):
stride = [stride, stride]
if isinstance(padding, int):
padding = [padding, padding]
if len(padding) == 2:
padding.append(padding[0])
padding.append(padding[1])
helper = LayerHelper('im2sequence', **locals()) helper = LayerHelper('im2sequence', **locals())
out = helper.create_tmp_variable(dtype=helper.input_dtype()) out = helper.create_tmp_variable(dtype=helper.input_dtype())
helper.append_op( helper.append_op(
...@@ -2079,11 +2093,8 @@ def im2sequence(input, ...@@ -2079,11 +2093,8 @@ def im2sequence(input,
inputs={'X': input}, inputs={'X': input},
outputs={'Out': out}, outputs={'Out': out},
attrs={ attrs={
'block_height': block_y, 'kernels': filter_size,
'block_width': block_x, 'strides': stride,
'stride_height': stride_y, 'paddings': padding,
'stride_width': stride_x,
'padding_height': padding_y,
'padding_width': padding_x
}) })
return out return out
...@@ -225,6 +225,16 @@ class TestBook(unittest.TestCase): ...@@ -225,6 +225,16 @@ class TestBook(unittest.TestCase):
self.assertIsNotNone(out) self.assertIsNotNone(out)
print(str(program)) print(str(program))
def test_im2sequence(self):
print("test_im2sequence")
program = Program()
with program_guard(program):
x = layers.data(name='x', shape=[3, 128, 128], dtype='float32')
output = layers.im2sequence(
input=x, stride=[1, 1], filter_size=[2, 2])
self.assertIsNotNone(output)
print(str(program))
if __name__ == '__main__': if __name__ == '__main__':
unittest.main() unittest.main()
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册