Safemotion Lib
Loading...
Searching...
No Matches
roc.py
Go to the documentation of this file.
1# encoding: utf-8
2"""
3@author: l1aoxingyu
4@contact: sherlockliao01@gmail.com
5"""
6
7import warnings
8
9import faiss
10import numpy as np
11
12try:
13 from .rank_cylib.roc_cy import evaluate_roc_cy
14
15 IS_CYTHON_AVAI = True
16except ImportError:
17 IS_CYTHON_AVAI = False
18 warnings.warn(
19 'Cython roc evaluation (very fast so highly recommended) is '
20 'unavailable, now use python evaluation.'
21 )
22
23
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.
27
28 Args:
29 distmat (np.ndarray): cosine distance matrix
30 """
31 num_q, num_g = distmat.shape
32 dim = q_feats.shape[1]
33
34 index = faiss.IndexFlatL2(dim)
35 index.add(g_feats)
36
37 _, indices = index.search(q_feats, k=num_g)
38 matches = (g_pids[indices] == q_pids[:, np.newaxis]).astype(np.int32)
39
40 pos = []
41 neg = []
42 for q_idx in range(num_q):
43 # get query pid and camid
44 q_pid = q_pids[q_idx]
45 q_camid = q_camids[q_idx]
46
47 # Remove gallery samples that have the same pid and camid with query
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]
52
53 sort_idx = order[keep]
54
55 q_dist = distmat[q_idx]
56 ind_pos = np.where(raw_cmc == 1)[0]
57 pos.extend(q_dist[sort_idx[ind_pos]])
58
59 ind_neg = np.where(raw_cmc == 0)[0]
60 neg.extend(q_dist[sort_idx[ind_neg]])
61
62 scores = np.hstack((pos, neg))
63
64 labels = np.hstack((np.zeros(len(pos)), np.ones(len(neg))))
65 return scores, labels
66
67
69 distmat,
70 q_feats,
71 g_feats,
72 q_pids,
73 g_pids,
74 q_camids,
75 g_camids,
76 use_cython=True
77):
78 """Evaluates CMC rank.
79 Args:
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.
92 """
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)
95 else:
96 return evaluate_roc_py(distmat, q_feats, g_feats, q_pids, g_pids, q_camids, g_camids)
evaluate_roc_py(distmat, q_feats, g_feats, q_pids, g_pids, q_camids, g_camids)
Definition roc.py:24
evaluate_roc(distmat, q_feats, g_feats, q_pids, g_pids, q_camids, g_camids, use_cython=True)
Definition roc.py:77