Quality with No Reference

Module Interface

class torchmetrics.image.QualityWithNoReference(alpha=1, beta=1, norm_order=1, window_size=7, reduction='elementwise_mean', **kwargs)[source]

Compute Quality with No Reference (QualityWithNoReference) also now as QNR.

The metric is used to compare the joint spectral and spatial distortion between two images.

As input to forward and update the metric accepts the following input

  • preds (Tensor): High resolution multispectral image of shape (N,C,H,W).

  • target (Dict): A dictionary containing the following keys:

    • ms (Tensor): Low resolution multispectral image of shape (N,C,H',W').

    • pan (Tensor): High resolution panchromatic image of shape (N,C,H,W).

    • pan_lr (Tensor): (optional) Low resolution panchromatic image of shape (N,C,H',W').

where H and W must be multiple of H’ and W’.

When pan_lr is None, a uniform filter will be applied on pan to produce a degraded image. The degraded image is then resized to match the size of ms and served as pan_lr in the calculation.

As output of forward and compute the metric returns the following output

  • qnr (Tensor): if reduction!='none' returns float scalar tensor with average QNR value over sample else returns tensor of shape (N,) with QNR values per sample

Parameters:
  • alpha (float) – Relevance of spectral distortion.

  • beta (float) – Relevance of spatial distortion.

  • norm_order (int) – Order of the norm applied on the difference.

  • window_size (int) – Window size of the filter applied to degrade the high resolution panchromatic image.

  • reduction (Literal['elementwise_mean', 'sum', 'none']) –

    a method to reduce metric score over labels.

    • 'elementwise_mean': takes the mean (default)

    • 'sum': takes the sum

    • 'none': no reduction will be applied

  • kwargs (Any) – Additional keyword arguments, see Advanced metric settings for more info.

Example

>>> import torch
>>> _ = torch.manual_seed(42)
>>> from torchmetrics.image import QualityWithNoReference
>>> preds = torch.rand([16, 3, 32, 32])
>>> target = {
...     'ms': torch.rand([16, 3, 16, 16]),
...     'pan': torch.rand([16, 3, 32, 32]),
... }
>>> qnr = QualityWithNoReference()
>>> qnr(preds, target)
tensor(0.9694)
plot(val=None, ax=None)[source]

Plot a single or multiple values from the metric.

Parameters:
  • val (Union[Tensor, Sequence[Tensor], None]) – Either a single result from calling metric.forward or metric.compute or a list of these results. If no value is provided, will automatically call metric.compute and plot that result.

  • ax (Optional[Axes]) – An matplotlib axis object. If provided will add plot to that axis

Return type:

Tuple[Figure, Union[Axes, ndarray]]

Returns:

Figure and Axes object

Raises:

ModuleNotFoundError – If matplotlib is not installed

>>> # Example plotting a single value
>>> import torch
>>> _ = torch.manual_seed(42)
>>> from torchmetrics.image import QualityWithNoReference
>>> preds = torch.rand([16, 3, 32, 32])
>>> target = {
...     'ms': torch.rand([16, 3, 16, 16]),
...     'pan': torch.rand([16, 3, 32, 32]),
... }
>>> metric = QualityWithNoReference()
>>> metric.update(preds, target)
>>> fig_, ax_ = metric.plot()
../_images/quality_with_no_reference-1.png
>>> # Example plotting multiple values
>>> import torch
>>> _ = torch.manual_seed(42)
>>> from torchmetrics.image import QualityWithNoReference
>>> preds = torch.rand([16, 3, 32, 32])
>>> target = {
...     'ms': torch.rand([16, 3, 16, 16]),
...     'pan': torch.rand([16, 3, 32, 32]),
... }
>>> metric = QualityWithNoReference()
>>> values = [ ]
>>> for _ in range(10):
...     values.append(metric(preds, target))
>>> fig_, ax_ = metric.plot(values)
../_images/quality_with_no_reference-2.png

Functional Interface

torchmetrics.functional.image.quality_with_no_reference(preds, ms, pan, pan_lr=None, alpha=1, beta=1, norm_order=1, window_size=7, reduction='elementwise_mean')[source]

Calculate Quality with No Reference (QualityWithNoReference) also known as QNR.

Metric is used to compare the joint spectral and spatial distortion between two images.

Parameters:
  • preds (Tensor) – High resolution multispectral image.

  • ms (Tensor) – Low resolution multispectral image.

  • pan (Tensor) – High resolution panchromatic image.

  • pan_lr (Optional[Tensor]) – Low resolution panchromatic image.

  • alpha (float) – Relevance of spectral distortion.

  • beta (float) – Relevance of spatial distortion.

  • norm_order (int) – Order of the norm applied on the difference.

  • window_size (int) – Window size of the filter applied to degrade the high resolution panchromatic image.

  • reduction (Literal['elementwise_mean', 'sum', 'none']) –

    A method to reduce metric score over labels.

    • 'elementwise_mean': takes the mean (default)

    • 'sum': takes the sum

    • 'none': no reduction will be applied

Return type:

Tensor

Returns:

Tensor with QualityWithNoReference score

Raises:

ValueError – If alpha or beta is not a non-negative real number.

Example

>>> import torch
>>> from torchmetrics.functional.image import quality_with_no_reference
>>> _ = torch.manual_seed(42)
>>> preds = torch.rand([16, 3, 32, 32])
>>> ms = torch.rand([16, 3, 16, 16])
>>> pan = torch.rand([16, 3, 32, 32])
>>> quality_with_no_reference(preds, ms, pan)
tensor(0.9694)