Bird
Raised Fist0
PyTorchml~10 mins

torchvision detection models in PyTorch - Interactive Code Practice

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

Complete the code to load a pre-trained Faster R-CNN model from torchvision.

PyTorch
import torchvision.models.detection as detection
model = detection.[1](pretrained=True)
Drag options to blanks, or click blank then click option'
Afasterrcnn_resnet50_fpn
Bresnet50
Cmobilenet_v2
Dvgg16
Attempts:
3 left
💡 Hint
Common Mistakes
Using classification model names like 'resnet50' instead of detection models.
Forgetting to set pretrained=True to load weights.
2fill in blank
medium

Complete the code to put the model in evaluation mode before inference.

PyTorch
model = detection.fasterrcnn_resnet50_fpn(pretrained=True)
model.[1]()
Drag options to blanks, or click blank then click option'
Aeval
Bfit
Ctrain
Dpredict
Attempts:
3 left
💡 Hint
Common Mistakes
Using train() mode during inference, which can cause inconsistent results.
Trying to call predict() which is not a PyTorch model method.
3fill in blank
hard

Fix the error in the code to move the model to the GPU device if available.

PyTorch
import torch
model = detection.fasterrcnn_resnet50_fpn(pretrained=True)
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
model = model.[1](device)
Drag options to blanks, or click blank then click option'
Amove
Bto
Ccuda
Ddevice
Attempts:
3 left
💡 Hint
Common Mistakes
Using cuda() directly, which fails if CUDA is not available.
Using non-existent methods like device() or move().
4fill in blank
hard

Fill in the blank to perform inference with the model.

PyTorch
import torch
from PIL import Image
from torchvision import transforms

image = Image.open('image.jpg').convert('RGB')
transform = transforms.Compose([
    transforms.ToTensor(),
])
input_tensor = transform(image)
model.eval()
with torch.no_grad():
    outputs = model([1])
Drag options to blanks, or click blank then click option'
Aunsqueeze
Bsqueeze
Cinput_tensor
D[input_tensor]
Attempts:
3 left
💡 Hint
Common Mistakes
Passing input_tensor directly instead of a list.
Trying to add a batch dimension with unsqueeze(), which is unnecessary and incorrect for these models.
5fill in blank
hard

Fill all three blanks to filter detection results by confidence score and print detected labels.

PyTorch
labels = outputs[0]['labels']
scores = outputs[0]['scores']
threshold = 0.8
filtered_indices = [i for i, score in enumerate(scores) if score [1] threshold]
filtered_labels = [labels[i].item() for i in filtered_indices]
print('Detected labels:', [2])

# Convert label IDs to class names
COCO_INSTANCE_CATEGORY_NAMES = [
    '__background__', 'person', 'bicycle', 'car', 'motorcycle', 'airplane', 'bus',
    'train', 'truck', 'boat', 'traffic light', 'fire hydrant', 'stop sign',
    'parking meter', 'bench', 'bird', 'cat', 'dog', 'horse', 'sheep', 'cow',
    'elephant', 'bear', 'zebra', 'giraffe', 'backpack', 'umbrella', 'handbag',
    'tie', 'suitcase', 'frisbee', 'skis', 'snowboard', 'sports ball', 'kite',
    'baseball bat', 'baseball glove', 'skateboard', 'surfboard', 'tennis racket',
    'bottle', 'wine glass', 'cup', 'fork', 'knife', 'spoon', 'bowl', 'banana',
    'apple', 'sandwich', 'orange', 'broccoli', 'carrot', 'hot dog', 'pizza',
    'donut', 'cake', 'chair', 'couch', 'potted plant', 'bed', 'dining table',
    'toilet', 'tv', 'laptop', 'mouse', 'remote', 'keyboard', 'cell phone',
    'microwave', 'oven', 'toaster', 'sink', 'refrigerator', 'book', 'clock',
    'vase', 'scissors', 'teddy bear', 'hair drier', 'toothbrush'
]

filtered_names = [COCO_INSTANCE_CATEGORY_NAMES[label] for label in filtered_labels]
print('Detected class names:', [3])
Drag options to blanks, or click blank then click option'
A>
B>=
Cfiltered_labels
Dfiltered_names
Attempts:
3 left
💡 Hint
Common Mistakes
Using '>' which excludes scores exactly equal to threshold.
Printing class names before converting label IDs.
Confusing filtered_labels and filtered_names in print statements.

Practice

(1/5)
1. Which of the following is a detection model provided by torchvision for finding objects in images?
easy
A. ResNet
B. Faster R-CNN
C. VGG16
D. Transformer

Solution

  1. Step 1: Understand torchvision detection models

    torchvision offers models specifically for object detection like Faster R-CNN, Mask R-CNN, and RetinaNet.
  2. Step 2: Identify the correct model

    ResNet and VGG16 are classification models, and Transformer is a general architecture, not a torchvision detection model.
  3. Final Answer:

    Faster R-CNN -> Option B
  4. Quick Check:

    Detection model = Faster R-CNN [OK]
Hint: Detection models include 'R-CNN' names in torchvision [OK]
Common Mistakes:
  • Confusing classification models with detection models
  • Thinking Transformer is a torchvision detection model
  • Mixing up model types
2. Which code snippet correctly loads a pretrained Faster R-CNN model from torchvision?
easy
A. model = torchvision.models.detection.fasterrcnn_resnet50_fpn(weights='DEFAULT')
B. model = torchvision.models.fasterrcnn_resnet50_fpn(pretrained=True)
C. model = torchvision.detection.fasterrcnn_resnet50_fpn(pretrained=True)
D. model = torchvision.models.detection.fasterrcnn_resnet50_fpn(pretrained=False)

Solution

  1. Step 1: Check the correct module and argument

    In recent torchvision versions, detection models are under torchvision.models.detection and use weights='DEFAULT' instead of pretrained=True.
  2. Step 2: Identify the correct syntax

    model = torchvision.models.detection.fasterrcnn_resnet50_fpn(weights='DEFAULT') uses the correct module path and argument for loading pretrained weights.
  3. Final Answer:

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

    Use weights='DEFAULT' to load pretrained detection models [OK]
Hint: Use weights='DEFAULT' for pretrained detection models [OK]
Common Mistakes:
  • Using pretrained=True instead of weights='DEFAULT'
  • Wrong module path
  • Missing 'models' in import path
3. What will be the output type of the following code snippet?
import torch
import torchvision
model = torchvision.models.detection.fasterrcnn_resnet50_fpn(weights='DEFAULT')
model.eval()
inputs = [torch.rand(3, 300, 400)]
outputs = model(inputs)
print(type(outputs))
medium
A.
B.
C.
D.

Solution

  1. Step 1: Understand model output format

    torchvision detection models return a list of dictionaries, one per input image.
  2. Step 2: Check the type of outputs variable

    Since inputs is a list with one tensor, outputs will be a list containing one dictionary.
  3. Final Answer:

    <class 'list'> -> Option D
  4. Quick Check:

    Detection model output = list of dicts [OK]
Hint: Detection models output a list, one dict per input image [OK]
Common Mistakes:
  • Expecting a single tensor output
  • Confusing output with dict directly
  • Not wrapping inputs in a list
4. Identify the error in this code snippet that tries to use a pretrained Mask R-CNN model:
import torchvision
model = torchvision.models.detection.maskrcnn_resnet50_fpn(pretrained=True)
model.eval()
medium
A. The function name should be fasterrcnn_resnet50_fpn
B. Mask R-CNN is not available in torchvision
C. The argument 'pretrained' is deprecated; use 'weights' instead
D. Missing import for torch

Solution

  1. Step 1: Check function and argument usage

    Mask R-CNN is available as maskrcnn_resnet50_fpn in torchvision.models.detection.
  2. Step 2: Identify deprecated argument

    Using pretrained=True is deprecated; the correct way is weights='DEFAULT'.
  3. Final Answer:

    The argument 'pretrained' is deprecated; use 'weights' instead -> Option C
  4. Quick Check:

    Use weights='DEFAULT' instead of pretrained=True [OK]
Hint: Replace pretrained=True with weights='DEFAULT' [OK]
Common Mistakes:
  • Using deprecated pretrained argument
  • Confusing model names
  • Assuming Mask R-CNN is missing
5. You want to fine-tune a pretrained RetinaNet model from torchvision on your custom dataset with 5 classes. Which step is necessary before training?
hard
A. Replace the model's box predictor head to output 6 classes (5 + background)
B. Change the input image size to 224x224
C. Set pretrained=False to train from scratch
D. Freeze all layers except the backbone

Solution

  1. Step 1: Understand class output adjustment

    Detection models include a background class, so for 5 classes, output head must predict 6 classes.
  2. Step 2: Modify the box predictor head

    Replace the box predictor to match the number of classes (5 + 1 background) before training.
  3. Final Answer:

    Replace the model's box predictor head to output 6 classes (5 + background) -> Option A
  4. Quick Check:

    Adjust output head for custom classes [OK]
Hint: Add 1 for background class when changing output head [OK]
Common Mistakes:
  • Not adjusting output head for new classes
  • Changing input size unnecessarily
  • Freezing all layers without fine-tuning head
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