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
andupdate
the metric accepts the following inputpreds
(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:
kwargs¶ (
Any
) – Additional keyword arguments, see Advanced metric settings for more info.
Example
>>> from torch import rand >>> metric = PeakSignalNoiseRatioWithBlockedEffect() >>> preds = rand(2, 1, 10, 10) >>> target = 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:
- 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()
>>> # 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)
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:
- Return type:
- Returns:
Tensor with PSNRB score
Example
>>> from torch import rand >>> from torchmetrics.functional.image import peak_signal_noise_ratio_with_blocked_effect >>> preds = rand(1, 1, 28, 28) >>> target = rand(1, 1, 28, 28) >>> peak_signal_noise_ratio_with_blocked_effect(preds, target) tensor(7.8402)