Training

Training fits the downstream model (an aggregator or a decoder, and a task head) to the features of a source. A config sets it in three sections, each holding the keywords of one callable: loaders those of soma.learn.loaders(), learner the name and params of the soma.learn.Learner, and trainer those of the soma.learn.Trainer. From Python, soma.fit() takes the same values.

Budget, optimizer and schedulers

loaders:
  batch_size: 1
learner:
  name: mil
  params:
    aggregator: {name: abmil}
    head: {name: binary_classification}
    optimizer:
      name: adamw
      params: {lr: 1.0e-4, weight_decay: 1.0e-5}
    scheduler:
      - {name: constant, params: {factor: 0.333, total_iters: 5}}
      - cosine
trainer:
  max_epochs: 50
  callbacks:
    - {name: checkpoint, params: {monitor: tune/auroc, mode: max}}
    - {name: early_stopping, params: {monitor: tune/auroc, mode: max, patience: 10}}

loaders holds batch_size, num_workers, pin_memory, persistent_workers and the optional sampler and augment leaves. learner.params holds the model’s aggregator or decoder leaf and its head leaf, the optimizer and scheduler leaves, the feature normalization and projection, and the keywords of a custom learner. The mil learner also takes checkpoint_cox_window, which saves GPU memory in a Cox window (see Survival). learner.name is mil for the set shape and dense for the grid shape; a registered or dotted soma.learn.Learner subclass owns custom step logic (see A custom learner).

Choose one budget: trainer.max_epochs counts passes through the training loader; trainer.max_steps counts optimizer updates after accumulate_grad_batches, and replaces the epoch budget when set. A cosine scheduler anneals over the budget, per epoch or per update.

Every component is a {name, params} leaf whose name is a registry key or a dotted import path; a bare name keeps the component’s default params. The optimizer defaults to Adam (lr 1e-4, weight_decay 1e-5) when the learner names none. scheduler may be one leaf or a list, each stepped at its own interval (epoch unless set); a warm-up is a constant leaf. With no scheduler the learning rate stays constant.

A model with no trainable parameter, such as one with the attention_map detection decoder, is not fitted: the trainer returns it as built, with an empty training history. It still predicts on the trainer’s device.

Checkpoint selection and early stopping

The checkpoint callback selects the weights: selection: best (the default) keeps the epoch that is best on monitor in direction mode (default: minimize tune/loss; tune/<metric> selects that tune metric), and selection: last keeps the final weights; it refuses an early_stopping callback and needs trainer.val_every_n_epochs: 1. early_stopping stops after patience tune evaluations without improvement; leave it out to train the full budget.

trainer.val_every_n_epochs: N evaluates the tune split after every N-th epoch and after the final epoch, for runs where tune evaluation dominates the epoch cost; selection and patience then count tune evaluations, not epochs. Tune metrics are always recorded for diagnostics.

Monitors

A monitor names a value of the tune evaluation by its full name, with the tune/ prefix (see Logged values). These are valid monitors:

  • tune/loss, the default, and tune/<metric> for a metric the head reports: values over the whole tune split.

  • tune/mean_over_<column>/loss and tune/mean_over_<column>/<metric>: the mean over the groups of a column that evaluation.subgroups names (see Subgroup metrics).

The plan refuses any other name before the run starts:

  • A bare name, such as loss or auroc. The error names the tune/ form.

  • A train/ value of the train pass (see The train pass). These values show the gap between train and tune; selecting on them would select on the training data.

  • The value of one group, such as tune/center=A/auroc. A fold’s tune split can lack the group, and the monitor then does not exist. The error names the mean over the column’s groups instead.

  • A mean over a column that evaluation.subgroups does not name, or a metric the head does not report (checked for the mil and dense learners; with a custom learner, an unknown key fails at the first evaluation).

A monitor that is NaN at an evaluated epoch stops the run with an error, in early_stopping and in a checkpoint with selection: best, because it can never improve. Under selection: last, the monitor is only logged. AUROC, AUPRC and the C-index are NaN on a tune split with one class or no comparable pairs, a Cox loss is NaN on a tune split without an event, and a mean over groups is NaN when no group has a value.

Monitor names from soma 2.4.0 and earlier. Monitor names now carry the tune/ prefix. Replace monitor: loss with monitor: tune/loss, and monitor: auroc with monitor: tune/auroc. A bare name is refused, not mapped. A run made before the rename cannot be resumed.

Logged values

While a fold trains, a summary panel shows the epoch, the training loss, the learning rate, the pooled tune values, the monitors, patience, the trainable parameter count, timing and, for cross-validation, the active fold. Each fold writes every logged value to training_history.json, one row per epoch: epoch, train_loss, lr, elapsed_seconds, then one column per value of the evaluation, null on an epoch without an evaluation.

Each name starts with the split. A value of one group, or the mean over a column’s groups, names it after the split:

Name

Value

tune/loss, tune/<metric>

The loss and each metric of the head, pooled over the whole tune split.

tune/<column>=<group>/loss, tune/<column>=<group>/<metric>

The same over the samples of one group of an evaluation.subgroups column, for example tune/center=A/auroc.

tune/mean_over_<column>/loss, tune/mean_over_<column>/<metric>

The unweighted mean of the column’s group values.

tune/mean_over_<column>/<name>_groups

The number of groups in that mean.

train/loss, train/<metric>, train/<column>=<group>/..., train/mean_over_<column>/...

The same values over the train split, with trainer.eval_train: true (see The train pass).

Subgroup metrics explains the group values and their mean. The panel shows the pooled values and the monitors, and train/loss when the train pass is on. The values of each group go to training_history.json only.

With the mil learner, tune/loss is the head’s loss computed once over the whole tune split, not a mean of per-batch losses. The value does not depend on the batch size. With the dense learner, it is the mean of the batch losses.

train_loss is not an evaluation of the train split: see The train pass for how it differs from train/loss.

The train pass

trainer.eval_train: true also scores the train split at each tune evaluation, to show the gap between train and tune:

trainer:
  eval_train: true

At each epoch that evaluates tune, the trainer runs the model in eval mode over every train sample, in split order and in batches like tune. It logs train/loss and train/<metric> and, with evaluation.subgroups, train/<column>=<group>/... and train/mean_over_<column>/.... These are the model’s values at the end of the epoch, computed as the tune values are, so train/auroc and tune/auroc compare directly.

train_loss is not comparable. It is the mean of the losses of the epoch’s training batches, computed in train mode (dropout on) while the weights change through the epoch, and it includes any auxiliary loss of the aggregator. For a Cox head, it is computed over the event-balanced training batches, while train/loss is the partial-likelihood loss over the whole train split.

The pass changes nothing in the training: train_loss, every tune/ value and the selected epoch are the same with it or without it. A train/ value is never a monitor (see Monitors).

The pass costs one more forward pass over the train split at each evaluated epoch, about the time of a tune evaluation scaled to the size of the train split. It is for the set shape, which reads cached features, so this is cheap: no encoder runs. With live encoding, the same pass would encode every train tile again. The grid shape refuses eval_train. To run the pass less often, raise trainer.val_every_n_epochs.

Without a tune loader, or with an empty one (soma.fit without tune, or a tune split where no sample has features), nothing is evaluated, so the pass does not run and the trainer logs a warning. When cohort.allow_missing_tune reuses train as tune, the pass runs. eval_train is not part of the experiment identity, but a resume refuses a change to it, as it does for trainer.device.

The tune split

By default, training requires a tune split. The cohort section offers two explicit alternatives:

  • cohort.tune_is_test: true uses one held-out split for both checkpoint selection and test reporting. Provide either tune or test, not both. Use this only when reproducing a benchmark protocol that selects on its reported cohort.

  • cohort.allow_missing_tune: true reuses train as tune when tune is absent and emits a warning. This controls the diagnostic split independently of checkpoint selection.

Feature normalization

learner.params.normalization applies a feature adaptor before the trainable model, so encoders with different activation scales can share training settings.

learner:
  params:
    normalization:
      method: zscore   # none | zscore | l2 | layernorm
      eps: 1.0e-6

zscore estimates per-feature center and scale from the feature rows of the train split alone; held-out samples use the fitted transform without updating it. eps floors the scale of constant or near-constant channels and must be finite and positive. l2 and layernorm are stateless. none adds no adaptor and requires eps to keep its default.

The adaptor runs on cached tile-encoder MIL bags, hierarchical stores (one (M, P, D) grid of tiles per sample, read grouped by hipt and flat by any other aggregator), slide-encoder embeddings and single-encoder dense grids; other paths refuse an active transform. On a hierarchical store, the adaptor is fitted on the stored tiles of the train samples, so the fit is the same as on those tiles stored as a flat bag. The padding added when a batch is collated never enters the fit. A composite encoder normalizes each member’s block before concatenation instead (member_norm, see Composite encoders).

Feature projection

learner.params.projection maps frozen features to a common width without labels. With the same downstream input width, encoders can be compared without the aggregator’s capacity changing with their embedding widths.

learner:
  params:
    projection:
      method: pca      # none | pca | random
      target_dim: 512  # required when method != none
      seed: 0          # configurable only for random

Projection follows normalization; either can be enabled alone.

pca fits centered principal components on the train split alone, without whitening, and fixes each component’s sign by making its largest-magnitude entry positive. target_dim must not exceed the encoder dimension or the number of training feature rows; the adaptor refuses either before training starts.

random uses a fixed Gaussian matrix scaled by 1/sqrt(target_dim). It can reduce or expand the input while approximately preserving inner products and distances. Its seed is derived from the configured seed, the encoder identity and the dimensions, without touching the global random state. pca requires the default seed. The aggregator and head take target_dim as their input width when a projection is active.

Both stages are buffers of the model, so the fold’s bundle stores the fitted transform and evaluation reuses it. Neither changes the feature cache. The plan spells both out (method: none when off), so they are part of the experiment identity. A fold with an active transform also writes feature_adapter.json; see Run outputs.

Live re-encoding and augmentation

Segmentation and detection read cached feature grids by default (features.kind: cached): the frozen encoder runs once and every epoch reuses its output. With features.kind: live, soma re-encodes each pre-cropped tile through the frozen encoder at every step instead, so pixel augmentation reaches the features. This costs one encoder forward pass per tile and step.

features:
  kind: live
  encoder: {name: uni2}
loaders:
  augment:
    name: segmentation
    params:
      horizontal_flip: 0.5
      vertical_flip: 0.5
      rotation_degrees: 90
      crop_scale: [0.08, 1.0]

loaders.augment is null by default. It is a {name, params} leaf: the name is a registry key (segmentation is soma.learn.SegmentationAugment) or a dotted import path, and params go to its constructor. It applies to the train split only. Geometric operations transform the image and its label mask together (the mask with nearest-neighbour resampling); photometric operations change the image only. Pixels that a rotation or translation brings into the tile get ignore_index in the mask, so they carry no supervision. crop_scale takes a random crop of that area fraction and resizes it to the tile size before the other operations. For detection, the points move with the image: a point that leaves the tile is dropped, and the ignore mask moves like a label mask, so the pixels brought into the tile are ignored.

Live mode has these limits:

  • loaders.augment requires features.kind: live; cached grids cannot be augmented.

  • Live mode reads pre-cropped tiles only. It does not support annotation-sampled whole slides, composite encoders, an active normalization or projection, or a loaders.sampler.

Training from Python

soma.fit() trains a learner, or a bare model wrapped in its shape’s learner. It installs a Checkpoint and an EarlyStopping built from monitor, mode and patience (patience=None installs none), passes its other keywords to the soma.learn.Trainer, and returns a soma.FitResult: the learner in its selected state, the history frame and the selected epoch. soma.learn.loaders() builds one fold’s loaders from a source and a cohort: train shuffles, tune and each of tests keep split order.

fold = soma.learn.loaders(source, cohort, head=model.task_head, fold=0, batch_size=1)
result = soma.fit(model, fold.train, fold.tune, max_epochs=50, monitor="tune/auroc", mode="max")
result.selected_epoch, result.history

tune_groups adds the subgroup values to each evaluation. It maps each column to the group of every tune sample: {column: {sample_id: group}}. With eval_train=True, pass train_eval, a second loader over the train samples that does not shuffle, and train_groups for its groups:

from torch.utils.data import DataLoader


def groups(split):
    return {"center": cohort.targets(split)["center"].dropna().astype(str).to_dict()}


train_eval = DataLoader(fold.train.dataset, batch_size=1, collate_fn=fold.train.collate_fn)
result = soma.fit(
    model, fold.train, fold.tune, max_epochs=50,
    monitor="tune/mean_over_center/auroc", mode="max", tune_groups=groups("tune"),
    eval_train=True, train_eval=train_eval, train_groups=groups("train"),
)

fit refuses the training loader itself as train_eval: a second pass over it would change the training’s shuffle. The pass keeps the training’s random state, but not the state that a dataset keeps for itself, so a dataset that draws random numbers needs its own instance for train_eval.

Section 3 of the getting-started notebook, the same run from the verbs, runs a whole fold this way; Evaluation covers what follows fit.

The grid shape

The same verbs train a decoder over feature grids, with three differences:

  • The source is a GridSource of cached grids, or a TileImageSource of pixels that the model re-encodes.

  • soma.learn.loaders() needs targets=: a LabelMapSource for a segmentation head or a PointSource for a detection head. The head binds the grid geometry when it is built, and the loaders check that every sample matches it.

  • The model is a soma.models.DenseModel, trained by soma.learn.DenseLearner. With an encoder stage it reads pixels; without one it reads cached grids.

soma.extract.live_encoder() wraps a slide2vec encoder as a parameter-free soma.extract.FrozenEncoder stage, so the foundation model never enters a checkpoint or an optimizer. preprocess=stage.preprocessor() runs the encoder’s preprocessing in the loader workers, after augmentation; augment is refused for cached grids:

from soma.models.decoders.registry import build_decoder_for_grid

stage = soma.extract.live_encoder("uni2", target_size=448, spacing_um=0.5, window=224)
images = soma.data.TileImageSource.from_manifest(soma.ImageManifest.from_csv("dataset.csv"), size=448, spacing_um=0.5)
labels = soma.data.LabelMapSource.from_manifests(soma.AnnotationManifest.from_csv("dataset.csv"), size=(448, 448))
head = soma.models.SegmentationHead(num_classes=3, geometry=stage.geometry)
decoder = build_decoder_for_grid(
    "lightweight_conv", {}, geometry=stage.geometry, input_dim=stage.feature_dim, num_classes=3
)
model = soma.models.DenseModel(decoder, head, encoder=stage)
fold = soma.learn.loaders(
    images, cohort, head=head, targets=labels, batch_size=8,
    preprocess=stage.preprocessor(),
    augment=soma.learn.SegmentationAugment(horizontal_flip=0.5, vertical_flip=0.5),
)
result = soma.fit(model, fold.train, fold.tune, max_epochs=50)

soma.testing.FakeEncoder is a weight-free stage with the same contract, for trying the grid flow without downloading model weights.

Watching a managed run

A fold writes training_history.json only after it is trained and scored. To read each epoch while a Pipeline run trains, pass a callback_factory. It returns runtime observers: Trainer callbacks that watch the fold.

from soma import Pipeline
from soma.pipeline import FoldContext


class EpochPrinter:
    def __init__(self, context: FoldContext):
        self.context = context

    def on_epoch_end(self, trainer, learner):
        row = trainer.history.rows[-1]
        print(self.context.fold, trainer.epoch, row["train_loss"], row.get("tune/loss"))


result = Pipeline(config, callback_factory=lambda context: [EpochPrinter(context)]).run()

Runtime observers and config callbacks. trainer.callbacks in the config is the selection policy (checkpoint, early_stopping). It is part of config.yaml, plan.yaml and the experiment identity. A runtime observer is a Python input, like source. It is not part of config.yaml, plan.yaml, the experiment identity, the component record in run.yaml or the resume check. A run started with observers resumes without them, and the other way round. The CLI has no option for them.

When the factory is called. run() calls callback_factory(context) once for each fold it trains, before it seeds the fold. Building observers therefore cannot change the fold’s random state. The factory is not called for a fold that is already complete on resume, or for a fold outside run.folds. A fold that was not complete restarts from epoch 0 on resume, so its new observers see the epochs from 0 again. A representation run (evaluation.representation) trains no fold and never calls the factory. A model with no trainable parameter is not fitted, so its observers see no epoch.

The context is a frozen soma.pipeline.FoldContext:

Field

Value

run_id, run_dir

The run’s id and directory.

fold_dir

The fold’s directory. With a single fold, it is run_dir.

fold, num_folds

The fold index (from 0) and the number of folds in the splits.

seed

The base seed, run.seed. The fold trains under soma.seed_everything(seed, fold=fold), so its effective seed is seed + fold.

Order and hooks. The observers run after the config’s callbacks and the progress panel. History is always the first callback, so in on_epoch_end trainer.history.rows[-1] is the current epoch’s row: the same row that training_history.json writes at the end of the fold. An epoch without a tune evaluation has only epoch, train_loss, lr and elapsed_seconds in its row, where the file writes null for the evaluation values. Observers use the fit-callback hooks (see Callbacks on fit and on predict); there is no other event.

The observer contract. An observer may read these Trainer attributes: epoch, total_epochs, train_loss, lr, optimizer_steps, summary (the epoch’s evaluation values under the names of Logged values, or None on an epoch without a tune evaluation) and history.rows. The running training values are train_loss and lr. An observer must not set trainer.should_stop, change the learner or the optimizer, or draw from the global random state (Python random, NumPy or torch). An observer that follows these rules changes nothing in the run: its predictions, metrics and bundle/weights.safetensors are byte-identical to the same run without it.

Failures. soma does not catch an observer’s errors. An exception from the factory or from an observer hook fails the run like any callback error. During the fit, every callback’s on_exception runs first. run.yaml then records status: failed and the error. To make your telemetry best-effort, catch the errors inside your own observer.

Reference

class soma.config.LoadersConfig(batch_size=1, num_workers=0, pin_memory=False, persistent_workers=False, sampler=None, augment=None)

Bases: Editable

loaders: soma.learn.loaders()’ keywords. sampler is a training-batch sampler leaf (cached segmentation ROI strategies); augment augments a live grid run’s train split.

class soma.config.LearnerConfig(name='mil', params=<factory>)

Bases: Component

learner: the Learner and the model it trains.

params holds the model’s leaves (aggregator for the set shape, decoder for the grid shape, head with its targets: target key -> cohort column), the feature adaptor (normalization, projection), the optimizer leaf, the scheduler leaf or list of leaves, and any other keyword of the learner class.

class soma.config.TrainerConfig(max_epochs=50, max_steps=None, accumulate_grad_batches=1, val_every_n_epochs=1, device='auto', precision='fp32', eval_train=False, callbacks=<factory>)

Bases: Editable

trainer: Trainer’s keywords. A step budget (max_steps) replaces the epoch budget; callbacks are the selection policy (checkpoint, early_stopping; leave the latter out to train the whole budget). eval_train also scores the train split in eval mode at every tune evaluation (train/* diagnostics, never a monitor; set shape only).

class soma.learn.SegmentationAugment(*, crop_scale=None, horizontal_flip=0.0, vertical_flip=0.0, rotation_degrees=0.0, translate=0.0, scale=0.0, brightness=0.0, contrast=0.0, saturation=0.0, hue=0.0, ignore_index=255, target_size=None)

Bases: object

Joint torchvision-v2 image + label-map augmentation.

Geometric ops transform the pixels and the label map together (the map resampled nearest-neighbour); photometric ops touch the pixels only. Out-of-canvas affine pixels fill the image with 0 and the label map with ignore_index, so synthesised borders carry no supervision. An optional RandomResizedCrop to target_size runs first, so images of any size reach the encoder at the supervision size.

Parameters:
  • crop_scale (tuple[float, float] | None) – RandomResizedCrop area range (None = no crop); needs target_size.

  • vertical_flip (horizontal_flip /) – Flip probabilities.

  • rotation_degrees (float) – RandomAffine symmetric rotation range (±value).

  • translate (float) – RandomAffine max translation fraction (both axes).

  • scale (float) – RandomAffine scale jitter, scale ∈ (1 - value, 1 + value).

  • hue (brightness / contrast / saturation /) – ColorJitter strengths.

  • ignore_index (int) – Label-map fill for synthesised pixels.

  • target_size (int | tuple[int, int] | None) – (H, W) output of crop_scale.

A Points target (in the pixel frame) rides as tv_tensors.KeyPoints: its points move with the pixels, points that leave the canvas are dropped (never clamped onto the edge), and its ignore raster moves like a label map, filled with 255 outside the canvas. Points without an ignore raster get one when the transform synthesises border pixels, so those carry no supervision.

class soma.learn.GridBatch(features, pixels, targets, sample_ids)

Bases: object

One collated grid batch.

features (B, D, h, w) are cached token grids; pixels (B, 3, H, W) are image pixels. Exactly one of them is the model’s input: features on the cached path, pixels on the live path, where the model’s encoder stage turns them into grids. An image-prior decoder reads pixels as a named side input, so a cached batch for such a decoder carries both. targets holds supervision only: {"mask": (B, H, W) long} or {"points": (B, K, 3) level-0 (x, y, class) NaN-padded, "ignore": (B, H, W) bool} (ignore absent when no sample has an ignore mask).

class soma.models.DenseModel(decoder, head, *, encoder=None, adaptor=None)

Bases: Module

The grid-shape model: pixels -> encoder -> adaptor -> decoder -> head.

Every stage but the decoder and the head is optional. Without an encoder the model reads cached feature grids (B, d, h, w); with one (a parameter-free FrozenEncoder, or any stage with the same contract) it re-encodes pixels every forward, so augmentation reaches the features. The adaptor (a fitted FeatureAdaptor: normalisation, projection) transforms the grid channel-axis before the decoder. The head resizes and crops the decoder’s logits to the supervision frame.

An image-prior decoder (consumes_image) also receives the batch’s raw uint8 pixels at the mask size, as [0, 1] floats. On the live path those pixels must reach the model unpreprocessed (the encoder stage then preprocesses them itself).

Parameters:
  • decoder (Decoder) – Maps (B, d, h, w) to (B, C, h', w') logits, built against the adaptor’s output width.

  • head (TaskHead) – A grid head (segmentation or detection) bound to the run’s geometry.

  • encoder (Module | None) – Optional frozen encoder stage over pixels.

  • adaptor (Module | None) – Optional channel-axis feature adaptor.

soma.extract.live_encoder(encoder, *, target_size, spacing_um, window=None, overlap=0.0, feature_kind='patch_features', precision=None, output_variant=None, allow_non_recommended_settings=False, tolerance=0.05, backend='auto', pad_mode='reflect', image_pad_value=None, attention_blocks=(-1,), attention_include_registers=False, num_gpus=None, device=None)

Prepare a slide2vec dense encoder for live re-encoding and wrap it as a stage.

Parameters:
  • encoder (str) – slide2vec preset name (e.g. "uni2").

  • target_size (int) – Supervision tile side in pixels; the stage pads it to the encoder’s patch multiple.

  • spacing_um (float) – µm/px the pixels are read at.

  • overlap (window /) – Sliding-window encoding of a tile larger than the encoder’s input (None = one forward over the whole tile).

  • feature_kind (str) – slide2vec dense feature kind (patch_features, patch_features_prenorm, cls_attention).

  • precision (str | None) – Encoder precision; None resolves the preset’s recommendation.

  • image_pad_value (pad_mode /) – How the tile is padded to the encoder’s patch multiple, as DenseOptions states it for the cached path.

  • attention_include_registers (attention_blocks /) – The cls_attention recipe.

Return type:

FrozenEncoder

The stage probe-encodes one blank tile to learn its feature width and to check that the kit’s grid matches its declared geometry.

class soma.data.TileImageSource(entries, *, size, spacing_um=None, spacing_policy='strict', backend='auto', tolerance=0.05)

Bases: object

Pixels of pre-cropped tiles (or whole small images), one (3, H, W) uint8 per id.

Parameters:
  • entries (Mapping[str, ImageEntry] | Sequence[ImageEntry]) – The images, keyed by sample id (an ImageManifest’s entries).

  • size (int | tuple[int, int]) – The declared (H, W) (or side) the encoder stage expects; images larger than it reach that size only through an augmentation crop.

  • spacing_um (float | None) – µm/px to read pyramidal images at, and flat ones whose declared spacing_at_level_0 lies outside tolerance of it; None reads every image as stored.

  • spacing_policy (str) – "strict" reads every image at spacing_um; "native_if_coarser" reads one whose declared spacing_at_level_0 is coarser than it at that native spacing (no upsampling).

  • backend (str) – hs2p backend for spacing-aware reads.

  • tolerance (float) – hs2p spacing tolerance: a level within it is read without resampling.