Skip to content

Commit 186682f

Browse files
authored
add paddle.nn.unfold #32297 (#32298)
* add paddle.nn.unfold * update Parameters of Unfold
1 parent e348901 commit 186682f

3 files changed

Lines changed: 100 additions & 0 deletions

File tree

python/paddle/fluid/tests/unittests/test_unfold_op.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818
import numpy as np
1919
import unittest
2020
from op_test import OpTest
21+
import paddle
22+
import paddle.fluid as fluid
23+
from paddle.fluid import core
2124

2225

2326
class TestUnfoldOp(OpTest):
@@ -98,5 +101,30 @@ def test_check_grad(self):
98101
self.check_grad(['X'], 'Y')
99102

100103

104+
class TestUnfoldAPI(TestUnfoldOp):
105+
"""
106+
This is for test on paddle.nn.Unfold
107+
"""
108+
109+
def setUp(self):
110+
self.op_type = 'unfold'
111+
self.set_data()
112+
self.places = [fluid.CPUPlace()]
113+
if core.is_compiled_with_cuda():
114+
self.places.append(fluid.CUDAPlace(0))
115+
116+
def test_dygraph(self):
117+
for place in self.places:
118+
with fluid.dygraph.guard(place):
119+
input = fluid.dygraph.to_variable(self.inputs['X'])
120+
m = paddle.nn.Unfold(**self.attrs)
121+
m.eval()
122+
result = m(input)
123+
self.assertTrue(np.allclose(result.numpy(), self.outputs['Y']))
124+
125+
def test_info(self):
126+
str(paddle.nn.Unfold(**self.attrs))
127+
128+
101129
if __name__ == '__main__':
102130
unittest.main()

python/paddle/nn/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@
8383
from .layer.common import Dropout2D #DEFINE_ALIAS
8484
from .layer.common import Dropout3D #DEFINE_ALIAS
8585
from .layer.common import AlphaDropout #DEFINE_ALIAS
86+
from .layer.common import Unfold #DEFINE_ALIAS
8687

8788
from .layer.pooling import AvgPool1D #DEFINE_ALIAS
8889
from .layer.pooling import AvgPool2D #DEFINE_ALIAS

python/paddle/nn/layer/common.py

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
'Dropout3D',
3636
'Bilinear',
3737
'AlphaDropout',
38+
'Unfold',
3839
]
3940

4041

@@ -1380,3 +1381,73 @@ def extra_repr(self):
13801381
if self._name is not None:
13811382
main_str += ', name={_name}'
13821383
return main_str.format(**self.__dict__)
1384+
1385+
1386+
class Unfold(layers.Layer):
1387+
"""
1388+
This op returns a col buffer of sliding local blocks of input x, also known
1389+
as im2col for batched 2D image tensors. For each block under the convolution filter,
1390+
all element will be rearranged as a column. While the convolution filter sliding over
1391+
the input feature map, a series of such columns will be formed.
1392+
1393+
For each input :math:`x` with shape [N, C, H, W], the output shape [N, Cout, Lout]
1394+
can be calculated as following.
1395+
1396+
See ``paddle.nn.functional.unfold`` for more details.
1397+
1398+
1399+
Parameters:
1400+
kernel_sizes(int|list): The size of convolution kernel, should be [k_h, k_w]
1401+
or an integer k treated as [k, k].
1402+
strides(int|list): The strides, should be [stride_h, stride_w]
1403+
or an integer stride treated as [sride, stride].
1404+
For default, strides will be [1, 1].
1405+
paddings(int|list): The paddings of each dimension, should be
1406+
[padding_top, padding_left, padding_bottom, padding_right]
1407+
or [padding_h, padding_w] or an integer padding.
1408+
If [padding_h, padding_w] was given, it will expanded to
1409+
[padding_h, padding_w, padding_h, padding_w]. If an integer
1410+
padding was given, [padding, padding, padding, padding] will
1411+
be used. For default, paddings will be [0, 0, 0, 0]
1412+
dilations(int|list): the dilations of convolution kernel, should be
1413+
[dilation_h, dilation_w], or an integer dilation treated as
1414+
[dilation, dilation]. For default, it will be [1, 1].
1415+
name(str, optional): The default value is None.
1416+
Normally there is no need for user to set this property.
1417+
For more information, please refer to :ref:`api_guide_Name`
1418+
1419+
1420+
Examples:
1421+
.. code-block:: python
1422+
1423+
import paddle
1424+
import paddle.nn as nn
1425+
1426+
x = paddle.randn((100,3,224,224))
1427+
unfold = nn.Unfold(kernel_sizes=[3, 3])
1428+
result = unfold(x)
1429+
print(result)
1430+
"""
1431+
1432+
def __init__(self,
1433+
kernel_sizes,
1434+
dilations=1,
1435+
paddings=0,
1436+
strides=1,
1437+
name=None):
1438+
super(Unfold, self).__init__()
1439+
1440+
self.kernel_sizes = kernel_sizes
1441+
self.dilations = dilations
1442+
self.paddings = paddings
1443+
self.strides = strides
1444+
self.name = name
1445+
1446+
def forward(self, input):
1447+
return F.unfold(input, self.kernel_sizes, self.dilations, self.paddings,
1448+
self.strides, self.name)
1449+
1450+
def extra_repr(self):
1451+
name_str = ', name={}'.format(self.name) if self.name else ''
1452+
return 'kernel_size={}, dilation={}, padding={}, stride={}{}'.\
1453+
format(self.kernel_sizes, self.dilations, self.paddings, self.strides, name_str)

0 commit comments

Comments
 (0)