basicsr.losses.loss_util
- basicsr.losses.loss_util.get_local_weights(residual, ksize)[source]
Get local weights for generating the artifact map of LDL.
It is only called by the get_refined_artifact_map function.
- Parameters
residual (Tensor) – Residual between predicted and ground truth images.
ksize (Int) – size of the local window.
- Returns
weight for each pixel to be discriminated as an artifact pixel
- Return type
Tensor
- basicsr.losses.loss_util.get_refined_artifact_map(img_gt, img_output, img_ema, ksize)[source]
Calculate the artifact map of LDL (Details or Artifacts: A Locally Discriminative Learning Approach to Realistic Image Super-Resolution. In CVPR 2022)
- Parameters
img_gt (Tensor) – ground truth images.
img_output (Tensor) – output images given by the optimizing model.
img_ema (Tensor) – output images given by the ema model.
ksize (Int) – size of the local window.
- Returns
weight for each pixel to be discriminated as an artifact pixel (calculated based on both local and global observations).
- Return type
overall_weight
- basicsr.losses.loss_util.reduce_loss(loss, reduction)[source]
Reduce loss as specified.
- Parameters
loss (Tensor) – Elementwise loss tensor.
reduction (str) – Options are ‘none’, ‘mean’ and ‘sum’.
- Returns
Reduced loss tensor.
- Return type
Tensor
- basicsr.losses.loss_util.weight_reduce_loss(loss, weight=None, reduction='mean')[source]
Apply element-wise weight and reduce loss.
- Parameters
loss (Tensor) – Element-wise loss.
weight (Tensor) – Element-wise weights. Default: None.
reduction (str) – Same as built-in losses of PyTorch. Options are ‘none’, ‘mean’ and ‘sum’. Default: ‘mean’.
- Returns
Loss values.
- Return type
Tensor
- basicsr.losses.loss_util.weighted_loss(loss_func)[source]
Create a weighted version of a given loss function.
To use this decorator, the loss function must have the signature like loss_func(pred, target, **kwargs). The function only needs to compute element-wise loss without any reduction. This decorator will add weight and reduction arguments to the function. The decorated function will have the signature like loss_func(pred, target, weight=None, reduction=’mean’, **kwargs).
- Example
>>> import torch >>> @weighted_loss >>> def l1_loss(pred, target): >>> return (pred - target).abs()
>>> pred = torch.Tensor([0, 2, 3]) >>> target = torch.Tensor([1, 1, 1]) >>> weight = torch.Tensor([1, 0, 1])
>>> l1_loss(pred, target) tensor(1.3333) >>> l1_loss(pred, target, weight) tensor(1.5000) >>> l1_loss(pred, target, reduction='none') tensor([1., 1., 2.]) >>> l1_loss(pred, target, weight, reduction='sum') tensor(3.)