如何获得累积匹配特征 (CMC) 曲线的正确结果

How to get the right results for Cumulative Match Characteristic (CMC) curve

我正在使用它作为我的方程式的基础,但它似乎效果不佳

https://blog.rankone.io/2018/11/01/face-recognition-dictionary/#cmc

我有两个数组,一个是查询数组,一个是画廊数组,形状如下

query_array = [filename, feature] 
gallery_array = [filename, feature]

文件名是图片文件,特征是使用resnet50提取的特征。

结果是这样的,我觉得不对。

[0.8, 0.2, 0.2, 0.4, 0.2, 0.2, 0.4, 0.4, 0.0, 0.6, 0.2, 0.0, 0.4, 0.4, 0.0, 0.4, 0.0, 0.0, 0.0, 0.6, 0.0, 0.0, 0.0, 0.2, 0.0, 0.0, 0.2, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.2, 0.2, 0.0, 0.2, 0.2, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]

我认为随着等级的提高它会接近 100%。 1->10 但事实并非如此。

这是我的代码

# Gallery is a array[image, feature]
# query is a array[image, feature]
def find_cmc(query, gallery):
    print("Calculating CMC")
     # Create rank array to see where a person is positivly id

    correct = np.zeros(len(gallery))
    total_at_i = np.zeros(len(gallery))
    total_compared = 0

    for query_id, query_feature in query:
        # total number of images in the gallery
        dist = []
        # Cacluate the distance between query and each image in the gallery
        for gallery_img, gallery_feature in gallery:
            # Use eucludean distance
            d = np.linalg.norm(query_feature - gallery_feature)
            # Add to the dist array
            dist.append([gallery_img, d])

        # Sort the array by smallest to larget distance [1] index
        dist.sort(key=custom_sort)

        # Now check to see where the positive images are found
        for i in range(0,len(dist)):
            total_compared +=1
            total_at_i[i] += 1
            name,_,_ = get_info(dist[i][0])
            if name == query_id:
                # Increase rank by 1 as there is a match
                correct[i] +=1
    # Get the percentage for each rank@i
    ret_cmc = []
    for i in range(0,len(correct)):
        percent = correct[i]/total_at_i[i]
        ret_cmc.append(percent)

    return ret_cmc

编辑

我一直在研究求和,但我仍然可以按照我认为的那样做对

# Get the percentage for each rank@i
ret_cmc = []
correct_sum = 0
for i in range(0,len(correct)):
    correct_sum += correct[i]
    #percent = correct_sum/total_compared
    percent = correct_sum/total_at_i[i]
    #percent = correct[i]/total_at_i[i]
    ret_cmc.append(percent)

return ret_cmc

编辑

我想我明白了;有人会给我他们的意见吗,因为它似乎工作得很好。

这个的最新代码

def cmc(querys, gallery, topk):
    ret = np.zeros(topk)
    valid_queries = 0
    all_rank = []
    sum_rank = np.zeros(topk)
    for query in querys:
        q_id = query[0]
        q_feature = query[1]
        # Calculate the distances for each query
        distmat = []
        for img, feature in gallery:
            # Get the label from the image
            name,_,_ = get_info(img)
            dist = np.linalg.norm(q_feature - feature)
            distmat.append([name, dist, img])

        # Sort the results for each query
        distmat.sort(key=custom_sort)
        # Find matches
        matches = np.zeros(len(distmat))
        # Zero if no match 1 if match
        for i in range(0, len(distmat)):
            if distmat[i][0] == q_id:
                # Match found
                matches[i] = 1
        rank = np.zeros(topk)
        for i in range(0, topk):
            if matches[i] == 1:
                rank[i] = 1
                # If 1 is found then break as you dont need to look further path k
                break
        all_rank.append(rank)
        valid_queries +=1
    #print(all_rank)
    sum_all_ranks = np.zeros(len(all_rank[0]))
    for i in range(0,len(all_rank)):
        my_array = all_rank[i]
        for g in range(0, len(my_array)):
            sum_all_ranks[g] = sum_all_ranks[g] + my_array[g]
    sum_all_ranks = np.array(sum_all_ranks)
    print("NPSAR", sum_all_ranks)
    cmc_restuls = np.cumsum(sum_all_ranks) / valid_queries
    print(cmc_restuls)
    return cmc_restuls

我对此进行了相当严格的测试,它似乎产生了我认为正确的结果。

def cmc(querys, gallery, topk):
    ret = np.zeros(topk)
    valid_queries = 0
    all_rank = []
    sum_rank = np.zeros(topk)
    for query in querys:
        q_id = query[0]
        q_feature = query[1]
        # Calculate the distances for each query
        distmat = []
        for img, feature in gallery:
            # Get the label from the image
            name,_,_ = get_info(img)
            dist = np.linalg.norm(q_feature - feature)
            distmat.append([name, dist, img])

        # Sort the results for each query
        distmat.sort(key=custom_sort)
        # Find matches
        matches = np.zeros(len(distmat))
        # Zero if no match 1 if match
        for i in range(0, len(distmat)):
            if distmat[i][0] == q_id:
                # Match found
                matches[i] = 1
        rank = np.zeros(topk)
        for i in range(0, topk):
            if matches[i] == 1:
            rank[i] = 1
                # If 1 is found then break as you dont need to look further path k
                break
        all_rank.append(rank)
        valid_queries +=1
    #print(all_rank)
    sum_all_ranks = np.zeros(len(all_rank[0]))
    for i in range(0,len(all_rank)):
        my_array = all_rank[i]
        for g in range(0, len(my_array)):
            sum_all_ranks[g] = sum_all_ranks[g] + my_array[g]
    sum_all_ranks = np.array(sum_all_ranks)
    print("NPSAR", sum_all_ranks)
    cmc_restuls = np.cumsum(sum_all_ranks) / valid_queries
    print(cmc_restuls)
    return cmc_restuls

我知道这是一件很难解决的事情,所以我希望这对我所在位置的其他人有所帮助,因为没有真正好的方法或说明如何实现它。 所以除非