Shortcuts

MaxUnpool2d

class torch.nn.MaxUnpool2d(kernel_size, stride=None, padding=0)[source]

Computes a partial inverse of MaxPool2d.

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

MaxUnpool2d takes in as input the output of MaxPool2d including the indices of the maximal values and computes a partial inverse in which all non-maximal values are set to zero.

Note

This operation may behave nondeterministically when the input indices has repeat values. See https://github.com/pytorch/pytorch/issues/80827 and Reproducibility for more information.

Note

MaxPool2d can map several input sizes to the same output sizes. Hence, the inversion process can get ambiguous. To accommodate this, you can provide the needed output size as an additional argument output_size in the forward call. See the Inputs and Example below.

Parameters
  • kernel_size (int or tuple) – Size of the max pooling window.

  • stride (int or tuple) – Stride of the max pooling window. It is set to kernel_size by default.

  • padding (int or tuple) – Padding that was added to the input

Inputs:
  • input: the input Tensor to invert

  • indices: the indices given out by MaxPool2d

  • output_size (optional): the targeted output size

Shape:
  • Input: (N,C,Hin,Win)(N, C, H_{in}, W_{in}) or (C,Hin,Win)(C, H_{in}, W_{in}).

  • Output: (N,C,Hout,Wout)(N, C, H_{out}, W_{out}) or (C,Hout,Wout)(C, H_{out}, W_{out}), where

    Hout=(Hin1)×stride[0]2×padding[0]+kernel_size[0]H_{out} = (H_{in} - 1) \times \text{stride[0]} - 2 \times \text{padding[0]} + \text{kernel\_size[0]}
    Wout=(Win1)×stride[1]2×padding[1]+kernel_size[1]W_{out} = (W_{in} - 1) \times \text{stride[1]} - 2 \times \text{padding[1]} + \text{kernel\_size[1]}

    or as given by output_size in the call operator

Example:

>>> pool = nn.MaxPool2d(2, stride=2, return_indices=True)
>>> unpool = nn.MaxUnpool2d(2, stride=2)
>>> input = torch.tensor([[[[ 1.,  2.,  3.,  4.],
                            [ 5.,  6.,  7.,  8.],
                            [ 9., 10., 11., 12.],
                            [13., 14., 15., 16.]]]])
>>> output, indices = pool(input)
>>> unpool(output, indices)
tensor([[[[  0.,   0.,   0.,   0.],
          [  0.,   6.,   0.,   8.],
          [  0.,   0.,   0.,   0.],
          [  0.,  14.,   0.,  16.]]]])
>>> # Now using output_size to resolve an ambiguous size for the inverse
>>> input = torch.tensor([[[[ 1.,  2.,  3., 4., 5.],
                            [ 6.,  7.,  8., 9., 10.],
                            [11., 12., 13., 14., 15.],
                            [16., 17., 18., 19., 20.]]]])
>>> output, indices = pool(input)
>>> # This call will not work without specifying output_size
>>> unpool(output, indices, output_size=input.size())
tensor([[[[ 0.,  0.,  0.,  0.,  0.],
          [ 0.,  7.,  0.,  9.,  0.],
          [ 0.,  0.,  0.,  0.,  0.],
          [ 0., 17.,  0., 19.,  0.]]]])

Docs

Access comprehensive developer documentation for PyTorch

View Docs

Tutorials

Get in-depth tutorials for beginners and advanced developers

View Tutorials

Resources

Find development resources and get your questions answered

View Resources