-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Add Average Precision to metrics #8089
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
KumoLiu
merged 26 commits into
Project-MONAI:dev
from
thibaultdvx:8085-average-precision
Feb 20, 2025
Merged
Changes from 11 commits
Commits
Show all changes
26 commits
Select commit
Hold shift + click to select a range
f2ec7fa
average precision metric
thibaultdvx 4912a34
average precision handler
thibaultdvx fc7846d
inits
thibaultdvx febc9ee
unittets
thibaultdvx d88c9d8
min_tests
thibaultdvx 6df834f
mention AP in enum
thibaultdvx e2e36ee
docs
thibaultdvx 96e6b50
fix line too long issue
thibaultdvx 0ec68b2
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 7a2971a
Merge branch 'dev' into 8085-average-precision
KumoLiu 8e7d2e2
Merge branch 'dev' into 8085-average-precision
ericspod 2cb04e7
Update docs/source/metrics.rst
thibaultdvx bbd9522
Merge branch 'Project-MONAI:dev' into 8085-average-precision
thibaultdvx 88a6b4c
additional information in docstring
thibaultdvx 8926cad
merge handler test files
thibaultdvx ec94e3a
update min_tests.py
thibaultdvx 432ffcb
update test structure
thibaultdvx 4fc9cf5
fix test handler import issue
thibaultdvx a9f19c1
add some info in docstring
thibaultdvx 35aca33
Merge branch 'dev' into 8085-average-precision
ericspod ccca17d
Update tests/metrics/test_compute_average_precision.py
KumoLiu 086446b
fix format issue
thibaultdvx 72174c2
Merge branch 'dev' into 8085-average-precision
KumoLiu 0d4ee51
fix format issue
thibaultdvx f743844
Merge branch 'dev' into 8085-average-precision
KumoLiu f9b5347
Merge branch 'dev' into 8085-average-precision
KumoLiu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| # Copyright (c) MONAI Consortium | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from collections.abc import Callable | ||
|
|
||
| from monai.handlers.ignite_metric import IgniteMetricHandler | ||
| from monai.metrics import AveragePrecisionMetric | ||
| from monai.utils import Average | ||
|
|
||
|
|
||
| class AveragePrecision(IgniteMetricHandler): | ||
| """ | ||
| Computes Average Precision (AP). | ||
| accumulating predictions and the ground-truth during an epoch and applying `compute_average_precision`. | ||
|
|
||
| Args: | ||
| average: {``"macro"``, ``"weighted"``, ``"micro"``, ``"none"``} | ||
| Type of averaging performed if not binary classification. Defaults to ``"macro"``. | ||
|
|
||
| - ``"macro"``: calculate metrics for each label, and find their unweighted mean. | ||
| This does not take label imbalance into account. | ||
| - ``"weighted"``: calculate metrics for each label, and find their average, | ||
| weighted by support (the number of true instances for each label). | ||
| - ``"micro"``: calculate metrics globally by considering each element of the label | ||
| indicator matrix as a label. | ||
| - ``"none"``: the scores for each class are returned. | ||
|
|
||
| output_transform: callable to extract `y_pred` and `y` from `ignite.engine.state.output` then | ||
| construct `(y_pred, y)` pair, where `y_pred` and `y` can be `batch-first` Tensors or | ||
| lists of `channel-first` Tensors. the form of `(y_pred, y)` is required by the `update()`. | ||
| `engine.state` and `output_transform` inherit from the ignite concept: | ||
| https://pytorch.org/ignite/concepts.html#state, explanation and usage example are in the tutorial: | ||
| https://github.com/Project-MONAI/tutorials/blob/master/modules/batch_output_transform.ipynb. | ||
|
|
||
| Note: | ||
| Average Precision expects y to be comprised of 0's and 1's. | ||
| y_pred must either be probability estimates or confidence values. | ||
|
|
||
| """ | ||
|
|
||
| def __init__(self, average: Average | str = Average.MACRO, output_transform: Callable = lambda x: x) -> None: | ||
| metric_fn = AveragePrecisionMetric(average=Average(average)) | ||
| super().__init__(metric_fn=metric_fn, output_transform=output_transform, save_details=False) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,173 @@ | ||
| # Copyright (c) MONAI Consortium | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import warnings | ||
| from typing import TYPE_CHECKING, cast | ||
|
|
||
| import numpy as np | ||
|
|
||
| if TYPE_CHECKING: | ||
| import numpy.typing as npt | ||
|
|
||
| import torch | ||
|
|
||
| from monai.utils import Average, look_up_option | ||
|
|
||
| from .metric import CumulativeIterationMetric | ||
|
|
||
|
|
||
| class AveragePrecisionMetric(CumulativeIterationMetric): | ||
| """ | ||
| Computes Average Precision (AP). Referring to: `sklearn.metrics.average_precision_score | ||
KumoLiu marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| <https://scikit-learn.org/stable/modules/generated/sklearn.metrics.average_precision_score>`_. | ||
| The input `y_pred` and `y` can be a list of `channel-first` Tensor or a `batch-first` Tensor. | ||
| Example of the typical execution steps of this metric class follows :py:class:`monai.metrics.metric.Cumulative`. | ||
| Args: | ||
| average: {``"macro"``, ``"weighted"``, ``"micro"``, ``"none"``} | ||
| Type of averaging performed if not binary classification. | ||
| Defaults to ``"macro"``. | ||
| - ``"macro"``: calculate metrics for each label, and find their unweighted mean. | ||
| This does not take label imbalance into account. | ||
| - ``"weighted"``: calculate metrics for each label, and find their average, | ||
| weighted by support (the number of true instances for each label). | ||
| - ``"micro"``: calculate metrics globally by considering each element of the label | ||
| indicator matrix as a label. | ||
| - ``"none"``: the scores for each class are returned. | ||
| """ | ||
|
|
||
| def __init__(self, average: Average | str = Average.MACRO) -> None: | ||
| super().__init__() | ||
| self.average = average | ||
|
|
||
| def _compute_tensor(self, y_pred: torch.Tensor, y: torch.Tensor) -> tuple[torch.Tensor, torch.Tensor]: # type: ignore[override] | ||
| return y_pred, y | ||
|
|
||
| def aggregate(self, average: Average | str | None = None) -> np.ndarray | float | npt.ArrayLike: | ||
| """ | ||
| Typically `y_pred` and `y` are stored in the cumulative buffers at each iteration, | ||
| This function reads the buffers and computes the Average Precision. | ||
| Args: | ||
| average: {``"macro"``, ``"weighted"``, ``"micro"``, ``"none"``} | ||
| Type of averaging performed if not binary classification. Defaults to `self.average`. | ||
| """ | ||
| y_pred, y = self.get_buffer() | ||
| # compute final value and do metric reduction | ||
| if not isinstance(y_pred, torch.Tensor) or not isinstance(y, torch.Tensor): | ||
| raise ValueError("y_pred and y must be PyTorch Tensor.") | ||
|
|
||
| return compute_average_precision(y_pred=y_pred, y=y, average=average or self.average) | ||
|
|
||
|
|
||
| def _calculate(y_pred: torch.Tensor, y: torch.Tensor) -> float: | ||
| if not (y.ndimension() == y_pred.ndimension() == 1 and len(y) == len(y_pred)): | ||
| raise AssertionError("y and y_pred must be 1 dimension data with same length.") | ||
| y_unique = y.unique() | ||
| if len(y_unique) == 1: | ||
| warnings.warn(f"y values can not be all {y_unique.item()}, skip AP computation and return `Nan`.") | ||
| return float("nan") | ||
| if not y_unique.equal(torch.tensor([0, 1], dtype=y.dtype, device=y.device)): | ||
| warnings.warn(f"y values must be 0 or 1, but in {y_unique.tolist()}, skip AP computation and return `Nan`.") | ||
| return float("nan") | ||
|
|
||
| n = len(y) | ||
| indices = y_pred.argsort(descending=True) | ||
| y = y[indices].cpu().numpy() # type: ignore[assignment] | ||
| y_pred = y_pred[indices].cpu().numpy() # type: ignore[assignment] | ||
| npos = ap = tmp_pos = 0.0 | ||
|
|
||
| for i in range(n): | ||
| y_i = cast(float, y[i]) | ||
| if i + 1 < n and y_pred[i] == y_pred[i + 1]: | ||
| tmp_pos += y_i | ||
| else: | ||
| tmp_pos += y_i | ||
| npos += tmp_pos | ||
| ap += tmp_pos * npos / (i + 1) | ||
| tmp_pos = 0 | ||
|
|
||
| return ap / npos | ||
|
|
||
|
|
||
| def compute_average_precision( | ||
| y_pred: torch.Tensor, y: torch.Tensor, average: Average | str = Average.MACRO | ||
| ) -> np.ndarray | float | npt.ArrayLike: | ||
| """Computes Average Precision (AP). Referring to: `sklearn.metrics.average_precision_score | ||
| <https://scikit-learn.org/stable/modules/generated/sklearn.metrics.average_precision_score>`_. | ||
| Args: | ||
| y_pred: input data to compute, typical classification model output. | ||
| the first dim must be batch, if multi-classes, it must be in One-Hot format. | ||
| for example: shape `[16]` or `[16, 1]` for a binary data, shape `[16, 2]` for 2 classes data. | ||
| y: ground truth to compute AP metric, the first dim must be batch. | ||
| if multi-classes, it must be in One-Hot format. | ||
| for example: shape `[16]` or `[16, 1]` for a binary data, shape `[16, 2]` for 2 classes data. | ||
| average: {``"macro"``, ``"weighted"``, ``"micro"``, ``"none"``} | ||
| Type of averaging performed if not binary classification. | ||
| Defaults to ``"macro"``. | ||
| - ``"macro"``: calculate metrics for each label, and find their unweighted mean. | ||
| This does not take label imbalance into account. | ||
| - ``"weighted"``: calculate metrics for each label, and find their average, | ||
| weighted by support (the number of true instances for each label). | ||
| - ``"micro"``: calculate metrics globally by considering each element of the label | ||
| indicator matrix as a label. | ||
| - ``"none"``: the scores for each class are returned. | ||
| Raises: | ||
| ValueError: When ``y_pred`` dimension is not one of [1, 2]. | ||
| ValueError: When ``y`` dimension is not one of [1, 2]. | ||
| ValueError: When ``average`` is not one of ["macro", "weighted", "micro", "none"]. | ||
| Note: | ||
| Average Precision expects y to be comprised of 0's and 1's. `y_pred` must be either prob. estimates or confidence values. | ||
| """ | ||
| y_pred_ndim = y_pred.ndimension() | ||
| y_ndim = y.ndimension() | ||
| if y_pred_ndim not in (1, 2): | ||
| raise ValueError( | ||
| f"Predictions should be of shape (batch_size, num_classes) or (batch_size, ), got {y_pred.shape}." | ||
| ) | ||
| if y_ndim not in (1, 2): | ||
| raise ValueError(f"Targets should be of shape (batch_size, num_classes) or (batch_size, ), got {y.shape}.") | ||
| if y_pred_ndim == 2 and y_pred.shape[1] == 1: | ||
| y_pred = y_pred.squeeze(dim=-1) | ||
| y_pred_ndim = 1 | ||
| if y_ndim == 2 and y.shape[1] == 1: | ||
| y = y.squeeze(dim=-1) | ||
|
|
||
| if y_pred_ndim == 1: | ||
| return _calculate(y_pred, y) | ||
|
|
||
| if y.shape != y_pred.shape: | ||
| raise ValueError(f"data shapes of y_pred and y do not match, got {y_pred.shape} and {y.shape}.") | ||
|
|
||
| average = look_up_option(average, Average) | ||
| if average == Average.MICRO: | ||
| return _calculate(y_pred.flatten(), y.flatten()) | ||
| y, y_pred = y.transpose(0, 1), y_pred.transpose(0, 1) | ||
| ap_values = [_calculate(y_pred_, y_) for y_pred_, y_ in zip(y_pred, y)] | ||
| if average == Average.NONE: | ||
| return ap_values | ||
| if average == Average.MACRO: | ||
| return np.mean(ap_values) | ||
| if average == Average.WEIGHTED: | ||
| weights = [sum(y_) for y_ in y] | ||
| return np.average(ap_values, weights=weights) # type: ignore[no-any-return] | ||
| raise ValueError(f'Unsupported average: {average}, available options are ["macro", "weighted", "micro", "none"].') | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.