Bird
Raised Fist0
PyTorchml~20 mins

Bounding box handling in PyTorch - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Bounding Box Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of bounding box area calculation
What is the output of the following code that calculates the area of bounding boxes stored as (x_min, y_min, x_max, y_max)?
PyTorch
import torch
boxes = torch.tensor([[1, 2, 4, 6], [0, 0, 3, 3], [2, 2, 5, 5]])
widths = boxes[:, 2] - boxes[:, 0]
heights = boxes[:, 3] - boxes[:, 1]
areas = widths * heights
print(areas.tolist())
A[15, 9, 12]
B[12, 9, 9]
C[12, 6, 9]
D[9, 9, 9]
Attempts:
2 left
💡 Hint
Remember area = width * height, where width = x_max - x_min and height = y_max - y_min.
❓ Model Choice
intermediate
2:00remaining
Choosing the right bounding box format for IoU calculation
Which bounding box format is best suited for calculating Intersection over Union (IoU) directly without conversion?
ATop-left and bottom-right corners (x_min, y_min, x_max, y_max)
BTop-left corner with width and height (x_min, y_min, w, h)
CCenter coordinates with corner points (cx, cy, x_min, y_min)
DCenter coordinates with width and height (cx, cy, w, h)
Attempts:
2 left
💡 Hint
IoU requires overlapping area calculation which is easier with corner coordinates.
❓ Hyperparameter
advanced
2:00remaining
Effect of IoU threshold on Non-Maximum Suppression (NMS)
What is the effect of increasing the IoU threshold parameter in Non-Maximum Suppression when filtering bounding boxes?
ABoxes are sorted by confidence score instead of IoU
BFewer boxes are kept, resulting in stricter suppression
CNo change in number of boxes kept
DMore boxes are kept, allowing more overlapping detections
Attempts:
2 left
💡 Hint
Higher IoU threshold means boxes must overlap more to be suppressed.
🔧 Debug
advanced
2:00remaining
Debugging incorrect bounding box clipping
Given bounding boxes and image size, which option correctly clips boxes to image boundaries without errors?
PyTorch
import torch
boxes = torch.tensor([[50, 30, 200, 180], [-10, 20, 100, 150], [30, 40, 500, 400]])
image_size = (300, 300)  # height, width
# Clip boxes so coordinates stay within image boundaries
A
boxes[:, 0] = boxes[:, 0].clamp(0, image_size[1])
boxes[:, 1] = boxes[:, 1].clamp(0, image_size[0])
boxes[:, 2] = boxes[:, 2].clamp(0, image_size[1])
boxes[:, 3] = boxes[:, 3].clamp(0, image_size[0])
B
boxes[:, 0] = boxes[:, 0].clamp(0, image_size[0])
boxes[:, 1] = boxes[:, 1].clamp(0, image_size[1])
boxes[:, 2] = boxes[:, 2].clamp(0, image_size[0])
boxes[:, 3] = boxes[:, 3].clamp(0, image_size[1])
C
boxes[:, 0] = boxes[:, 0].clamp(0, image_size[1]-1)
boxes[:, 1] = boxes[:, 1].clamp(0, image_size[0]-1)
boxes[:, 2] = boxes[:, 2].clamp(0, image_size[1]-1)
boxes[:, 3] = boxes[:, 3].clamp(0, image_size[0]-1)
D
boxes[:, 0] = boxes[:, 0].clamp(0, image_size[0]-1)
boxes[:, 1] = boxes[:, 1].clamp(0, image_size[0]-1)
boxes[:, 2] = boxes[:, 2].clamp(0, image_size[1]-1)
boxes[:, 3] = boxes[:, 3].clamp(0, image_size[1]-1)
Attempts:
2 left
💡 Hint
Remember image_size is (height, width). x coordinates clamp to width, y coordinates clamp to height.
🧠 Conceptual
expert
3:00remaining
Understanding bounding box regression targets in object detection
In object detection, bounding box regression predicts offsets relative to anchor boxes. Which statement correctly describes why offsets are predicted instead of absolute coordinates?
AOffsets normalize the prediction space, making training more stable and easier to learn
BAbsolute coordinates are always better because they are direct and simpler
COffsets reduce the number of bounding boxes needed during inference
DAbsolute coordinates allow the model to ignore anchor boxes completely
Attempts:
2 left
💡 Hint
Think about how predicting relative changes helps the model focus on small adjustments.

Practice

(1/5)
1. What do the four numbers in a bounding box usually represent in PyTorch object detection tasks?
easy
A. Width, height, depth, and color
B. Center x, center y, width, height
C. Top-left corner x and y, width, height
D. Left, top, right, bottom coordinates of the box

Solution

  1. Step 1: Understand bounding box format

    Bounding boxes in PyTorch detection models are often represented by four numbers indicating the edges: left (x_min), top (y_min), right (x_max), and bottom (y_max).
  2. Step 2: Compare options with standard format

    Left, top, right, bottom coordinates of the box matches this format exactly, while others describe different formats or unrelated values.
  3. Final Answer:

    Left, top, right, bottom coordinates of the box -> Option D
  4. Quick Check:

    Bounding box = left, top, right, bottom [OK]
Hint: Remember bounding box edges are left, top, right, bottom [OK]
Common Mistakes:
  • Confusing center coordinates with edges
  • Mixing width and height with coordinates
  • Assuming depth or color are part of bounding box
2. Which PyTorch tensor shape correctly represents a batch of 5 bounding boxes, each with 4 coordinates?
easy
A. torch.Size([5, 4])
B. torch.Size([4, 5])
C. torch.Size([5])
D. torch.Size([4])

Solution

  1. Step 1: Identify bounding box tensor shape

    Each bounding box has 4 coordinates. For 5 boxes, the tensor shape should be (5, 4), meaning 5 rows (boxes) and 4 columns (coordinates).
  2. Step 2: Match options with correct shape

    torch.Size([5, 4]) matches (5, 4). torch.Size([4, 5]) reverses dimensions, and C and D are one-dimensional, which is incorrect.
  3. Final Answer:

    torch.Size([5, 4]) -> Option A
  4. Quick Check:

    Batch size 5 and 4 coords = (5, 4) [OK]
Hint: Batch size first, coordinates second in tensor shape [OK]
Common Mistakes:
  • Swapping batch and coordinate dimensions
  • Using 1D tensor for multiple boxes
  • Confusing number of boxes with number of coordinates
3. Given the following PyTorch code, what is the output?
import torch
boxes = torch.tensor([[1, 2, 4, 6], [0, 0, 3, 3]])
widths = boxes[:, 2] - boxes[:, 0]
heights = boxes[:, 3] - boxes[:, 1]
areas = widths * heights
print(areas.tolist())
medium
A. [10, 9]
B. [15, 9]
C. [12, 9]
D. [12, 6]

Solution

  1. Step 1: Calculate widths and heights

    For box 1: width = 4 - 1 = 3, height = 6 - 2 = 4. For box 2: width = 3 - 0 = 3, height = 3 - 0 = 3.
  2. Step 2: Calculate areas

    Area box 1 = 3 * 4 = 12, area box 2 = 3 * 3 = 9.
  3. Final Answer:

    [12, 9] -> Option C
  4. Quick Check:

    Area = width * height = [12, 9] [OK]
Hint: Area = (right-left) * (bottom-top) for each box [OK]
Common Mistakes:
  • Subtracting coordinates in wrong order
  • Mixing width with height values
  • Forgetting to multiply widths and heights
4. Identify the error in this PyTorch code snippet for checking if points lie inside bounding boxes:
import torch
boxes = torch.tensor([1, 1, 5, 5])
points = torch.tensor([[3, 3], [6, 6]])
inside = (points[:, 0] >= boxes[:, 0]) & (points[:, 0] <= boxes[:, 2]) & (points[:, 1] >= boxes[:, 1]) & (points[:, 1] <= boxes[:, 3])
print(inside)
medium
A. The comparison between points and boxes tensors will fail due to shape mismatch
B. The logical AND operator '&' cannot be used with tensors
C. The code should use .all() to combine conditions
D. Points coordinates are reversed in the comparisons

Solution

  1. Step 1: Check tensor shapes for broadcasting

    Boxes shape is (4,), points shape is (2,2). Comparing points[:, 0] (shape 2) with boxes[:, 0] causes shape mismatch as boxes is 1D.
  2. Step 2: Understand broadcasting rules

    Direct element-wise comparison fails because points and boxes dimensions don't align for broadcasting in this code.
  3. Final Answer:

    The comparison between points and boxes tensors will fail due to shape mismatch -> Option A
  4. Quick Check:

    Shape mismatch causes error in element-wise comparison [OK]
Hint: Check tensor shapes before element-wise comparisons [OK]
Common Mistakes:
  • Assuming PyTorch auto-broadcasts all shapes
  • Using bitwise operators incorrectly
  • Mixing coordinate order in comparisons
5. You want to convert bounding boxes from (left, top, right, bottom) format to (center_x, center_y, width, height) in PyTorch. Which code snippet correctly performs this conversion for a tensor boxes of shape (N, 4)?
hard
A. centers = boxes[:, :2] - boxes[:, 2:] sizes = boxes[:, 2:] + boxes[:, :2] converted = torch.cat([centers, sizes], dim=1)
B. centers = (boxes[:, :2] + boxes[:, 2:]) / 2 sizes = boxes[:, 2:] - boxes[:, :2] converted = torch.cat([centers, sizes], dim=1)
C. centers = boxes[:, 2:] - boxes[:, :2] sizes = (boxes[:, :2] + boxes[:, 2:]) / 2 converted = torch.cat([sizes, centers], dim=1)
D. centers = boxes[:, :2] * boxes[:, 2:] sizes = boxes[:, 2:] / boxes[:, :2] converted = torch.cat([centers, sizes], dim=1)

Solution

  1. Step 1: Calculate center coordinates

    Center x and y are the average of left and right, top and bottom: (left + right)/2, (top + bottom)/2, which is (boxes[:, :2] + boxes[:, 2:]) / 2.
  2. Step 2: Calculate width and height

    Width and height are right - left and bottom - top: boxes[:, 2:] - boxes[:, :2].
  3. Step 3: Concatenate centers and sizes

    Combine centers and sizes along dimension 1 to get (center_x, center_y, width, height).
  4. Final Answer:

    centers = (boxes[:, :2] + boxes[:, 2:]) / 2 sizes = boxes[:, 2:] - boxes[:, :2] converted = torch.cat([centers, sizes], dim=1) -> Option B
  5. Quick Check:

    Center = avg edges, size = difference edges [OK]
Hint: Center = average edges; size = difference edges [OK]
Common Mistakes:
  • Swapping center and size calculations
  • Using addition instead of subtraction for sizes
  • Concatenating in wrong order
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