mindspore.mint.nn.functional.unfold

mindspore.mint.nn.functional.unfold(input, kernel_size, dilation=1, padding=0, stride=1)[source]

Extracts sliding local blocks from a batched input tensor.

Consider a batched input tensor of shape \((N, C, *)\), where \(N\) is the batch dimension, \(C\) is the channel dimension, and \(*\) represent arbitrary spatial dimensions. This operation flattens each sliding Kernel_size- sized block within the spatial dimensions of input into a column (i.e., last dimension) of a 3-D output tensor of shape \((N, C \times \prod(\text{kernel_size}), L)\), where \(C \times \prod(\text{kernel_size})\) is the total number of values within each block (a block has \(\prod(\text{kernel_size})\) spatial locations each containing a C-channeled vector), and \(L\) is the total number of such blocks:

\[L = \prod_d \left\lfloor\frac{\text{spatial_size}[d] + 2 \times \text{padding}[d] % - \text{dilation}[d] \times (\text{kernel_size}[d] - 1) - 1}{\text{stride}[d]} + 1\right\rfloor,\]

where \(\text{spatial_size}\) is formed by the spatial dimensions of input (\(*\) above), and \(d\) is over all spatial dimensions.

Therefore, indexing output at the last dimension (column dimension) gives all values within a certain block.

The dilation, padding and stride arguments specify how the sliding blocks are retrieved.

Warning

  • Currently, batched(4D) image-like tensors are supported.

  • For Ascend, it is only supported on platforms above Atlas A2.

Parameters
  • input (Tensor) – 4-D Tensor.

  • kernel_size (Union[int, tuple[int], list[int]]) – The size of the kernel, should be two int for height and width. If type is int, it means that height equal with width. Must be specified.

  • dilation (Union[int, tuple[int], list[int]], optional) – The dilation of the window, should be two int for height and width. If type is int, it means that height equal with width. Default: 1 .

  • padding (Union[int, tuple[int], list[int]], optional) – The pad of the window, should be two int for height and width. If type is int, it means that height equal with width. Default: 0 .

  • stride (Union[int, tuple[int], list[int]], optional) – The stride of the window, should be two int for height and width. If type is int, it means that height equal with width. Default: 1 .

Returns

A Tensor, with same type as input .

Shape:
  • Input: \((N, C, *)\)

  • Output: \((N, C \times \prod(\text{kernel_size}), L)\)

Raises
  • TypeError – If any data type of kernel_size, stride, dilation, padding is not int, tuple or list.

  • ValueError – If kernel_size, dilation, stride value is not greater than zero or elements number more than 2.

  • ValueError – If padding value is less than zero.

Supported Platforms:

Ascend

Examples

>>> import mindspore
>>> import numpy as np
>>> from mindspore import Tensor, mint
>>> x = Tensor(np.random.rand(4, 4, 32, 32), mindspore.float32)
>>> output = mint.nn.functional.unfold(x, kernel_size=3, dilation=1, stride=1)
>>> print(output.shape)
(4, 36, 900)