py123d_detection.torch.models.bevformer.camera_embedding

Telling a BEVFormer head which camera a feature came from.

The paper adds a learned vector per camera to the image features before the BEV encoder, so the encoder can treat the front camera differently from the rear one. That table is indexed by camera position, which locks a model to one rig: train with it on nuScenes’ six cameras and the weights mean nothing on Argoverse’s seven.

"none" is therefore the right choice for cross-dataset training, and "bevformer" for reproducing the paper on a single dataset. The embedding is added once, before the encoder, which is where the official implementation puts it.

Module Attributes

VALID_CAM_EMBED_TYPES

Accepted values of cam_embed_type.

Functions

build_camera_embedding(cam_embed_type, *, ...)

Build the per-camera embedding a BEVFormer head adds to its features.

Classes

BEVFormerCamEmbed(num_cameras, embed_dims)

BEVFormer's own learnable per-camera embedding.

CameraEmbedding(*args, **kwargs)

Abstract base.

py123d_detection.torch.models.bevformer.camera_embedding.VALID_CAM_EMBED_TYPES = ('none', 'bevformer')

Accepted values of cam_embed_type.

class py123d_detection.torch.models.bevformer.camera_embedding.CameraEmbedding(*args, **kwargs)[source]

Bases: Module

Abstract base. Forward returns a tensor to be ADDED to image_features (same shape (B, N, embed_dims, H, W)).

forward(image_features, *, imu2img=None, cam_mask=None, pad_h=None, pad_w=None)[source]
Return type:

Tensor

class py123d_detection.torch.models.bevformer.camera_embedding.BEVFormerCamEmbed(num_cameras, embed_dims)[source]

Bases: CameraEmbedding

BEVFormer’s own learnable per-camera embedding.

This is the cams_embeds parameter of the official implementation’s transformer: one vector per camera, normal-initialised, broadcast-added to that camera’s features.

The table is indexed by camera position, so the number of cameras is fixed at construction and a batch with a different N raises rather than silently reusing the wrong rows. That is the official design, and the error is the point: these weights mean nothing on another rig.

There is one FPN level here, so there are no level_embeds to go with it. Adding multi-scale features later means adding both parameters together, since the official code adds them in the same place.

Parameters:
  • num_cameras (int) – How many cameras the model is trained on, 6 for nuScenes. A forward pass with any other count raises.

  • embed_dims (int) – Feature dimension, matching image_features.

forward(image_features, *, cam_mask=None, **_ignored)[source]
Return type:

Tensor

py123d_detection.torch.models.bevformer.camera_embedding.build_camera_embedding(cam_embed_type, *, embed_dims, num_cameras)[source]

Build the per-camera embedding a BEVFormer head adds to its features.

Parameters:
  • cam_embed_type (str) – "bevformer" for the paper’s learned per-camera token, or "none". The learned token is indexed by camera position, so it fixes the model to one rig; "none" is what lets a model train across datasets with different camera counts.

  • num_cameras (int) – Size of the learned table. Only meaningful for "bevformer".

Return type:

Optional[CameraEmbedding]