mindspore.ops.max_unpool2d

mindspore.ops.max_unpool2d(x, indices, kernel_size, stride=None, padding=0, output_size=None)[source]

Computes a partial inverse of maxpool2d.

maxpool2d is not fully invertible, since the non-maximal values are lost.

max_unpool2d takes the output of maxpool2d as inputs including the indices of the maximal values and computes a partial inverse in which all non-maximal values are set to zero. Typically the input is of shape \((N, C, H_{in}, W_{in})\) or \((C, H_{in}, W_{in})\), and the output is of shape \((N, C, H_{out}, W_{out})\) or \((C, H_{out}, W_{out})\). The operation is as follows.

\[\begin{split}\begin{array}{ll} \\ H_{out} = (H{in} - 1) \times stride[0] - 2 \times padding[0] + kernel\_size[0] \\ W_{out} = (W{in} - 1) \times stride[1] - 2 \times padding[1] + kernel\_size[1] \\ \end{array}\end{split}\]
Parameters
  • x (Tensor) – The input Tensor to invert. Tensor of shape \((N, C, H_{in}, W_{in})\) or \((C, H_{in}, W_{in})\).

  • indices (Tensor) – Max values’ index represented by the indices. Tensor of shape must be same with input ‘x’. Values of indices must belong to \([0, H_{in} \times W_{in} - 1]\). Data type must be in int32 or int64.

  • kernel_size (Union[int, tuple[int]]) – The size of kernel used to take the maximum value, an int number that represents height and width of the kernel, or a tuple of two int numbers that represent height and width respectively.

  • stride (Union[int, tuple[int]]) – The distance of kernel moving, an int number that represents the height and width of movement are both stride, or a tuple of two int numbers that represent height and width of movement respectively. If stride is 0, (0, 0) or None, then stride equal to kernel_size. Default: None.

  • padding (Union[int, tuple[int]]) – The pad value to be filled. Default: 0. If padding is an integer, the paddings of height and width are the same, equal to padding. If padding is a tuple of two integers, the padding of height and width equal to padding[0] and padding[1] correspondingly.

  • output_size (tuple[int], optional) – The target output size. Default: None. If output_size == (), then the shape of output computed by kernel_size, stride and padding. If output_size != (), then output_size must be \((N, C, H, W)\) and output_size must belong to \([(N, C, H_{out} - stride[0], W_{out} - stride[1]), (N, C, H_{out} + stride[0], W_{out} + stride[1])]\).

Returns

Tensor, with shape \((N, C, H_{out}, W_{out})\) or \((C, H_{out}, W_{out})\), with the same data type with x.

Raises
  • TypeError – If data type of x or indices is not supported.

  • TypeError – If kernel_size, stride or padding is neither an int nor a tuple.

  • ValueError – If numbers in stride, padding (also support 0 and (0, 0)) or kernel_size is not positive.

  • ValueError – If the shape of x and indices are not equal.

  • ValueError – If kernel_size, stride or padding is a tuple whose length is not equal to 2.

  • ValueError – If x whose length is not 3 or 4.

  • ValueError – If output_size whose type is not tuple.

  • ValueError – If output_size whose length is not 0, 3 or 4.

  • ValueError – If output_size is not close to output size computed by attr kernel_size, stride, padding.

Supported Platforms:

GPU CPU

Examples

>>> x = Tensor(np.array([[[[0, 1], [8, 9]]]]).astype(np.float32))
>>> indices = Tensor(np.array([[[[0, 1], [2, 3]]]]).astype(np.int64))
>>> output = ops.max_unpool2d(x, indices, kernel_size=1, stride=1, padding=0)
>>> print(output.asnumpy())
[[[[0. 1.]
   [8. 9.]]]]