kaira.losses.image.StyleLoss

Inheritance diagram for StyleLoss
- class kaira.losses.image.StyleLoss(apply_gram=True, normalize=False, layer_weights=None)[source]
Bases:
BaseLossStyle Loss Module based on Gram matrices.
This module calculates the style loss used in neural style transfer. Style loss computes the difference between Gram matrices of feature maps, capturing texture information independent of spatial arrangement [Gatys et al., 2016].
Methods
Initialize the StyleLoss module.
Forward pass through the StyleLoss module.
Calculate Gram matrix from features.
- __init__(apply_gram=True, normalize=False, layer_weights=None)[source]
Initialize the StyleLoss module.
- Parameters:
apply_gram (bool) – Whether to apply Gram matrix computation (True) or use precomputed Gram matrices as input (False). Default is True.
normalize (bool) – Whether to normalize the Gram matrices. Default is False.
layer_weights (dict, optional) – Weights for different layers. Default is None (equal weights for all layers).
- gram_matrix(x)[source]
Calculate Gram matrix from features.
- Parameters:
x (torch.Tensor) – Feature tensor.
- Returns:
Gram matrix.
- Return type:
- forward(x: Tensor, target: Tensor) Tensor[source]
Forward pass through the StyleLoss module.
- Parameters:
x (torch.Tensor) – The input tensor.
target (torch.Tensor) – The target tensor.
- Returns:
The style loss between the input and the target.
- Return type: