|
35 | 35 | 'Dropout3D', |
36 | 36 | 'Bilinear', |
37 | 37 | 'AlphaDropout', |
| 38 | + 'Unfold', |
38 | 39 | ] |
39 | 40 |
|
40 | 41 |
|
@@ -1380,3 +1381,73 @@ def extra_repr(self): |
1380 | 1381 | if self._name is not None: |
1381 | 1382 | main_str += ', name={_name}' |
1382 | 1383 | 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