mindspore.ops.Im2Col

class mindspore.ops.Im2Col(ksizes, strides=1, dilations=1, pads=0)[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 ksizes- sized block within the spatial dimensions of input x into a column (i.e., last dimension) of a 4-D output tensor of shape \((N, C, \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{pads}[d] % - \text{dilations}[d] \times (\text{kernel_size}[d] - 1) - 1}{\text{strides}[d]} + 1\right\rfloor,\]

where \(\text{spatial_size}\) is formed by the spatial dimensions of input x (\(*\) 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 pads, strides and dilations arguments specify how the sliding blocks are retrieved.

Note

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

Warning

This is an experimental API that is subject to change or deletion.

Parameters
  • ksizes (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.

  • strides (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 .

  • dilations (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 .

  • pads (Union[int, tuple[int], list[int]], optional) –

    The pad of the window, that must be a tuple of one or two int for height and width. Default: 0 .

    • If one int, \(pad\_height = pad\_width\).

    • If two int, \(pad\_height = pads[0]\), \(pad\_width = pads[1]\).

Inputs:
  • x (Tensor) - input tensor, only 4-D input tensors (batched image-like tensors) are supported.

Outputs:

Tensor, a 4-D Tensor with same type of input x.

Raises
  • TypeError – If ksizes data type is not in Union[int, tuple[int], list[int]].

  • TypeError – If strides data type is not in Union[int, tuple[int], list[int]].

  • TypeError – If dilations data type is not in Union[int, tuple[int], list[int]].

  • TypeError – If pads data type isnot in Union[int, tuple[int], list[int]].

  • ValueError – If ksizes value is not greater than zero or elements number more than 2.

  • ValueError – If strides value is not greater than zero or elements number more than 2.

  • ValueError – If dilations value is not greater than zero or elements number more than 2.

  • ValueError – If pads value is not greater than zero.

Supported Platforms:

Ascend GPU CPU

Examples

>>> import numpy as np
>>> from mindspore import Tensor, ops
>>> from mindspore import dtype as mstype
>>> x = Tensor(input_data=np.random.rand(4, 4, 32, 32), dtype=mstype.float64)
>>> im2col = ops.Im2Col(ksizes=3, strides=1, dilations=1)
>>> y = im2col(x)
>>> print(y.shape)
(4, 4, 9, 900)