ModelBuilder
ModelBuilder is the core class in DX-ModelZoo that parses YAML configuration files and creates all components required at runtime.
Module: dx_modelzoo.loader.model_builder
Method Map
Constructor
ModelBuilder(yaml_path: Union[str, Path], resolve_env: bool = True)
| Parameter | Type | Description |
|---|---|---|
yaml_path | str | Path | Path to the YAML configuration file |
resolve_env | bool | Whether to resolve environment variables (default: True) |
from dx_modelzoo.loader.model_builder import ModelBuilder
builder = ModelBuilder("src/dx_modelzoo/models/cv/image_classification/resnet/resnet50_224x224.yaml")
Properties
| Property | Type | Description |
|---|---|---|
name | str | Model name (config["name"]) |
inputs | list[dict] | Input tensor definitions |
config | dict | Complete parsed YAML configuration |
yaml_path | Path | Path to the YAML file |
model_dir | Path | Parent directory of the YAML file |
is_pipeline | bool | True if YAML defines a multi-model pipeline |
pipeline_stages | list[str] | Ordered list of pipeline stage names |
appendix | dict | Model-specific overrides (npu_skip, label_preprocessing) |
print(builder.name) # "resnet50_224x224"
print(builder.inputs) # [{"name": "input", "shape": [1,3,224,224], ...}]
Methods
get_profile
def get_profile(self, profile_name: str, stage: Optional[str] = None) -> dict
Returns the profile configuration dictionary for the given profile name.
| Parameter | Type | Description |
|---|---|---|
profile_name | str | Profile name |
stage | str | None | Pipeline stage name (for multi-stage pipeline YAMLs) |
profile = builder.get_profile("onnx")
# {"target": "onnx", "runtime": {"device": "gpu", "batch_size": 1}}
build_preprocessing
def build_preprocessing(
self,
profile_name: str,
modal: Optional[str] = None,
input_dtype: Optional[str] = None,
stage: Optional[str] = None,
) -> PreprocessingPipeline
Builds the preprocessing pipeline for the given profile, from the YAML's preprocessing section.
| Parameter | Type | Description |
|---|---|---|
profile_name | str | Profile name (used to determine NPU mode) |
modal | str | None | Modality selection for multimodal models |
input_dtype | str | None | Input dtype override |
stage | str | None | Pipeline stage name (for multi-stage pipeline YAMLs) |
preprocessing = builder.build_preprocessing("onnx")
# -> PreprocessingPipeline with .steps == [Resize(...), CenterCrop(...), ...], npu_mode=False
# NPU uint8 input → arithmetic operations skipped
preprocessing_npu = builder.build_preprocessing("q-lite")
# -> PreprocessingPipeline with .steps == [Resize(...), CenterCrop(...)], npu_mode=True
target: dxnn enables NPU mode. Arithmetic operations are skipped when
runtime input dtype is uint8; they remain for float32. Override this
behavior with appendix.npu_skip_arithmetic.
build_postprocessing
def build_postprocessing(self, stage: Optional[str] = None) -> PostprocessingPipeline
Creates a pipeline from the postprocessing section of the YAML. Returns the same pipeline regardless of the profile.
| Parameter | Type | Description |
|---|---|---|
stage | str | None | Pipeline stage name (for multi-stage pipeline YAMLs) |
postprocessing = builder.build_postprocessing()
# -> PostprocessingPipeline with .steps == [TopK(k=[1, 5])]
build_session
def build_session(
self,
profile_name: str,
model_path: Optional[str] = None,
device: Optional[Union[int, str, list]] = None,
stage: Optional[str] = None,
) -> SessionBase
Builds the inference session for the given profile (ONNX Runtime or DxRuntime).
| Parameter | Type | Description |
|---|---|---|
profile_name | str | Profile name |
model_path | str | None | Direct path to the model file |
device | int | str | list | None | Runtime device override |
stage | str | None | Pipeline stage name |
session = builder.build_session("onnx")
# OnnxRuntimeSession("/path/to/resnet50_224x224.onnx")
session = builder.build_session("q-lite")
# DxRuntimeSession("/path/to/resnet50_224x224.dxnn")
If the DXMZ_MODEL_URL environment variable is set, the model file is automatically downloaded when it is not available locally.
build_dataset
def build_dataset(
self,
data_dir: Optional[str] = None,
) -> DatasetBase
Builds the dataset instance from the YAML's dataset section.
| Parameter | Type | Description |
|---|---|---|
data_dir | str | None | Data directory override |
dataset = builder.build_dataset()
# ImageNetDataset("/data/ILSVRC2012/val")
dataset = builder.build_dataset(data_dir="/custom/data/path")
build_evaluator
def build_evaluator(
self,
session: SessionBase,
dataset: DatasetBase,
profile_name: Optional[str] = None,
) -> EvaluatorBase
Builds the evaluator that ties together the session, dataset, and postprocessing pipeline.
| Parameter | Type | Description |
|---|---|---|
session | SessionBase | Inference session |
dataset | DatasetBase | Dataset |
profile_name | str | None | Profile name (for context configuration) |
evaluator = builder.build_evaluator(session, dataset, "onnx")
run_eval
def run_eval(
self,
profile_name: str,
data_dir: Optional[str] = None,
model_path: Optional[str] = None,
device: Optional[Union[int, str, list]] = None,
) -> dict
Runs the entire evaluation pipeline in a single call. Internally invokes build_session → build_dataset → build_evaluator → eval() in sequence.
| Parameter | Type | Description |
|---|---|---|
profile_name | str | Profile name |
data_dir | str | None | Data directory |
model_path | str | None | Path to the model file |
device | int | str | list | None | Runtime device override |
results = builder.run_eval("onnx")
# {
# "model": "resnet50_224x224",
# "metrics": [
# {"name": "Top-1", "metric_value": 69.76},
# {"name": "Top-5", "metric_value": 89.08}
# ],
# "fps": 624.7,
# "elapsed_time": 82,
# "start_time": "2026-07-23 14:30:00",
# "profile": "onnx"
# }
Examples
Simple Evaluation
from dx_modelzoo.loader.model_builder import ModelBuilder
builder = ModelBuilder("resnet50_224x224.yaml")
results = builder.run_eval("onnx")
print(results["metrics"])
Step-by-Step Execution
from dx_modelzoo.loader.model_builder import ModelBuilder
builder = ModelBuilder("resnet50_224x224.yaml")
# Build individual components
session = builder.build_session("onnx")
dataset = builder.build_dataset()
preprocessing = builder.build_preprocessing("onnx")
postprocessing = builder.build_postprocessing()
# Attach preprocessing to dataset
dataset.preprocessing = preprocessing
# Build & run Evaluator
evaluator = builder.build_evaluator(session, dataset, "onnx")
evaluator.set_postprocessing(postprocessing)
results = evaluator.eval()
session.close()
Custom Model Path
builder = ModelBuilder("MyModel.yaml")
results = builder.run_eval(
profile_name="onnx",
model_path="/custom/path/model.onnx",
data_dir="/custom/data",
)