Skip to content

Commit

Permalink
Prune metrics: AUC & AUROC (#6572)
Browse files Browse the repository at this point in the history
* class: AUC AUROC

* func: auc auroc

* format

* tests
  • Loading branch information
Borda authored Mar 18, 2021
1 parent 2f6ce1a commit 9e35f97
Show file tree
Hide file tree
Showing 14 changed files with 101 additions and 1,057 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).

[#6515](https://github.com/PyTorchLightning/pytorch-lightning/pull/6515),

[#6572](https://github.com/PyTorchLightning/pytorch-lightning/pull/6572),

)


Expand Down
67 changes: 7 additions & 60 deletions pytorch_lightning/metrics/classification/auc.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,36 +13,14 @@
# limitations under the License.
from typing import Any, Callable, Optional

import torch
from torchmetrics import Metric
from torchmetrics import AUC as _AUC

from pytorch_lightning.metrics.functional.auc import _auc_compute, _auc_update
from pytorch_lightning.utilities import rank_zero_warn
from pytorch_lightning.utilities.deprecation import deprecated


class AUC(Metric):
r"""
Computes Area Under the Curve (AUC) using the trapezoidal rule
Forward accepts two input tensors that should be 1D and have the same number
of elements
Args:
reorder: AUC expects its first input to be sorted. If this is not the case,
setting this argument to ``True`` will use a stable sorting algorithm to
sort the input in decending order
compute_on_step:
Forward only calls ``update()`` and return None if this is set to False.
dist_sync_on_step:
Synchronize metric state across processes at each ``forward()``
before returning the value at the step.
process_group:
Specify the process group on which synchronization is called. default: None (which selects the entire world)
dist_sync_fn:
Callback that performs the allgather operation on the metric state. When ``None``, DDP
will be used to perform the allgather
"""
class AUC(_AUC):

@deprecated(target=_AUC, ver_deprecate="1.3.0", ver_remove="1.5.0")
def __init__(
self,
reorder: bool = False,
Expand All @@ -51,40 +29,9 @@ def __init__(
process_group: Optional[Any] = None,
dist_sync_fn: Callable = None,
):
super().__init__(
compute_on_step=compute_on_step,
dist_sync_on_step=dist_sync_on_step,
process_group=process_group,
dist_sync_fn=dist_sync_fn,
)

self.reorder = reorder

self.add_state("x", default=[], dist_reduce_fx=None)
self.add_state("y", default=[], dist_reduce_fx=None)

rank_zero_warn(
'Metric `AUC` will save all targets and predictions in buffer.'
' For large datasets this may lead to large memory footprint.'
)

def update(self, x: torch.Tensor, y: torch.Tensor):
"""
Update state with predictions and targets.
Args:
x: Predictions from model (probabilities, or labels)
y: Ground truth labels
"""
x, y = _auc_update(x, y)
This implementation refers to :class:`~torchmetrics.AUC`.
self.x.append(x)
self.y.append(y)

def compute(self) -> torch.Tensor:
"""
Computes AUC based on inputs passed in to ``update`` previously.
.. deprecated::
Use :class:`~torchmetrics.AUC`. Will be removed in v1.5.0.
"""
x = torch.cat(self.x, dim=0)
y = torch.cat(self.y, dim=0)
return _auc_compute(x, y, reorder=self.reorder)
158 changes: 7 additions & 151 deletions pytorch_lightning/metrics/classification/auroc.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,95 +11,16 @@
# 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 distutils.version import LooseVersion
from typing import Any, Callable, Optional

import torch
from torchmetrics import Metric
from torchmetrics import AUROC as _AUROC

from pytorch_lightning.metrics.functional.auroc import _auroc_compute, _auroc_update
from pytorch_lightning.utilities import rank_zero_warn
from pytorch_lightning.utilities.deprecation import deprecated


class AUROC(Metric):
r"""Compute `Area Under the Receiver Operating Characteristic Curve (ROC AUC)
<https://en.wikipedia.org/wiki/Receiver_operating_characteristic#Further_interpretations>`_.
Works for both binary, multilabel and multiclass problems. In the case of
multiclass, the values will be calculated based on a one-vs-the-rest approach.
Forward accepts
- ``preds`` (float tensor): ``(N, ...)`` (binary) or ``(N, C, ...)`` (multiclass) tensor
with probabilities, where C is the number of classes.
- ``target`` (long tensor): ``(N, ...)`` or ``(N, C, ...)`` with integer labels
For non-binary input, if the ``preds`` and ``target`` tensor have the same
size the input will be interpretated as multilabel and if ``preds`` have one
dimension more than the ``target`` tensor the input will be interpretated as
multiclass.
Args:
num_classes: integer with number of classes. Not nessesary to provide
for binary problems.
pos_label: integer determining the positive class. Default is ``None``
which for binary problem is translate to 1. For multiclass problems
this argument should not be set as we iteratively change it in the
range [0,num_classes-1]
average:
- ``'macro'`` computes metric for each class and uniformly averages them
- ``'weighted'`` computes metric for each class and does a weighted-average,
where each class is weighted by their support (accounts for class imbalance)
- ``None`` computes and returns the metric per class
max_fpr:
If not ``None``, calculates standardized partial AUC over the
range [0, max_fpr]. Should be a float between 0 and 1.
compute_on_step:
Forward only calls ``update()`` and return None if this is set to False. default: True
dist_sync_on_step:
Synchronize metric state across processes at each ``forward()``
before returning the value at the step.
process_group:
Specify the process group on which synchronization is called. default: None (which selects the entire world)
dist_sync_fn:
Callback that performs the allgather operation on the metric state. When ``None``, DDP
will be used to perform the allgather
Raises:
ValueError:
If ``average`` is none of ``None``, ``"macro"`` or ``"weighted"``.
ValueError:
If ``max_fpr`` is not a ``float`` in the range ``(0, 1]``.
RuntimeError:
If ``PyTorch version`` is ``below 1.6`` since max_fpr requires ``torch.bucketize``
which is not available below 1.6.
ValueError:
If the mode of data (binary, multi-label, multi-class) changes between batches.
Example (binary case):
>>> from pytorch_lightning.metrics import AUROC
>>> preds = torch.tensor([0.13, 0.26, 0.08, 0.19, 0.34])
>>> target = torch.tensor([0, 0, 1, 1, 1])
>>> auroc = AUROC(pos_label=1)
>>> auroc(preds, target)
tensor(0.5000)
Example (multiclass case):
>>> from pytorch_lightning.metrics import AUROC
>>> preds = torch.tensor([[0.90, 0.05, 0.05],
... [0.05, 0.90, 0.05],
... [0.05, 0.05, 0.90],
... [0.85, 0.05, 0.10],
... [0.10, 0.10, 0.80]])
>>> target = torch.tensor([0, 1, 1, 2, 2])
>>> auroc = AUROC(num_classes=3)
>>> auroc(preds, target)
tensor(0.7778)
"""
class AUROC(_AUROC):

@deprecated(target=_AUROC, ver_deprecate="1.3.0", ver_remove="1.5.0")
def __init__(
self,
num_classes: Optional[int] = None,
Expand All @@ -111,74 +32,9 @@ def __init__(
process_group: Optional[Any] = None,
dist_sync_fn: Callable = None,
):
super().__init__(
compute_on_step=compute_on_step,
dist_sync_on_step=dist_sync_on_step,
process_group=process_group,
dist_sync_fn=dist_sync_fn,
)

self.num_classes = num_classes
self.pos_label = pos_label
self.average = average
self.max_fpr = max_fpr

allowed_average = (None, 'macro', 'weighted')
if self.average not in allowed_average:
raise ValueError(
f'Argument `average` expected to be one of the following: {allowed_average} but got {average}'
)

if self.max_fpr is not None:
if (not isinstance(max_fpr, float) and 0 < max_fpr <= 1):
raise ValueError(f"`max_fpr` should be a float in range (0, 1], got: {max_fpr}")

if LooseVersion(torch.__version__) < LooseVersion('1.6.0'):
raise RuntimeError(
'`max_fpr` argument requires `torch.bucketize` which is not available below PyTorch version 1.6'
)

self.mode = None
self.add_state("preds", default=[], dist_reduce_fx=None)
self.add_state("target", default=[], dist_reduce_fx=None)

rank_zero_warn(
'Metric `AUROC` will save all targets and predictions in buffer.'
' For large datasets this may lead to large memory footprint.'
)

def update(self, preds: torch.Tensor, target: torch.Tensor):
"""
Update state with predictions and targets.
This implementation refers to :class:`~torchmetrics.AUROC`.
Args:
preds: Predictions from model (probabilities, or labels)
target: Ground truth labels
"""
preds, target, mode = _auroc_update(preds, target)

self.preds.append(preds)
self.target.append(target)

if self.mode is not None and self.mode != mode:
raise ValueError(
'The mode of data (binary, multi-label, multi-class) should be constant, but changed'
f' between batches from {self.mode} to {mode}'
)
self.mode = mode

def compute(self) -> torch.Tensor:
"""
Computes AUROC based on inputs passed in to ``update`` previously.
.. deprecated::
Use :class:`~torchmetrics.AUROC`. Will be removed in v1.5.0.
"""
preds = torch.cat(self.preds, dim=0)
target = torch.cat(self.target, dim=0)
return _auroc_compute(
preds,
target,
self.mode,
self.num_classes,
self.pos_label,
self.average,
self.max_fpr,
)
Loading

0 comments on commit 9e35f97

Please sign in to comment.