---
title: Peak Signal To Noise Ratio With Blocked Effect
description: Computes Peak Signal to Noise Ratio With Blocked Effect (PSNRB).
canonical: >-
  https://lightning.ai/docs/torchmetrics/stable/image/peak_signal_to_noise_with_block
---
# Peak Signal To Noise Ratio With Blocked Effect {#peak-signal-to-noise-ratio-with-blocked-effect}

## Module Interface {#module-interface}

### `class torchmetrics.image.PeakSignalNoiseRatioWithBlockedEffect(data_range, block_size=8, **kwargs)` {#torchmetrics.image.PeakSignalNoiseRatioWithBlockedEffect}

[\[source\]](https://github.com/Lightning-AI/torchmetrics/blob/v1.9.0/src/torchmetrics/image/psnrb.py#L29-L152)

Computes [Peak Signal to Noise Ratio With Blocked Effect](https://ieeexplore.ieee.org/abstract/document/5535179) (PSNRB).

```math
\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](https://en.wikipedia.org/wiki/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.

Attention

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`](https://docs.pytorch.org/docs/stable/tensors.html#torch.Tensor "(in PyTorch v2.15)")): Predictions from model of shape `(N,1,H,W)`
    
*   `target` ([`Tensor`](https://docs.pytorch.org/docs/stable/tensors.html#torch.Tensor "(in PyTorch v2.15)")): Ground truth values of shape `(N,1,H,W)`
    

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

*   `psnrb` ([`Tensor`](https://docs.pytorch.org/docs/stable/tensors.html#torch.Tensor "(in PyTorch v2.15)")): float scalar tensor with aggregated PSNRB value
    

Parameters:

*   **data\_range** (`Union`\[[`float`](https://docs.python.org/3/builtins/functions.html#float "(in Python v3.15)"), [`tuple`](https://docs.python.org/3/builtins/stdtypes.html#tuple "(in Python v3.15)")\[[`float`](https://docs.python.org/3/builtins/functions.html#float "(in Python v3.15)"), [`float`](https://docs.python.org/3/builtins/functions.html#float "(in Python v3.15)")\]\]) – the range of the data. If a tuple is provided then the range is calculated as the difference and input is clamped between the values.
    
*   **block\_size** ([`int`](https://docs.python.org/3/builtins/functions.html#int "(in Python v3.15)")) – integer indication the block size
    
*   **kwargs** ([`Any`](https://docs.python.org/3/library/typing.html#typing.Any "(in Python v3.15)")) – Additional keyword arguments, see [Advanced metric settings](/docs/torchmetrics/1.9.0/user-guide/overview.md#metric-kwargs) for more info.
    

Example

```python
>>> from torch import rand
>>> metric = PeakSignalNoiseRatioWithBlockedEffect(data_range=1.0)
>>> preds = rand(2, 1, 10, 10)
>>> target = rand(2, 1, 10, 10)
>>> metric(preds, target)
tensor(7.2893)
```

#### `plot(val=None, ax=None)` {#torchmetrics.image.PeakSignalNoiseRatioWithBlockedEffect.plot}

[\[source\]](https://github.com/Lightning-AI/torchmetrics/blob/v1.9.0/src/torchmetrics/image/psnrb.py#L112-L152)

Plot a single or multiple values from the metric.

Parameters:

*   **val** (`Union`\[[`Tensor`](https://docs.pytorch.org/docs/stable/tensors.html#torch.Tensor "(in PyTorch v2.15)"), [`Sequence`](https://docs.python.org/3/library/collections.abc.html#collections.abc.Sequence "(in Python v3.15)")\[[`Tensor`](https://docs.pytorch.org/docs/stable/tensors.html#torch.Tensor "(in PyTorch v2.15)")\], [`None`](https://docs.python.org/3/builtins/constants.html#None "(in Python v3.15)")\]) – 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`](https://docs.python.org/3/library/typing.html#typing.Optional "(in Python v3.15)")\[[`Axes`](https://matplotlib.org/stable/api/_as_gen/matplotlib.axes.Axes.html#matplotlib.axes.Axes "(in Matplotlib v3.11.2)")\]) – An matplotlib axis object. If provided will add plot to that axis
    

Return type:

[`tuple`](https://docs.python.org/3/builtins/stdtypes.html#tuple "(in Python v3.15)")\[[`Figure`](https://matplotlib.org/stable/api/_as_gen/matplotlib.figure.Figure.html#matplotlib.figure.Figure "(in Matplotlib v3.11.2)"), `Union`\[[`Axes`](https://matplotlib.org/stable/api/_as_gen/matplotlib.axes.Axes.html#matplotlib.axes.Axes "(in Matplotlib v3.11.2)"), [`ndarray`](https://numpy.org/doc/stable/reference/generated/numpy.ndarray.html#numpy.ndarray "(in NumPy v2.5)")\]\]

Returns:

Figure and Axes object

Raises:

[**ModuleNotFoundError**](https://docs.python.org/3/builtins/exceptions.html#ModuleNotFoundError "(in Python v3.15)") – If matplotlib is not installed

```python
>>> # Example plotting a single value
>>> import torch
>>> from torchmetrics.image import PeakSignalNoiseRatioWithBlockedEffect
>>> metric = PeakSignalNoiseRatioWithBlockedEffect(data_range=1.0)
>>> 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](/docs/torchmetrics/1.9.0/static/_images/peak_signal_to_noise_with_block-1.png)](/docs/torchmetrics/1.9.0/static/_images/peak_signal_to_noise_with_block-1.png)

```python
>>> # Example plotting multiple values
>>> import torch
>>> from torchmetrics.image import PeakSignalNoiseRatioWithBlockedEffect
>>> metric = PeakSignalNoiseRatioWithBlockedEffect(data_range=1.0)
>>> 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](/docs/torchmetrics/1.9.0/static/_images/peak_signal_to_noise_with_block-2.png)](/docs/torchmetrics/1.9.0/static/_images/peak_signal_to_noise_with_block-2.png)

## Functional Interface {#functional-interface}

### `torchmetrics.functional.image.peak_signal_noise_ratio_with_blocked_effect(preds, target, data_range, block_size=8)` {#torchmetrics.functional.image.peak_signal_noise_ratio_with_blocked_effect}

[\[source\]](https://github.com/Lightning-AI/torchmetrics/blob/v1.9.0/src/torchmetrics/functional/image/psnrb.py#L102-L142)

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

```math
\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](https://en.wikipedia.org/wiki/Mean_squared_error) function.

Parameters:

*   **preds** ([`Tensor`](https://docs.pytorch.org/docs/stable/tensors.html#torch.Tensor "(in PyTorch v2.15)")) – estimated signal
    
*   **target** ([`Tensor`](https://docs.pytorch.org/docs/stable/tensors.html#torch.Tensor "(in PyTorch v2.15)")) – ground truth signal
    
*   **data\_range** (`Union`\[[`float`](https://docs.python.org/3/builtins/functions.html#float "(in Python v3.15)"), [`tuple`](https://docs.python.org/3/builtins/stdtypes.html#tuple "(in Python v3.15)")\[[`float`](https://docs.python.org/3/builtins/functions.html#float "(in Python v3.15)"), [`float`](https://docs.python.org/3/builtins/functions.html#float "(in Python v3.15)")\]\]) – the range of the data. If a tuple is provided then the range is calculated as the difference and input is clamped between the values.
    
*   **block\_size** ([`int`](https://docs.python.org/3/builtins/functions.html#int "(in Python v3.15)")) – integer indication the block size
    

Return type:

[`Tensor`](https://docs.pytorch.org/docs/stable/tensors.html#torch.Tensor "(in PyTorch v2.15)")

Returns:

Tensor with PSNRB score

Example

```python
>>> 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, data_range=1.0)
tensor(7.8402)
```

---

**Navigation**

- Parent: [Image](/docs/torchmetrics/1.9.0/image.md)
