MCPcopy Create free account
hub / github.com/OpenGVLab/HumanBench / eval_func

Function eval_func

PATH/core/testers/utils/metrics.py:28–87  ·  view source on GitHub ↗

Evaluation with market1501 metric Key: for each query identity, its gallery images from the same camera view are discarded.

(distmat, q_pids, g_pids, q_camids, g_camids, max_rank=50)

Source from the content-addressed store, hash-verified

26
27
28def eval_func(distmat, q_pids, g_pids, q_camids, g_camids, max_rank=50):
29 """Evaluation with market1501 metric
30 Key: for each query identity, its gallery images from the same camera view are discarded.
31 """
32 num_q, num_g = distmat.shape
33 # distmat g
34 # q 1 3 2 4
35 # 4 1 2 3
36 if num_g < max_rank:
37 max_rank = num_g
38 print("Note: number of gallery samples is quite small, got {}".format(num_g))
39 indices = np.argsort(distmat, axis=1)
40 # 0 2 1 3
41 # 1 2 3 0
42 matches = (g_pids[indices] == q_pids[:, np.newaxis]).astype(np.int32)
43 # compute cmc curve for each query
44 all_cmc = []
45 all_AP = []
46 num_valid_q = 0. # number of valid query
47 for q_idx in range(num_q):
48 # get query pid and camid
49 q_pid = q_pids[q_idx]
50 q_camid = q_camids[q_idx]
51
52 # remove gallery samples that have the same pid and camid with query
53 order = indices[q_idx] # select one row
54 remove = (g_pids[order] == q_pid) & (g_camids[order] == q_camid)
55 keep = np.invert(remove)
56
57 # compute cmc curve
58 # binary vector, positions with value 1 are correct matches
59 orig_cmc = matches[q_idx][keep]
60 if not np.any(orig_cmc):
61 # this condition is true when query identity does not appear in gallery
62 continue
63
64 cmc = orig_cmc.cumsum()
65 cmc[cmc > 1] = 1
66
67 all_cmc.append(cmc[:max_rank])
68 num_valid_q += 1.
69
70 # compute average precision
71 # reference: https://en.wikipedia.org/wiki/Evaluation_measures_(information_retrieval)#Average_precision
72 num_rel = orig_cmc.sum()
73 tmp_cmc = orig_cmc.cumsum()
74 #tmp_cmc = [x / (i + 1.) for i, x in enumerate(tmp_cmc)]
75 y = np.arange(1, tmp_cmc.shape[0] + 1) * 1.0
76 tmp_cmc = tmp_cmc / y
77 tmp_cmc = np.asarray(tmp_cmc) * orig_cmc
78 AP = tmp_cmc.sum() / num_rel
79 all_AP.append(AP)
80
81 assert num_valid_q > 0, "Error: all query identities do not appear in gallery"
82
83 all_cmc = np.asarray(all_cmc).astype(np.float32)
84 all_cmc = all_cmc.sum(0) / num_valid_q
85 mAP = np.mean(all_AP)

Callers 1

computeMethod · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected