1. 范围
图像的多标签分类中,需要用二元交叉熵损失函数,同时评价标准中用map(mean average precision),计算map则要计算ap,AP衡量的是学出来的模型在每个类别上的好坏,mAP衡量的是学出的模型在所有类别上的好坏,得到AP后mAP的计算就变得很简单了,就是取所有AP的平均值。
2. AP的计算
可以参考
可以首先看一下以上的两个链接,了解一些概念。其实关键就在第二篇的这句:
precision = cumsum(gt) / index
。如下表:
这就是求的在一个类别id上的AP,然后求出每个类别id上的AP,最后求一个平均就是map了。
3. 实例
这是github项目SIXray中的源码。求AP的
# sort examples
sorted, indices = torch.sort(output, dim=0, descending=True)
# Computes prec@i
pos_count = 0.000000001
total_count = 0.
precision_at_i = 0.
for i in indices:
label = target[i]
if difficult_examples and label == 0:
continue
if label == 1:
pos_count += 1
total_count += 1
if label == 1:
precision_at_i += pos_count / total_count
precision_at_i /= pos_count
主要就是一个sort,然后是求precision = cumsum(gt) / index
,最后precision /= pos_count
,就是AP了。其他类别的AP,以此类推,最后求平均就是map。