Bird
Raised Fist0
PyTorchml~20 mins

Faster R-CNN usage in PyTorch - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Faster R-CNN Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output shape of Faster R-CNN model predictions
Given the following PyTorch code snippet using a pretrained Faster R-CNN model, what is the type and structure of the output after running the model on a batch of images?
PyTorch
import torch
from torchvision.models.detection import fasterrcnn_resnet50_fpn

model = fasterrcnn_resnet50_fpn(pretrained=True)
model.eval()

images = [torch.rand(3, 300, 400), torch.rand(3, 500, 600)]
outputs = model(images)

print(type(outputs))
print(len(outputs))
print(outputs[0].keys())
Aoutputs is a list of length 2 (one per image), each element is a dict with keys 'boxes', 'labels', and 'scores'.
Boutputs is a tensor of shape (2, 100, 6) containing bounding boxes and labels for all images.
Coutputs is a dict with keys 'boxes', 'labels', and 'scores' containing tensors for all images combined.
Doutputs is a list of length 1 containing a dict with keys 'boxes', 'labels', and 'scores'.
Attempts:
2 left
💡 Hint
Think about how torchvision detection models return predictions for each image separately.
❓ Model Choice
intermediate
1:30remaining
Choosing the right backbone for Faster R-CNN
You want to detect small objects in high-resolution images using Faster R-CNN. Which backbone architecture is generally better for capturing fine details?
AResNet-18 without FPN
BResNet-50 with Feature Pyramid Network (FPN)
CMobileNetV2 without FPN
DVGG16 without FPN
Attempts:
2 left
💡 Hint
Feature Pyramid Network helps detect objects at multiple scales.
❓ Hyperparameter
advanced
2:00remaining
Adjusting Faster R-CNN training for fewer false positives
During training Faster R-CNN, you notice many false positive detections. Which hyperparameter adjustment is most likely to reduce false positives?
AIncrease the batch size to 64
BDecrease the number of proposals generated by RPN
CIncrease the score threshold for predicted boxes during inference
DLower the learning rate drastically
Attempts:
2 left
💡 Hint
False positives can be filtered by confidence score thresholds.
🔧 Debug
advanced
2:00remaining
Identifying error in Faster R-CNN input format
You run this code and get a runtime error: import torch from torchvision.models.detection import fasterrcnn_resnet50_fpn model = fasterrcnn_resnet50_fpn(pretrained=True) model.eval() image = torch.rand(3, 224, 224) output = model(image) What is the cause of the error?
AThe pretrained model cannot run in eval mode.
BThe image tensor must have shape (224, 224, 3) instead of (3, 224, 224).
CThe model requires images to be normalized before input.
DThe model expects a list of images, not a single tensor.
Attempts:
2 left
💡 Hint
Check the expected input type for torchvision detection models.
❓ Metrics
expert
2:30remaining
Evaluating Faster R-CNN with mAP metric
You trained a Faster R-CNN model and want to evaluate its performance using mean Average Precision (mAP) at IoU=0.5. Which statement about mAP is correct?
AmAP@0.5 measures the average precision of detected boxes that have Intersection over Union (IoU) greater than 0.5 with ground truth boxes.
BmAP@0.5 is the average recall of all predicted boxes with confidence scores above 0.5.
CmAP@0.5 counts all predicted boxes regardless of IoU and averages their confidence scores.
DmAP@0.5 is the accuracy of classifying images as containing objects or not.
Attempts:
2 left
💡 Hint
mAP involves precision and IoU thresholds for matching predictions to ground truth.

Practice

(1/5)
1. What is the primary purpose of the Faster R-CNN model in PyTorch?
easy
A. To enhance image resolution
B. To generate captions for images
C. To detect objects and their locations in images
D. To classify images into categories without localization

Solution

  1. Step 1: Understand Faster R-CNN's role

    Faster R-CNN is designed to find objects and their bounding boxes in images.
  2. Step 2: Compare with other tasks

    Unlike captioning or classification without localization, Faster R-CNN outputs object locations and labels.
  3. Final Answer:

    To detect objects and their locations in images -> Option C
  4. Quick Check:

    Object detection = Faster R-CNN purpose [OK]
Hint: Faster R-CNN = object detection with bounding boxes [OK]
Common Mistakes:
  • Confusing detection with classification only
  • Thinking it generates image captions
  • Assuming it enhances image quality
2. Which of the following is the correct way to load a pretrained Faster R-CNN model in PyTorch?
easy
A. model = torchvision.models.detection.fasterrcnn_resnet50_fpn(weights='DEFAULT')
B. model = torchvision.models.fasterrcnn_resnet50_fpn(pretrained=False)
C. model = torchvision.models.fasterrcnn_resnet50_fpn(pretrained=True)
D. model = torchvision.models.detection.fasterrcnn_resnet50_fpn(weights=None)

Solution

  1. Step 1: Check PyTorch's current API for pretrained models

    PyTorch now uses the 'weights' argument with 'DEFAULT' to load pretrained weights.
  2. Step 2: Identify correct syntax

    model = torchvision.models.detection.fasterrcnn_resnet50_fpn(weights='DEFAULT') uses weights='DEFAULT', which is the recommended way to load pretrained Faster R-CNN.
  3. Final Answer:

    model = torchvision.models.detection.fasterrcnn_resnet50_fpn(weights='DEFAULT') -> Option A
  4. Quick Check:

    Use weights='DEFAULT' for pretrained models [OK]
Hint: Use weights='DEFAULT' to load pretrained Faster R-CNN [OK]
Common Mistakes:
  • Using deprecated pretrained=True argument
  • Setting weights=None disables pretrained weights
  • Confusing model namespaces in torchvision
3. Given the following code snippet, what is the output type of outputs after the model prediction?
import torchvision
import torch
model = torchvision.models.detection.fasterrcnn_resnet50_fpn(weights='DEFAULT')
model.eval()
image = torch.rand(3, 300, 400)  # Random image tensor
outputs = model([image])
medium
A. A list of dictionaries with keys 'boxes', 'labels', and 'scores'
B. A single tensor with class probabilities
C. A dictionary with keys 'boxes' and 'labels' only
D. A tensor of shape (3, 300, 400) identical to input

Solution

  1. Step 1: Understand Faster R-CNN output format

    Faster R-CNN returns a list of dictionaries, one per input image, each containing 'boxes', 'labels', and 'scores'.
  2. Step 2: Analyze the code snippet

    Since input is a list with one image, output is a list with one dictionary containing detection results.
  3. Final Answer:

    A list of dictionaries with keys 'boxes', 'labels', and 'scores' -> Option A
  4. Quick Check:

    Output = list of dicts with boxes, labels, scores [OK]
Hint: Faster R-CNN outputs list of dicts with boxes, labels, scores [OK]
Common Mistakes:
  • Expecting a single tensor output
  • Missing 'scores' key in output
  • Confusing input and output tensor shapes
4. Identify the error in the following code snippet for using Faster R-CNN for prediction:
import torchvision
import torch
model = torchvision.models.detection.fasterrcnn_resnet50_fpn(weights='DEFAULT')
model.eval()
image = torch.rand(3, 224, 224)
outputs = model(image)
medium
A. Model.eval() should be called after prediction
B. Input image should be a list of tensors, not a single tensor
C. Weights argument 'DEFAULT' is invalid
D. Image tensor shape should be (224, 224, 3) instead of (3, 224, 224)

Solution

  1. Step 1: Check input format for Faster R-CNN

    Faster R-CNN expects a list of image tensors, even if only one image is passed.
  2. Step 2: Identify the error in the code

    The code passes a single tensor instead of a list, causing a runtime error.
  3. Final Answer:

    Input image should be a list of tensors, not a single tensor -> Option B
  4. Quick Check:

    Input = list of tensors for Faster R-CNN [OK]
Hint: Wrap input image in a list before passing to model [OK]
Common Mistakes:
  • Passing single tensor instead of list
  • Calling eval() after prediction
  • Confusing channel order in tensor shape
5. You want to fine-tune a pretrained Faster R-CNN model on a custom dataset with 5 classes (including background). Which step is necessary before training?
hard
A. Change the input image size to (5, 224, 224)
B. Freeze all layers except the backbone
C. Set model.train() before loading pretrained weights
D. Replace the model's box predictor head with a new one having 5 output classes

Solution

  1. Step 1: Understand model adaptation for new classes

    The box predictor head controls class outputs; it must match the number of classes in your dataset.
  2. Step 2: Replace the box predictor head

    Replace the existing head with a new one configured for 5 classes to fine-tune properly.
  3. Final Answer:

    Replace the model's box predictor head with a new one having 5 output classes -> Option D
  4. Quick Check:

    Adjust box predictor for new class count [OK]
Hint: Change box predictor head to match your classes [OK]
Common Mistakes:
  • Changing input image channels incorrectly
  • Not replacing box predictor head
  • Freezing all layers prevents fine-tuning
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard