一、图像分类性能评估指标(概念介绍)

Top-1 & Top5: 这两个标准主要用于图像分类任务中

Top-1 error rate: 对一张图片,若概率最大的是正确答案,则认为分类正确,否则错误;用 argmax 从网络输出取到的预测 index 与真实 index 的准确率。

Top-5 error rate: 对一张图片,若概率前五的预测中包含正确答案,则认为分类正确,否则错误;

二、问题分析

1. 针对 Top-1 的准确率: 直接通过 argmax 就可以了

import numpy as np
lists = np.array([0.4,0.2,0.3,0.1])
index = np.argmax(lists)
score = lists[index]

2. 针对 Top-N(N>1) 的准确率: 无法使用 argmax 进行解决了,可以考虑利用 Numpy 中的 argsort

np.argmax 的功能就是对 list 从小到大进行排序,最后输出排序过后每个元素本来的下标。

import numpy as np
lists = np.array([0.4,0.2,0.3,0.1])
indexs = np.argsort(lists)
print(indexs)		# [3 1 2 0]

这个怎么应用到 Top-N 计算中呢?其实就很容易了,可以利用 argsort 取得排好序元素的下标,再通过下标找到对应的概率值即可:以 Top-3 指标举例

import numpy as np

lists = np.array([0.4,0.2,0.3,0.1])

def get_top_n(lists,n):
    sort_index = np.argsort(lists)
    n_index = sort_index[-n:]	# 因为是按照概率从大到小取 n 个
    indexs = []
    scores = []
    for index in reversed(n_index):	# 从大到小取,所以通过 reversed() 倒置一下
        indexs.append(index)
        scores.append(lists[index])
    return(indexs,socres)
 
indexs,scores = get_top_n(lists, 3)
print(indexs, scores)	# [0, 2, 1] [0.4, 0.3, 0.2]

本文地址:https://blog.csdn.net/libo1004/article/details/110876316