py123d_detection.torch.models.base

The training machinery every detector in this package shares.

Detector3D owns the Lightning loop, the optimiser and the logging; a subclass supplies a backbone and a head and says how one batch becomes predictions. PETR and BEVFormer are then siblings rather than one inheriting from the other, which is what stops a change to PETR silently changing BEVFormer. In the thesis they were the same class, and it did.

The one piece of real machinery here is per-dataset loss. Training on nuScenes and Argoverse together means a batch holds frames from both, and averaging their losses into one number hides which dataset is failing. The loss is instead computed once per dataset in the batch and weighted by that dataset’s share, which gives the same gradient and a readable train_loss_<dataset> for each.

Distributed training needs one more thing. The loss normalises by the number of ground-truth boxes across all ranks, which is an all-reduce. Doing that inside each per-dataset group would have rank 0 reducing over three groups while rank 1 reduces over two, and the job hangs. sync_box_count() does it once, before the split.

Functions

petr_schedule(step, steps_per_epoch, max_epochs)

Learning-rate factor at one optimiser step: PETR's and BEVFormer's published schedule.

Classes

Detector3D([learning_rate, weight_decay, ...])

Base for camera-based 3D detectors.

py123d_detection.torch.models.base.petr_schedule(step, steps_per_epoch, max_epochs, warmup_steps=500, warmup_ratio=1.0 / 3.0, min_lr_ratio=1e-3)[source]

Learning-rate factor at one optimiser step: PETR’s and BEVFormer’s published schedule.

mmcv’s CosineAnnealing with by_epoch=True: the rate is constant within an epoch and follows a cosine from 1 to min_lr_ratio across epochs. The first warmup_steps steps ramp it linearly up from warmup_ratio of that. The thesis trained with exactly this.

Return type:

float

class py123d_detection.torch.models.base.Detector3D(learning_rate=0.0002, weight_decay=0.01, max_epochs=24, warmup_steps=500, backbone_lr_scale=0.1)[source]

Bases: LightningModule

Base for camera-based 3D detectors.

A subclass sets self.backbone and self.head and implements forward(). The head must return {"all_cls_scores": (L, B, Q, C), "all_bbox_preds": (L, B, Q, 10)} and provide loss(boxes, labels, predictions, ...) and get_bboxes(predictions).

Parameters:
  • learning_rate (float) – Base rate for the head. The backbone gets a tenth of it, which is the usual treatment for a pretrained ImageNet trunk under a randomly initialised head.

  • weight_decay (float) – Applied to weights only, never to norms or biases.

  • warmup_steps (int) – Optimiser steps of linear warmup from a third of the rate, before the per-epoch cosine. See petr_schedule().

forward(batch)[source]
Return type:

Dict[str, Tensor]

training_step(batch, batch_idx)[source]
Return type:

Tensor

validation_step(batch, batch_idx, dataloader_idx=0)[source]
Return type:

Tensor

on_validation_epoch_start()[source]
Return type:

None

on_validation_epoch_end()[source]

One val_loss over every validation dataset.

Validation runs one dataloader per dataset, and a key logged from several of them is a key Lightning refuses to reconcile. Accumulating here gives a single frame-weighted number for the checkpoint monitor to watch, while the per-dataset keys keep saying which dataset a number came from.

Return type:

None

static group_by_dataset(batch)[source]

Indices of the batch, grouped by which dataset each sample came from.

A batch from a single dataset produces one group keyed None, which costs nothing and keeps the code path the same either way.

Return type:

Dict[Optional[str], List[int]]

static sync_box_count(labels, device)[source]

Ground-truth box count across all ranks, reduced exactly once.

The loss divides by this, so every rank must agree. Reducing inside the per-dataset loop instead would have ranks calling all_reduce a different number of times whenever their batches hold different datasets, and the job would hang rather than fail.

Return type:

Tensor

parameter_groups()[source]

Four groups: backbone and head, each split into decayed and not.

Norm and bias parameters are excluded from weight decay because shrinking them toward zero changes what the layer computes rather than regularising it.

Return type:

List[Dict[str, Any]]

configure_optimizers()[source]
Return type:

Dict[str, Any]

predict_step(batch, batch_idx, dataloader_idx=0)[source]

Decoded detections for one batch, ready for py123d_detection.core.evaluation.

Return type:

List[Dict[str, Any]]