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
|
Learning-rate factor at one optimiser step: PETR's and BEVFormer's published schedule. |
Classes
|
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
CosineAnnealingwithby_epoch=True: the rate is constant within an epoch and follows a cosine from 1 tomin_lr_ratioacross epochs. The firstwarmup_stepssteps ramp it linearly up fromwarmup_ratioof that. The thesis trained with exactly this.- Return type:
- 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:
LightningModuleBase for camera-based 3D detectors.
A subclass sets
self.backboneandself.headand implementsforward(). The head must return{"all_cls_scores": (L, B, Q, C), "all_bbox_preds": (L, B, Q, 10)}and provideloss(boxes, labels, predictions, ...)andget_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. Seepetr_schedule().
- on_validation_epoch_end()[source]¶
One
val_lossover 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:
- 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.
- 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