clinicadl.networks.nn.ConvDecoder

class clinicadl.networks.nn.ConvDecoder(spatial_dims: int, in_channels: int, channels: Sequence[int], kernel_size: int | tuple[int, ...] | list[int | tuple[int, ...]] = 3, stride: int | tuple[int, ...] | list[int | tuple[int, ...]] = 1, padding: int | tuple[int, ...] | list[int | tuple[int, ...]] = 0, output_padding: int | tuple[int, ...] | list[int | tuple[int, ...]] = 0, dilation: int | tuple[int, ...] | list[int | tuple[int, ...]] = 1, unpooling: tuple[UnpoolingLayer, dict[str, Any]] | list[tuple[UnpoolingLayer, dict[str, Any]]] | None = (UnpoolingLayer.UPSAMPLE, {'scale_factor': 2}), unpooling_indices: Sequence[int] | None = None, act: ActFunction | tuple[ActFunction, dict[str, Any]] | None = ActFunction.PRELU, output_act: ActFunction | tuple[ActFunction, dict[str, Any]] | None = None, norm: ConvNormLayer | tuple[ConvNormLayer, dict[str, Any]] | None = ConvNormLayer.INSTANCE, dropout: float | None = None, bias: bool = True, adn_ordering: str = 'NDA', _input_size: Sequence[int] | None = None) None[source]

Fully convolutional decoder network with transposed convolutions, unpooling, normalization, activation and dropout layers.

It is the symmetric of ConvEncoder, where convolutions are replaced by transposed convolutions, and pooling layers by unpooling layers.

Works with 2D or 3D images (with additional batch and channel dimensions).

Parameters:
  • spatial_dims (int) – Number of spatial dimensions of the input image.

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

  • channels (Sequence[int]) – Number of output channels of each transposed convolution. Thus, this parameter also controls the number of transposed convolutions (equal to the length of the sequence).

  • kernel_size (ConvParameters, default=3) –

    Kernel size of the transposed convolutions. Can be an int, a tuple, or a list:

    • int: the value will be used for all layers and all dimensions;

    • tuple (e.g. (3, 3, 2)): it will be interpreted as the values for each dimension. These values will be used for all the layers;

    • list (e.g. [(3, 3, 2), 3]): it will be interpreted as the kernel sizes for each layer. The length of the list must be equal to the number of transposed convolutions (i.e. len(channels)).

  • stride (ConvParameters, default=1) – Stride of the transposed convolutions. Can be an int, a tuple, or a list, and is passed in the same way as kernel_size.

  • padding (ConvParameters, default=0) – Padding of the transposed convolutions. Can be an int, a tuple, or a list, and is passed in the same way as kernel_size.

  • output_padding (ConvParameters, default=0) – Output padding of the transposed convolutions. Can be an int, a tuple, or a list, and is passed in the same way as kernel_size.

  • dilation (ConvParameters, default=1) – Dilation factor of the transposed convolutions. Can be an int, a tuple, or a list, and is passed in the same way as kernel_size.

  • unpooling (Optional[UnpoolingParameters], default=("upsample", {"scale_factor": 2})) –

    The unpooling mode and the arguments of the unpooling layer, passed as (unpooling_mode, arguments), where arguments is a dictionary. If None, no unpooling will be performed in the network.

    unpooling_mode can be either upsample or convtranspose. Please refer to torch.nn.Upsample or torch.nn.ConvTranspose3d to know their arguments.

    If a list is passed, it will be understood as the unpooling for each unpooling layer.

    Note

    No need to pass in_channels and out_channels for convtranspose, because the unpooling layers are not intended to modify the number of channels here.

  • unpooling_indices (Optional[Sequence[int]], default=None) – Indices of the transposed convolutions after which unpooling should be performed. If None, no unpooling will be performed. An index equal to -1 will be understood as a pooling layer before the first transposed convolution.

  • act (Optional[ActivationParameters], default="prelu") –

    The activation function used after a transposed convolution, and optionally its arguments. Must be passed as activation_name or (activation_name, arguments), where arguments is a dictionary. If None, no activation will be used.

    activation_name can be any value in {"celu", "elu", "gelu", "leakyrelu", "logsoftmax", "mish", "prelu", "relu", "relu6", "selu", "sigmoid", "softmax", "tanh"}. Please refer to PyTorch activation functions to know the arguments for each of them.

  • output_act (Optional[ActivationParameters], default=None) – A potential activation layer applied to the output of the network. Must be passed in the same way as act. If None, no last activation will be applied.

  • norm (Optional[ConvNormalizationParameters], default="instance") –

    The normalization layer used after a transposed convolution, and optionally its arguments. Must be passed as norm_type or (norm_type, arguments) where arguments is a dictionary. If None, no normalization will be performed.

    norm_type can be any value in {"batch", "group", "instance", "syncbatch"}. Please refer to PyTorch normalization layers to know the arguments for each of them.

    Note

    Please note that there’s no need to pass the arguments num_channels and num_features of the normalization layer, as they are automatically inferred from the output of the previous layer in the network.

  • dropout (Optional[float], default=None) – Dropout ratio. If None, no dropout.

  • bias (bool, default=True) – Whether to have a bias term in linear layers.

  • adn_ordering (str) – Order of operations Activation, Dropout and Normalization, after a linear layer (except the last one). Cannot contain duplicated letters. For example if "ND" is passed, Normalization and then Dropout will be performed (without Activation).

Raises:
  • ValueError – If a list is passed for kernel_size, stride, padding, output_padding, or dilation, and the size of this list in not equal to len(channels).

  • ValueError – If indices in unpooling_indices are greater than len(channels)-1 (len(channels)-1 being the index of the last transposed convolution).

  • ValueError – If a list is passed for unpooling, and len(unpooling)!=len(unpooling_indices).

  • ValueError – If the activation or normalization layer requires a mandatory argument, which is not passed by the user (via a dictionary in act or norm).

See also

torch.nn.Module

To see all the methods of this neural network.

ConvEncoder

Examples

>>> ConvDecoder(
        in_channels=16,
        spatial_dims=2,
        channels=[8, 4, 1],
        kernel_size=(3, 5),
        stride=2,
        padding=[1, 0, 0],
        output_padding=[0, 0, (1, 2)],
        dilation=1,
        unpooling=[("upsample", {"scale_factor": 2}), ("upsample", {"size": (32, 32)})],
        unpooling_indices=[0, 1],
        act="elu",
        output_act="relu",
        norm=("batch", {"eps": 1e-05}),
        dropout=0.1,
        bias=True,
        adn_ordering="NDA",
    )
ConvDecoder(
    (layer0): Convolution(
        (conv): ConvTranspose2d(16, 8, kernel_size=(3, 5), stride=(2, 2), padding=(1, 1))
        (adn): ADN(
            (N): BatchNorm2d(8, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
            (D): Dropout(p=0.1, inplace=False)
            (A): ELU(alpha=1.0)
        )
    )
    (unpool0): Upsample(scale_factor=2.0, mode='nearest')
    (layer1): Convolution(
        (conv): ConvTranspose2d(8, 4, kernel_size=(3, 5), stride=(2, 2))
        (adn): ADN(
            (N): BatchNorm2d(4, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
            (D): Dropout(p=0.1, inplace=False)
            (A): ELU(alpha=1.0)
        )
    )
    (unpool1): Upsample(size=(32, 32), mode='nearest')
    (layer2): Convolution(
        (conv): ConvTranspose2d(4, 1, kernel_size=(3, 5), stride=(2, 2), output_padding=(1, 2))
    )
    (output_act): ReLU()
)