clinicadl.networks.nn.ConvEncoder

class clinicadl.networks.nn.ConvEncoder(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, dilation: int | tuple[int, ...] | list[int | tuple[int, ...]] = 1, pooling: tuple[PoolingLayer, dict[str, Any]] | list[tuple[PoolingLayer, dict[str, Any]]] | None = (PoolingLayer.MAX, {'kernel_size': 2}), pooling_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 encoder network with convolutional, pooling, normalization, activation and dropout 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 convolutional layer. Thus, this parameter also controls the number of convolutional layers (equal to the length of the sequence).

  • kernel_size (ConvParameters, default=3) –

    Kernel size of the convolutional layers. 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 convolutional layers (i.e. len(channels)).

  • stride (ConvParameters, default=1) – Stride of the convolutional layers. 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 convolutional layers. 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 convolutional layers. Can be an int, a tuple, or a list, and is passed in the same way as kernel_size.

  • pooling (Optional[PoolingParameters], default=("max", {"kernel_size": 2})) –

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

    pooling_mode can be any value in {"max", "avg", "adaptivemax", "adaptiveavg"}. Please refer to PyTorch pooling layers to know the arguments for each of them.

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

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

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

    The activation function used after a convolutional layer, 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 convolutional layer, 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, or dilation, and the size of this list in not equal to len(channels).

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

  • ValueError – If a list is passed for pooling, and len(pooling)!=len(pooling_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.

ConvDecoder

Examples

>>> ConvEncoder(
        spatial_dims=2,
        in_channels=1,
        channels=[2, 4, 8],
        kernel_size=(3, 5),
        stride=1,
        padding=[1, (0, 1), 0],
        dilation=1,
        pooling=[("max", {"kernel_size": 2}), ("avg", {"kernel_size": 2})],
        pooling_indices=[0, 1],
        act="elu",
        output_act="relu",
        norm=("batch", {"eps": 1e-05}),
        dropout=0.1,
        bias=True,
        adn_ordering="NDA",
    )
ConvEncoder(
    (layer0): Convolution(
        (conv): Conv2d(1, 2, kernel_size=(3, 5), stride=(1, 1), padding=(1, 1))
        (adn): ADN(
            (N): BatchNorm2d(2, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
            (D): Dropout(p=0.1, inplace=False)
            (A): ELU(alpha=1.0)
        )
    )
    (pool0): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)
    (layer1): Convolution(
        (conv): Conv2d(2, 4, kernel_size=(3, 5), stride=(1, 1), padding=(0, 1))
        (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)
        )
    )
    (pool1): AvgPool2d(kernel_size=2, stride=2, padding=0)
    (layer2): Convolution(
        (conv): Conv2d(4, 8, kernel_size=(3, 5), stride=(1, 1))
    )
    (output_act): ReLU()
)