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