# oneflow.nn.Unfold¶

class oneflow.nn.Unfold(kernel_size, dilation=1, padding=0, stride=1)

The documentation is referenced from: https://pytorch.org/docs/1.10/generated/torch.nn.Unfold.html.

This op extracts elements in a local window from input tensor, it also called img2col.

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 padding, stride and dilation arguments specify how the sliding blocks are retrieved.

• stride controls the stride for the sliding blocks.

• padding controls the amount of implicit zero-paddings on both sides for padding number of points for each dimension before reshaping.

• dilation controls the spacing between the kernel points; also known as the à trous algorithm.

Parameters
• kernel_size (int or tuple) – the size of the sliding blocks

• stride (int or tuple, optional) – the stride of the sliding blocks in the input spatial dimensions. Default: 1

• padding (int or tuple, optional) – implicit zero padding to be added on both sides of input. Default: 0

• dilation (int or tuple, optional) – a parameter that controls the stride of elements within the neighborhood. Default: 1

• If kernel_size, dilation, padding or stride is an int or a tuple of length 1, their values will be replicated across all spatial dimensions.

• For the case of two input spatial dimensions this operation is sometimes called im2col.

Note

Fold calculates each combined value in the resulting large tensor by summing all values from all containing blocks. Unfold extracts the values in the local blocks by copying from the large tensor. So, if the blocks overlap, they are not inverses of each other.

In general, folding and unfolding operations are related as follows. Consider Fold and Unfold instances created with the same parameters:

>>> fold_params = dict(kernel_size=..., dilation=..., padding=..., stride=...)
>>> fold = nn.Fold(output_size=..., **fold_params)
>>> unfold = nn.Unfold(**fold_params)


Then for any (supported) input tensor the following equality holds:

::

fold(unfold(input)) == divisor * input

where divisor is a tensor that depends only on the shape and dtype of the input:

>>> input_ones = oneflow.ones(input.shape, dtype=input.dtype)
>>> divisor = fold(unfold(input_ones))


When the divisor tensor contains no zero elements, then fold and unfold operations are inverses of each other (up to constant divisor).

Warning

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

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

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

For example:

>>> import oneflow as flow
>>> import numpy as np

>>> x_tensor = flow.Tensor(np.random.randn(1, 1, 4, 4))