Python:rank-1 rank-5 top1 top5 mAP

数据集:[0,1,2,3,4,5,6,7,8,9]
目标集:[1,3,5,7,9]
三次预测结果分别为(按照预测所得概率从大到小排序):
[8,9,4,7,5,6,3,2,1,0],可以看出在第2、4、5、7、9次预测正确
[9,6,7,2,1,4,3,5,8,0],可以看出在第1、3、5、7、8次预测正确
[1,9,5,3,7,2,4,6,8,0],可以看出在第1、2、3、4、5次预测正确

import numpy as np

indexxx = np.array([[8,9,4,7,5,6,3,2,1,0],[9,6,7,2,1,4,3,5,8,0],[7,9,5,3,1,2,4,6,8,0]])
good_index = np.array([1,3,5,7,9])
CMC = np.array([0,0,0,0,0,0,0,0,0,0])
mAP = 0.0

for i in indexxx:
    cmc = np.array([0,0,0,0,0,0,0,0,0,0])
    index = i
    ngood = len(good_index)
    mask = np.in1d(index, good_index)
    rows_good = np.argwhere(mask==True)
    rows_good = rows_good.flatten()
    cmc[rows_good[0]:] = 1
    print('cmc:',cmc)
    CMC += cmc

    ap = 0.0
    for i in range(ngood):
        d_recall = 1.0/ngood
        precision = (i+1)*1.0/(rows_good[i]+1)
        ap = ap + d_recall*precision
    print('ap:{:.2f}%:'.format(100*ap))
    mAP += ap

CMC = CMC/3
mAP = mAP/3
print('top1:{:.2f}%  top5:{:.2f}%  mAP:{:.2f}%'.format(100*CMC[0],100*CMC[4],100*mAP))
输出结果:
cmc: [0 1 1 1 1 1 1 1 1 1]
ap:54.54%
cmc: [1 1 1 1 1 1 1 1 1 1]
ap:69.26%
cmc: [1 1 1 1 1 1 1 1 1 1]
ap:100.00%
top1:66.67%  top5:100.00%  mAP:74.60%

猜你喜欢

转载自blog.csdn.net/kaixinjiuxing666/article/details/81272796
今日推荐