← Questions

Train a small CNN to regress a rotated bounding box

0
Scale AICodingLast asked Dec 2023

A coding interview question asked in Scale AI's applied engineering round, last asked Dec 2023. It tests computer vision, pytorch and iou.

A practical round in an existing ML project, about two hours. Each image contains one object, labelled with a rotated (oriented) bounding box. Get a compact convolutional network training end to end so it predicts that box, write the IoU between two rotated boxes to measure it, then run a few model variants under a fixed parameter budget and say which one you would keep and why.

Interface

Unless you agree on a different interface with the interviewer, assume:

  • A box is (cx, cy, w, h, angle): centre, width, height and rotation in radians, in image coordinates.
  • rotated_iou(a, b) -> float — the area of the two boxes' intersection divided by the area of their union, in [0, 1].
  • build_model(config) -> nn.Module — a small CNN with a regression head; count_params(model) must stay under the budget the project sets.
  • train(model, train_loader, val_loader, epochs) -> dict — returns the training loss curve and validation mean IoU.

Example

rotated_iou((0, 0, 2, 2, 0), (0, 0, 2, 2, 0))        ->  1.0
rotated_iou((0, 0, 4, 2, 0), (1, 0, 4, 2, 0))        ->  0.6      # overlap 3x2 = 6, union 8 + 8 - 6 = 10
rotated_iou((0, 0, 4, 2, 0), (0, 0, 4, 2, pi/2))     ->  0.333    # overlap 2x2 = 4, union 12
rotated_iou((0, 0, 4, 2, 0), (0, 0, 4, 2, pi))       ->  1.0      # same box, angle differs by pi
rotated_iou((0, 0, 2, 2, 0), (0, 0, 2, 2, pi/4))     ->  0.7071   # the overlap is a regular octagon
rotated_iou((0, 0, 2, 2, 0), (5, 5, 2, 2, 0.3))      ->  0.0

Constraints

  • A hard limit on trainable parameters; every variant you report must fit under it.
  • Exactly one box per image.
  • A limited time box, so pick experiments that answer a question.

Clarifying questions

  • How is the angle defined: range, units, and which side it measures from?
  • What is the parameter budget, and is the metric mean IoU or accuracy at an IoU threshold?
  • Are image sizes fixed, and are box coordinates normalized?
  • Is the existing training loop known to work, or is part of the task to fix it?

What a strong answer covers

  • Rotated IoU by polygon clipping (Sutherland-Hodgman) and the shoelace area, with care for no overlap, identical boxes and near-parallel edges.
  • Angle ambiguity: a box at θ equals one at θ + π, and (w, h, θ) equals (h, w, θ + π/2); predict sin 2θ, cos 2θ or use a loss that respects the periodicity.
  • A sensible regression loss (smooth L1 on normalized targets, possibly plus an IoU-based term) and target normalization.
  • A small backbone under budget: depthwise convolutions, global pooling instead of large dense layers, and a parameter count printed for each variant.
  • Controlled comparisons: one change at a time, the same seed and data split, validation mean IoU reported with its spread, and a reasoned pick.
  • Sanity checks: overfit a tiny batch first, and plot predicted boxes on images.

Follow-ups

  • Several objects per image: what changes in the head and the loss?
  • Make the IoU differentiable enough to train on directly.
  • Batch the IoU over thousands of box pairs on a GPU.

Where it's asked

What it tests

computer vision · pytorch · iou

Write and test your solution herePython in your browser, the question's tests level by level, every submission kept for you.Sign in to start coding