Peak Signal To Noise Ratio With Blocked Effect

Module Interface

class torchmetrics.image.PeakSignalNoiseRatioWithBlockedEffect(block_size=8, **kwargs)[source]

Computes Peak Signal to Noise Ratio With Blocked Effect (PSNRB).

\[\text{PSNRB}(I, J) = 10 * \log_{10} \left(\frac{\max(I)^2}{\text{MSE}(I, J)-\text{B}(I, J)}\right)\]

Where \(\text{MSE}\) denotes the mean-squared-error function. This metric is a modified version of PSNR that better supports evaluation of images with blocked artifacts, that oftens occur in compressed images.

Note

Metric only supports grayscale images. If you have RGB images, please convert them to grayscale first.

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

  • preds (Tensor): Predictions from model of shape (N,1,H,W)

  • target (Tensor): Ground truth values of shape (N,1,H,W)

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

  • psnrb (Tensor): float scalar tensor with aggregated PSNRB value

Parameters:

Example

>>> import torch
>>> from torchmetrics.image import PeakSignalNoiseRatioWithBlockedEffect
>>> metric = PeakSignalNoiseRatioWithBlockedEffect()
>>> _ = torch.manual_seed(42)
>>> preds = torch.rand(2, 1, 10, 10)
>>> target = torch.rand(2, 1, 10, 10)
>>> metric(preds, target)
tensor(7.2893)
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
>>> from torchmetrics.image import PeakSignalNoiseRatioWithBlockedEffect
>>> metric = PeakSignalNoiseRatioWithBlockedEffect()
>>> metric.update(torch.rand(2, 1, 10, 10), torch.rand(2, 1, 10, 10))
>>> fig_, ax_ = metric.plot()
../_images/peak_signal_to_noise_with_block-1.png
>>> # Example plotting multiple values
>>> import torch
>>> from torchmetrics.image import PeakSignalNoiseRatioWithBlockedEffect
>>> metric = PeakSignalNoiseRatioWithBlockedEffect()
>>> values = [ ]
>>> for _ in range(10):
...     values.append(metric(torch.rand(2, 1, 10, 10), torch.rand(2, 1, 10, 10)))
>>> fig_, ax_ = metric.plot(values)
../_images/peak_signal_to_noise_with_block-2.png

Functional Interface

torchmetrics.functional.image.peak_signal_noise_ratio_with_blocked_effect(preds, target, block_size=8)[source]

Computes Peak Signal to Noise Ratio With Blocked Effect (PSNRB) metrics.

\[\text{PSNRB}(I, J) = 10 * \log_{10} \left(\frac{\max(I)^2}{\text{MSE}(I, J)-\text{B}(I, J)}\right)\]

Where \(\text{MSE}\) denotes the mean-squared-error function.

Parameters:
  • preds (Tensor) – estimated signal

  • target (Tensor) – groun truth signal

  • block_size (int) – integer indication the block size

Return type:

Tensor

Returns:

Tensor with PSNRB score

Example

>>> import torch
>>> from torchmetrics.functional.image import peak_signal_noise_ratio_with_blocked_effect
>>> _ = torch.manual_seed(42)
>>> preds = torch.rand(1, 1, 28, 28)
>>> target = torch.rand(1, 1, 28, 28)
>>> peak_signal_noise_ratio_with_blocked_effect(preds, target)
tensor(7.8402)