psnrb.py 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. # Copyright The PyTorch Lightning team.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. from collections.abc import Sequence
  15. from typing import Any, Optional, Union
  16. import torch
  17. from torch import Tensor, tensor
  18. from torchmetrics.functional.image.psnrb import _psnrb_compute, _psnrb_update
  19. from torchmetrics.metric import Metric
  20. from torchmetrics.utilities.imports import _MATPLOTLIB_AVAILABLE
  21. from torchmetrics.utilities.plot import _AX_TYPE, _PLOT_OUT_TYPE
  22. if not _MATPLOTLIB_AVAILABLE:
  23. __doctest_skip__ = ["PeakSignalNoiseRatioWithBlockedEffect.plot"]
  24. class PeakSignalNoiseRatioWithBlockedEffect(Metric):
  25. r"""Computes `Peak Signal to Noise Ratio With Blocked Effect`_ (PSNRB).
  26. .. math::
  27. \text{PSNRB}(I, J) = 10 * \log_{10} \left(\frac{\max(I)^2}{\text{MSE}(I, J)-\text{B}(I, J)}\right)
  28. Where :math:`\text{MSE}` denotes the `mean-squared-error`_ function. This metric is a modified version of PSNR that
  29. better supports evaluation of images with blocked artifacts, that oftens occur in compressed images.
  30. .. attention::
  31. Metric only supports grayscale images. If you have RGB images, please convert them to grayscale first.
  32. As input to ``forward`` and ``update`` the metric accepts the following input
  33. - ``preds`` (:class:`~torch.Tensor`): Predictions from model of shape ``(N,1,H,W)``
  34. - ``target`` (:class:`~torch.Tensor`): Ground truth values of shape ``(N,1,H,W)``
  35. As output of `forward` and `compute` the metric returns the following output
  36. - ``psnrb`` (:class:`~torch.Tensor`): float scalar tensor with aggregated PSNRB value
  37. Args:
  38. data_range: the range of the data. If a tuple is provided then the range is calculated as the difference and
  39. input is clamped between the values.
  40. block_size: integer indication the block size
  41. kwargs: Additional keyword arguments, see :ref:`Metric kwargs` for more info.
  42. Example:
  43. >>> from torch import rand
  44. >>> metric = PeakSignalNoiseRatioWithBlockedEffect(data_range=1.0)
  45. >>> preds = rand(2, 1, 10, 10)
  46. >>> target = rand(2, 1, 10, 10)
  47. >>> metric(preds, target)
  48. tensor(7.2893)
  49. """
  50. is_differentiable: bool = True
  51. higher_is_better: bool = True
  52. full_state_update: bool = False
  53. sum_squared_error: Tensor
  54. total: Tensor
  55. bef: Tensor
  56. data_range: Tensor
  57. def __init__(
  58. self,
  59. data_range: Union[float, tuple[float, float]],
  60. block_size: int = 8,
  61. **kwargs: Any,
  62. ) -> None:
  63. super().__init__(**kwargs)
  64. if not isinstance(block_size, int) and block_size < 1:
  65. raise ValueError("Argument ``block_size`` should be a positive integer")
  66. self.block_size = block_size
  67. self.add_state("sum_squared_error", default=tensor(0.0), dist_reduce_fx="sum")
  68. self.add_state("total", default=tensor(0), dist_reduce_fx="sum")
  69. self.add_state("bef", default=tensor(0.0), dist_reduce_fx="sum")
  70. if isinstance(data_range, tuple):
  71. self.add_state("data_range", default=tensor(data_range[1] - data_range[0]), dist_reduce_fx="mean")
  72. self.clamping_fn = lambda x: torch.clamp(x, min=data_range[0], max=data_range[1])
  73. else:
  74. self.add_state("data_range", default=tensor(float(data_range)), dist_reduce_fx="mean")
  75. self.clamping_fn = None # type: ignore[assignment]
  76. def update(self, preds: Tensor, target: Tensor) -> None:
  77. """Update state with predictions and targets."""
  78. if self.clamping_fn is not None:
  79. preds = self.clamping_fn(preds)
  80. target = self.clamping_fn(target)
  81. sum_squared_error, bef, num_obs = _psnrb_update(preds, target, block_size=self.block_size)
  82. self.sum_squared_error += sum_squared_error
  83. self.bef += bef
  84. self.total += num_obs
  85. def compute(self) -> Tensor:
  86. """Compute peak signal-to-noise ratio over state."""
  87. return _psnrb_compute(self.sum_squared_error, self.bef, self.total, self.data_range)
  88. def plot(
  89. self, val: Optional[Union[Tensor, Sequence[Tensor]]] = None, ax: Optional[_AX_TYPE] = None
  90. ) -> _PLOT_OUT_TYPE:
  91. """Plot a single or multiple values from the metric.
  92. Args:
  93. val: Either a single result from calling `metric.forward` or `metric.compute` or a list of these results.
  94. If no value is provided, will automatically call `metric.compute` and plot that result.
  95. ax: An matplotlib axis object. If provided will add plot to that axis
  96. Returns:
  97. Figure and Axes object
  98. Raises:
  99. ModuleNotFoundError:
  100. If `matplotlib` is not installed
  101. .. plot::
  102. :scale: 75
  103. >>> # Example plotting a single value
  104. >>> import torch
  105. >>> from torchmetrics.image import PeakSignalNoiseRatioWithBlockedEffect
  106. >>> metric = PeakSignalNoiseRatioWithBlockedEffect(data_range=1.0)
  107. >>> metric.update(torch.rand(2, 1, 10, 10), torch.rand(2, 1, 10, 10))
  108. >>> fig_, ax_ = metric.plot()
  109. .. plot::
  110. :scale: 75
  111. >>> # Example plotting multiple values
  112. >>> import torch
  113. >>> from torchmetrics.image import PeakSignalNoiseRatioWithBlockedEffect
  114. >>> metric = PeakSignalNoiseRatioWithBlockedEffect(data_range=1.0)
  115. >>> values = [ ]
  116. >>> for _ in range(10):
  117. ... values.append(metric(torch.rand(2, 1, 10, 10), torch.rand(2, 1, 10, 10)))
  118. >>> fig_, ax_ = metric.plot(values)
  119. """
  120. return self._plot(val, ax)