diff --git a/CHANGELOG.md b/CHANGELOG.md index b3ac4faf9ccfe1..6a7282de1c5fbe 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,8 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/). ### Added - Added support to checkpoint after training batches in `ModelCheckpoint` callback ([#6146](https://github.com/PyTorchLightning/pytorch-lightning/pull/6146)) +- Added a way to print to terminal without breaking up the progress bar ([#5470](https://github.com/PyTorchLightning/pytorch-lightning/pull/5470)) + ### Changed @@ -25,6 +27,11 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/). - Removed deprecated Trainer argument `enable_pl_optimizer` and `automatic_optimization` ([#6163](https://github.com/PyTorchLightning/pytorch-lightning/pull/6163)) +- Removed deprecated metrics ([#6161](https://github.com/PyTorchLightning/pytorch-lightning/pull/6161)) + * from `pytorch_lightning.metrics.functional.classification` removed `to_onehot`, `to_categorical`, `get_num_classes`, `roc`, `multiclass_roc`, `average_precision`, `precision_recall_curve`, `multiclass_precision_recall_curve` + * from `pytorch_lightning.metrics.functional.reduction` removed `reduce`, `class_reduce` + + ### Fixed - Made the `Plugin.reduce` method more consistent across all Plugins to reflect a mean-reduction by default ([#6011](https://github.com/PyTorchLightning/pytorch-lightning/pull/6011)) @@ -94,7 +101,6 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/). - Added `Trainer` flag to activate Stochastic Weight Averaging (SWA) `Trainer(stochastic_weight_avg=True)` ([#6038](https://github.com/PyTorchLightning/pytorch-lightning/pull/6038)) - Added DeepSpeed integration ([#5954](https://github.com/PyTorchLightning/pytorch-lightning/pull/5954), [#6042](https://github.com/PyTorchLightning/pytorch-lightning/pull/6042)) -- Added a way to print to terminal without breaking up the progress bar ([#5470](https://github.com/PyTorchLightning/pytorch-lightning/pull/5470)) ### Changed diff --git a/pytorch_lightning/metrics/functional/__init__.py b/pytorch_lightning/metrics/functional/__init__.py index 74e36bcc54b138..3b31dad5d3411d 100644 --- a/pytorch_lightning/metrics/functional/__init__.py +++ b/pytorch_lightning/metrics/functional/__init__.py @@ -21,7 +21,6 @@ multiclass_auroc, stat_scores_multiple_classes, to_categorical, - to_onehot, ) from pytorch_lightning.metrics.functional.confusion_matrix import confusion_matrix # noqa: F401 from pytorch_lightning.metrics.functional.explained_variance import explained_variance # noqa: F401 diff --git a/pytorch_lightning/metrics/functional/classification.py b/pytorch_lightning/metrics/functional/classification.py index ab3dbaaca8fd5c..e697ade9be16b4 100644 --- a/pytorch_lightning/metrics/functional/classification.py +++ b/pytorch_lightning/metrics/functional/classification.py @@ -18,70 +18,11 @@ from pytorch_lightning.metrics.functional.auc import auc as __auc from pytorch_lightning.metrics.functional.auroc import auroc as __auroc -from pytorch_lightning.metrics.functional.average_precision import average_precision as __ap from pytorch_lightning.metrics.functional.iou import iou as __iou -from pytorch_lightning.metrics.functional.precision_recall_curve import _binary_clf_curve -from pytorch_lightning.metrics.functional.precision_recall_curve import precision_recall_curve as __prc -from pytorch_lightning.metrics.functional.roc import roc as __roc -from pytorch_lightning.metrics.utils import class_reduce -from pytorch_lightning.metrics.utils import get_num_classes as __gnc -from pytorch_lightning.metrics.utils import reduce -from pytorch_lightning.metrics.utils import to_categorical as __tc -from pytorch_lightning.metrics.utils import to_onehot as __to +from pytorch_lightning.metrics.utils import class_reduce, get_num_classes, reduce, to_categorical from pytorch_lightning.utilities import rank_zero_warn -def to_onehot( - tensor: torch.Tensor, - num_classes: Optional[int] = None, -) -> torch.Tensor: - """ - Converts a dense label tensor to one-hot format - - .. warning :: Deprecated in favor of :func:`~pytorch_lightning.metrics.utils.to_onehot` - """ - rank_zero_warn( - "This `to_onehot` was deprecated in v1.1.0 in favor of" - " `from pytorch_lightning.metrics.utils import to_onehot`." - " It will be removed in v1.3.0", DeprecationWarning - ) - return __to(tensor, num_classes) - - -def to_categorical(tensor: torch.Tensor, argmax_dim: int = 1) -> torch.Tensor: - """ - Converts a tensor of probabilities to a dense label tensor - - .. warning :: Deprecated in favor of :func:`~pytorch_lightning.metrics.utils.to_categorical` - - """ - rank_zero_warn( - "This `to_categorical` was deprecated in v1.1.0 in favor of" - " `from pytorch_lightning.metrics.utils import to_categorical`." - " It will be removed in v1.3.0", DeprecationWarning - ) - return __tc(tensor) - - -def get_num_classes( - pred: torch.Tensor, - target: torch.Tensor, - num_classes: Optional[int] = None, -) -> int: - """ - Calculates the number of classes for a given prediction and target tensor. - - .. warning :: Deprecated in favor of :func:`~pytorch_lightning.metrics.utils.get_num_classes` - - """ - rank_zero_warn( - "This `get_num_classes` was deprecated in v1.1.0 in favor of" - " `from pytorch_lightning.metrics.utils import get_num_classes`." - " It will be removed in v1.3.0", DeprecationWarning - ) - return __gnc(pred, target, num_classes) - - def stat_scores( pred: torch.Tensor, target: torch.Tensor, @@ -122,6 +63,7 @@ def stat_scores( return tp, fp, tn, fn, sup +# todo: remove in 1.4 def stat_scores_multiple_classes( pred: torch.Tensor, target: torch.Tensor, @@ -210,6 +152,7 @@ def _confmat_normalize(cm): return cm +# todo: remove in 1.4 def precision_recall( pred: torch.Tensor, target: torch.Tensor, @@ -268,6 +211,7 @@ def precision_recall( return precision, recall +# todo: remove in 1.4 def precision( pred: torch.Tensor, target: torch.Tensor, @@ -311,6 +255,7 @@ def precision( return precision_recall(pred=pred, target=target, num_classes=num_classes, class_reduction=class_reduction)[0] +# todo: remove in 1.4 def recall( pred: torch.Tensor, target: torch.Tensor, @@ -353,128 +298,7 @@ def recall( return precision_recall(pred=pred, target=target, num_classes=num_classes, class_reduction=class_reduction)[1] -# todo: remove in 1.3 -def roc( - pred: torch.Tensor, - target: torch.Tensor, - sample_weight: Optional[Sequence] = None, - pos_label: int = 1., -) -> Tuple[torch.Tensor, torch.Tensor, torch.Tensor]: - """ - Computes the Receiver Operating Characteristic (ROC). It assumes classifier is binary. - - .. warning :: Deprecated in favor of :func:`~pytorch_lightning.metrics.functional.roc.roc` - """ - rank_zero_warn( - "This `multiclass_roc` was deprecated in v1.1.0 in favor of" - " `from pytorch_lightning.metrics.functional.roc import roc`." - " It will be removed in v1.3.0", DeprecationWarning - ) - return __roc(preds=pred, target=target, sample_weights=sample_weight, pos_label=pos_label) - - -# TODO: deprecated in favor of general ROC in pytorch_lightning/metrics/functional/roc.py -def _roc( - pred: torch.Tensor, - target: torch.Tensor, - sample_weight: Optional[Sequence] = None, - pos_label: int = 1., -) -> Tuple[torch.Tensor, torch.Tensor, torch.Tensor]: - """ - Computes the Receiver Operating Characteristic (ROC). It assumes classifier is binary. - - .. warning :: Deprecated in favor of :func:`~pytorch_lightning.metrics.functional.roc.roc` - - Example: - - >>> x = torch.tensor([0, 1, 2, 3]) - >>> y = torch.tensor([0, 1, 1, 1]) - >>> fpr, tpr, thresholds = _roc(x, y) - >>> fpr - tensor([0., 0., 0., 0., 1.]) - >>> tpr - tensor([0.0000, 0.3333, 0.6667, 1.0000, 1.0000]) - >>> thresholds - tensor([4, 3, 2, 1, 0]) - - """ - rank_zero_warn( - "This `multiclass_roc` was deprecated in v1.1.0 in favor of" - " `from pytorch_lightning.metrics.functional.roc import roc`." - " It will be removed in v1.3.0", DeprecationWarning - ) - fps, tps, thresholds = _binary_clf_curve(pred, target, sample_weights=sample_weight, pos_label=pos_label) - - # Add an extra threshold position - # to make sure that the curve starts at (0, 0) - tps = torch.cat([torch.zeros(1, dtype=tps.dtype, device=tps.device), tps]) - fps = torch.cat([torch.zeros(1, dtype=fps.dtype, device=fps.device), fps]) - thresholds = torch.cat([thresholds[0][None] + 1, thresholds]) - - if fps[-1] <= 0: - raise ValueError("No negative samples in targets, false positive value should be meaningless") - - fpr = fps / fps[-1] - - if tps[-1] <= 0: - raise ValueError("No positive samples in targets, true positive value should be meaningless") - - tpr = tps / tps[-1] - - return fpr, tpr, thresholds - - -# TODO: deprecated in favor of general ROC in pytorch_lightning/metrics/functional/roc.py -def multiclass_roc( - pred: torch.Tensor, - target: torch.Tensor, - sample_weight: Optional[Sequence] = None, - num_classes: Optional[int] = None, -) -> Tuple[Tuple[torch.Tensor, torch.Tensor, torch.Tensor]]: - """ - Computes the Receiver Operating Characteristic (ROC) for multiclass predictors. - - .. warning :: Deprecated in favor of :func:`~pytorch_lightning.metrics.functional.roc.roc` - - Args: - pred: estimated probabilities - target: ground-truth labels - sample_weight: sample weights - num_classes: number of classes (default: None, computes automatically from data) - - Return: - returns roc for each class. - Number of classes, false-positive rate (fpr), true-positive rate (tpr), thresholds - - Example: - - >>> pred = torch.tensor([[0.85, 0.05, 0.05, 0.05], - ... [0.05, 0.85, 0.05, 0.05], - ... [0.05, 0.05, 0.85, 0.05], - ... [0.05, 0.05, 0.05, 0.85]]) - >>> target = torch.tensor([0, 1, 3, 2]) - >>> multiclass_roc(pred, target) # doctest: +NORMALIZE_WHITESPACE - ((tensor([0., 0., 1.]), tensor([0., 1., 1.]), tensor([1.8500, 0.8500, 0.0500])), - (tensor([0., 0., 1.]), tensor([0., 1., 1.]), tensor([1.8500, 0.8500, 0.0500])), - (tensor([0.0000, 0.3333, 1.0000]), tensor([0., 0., 1.]), tensor([1.8500, 0.8500, 0.0500])), - (tensor([0.0000, 0.3333, 1.0000]), tensor([0., 0., 1.]), tensor([1.8500, 0.8500, 0.0500]))) - """ - rank_zero_warn( - "This `multiclass_roc` was deprecated in v1.1.0 in favor of" - " `from pytorch_lightning.metrics.functional.roc import roc`." - " It will be removed in v1.3.0", DeprecationWarning - ) - num_classes = get_num_classes(pred, target, num_classes) - - class_roc_vals = [] - for c in range(num_classes): - pred_c = pred[:, c] - - class_roc_vals.append(_roc(pred=pred_c, target=target, sample_weight=sample_weight, pos_label=c)) - - return tuple(class_roc_vals) - - +# todo: remove in 1.4 def auc( x: torch.Tensor, y: torch.Tensor, @@ -508,6 +332,7 @@ def auc( return __auc(x, y) +# todo: remove in 1.4 def auc_decorator() -> Callable: rank_zero_warn("This `auc_decorator` was deprecated in v1.2.0." " It will be removed in v1.4.0", DeprecationWarning) @@ -524,6 +349,7 @@ def new_func(*args, **kwargs) -> torch.Tensor: return wrapper +# todo: remove in 1.4 def multiclass_auc_decorator() -> Callable: rank_zero_warn( "This `multiclass_auc_decorator` was deprecated in v1.2.0." @@ -546,6 +372,7 @@ def new_func(*args, **kwargs) -> torch.Tensor: return wrapper +# todo: remove in 1.4 def auroc( pred: torch.Tensor, target: torch.Tensor, @@ -588,6 +415,7 @@ def auroc( ) +# todo: remove in 1.4 def multiclass_auroc( pred: torch.Tensor, target: torch.Tensor, @@ -767,68 +595,3 @@ def iou( num_classes=num_classes, reduction=reduction ) - - -# todo: remove in 1.3 -def precision_recall_curve( - pred: torch.Tensor, - target: torch.Tensor, - sample_weight: Optional[Sequence] = None, - pos_label: int = 1., -): - """ - Computes precision-recall pairs for different thresholds. - - .. warning :: Deprecated in favor of - :func:`~pytorch_lightning.metrics.functional.precision_recall_curve.precision_recall_curve` - """ - rank_zero_warn( - "This `precision_recall_curve` was deprecated in v1.1.0 in favor of" - " `from pytorch_lightning.metrics.functional.precision_recall_curve import precision_recall_curve`." - " It will be removed in v1.3.0", DeprecationWarning - ) - return __prc(preds=pred, target=target, sample_weights=sample_weight, pos_label=pos_label) - - -# todo: remove in 1.3 -def multiclass_precision_recall_curve( - pred: torch.Tensor, - target: torch.Tensor, - sample_weight: Optional[Sequence] = None, - num_classes: Optional[int] = None, -): - """ - Computes precision-recall pairs for different thresholds given a multiclass scores. - - .. warning :: Deprecated in favor of - :func:`~pytorch_lightning.metrics.functional.precision_recall_curve.precision_recall_curve` - """ - rank_zero_warn( - "This `multiclass_precision_recall_curve` was deprecated in v1.1.0 in favor of" - " `from pytorch_lightning.metrics.functional.precision_recall_curve import precision_recall_curve`." - " It will be removed in v1.3.0", DeprecationWarning - ) - if num_classes is None: - num_classes = get_num_classes(pred, target, num_classes) - return __prc(preds=pred, target=target, sample_weights=sample_weight, num_classes=num_classes) - - -# todo: remove in 1.3 -def average_precision( - pred: torch.Tensor, - target: torch.Tensor, - sample_weight: Optional[Sequence] = None, - pos_label: int = 1., -): - """ - Compute average precision from prediction scores. - - .. warning :: Deprecated in favor of - :func:`~pytorch_lightning.metrics.functional.average_precision.average_precision` - """ - rank_zero_warn( - "This `average_precision` was deprecated in v1.1.0 in favor of" - " `pytorch_lightning.metrics.functional.average_precision import average_precision`." - " It will be removed in v1.3.0", DeprecationWarning - ) - return __ap(preds=pred, target=target, sample_weights=sample_weight, pos_label=pos_label) diff --git a/pytorch_lightning/metrics/functional/iou.py b/pytorch_lightning/metrics/functional/iou.py index 5413586e0f26f3..7b6851b5cebd06 100644 --- a/pytorch_lightning/metrics/functional/iou.py +++ b/pytorch_lightning/metrics/functional/iou.py @@ -16,8 +16,7 @@ import torch from pytorch_lightning.metrics.functional.confusion_matrix import _confusion_matrix_update -from pytorch_lightning.metrics.functional.reduction import reduce -from pytorch_lightning.metrics.utils import get_num_classes +from pytorch_lightning.metrics.utils import get_num_classes, reduce def _iou_from_confmat( diff --git a/pytorch_lightning/metrics/functional/reduction.py b/pytorch_lightning/metrics/functional/reduction.py deleted file mode 100644 index 34c3ff32f68885..00000000000000 --- a/pytorch_lightning/metrics/functional/reduction.py +++ /dev/null @@ -1,36 +0,0 @@ -# Copyright The PyTorch Lightning team. -# -# 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. -import torch - -from pytorch_lightning.metrics.utils import class_reduce as __cr -from pytorch_lightning.metrics.utils import reduce as __reduce -from pytorch_lightning.utilities import rank_zero_warn - - -def reduce(to_reduce: torch.Tensor, reduction: str) -> torch.Tensor: - rank_zero_warn( - "This `reduce` was deprecated in v1.1.0 in favor of" - " `pytorch_lightning.metrics.utils import reduce`." - " It will be removed in v1.3.0", DeprecationWarning - ) - return __reduce(to_reduce=to_reduce, reduction=reduction) - - -def class_reduce(num: torch.Tensor, denom: torch.Tensor, weights: torch.Tensor, class_reduction: str = 'none'): - rank_zero_warn( - "This `class_reduce` was deprecated in v1.1.0 in favor of" - " `pytorch_lightning.metrics.utils import class_reduce`." - " It will be removed in v1.3.0", DeprecationWarning - ) - return __cr(num=num, denom=denom, weights=weights, class_reduction=class_reduction) diff --git a/pytorch_lightning/trainer/connectors/callback_connector.py b/pytorch_lightning/trainer/connectors/callback_connector.py index 14694c8f778116..ee768c05cc8a27 100644 --- a/pytorch_lightning/trainer/connectors/callback_connector.py +++ b/pytorch_lightning/trainer/connectors/callback_connector.py @@ -14,12 +14,7 @@ import os from typing import List, Union -from pytorch_lightning.callbacks import ( - Callback, - ModelCheckpoint, - ProgressBar, - ProgressBarBase, -) +from pytorch_lightning.callbacks import Callback, ModelCheckpoint, ProgressBar, ProgressBarBase from pytorch_lightning.core.lightning import LightningModule from pytorch_lightning.utilities import rank_zero_info, rank_zero_warn from pytorch_lightning.utilities.exceptions import MisconfigurationException diff --git a/pytorch_lightning/trainer/optimizers.py b/pytorch_lightning/trainer/optimizers.py index ea881b796e8255..5cafa438cffcc2 100644 --- a/pytorch_lightning/trainer/optimizers.py +++ b/pytorch_lightning/trainer/optimizers.py @@ -13,7 +13,7 @@ # limitations under the License. from abc import ABC -from typing import List, Optional, Tuple, Dict, Any +from typing import Any, Dict, List, Optional, Tuple import torch from torch import optim diff --git a/tests/deprecated_api/test_remove_1-3.py b/tests/deprecated_api/test_remove_1-3.py index 54a96045b08a72..2cf0dd990c8d79 100644 --- a/tests/deprecated_api/test_remove_1-3.py +++ b/tests/deprecated_api/test_remove_1-3.py @@ -14,7 +14,6 @@ """Test deprecated functionality which will be removed in vX.Y.Z""" import pytest -import torch from pytorch_lightning import LightningModule, Trainer from pytorch_lightning.callbacks import EarlyStopping, ModelCheckpoint @@ -45,64 +44,3 @@ def __init__(self, hparams): self.hparams = hparams DeprecatedHparamsModel({}) - - -def test_v1_3_0_deprecated_metrics(): - from pytorch_lightning.metrics.functional.classification import to_onehot - with pytest.deprecated_call(match='will be removed in v1.3'): - to_onehot(torch.tensor([1, 2, 3])) - - from pytorch_lightning.metrics.functional.classification import to_categorical - with pytest.deprecated_call(match='will be removed in v1.3'): - to_categorical(torch.tensor([[0.2, 0.5], [0.9, 0.1]])) - - from pytorch_lightning.metrics.functional.classification import get_num_classes - with pytest.deprecated_call(match='will be removed in v1.3'): - get_num_classes(pred=torch.tensor([0, 1]), target=torch.tensor([1, 1])) - - x_binary = torch.tensor([0, 1, 2, 3]) - y_binary = torch.tensor([0, 1, 2, 3]) - - from pytorch_lightning.metrics.functional.classification import roc - with pytest.deprecated_call(match='will be removed in v1.3'): - roc(pred=x_binary, target=y_binary) - - from pytorch_lightning.metrics.functional.classification import _roc - with pytest.deprecated_call(match='will be removed in v1.3'): - _roc(pred=x_binary, target=y_binary) - - x_multy = torch.tensor([ - [0.85, 0.05, 0.05, 0.05], - [0.05, 0.85, 0.05, 0.05], - [0.05, 0.05, 0.85, 0.05], - [0.05, 0.05, 0.05, 0.85], - ]) - y_multy = torch.tensor([0, 1, 3, 2]) - - from pytorch_lightning.metrics.functional.classification import multiclass_roc - with pytest.deprecated_call(match='will be removed in v1.3'): - multiclass_roc(pred=x_multy, target=y_multy) - - from pytorch_lightning.metrics.functional.classification import average_precision - with pytest.deprecated_call(match='will be removed in v1.3'): - average_precision(pred=x_binary, target=y_binary) - - from pytorch_lightning.metrics.functional.classification import precision_recall_curve - with pytest.deprecated_call(match='will be removed in v1.3'): - precision_recall_curve(pred=x_binary, target=y_binary) - - from pytorch_lightning.metrics.functional.classification import multiclass_precision_recall_curve - with pytest.deprecated_call(match='will be removed in v1.3'): - multiclass_precision_recall_curve(pred=x_multy, target=y_multy) - - from pytorch_lightning.metrics.functional.reduction import reduce - with pytest.deprecated_call(match='will be removed in v1.3'): - reduce(torch.tensor([0, 1, 1, 0]), 'sum') - - from pytorch_lightning.metrics.functional.reduction import class_reduce - with pytest.deprecated_call(match='will be removed in v1.3'): - class_reduce( - torch.randint(1, 10, (50, )).float(), - torch.randint(10, 20, (50, )).float(), - torch.randint(1, 100, (50, )).float() - )