kaira.losses.image.StyleLoss

Inheritance diagram of StyleLoss

Inheritance diagram for StyleLoss

class kaira.losses.image.StyleLoss(apply_gram=True, normalize=False, layer_weights=None)[source]

Bases: BaseLoss

Style 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

__init__

Initialize the StyleLoss module.

forward

Forward pass through the StyleLoss module.

gram_matrix

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:

torch.Tensor

forward(x: Tensor, target: Tensor) Tensor[source]

Forward pass through the StyleLoss module.

Parameters:
Returns:

The style loss between the input and the target.

Return type:

torch.Tensor