espnet2.enh.layers.ncsnpp_utils.normalization.InstanceNorm2dPlus
espnet2.enh.layers.ncsnpp_utils.normalization.InstanceNorm2dPlus
class espnet2.enh.layers.ncsnpp_utils.normalization.InstanceNorm2dPlus(num_features, bias=True)
Bases: Module
Instance Normalization with additional parameters for improved performance.
This layer performs instance normalization on input data and incorporates additional parameters to adapt the normalization based on the input’s statistics. It is particularly useful for improving the stability and convergence of deep learning models.
num_features
The number of features (channels) in the input.
- Type: int
bias
Whether to include a bias term in the normalization.
- Type: bool
instance_norm
The instance normalization layer.
- Type: nn.InstanceNorm2d
alpha
Learnable scaling parameter.
- Type: nn.Parameter
gamma
Learnable scaling parameter.
- Type: nn.Parameter
beta
Learnable bias parameter, if bias is True.
Type: nn.Parameter, optional
Parameters:
- num_features (int) – Number of features (channels) in the input.
- bias (bool , optional) – If True, includes a bias term. Defaults to True.
Returns: The normalized output tensor with the same shape as the input.
Return type: Tensor
####### Examples
>>> model = InstanceNorm2dPlus(num_features=64)
>>> input_tensor = torch.randn(10, 64, 32, 32) # Batch of 10 images
>>> output_tensor = model(input_tensor)
>>> output_tensor.shape
torch.Size([10, 64, 32, 32])
NOTE
The normalization is performed per-instance, meaning that each instance in the batch is normalized independently.
- Raises:RuntimeError – If the input tensor is not of the expected shape.
Initialize internal Module state, shared by both nn.Module and ScriptModule.
forward(x)
Applies the Conditional Instance Normalization operation.
This method normalizes the input tensor x using instance normalization, followed by a conditional scaling and shifting based on the class embeddings obtained from the input y. The normalization is computed using the statistics of the input tensor, and the scaling and shifting parameters are learned through the embedding layer.
- Parameters:
- x (torch.Tensor) – Input tensor of shape (N, C, H, W), where N is the batch size, C is the number of channels, H is the height, and W is the width.
- y (torch.Tensor) – Class labels tensor of shape (N,) containing class indices corresponding to each input in the batch.
- Returns: The normalized output tensor with the same shape as : input x.
- Return type: torch.Tensor
####### Examples
>>> model = ConditionalInstanceNorm2dPlus(num_features=64, num_classes=10)
>>> x = torch.randn(8, 64, 32, 32) # Batch of 8 images
>>> y = torch.randint(0, 10, (8,)) # Random class indices for the batch
>>> output = model(x, y)
>>> print(output.shape)
torch.Size([8, 64, 32, 32])
NOTE
The instance normalization is computed over the spatial dimensions of the input tensor, and the class embeddings are used to adjust the output through learned parameters.