piqa.psnr#

Peak Signal-to-Noise Ratio (PSNR)

This module implements the PSNR in PyTorch.

Wikipedia

https://wikipedia.org/wiki/Peak_signal-to-noise_ratio

Functions#

mse

Returns the Mean Squared Error (MSE) between \(x\) and \(y\).

psnr

Returns the PSNR between \(x\) and \(y\).

Classes#

PSNR

Measures the PSNR between an input and a target.

Descriptions#

piqa.psnr.mse(x, y)#

Returns the Mean Squared Error (MSE) between \(x\) and \(y\).

\[\text{MSE}(x, y) = \frac{1}{\text{size}(x)} \sum_i (x_i - y_i)^2\]
Parameters:
  • x (Tensor) – An input tensor, \((N, *)\).

  • y (Tensor) – A target tensor, \((N, *)\).

Returns:

The MSE vector, \((N,)\).

Return type:

Tensor

Example

>>> x = torch.rand(5, 3, 256, 256)
>>> y = torch.rand(5, 3, 256, 256)
>>> l = mse(x, y)
>>> l.shape
torch.Size([5])
piqa.psnr.psnr(x, y, epsilon=1e-08, value_range=1.0)#

Returns the PSNR between \(x\) and \(y\).

\[\text{PSNR}(x, y) = 10 \log_{10} \left( \frac{L^2}{\text{MSE}(x, y)} \right)\]
Parameters:
  • x (Tensor) – An input tensor, \((N, *)\).

  • y (Tensor) – A target tensor, \((N, *)\).

  • epsilon (float) – A numerical stability term.

  • value_range (float) – The value range \(L\) of the inputs (usually 1 or 255).

Returns:

The PSNR vector, \((N,)\).

Return type:

Tensor

Example

>>> x = torch.rand(5, 3, 256, 256)
>>> y = torch.rand(5, 3, 256, 256)
>>> l = psnr(x, y)
>>> l.shape
torch.Size([5])
class piqa.psnr.PSNR(reduction='mean', **kwargs)#

Measures the PSNR between an input and a target.

Parameters:
  • reduction (str) – Specifies the reduction to apply to the output: 'none', 'mean' or 'sum'.

  • kwargs – Keyword arguments passed to psnr.

Example

>>> criterion = PSNR()
>>> x = torch.rand(5, 3, 256, 256, requires_grad=True)
>>> y = torch.rand(5, 3, 256, 256)
>>> l = -criterion(x, y)
>>> l.shape
torch.Size([])
>>> l.backward()
forward(x, y)#
Parameters:
  • x (Tensor) – An input tensor, \((N, *)\).

  • y (Tensor) – A target tensor, \((N, *)\).

Returns:

The PSNR vector, \((N,)\) or \(()\) depending on reduction.

Return type:

Tensor