spyrit.misc.metrics.psnr_torch

spyrit.misc.metrics.psnr_torch(img_gt, img_rec, dim=(-2, -1), img_dyn=None)[source]

Computes the Peak Signal-to-Noise Ratio (PSNR) between two images.

\[\begin{split}\text{PSNR} = 20 \, \log_{10} \left( \frac{\text{d}}{\sqrt{\text{MSE}}} \right), \\ \text{MSE} = \frac{1}{L}\sum_{\ell=1}^L \|I_\ell - \tilde{I}_\ell\|^2_2,\end{split}\]

where \(d\) is the image dynamic and \(\{I_\ell\}\) (resp. \(\{\tilde{I}_\ell\}\)) is the set of ground truth (resp. reconstructed) images.

Args:

img_gt: Tensor containing the ground-truth image.

img_rec: Tensor containing the reconstructed image.

dim: Dimensions where the squared error is computed. Defaults to the last two dimensions.

img_dyn: Image dynamic range (e.g., 1.0 for normalized images, 255 for 8-bit images). When img_dyn is None, the dynamic range is computed from the ground-truth image.

Returns:

PSNR value.

Note

psnr_torch(img_gt, img_rec) is different from psnr_torch(img_rec, img_gt). The first expression assumes img_gt is the ground truth while the second assumes that this is img_rec. This leads to different dynamic ranges.

Example 1: 10 images of size 64x64 with values in [0,1) corrupted with 5% noise
>>> x = torch.rand(10,1,64,64)
>>> n = x + 0.05*torch.randn(x.shape)
>>> out = psnr_torch(x,n)
>>> print(out.shape)
torch.Size([10, 1])
Example 2: 10 images of size 64x64 with values in [0,1) corrupted with 5% noise
>>> psnr_torch(n,x)
tensor(...)
>>> psnr_torch(x,n)
tensor(...)
>>> psnr_torch(n,x,img_dyn=1.0)
tensor(...)