callbacks
Lightning callbacks for batch norm momentum scheduling and metric logging.
Classes:
-
BNMomentumScheduler–Exponentially decay BatchNorm momentum over training epochs.
-
MetricCallback–Accumulate and log a torchmetrics metric over a validation/test epoch.
BNMomentumScheduler
¶
BNMomentumScheduler(
bn_momentum_init: float = 0.5,
bn_decay_rate: float = 0.5,
bn_decay_step: int = 20,
bn_momentum_clip: float = 0.001,
)
Bases: Callback
Exponentially decay BatchNorm momentum over training epochs.
Reference implementation:
facebookresearch/votenet (train.py).
At the start of each training epoch, every nn.BatchNorm* module in pl_module.model has its
momentum set to
with \(m_0\) the initial momentum, \(\gamma\) the decay rate, \(s\) the decay step (epochs) and \(m_\text{clip}\) the floor.
Parameters:
-
bn_momentum_init(float, default:0.5) –Initial BatchNorm momentum \(m_0\).
-
bn_decay_rate(float, default:0.5) –Per-step multiplicative decay \(\gamma\).
-
bn_decay_step(int, default:20) –Number of epochs between decay steps \(s\).
-
bn_momentum_clip(float, default:0.001) –Lower bound on the momentum \(m_\text{clip}\).
Methods:
-
on_train_epoch_start–Set the decayed momentum on every BatchNorm module of the model.
on_train_epoch_start
¶
Set the decayed momentum on every BatchNorm module of the model.
MetricCallback
¶
MetricCallback(
metric: Metric,
name: str,
*,
stages: Sequence[str] = ("val", "test"),
preds_key: str = "preds",
target_key: str = "target",
batch_key: Optional[str] = None,
prog_bar: bool = True,
)
Bases: Callback
Accumulate and log a torchmetrics metric over a validation/test epoch.
Model- and task-agnostic: the LightningModule's validation_step / test_step returns a
{preds_key: ..., target_key: ...} dict (the repo's Lit* modules do), and this callback updates a
torchmetrics Metric with it each batch, logging the epoch value as {stage}/{name}. List one per
metric to plug accuracy, mIoU, mAP, ... onto any model.
metric is a ready torchmetrics Metric whose num_classes and ignore_index the caller sets to match the
model and the criterion, so the callback holds it as-is and stays model-agnostic.
A step output may carry extra entries beyond preds_key / target_key (the Lit* modules' metric_input_keys
passthrough); entries matching a parameter name of the metric's update signature (inspected once at construction)
are forwarded as keyword arguments and the rest are ignored, so each metric declares the inputs it consumes.
A metric whose compute returns a dict (e.g. MeanAveragePrecision3D returning mAP@0.25 / mAP@0.5)
is logged one entry per key as {stage}/{key}; a scalar metric is logged as {stage}/{name}.
Parameters:
-
metric(Metric) –A torchmetrics
Metric, e.g.JaccardIndex(task="multiclass", num_classes=..., ignore_index=...). -
name(str) –Metric name; logged as
{stage}/{name}(ignored for dict-valued metrics, whose keys name themselves). -
stages(Sequence[str], default:('val', 'test')) –Stages to score; each listed stage's
*_stepmust returnpreds_key/target_key. -
preds_key(str, default:'preds') –Key in the step output holding predictions (logits, probabilities, labels, or detections).
-
target_key(str, default:'target') –Key in the step output holding the targets.
-
batch_key(Optional[str], default:None) –Optional key in the step output holding the per-point shape index, forwarded as a third positional argument to
metric.update(packed multi-shape metrics likeInstancePartMeanIoUneed it). LeaveNonefor two-argument metrics. -
prog_bar(bool, default:True) –Whether to show the metric on the progress bar.
Methods:
-
on_validation_epoch_start–Reset the metric when
valis scored. -
on_validation_batch_end–Update the metric with the step output when
valis scored. -
on_validation_epoch_end–Compute and log the metric when
valis scored. -
on_test_epoch_start–Reset the metric when
testis scored. -
on_test_batch_end–Update the metric with the step output when
testis scored. -
on_test_epoch_end–Compute and log the metric when
testis scored.
on_validation_epoch_start
¶
Reset the metric when val is scored.
on_validation_batch_end
¶
on_validation_batch_end(
trainer: Trainer,
pl_module: LightningModule,
outputs: Any,
batch: Dict[str, Any],
batch_idx: int,
dataloader_idx: int = 0,
) -> None
Update the metric with the step output when val is scored.
on_validation_epoch_end
¶
Compute and log the metric when val is scored.
on_test_epoch_start
¶
Reset the metric when test is scored.
on_test_batch_end
¶
on_test_batch_end(
trainer: Trainer,
pl_module: LightningModule,
outputs: Any,
batch: Dict[str, Any],
batch_idx: int,
dataloader_idx: int = 0,
) -> None
Update the metric with the step output when test is scored.
on_test_epoch_end
¶
Compute and log the metric when test is scored.