Expand description
Model configuration discovery.
Finds what a model needs to be run and decoded — its output schema, class labels and input preprocessing — from whatever the model carries, and records where each value came from.
§Sources
Sources are read in this order; each fills only what the ones before it
left unset, so edgefirst.json wins over a Hugging Face config field by
field:
edgefirst.jsonembedded in the model (itsinput.preprocessing, then the legacyvalidation.normalization/validation.preprocessing);edgefirst.jsonandlabels.txtbeside the model;- Hugging Face
config.json+preprocessor_config.jsonembedded in the model; - the same files beside the model (the
optimumexport layout); - Ultralytics export metadata (
infer_ultralytics_schema); - Hugging Face input/output names (
pixel_values→logits), as guesses; - the documented defaults.
ModelOverrides apply last.
§Embedding conventions
| Container | Where | Names |
|---|---|---|
| ONNX | ModelProto.metadata_props | edgefirst (legacy edgefirst.json), labels, hf_config, hf_preprocessor_config |
| TFLite, HEF, DVM, TensorRT, any file | appended ZIP (TFLite associated files) | edgefirst.json, labels.txt, config.json, preprocessor_config.json |
| any | the model’s directory | as for ZIP |
ONNX models are read by walking the top-level protobuf fields and seeking
past the graph; ZIP trailers need the model-files feature (off by
default). A config.json is used only when it looks like a Hugging Face
config (a string model_type and architectures or
transformers_version); any other config.json is skipped and noted.
§Class labels
Every label source — edgefirst.json dataset.classes, labels.txt and
the ONNX labels key, Hugging Face id2label / label2id, Ultralytics
names and the labels override — has leading and trailing whitespace
removed from each name (str::trim), so ADE20K’s "bed " resolves to
"bed". Whitespace inside a name ("chest of drawers") is kept.
Discovered class names never fail resolution: a source whose names are
unusable (a name empty after trimming, a malformed id2label, or a count
that disagrees with num_labels or the output) is ignored with a warning
and a note naming the source and the index (id2label[7]), and the next
source supplies the labels. The count is checked against the class count
the decoder builder derives from the outputs, or the segmentation
channels of a semantic model, so an earlier source that miscounts yields
to a later one that fits; end-to-end detections and class maps carry no
count, so there the first source wins. A ModelPack segmentation output
has one channel more than its classes: the last is background and has no
label, so a multitask model’s masks and boxes share class indices. Ultralytics names that disagree with the output are
ignored and the schema is laid out from the count a labels override
gives, or else from the one output whose class count fits; when several
could, the layout is ambiguous and resolution fails, naming the override
and a sidecar edgefirst.json as the ways to settle it.
When no source fits, the labels are empty with a warning.
labels.txt skips blank lines, so it cannot name an empty class.
Names that repeat ignoring case add a note, since they make matching
classes by name ambiguous. The labels override is checked strictly.
Structs§
- Config
File - One configuration file and where it was found.
- Model
Files - The configuration files of one location: a model container or its directory.
- Model
Overrides - Values that replace what discovery found, applied after every source and
recorded as
ConfigSource::Override. - Model
Sources - Everything discovery reads: the files in the model container, the files beside it, and the model’s tensors as the runtime reports them.
- Provenance
- The source of one resolved value, with an optional detail naming the file or the reason for a guess.
- Resolved
Model Config - A model’s configuration as resolved from every source.
Enums§
- Config
Field - A resolved value whose source is recorded. The code is the one the C
EF_CONFIG_FIELD_*enumerators and the PythonConfigFieldenum use; the string is the nameResolvedModelConfig::describeprints. - Config
Source - The source that supplied a resolved value. The code is the one the C
EF_CONFIG_SOURCE_*enumerators and the PythonConfigSourceenum use. - Model
Config Error - Why a model configuration could not be read or resolved.
- Model
Family - The family a model’s configuration identifies. It selects the
convention a bare
letterboxoverride follows. The code is the one the CEF_MODEL_FAMILY_*enumerators and the PythonModelFamilyenum use. - Model
Task - What a model’s outputs decode to. The code is the one the C
EF_MODEL_TASK_*enumerators and the PythonModelTaskenum use; the string is thetaskoverride grammar. - Normalize
Override - A
normalizeoverride: a preset name orR,G,B/R,G,B, a per-channel mean and standard deviation in the 0–1 domain as Hugging Face and torchvision write them. - Resize
Override - A
resizeoverride:stretch, orletterboxwith an optional placement (letterbox-center,letterbox-topleft). A bareletterboxfollows the model family’s convention; an explicit placement keeps the family’s pad.
Constants§
- EDGEFIRST_
JSON edgefirst.json, as a file name and the legacy ONNX metadata key.- HF_
CONFIG - Hugging Face model config.
- HF_
PREPROCESSOR - Hugging Face image processor config.
- LABELS_
TXT - Class names, one per line.
- ONNX_
EDGEFIRST - ONNX
metadata_propskey foredgefirst.json. - ONNX_
HF_ CONFIG - ONNX
metadata_propskey for the Hugging Faceconfig.json. - ONNX_
HF_ PREPROCESSOR - ONNX
metadata_propskey for the Hugging Facepreprocessor_config.json. - ONNX_
LABELS - ONNX
metadata_propskey for the class names.
Functions§
- read_
embedded - The configuration files embedded in the model file at
path: ONNXmetadata_propsand, with themodel-filesfeature, a ZIP trailer. Neither the graph nor the model payload is read. - read_
embedded_ bytes read_embeddedon a model already in memory.onnxsays the bytes are an ONNX model, which makes a malformedModelProtoan error instead of “not ONNX”.- read_
sidecars - The configuration files beside a model, in
dir:edgefirst.json,labels.txt,config.jsonandpreprocessor_config.json. - resolve_
model_ config - Resolves the configuration of the model file at
path: what it embeds, the files in its directory, andsignalswhen given. - resolve_
sources - Resolves a configuration from already-read sources.