Skip to content

Layers

Source

Layer module.

This submodule contains layers used in the CAREamics models.

Conv_Block

Bases: Module

Convolution block used in UNets.

Convolution block consist of two convolution layers with optional batch norm, dropout and with a final activation function.

The parameters are directly mapped to PyTorch Conv2D and Conv3d parameters, see PyTorch torch.nn.Conv2d and torch.nn.Conv3d for more information.

Parameters:

  • conv_dim (int) –

    Number of dimension of the convolutions, 2 or 3.

  • in_channels (int) –

    Number of input channels.

  • out_channels (int) –

    Number of output channels.

  • intermediate_channel_multiplier (int, default: 1 ) –

    Multiplied for the number of output channels, by default 1.

  • stride (int, default: 1 ) –

    Stride of the convolutions, by default 1.

  • padding (int, default: 1 ) –

    Padding of the convolutions, by default 1.

  • bias (bool, default: True ) –

    Bias of the convolutions, by default True.

  • groups (int, default: 1 ) –

    Controls the connections between inputs and outputs, by default 1.

  • activation (str, default: 'ReLU' ) –

    Activation function, by default "ReLU".

  • dropout_perc (float, default: 0 ) –

    Dropout percentage, by default 0.

  • use_batch_norm (bool, default: False ) –

    Use batch norm, by default False.

__init__(conv_dim, in_channels, out_channels, intermediate_channel_multiplier=1, stride=1, padding=1, bias=True, groups=1, activation='ReLU', dropout_perc=0, use_batch_norm=False)

Constructor.

Parameters:

  • conv_dim (int) –

    Number of dimension of the convolutions, 2 or 3.

  • in_channels (int) –

    Number of input channels.

  • out_channels (int) –

    Number of output channels.

  • intermediate_channel_multiplier (int, default: 1 ) –

    Multiplied for the number of output channels, by default 1.

  • stride (int, default: 1 ) –

    Stride of the convolutions, by default 1.

  • padding (int, default: 1 ) –

    Padding of the convolutions, by default 1.

  • bias (bool, default: True ) –

    Bias of the convolutions, by default True.

  • groups (int, default: 1 ) –

    Controls the connections between inputs and outputs, by default 1.

  • activation (str, default: 'ReLU' ) –

    Activation function, by default "ReLU".

  • dropout_perc (float, default: 0 ) –

    Dropout percentage, by default 0.

  • use_batch_norm (bool, default: False ) –

    Use batch norm, by default False.

forward(x)

Forward pass.

Parameters:

  • x (Tensor) –

    Input tensor.

Returns:

  • Tensor

    Output tensor.

MaxBlurPool

Bases: Module

Compute pools and blurs and downsample a given feature map.

Inspired by Kornia MaxBlurPool implementation. Equivalent to nn.Sequential(nn.MaxPool2d(...), BlurPool2D(...))

Parameters

dim : int Toggles between 2D and 3D. kernel_size : Union[tuple[int, int], int] Kernel size for max pooling. stride : int Stride for pooling. max_pool_size : int Max kernel size for max pooling. ceil_mode : bool Ceil mode, by default False. Set to True to match output size of conv2d.

__init__(dim, kernel_size, stride=2, max_pool_size=2, ceil_mode=False)

Constructor.

Parameters:

  • dim (int) –

    Dimension of the convolution.

  • kernel_size (Union[tuple[int, int], int]) –

    Kernel size for max pooling.

  • stride (int, default: 2 ) –

    Stride, by default 2.

  • max_pool_size (int, default: 2 ) –

    Maximum pool size, by default 2.

  • ceil_mode (bool, default: False ) –

    Ceil mode, by default False. Set to True to match output size of conv2d.

forward(x)

Forward pass of the function.

Parameters:

  • x (Tensor) –

    Input tensor.

Returns:

  • Tensor

    Output tensor.

get_pascal_kernel_1d(kernel_size, norm=False, *, device=None, dtype=None)

Generate Yang Hui triangle (Pascal's triangle) for a given number.

Inspired by Kornia implementation. TODO link

Parameters:

  • kernel_size (int) –

    Kernel size.

  • norm (bool, default: False ) –

    Normalize the kernel, by default False.

  • device (Optional[device], default: None ) –

    Device of the tensor, by default None.

  • dtype (Optional[dtype], default: None ) –

    Data type of the tensor, by default None.

Returns:

  • Tensor

    Pascal kernel.

Examples:

>>> get_pascal_kernel_1d(1)
tensor([1.])
>>> get_pascal_kernel_1d(2)
tensor([1., 1.])
>>> get_pascal_kernel_1d(3)
tensor([1., 2., 1.])
>>> get_pascal_kernel_1d(4)
tensor([1., 3., 3., 1.])
>>> get_pascal_kernel_1d(5)
tensor([1., 4., 6., 4., 1.])
>>> get_pascal_kernel_1d(6)
tensor([ 1.,  5., 10., 10.,  5.,  1.])