Skip to content

labels= — Classes and Ground Truth

labels= is the only required field of a ModelSpec. It declares the class count and the ground-truth structure via LabelSchema(n_class=..., ground_truth=...).

from ml_debugger.spec import LabelSchema, GroundTruthSchema, ModelSpec
from ml_debugger.spec import schema as S

spec = ModelSpec(
    labels=LabelSchema(
        n_class=3,               # foreground classes (do not count a background channel)
        ground_truth=gt,         # GT structure (optional if used in production only)
    ),
    output=...,
)

LabelSchema fields

Field Type Contents
n_class int / NClassFrom The foreground class count (required). NClassFrom("attr.path") reads it from a model attribute
ground_truth GroundTruthSchema / structure node The ground-truth container structure. Optional

GroundTruthSchema fields

Field Type Contents
structure structure node The ground-truth container structure (required). Written with the same leaves as the model output (S.Coords / S.Coords3D / S.LabelId)

A structure node passed straight to ground_truth= becomes GroundTruthSchema(structure=...), so you rarely need to write GroundTruthSchema explicitly.

# these two mean the same thing
labels=LabelSchema(n_class=3, ground_truth=gt)
labels=LabelSchema(n_class=3, ground_truth=GroundTruthSchema(structure=gt))

When ground_truth= can be omitted

If the spec is only used in the production phase (Logger), where no GT flows, you can omit it. Declare it when you measure training data with a Tracer.


Ground-truth structure

Write the GT structure with the same leaves you use for the model output (S.Coords, S.LabelId). For the torchvision convention — a list of per-image {"boxes", "labels"} dicts:

gt = S.BatchAxis[                                    # the sample (image) axis
    S.Dict(
        boxes=S.DetAxis[S.Coords("xyxy", "pixel")],  # the detection (object) axis
        labels=S.DetAxis[S.LabelId(0)],
    )
]

spec = ModelSpec(
    labels=LabelSchema(n_class=3, ground_truth=gt),
    output=S.BatchAxis[
        S.Dict(
            boxes=S.DetAxis[S.Coords("xyxy", "pixel")],
            scores=S.DetAxis[S.Score("raw")],
            labels=S.DetAxis[S.LabelId(0)],
        )
    ],
)

When GT coordinates differ from the output

If the GT uses a format other than pixel xyxy (normalized cxcywh, for example), just declare that format on the ground_truth= leaf. GT coordinate conversion happens only through this declaration.

spec = ModelSpec(
    labels=LabelSchema(
        n_class=3,
        ground_truth=S.BatchAxis[S.Dict(
            boxes=S.DetAxis[S.Coords("cxcywh", "normalized")],   # GT is normalized cxcywh
            labels=S.DetAxis[S.LabelId(0)],
        )],
    ),
    output=S.BatchAxis[S.Dict(
        boxes=S.DetAxis[S.Coords("xyxy", "pixel")],              # output is pixel xyxy
        scores=S.DetAxis[S.Score("raw")],
        labels=S.DetAxis[S.LabelId(0)],
    )],
)

3D detection GT

For 3D, declare the box layout with S.Coords3D(layout=...) (see output=).

labels=LabelSchema(
    n_class=10,
    ground_truth=S.BatchAxis[                        # per-frame GT
        S.Dict(
            gt_boxes=S.DetAxis[S.Coords3D(layout="standard_7")],
            gt_labels=S.DetAxis[S.LabelId(0)],
        )
    ],
)

Classification GT

Classification GT is passed as a plain array of class ids, so you normally do not declare ground_truth= at all (the default is S.BatchAxis[S.LabelId(0)]).

spec = ModelSpec(
    labels=LabelSchema(n_class=5),
    output=S.ClassConfs(activation="raw"),
)

Class count (n_class)

n_class is the foreground class count. Do not count a background channel.

To read it from a model attribute, pass the attribute path to NClassFrom.

from ml_debugger.spec import NClassFrom

labels=LabelSchema(n_class=NClassFrom("fc.out_features"), ground_truth=gt)

Label base and background channel

The single most error-prone part of a ModelSpec is the combination of the class-id base and the position of the background channel.

Declaration What it expresses
S.LabelId(base) 0 if class ids start at 0, 1 if they start at 1
S.ClassConfs(background=...) Background channel position: "none" / "index0" / "last"

There are six combinations (the label values themselves are never rewritten).

LabelId(base=) ClassConfs(background=) Typical example
1 index0 Faster R-CNN / SSD (background first, 1-based labels)
0 none No background channel, 0-based
0 last Background last, 0-based
0 index0 Background first, 0-based
1 last Background last, 1-based
1 none No background channel, 1-based

1-based labels with no background channel

LabelId(1) together with background="none" emits a RuntimeWarning at construction time. The behavior is as specified — if that is what you meant, carry on.

Mistakes here are caught by validation

A wrong base or background position is detected against real data by the class-confs check group (Validation). When you see that warning, review these two first.


  • output= — the final output structure (uses the same leaves as GT)
  • Validation — the gt / class-confs / n-class check groups