Add evaluation metrics for multilabel task (#123)
* add mean_ap
* add difficult_examples in mAP to support dataset without difficult_examples
* fix docstring
* add CP,CR,CF1,OP,OR,OF1 as multilabel metrics
* fix docstring
* temporary solution to ci until new version of mmcv is avaliable (#127)
* temporary solution to ci until new version of mmcv is avaliable
* temporary solution to ci until new version of mmcv is avaliable
* add mean_ap
* add difficult_examples in mAP to support dataset without difficult_examples
* fix docstring
* add CP,CR,CF1,OP,OR,OF1 as multilabel metrics
* fix docstring
* Swap -1 and 0 for labels
* Revised according to comments
* Revised according to comments
* Revised according to comments
* Revert "Revised according to comments"
It is suggested that we should not include paper from arxiv.
This reverts commit 48a781cd6aca7be40821b55679bebb30295d4336.
* Revert "Revert "Revised according to comments""
This reverts commit 6d3b0f1a7b52afc8ed7a01a28e2911afa4f67c2b.
* Revert "Revised according to comments"
It is suggested we should not cite paper from arxiv.
This reverts commit 120ecda884a43d20a53f375fe1e97938bbd4cffd.
* Revised according to comments
* revised according to comments
* Revised according to comments
2021-01-04 12:25:33 +08:00
|
|
|
import numpy as np
|
|
|
|
import torch
|
|
|
|
|
|
|
|
|
|
|
|
def average_precision(pred, target):
|
2021-07-14 15:06:50 +08:00
|
|
|
r"""Calculate the average precision for a single class.
|
Add evaluation metrics for multilabel task (#123)
* add mean_ap
* add difficult_examples in mAP to support dataset without difficult_examples
* fix docstring
* add CP,CR,CF1,OP,OR,OF1 as multilabel metrics
* fix docstring
* temporary solution to ci until new version of mmcv is avaliable (#127)
* temporary solution to ci until new version of mmcv is avaliable
* temporary solution to ci until new version of mmcv is avaliable
* add mean_ap
* add difficult_examples in mAP to support dataset without difficult_examples
* fix docstring
* add CP,CR,CF1,OP,OR,OF1 as multilabel metrics
* fix docstring
* Swap -1 and 0 for labels
* Revised according to comments
* Revised according to comments
* Revised according to comments
* Revert "Revised according to comments"
It is suggested that we should not include paper from arxiv.
This reverts commit 48a781cd6aca7be40821b55679bebb30295d4336.
* Revert "Revert "Revised according to comments""
This reverts commit 6d3b0f1a7b52afc8ed7a01a28e2911afa4f67c2b.
* Revert "Revised according to comments"
It is suggested we should not cite paper from arxiv.
This reverts commit 120ecda884a43d20a53f375fe1e97938bbd4cffd.
* Revised according to comments
* revised according to comments
* Revised according to comments
2021-01-04 12:25:33 +08:00
|
|
|
|
|
|
|
AP summarizes a precision-recall curve as the weighted mean of maximum
|
|
|
|
precisions obtained for any r'>r, where r is the recall:
|
|
|
|
|
2021-07-14 15:06:50 +08:00
|
|
|
.. math::
|
|
|
|
\text{AP} = \sum_n (R_n - R_{n-1}) P_n
|
Add evaluation metrics for multilabel task (#123)
* add mean_ap
* add difficult_examples in mAP to support dataset without difficult_examples
* fix docstring
* add CP,CR,CF1,OP,OR,OF1 as multilabel metrics
* fix docstring
* temporary solution to ci until new version of mmcv is avaliable (#127)
* temporary solution to ci until new version of mmcv is avaliable
* temporary solution to ci until new version of mmcv is avaliable
* add mean_ap
* add difficult_examples in mAP to support dataset without difficult_examples
* fix docstring
* add CP,CR,CF1,OP,OR,OF1 as multilabel metrics
* fix docstring
* Swap -1 and 0 for labels
* Revised according to comments
* Revised according to comments
* Revised according to comments
* Revert "Revised according to comments"
It is suggested that we should not include paper from arxiv.
This reverts commit 48a781cd6aca7be40821b55679bebb30295d4336.
* Revert "Revert "Revised according to comments""
This reverts commit 6d3b0f1a7b52afc8ed7a01a28e2911afa4f67c2b.
* Revert "Revised according to comments"
It is suggested we should not cite paper from arxiv.
This reverts commit 120ecda884a43d20a53f375fe1e97938bbd4cffd.
* Revised according to comments
* revised according to comments
* Revised according to comments
2021-01-04 12:25:33 +08:00
|
|
|
|
|
|
|
Note that no approximation is involved since the curve is piecewise
|
|
|
|
constant.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
pred (np.ndarray): The model prediction with shape (N, ).
|
|
|
|
target (np.ndarray): The target of each prediction with shape (N, ).
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
float: a single float as average precision value.
|
|
|
|
"""
|
|
|
|
eps = np.finfo(np.float32).eps
|
|
|
|
|
|
|
|
# sort examples
|
|
|
|
sort_inds = np.argsort(-pred)
|
|
|
|
sort_target = target[sort_inds]
|
|
|
|
|
|
|
|
# count true positive examples
|
|
|
|
pos_inds = sort_target == 1
|
|
|
|
tp = np.cumsum(pos_inds)
|
|
|
|
total_pos = tp[-1]
|
|
|
|
|
|
|
|
# count not difficult examples
|
|
|
|
pn_inds = sort_target != -1
|
|
|
|
pn = np.cumsum(pn_inds)
|
|
|
|
|
|
|
|
tp[np.logical_not(pos_inds)] = 0
|
|
|
|
precision = tp / np.maximum(pn, eps)
|
|
|
|
ap = np.sum(precision) / np.maximum(total_pos, eps)
|
|
|
|
return ap
|
|
|
|
|
|
|
|
|
|
|
|
def mAP(pred, target):
|
2021-04-14 21:22:37 +08:00
|
|
|
"""Calculate the mean average precision with respect of classes.
|
Add evaluation metrics for multilabel task (#123)
* add mean_ap
* add difficult_examples in mAP to support dataset without difficult_examples
* fix docstring
* add CP,CR,CF1,OP,OR,OF1 as multilabel metrics
* fix docstring
* temporary solution to ci until new version of mmcv is avaliable (#127)
* temporary solution to ci until new version of mmcv is avaliable
* temporary solution to ci until new version of mmcv is avaliable
* add mean_ap
* add difficult_examples in mAP to support dataset without difficult_examples
* fix docstring
* add CP,CR,CF1,OP,OR,OF1 as multilabel metrics
* fix docstring
* Swap -1 and 0 for labels
* Revised according to comments
* Revised according to comments
* Revised according to comments
* Revert "Revised according to comments"
It is suggested that we should not include paper from arxiv.
This reverts commit 48a781cd6aca7be40821b55679bebb30295d4336.
* Revert "Revert "Revised according to comments""
This reverts commit 6d3b0f1a7b52afc8ed7a01a28e2911afa4f67c2b.
* Revert "Revised according to comments"
It is suggested we should not cite paper from arxiv.
This reverts commit 120ecda884a43d20a53f375fe1e97938bbd4cffd.
* Revised according to comments
* revised according to comments
* Revised according to comments
2021-01-04 12:25:33 +08:00
|
|
|
|
|
|
|
Args:
|
|
|
|
pred (torch.Tensor | np.ndarray): The model prediction with shape
|
|
|
|
(N, C), where C is the number of classes.
|
|
|
|
target (torch.Tensor | np.ndarray): The target of each prediction with
|
|
|
|
shape (N, C), where C is the number of classes. 1 stands for
|
|
|
|
positive examples, 0 stands for negative examples and -1 stands for
|
|
|
|
difficult examples.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
float: A single float as mAP value.
|
|
|
|
"""
|
|
|
|
if isinstance(pred, torch.Tensor) and isinstance(target, torch.Tensor):
|
2021-01-25 18:10:14 +08:00
|
|
|
pred = pred.detach().cpu().numpy()
|
|
|
|
target = target.detach().cpu().numpy()
|
Add evaluation metrics for multilabel task (#123)
* add mean_ap
* add difficult_examples in mAP to support dataset without difficult_examples
* fix docstring
* add CP,CR,CF1,OP,OR,OF1 as multilabel metrics
* fix docstring
* temporary solution to ci until new version of mmcv is avaliable (#127)
* temporary solution to ci until new version of mmcv is avaliable
* temporary solution to ci until new version of mmcv is avaliable
* add mean_ap
* add difficult_examples in mAP to support dataset without difficult_examples
* fix docstring
* add CP,CR,CF1,OP,OR,OF1 as multilabel metrics
* fix docstring
* Swap -1 and 0 for labels
* Revised according to comments
* Revised according to comments
* Revised according to comments
* Revert "Revised according to comments"
It is suggested that we should not include paper from arxiv.
This reverts commit 48a781cd6aca7be40821b55679bebb30295d4336.
* Revert "Revert "Revised according to comments""
This reverts commit 6d3b0f1a7b52afc8ed7a01a28e2911afa4f67c2b.
* Revert "Revised according to comments"
It is suggested we should not cite paper from arxiv.
This reverts commit 120ecda884a43d20a53f375fe1e97938bbd4cffd.
* Revised according to comments
* revised according to comments
* Revised according to comments
2021-01-04 12:25:33 +08:00
|
|
|
elif not (isinstance(pred, np.ndarray) and isinstance(target, np.ndarray)):
|
|
|
|
raise TypeError('pred and target should both be torch.Tensor or'
|
|
|
|
'np.ndarray')
|
|
|
|
|
|
|
|
assert pred.shape == \
|
|
|
|
target.shape, 'pred and target should be in the same shape.'
|
|
|
|
num_classes = pred.shape[1]
|
|
|
|
ap = np.zeros(num_classes)
|
|
|
|
for k in range(num_classes):
|
|
|
|
ap[k] = average_precision(pred[:, k], target[:, k])
|
|
|
|
mean_ap = ap.mean() * 100.0
|
|
|
|
return mean_ap
|