Skip to content

Commit 6453091

Browse files
authored
Prune metrics base classes 2/n (#6530)
* base class * extensions * chlog * _stable_1d_sort * _check_same_shape * _input_format_classification_one_hot * utils * to_onehot * select_topk * to_categorical * get_num_classes * reduce * class_reduce * tests
1 parent 9c59733 commit 6453091

29 files changed

+187
-1921
lines changed

CHANGELOG.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,11 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).
6565
- Deprecated `trainer.running_sanity_check` in favor of `trainer.sanity_checking` ([#4945](https://github.com/PyTorchLightning/pytorch-lightning/pull/4945))
6666

6767

68-
- Deprecated metrics in favor of `torchmetrics` ([#6505](https://github.com/PyTorchLightning/pytorch-lightning/pull/6505))
68+
- Deprecated metrics in favor of `torchmetrics` ([#6505](https://github.com/PyTorchLightning/pytorch-lightning/pull/6505),
69+
70+
[#6530](https://github.com/PyTorchLightning/pytorch-lightning/pull/6530),
71+
72+
)
6973

7074

7175
### Removed

pl_examples/basic_examples/conv_sequential_example.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,11 @@
2727
import torch.nn as nn
2828
import torch.nn.functional as F
2929
import torchvision
30+
from torchmetrics.functional import accuracy
3031

3132
import pytorch_lightning as pl
3233
from pl_examples import cli_lightning_logo
3334
from pytorch_lightning import Trainer
34-
from torchmetrics.functional import accuracy
3535
from pytorch_lightning.plugins import RPCSequentialPlugin
3636
from pytorch_lightning.utilities import _BOLTS_AVAILABLE, _FAIRSCALE_PIPE_AVAILABLE
3737

pytorch_lightning/accelerators/gpu.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import logging
22
import os
3-
from typing import TYPE_CHECKING, Any
3+
from typing import Any, TYPE_CHECKING
44

55
import torch
66

pytorch_lightning/metrics/classification/helpers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515

1616
import numpy as np
1717
import torch
18+
from torchmetrics.utilities.data import select_topk, to_onehot
1819

19-
from pytorch_lightning.metrics.utils import select_topk, to_onehot
2020
from pytorch_lightning.utilities import LightningEnum
2121

2222

Lines changed: 24 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,30 @@
1+
# Copyright The PyTorch 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.
114
from typing import Callable, Union
215

316
import torch
17+
from torchmetrics import Metric
18+
from torchmetrics.metric import CompositionalMetric as __CompositionalMetric
419

5-
from pytorch_lightning.metrics.metric import Metric
20+
from pytorch_lightning.utilities import rank_zero_warn
621

722

8-
class CompositionalMetric(Metric):
9-
"""Composition of two metrics with a specific operator
10-
which will be executed upon metric's compute
23+
class CompositionalMetric(__CompositionalMetric):
24+
r"""
25+
This implementation refers to :class:`~torchmetrics.metric.CompositionalMetric`.
1126
27+
.. warning:: This metric is deprecated, use ``torchmetrics.metric.CompositionalMetric``. Will be removed in v1.5.0.
1228
"""
1329

1430
def __init__(
@@ -17,76 +33,8 @@ def __init__(
1733
metric_a: Union[Metric, int, float, torch.Tensor],
1834
metric_b: Union[Metric, int, float, torch.Tensor, None],
1935
):
20-
"""
21-
22-
Args:
23-
operator: the operator taking in one (if metric_b is None)
24-
or two arguments. Will be applied to outputs of metric_a.compute()
25-
and (optionally if metric_b is not None) metric_b.compute()
26-
metric_a: first metric whose compute() result is the first argument of operator
27-
metric_b: second metric whose compute() result is the second argument of operator.
28-
For operators taking in only one input, this should be None
29-
"""
30-
super().__init__()
31-
32-
self.op = operator
33-
34-
if isinstance(metric_a, torch.Tensor):
35-
self.register_buffer("metric_a", metric_a)
36-
else:
37-
self.metric_a = metric_a
38-
39-
if isinstance(metric_b, torch.Tensor):
40-
self.register_buffer("metric_b", metric_b)
41-
else:
42-
self.metric_b = metric_b
43-
44-
def _sync_dist(self, dist_sync_fn=None):
45-
# No syncing required here. syncing will be done in metric_a and metric_b
46-
pass
47-
48-
def update(self, *args, **kwargs):
49-
if isinstance(self.metric_a, Metric):
50-
self.metric_a.update(*args, **self.metric_a._filter_kwargs(**kwargs))
51-
52-
if isinstance(self.metric_b, Metric):
53-
self.metric_b.update(*args, **self.metric_b._filter_kwargs(**kwargs))
54-
55-
def compute(self):
56-
57-
# also some parsing for kwargs?
58-
if isinstance(self.metric_a, Metric):
59-
val_a = self.metric_a.compute()
60-
else:
61-
val_a = self.metric_a
62-
63-
if isinstance(self.metric_b, Metric):
64-
val_b = self.metric_b.compute()
65-
else:
66-
val_b = self.metric_b
67-
68-
if val_b is None:
69-
return self.op(val_a)
70-
71-
return self.op(val_a, val_b)
72-
73-
def reset(self):
74-
if isinstance(self.metric_a, Metric):
75-
self.metric_a.reset()
76-
77-
if isinstance(self.metric_b, Metric):
78-
self.metric_b.reset()
79-
80-
def persistent(self, mode: bool = False):
81-
if isinstance(self.metric_a, Metric):
82-
self.metric_a.persistent(mode=mode)
83-
if isinstance(self.metric_b, Metric):
84-
self.metric_b.persistent(mode=mode)
85-
86-
def __repr__(self):
87-
repr_str = (
88-
self.__class__.__name__
89-
+ f"(\n {self.op.__name__}(\n {repr(self.metric_a)},\n {repr(self.metric_b)}\n )\n)"
36+
rank_zero_warn(
37+
"This `Metric` was deprecated since v1.3.0 in favor of `torchmetrics.Metric`."
38+
" It will be removed in v1.5.0", DeprecationWarning
9039
)
91-
92-
return repr_str
40+
super().__init__(operator=operator, metric_a=metric_a, metric_b=metric_b)

pytorch_lightning/metrics/functional/auc.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@
1414
from typing import Tuple
1515

1616
import torch
17-
18-
from pytorch_lightning.metrics.utils import _stable_1d_sort
17+
from torchmetrics.utilities.data import _stable_1d_sort
1918

2019

2120
def _auc_update(x: torch.Tensor, y: torch.Tensor) -> Tuple[torch.Tensor, torch.Tensor]:

pytorch_lightning/metrics/functional/classification.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,12 @@
1515
from typing import Callable, Optional, Sequence, Tuple
1616

1717
import torch
18+
from torchmetrics.utilities import class_reduce, reduce
19+
from torchmetrics.utilities.data import get_num_classes, to_categorical
1820

1921
from pytorch_lightning.metrics.functional.auc import auc as __auc
2022
from pytorch_lightning.metrics.functional.auroc import auroc as __auroc
2123
from pytorch_lightning.metrics.functional.iou import iou as __iou
22-
from pytorch_lightning.metrics.utils import class_reduce, get_num_classes, reduce, to_categorical
2324
from pytorch_lightning.utilities import rank_zero_warn
2425

2526

pytorch_lightning/metrics/functional/explained_variance.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@
1414
from typing import Sequence, Tuple, Union
1515

1616
import torch
17-
18-
from pytorch_lightning.metrics.utils import _check_same_shape
17+
from torchmetrics.utilities.checks import _check_same_shape
1918

2019

2120
def _explained_variance_update(preds: torch.Tensor, target: torch.Tensor) -> Tuple[torch.Tensor, torch.Tensor]:

pytorch_lightning/metrics/functional/f_beta.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414
from typing import Tuple
1515

1616
import torch
17-
18-
from pytorch_lightning.metrics.utils import _input_format_classification_one_hot, class_reduce
17+
from torchmetrics.utilities import class_reduce
18+
from torchmetrics.utilities.checks import _input_format_classification_one_hot
1919

2020

2121
def _fbeta_update(

pytorch_lightning/metrics/functional/iou.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,10 @@
1414
from typing import Optional
1515

1616
import torch
17+
from torchmetrics.utilities import reduce
18+
from torchmetrics.utilities.data import get_num_classes
1719

1820
from pytorch_lightning.metrics.functional.confusion_matrix import _confusion_matrix_update
19-
from pytorch_lightning.metrics.utils import get_num_classes, reduce
2021

2122

2223
def _iou_from_confmat(

pytorch_lightning/metrics/functional/mean_absolute_error.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@
1414
from typing import Tuple
1515

1616
import torch
17-
18-
from pytorch_lightning.metrics.utils import _check_same_shape
17+
from torchmetrics.utilities.checks import _check_same_shape
1918

2019

2120
def _mean_absolute_error_update(preds: torch.Tensor, target: torch.Tensor) -> Tuple[torch.Tensor, int]:

pytorch_lightning/metrics/functional/mean_relative_error.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@
1414
from typing import Tuple
1515

1616
import torch
17-
18-
from pytorch_lightning.metrics.utils import _check_same_shape
17+
from torchmetrics.utilities.checks import _check_same_shape
1918

2019

2120
def _mean_relative_error_update(preds: torch.Tensor, target: torch.Tensor) -> Tuple[torch.Tensor, int]:

pytorch_lightning/metrics/functional/mean_squared_error.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@
1414
from typing import Tuple
1515

1616
import torch
17-
18-
from pytorch_lightning.metrics.utils import _check_same_shape
17+
from torchmetrics.utilities.checks import _check_same_shape
1918

2019

2120
def _mean_squared_error_update(preds: torch.Tensor, target: torch.Tensor) -> Tuple[torch.Tensor, int]:

pytorch_lightning/metrics/functional/mean_squared_log_error.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@
1414
from typing import Tuple
1515

1616
import torch
17-
18-
from pytorch_lightning.metrics.utils import _check_same_shape
17+
from torchmetrics.utilities.checks import _check_same_shape
1918

2019

2120
def _mean_squared_log_error_update(preds: torch.Tensor, target: torch.Tensor) -> Tuple[torch.Tensor, int]:

pytorch_lightning/metrics/functional/psnr.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@
1414
from typing import Optional, Tuple, Union
1515

1616
import torch
17+
from torchmetrics.utilities import reduce
1718

18-
from pytorch_lightning import utilities
19-
from pytorch_lightning.metrics import utils
19+
from pytorch_lightning.utilities import rank_zero_warn
2020

2121

2222
def _psnr_compute(
@@ -28,7 +28,7 @@ def _psnr_compute(
2828
) -> torch.Tensor:
2929
psnr_base_e = 2 * torch.log(data_range) - torch.log(sum_squared_error / n_obs)
3030
psnr = psnr_base_e * (10 / torch.log(torch.tensor(base)))
31-
return utils.reduce(psnr, reduction=reduction)
31+
return reduce(psnr, reduction=reduction)
3232

3333

3434
def _psnr_update(preds: torch.Tensor,
@@ -97,7 +97,7 @@ def psnr(
9797
9898
"""
9999
if dim is None and reduction != 'elementwise_mean':
100-
utilities.rank_zero_warn(f'The `reduction={reduction}` will not have any effect when `dim` is None.')
100+
rank_zero_warn(f'The `reduction={reduction}` will not have any effect when `dim` is None.')
101101

102102
if data_range is None:
103103
if dim is not None:

pytorch_lightning/metrics/functional/r2score.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414
from typing import Tuple
1515

1616
import torch
17+
from torchmetrics.utilities.checks import _check_same_shape
1718

18-
from pytorch_lightning.metrics.utils import _check_same_shape
1919
from pytorch_lightning.utilities import rank_zero_warn
2020

2121

pytorch_lightning/metrics/functional/ssim.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515

1616
import torch
1717
from torch.nn import functional as F
18-
19-
from pytorch_lightning.metrics.utils import _check_same_shape, reduce
18+
from torchmetrics.utilities import reduce
19+
from torchmetrics.utilities.checks import _check_same_shape
2020

2121

2222
def _gaussian(kernel_size: int, sigma: int, dtype: torch.dtype, device: torch.device):

0 commit comments

Comments
 (0)