py123d_detection.torch.models.common.position_embedding

PETR’s 3D frustum position embedding, shared by both detectors.

compute_frustum_coords3d() is the geometry on its own: per-camera imu2img matrices in, a per-pixel 3D coordinate volume out, ready for inverse_sigmoid and an MLP. PETR’s head and BEVFormer’s "petr" camera-embedding option both call it.

PETRFrustumPositionEmbedding wraps the whole pipeline around that helper (the frustum geometry, the MLP, an optional sine embedding and the adapt_pos3d fusion) as a module BEVFormer can own.

The geometry is a plain function rather than a method so that PETRHead keeps owning its own submodules, and therefore its state_dict keys. Moving the MLP into a shared module would rename those keys, and every checkpoint trained before the move would fail to load with missing keys.

Functions

compute_frustum_coords3d(B, N, H, W, pad_h, ...)

Build the 3D frustum coordinate volume and its validity mask.

py123d_detection.torch.models.common.position_embedding.compute_frustum_coords3d(B, N, H, W, pad_h, pad_w, imu2img, depth_num, depth_start, LID, position_range_t, mask_strict, device, masks=None, eps=1e-5)[source]

Build the 3D frustum coordinate volume and its validity mask.

This is the frustum branch of PETRHead.position_embeding, lifted out so BEVFormer can use it too. The result still has to go through inverse_sigmoid and the model’s own position_encoder MLP.

Parameters:
  • B (int) – Batch size.

  • N (int) – Number of cameras.

  • H (int) – Feature-grid height.

  • W (int) – Feature-grid width.

  • pad_h (int) – Padded image height; the feature grid is downsampled from it.

  • pad_w (int) – Padded image width.

  • imu2img (Tensor) – (B, N, 4, 4) projections, intrinsics @ extrinsics. A singular matrix (a padded camera, |det| < 1e-6) is replaced with the identity.

  • depth_num (int) – Discrete depth bins along each ray. PETR uses 64.

  • depth_start (float) – Near-plane depth in metres. PETR uses 1.0.

  • LID (bool) – Log-increasing bins, finer near the camera, rather than uniform ones.

  • position_range_t (Tensor) – (6,) float32 buffer, [x_min, y_min, z_min, x_max, y_max, z_max], that the coordinates are normalised against. Keep it wider than the detection pc_range, or points at the edge of the detection volume saturate the following inverse_sigmoid.

  • mask_strict (bool) – Count the violations and compare the sum, as official PETR does, rather than masking a pixel as soon as any one component leaves the range.

  • masks (Optional[Tensor]) – (B, N, H, W), True where a pixel is invalid (a padded camera, a padded image border). Those pixels’ coordinates are zeroed before inverse_sigmoid.

  • eps (float) – Floor for the perspective division.

Return type:

Tuple[Tensor, Tensor]

Returns:

(coords3d, coords_mask). coords3d is (B*N, depth_num * 3, H, W) and still in [0, 1], not yet inverse-sigmoid’d, so the caller chooses its own float32 autocast scope. coords_mask is (B, N, H, W) and True where downstream attention should mask the pixel. The centre pixel of every batch element is forced valid, because an all-masked row has no softmax.