2020-10-13 11:18:07 +00:00
|
|
|
# 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.
|
2021-03-15 19:28:18 +00:00
|
|
|
from typing import List, Optional
|
2020-10-06 21:03:24 +00:00
|
|
|
|
2020-11-23 08:44:35 +00:00
|
|
|
import torch
|
2021-03-16 14:55:31 +00:00
|
|
|
from torchmetrics.utilities.data import dim_zero_cat as _dim_zero_cat
|
|
|
|
from torchmetrics.utilities.data import dim_zero_mean as _dim_zero_mean
|
|
|
|
from torchmetrics.utilities.data import dim_zero_sum as _dim_zero_sum
|
|
|
|
from torchmetrics.utilities.data import get_num_classes as _get_num_classes
|
|
|
|
from torchmetrics.utilities.data import select_topk as _select_topk
|
|
|
|
from torchmetrics.utilities.data import to_categorical as _to_categorical
|
|
|
|
from torchmetrics.utilities.data import to_onehot as _to_onehot
|
|
|
|
from torchmetrics.utilities.distributed import class_reduce as _class_reduce
|
|
|
|
from torchmetrics.utilities.distributed import reduce as _reduce
|
2020-10-06 21:03:24 +00:00
|
|
|
|
2021-03-16 14:55:31 +00:00
|
|
|
from pytorch_lightning.utilities.deprecation import deprecated
|
2020-10-06 21:03:24 +00:00
|
|
|
|
2020-10-10 16:31:00 +00:00
|
|
|
|
2021-03-16 14:55:31 +00:00
|
|
|
@deprecated(target=_dim_zero_cat, ver_deprecate="1.3.0", ver_remove="1.5.0")
|
2020-10-06 21:03:24 +00:00
|
|
|
def dim_zero_cat(x):
|
2021-03-16 14:55:31 +00:00
|
|
|
pass
|
2020-10-06 21:03:24 +00:00
|
|
|
|
|
|
|
|
2021-03-16 14:55:31 +00:00
|
|
|
@deprecated(target=_dim_zero_sum, ver_deprecate="1.3.0", ver_remove="1.5.0")
|
2020-10-06 21:03:24 +00:00
|
|
|
def dim_zero_sum(x):
|
2021-03-16 14:55:31 +00:00
|
|
|
pass
|
2020-10-06 21:03:24 +00:00
|
|
|
|
|
|
|
|
2021-03-16 14:55:31 +00:00
|
|
|
@deprecated(target=_dim_zero_mean, ver_deprecate="1.3.0", ver_remove="1.5.0")
|
2020-10-06 21:03:24 +00:00
|
|
|
def dim_zero_mean(x):
|
2021-03-16 14:55:31 +00:00
|
|
|
pass
|
2020-12-04 21:42:23 +00:00
|
|
|
|
|
|
|
|
2021-03-15 11:18:43 +00:00
|
|
|
def get_group_indexes(idx: torch.Tensor) -> List[torch.Tensor]:
|
|
|
|
"""
|
|
|
|
Given an integer `torch.Tensor` `idx`, return a `torch.Tensor` of indexes for
|
|
|
|
each different value in `idx`.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
idx: a `torch.Tensor` of integers
|
|
|
|
|
|
|
|
Return:
|
|
|
|
A list of integer `torch.Tensor`s
|
|
|
|
|
|
|
|
Example:
|
|
|
|
|
|
|
|
>>> indexes = torch.tensor([0, 0, 0, 1, 1, 1, 1])
|
|
|
|
>>> groups = get_group_indexes(indexes)
|
|
|
|
>>> groups
|
|
|
|
[tensor([0, 1, 2]), tensor([3, 4, 5, 6])]
|
|
|
|
"""
|
|
|
|
|
|
|
|
indexes = dict()
|
|
|
|
for i, _id in enumerate(idx):
|
|
|
|
_id = _id.item()
|
|
|
|
if _id in indexes:
|
|
|
|
indexes[_id] += [i]
|
|
|
|
else:
|
|
|
|
indexes[_id] = [i]
|
|
|
|
return [torch.tensor(x, dtype=torch.int64) for x in indexes.values()]
|
|
|
|
|
|
|
|
|
2021-03-16 14:55:31 +00:00
|
|
|
@deprecated(target=_to_onehot, ver_deprecate="1.3.0", ver_remove="1.5.0")
|
2021-03-15 19:28:18 +00:00
|
|
|
def to_onehot(label_tensor: torch.Tensor, num_classes: Optional[int] = None) -> torch.Tensor:
|
2020-12-04 21:42:23 +00:00
|
|
|
"""
|
2021-03-16 14:55:31 +00:00
|
|
|
.. deprecated::
|
|
|
|
Use :func:`torchmetrics.utilities.data.to_onehot`. Will be removed in v1.5.0.
|
|
|
|
"""
|
2020-12-04 21:42:23 +00:00
|
|
|
|
|
|
|
|
2021-03-16 14:55:31 +00:00
|
|
|
@deprecated(target=_select_topk, ver_deprecate="1.3.0", ver_remove="1.5.0")
|
2020-12-07 16:49:35 +00:00
|
|
|
def select_topk(prob_tensor: torch.Tensor, topk: int = 1, dim: int = 1) -> torch.Tensor:
|
|
|
|
"""
|
2021-03-16 14:55:31 +00:00
|
|
|
.. deprecated::
|
|
|
|
Use :func:`torchmetrics.utilities.data.select_topk`. Will be removed in v1.5.0.
|
|
|
|
"""
|
2020-12-07 16:49:35 +00:00
|
|
|
|
|
|
|
|
2021-03-16 14:55:31 +00:00
|
|
|
@deprecated(target=_to_categorical, ver_deprecate="1.3.0", ver_remove="1.5.0")
|
2020-12-07 16:49:35 +00:00
|
|
|
def to_categorical(tensor: torch.Tensor, argmax_dim: int = 1) -> torch.Tensor:
|
2020-12-04 21:42:23 +00:00
|
|
|
"""
|
2021-03-16 14:55:31 +00:00
|
|
|
.. deprecated::
|
|
|
|
Use :func:`torchmetrics.utilities.data.to_categorical`. Will be removed in v1.5.0.
|
|
|
|
"""
|
2020-12-04 21:42:23 +00:00
|
|
|
|
|
|
|
|
2021-03-16 14:55:31 +00:00
|
|
|
@deprecated(target=_get_num_classes, ver_deprecate="1.3.0", ver_remove="1.5.0")
|
2021-03-15 19:28:18 +00:00
|
|
|
def get_num_classes(pred: torch.Tensor, target: torch.Tensor, num_classes: Optional[int] = None) -> int:
|
2020-12-04 21:42:23 +00:00
|
|
|
"""
|
2021-03-16 14:55:31 +00:00
|
|
|
.. deprecated::
|
|
|
|
Use :func:`torchmetrics.utilities.data.get_num_classes`. Will be removed in v1.5.0.
|
|
|
|
"""
|
2020-12-04 21:42:23 +00:00
|
|
|
|
|
|
|
|
2021-03-16 14:55:31 +00:00
|
|
|
@deprecated(target=_reduce, ver_deprecate="1.3.0", ver_remove="1.5.0")
|
2020-12-04 21:42:23 +00:00
|
|
|
def reduce(to_reduce: torch.Tensor, reduction: str) -> torch.Tensor:
|
|
|
|
"""
|
2021-03-16 14:55:31 +00:00
|
|
|
.. deprecated::
|
|
|
|
Use :func:`torchmetrics.utilities.reduce`. Will be removed in v1.5.0.
|
|
|
|
"""
|
2020-12-04 21:42:23 +00:00
|
|
|
|
|
|
|
|
2021-03-16 14:55:31 +00:00
|
|
|
@deprecated(target=_class_reduce, ver_deprecate="1.3.0", ver_remove="1.5.0")
|
2020-12-07 16:49:35 +00:00
|
|
|
def class_reduce(
|
|
|
|
num: torch.Tensor, denom: torch.Tensor, weights: torch.Tensor, class_reduction: str = "none"
|
|
|
|
) -> torch.Tensor:
|
2020-12-04 21:42:23 +00:00
|
|
|
"""
|
2021-03-16 14:55:31 +00:00
|
|
|
.. deprecated::
|
|
|
|
Use :func:`torchmetrics.utilities.class_reduce`. Will be removed in v1.5.0.
|
|
|
|
"""
|