"Target Detection" Top-N Index Calculation Method

1. Image classification performance evaluation index (concept introduction)

Top-1 & Top5: These two standards are mainly used in image classification tasks

Top-1 error rate: For a picture, if the highest probability is the correct answer, the classification is considered correct, otherwise it is wrong; the accuracy rate of the predicted index and the real index obtained from the network output with argmax.

Top-5 error rate: For a picture, if the correct answer is included in the top five probability predictions, the classification is considered correct, otherwise it is wrong;

2. Problem analysis

1. Accuracy rate for Top-1: just pass argmax directly

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

2. Accuracy rate for Top-N (N>1): Can not be solved with argmax, you can consider using argsort in Numpy

The function of np.argmax is to sort the list from small to large, and finally output the original subscript of each element after sorting.

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]

How does this apply to Top-N calculations? In fact, it is very easy. You can use argsort to get the subscripts of the sorted elements, and then find the corresponding probability value through the subscripts: Take the Top-3 indicator as an example

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]

Guess you like

Origin blog.csdn.net/libo1004/article/details/110876316