其他分享
首页 > 其他分享> > reid领域中cmc评估方式的代码实现

reid领域中cmc评估方式的代码实现

作者:互联网

def evaluate(distmat, q_pids, g_pids, q_camids, g_camids, max_rank=50):

1.首先参数分别为dismat矩阵维度为(probe,gallery);query; gallery; query_camids; g_camids

    num_q, num_g = distmat.shape
    if num_g < max_rank:
        max_rank = num_g
        print("Note: number of gallery samples is quite small, got {}".format(num_g))

2.检测输入的gallery数目是否大于排序所需的数目

 indices = np.argsort(distmat, axis=1)   
    print(indices.shape)
    matches = (g_pids[indices] == q_pids[:, np.newaxis]).astype(np.int32)   
    print(matches.shape)

3.将dismat矩阵的行方向按照从小到大排列,并且输出对应的索引到indices;gallery按索引排序后与probe做对比,相同的身份为1,否则为0。(经过排序后一般相同的身份都会出现在前面几位)

    all_cmc = []
    all_AP = []
    num_valid_q = 0.
    for q_idx in range(num_q):
        # get query pid and camid
        q_pid = q_pids[q_idx]
        q_camid = q_camids[q_idx]

        # remove gallery samples that have the same pid and camid with query
        order = indices[q_idx]
        remove = (g_pids[order] == q_pid) & (g_camids[order] == q_camid)
        keep = np.invert(remove)    

        # compute cmc curve
        orig_cmc = matches[q_idx][keep] # binary vector, positions with value 1 are correct matches
        if not np.any(orig_cmc):    
            # this condition is true when query identity does not appear in gallery
            continue

        cmc = orig_cmc.cumsum()
        cmc[cmc > 1] = 1

        all_cmc.append(cmc[:max_rank])
        num_valid_q += 1.

4.建立空列表cmc和AP:对于每一个query提取出ID和camid

5.提取出dismat矩阵query对应的行,每一行是gallery中按相似度由大到小排列

6.与(&):将gallery中与probe在相同镜头下出现的positive ID挑出, 返回bool值,并反转(就是让应该remove的位置置为false,保留的位置置为True)

7.在match矩阵中只保留不在相同camid下的same ID和其他干扰项,赋给orig_cmc

8.np.any 是检测gallery中是否有正确的匹配项,要是没有就跳出计算下一个probe

9..cumsum 对一维数组进行加和,保留

all_cmc = np.asarray(all_cmc).astype(np.float32)

10.all_cmc[] 列表中为1的位置就是top-k中的正确匹配。

标签:代码,cmc,reid,num,np,query,pids,gallery
来源: https://blog.csdn.net/qq_36819661/article/details/120437615