本文へスキップ
SDK Version: 2.5.0

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)
ParameterTypeDescription
yaml_pathstr | PathPath to the YAML configuration file
resolve_envboolWhether 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​

PropertyTypeDescription
namestrModel name (config["name"])
inputslist[dict]Input tensor definitions
configdictComplete parsed YAML configuration
yaml_pathPathPath to the YAML file
model_dirPathParent directory of the YAML file
is_pipelineboolTrue if YAML defines a multi-model pipeline
pipeline_stageslist[str]Ordered list of pipeline stage names
appendixdictModel-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.

ParameterTypeDescription
profile_namestrProfile name
stagestr | NonePipeline 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.

ParameterTypeDescription
profile_namestrProfile name (used to determine NPU mode)
modalstr | NoneModality selection for multimodal models
input_dtypestr | NoneInput dtype override
stagestr | NonePipeline 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
Automatic NPU Mode Detection

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.

ParameterTypeDescription
stagestr | NonePipeline 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).

ParameterTypeDescription
profile_namestrProfile name
model_pathstr | NoneDirect path to the model file
deviceint | str | list | NoneRuntime device override
stagestr | NonePipeline 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")
Automatic Download

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.

ParameterTypeDescription
data_dirstr | NoneData 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.

ParameterTypeDescription
sessionSessionBaseInference session
datasetDatasetBaseDataset
profile_namestr | NoneProfile 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.

ParameterTypeDescription
profile_namestrProfile name
data_dirstr | NoneData directory
model_pathstr | NonePath to the model file
deviceint | str | list | NoneRuntime 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",
)