Bird
Raised Fist0
Computer Visionml~20 mins

Top-K accuracy in Computer Vision - ML Experiment: Train & Evaluate

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Experiment - Top-K accuracy
Problem:You are training a computer vision model to classify images into 10 categories. The current model achieves 85% accuracy on the training set but only 70% accuracy on the validation set. You want to better understand how often the correct label is among the top 3 predictions, not just the top 1.
Current Metrics:Training accuracy: 85%, Validation accuracy: 70%, Top-1 accuracy only
Issue:The model's top-1 accuracy on validation is low, and you want to evaluate top-K accuracy (top-3) to see if the model predicts the correct label within its top 3 guesses.
Your Task
Calculate and report the top-3 accuracy on the validation set to better understand model performance beyond top-1 accuracy.
Use the existing trained model without retraining.
Do not change the model architecture.
Use only the validation dataset for this evaluation.
Hint 1
Hint 2
Hint 3
Solution
Computer Vision
import tensorflow as tf
from tensorflow.keras.datasets import cifar10
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Conv2D, MaxPooling2D, Flatten, Dense
from tensorflow.keras.utils import to_categorical

# Load data
(x_train, y_train), (x_test, y_test) = cifar10.load_data()

# Normalize images
x_train, x_test = x_train / 255.0, x_test / 255.0

# Convert labels to categorical
num_classes = 10
y_train_cat = to_categorical(y_train, num_classes)
y_test_cat = to_categorical(y_test, num_classes)

# Define a simple CNN model
model = Sequential([
    Conv2D(32, (3,3), activation='relu', input_shape=(32,32,3)),
    MaxPooling2D((2,2)),
    Conv2D(64, (3,3), activation='relu'),
    MaxPooling2D((2,2)),
    Flatten(),
    Dense(64, activation='relu'),
    Dense(num_classes, activation='softmax')
])

model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])

# Train model briefly for demonstration
model.fit(x_train, y_train_cat, epochs=3, batch_size=64, validation_split=0.1, verbose=0)

# Evaluate top-1 accuracy
loss, top1_acc = model.evaluate(x_test, y_test_cat, verbose=0)

# Calculate top-3 accuracy
# Use tf.keras.metrics.TopKCategoricalAccuracy
top3_acc_metric = tf.keras.metrics.TopKCategoricalAccuracy(k=3)
top3_acc_metric.update_state(y_test_cat, model.predict(x_test, verbose=0))
top3_acc = top3_acc_metric.result().numpy()

print(f"Top-1 accuracy on test set: {top1_acc*100:.2f}%")
print(f"Top-3 accuracy on test set: {top3_acc*100:.2f}%")
Added calculation of top-3 accuracy using TensorFlow's TopKCategoricalAccuracy metric.
Kept the original model and dataset unchanged.
Reported both top-1 and top-3 accuracy for comparison.
Results Interpretation

Before: Only top-1 accuracy was reported at 70.5%.
After: Top-3 accuracy is 89.3%, showing the model predicts the correct label within its top 3 guesses much more often.

Top-K accuracy helps us understand model performance beyond just the single best guess. It is useful in applications where having the correct answer in the top few predictions is acceptable.
Bonus Experiment
Try calculating top-5 accuracy and compare it with top-1 and top-3 accuracies.
💡 Hint
Use the same TopKCategoricalAccuracy metric with k=5 and observe how accuracy improves as k increases.

Practice

(1/5)
1. What does Top-K accuracy measure in a classification model?
easy
A. If the true label is among the top K predicted labels
B. The accuracy of the model's single best prediction only
C. The time taken to make K predictions
D. The number of classes in the dataset

Solution

  1. Step 1: Understand the definition of Top-K accuracy

    Top-K accuracy checks if the correct label is within the top K guesses made by the model, not just the top 1.
  2. Step 2: Compare options with the definition

    Only If the true label is among the top K predicted labels correctly states that Top-K accuracy measures if the true label is in the top K predictions.
  3. Final Answer:

    If the true label is among the top K predicted labels -> Option A
  4. Quick Check:

    Top-K accuracy = True label in top K predictions [OK]
Hint: Top-K means checking top K guesses, not just one [OK]
Common Mistakes:
  • Confusing Top-K accuracy with single-label accuracy
  • Thinking it measures prediction speed
  • Assuming it counts total classes
2. Which of the following is the correct way to compute Top-3 accuracy using PyTorch's topk method on model outputs outputs and true labels labels?
easy
A. pred = outputs.max(3); correct = pred.eq(labels).sum().item()
B. correct = outputs.topk(3).eq(labels).sum().item()
C. _, pred = outputs.topk(3, dim=1); correct = pred.eq(labels.view(-1,1)).sum().item()
D. _, pred = outputs.topk(1, dim=0); correct = pred.eq(labels).sum().item()

Solution

  1. Step 1: Understand PyTorch topk usage

    The topk(k, dim=1) returns top k values and indices along dimension 1 (classes). We want indices for predictions.
  2. Step 2: Match predictions with labels

    Reshape labels to (-1,1) to compare with top-k predictions and count matches with eq and sum.
  3. Final Answer:

    _, pred = outputs.topk(3, dim=1); correct = pred.eq(labels.view(-1,1)).sum().item() -> Option C
  4. Quick Check:

    Use topk with dim=1 and compare with labels reshaped [OK]
Hint: Use topk(dim=1) and reshape labels for comparison [OK]
Common Mistakes:
  • Using max instead of topk for multiple predictions
  • Wrong dimension in topk call
  • Not reshaping labels for comparison
3. Given the following PyTorch code snippet, what is the printed Top-2 accuracy count?
import torch
outputs = torch.tensor([[0.1, 0.8, 0.05, 0.05],
                        [0.4, 0.3, 0.2, 0.1],
                        [0.25, 0.25, 0.25, 0.25]])
labels = torch.tensor([1, 2, 3])
_, pred = outputs.topk(2, dim=1)
correct = pred.eq(labels.view(-1,1)).sum().item()
print(correct)
medium
A. 2
B. 1
C. 3
D. 0

Solution

  1. Step 1: Identify top 2 predictions per sample

    For each row: - Row 1: top 2 indices are [1, 0] (0.8, 0.1) - Row 2: top 2 indices are [0, 1] (0.4, 0.3) - Row 3: top 2 indices are [0, 1] (both 0.25, tie broken by index)
  2. Step 2: Check if true label is in top 2 predictions

    Labels are [1, 2, 3]: - Sample 1: label 1 in [1,0] -> yes - Sample 2: label 2 in [0,1] -> no - Sample 3: label 3 in [0,1] -> no
  3. Final Answer:

    1 -> Option B
  4. Quick Check:

    Only one label in top 2 predictions [OK]
Hint: Check top K indices and compare with labels one by one [OK]
Common Mistakes:
  • Assuming all labels are in top 2
  • Ignoring tie-breaking in topk
  • Not reshaping labels for comparison
4. You wrote this code to compute Top-5 accuracy but it always returns zero. What is the bug?
_, pred = outputs.topk(5)
correct = pred.eq(labels).sum().item()
medium
A. Missing dimension argument in topk causes wrong axis selection
B. Labels should be converted to float before comparison
C. topk should be called with k=1 for Top-5 accuracy
D. Using sum().item() returns a tensor, not a number

Solution

  1. Step 1: Check topk usage without dimension

    Calling topk(5) without dim defaults to dim=0, which is incorrect for class predictions along dim=1.
  2. Step 2: Understand effect on predictions and comparison

    Wrong dimension means predicted indices do not align with labels, so pred.eq(labels) never matches, resulting in zero correct.
  3. Final Answer:

    Missing dimension argument in topk causes wrong axis selection -> Option A
  4. Quick Check:

    Always specify dim=1 for class predictions in topk [OK]
Hint: Always specify dim=1 in topk for class dimension [OK]
Common Mistakes:
  • Forgetting dim argument in topk
  • Converting labels unnecessarily
  • Misunderstanding sum().item() output
5. You have a model with 100 classes and want to report Top-1 and Top-5 accuracy on a test set. Which approach best handles the evaluation efficiently and correctly?
hard
A. Use topk(1, dim=1) for Top-5 accuracy and topk(5, dim=1) for Top-1 accuracy
B. Compute Top-1 accuracy by max(dim=0) and Top-5 by topk(5, dim=0) without reshaping labels
C. Calculate Top-5 accuracy by checking if label is in top 5 predictions using a for loop over each sample
D. Use topk(5, dim=1) on model outputs, compare with labels reshaped, then compute Top-1 by checking if label equals top prediction

Solution

  1. Step 1: Understand correct usage of topk for Top-K accuracy

    Top-5 accuracy requires topk(5, dim=1) to get top 5 class indices per sample. Labels must be reshaped to compare with these indices.
  2. Step 2: Compute Top-1 accuracy separately

    Top-1 accuracy is checking if label equals the top prediction (max or topk with k=1). This is done by comparing labels with top prediction indices.
  3. Final Answer:

    Use topk(5, dim=1) on model outputs, compare with labels reshaped, then compute Top-1 by checking if label equals top prediction -> Option D
  4. Quick Check:

    Top-K needs topk(dim=1) and label reshape; Top-1 is top prediction check [OK]
Hint: Top-K needs topk(dim=1) and label reshape; Top-1 is top prediction check [OK]
Common Mistakes:
  • Using wrong dimension in topk
  • Mixing up Top-1 and Top-5 calls
  • Not reshaping labels for comparison
  • Using loops instead of vectorized operations