transformations.py 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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 typing import Any, Callable, Optional, Union
  15. import torch
  16. from torchmetrics.collections import MetricCollection
  17. from torchmetrics.metric import Metric
  18. from torchmetrics.wrappers.abstract import WrapperMetric
  19. class MetricInputTransformer(WrapperMetric):
  20. """Abstract base class for metric input transformations.
  21. Input transformations are characterized by them applying a transformation to the input data of a metric, and then
  22. forwarding all calls to the wrapped metric with modifications applied.
  23. """
  24. def __init__(self, wrapped_metric: Union[Metric, MetricCollection], **kwargs: dict[str, Any]) -> None:
  25. super().__init__(**kwargs)
  26. if not isinstance(wrapped_metric, (Metric, MetricCollection)):
  27. raise TypeError(
  28. f"Expected wrapped metric to be an instance of `torchmetrics.Metric` or "
  29. f"`torchmetrics.MetricsCollection`but received {wrapped_metric}"
  30. )
  31. self.wrapped_metric = wrapped_metric
  32. def transform_pred(self, pred: torch.Tensor) -> torch.Tensor:
  33. """Define transform operations on the prediction data.
  34. Overridden by subclasses. Identity by default.
  35. """
  36. return pred
  37. def transform_target(self, target: torch.Tensor) -> torch.Tensor:
  38. """Define transform operations on the target data.
  39. Overridden by subclasses. Identity by default.
  40. """
  41. return target
  42. def _wrap_transform(self, *args: torch.Tensor) -> tuple[torch.Tensor, ...]:
  43. """Wrap transformation functions to dispatch args to their individual transform functions."""
  44. if len(args) == 1:
  45. return (self.transform_pred(args[0]),)
  46. if len(args) == 2:
  47. return self.transform_pred(args[0]), self.transform_target(args[1])
  48. return self.transform_pred(args[0]), self.transform_target(args[1]), *args[2:]
  49. def update(self, *args: torch.Tensor, **kwargs: dict[str, Any]) -> None:
  50. """Wrap the update call of the underlying metric."""
  51. args = self._wrap_transform(*args)
  52. self.wrapped_metric.update(*args, **kwargs)
  53. def compute(self) -> Any:
  54. """Wrap the compute call of the underlying metric."""
  55. return self.wrapped_metric.compute()
  56. def forward(self, *args: torch.Tensor, **kwargs: dict[str, Any]) -> Any:
  57. """Wrap the forward call of the underlying metric."""
  58. args = self._wrap_transform(*args)
  59. return self.wrapped_metric.forward(*args, **kwargs)
  60. def reset(self) -> None:
  61. """Wrap the reset call of the underlying metric."""
  62. self.wrapped_metric.reset()
  63. super().reset()
  64. class LambdaInputTransformer(MetricInputTransformer):
  65. """Wrapper class for transforming a metrics' inputs given a user-defined lambda function.
  66. Args:
  67. wrapped_metric:
  68. The underlying `Metric` or `MetricCollection`.
  69. transform_pred:
  70. The function to apply to the predictions before computing the metric.
  71. transform_target:
  72. The function to apply to the target before computing the metric.
  73. Raises:
  74. TypeError:
  75. If `transform_pred` is not a Callable.
  76. TypeError:
  77. If `transform_target` is not a Callable.
  78. Example:
  79. >>> import torch
  80. >>> from torchmetrics.classification import BinaryAccuracy
  81. >>> from torchmetrics.wrappers import LambdaInputTransformer
  82. >>>
  83. >>> preds = torch.tensor([0.9, 0.8, 0.7, 0.6, 0.5, 0.6, 0.7, 0.8, 0.5, 0.4])
  84. >>> targets = torch.tensor([1,0,0,0,0,1,1,0,0,0])
  85. >>>
  86. >>> metric = LambdaInputTransformer(BinaryAccuracy(), lambda preds: 1 - preds)
  87. >>> metric.update(preds, targets)
  88. >>> metric.compute()
  89. tensor(0.6000)
  90. """
  91. def __init__(
  92. self,
  93. wrapped_metric: Metric,
  94. transform_pred: Optional[Callable[[torch.Tensor], torch.Tensor]] = None,
  95. transform_target: Optional[Callable[[torch.Tensor], torch.Tensor]] = None,
  96. **kwargs: Any,
  97. ) -> None:
  98. super().__init__(wrapped_metric, **kwargs)
  99. if transform_pred is not None:
  100. if not callable(transform_pred):
  101. raise TypeError(f"Expected `transform_pred` to be of type `Callable` but received `{transform_pred}`")
  102. self.transform_pred = transform_pred # type: ignore[assignment,method-assign]
  103. if transform_target is not None:
  104. if not callable(transform_target):
  105. raise TypeError(
  106. f"Expected `transform_target` to be of type `Callable` but received `{transform_target}`"
  107. )
  108. self.transform_target = transform_target # type: ignore[assignment,method-assign]
  109. class BinaryTargetTransformer(MetricInputTransformer):
  110. """Wrapper class for computing a metric on binarized targets.
  111. Useful when the given ground-truth targets are continuous, but the metric requires binary targets.
  112. Args:
  113. wrapped_metric:
  114. The underlying `Metric` or `MetricCollection`.
  115. threshold:
  116. The binarization threshold for the targets. Targets values `t` are cast to binary with `t > threshold`.
  117. Raises:
  118. TypeError:
  119. If `threshold` is not an `int` or `float`.
  120. Example:
  121. >>> import torch
  122. >>> from torchmetrics.retrieval import RetrievalMRR
  123. >>> from torchmetrics.wrappers import BinaryTargetTransformer
  124. >>>
  125. >>> preds = torch.tensor([0.9, 0.8, 0.7, 0.6, 0.5, 0.6, 0.7, 0.8, 0.5, 0.4])
  126. >>> targets = torch.tensor([1,0,0,0,0,2,1,0,0,0])
  127. >>> topics = torch.tensor([0,0,0,0,0,1,1,1,1,1])
  128. >>>
  129. >>> metric = BinaryTargetTransformer(RetrievalMRR())
  130. >>> metric.update(preds, targets, indexes=topics)
  131. >>> metric.compute()
  132. tensor(0.7500)
  133. """
  134. def __init__(self, wrapped_metric: Union[Metric, MetricCollection], threshold: float = 0, **kwargs: Any) -> None:
  135. super().__init__(wrapped_metric, **kwargs)
  136. if not isinstance(threshold, (int, float)):
  137. raise TypeError(f"Expected `threshold` to be of type `int` or `float` but received `{threshold}`")
  138. self.threshold = threshold
  139. def transform_target(self, target: torch.Tensor) -> torch.Tensor:
  140. """Cast the target tensor to binary values according to the threshold.
  141. Output assumes same type as input.
  142. """
  143. return target.gt(self.threshold).to(target.dtype)