psnr.py 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. # LICENSE HEADER MANAGED BY add-license-header
  2. #
  3. # Copyright 2018 Kornia Team
  4. #
  5. # Licensed under the Apache License, Version 2.0 (the "License");
  6. # you may not use this file except in compliance with the License.
  7. # You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing, software
  12. # distributed under the License is distributed on an "AS IS" BASIS,
  13. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. # See the License for the specific language governing permissions and
  15. # limitations under the License.
  16. #
  17. import torch
  18. from torch.nn.functional import mse_loss as mse
  19. def psnr(image: torch.Tensor, target: torch.Tensor, max_val: float) -> torch.Tensor:
  20. r"""Create a function that calculates the PSNR between 2 images.
  21. PSNR is Peek Signal to Noise Ratio, which is similar to mean squared error.
  22. Given an m x n image, the PSNR is:
  23. .. math::
  24. \text{PSNR} = 10 \log_{10} \bigg(\frac{\text{MAX}_I^2}{MSE(I,T)}\bigg)
  25. where
  26. .. math::
  27. \text{MSE}(I,T) = \frac{1}{mn}\sum_{i=0}^{m-1}\sum_{j=0}^{n-1} [I(i,j) - T(i,j)]^2
  28. and :math:`\text{MAX}_I` is the maximum possible input value
  29. (e.g for floating point images :math:`\text{MAX}_I=1`).
  30. Args:
  31. image: the input image with arbitrary shape :math:`(*)`.
  32. target: the labels image with arbitrary shape :math:`(*)`.
  33. max_val: The maximum value in the input tensor.
  34. Return:
  35. the computed loss as a scalar.
  36. Examples:
  37. >>> ones = torch.ones(1)
  38. >>> psnr(ones, 1.2 * ones, 2.) # 10 * log(4/((1.2-1)**2)) / log(10)
  39. tensor(20.0000)
  40. Reference:
  41. https://en.wikipedia.org/wiki/Peak_signal-to-noise_ratio#Definition
  42. """
  43. if not isinstance(image, torch.Tensor):
  44. raise TypeError(f"Expected torch.Tensor but got {type(image)}.")
  45. if not isinstance(target, torch.Tensor):
  46. raise TypeError(f"Expected torch.Tensor but got {type(target)}.")
  47. if image.shape != target.shape:
  48. raise TypeError(f"Expected tensors of equal shapes, but got {image.shape} and {target.shape}")
  49. return 10.0 * torch.log10(max_val**2 / mse(image, target, reduction="mean"))