exact_match.py 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  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, Optional, Union
  16. import torch
  17. from torch import Tensor
  18. from typing_extensions import Literal
  19. from torchmetrics.classification.base import _ClassificationTaskWrapper
  20. from torchmetrics.functional.classification.exact_match import (
  21. _exact_match_reduce,
  22. _multiclass_exact_match_update,
  23. _multilabel_exact_match_update,
  24. )
  25. from torchmetrics.functional.classification.stat_scores import (
  26. _multiclass_stat_scores_arg_validation,
  27. _multiclass_stat_scores_format,
  28. _multiclass_stat_scores_tensor_validation,
  29. _multilabel_stat_scores_arg_validation,
  30. _multilabel_stat_scores_format,
  31. _multilabel_stat_scores_tensor_validation,
  32. )
  33. from torchmetrics.metric import Metric
  34. from torchmetrics.utilities.data import dim_zero_cat
  35. from torchmetrics.utilities.enums import ClassificationTaskNoBinary
  36. from torchmetrics.utilities.imports import _MATPLOTLIB_AVAILABLE
  37. from torchmetrics.utilities.plot import _AX_TYPE, _PLOT_OUT_TYPE
  38. if not _MATPLOTLIB_AVAILABLE:
  39. __doctest_skip__ = ["MulticlassExactMatch.plot", "MultilabelExactMatch.plot"]
  40. class MulticlassExactMatch(Metric):
  41. r"""Compute Exact match (also known as subset accuracy) for multiclass tasks.
  42. Exact Match is a stricter version of accuracy where all labels have to match exactly for the sample to be
  43. correctly classified.
  44. As input to ``forward`` and ``update`` the metric accepts the following input:
  45. - ``preds`` (:class:`~torch.Tensor`): An int tensor of shape ``(N, ...)`` or float tensor of shape ``(N, C, ..)``.
  46. If preds is a floating point we apply ``torch.argmax`` along the ``C`` dimension to automatically convert
  47. probabilities/logits into an int tensor.
  48. - ``target`` (:class:`~torch.Tensor`): An int tensor of shape ``(N, ...)``.
  49. As output to ``forward`` and ``compute`` the metric returns the following output:
  50. - ``mcem`` (:class:`~torch.Tensor`): A tensor whose returned shape depends on the ``multidim_average`` argument:
  51. - If ``multidim_average`` is set to ``global`` the output will be a scalar tensor
  52. - If ``multidim_average`` is set to ``samplewise`` the output will be a tensor of shape ``(N,)``
  53. If ``multidim_average`` is set to ``samplewise`` we expect at least one additional dimension ``...`` to be present,
  54. which the reduction will then be applied over instead of the sample dimension ``N``.
  55. Args:
  56. num_classes: Integer specifying the number of labels
  57. multidim_average:
  58. Defines how additionally dimensions ``...`` should be handled. Should be one of the following:
  59. - ``global``: Additional dimensions are flatted along the batch dimension
  60. - ``samplewise``: Statistic will be calculated independently for each sample on the ``N`` axis.
  61. The statistics in this case are calculated over the additional dimensions.
  62. ignore_index:
  63. Specifies a target value that is ignored and does not contribute to the metric calculation
  64. validate_args: bool indicating if input arguments and tensors should be validated for correctness.
  65. Set to ``False`` for faster computations.
  66. Example (multidim tensors):
  67. >>> from torch import tensor
  68. >>> from torchmetrics.classification import MulticlassExactMatch
  69. >>> target = tensor([[[0, 1], [2, 1], [0, 2]], [[1, 1], [2, 0], [1, 2]]])
  70. >>> preds = tensor([[[0, 1], [2, 1], [0, 2]], [[2, 2], [2, 1], [1, 0]]])
  71. >>> metric = MulticlassExactMatch(num_classes=3, multidim_average='global')
  72. >>> metric(preds, target)
  73. tensor(0.5000)
  74. Example (multidim tensors):
  75. >>> from torchmetrics.classification import MulticlassExactMatch
  76. >>> target = tensor([[[0, 1], [2, 1], [0, 2]], [[1, 1], [2, 0], [1, 2]]])
  77. >>> preds = tensor([[[0, 1], [2, 1], [0, 2]], [[2, 2], [2, 1], [1, 0]]])
  78. >>> metric = MulticlassExactMatch(num_classes=3, multidim_average='samplewise')
  79. >>> metric(preds, target)
  80. tensor([1., 0.])
  81. """
  82. total: Tensor
  83. is_differentiable: bool = False
  84. higher_is_better: bool = True
  85. full_state_update: bool = False
  86. plot_lower_bound: float = 0.0
  87. plot_upper_bound: float = 1.0
  88. plot_legend_name: str = "Class"
  89. def __init__(
  90. self,
  91. num_classes: int,
  92. multidim_average: Literal["global", "samplewise"] = "global",
  93. ignore_index: Optional[int] = None,
  94. validate_args: bool = True,
  95. **kwargs: Any,
  96. ) -> None:
  97. super().__init__(**kwargs)
  98. top_k, average = 1, None
  99. if validate_args:
  100. _multiclass_stat_scores_arg_validation(num_classes, top_k, average, multidim_average, ignore_index)
  101. self.num_classes = num_classes
  102. self.multidim_average = multidim_average
  103. self.ignore_index = ignore_index
  104. self.validate_args = validate_args
  105. self.add_state(
  106. "correct",
  107. torch.zeros(1, dtype=torch.long) if self.multidim_average == "global" else [],
  108. dist_reduce_fx="sum" if self.multidim_average == "global" else "cat",
  109. )
  110. self.add_state(
  111. "total",
  112. torch.zeros(1, dtype=torch.long),
  113. dist_reduce_fx="sum" if self.multidim_average == "global" else "mean",
  114. )
  115. def update(self, preds: Tensor, target: Tensor) -> None:
  116. """Update metric states with predictions and targets."""
  117. if self.validate_args:
  118. _multiclass_stat_scores_tensor_validation(
  119. preds, target, self.num_classes, self.multidim_average, self.ignore_index
  120. )
  121. preds, target = _multiclass_stat_scores_format(preds, target, 1)
  122. correct, total = _multiclass_exact_match_update(preds, target, self.multidim_average, self.ignore_index)
  123. if self.multidim_average == "samplewise":
  124. if not isinstance(self.correct, list):
  125. raise TypeError("Expected `self.correct` to be a list in samplewise mode.")
  126. self.correct.append(correct)
  127. if not isinstance(self.total, Tensor):
  128. raise TypeError("Expected `self.total` to be a Tensor in samplewise mode.")
  129. self.total = total
  130. else:
  131. if not isinstance(self.correct, Tensor):
  132. raise TypeError("Expected `self.correct` to be a tensor in global mode.")
  133. self.correct += correct
  134. if not isinstance(self.total, Tensor):
  135. raise TypeError("Expected `self.total` to be a Tensor in samplewise mode.")
  136. self.total += total
  137. def compute(self) -> Tensor:
  138. """Compute metric."""
  139. correct = dim_zero_cat(self.correct) if isinstance(self.correct, list) else self.correct
  140. # Validate that `correct` and `total` are tensors
  141. if not isinstance(correct, Tensor) or not isinstance(self.total, Tensor):
  142. raise TypeError("Expected `correct` and `total` to be tensors after processing.")
  143. return _exact_match_reduce(correct, self.total)
  144. def plot(
  145. self, val: Optional[Union[Tensor, Sequence[Tensor]]] = None, ax: Optional[_AX_TYPE] = None
  146. ) -> _PLOT_OUT_TYPE:
  147. """Plot a single or multiple values from the metric.
  148. Args:
  149. val: Either a single result from calling `metric.forward` or `metric.compute` or a list of these results.
  150. If no value is provided, will automatically call `metric.compute` and plot that result.
  151. ax: An matplotlib axis object. If provided will add plot to that axis
  152. Returns:
  153. Figure object and Axes object
  154. Raises:
  155. ModuleNotFoundError:
  156. If `matplotlib` is not installed
  157. .. plot::
  158. :scale: 75
  159. >>> # Example plotting a single value per class
  160. >>> from torch import randint
  161. >>> from torchmetrics.classification import MulticlassExactMatch
  162. >>> metric = MulticlassExactMatch(num_classes=3)
  163. >>> metric.update(randint(3, (20,5)), randint(3, (20,5)))
  164. >>> fig_, ax_ = metric.plot()
  165. .. plot::
  166. :scale: 75
  167. >>> from torch import randint
  168. >>> # Example plotting a multiple values per class
  169. >>> from torchmetrics.classification import MulticlassExactMatch
  170. >>> metric = MulticlassExactMatch(num_classes=3)
  171. >>> values = []
  172. >>> for _ in range(20):
  173. ... values.append(metric(randint(3, (20,5)), randint(3, (20,5))))
  174. >>> fig_, ax_ = metric.plot(values)
  175. """
  176. return self._plot(val, ax)
  177. class MultilabelExactMatch(Metric):
  178. r"""Compute Exact match (also known as subset accuracy) for multilabel tasks.
  179. Exact Match is a stricter version of accuracy where all labels have to match exactly for the sample to be
  180. correctly classified.
  181. As input to ``forward`` and ``update`` the metric accepts the following input:
  182. - ``preds`` (:class:`~torch.Tensor`): An int tensor or float tensor of shape ``(N, C, ..)``. If preds is a
  183. floating point tensor with values outside [0,1] range we consider the input to be logits and will auto apply
  184. sigmoid per element. Additionally, we convert to int tensor with thresholding using the value in ``threshold``.
  185. - ``target`` (:class:`~torch.Tensor`): An int tensor of shape ``(N, C, ...)``.
  186. As output to ``forward`` and ``compute`` the metric returns the following output:
  187. - ``mlem`` (:class:`~torch.Tensor`): A tensor whose returned shape depends on the ``multidim_average`` argument:
  188. - If ``multidim_average`` is set to ``global`` the output will be a scalar tensor
  189. - If ``multidim_average`` is set to ``samplewise`` the output will be a tensor of shape ``(N,)``
  190. If ``multidim_average`` is set to ``samplewise`` we expect at least one additional dimension ``...`` to be present,
  191. which the reduction will then be applied over instead of the sample dimension ``N``.
  192. Args:
  193. num_labels: Integer specifying the number of labels
  194. threshold: Threshold for transforming probability to binary (0,1) predictions
  195. multidim_average:
  196. Defines how additionally dimensions ``...`` should be handled. Should be one of the following:
  197. - ``global``: Additional dimensions are flatted along the batch dimension
  198. - ``samplewise``: Statistic will be calculated independently for each sample on the ``N`` axis.
  199. The statistics in this case are calculated over the additional dimensions.
  200. ignore_index:
  201. Specifies a target value that is ignored and does not contribute to the metric calculation
  202. validate_args: bool indicating if input arguments and tensors should be validated for correctness.
  203. Set to ``False`` for faster computations.
  204. Example (preds is int tensor):
  205. >>> from torch import tensor
  206. >>> from torchmetrics.classification import MultilabelExactMatch
  207. >>> target = tensor([[0, 1, 0], [1, 0, 1]])
  208. >>> preds = tensor([[0, 0, 1], [1, 0, 1]])
  209. >>> metric = MultilabelExactMatch(num_labels=3)
  210. >>> metric(preds, target)
  211. tensor(0.5000)
  212. Example (preds is float tensor):
  213. >>> from torchmetrics.classification import MultilabelExactMatch
  214. >>> target = tensor([[0, 1, 0], [1, 0, 1]])
  215. >>> preds = tensor([[0.11, 0.22, 0.84], [0.73, 0.33, 0.92]])
  216. >>> metric = MultilabelExactMatch(num_labels=3)
  217. >>> metric(preds, target)
  218. tensor(0.5000)
  219. Example (multidim tensors):
  220. >>> from torchmetrics.classification import MultilabelExactMatch
  221. >>> target = tensor([[[0, 1], [1, 0], [0, 1]], [[1, 1], [0, 0], [1, 0]]])
  222. >>> preds = tensor([[[0.59, 0.91], [0.91, 0.99], [0.63, 0.04]],
  223. ... [[0.38, 0.04], [0.86, 0.780], [0.45, 0.37]]])
  224. >>> metric = MultilabelExactMatch(num_labels=3, multidim_average='samplewise')
  225. >>> metric(preds, target)
  226. tensor([0., 0.])
  227. """
  228. total: Tensor
  229. is_differentiable: bool = False
  230. higher_is_better: bool = True
  231. full_state_update: bool = False
  232. plot_lower_bound: float = 0.0
  233. plot_upper_bound: float = 1.0
  234. plot_legend_name: str = "Label"
  235. def __init__(
  236. self,
  237. num_labels: int,
  238. threshold: float = 0.5,
  239. multidim_average: Literal["global", "samplewise"] = "global",
  240. ignore_index: Optional[int] = None,
  241. validate_args: bool = True,
  242. **kwargs: Any,
  243. ) -> None:
  244. super().__init__(**kwargs)
  245. if validate_args:
  246. _multilabel_stat_scores_arg_validation(
  247. num_labels, threshold, average=None, multidim_average=multidim_average, ignore_index=ignore_index
  248. )
  249. self.num_labels = num_labels
  250. self.threshold = threshold
  251. self.multidim_average = multidim_average
  252. self.ignore_index = ignore_index
  253. self.validate_args = validate_args
  254. self.add_state(
  255. "correct",
  256. torch.zeros(1, dtype=torch.long) if self.multidim_average == "global" else [],
  257. dist_reduce_fx="sum" if self.multidim_average == "global" else "cat",
  258. )
  259. self.add_state(
  260. "total",
  261. torch.zeros(1, dtype=torch.long),
  262. dist_reduce_fx="sum" if self.multidim_average == "global" else "mean",
  263. )
  264. def update(self, preds: Tensor, target: Tensor) -> None:
  265. """Update state with predictions and targets."""
  266. if self.validate_args:
  267. _multilabel_stat_scores_tensor_validation(
  268. preds, target, self.num_labels, self.multidim_average, self.ignore_index
  269. )
  270. preds, target = _multilabel_stat_scores_format(
  271. preds, target, self.num_labels, self.threshold, self.ignore_index
  272. )
  273. correct, total = _multilabel_exact_match_update(
  274. preds=preds,
  275. target=target,
  276. num_labels=self.num_labels,
  277. multidim_average=self.multidim_average,
  278. ignore_index=self.ignore_index,
  279. )
  280. if self.multidim_average == "samplewise":
  281. if not isinstance(self.correct, list):
  282. raise TypeError("Expected `self.correct` to be a list in samplewise mode.")
  283. self.correct.append(correct)
  284. if not isinstance(self.total, Tensor):
  285. raise TypeError("Expected `self.total` to be a Tensor in samplewise mode.")
  286. self.total = total
  287. else:
  288. if not isinstance(self.correct, Tensor):
  289. raise TypeError("Expected `self.correct` to be a tensor in global mode.")
  290. self.correct += correct
  291. if not isinstance(self.total, Tensor):
  292. raise TypeError("Expected `self.total` to be a Tensor in samplewise mode.")
  293. self.total += total
  294. def compute(self) -> Tensor:
  295. """Compute metric."""
  296. correct = dim_zero_cat(self.correct) if isinstance(self.correct, list) else self.correct
  297. # Validate that `correct` and `total` are tensors
  298. if not isinstance(correct, Tensor) or not isinstance(self.total, Tensor):
  299. raise TypeError("Expected `correct` and `total` to be tensors after processing.")
  300. return _exact_match_reduce(correct, self.total)
  301. def plot(
  302. self, val: Optional[Union[Tensor, Sequence[Tensor]]] = None, ax: Optional[_AX_TYPE] = None
  303. ) -> _PLOT_OUT_TYPE:
  304. """Plot a single or multiple values from the metric.
  305. Args:
  306. val: Either a single result from calling `metric.forward` or `metric.compute` or a list of these results.
  307. If no value is provided, will automatically call `metric.compute` and plot that result.
  308. ax: An matplotlib axis object. If provided will add plot to that axis
  309. Returns:
  310. Figure and Axes object
  311. Raises:
  312. ModuleNotFoundError:
  313. If `matplotlib` is not installed
  314. .. plot::
  315. :scale: 75
  316. >>> # Example plotting a single value
  317. >>> from torch import rand, randint
  318. >>> from torchmetrics.classification import MultilabelExactMatch
  319. >>> metric = MultilabelExactMatch(num_labels=3)
  320. >>> metric.update(randint(2, (20, 3, 5)), randint(2, (20, 3, 5)))
  321. >>> fig_, ax_ = metric.plot()
  322. .. plot::
  323. :scale: 75
  324. >>> # Example plotting multiple values
  325. >>> from torch import rand, randint
  326. >>> from torchmetrics.classification import MultilabelExactMatch
  327. >>> metric = MultilabelExactMatch(num_labels=3)
  328. >>> values = [ ]
  329. >>> for _ in range(10):
  330. ... values.append(metric(randint(2, (20, 3, 5)), randint(2, (20, 3, 5))))
  331. >>> fig_, ax_ = metric.plot(values)
  332. """
  333. return self._plot(val, ax)
  334. class ExactMatch(_ClassificationTaskWrapper):
  335. r"""Compute Exact match (also known as subset accuracy).
  336. Exact Match is a stricter version of accuracy where all labels have to match exactly for the sample to be
  337. correctly classified.
  338. This module is a simple wrapper to get the task specific versions of this metric, which is done by setting the
  339. ``task`` argument to either ``'multiclass'`` or ``'multilabel'``. See the documentation of
  340. :class:`~torchmetrics.classification.MulticlassExactMatch` and
  341. :class:`~torchmetrics.classification.MultilabelExactMatch` for the specific details of each argument influence and
  342. examples.
  343. Legacy Example:
  344. >>> from torch import tensor
  345. >>> target = tensor([[[0, 1], [2, 1], [0, 2]], [[1, 1], [2, 0], [1, 2]]])
  346. >>> preds = tensor([[[0, 1], [2, 1], [0, 2]], [[2, 2], [2, 1], [1, 0]]])
  347. >>> metric = ExactMatch(task="multiclass", num_classes=3, multidim_average='global')
  348. >>> metric(preds, target)
  349. tensor(0.5000)
  350. >>> target = tensor([[[0, 1], [2, 1], [0, 2]], [[1, 1], [2, 0], [1, 2]]])
  351. >>> preds = tensor([[[0, 1], [2, 1], [0, 2]], [[2, 2], [2, 1], [1, 0]]])
  352. >>> metric = ExactMatch(task="multiclass", num_classes=3, multidim_average='samplewise')
  353. >>> metric(preds, target)
  354. tensor([1., 0.])
  355. """
  356. def __new__( # type: ignore[misc]
  357. cls: type["ExactMatch"],
  358. task: Literal["binary", "multiclass", "multilabel"],
  359. threshold: float = 0.5,
  360. num_classes: Optional[int] = None,
  361. num_labels: Optional[int] = None,
  362. multidim_average: Literal["global", "samplewise"] = "global",
  363. ignore_index: Optional[int] = None,
  364. validate_args: bool = True,
  365. **kwargs: Any,
  366. ) -> Metric:
  367. """Initialize task metric."""
  368. task = ClassificationTaskNoBinary.from_str(task)
  369. kwargs.update({
  370. "multidim_average": multidim_average,
  371. "ignore_index": ignore_index,
  372. "validate_args": validate_args,
  373. })
  374. if task == ClassificationTaskNoBinary.MULTICLASS:
  375. if not isinstance(num_classes, int):
  376. raise ValueError(f"`num_classes` is expected to be `int` but `{type(num_classes)} was passed.`")
  377. return MulticlassExactMatch(num_classes, **kwargs)
  378. if task == ClassificationTaskNoBinary.MULTILABEL:
  379. if not isinstance(num_labels, int):
  380. raise ValueError(f"`num_labels` is expected to be `int` but `{type(num_labels)} was passed.`")
  381. return MultilabelExactMatch(num_labels, threshold, **kwargs)
  382. raise ValueError(f"Task {task} not supported!")