sam.py 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. # Copyright The 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, List, Optional, Union
  16. from torch import Tensor, tensor
  17. from typing_extensions import Literal
  18. from torchmetrics.functional.image.sam import _sam_compute, _sam_update
  19. from torchmetrics.metric import Metric
  20. from torchmetrics.utilities import rank_zero_warn
  21. from torchmetrics.utilities.data import dim_zero_cat
  22. from torchmetrics.utilities.imports import _MATPLOTLIB_AVAILABLE
  23. from torchmetrics.utilities.plot import _AX_TYPE, _PLOT_OUT_TYPE
  24. if not _MATPLOTLIB_AVAILABLE:
  25. __doctest_skip__ = ["SpectralAngleMapper.plot"]
  26. class SpectralAngleMapper(Metric):
  27. """`Spectral Angle Mapper`_ determines the spectral similarity between image spectra and reference spectra.
  28. It works by calculating the angle between the spectra, where small angles between indicate high similarity and
  29. high angles indicate low similarity.
  30. As input to ``forward`` and ``update`` the metric accepts the following input
  31. - ``preds`` (:class:`~torch.Tensor`): Predictions from model of shape ``(N,C,H,W)``
  32. - ``target`` (:class:`~torch.Tensor`): Ground truth values of shape ``(N,C,H,W)``
  33. As output of `forward` and `compute` the metric returns the following output
  34. - ``sam`` (:class:`~torch.Tensor`): if ``reduction!='none'`` returns float scalar tensor with average SAM value
  35. over sample else returns tensor of shape ``(N,)`` with SAM values per sample
  36. Args:
  37. reduction: a method to reduce metric score over labels.
  38. - ``'elementwise_mean'``: takes the mean (default)
  39. - ``'sum'``: takes the sum
  40. - ``'none'`` or ``None``: no reduction will be applied
  41. kwargs: Additional keyword arguments, see :ref:`Metric kwargs` for more info.
  42. Return:
  43. Tensor with SpectralAngleMapper score
  44. Example:
  45. >>> from torch import rand
  46. >>> from torchmetrics.image import SpectralAngleMapper
  47. >>> preds = rand([16, 3, 16, 16])
  48. >>> target = rand([16, 3, 16, 16])
  49. >>> sam = SpectralAngleMapper()
  50. >>> sam(preds, target)
  51. tensor(0.5914)
  52. """
  53. higher_is_better: bool = False
  54. is_differentiable: bool = True
  55. full_state_update: bool = False
  56. plot_lower_bound: float = 0.0
  57. plot_upper_bound: float = 1.0
  58. preds: List[Tensor]
  59. target: List[Tensor]
  60. sum_sam: Tensor
  61. numel: Tensor
  62. def __init__(
  63. self,
  64. reduction: Optional[Literal["elementwise_mean", "sum", "none"]] = "elementwise_mean",
  65. **kwargs: Any,
  66. ) -> None:
  67. super().__init__(**kwargs)
  68. if reduction not in ("elementwise_mean", "sum", "none", None):
  69. raise ValueError(
  70. f"The `reduction` {reduction} is not valid. Valid options are `elementwise_mean`, `sum`, `none`, None."
  71. )
  72. if reduction == "none" or reduction is None:
  73. rank_zero_warn(
  74. "Metric `SpectralAngleMapper` will save all targets and predictions in the buffer when using"
  75. "`reduction=None` or `reduction='none'. For large datasets, this may lead to a large memory footprint."
  76. )
  77. self.add_state("preds", default=[], dist_reduce_fx="cat")
  78. self.add_state("target", default=[], dist_reduce_fx="cat")
  79. else:
  80. self.add_state("sum_sam", tensor(0.0), dist_reduce_fx="sum")
  81. self.add_state("numel", tensor(0), dist_reduce_fx="sum")
  82. self.reduction = reduction
  83. def update(self, preds: Tensor, target: Tensor) -> None:
  84. """Update state with predictions and targets."""
  85. preds, target = _sam_update(preds, target)
  86. if self.reduction == "none" or self.reduction is None:
  87. self.preds.append(preds)
  88. self.target.append(target)
  89. else:
  90. sam_score = _sam_compute(preds, target, reduction="sum")
  91. self.sum_sam += sam_score
  92. p_shape = preds.shape
  93. self.numel += p_shape[0] * p_shape[2] * p_shape[3]
  94. def compute(self) -> Tensor:
  95. """Compute spectra over state."""
  96. if self.reduction == "none" or self.reduction is None:
  97. preds = dim_zero_cat(self.preds)
  98. target = dim_zero_cat(self.target)
  99. return _sam_compute(preds, target, self.reduction)
  100. return self.sum_sam / self.numel if self.reduction == "elementwise_mean" else self.sum_sam
  101. def plot(
  102. self, val: Optional[Union[Tensor, Sequence[Tensor]]] = None, ax: Optional[_AX_TYPE] = None
  103. ) -> _PLOT_OUT_TYPE:
  104. """Plot a single or multiple values from the metric.
  105. Args:
  106. val: Either a single result from calling `metric.forward` or `metric.compute` or a list of these results.
  107. If no value is provided, will automatically call `metric.compute` and plot that result.
  108. ax: An matplotlib axis object. If provided will add plot to that axis
  109. Returns:
  110. Figure and Axes object
  111. Raises:
  112. ModuleNotFoundError:
  113. If `matplotlib` is not installed
  114. .. plot::
  115. :scale: 75
  116. >>> # Example plotting single value
  117. >>> from torch import rand
  118. >>> from torchmetrics.image import SpectralAngleMapper
  119. >>> preds = rand([16, 3, 16, 16])
  120. >>> target = rand([16, 3, 16, 16])
  121. >>> metric = SpectralAngleMapper()
  122. >>> metric.update(preds, target)
  123. >>> fig_, ax_ = metric.plot()
  124. .. plot::
  125. :scale: 75
  126. >>> # Example plotting multiple values
  127. >>> from torch import rand
  128. >>> from torchmetrics.image import SpectralAngleMapper
  129. >>> preds = rand([16, 3, 16, 16])
  130. >>> target = rand([16, 3, 16, 16])
  131. >>> metric = SpectralAngleMapper()
  132. >>> values = [ ]
  133. >>> for _ in range(10):
  134. ... values.append(metric(preds, target))
  135. >>> fig_, ax_ = metric.plot(values)
  136. """
  137. return self._plot(val, ax)