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
ConvEncoderand aMLP.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, exceptspatial_dimsandin_channelsthat are specified here viain_shape. So, the only mandatory argument ischannels.mlp_args (Optional[Dict[str, Any]], default=None) –
The arguments for the MLP part. The arguments are those accepted by
MLP, exceptnum_inputsthat is inferred from the output of the convolutional part, andnum_outputsthat is set here. So, the only mandatory argument ishidden_dims.If
None, the MLP part will be reduced to a single linear layer.
- Raises:
ValueError – If
conv_argsdoesn’t contain the keychannels.ValueError – If
mlp_argsis notNoneand doesn’t contain the keyhidden_dims.
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) ) )