clinicadl.networks.nn.CNN

class clinicadl.networks.nn.CNN(in_shape: Sequence[int], num_outputs: int, conv_args: dict[str, Any], mlp_args: dict[str, Any] | None = None) None[source]

A regressor/classifier with first convolutional layers and then fully connected layers.

This network is a simple aggregation of a ConvEncoder and a MLP.

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

Parameters:
  • in_shape (Sequence[int]) – Dimensions of the input tensor (without batch dimension).

  • num_outputs (int) – Number of variables to predict.

  • conv_args (Dict[str, Any]) – The arguments for the convolutional part. The arguments are those accepted by ConvEncoder, except spatial_dims and in_channels that are specified here via in_shape. So, the only mandatory argument is channels.

  • mlp_args (Optional[Dict[str, Any]], default=None) –

    The arguments for the MLP part. The arguments are those accepted by MLP, except num_inputs that is inferred from the output of the convolutional part, and num_outputs that is set here. So, the only mandatory argument is hidden_dims.

    If None, the MLP part will be reduced to a single linear layer.

Raises:
  • ValueError – If conv_args doesn’t contain the key channels.

  • ValueError – If mlp_args is not None and doesn’t contain the key hidden_dims.

See also

torch.nn.Module

To see all the methods of this neural network.

ConvEncoder, MLP

Examples

# a classifier
>>> CNN(
        in_shape=(1, 10, 10),
        num_outputs=2,
        conv_args={"channels": [2, 4], "norm": None, "act": None},
        mlp_args={"hidden_dims": [5], "act": "elu", "norm": None, "output_act": "softmax"},
    )
CNN(
    (convolutions): ConvEncoder(
        (layer0): Convolution(
            (conv): Conv2d(1, 2, kernel_size=(3, 3), stride=(1, 1))
        )
        (layer1): Convolution(
            (conv): Conv2d(2, 4, kernel_size=(3, 3), stride=(1, 1))
        )
    )
    (mlp): MLP(
        (flatten): Flatten(start_dim=1, end_dim=-1)
        (hidden0): Sequential(
            (linear): Linear(in_features=144, out_features=5, bias=True)
            (adn): ADN(
                (A): ELU(alpha=1.0)
            )
        )
        (output): Sequential(
            (linear): Linear(in_features=5, out_features=2, bias=True)
            (output_act): Softmax(dim=None)
        )
    )
)
# a regressor
>>> CNN(
        in_shape=(1, 10, 10),
        num_outputs=2,
        conv_args={"channels": [2, 4], "norm": None, "act": None},
    )
CNN(
    (convolutions): ConvEncoder(
        (layer0): Convolution(
            (conv): Conv2d(1, 2, kernel_size=(3, 3), stride=(1, 1))
        )
        (layer1): Convolution(
            (conv): Conv2d(2, 4, kernel_size=(3, 3), stride=(1, 1))
        )
    )
    (mlp): MLP(
        (flatten): Flatten(start_dim=1, end_dim=-1)
        (output): Linear(in_features=144, out_features=2, bias=True)
    )
)