0%

CMC

rank-1,rank-5,mAP

第一种,网上的标准计算公式

https://blog.csdn.net/u013698770/article/details/60776102
https://blog.csdn.net/zkp_987/article/details/79969512
https://blog.csdn.net/kaixinjiuxing666/article/details/81272796

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
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%

第二种:baseline的计算公式

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
def compute_mAP(index, good_index, junk_index):
ap = 0
cmc = torch.IntTensor(len(index)).zero_()
if good_index.size==0: # if empty
cmc[0] = -1
return ap,cmc

# remove junk_index
mask = np.in1d(index, junk_index, invert=True)
index = index[mask]

# find good_index index
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
for i in range(ngood):
d_recall = 1.0/ngood
precision = (i+1)*1.0/(rows_good[i]+1)
if rows_good[i]!=0:
old_precision = i*1.0/rows_good[i]
else:
old_precision=1.0
ap = ap + d_recall*(old_precision + precision)/2

return ap, cmc

CMC = torch.IntTensor(len(gallery_label)).zero_()
ap = 0.0
#print(query_label)
for i in range(len(query_label)):
ap_tmp, CMC_tmp = evaluate(query_feature[i],query_label[i],query_cam[i],gallery_feature,gallery_label,gallery_cam)
if CMC_tmp[0]==-1:
continue
CMC = CMC + CMC_tmp
ap += ap_tmp
print(i, CMC_tmp[0])

CMC = CMC.float()
CMC = CMC/len(query_label) #average CMC
print('top1:%f top5:%f top10:%f mAP:%f'%(CMC[0],CMC[4],CMC[9],ap/len(query_label)))

第二种的简化版本,只计算CMC

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
def evaluate(qf,ql,qc,gf,gl,gc):
"""
qf: list [1,2,3]
ql: 1
qc: 1
gf: list [[1,2,3],[1,2,3]]
gl: [1,2,3]
gc: [1,2,3]
len(gf)==len(gl)==len(gc)
"""
query = qf
score = np.dot(gf,query)
# predict index
index = np.argsort(score) #from small to large # 表示位置,[4,3,1,0,2]
index = index[::-1] # 表示
#index = index[0:2000]
# good index
query_index = np.argwhere(gl==ql) # list [[1],[2]] # 表示位置,即galley中的第几个样本是相同的id
camera_index = np.argwhere(gc==qc) # list [[1],[2]] # 表示位置

good_index = np.setdiff1d(query_index, camera_index, assume_unique=True) # [2,3]表示同一个id不同摄像头的图片的位置
junk_index1 = np.argwhere(gl==-1) # [[1],[2]] 表示id为-1的图片的位置
junk_index2 = np.intersect1d(query_index, camera_index) # [1,2] 表示同一个id同一个摄像头的图片的位置
junk_index = np.append(junk_index2, junk_index1) #.flatten())

CMC_tmp = compute_mAP(index, good_index, junk_index)
return CMC_tmp

def compute_cmc(index, good_index, junk_index):
"""
index: list [4,3,1,0,2],已经排序,数字表示第几张图片
good_index: [3,1] list 位置 数字表示第几张图片
junk_index: [4,2]list 位置 数字表示第几张图片
"""
cmc = torch.IntTensor(len(index)).zero_()
if good_index.size==0: # if empty
cmc[0] = -1
return ap,cmc

# remove junk_index
mask = np.in1d(index, junk_index, invert=True)
index = index[mask] # [3,1,0]

# find good_index index
ngood = len(good_index)
mask = np.in1d(index, good_index) # [t,t,f]
rows_good = np.argwhere(mask==True) #
rows_good = rows_good.flatten() # [0,1]

cmc[rows_good[0]:] = 1

return cmc
def main():
result = scipy.io.loadmat('pytorch_result.mat')
query_feature = result['query_f'] # list [[1,2,3],[1,2,3],[1,2,3]]
query_cam = result['query_cam'][0] # list [1,2,3]
query_label = result['query_label'][0] # list [1,2,3]
gallery_feature = result['gallery_f'] # list [[1,2,3],[1,2,3],[1,2,3]]
gallery_cam = result['gallery_cam'][0] # list [1,2,3]
gallery_label = result['gallery_label'][0] # list [1,2,3]


CMC = torch.IntTensor(len(gallery_label)).zero_()
for i in range(len(query_label)):
CMC_tmp = evaluate(query_feature[i],query_label[i],query_cam[i],gallery_feature,gallery_label,gallery_cam)
if CMC_tmp[0]==-1:
continue
CMC = CMC + CMC_tmp
print(i, CMC_tmp[0])
CMC = CMC.float()
CMC = CMC/len(query_label) #average CMC
print('top1:%f top5:%f top10:%f'%(CMC[0],CMC[4],CMC[9])

def get_id(img_path):
camera_id = []
labels = []
for path, v in img_path:
filename = path.split('/')[-1]
label = filename[0:4]
camera = filename.split('c')[1]
if label[0:2]=='-1':
labels.append(-1)
else:
labels.append(int(label))
camera_id.append(int(camera[0]))
return camera_id, labels

gallery_cam,gallery_label = get_id(gallery_path)
query_cam,query_label = get_id(query_path)