mindspore.mint.nn.ConvTranspose2d

class mindspore.mint.nn.ConvTranspose2d(in_channels, out_channels, kernel_size, stride=1, padding=0, output_padding=0, groups=1, bias=True, dilation=1, padding_mode='zeros', dtype=None)[source]

Applies a 2D transposed convolution operator over an input image composed of several input planes.

This module can be seen as the gradient of Conv2d with respect to its input. It is also known as a fractionally-strided convolution or a deconvolution (although it is not an actual deconvolution operation as it does not compute a true inverse of convolution).

The parameters kernel_size, stride, padding, output_padding can either be:

  • a single int – in which case the same value is used for the height and width dimensions

  • a tuple of two ints – in which case, the first int is used for the height dimension, and the second int for the width dimension

Warning

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

  • In the scenario where inputs are non-contiguous, output_padding must be less than stride .

  • For Atlas training products, when the dtype of input is float32, the groups only supports 1.

Parameters
  • in_channels (int) – Number of channels in the input image.

  • out_channels (int) – Number of channels produced by the convolution.

  • kernel_size (Union[int, tuple(int)]) – Size of the convolving kernel.

  • stride (Union[int, tuple(int)], optional) – Stride of the convolution. Default: 1 .

  • padding (Union[int, tuple(int)], optional) – \(dilation * (kernel\_size - 1) - padding\) zero-padding will be added to both sides of each dimension in the input. Default: 0 .

  • output_padding (Union[int, tuple(int)], optional) – Additional size added to one side of each dimension in the output shape. The value of output_padding must be less than stride or dilation . Default: 0 .

  • groups (int, optional) – Number of blocked connections from input channels to output channels. Default: 1

  • bias (bool, optional) – If True, adds a learnable bias to the output. Default: True .

  • dilation (Union[int, tuple(int)], optional) – Spacing between kernel elements. Default: 1 .

  • padding_mode (str, optional) – Specifies the padding mode with a padding value. For now, it can only be set to: "zeros". Default: "zeros" .

  • dtype (mindspore.dtype, optional) – Dtype of Parameters. Default: None , when it's None , the dtype of Parameters would be mstype.float32.

Variables:
  • weigh (Parameter) - the learnable weights of the module of shape \((\text{in_channels}, \frac{\text{out_channels}}{\text{groups}}, \text{kernel_size[0]}, \text{kernel_size[1]})\) . The values of these weights are sampled from \(\mathcal{U}(-\sqrt{k}, \sqrt{k})\) where \(k = \frac{groups}{C_\text{out} * \prod_{i=0}^{1}\text{kernel_size}[i]}\)

  • bias (Parameter) - the learnable bias of the module of shape \((\text{out_channels},)\) . If bias is True, then the values of these weights are sampled from \(\mathcal{U}(-\sqrt{k}, \sqrt{k})\) where \(k = \frac{groups}{C_\text{out} * \prod_{i=0}^{1}\text{kernel_size}[i]}\) .

Inputs:
  • input (Tensor) - Tensor of shape \((N, C_{in}, H_{in}, W_{in})\) or \((C_{in}, H_{in}, W_{in})\) .

Outputs:

Tensor of shape \((N, C_{out}, H_{out}, W_{out})\) or \((C_{out}, H_{out}, W_{out})\), where

\[H_{out} = (H_{in} - 1) \times \text{stride}[0] - 2 \times \text{padding}[0] + \text{dilation}[0] \times (\text{kernel_size}[0] - 1) + \text{output_padding}[0] + 1\]
\[W_{out} = (W_{in} - 1) \times \text{stride}[1] - 2 \times \text{padding}[1] + \text{dilation}[1] \times (\text{kernel_size}[1] - 1) + \text{output_padding}[1] + 1\]
Supported Platforms:

Ascend

Examples

>>> import mindspore as ms
>>> from mindspore import mint
>>> # With square kernels and equal stride
>>> m = mint.nn.ConvTranspose2d(16, 33, 3, stride=2)
>>> # non-square kernels and unequal stride and with padding
>>> m = mint.nn.ConvTranspose2d(16, 33, (3, 5), stride=(2, 1), padding=(4, 2))
>>> input = mint.randn(20, 16, 50, 100)
>>> output = m(input)
>>> # exact output size can be also specified as an argument
>>> input = mint.randn(1, 16, 12, 12)
>>> downsample = mint.nn.Conv2d(16, 16, 3, stride=2, padding=1)
>>> upsample = mint.nn.ConvTranspose2d(16, 16, 3, stride=2, padding=1)
>>> h = downsample(input)
>>> h.shape
(1, 16, 6, 6)
>>> output = upsample(h, output_size=input.shape)
>>> output.shape
(1, 16, 12, 12)