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

时间:2019-07-09 06:09:48

标签: python numpy ranking rank

我已经尝试了一段时间,并且越来越接近答案了,但仍然不尽然。我正在使用它作为我的方程式的基础,但似乎效果不佳

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

我有两个数组,它们的查询和图库数组的形状如下:

query_array = [文件名,功能] gallery_array = [文件名,功能]

文件名是图像文件,并且该功能是使用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

1 个答案:

答案 0 :(得分:0)

我对此进行了非常艰苦的测试,似乎得出了我认为正确的结论。

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

我知道这是一件很难解决的事情,所以我希望这对我有帮助的人有所帮助,因为没有真正好的方法或方法来实现这一目标。 所以除非