diff --git a/torchreid/metrics/rank_cylib/rank_cy.pyx b/torchreid/metrics/rank_cylib/rank_cy.pyx index 9698b29..b4a8690 100644 --- a/torchreid/metrics/rank_cylib/rank_cy.pyx +++ b/torchreid/metrics/rank_cylib/rank_cy.pyx @@ -2,6 +2,7 @@ from __future__ import print_function import numpy as np +from libc.stdint cimport int64_t, uint64_t import cython @@ -33,41 +34,41 @@ cpdef evaluate_cy(distmat, q_pids, g_pids, q_camids, g_camids, max_rank, use_met return eval_market1501_cy(distmat, q_pids, g_pids, q_camids, g_camids, max_rank) -cpdef eval_cuhk03_cy(float[:,:] distmat, long[:] q_pids, long[:]g_pids, - long[:]q_camids, long[:]g_camids, long max_rank): - - cdef long num_q = distmat.shape[0] - cdef long num_g = distmat.shape[1] +cpdef eval_cuhk03_cy(float[:,:] distmat, int64_t[:] q_pids, int64_t[:]g_pids, + int64_t[:]q_camids, int64_t[:]g_camids, int64_t max_rank): + + cdef int64_t num_q = distmat.shape[0] + cdef int64_t num_g = distmat.shape[1] if num_g < max_rank: max_rank = num_g print('Note: number of gallery samples is quite small, got {}'.format(num_g)) - + cdef: - long num_repeats = 10 - long[:,:] indices = np.argsort(distmat, axis=1) - long[:,:] matches = (np.asarray(g_pids)[np.asarray(indices)] == np.asarray(q_pids)[:, np.newaxis]).astype(np.int64) + int64_t num_repeats = 10 + int64_t[:,:] indices = np.argsort(distmat, axis=1) + int64_t[:,:] matches = (np.asarray(g_pids)[np.asarray(indices)] == np.asarray(q_pids)[:, np.newaxis]).astype(np.int64) float[:,:] all_cmc = np.zeros((num_q, max_rank), dtype=np.float32) float[:] all_AP = np.zeros(num_q, dtype=np.float32) float num_valid_q = 0. # number of valid query - long q_idx, q_pid, q_camid, g_idx - long[:] order = np.zeros(num_g, dtype=np.int64) - long keep + int64_t q_idx, q_pid, q_camid, g_idx + int64_t[:] order = np.zeros(num_g, dtype=np.int64) + int64_t keep float[:] raw_cmc = np.zeros(num_g, dtype=np.float32) # binary vector, positions with value 1 are correct matches float[:] masked_raw_cmc = np.zeros(num_g, dtype=np.float32) float[:] cmc, masked_cmc - long num_g_real, num_g_real_masked, rank_idx, rnd_idx - unsigned long meet_condition + int64_t num_g_real, num_g_real_masked, rank_idx, rnd_idx + uint64_t meet_condition float AP - long[:] kept_g_pids, mask + int64_t[:] kept_g_pids, mask float num_rel float[:] tmp_cmc = np.zeros(num_g, dtype=np.float32) float tmp_cmc_sum - + for q_idx in range(num_q): # get query pid and camid q_pid = q_pids[q_idx] @@ -87,7 +88,7 @@ cpdef eval_cuhk03_cy(float[:,:] distmat, long[:] q_pids, long[:]g_pids, num_g_real += 1 if matches[q_idx][g_idx] > 1e-31: meet_condition = 1 - + if not meet_condition: # this condition is true when query identity does not appear in gallery continue @@ -100,7 +101,7 @@ cpdef eval_cuhk03_cy(float[:,:] distmat, long[:] q_pids, long[:]g_pids, cmc = np.zeros(max_rank, dtype=np.float32) for _ in range(num_repeats): mask = np.zeros(num_g_real, dtype=np.int64) - + for _, idxs in g_pids_dict.items(): # randomly sample one image for each gallery person rnd_idx = np.random.choice(idxs) @@ -121,7 +122,7 @@ cpdef eval_cuhk03_cy(float[:,:] distmat, long[:] q_pids, long[:]g_pids, for rank_idx in range(max_rank): cmc[rank_idx] += masked_cmc[rank_idx] / num_repeats - + for rank_idx in range(max_rank): all_cmc[q_idx, rank_idx] = cmc[rank_idx] # compute average precision @@ -143,7 +144,7 @@ cpdef eval_cuhk03_cy(float[:,:] distmat, long[:] q_pids, long[:]g_pids, for q_idx in range(num_q): avg_cmc[rank_idx] += all_cmc[q_idx, rank_idx] avg_cmc[rank_idx] /= num_valid_q - + cdef float mAP = 0 for q_idx in range(num_q): mAP += all_AP[q_idx] @@ -152,37 +153,37 @@ cpdef eval_cuhk03_cy(float[:,:] distmat, long[:] q_pids, long[:]g_pids, return np.asarray(avg_cmc).astype(np.float32), mAP -cpdef eval_market1501_cy(float[:,:] distmat, long[:] q_pids, long[:]g_pids, - long[:]q_camids, long[:]g_camids, long max_rank): - - cdef long num_q = distmat.shape[0] - cdef long num_g = distmat.shape[1] +cpdef eval_market1501_cy(float[:,:] distmat, int64_t[:] q_pids, int64_t[:]g_pids, + int64_t[:]q_camids, int64_t[:]g_camids, int64_t max_rank): + + cdef int64_t num_q = distmat.shape[0] + cdef int64_t num_g = distmat.shape[1] if num_g < max_rank: max_rank = num_g print('Note: number of gallery samples is quite small, got {}'.format(num_g)) - + cdef: - long[:,:] indices = np.argsort(distmat, axis=1) - long[:,:] matches = (np.asarray(g_pids)[np.asarray(indices)] == np.asarray(q_pids)[:, np.newaxis]).astype(np.int64) + int64_t[:,:] indices = np.argsort(distmat, axis=1) + int64_t[:,:] matches = (np.asarray(g_pids)[np.asarray(indices)] == np.asarray(q_pids)[:, np.newaxis]).astype(np.int64) float[:,:] all_cmc = np.zeros((num_q, max_rank), dtype=np.float32) float[:] all_AP = np.zeros(num_q, dtype=np.float32) float num_valid_q = 0. # number of valid query - long q_idx, q_pid, q_camid, g_idx - long[:] order = np.zeros(num_g, dtype=np.int64) - long keep + int64_t q_idx, q_pid, q_camid, g_idx + int64_t[:] order = np.zeros(num_g, dtype=np.int64) + int64_t keep float[:] raw_cmc = np.zeros(num_g, dtype=np.float32) # binary vector, positions with value 1 are correct matches float[:] cmc = np.zeros(num_g, dtype=np.float32) - long num_g_real, rank_idx - unsigned long meet_condition + int64_t num_g_real, rank_idx + uint64_t meet_condition float num_rel float[:] tmp_cmc = np.zeros(num_g, dtype=np.float32) float tmp_cmc_sum - + for q_idx in range(num_q): # get query pid and camid q_pid = q_pids[q_idx] @@ -193,14 +194,14 @@ cpdef eval_market1501_cy(float[:,:] distmat, long[:] q_pids, long[:]g_pids, order[g_idx] = indices[q_idx, g_idx] num_g_real = 0 meet_condition = 0 - + for g_idx in range(num_g): if (g_pids[order[g_idx]] != q_pid) or (g_camids[order[g_idx]] != q_camid): raw_cmc[num_g_real] = matches[q_idx][g_idx] num_g_real += 1 if matches[q_idx][g_idx] > 1e-31: meet_condition = 1 - + if not meet_condition: # this condition is true when query identity does not appear in gallery continue @@ -233,7 +234,7 @@ cpdef eval_market1501_cy(float[:,:] distmat, long[:] q_pids, long[:]g_pids, for q_idx in range(num_q): avg_cmc[rank_idx] += all_cmc[q_idx, rank_idx] avg_cmc[rank_idx] /= num_valid_q - + cdef float mAP = 0 for q_idx in range(num_q): mAP += all_AP[q_idx] @@ -243,8 +244,8 @@ cpdef eval_market1501_cy(float[:,:] distmat, long[:] q_pids, long[:]g_pids, # Compute the cumulative sum -cdef void function_cumsum(cython.numeric[:] src, cython.numeric[:] dst, long n): - cdef long i +cdef void function_cumsum(cython.numeric[:] src, cython.numeric[:] dst, int64_t n): + cdef int64_t i dst[0] = src[0] for i in range(1, n): dst[i] = src[i] + dst[i - 1] \ No newline at end of file