0
0
Computer Visionml~10 mins

Top-K accuracy in Computer Vision - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to import the function that calculates top-k accuracy from PyTorch.

Computer Vision
from torchmetrics import [1]
Drag options to blanks, or click blank then click option'
AAccuracy
BRecall
CPrecision
DTopKAccuracy
Attempts:
3 left
💡 Hint
Common Mistakes
Choosing TopKAccuracy (does not exist)
Using Precision or Recall which are different metrics
2fill in blank
medium

Complete the code to create an Accuracy metric object for top 3 accuracy.

Computer Vision
top3_acc = Accuracy(task="multiclass", top_k=[1])
Drag options to blanks, or click blank then click option'
A1
B3
C5
D10
Attempts:
3 left
💡 Hint
Common Mistakes
Using k=1 which is normal accuracy
Using k=5 or 10 which are valid but not for top 3 accuracy
3fill in blank
hard

Fix the error in the code to compute top-5 accuracy from model outputs and labels.

Computer Vision
top5_acc = Accuracy(task="multiclass", top_k=[1])
outputs = torch.randn(8, 1000)  # batch of 8, 1000 classes
labels = torch.randint(0, 1000, (8,))
accuracy = top5_acc(outputs, labels)
Drag options to blanks, or click blank then click option'
A10
B3
C1
D5
Attempts:
3 left
💡 Hint
Common Mistakes
Using k=1 or k=3 which do not compute top-5 accuracy
Passing wrong shapes for outputs or labels
4fill in blank
hard

Fill both blanks to create a dictionary of top-1 and top-3 accuracy metrics.

Computer Vision
metrics = {
    'top1': Accuracy(task="multiclass", top_k=[1]),
    'top3': Accuracy(task="multiclass", top_k=[2])
}
Drag options to blanks, or click blank then click option'
A1
B2
C3
D5
Attempts:
3 left
💡 Hint
Common Mistakes
Confusing top-1 with top-3 values
Using k=2 which is not standard for top-k accuracy
5fill in blank
hard

Fill all three blanks to compute and print top-1, top-3, and top-5 accuracy from outputs and labels.

Computer Vision
outputs = torch.randn(16, 1000)
labels = torch.randint(0, 1000, (16,))
metrics = {
    'top1': Accuracy(task="multiclass", top_k=[1]),
    'top3': Accuracy(task="multiclass", top_k=[2]),
    'top5': Accuracy(task="multiclass", top_k=[3])
}
for name, metric in metrics.items():
    acc = metric(outputs, labels)
    print(f"{name} accuracy: {acc.item():.4f}")
Drag options to blanks, or click blank then click option'
A1
B3
C5
D10
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up k values for different top-k metrics
Using invalid k values like 10 for top1 or top3