0
0
Computer Visionml~5 mins

Top-K accuracy in Computer Vision

Choose your learning style9 modes available
Introduction

Top-K accuracy helps us check if the right answer is among the top K guesses of a model, not just the very top guess.

When you want to see if the correct label is within the top 3 or 5 guesses of an image classifier.
When exact single prediction is hard, but a few good guesses are useful, like in photo tagging apps.
When evaluating models on tasks with many classes, like recognizing many types of animals.
When you want to compare models by how often they include the right answer in their top choices.
Syntax
Computer Vision
top_k_accuracy_score(y_true, y_pred_probs, k=5)

# y_true: true labels
# y_pred_probs: predicted probabilities for each class
# k: number of top guesses to consider

The function checks if the true label is in the top K predicted classes.

Higher K means easier to get correct, but less strict accuracy.

Examples
Checks if the top guess matches the true label exactly.
Computer Vision
top_k_accuracy_score([2, 0, 1], [[0.1, 0.2, 0.7], [0.8, 0.1, 0.1], [0.2, 0.6, 0.2]], k=1)
Checks if the true label is in the top 2 guesses.
Computer Vision
top_k_accuracy_score([2, 0, 1], [[0.1, 0.2, 0.7], [0.8, 0.1, 0.1], [0.2, 0.6, 0.2]], k=2)
Sample Model

This program shows how often the model's top guess is correct (top-1) and how often the correct label is in the top 2 guesses (top-2).

Computer Vision
from sklearn.metrics import top_k_accuracy_score

# True labels for 4 images
y_true = [0, 1, 2, 3]

# Predicted probabilities for 4 classes
# Each inner list sums to 1 and shows model confidence
y_pred_probs = [
    [0.7, 0.1, 0.1, 0.1],  # Correct class 0 is top guess
    [0.2, 0.3, 0.4, 0.1],  # Correct class 1 is second guess
    [0.1, 0.2, 0.5, 0.2],  # Correct class 2 is top guess
    [0.1, 0.1, 0.2, 0.6]   # Correct class 3 is top guess
]

# Calculate top-1 accuracy (exact match)
top1 = top_k_accuracy_score(y_true, y_pred_probs, k=1)

# Calculate top-2 accuracy (correct label in top 2 guesses)
top2 = top_k_accuracy_score(y_true, y_pred_probs, k=2)

print(f"Top-1 accuracy: {top1:.2f}")
print(f"Top-2 accuracy: {top2:.2f}")
OutputSuccess
Important Notes

Top-K accuracy is useful when multiple guesses are acceptable.

Choosing K depends on your task and how many guesses you want to consider.

Top-K accuracy is common in image recognition challenges with many classes.

Summary

Top-K accuracy checks if the true label is within the top K model guesses.

It helps measure model performance beyond just the single best guess.

Use it when multiple possible answers are useful or when classes are many.