PARCtorch.utilities package
Submodules
PARCtorch.utilities.resnet module
- class PARCtorch.utilities.resnet.ResNet(in_channels, block_dimensions, kernel_size=3, pooling=False, padding_mode='constant')
Bases:
ModuleResNet model consisting of multiple residual blocks with varying feature dimensions.
The model initializes with a convolutional layer and iteratively adds residual blocks based on the provided block_dimensions. Optionally, it applies max pooling after each residual block to reduce spatial dimensions.
- Parameters:
in_channels (int) – Number of input channels.
block_dimensions (List[int]) – List specifying the number of feature channels for each residual block.
kernel_size (int, optional) – Size of the convolutional kernel. Default is 3.
pooling (bool, optional) – Whether to apply max pooling after each residual block. Default is False.
padding_mode (str, optional) – Padding mode for convolutional layers. Default is ‘constant’.
- forward(x)
Define the computation performed at every call.
Should be overridden by all subclasses. :rtype:
TensorNote
Although the recipe for forward pass needs to be defined within this function, one should call the
Moduleinstance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.
- class PARCtorch.utilities.resnet.ResNetBlock(in_channels, out_channels, kernel_size, padding_mode)
Bases:
ModuleResidual Block for ResNet with support for changing feature dimensions.
- x –> Conv2d –> ReLU –> Conv2d –> ReLU
-
——– Identity/Conv2d ——-
- Parameters:
in_channels (int) – Number of input channels.
out_channels (int) – Number of output channels.
kernel_size (int) – Size of the convolutional kernel.
padding_mode (str) – Padding mode for convolutional layers.
- forward(x)
Define the computation performed at every call.
Should be overridden by all subclasses. :rtype:
TensorNote
Although the recipe for forward pass needs to be defined within this function, one should call the
Moduleinstance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.
PARCtorch.utilities.spade module
- class PARCtorch.utilities.spade.SPADE(in_channels, mask_channels, kernel_size=3, epsilon=1e-05, padding_mode='constant')
Bases:
ModuleSpatially-Adaptive Normalization (SPADE) layer implementation in PyTorch.
This class normalizes the input feature map and modulates it with scaling (gamma) and shifting (beta) parameters that are functions of a spatially-varying mask.
- Parameters:
in_channels (int) – Number of channels in the input feature map.
mask_channels (int) – Number of channels in the input mask.
kernel_size (int, optional) – Size of the convolutional kernels. Default is 3.
epsilon (float, optional) – Small constant for numerical stability. Default is 1e-5.
padding_mode (str, optional) – Padding mode for F.pad. Default is ‘constant’ (for zero-padding).
- forward(x, mask)
Forward pass of the SPADE layer.
- Parameters:
x (torch.Tensor) – Input feature map to be normalized. Shape: [N, C, H, W].
mask (torch.Tensor) – Input mask providing spatial modulation. Shape: [N, M, H, W].
- Returns:
The output tensor after applying SPADE normalization. Shape: [N, C, H, W].
- Return type:
torch.Tensor
- class PARCtorch.utilities.spade.SPADEGeneratorUnit(in_channels, out_channels, mask_channels, kernel_size=1, spade_kernel_size=3, padding_mode='constant')
Bases:
ModuleSPADE Generator Unit implementation in PyTorch.
This module represents a SPADE block used in generator architectures, consisting of: - Gaussian noise addition - Two sequential SPADE-Conv blocks with LeakyReLU activations - A skip connection with a SPADE-Conv block
- Parameters:
in_channels (int) – Number of channels in the input feature map x.
out_channels (int) – Number of output channels after convolution.
mask_channels (int) – Number of channels in the input mask mask.
kernel_size (int, optional) – Size of the convolutional kernels not in SPADE. Default is 1.
spade_kernel_size (int, optional) – Size of the convolutional kernels in SPADE. Default is 3.
padding_mode (str, optional) – Padding mode for F.pad. Default is ‘constant’.
- forward(x, mask, add_noise)
Forward pass of the SPADEGeneratorUnit.
- Parameters:
x (torch.Tensor) – Input feature map. Shape: [N, C_in, H, W].
mask (torch.Tensor) – Input mask for spatial modulation. Shape: [N, M, H’, W’].
add_noise (bool, optional) – Whether to add Gaussian noise. If None, defaults to self.training.
- Returns:
The output tensor after processing. Shape: [N, C_out, H’, W’].
- Return type:
torch.Tensor
PARCtorch.utilities.unet module
- class PARCtorch.utilities.unet.UNet(block_dimensions, input_channels, output_channels, kernel_size=3, padding_mode='zeros', up_block_use_concat=None, skip_connection_indices=None)
Bases:
ModuleU-Net Model.
Constructs a U-Net architecture with customizable depth and feature dimensions. Supports selective use of skip connections and concatenation in the upsampling path.
- Parameters:
block_dimensions (list of int) – List of feature dimensions for each block.
output_channels (int) – Number of output channels of the final layer.
kernel_size (int, optional) – Size of the convolutional kernels. Default is 3.
padding_mode (str, optional) – Padding mode for convolutional layers. Default is ‘zeros’.
up_block_use_concat (list of bool, optional) – Flags indicating whether to concatenate skip connections in each up block.
skip_connection_indices (list of int, optional) – Indices of skip connections to use in the upsampling path.
- forward(x)
Define the computation performed at every call.
Should be overridden by all subclasses.
Note
Although the recipe for forward pass needs to be defined within this function, one should call the
Moduleinstance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.
- class PARCtorch.utilities.unet.UNetDownBlock(in_channels, out_channels, kernel_size=3, padding_mode='zeros')
Bases:
ModuleU-Net Downsampling Block.
Performs two convolutional operations followed by a max pooling operation to reduce the spatial dimensions of the input tensor while increasing the number of feature channels.
- Parameters:
in_channels (int) – Number of input channels.
out_channels (int) – Number of output channels after convolution.
kernel_size (int, optional) – Size of the convolutional kernels. Default is 3.
padding_mode (str, optional) – Padding mode for convolutional layers. Default is ‘zeros’.
- forward(x)
Define the computation performed at every call.
Should be overridden by all subclasses.
Note
Although the recipe for forward pass needs to be defined within this function, one should call the
Moduleinstance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.
- class PARCtorch.utilities.unet.UNetUpBlock(in_channels, out_channels, skip_channels=0, kernel_size=3, padding_mode='zeros', use_concat=True)
Bases:
ModuleU-Net Upsampling Block.
Performs upsampling using a transposed convolutional layer, optionally concatenates the corresponding skip connection from the downsampling path, and applies two convolutional operations to refine the features.
- Parameters:
in_channels (int) – Number of input channels from the previous layer.
out_channels (int) – Number of output channels after convolution.
skip_channels (int, optional) – Number of channels from the skip connection. Default is 0.
kernel_size (int, optional) – Size of the convolutional kernels. Default is 3.
padding_mode (str, optional) – Padding mode for convolutional layers. Default is ‘zeros’.
use_concat (bool, optional) – Whether to concatenate skip connections. Default is True.
- forward(x, skip_connection)
Define the computation performed at every call.
Should be overridden by all subclasses.
Note
Although the recipe for forward pass needs to be defined within this function, one should call the
Moduleinstance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.