0
0
Computer Visionml~20 mins

Top-K accuracy in Computer Vision - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Top-K Accuracy Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
1:30remaining
Understanding Top-K Accuracy Concept

Imagine you have a model that predicts the top 3 possible labels for an image. The true label is among these 3 predictions. What does this mean about the model's Top-3 accuracy?

AThe model's Top-3 accuracy counts this prediction as correct because the true label is in the top 3 guesses.
BThe model's Top-3 accuracy counts this prediction as incorrect because only the top 1 prediction matters.
CThe model's Top-3 accuracy ignores this prediction since it only considers the top 5 guesses.
DThe model's Top-3 accuracy counts this prediction as correct only if the true label is the first guess.
Attempts:
2 left
💡 Hint

Top-K accuracy checks if the true label is within the top K predictions, not just the first one.

Predict Output
intermediate
2:00remaining
Output of Top-2 Accuracy Calculation

Given the following predictions and true labels, what is the Top-2 accuracy?

predictions = [[0.1, 0.7, 0.2], [0.6, 0.3, 0.1], [0.2, 0.2, 0.6]]
true_labels = [1, 0, 2]

Each inner list shows predicted probabilities for classes 0, 1, and 2.

Computer Vision
import numpy as np

def top_k_accuracy(preds, labels, k):
    correct = 0
    for pred, label in zip(preds, labels):
        top_k = np.argsort(pred)[-k:][::-1]
        if label in top_k:
            correct += 1
    return correct / len(labels)

predictions = [[0.1, 0.7, 0.2], [0.6, 0.3, 0.1], [0.2, 0.2, 0.6]]
true_labels = [1, 0, 2]

result = top_k_accuracy(predictions, true_labels, 2)
print(result)
A1.0
B0.0
C0.33
D0.67
Attempts:
2 left
💡 Hint

Check if each true label is in the top 2 predicted classes by probability.

Model Choice
advanced
2:00remaining
Choosing Model for High Top-5 Accuracy

You want a model that performs well on Top-5 accuracy for a dataset with 100 classes. Which model architecture is best suited for this goal?

AA model that outputs only the top predicted class without probabilities.
BA model with a single sigmoid output for binary classification.
CA model with a softmax output layer producing probabilities for all 100 classes.
DA model that uses regression to predict continuous values instead of classes.
Attempts:
2 left
💡 Hint

Top-K accuracy requires ranking multiple class probabilities.

Hyperparameter
advanced
1:30remaining
Effect of Increasing K in Top-K Accuracy

If you increase K in Top-K accuracy from 1 to 10, what is the expected effect on the accuracy metric?

ATop-K accuracy will remain exactly the same regardless of K.
BTop-K accuracy will decrease because the model becomes less confident.
CTop-K accuracy will become meaningless and always zero.
DTop-K accuracy will generally increase or stay the same because more predictions are considered correct.
Attempts:
2 left
💡 Hint

Think about how including more guesses affects the chance of the true label being in the top K.

Metrics
expert
2:30remaining
Calculating Top-3 Accuracy from Model Outputs

Given the following model output logits and true labels, what is the Top-3 accuracy?

logits = [[2.0, 1.0, 0.1, 0.5], [0.1, 0.2, 3.0, 0.4], [1.0, 2.5, 0.3, 0.2]]
true_labels = [0, 2, 1]

Use softmax to convert logits to probabilities before selecting top predictions.

Computer Vision
import numpy as np

def softmax(x):
    e_x = np.exp(x - np.max(x))
    return e_x / e_x.sum()

def top_k_accuracy_from_logits(logits, labels, k):
    correct = 0
    for logit, label in zip(logits, labels):
        probs = softmax(logit)
        top_k = np.argsort(probs)[-k:][::-1]
        if label in top_k:
            correct += 1
    return correct / len(labels)

logits = [[2.0, 1.0, 0.1, 0.5], [0.1, 0.2, 3.0, 0.4], [1.0, 2.5, 0.3, 0.2]]
true_labels = [0, 2, 1]

result = top_k_accuracy_from_logits(logits, true_labels, 3)
print(round(result, 2))
A0.67
B1.0
C0.33
D0.0
Attempts:
2 left
💡 Hint

Apply softmax to logits, then check if true label is in top 3 probabilities.