Imports
run firstThe lab file's imports. Run this cell first (or press Run all). Read the comment in it: this lab asks you to add imports here.
Hands-on lab 09 of 10 · BEV projection
A BEV model builds one top-down feature map around the car from several cameras. The geometric core is plain linear algebra: lift every BEV cell to a few 3-D points, project them into each camera, keep the ones that land inside an image, and bilinearly sample the image features there. You build camera intrinsics and extrinsics, the projection, the BEV pillar points, the reference points with their visibility mask, and the feature sampling with F.grid_sample.
Frames: ego x forward, y left, z up (origin on the ground under the car)
camera x right, y down, z forward = depth (OpenCV convention)
[[f, 0, cx], [0, f, cy], [0, 0, 1]], cx = W/2, cy = H/2
Extrinsics ego → camera for a camera at height h, turned by yaw psi (counter-clockwise from +x, towards +y = left):
forward z_c = ( cos psi, sin psi, 0) (optical axis, in ego coords) right x_c = ( sin psi, -cos psi, 0) down y_c = ( 0, 0, -1) R = [x_c; y_c; z_c] (rows), C = (0, 0, h) camera centre p_cam = R (p_ego - C) = R p_ego + t, t = -R C
stored as a 4 x 4 matrix [[R, t], [0, 0, 0, 1]].
(X, Y, Z) = p_cam; u = f X / Z + cx, v = f Y / Z + cy
Z is the depth; a point with Z <= 0 is behind the camera.
BEV pillars: cell (i, j) of a bev_h x bev_w grid with resolution r metres
x = (i - (bev_h - 1) / 2) * r (forward) y = (j - (bev_w - 1) / 2) * r (left)
lifted to N_z heights z_1..z_Nz. Query index q = i * bev_w + j. points: [Q = bev_h * bev_w, N_z, 3]
uv_norm = (2u / W - 1, 2v / H - 1) in [-1, 1] inside the image valid = (Z > eps) and (0 < u < W) and (0 < v < H)
([-1, 1] coordinates are resolution-free: the same uv_norm samples the image or a stride-16 feature map of it.)
Sampling: F.grid_sample(features [N_cam, C, Hf, Wf], uv_norm as grid [N_cam, Q, N_z, 2], bilinear, align_corners=False) → [N_cam, C, Q, N_z]. BEVFormer's spatial cross-attention then averages over the cameras that see the cell (and mixes the heights with learned attention weights); here a plain average over the valid cameras and heights stands in for it.
Worked example (guide, front camera, W x H = 800 x 448, f = 500, h = 1.6 m): p = (10, 2, 0) → p_cam = (-2, 1.6, 10) → (u, v) = (300, 304) → uv_norm = (-0.25, 0.357). The rear camera sees Z = -10: invalid.
Editor keys: Tab / Shift+Tab indent and dedent by 4 spaces, Enter keeps the indentation, Shift+Enter runs the cell and moves to the next one.
Cells on this page:
The lab file's imports. Run this cell first (or press Run all). Read the comment in it: this lab asks you to add imports here.
make_intrinsics(f, img_w, img_h)K [3, 3] float32 with focal length f (pixels) and principal point at the image centre.
make_ego_to_cam(yaw_deg, cam_height)[4, 4] float32 ego → camera transform for a camera at (0, 0, cam_height) facing yaw_deg.
project_points(points, K, ego_to_cam)points [P, 3] in the ego frame → (uv [P, 2] pixels, depth [P]).
Divide by the depth clamped to at least 1e-5 so points behind the camera do not produce inf (they are marked invalid later anyway).
bev_pillar_points(bev_h, bev_w, resolution, z_anchors=(0.0, 1.0, 2.0, 3.0))[bev_h * bev_w, N_z, 3] ego-frame points; cell (i, j) → query q = i * bev_w + j.
reference_points(points, Ks, ego_to_cams, img_h, img_w)Project pillar points into every camera.
Args:
points: [Q, N_z, 3] Ks: [N_cam, 3, 3] ego_to_cams: [N_cam, 4, 4]
Returns:
uv_norm: [N_cam, Q, N_z, 2] (2u/W - 1, 2v/H - 1) valid: [N_cam, Q, N_z] bool, depth > 1e-5 and strictly inside the image
sample_features(features, uv_norm)Bilinear sampling at the reference points.
features: [N_cam, C, Hf, Wf] uv_norm: [N_cam, Q, N_z, 2] Returns [N_cam, Q, N_z, C]. Use F.grid_sample(mode="bilinear", padding_mode="zeros", align_corners=False): uv_norm treats the image edges as -1 and +1, which is the align_corners=False convention.
aggregate_views(sampled, valid)Average the valid samples of each BEV cell over cameras and heights.
sampled [N_cam, Q, N_z, C], valid [N_cam, Q, N_z] → bev [Q, C]. Cells that no camera sees get zeros.
Experiment here: print shapes, call your functions on small inputs. This cell runs in the same Python process as the cells above, is not part of Run all and is not saved to the lab file (your browser keeps it).