ModelSpec (Custom Model Support)
ModelSpec is an object that declares where and in what format a model's inputs and outputs appear.
MLdebugger reads this declaration to interpret predictions and ground truth, and records inference logs accordingly.
Even for custom or modified models that no preset covers, writing a single ModelSpec lets you use
MLdebugger across Tracer (training-time measurement), Logger (production monitoring), and
DataFilter (data selection).
New in v0.4.1
ModelSpec was added in v0.4.1. No subclassing and no method overrides are required —
you write 10 to 20 lines, once. A spec passed to a Tracer is stored, so from the second
call onward you no longer pass spec= at all.
Do you need a ModelSpec?
Your MLdebugger workflow branches on which of the following applies to your model.
| Condition | What you need to do |
|---|---|
| You use a preset-supported model as-is | No ModelSpec needed. Just pass the model to ObjectDetectionTracer and the like |
| You customized a preset, or use a model outside the presets | Write a ModelSpec declaring the model's inputs and outputs |
The presets are listed below. Models that match are detected automatically and work without spec=.
| Task | Presets |
|---|---|
| 2D object detection | Faster R-CNN / YOLO / YOLOX / SSD / CenterNet / DETR / KerasCV / TF Object Detection API |
| 3D object detection | BEVFormer / BEVFusion / CenterPoint / PointPillars / MonoCon |
| Classification | Any model (resolved from the final layer) |
from ml_debugger.training import ObjectDetectionTracer
# Presets (e.g. torchvision Faster R-CNN) are detected from the model
tracer = ObjectDetectionTracer(model, model_name="my_det", version_name="v1")
tracer(images, ground_truth, input_ids=[...], dataset_type="train")
Not sure? Run the inference first
If you cannot tell whether your model matches a preset, run inspect_model(model)
(AutoMode). Everything that can be inferred is inferred, and only the
items that cannot be decided come back as questions.
What you declare
A ModelSpec follows the model's own dataflow and consists of six fields.
You do not have to fill in all of them — declare only the parts that differ from the standard shape.
Invoke(inputs) + labels → postprocess(raw_output) → output
| Field | Type | What it declares | Page |
|---|---|---|---|
labels |
LabelSchema |
Class count and ground-truth structure | labels= |
inputs |
InputSchema |
Input shape (optional) | inputs= |
invoke |
Invoke |
How the model is called (defaults to model(input)) |
invoke= |
raw_output |
RawOutputSchema |
Shape and location of the model's raw output | raw_output= |
postprocess |
NMS / TopK / callable |
Raw output → final output | postprocess= |
output |
OutputSchema |
Shape of the final output | output= |
A model with several task outputs (e.g. a detector plus a classifier over one trunk)
declares one HeadSpec per head under heads= instead — inputs / invoke
stay shared, and the remaining fields move into each head (v0.4.2+).
Each *Schema wrapper is applied automatically when you pass a structure node directly
(output=S.BatchAxis[...] is the same as OutputSchema(structure=S.BatchAxis[...])).
The leaves (Coords / Score / LabelId, …) and structure nodes (Dict / BatchAxis, …) used to
write a structure all live in the schema namespace.
from ml_debugger.spec import ModelSpec, LabelSchema
from ml_debugger.spec import schema as S
The minimal flow
1. Declare
Mirror the model's output structure. The example below is a 2D detector that returns one
{"boxes", "scores", "labels"} dict per image.
from ml_debugger.spec import LabelSchema, ModelSpec
from ml_debugger.spec import schema as S
gt = S.BatchAxis[ # ground-truth structure
S.Dict(
boxes=S.DetAxis[S.Coords("xyxy", "pixel")],
labels=S.DetAxis[S.LabelId(0)],
)
]
spec = ModelSpec(
labels=LabelSchema(n_class=3, ground_truth=gt), # foreground classes + GT structure
output=S.BatchAxis[ # per-image list
S.Dict(
boxes=S.DetAxis[S.Coords("xyxy", "pixel")], # boxes: pixel xyxy
scores=S.DetAxis[S.Score("raw")], # confidence
labels=S.DetAxis[S.LabelId(0)], # class id (0-based)
)
],
)
2. Verify before you use it
If what you wrote disagrees with the real model, the inference log may not be recorded correctly. Verify once against the real model before you start recording.
report = spec.verify(model, sample_batch) # ground_truth=... is accepted too
report.raise_if_errors() # stop with an exception if there are errors
See Validation for details.
3. Pass it to a Tracer / Logger / DataFilter
A ModelSpec is task-independent. The same spec goes to any of the three classes.
from ml_debugger.training import ObjectDetectionTracer
tracer = ObjectDetectionTracer(model, spec=spec, model_name="my_det", version_name="v1")
tracer(images, ground_truth, input_ids=[...], dataset_type="train")
tracer.wait_for_save()
4. From the second call onward, spec= is optional
Once you pass spec=, the ModelSpec is stored against that (model_name, version_name).
After that, any of the three classes loads it automatically when the identifiers match — not just
the production Logger and the Active Learning DataFilter, but a second Tracer run as well.
from ml_debugger.monitoring import ObjectDetectionLogger
from ml_debugger.data_filter import ObjectDetectionDataFilter
# the stored ModelSpec is loaded automatically
tracer = ObjectDetectionTracer(model, model_name="my_det", version_name="v1")
logger = ObjectDetectionLogger(model, model_name="my_det", version_name="v1")
dfilter = ObjectDetectionDataFilter(model, model_name="my_det", version_name="v1")
Pass it again when the version_name changes
Storage is keyed on (model_name, version_name). When you start recording under a new version,
pass spec= once more on that first call.
Resolution order
MLdebugger resolves the ModelSpec in this order:
- An explicit
spec= - A stored
ModelSpec(bound earlier for the samemodel_name/version_name) - Preset auto-detection
- Automatic inference on the first call (AutoMode)
Even for a customized preset model, an explicit spec= always wins over the preset.
How deep is your customization?
How much you need to declare depends on how far your model departs from a preset. Identifying your level first narrows down which pages you need to read.
| Level | Case | What to declare |
|---|---|---|
| L0 | Preset as-is | Nothing |
| L1 | Only the output wrapping differs / a variant of a standard format | The output= structure only (a few to a dozen lines) |
| L2 | L1 + there is post-processing outside the model's return value | + postprocess= and raw_output= |
| L3 | The model's return value is not the final detections (custom post-process / query head) | + postprocess=<fn> or TopK, plus the structures of its input and output |
| L4 | Something cannot be expressed declaratively and needs one function | + a callable for that spot only (NMS(custom=fn) / S.Getter(fn) / OutputSchema(slice_with=fn)) |
Scope and limitations
| Task | Supported frameworks |
|---|---|
| Classification | PyTorch / TensorFlow |
| Object detection (2D) | PyTorch / TensorFlow |
| Object detection (3D) | PyTorch |
- A single post-processing stage only:
postprocess=accepts exactly one ofNMS/TopK/ a custom function. - 3D limitation: a layer hook (
LayerHook) combined with a generic NMS is not supported in 3D. Use anS.KeepIndexleaf,TopK, orNMS(custom=<selection fn>)instead. - For tasks outside the supported set, or models that need multiple post-processing stages, please contact support@adansons.ai.
How to read this section
| Page | Contents |
|---|---|
| AutoMode | Infer a ModelSpec from the model. Start here |
| labels= | Class count and ground-truth structure |
| inputs= | Input shape, models called with paths |
| invoke= | How inference is called |
| raw_output= | Raw-output shape and location (layer hooks) |
| postprocess= | NMS / TopK / custom post-processing |
| output= | Final output structure |
| heads= | Multi-head models (one HeadSpec per head) |
| Validation | Verifying before use, and how to read the findings |
| Additional Fields & Internal Features | additional_fields / target_layers |
| Cheat Sheet | Leaves, structure nodes, and fields at a glance |