24def evaluate_roc_py(distmat, q_feats, g_feats, q_pids, g_pids, q_camids, g_camids):
25 r"""Evaluation with ROC curve.
26 Key: for each query identity, its gallery images from the same camera view are discarded.
29 distmat (np.ndarray): cosine distance matrix
31 num_q, num_g = distmat.shape
32 dim = q_feats.shape[1]
34 index = faiss.IndexFlatL2(dim)
37 _, indices = index.search(q_feats, k=num_g)
38 matches = (g_pids[indices] == q_pids[:, np.newaxis]).astype(np.int32)
42 for q_idx
in range(num_q):
45 q_camid = q_camids[q_idx]
48 order = indices[q_idx]
49 remove = (g_pids[order] == q_pid) & (g_camids[order] == q_camid)
50 keep = np.invert(remove)
51 raw_cmc = matches[q_idx][keep]
53 sort_idx = order[keep]
55 q_dist = distmat[q_idx]
56 ind_pos = np.where(raw_cmc == 1)[0]
57 pos.extend(q_dist[sort_idx[ind_pos]])
59 ind_neg = np.where(raw_cmc == 0)[0]
60 neg.extend(q_dist[sort_idx[ind_neg]])
62 scores = np.hstack((pos, neg))
64 labels = np.hstack((np.zeros(len(pos)), np.ones(len(neg))))
78 """Evaluates CMC rank.
80 distmat (numpy.ndarray): distance matrix of shape (num_query, num_gallery).
81 q_pids (numpy.ndarray): 1-D array containing person identities
82 of each query instance.
83 g_pids (numpy.ndarray): 1-D array containing person identities
84 of each gallery instance.
85 q_camids (numpy.ndarray): 1-D array containing camera views under
86 which each query instance is captured.
87 g_camids (numpy.ndarray): 1-D array containing camera views under
88 which each gallery instance is captured.
89 use_cython (bool, optional): use cython code for evaluation. Default is True.
90 This is highly recommended as the cython code can speed up the cmc computation
91 by more than 10x. This requires Cython to be installed.
93 if use_cython
and IS_CYTHON_AVAI:
94 return evaluate_roc_cy(distmat, q_feats, g_feats, q_pids, g_pids, q_camids, g_camids)
96 return evaluate_roc_py(distmat, q_feats, g_feats, q_pids, g_pids, q_camids, g_camids)
evaluate_roc(distmat, q_feats, g_feats, q_pids, g_pids, q_camids, g_camids, use_cython=True)