Skip to main content

Module model_config

Module model_config 

Source
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:

  1. edgefirst.json embedded in the model (its input.preprocessing, then the legacy validation.normalization / validation.preprocessing);
  2. edgefirst.json and labels.txt beside the model;
  3. Hugging Face config.json + preprocessor_config.json embedded in the model;
  4. the same files beside the model (the optimum export layout);
  5. Ultralytics export metadata (infer_ultralytics_schema);
  6. Hugging Face input/output names (pixel_values → logits), as guesses;
  7. the documented defaults.

ModelOverrides apply last.

§Embedding conventions

ContainerWhereNames
ONNXModelProto.metadata_propsedgefirst (legacy edgefirst.json), labels, hf_config, hf_preprocessor_config
TFLite, HEF, DVM, TensorRT, any fileappended ZIP (TFLite associated files)edgefirst.json, labels.txt, config.json, preprocessor_config.json
anythe model’s directoryas 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§

ConfigFile
One configuration file and where it was found.
ModelFiles
The configuration files of one location: a model container or its directory.
ModelOverrides
Values that replace what discovery found, applied after every source and recorded as ConfigSource::Override.
ModelSources
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.
ResolvedModelConfig
A model’s configuration as resolved from every source.

Enums§

ConfigField
A resolved value whose source is recorded. The code is the one the C EF_CONFIG_FIELD_* enumerators and the Python ConfigField enum use; the string is the name ResolvedModelConfig::describe prints.
ConfigSource
The source that supplied a resolved value. The code is the one the C EF_CONFIG_SOURCE_* enumerators and the Python ConfigSource enum use.
ModelConfigError
Why a model configuration could not be read or resolved.
ModelFamily
The family a model’s configuration identifies. It selects the convention a bare letterbox override follows. The code is the one the C EF_MODEL_FAMILY_* enumerators and the Python ModelFamily enum use.
ModelTask
What a model’s outputs decode to. The code is the one the C EF_MODEL_TASK_* enumerators and the Python ModelTask enum use; the string is the task override grammar.
NormalizeOverride
A normalize override: a preset name or R,G,B/R,G,B, a per-channel mean and standard deviation in the 0–1 domain as Hugging Face and torchvision write them.
ResizeOverride
A resize override: stretch, or letterbox with an optional placement (letterbox-center, letterbox-topleft). A bare letterbox follows 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_props key for edgefirst.json.
ONNX_HF_CONFIG
ONNX metadata_props key for the Hugging Face config.json.
ONNX_HF_PREPROCESSOR
ONNX metadata_props key for the Hugging Face preprocessor_config.json.
ONNX_LABELS
ONNX metadata_props key for the class names.

Functions§

read_embedded
The configuration files embedded in the model file at path: ONNX metadata_props and, with the model-files feature, a ZIP trailer. Neither the graph nor the model payload is read.
read_embedded_bytes
read_embedded on a model already in memory. onnx says the bytes are an ONNX model, which makes a malformed ModelProto an error instead of “not ONNX”.
read_sidecars
The configuration files beside a model, in dir: edgefirst.json, labels.txt, config.json and preprocessor_config.json.
resolve_model_config
Resolves the configuration of the model file at path: what it embeds, the files in its directory, and signals when given.
resolve_sources
Resolves a configuration from already-read sources.