2020-09-30 19:00:20 +08:00
|
|
|
import numpy as np
|
|
|
|
import torch
|
2020-07-07 19:32:06 +08:00
|
|
|
import torch.nn as nn
|
|
|
|
|
|
|
|
|
2021-01-25 17:54:22 +08:00
|
|
|
def accuracy_numpy(pred, target, topk=1, thrs=None):
|
|
|
|
if thrs is None:
|
|
|
|
thrs = 0.0
|
|
|
|
if isinstance(thrs, float):
|
|
|
|
thrs = (thrs, )
|
|
|
|
res_single = True
|
|
|
|
elif isinstance(thrs, tuple):
|
|
|
|
res_single = False
|
|
|
|
else:
|
|
|
|
raise TypeError(
|
|
|
|
f'thrs should be float or tuple, but got {type(thrs)}.')
|
|
|
|
|
2020-09-30 19:00:20 +08:00
|
|
|
res = []
|
|
|
|
maxk = max(topk)
|
|
|
|
num = pred.shape[0]
|
|
|
|
pred_label = pred.argsort(axis=1)[:, -maxk:][:, ::-1]
|
2021-01-25 17:54:22 +08:00
|
|
|
pred_score = np.sort(pred, axis=1)[:, -maxk:][:, ::-1]
|
2020-09-30 19:00:20 +08:00
|
|
|
|
|
|
|
for k in topk:
|
2021-01-25 17:54:22 +08:00
|
|
|
correct_k = pred_label[:, :k] == target.reshape(-1, 1)
|
|
|
|
res_thr = []
|
|
|
|
for thr in thrs:
|
|
|
|
# Only prediction values larger than thr are counted as correct
|
|
|
|
_correct_k = correct_k & (pred_score[:, :k] > thr)
|
|
|
|
_correct_k = np.logical_or.reduce(_correct_k, axis=1)
|
|
|
|
res_thr.append(_correct_k.sum() * 100. / num)
|
|
|
|
if res_single:
|
|
|
|
res.append(res_thr[0])
|
|
|
|
else:
|
|
|
|
res.append(res_thr)
|
2020-09-30 19:00:20 +08:00
|
|
|
return res
|
|
|
|
|
|
|
|
|
2021-01-25 17:54:22 +08:00
|
|
|
def accuracy_torch(pred, target, topk=1, thrs=None):
|
|
|
|
if thrs is None:
|
|
|
|
thrs = 0.0
|
|
|
|
if isinstance(thrs, float):
|
|
|
|
thrs = (thrs, )
|
|
|
|
res_single = True
|
|
|
|
elif isinstance(thrs, tuple):
|
|
|
|
res_single = False
|
|
|
|
else:
|
|
|
|
raise TypeError(
|
|
|
|
f'thrs should be float or tuple, but got {type(thrs)}.')
|
|
|
|
|
2020-09-30 19:00:20 +08:00
|
|
|
res = []
|
|
|
|
maxk = max(topk)
|
|
|
|
num = pred.size(0)
|
2021-01-25 17:54:22 +08:00
|
|
|
pred_score, pred_label = pred.topk(maxk, dim=1)
|
2020-09-30 19:00:20 +08:00
|
|
|
pred_label = pred_label.t()
|
|
|
|
correct = pred_label.eq(target.view(1, -1).expand_as(pred_label))
|
|
|
|
for k in topk:
|
2021-01-25 17:54:22 +08:00
|
|
|
res_thr = []
|
|
|
|
for thr in thrs:
|
|
|
|
# Only prediction values larger than thr are counted as correct
|
|
|
|
_correct = correct & (pred_score.t() > thr)
|
|
|
|
correct_k = _correct[:k].reshape(-1).float().sum(0, keepdim=True)
|
|
|
|
res_thr.append(correct_k.mul_(100. / num))
|
|
|
|
if res_single:
|
|
|
|
res.append(res_thr[0])
|
|
|
|
else:
|
|
|
|
res.append(res_thr)
|
2020-09-30 19:00:20 +08:00
|
|
|
return res
|
|
|
|
|
|
|
|
|
2021-01-25 17:54:22 +08:00
|
|
|
def accuracy(pred, target, topk=1, thrs=None):
|
2021-04-14 21:22:37 +08:00
|
|
|
"""Calculate accuracy according to the prediction and target.
|
2020-07-07 19:32:06 +08:00
|
|
|
|
|
|
|
Args:
|
2020-09-30 19:00:20 +08:00
|
|
|
pred (torch.Tensor | np.array): The model prediction.
|
|
|
|
target (torch.Tensor | np.array): The target of each prediction
|
2021-01-14 11:09:08 +08:00
|
|
|
topk (int | tuple[int]): If the predictions in ``topk``
|
2020-07-07 19:32:06 +08:00
|
|
|
matches the target, the predictions will be regarded as
|
|
|
|
correct ones. Defaults to 1.
|
2021-01-25 17:54:22 +08:00
|
|
|
thrs (float, optional): thrs (float | tuple[float], optional):
|
|
|
|
Predictions with scores under the thresholds are considered
|
|
|
|
negative. Default to None.
|
2020-07-07 19:32:06 +08:00
|
|
|
|
|
|
|
Returns:
|
2021-07-14 15:06:50 +08:00
|
|
|
float | list[float] | list[list[float]]: Accuracy
|
|
|
|
- float: If both ``topk`` and ``thrs`` is a single value.
|
|
|
|
- list[float]: If one of ``topk`` or ``thrs`` is a tuple.
|
|
|
|
- list[list[float]]: If both ``topk`` and ``thrs`` is a tuple. \
|
|
|
|
And the first dim is ``topk``, the second dim is ``thrs``.
|
2020-07-07 19:32:06 +08:00
|
|
|
"""
|
|
|
|
assert isinstance(topk, (int, tuple))
|
|
|
|
if isinstance(topk, int):
|
|
|
|
topk = (topk, )
|
|
|
|
return_single = True
|
|
|
|
else:
|
|
|
|
return_single = False
|
|
|
|
|
2020-09-30 19:00:20 +08:00
|
|
|
if isinstance(pred, torch.Tensor) and isinstance(target, torch.Tensor):
|
2021-01-25 17:54:22 +08:00
|
|
|
res = accuracy_torch(pred, target, topk, thrs)
|
2020-09-30 19:00:20 +08:00
|
|
|
elif isinstance(pred, np.ndarray) and isinstance(target, np.ndarray):
|
2021-01-25 17:54:22 +08:00
|
|
|
res = accuracy_numpy(pred, target, topk, thrs)
|
2020-09-30 19:00:20 +08:00
|
|
|
else:
|
2021-01-25 17:54:22 +08:00
|
|
|
raise TypeError(
|
|
|
|
f'pred and target should both be torch.Tensor or np.ndarray, '
|
|
|
|
f'but got {type(pred)} and {type(target)}.')
|
2020-07-07 19:32:06 +08:00
|
|
|
|
|
|
|
return res[0] if return_single else res
|
|
|
|
|
|
|
|
|
|
|
|
class Accuracy(nn.Module):
|
|
|
|
|
|
|
|
def __init__(self, topk=(1, )):
|
2021-04-14 21:22:37 +08:00
|
|
|
"""Module to calculate the accuracy.
|
2020-07-07 19:32:06 +08:00
|
|
|
|
|
|
|
Args:
|
2021-01-14 11:09:08 +08:00
|
|
|
topk (tuple): The criterion used to calculate the
|
2020-07-07 19:32:06 +08:00
|
|
|
accuracy. Defaults to (1,).
|
|
|
|
"""
|
|
|
|
super().__init__()
|
|
|
|
self.topk = topk
|
|
|
|
|
|
|
|
def forward(self, pred, target):
|
2021-04-14 21:22:37 +08:00
|
|
|
"""Forward function to calculate accuracy.
|
2020-07-07 19:32:06 +08:00
|
|
|
|
|
|
|
Args:
|
|
|
|
pred (torch.Tensor): Prediction of models.
|
|
|
|
target (torch.Tensor): Target for each prediction.
|
|
|
|
|
|
|
|
Returns:
|
2021-01-25 17:54:22 +08:00
|
|
|
list[float]: The accuracies under different topk criterions.
|
2020-07-07 19:32:06 +08:00
|
|
|
"""
|
|
|
|
return accuracy(pred, target, self.topk)
|