Losses & metrics#
Losses and metrics are both criteria — subclasses of
konfai.metric.measure.Criterion in konfai/metric/measure.py. You attach them
to a named model output and one or more target dataset groups, under
outputs_criterions: (training) or metrics: (evaluation). Bare names resolve
inside konfai.metric.measure; you can also point at any library, e.g.
torch:nn:L1Loss or monai.losses:DiceLoss.
Loss vs metric — how KonfAI actually decides#
Important
Whether a criterion is back-propagated is decided by the is_loss: flag in the
config, not by the Python return type. is_loss: true adds the returned tensor
to the training loss; is_loss: false detaches it and only logs it. In an
Evaluation.yml, every criterion is a metric.
The return shape controls logging: a criterion may return a bare Tensor,
or a tuple (tensor, scalar_or_dict). The tuple form is what lets Dice, TRE,
etc. log clean per-label values while still driving the gradient with the tensor.
So most pixelwise criteria are dual-use: the same class is a training loss or
a logged metric depending on is_loss.
Attaching a criterion (training)#
outputs_criterions:
UNetBlock_0:Head:Conv: # named model output (dotted path; ':' or '.')
targets_criterions:
SEG: # target group ("CT;MASK" to add a mask)
criterions_loader:
CrossEntropyLoss: # criterion name (bare → konfai.metric.measure)
is_loss: true # true → back-propagated; false → metric only
schedulers:
Constant: { nb_step: 0, value: 1 } # weight schedule
group: 0 # loss/optimizer group (e.g. GAN G vs D)
start: 0 # first active iteration
stop: None # last active iteration (None = never)
accumulation: false
# any remaining keys are forwarded to the criterion's constructor:
reduction: mean
The reserved keys (is_loss, group, start, stop, accumulation, plus the
schedulers: subtree) are consumed by KonfAI; all other keys are the
criterion’s own constructor arguments. For evaluation the shape is the same,
without is_loss/schedulers/group:
metrics:
sCT:
targets_criterions:
CT;MASK: # ';' joins target + mask into one masked metric
criterions_loader:
MAE: { reduction: mean }
PSNR: { dynamic_range: None }
SSIM: { dynamic_range: None }
Pixelwise / regression#
All subclass MaskedLoss and return (Tensor, float) (dual-use). Extra target
groups act as a mask.
Name |
Purpose |
Key args (defaults) |
|---|---|---|
|
Masked mean-squared error. |
|
|
Masked mean-absolute error. |
|
|
Signed mean error |
— |
|
Peak SNR over the mask. Default |
|
|
MAE that also returns a voxelwise L1 error map (a 3-tuple, for a save-map consumer). |
|
Segmentation / classification#
Name |
Role |
Purpose |
Key args (defaults) |
|---|---|---|---|
|
|
Soft Dice per label; loss |
|
|
|
Wraps |
|
|
|
Multi-class focal loss. Note: |
|
|
|
Running classification accuracy (accumulates across the run). |
— |
|
3-tuple |
Dice + voxelwise error map (for a save-map consumer). |
|
Adversarial / style#
Name |
Purpose |
Key args (defaults) |
|---|---|---|
|
|
|
|
LSGAN-style MSE against a constant target. |
|
|
|
— |
|
Gram-matrix (style) loss. |
— |
|
Feature-space perceptual loss over a pretrained KonfAI |
|
Registration / distributions#
Name |
Role |
Purpose |
Key args (defaults) |
|---|---|---|---|
|
|
Target Registration Error between predicted/target landmark coordinates. |
— |
|
|
Image-gradient smoothness loss (2D/3D auto); regulariser, or gradient-difference if a target is given. |
— |
|
|
Parzen-window Gaussian mutual information (returns |
|
|
|
VAE KL term. Rewires the graph on init, inserting a |
|
Uncertainty / bookkeeping#
Name |
Role |
Purpose |
Key args |
|---|---|---|---|
|
|
Channel-wise variance mean (ensemble/uncertainty). |
|
|
|
Mean of the output tensor. |
|
|
|
|
— |
|
|
L1 between two representations + variance-collapse regulariser. |
— |
IMPACT feature-based criteria#
These download TorchScript feature extractors from Hugging Face at construction
(hf_hub_download), so they need network access; the sanity check uses GPU 0.
All are CriterionWithAttribute and consume per-group Attribute statistics.
Name |
Purpose |
Key args (defaults) |
|---|---|---|
|
Feature-space registration loss over the layers of a TorchScript model. |
|
|
Content (MSE) + style (Gram) perceptual synthesis loss over two TorchScript models. |
|
|
SAM2-feature perceptual criterion (2D only). |
|
Optional-dependency criteria#
Imported lazily; a missing package raises a MeasureError with an install hint.
Name |
Extra |
Purpose |
Key args |
|---|---|---|---|
|
|
Masked structural similarity. Default |
|
|
|
Learned perceptual similarity (AlexNet by default), tiled over patches. |
|
|
|
Fréchet Inception Distance (InceptionV3). |
— |
Note
IMPACT* and SAM_Perceptual pin the sanity check to GPU 0; LPIPS/FID also
run on CUDA device 0.
Next steps#
Schedulers — the weight schedulers used in the
schedulers:subtreeModel graph and output naming — the named outputs criteria attach to
Extension points — write your own
Criterion