Pipeline
The DX-ModelZoo preprocessing and postprocessing pipelines execute operations sequentially in the order defined in YAML configuration.
Pipeline Overview
The following diagram shows how preprocessing, inference, and postprocessing connect:
PreprocessingPipeline
Applies preprocessing operations sequentially to the input data (images).
Class Structure
class PreprocessingPipeline(Compose):
"""Inherits from torchvision.transforms.Compose."""
def __init__(
self,
steps_config: Union[List[Dict], Dict[str, List[Dict]]],
npu_mode: bool = False,
):
# Builds steps from config dicts; skips NPU-handled ops when npu_mode=True
...
def __call__(self, img: np.ndarray) -> np.ndarray:
for step in self.steps:
img = step(img)
return img
YAML to Pipeline Mapping
preprocessing:
- type: resize
size: [256, 256]
mode: torchvision
- type: centercrop
height: 224
width: 224
- type: div
x: 255
- type: normalize
mean: [0.485, 0.456, 0.406]
std: [0.229, 0.224, 0.225]
- type: transpose
axis: [2, 0, 1]
- type: expanddim
axis: 0
The above YAML is transformed into the following:
# Automatically generated by ModelBuilder
pipeline = PreprocessingPipeline(
steps_config=[
{"type": "resize", "size": [256, 256], "mode": "torchvision"},
{"type": "centercrop", "height": 224, "width": 224},
{"type": "div", "x": 255},
{"type": "normalize", "mean": [0.485, 0.456, 0.406], "std": [0.229, 0.224, 0.225]},
{"type": "transpose", "axis": [2, 0, 1]},
{"type": "expanddim", "axis": 0},
],
npu_mode=False,
)
NPU Auto-Skip
For target: dxnn, arithmetic preprocessing is skipped when runtime input
dtype is uint8; it remains for float32 input unless
appendix.npu_skip_arithmetic overrides the behavior.
NPU_SKIP_DEFAULT
Arithmetic operations skipped on the NPU are defined in NPU_SKIP_DEFAULT:
div, normalize, transpose, expanddim, mul, add, subtract — see
Preprocessing API for the full definition.
Behavior Comparison
- ONNX Mode (Full Execution)
- NPU Mode (Auto-Skip)
resize → centercrop → convertcolor → div → normalize → transpose → expanddim
✅ ✅ ✅ ✅ ✅ ✅ ✅
All operations are executed in order.
resize → centercrop → convertcolor → div → normalize → transpose → expanddim
✅ ✅ ✅ ⏭️ ⏭️ ⏭️ ⏭️
Operations included in NPU_SKIP_DEFAULT are skipped.
The NPU accepts uint8 input and handles div (normalization), normalize, transpose (layout conversion), and expanddim (batch dimension) at the hardware level. Running these operations redundantly on the CPU would produce incorrect results.
Skip Flow
PostprocessingPipeline
Applies postprocessing operations sequentially to the model output.
Class Structure
class PostprocessingPipeline:
def __init__(self, steps: list):
self.steps = steps
def __call__(self, outputs):
for step in self.steps:
outputs = step(outputs)
return outputs
The postprocessing pipeline does not have NPU skip logic. All postprocessing operations are always executed.
YAML to Pipeline Mapping
postprocessing:
- type: nms
conf_thres: 0.001
iou_thres: 0.7
# Transformed result
pipeline = PostprocessingPipeline(
steps_config=[{"type": "nms", "conf_thres": 0.001, "iou_thres": 0.7}]
)
# pipeline.steps -> [NMS(conf_thres=0.001, iou_thres=0.7)]
Postprocessing Patterns by Task
See Postprocessing Guide by Task for the full task-to-postprocessing mapping.
Full Execution Flow
The following sequence diagram shows the complete evaluation flow, from raw data to final metrics:
Custom Pipeline Extension
To add a custom operation, register it with the appropriate Registry:
from dx_modelzoo.preprocessing import PREPROCESSING_REGISTRY
@PREPROCESSING_REGISTRY.register("my_augment")
class MyAugment:
def __init__(self, probability: float = 0.5):
self.probability = probability
def __call__(self, img: np.ndarray) -> np.ndarray:
# Custom logic
return img
preprocessing:
- type: resize
size: [224, 224]
- type: my_augment # Custom operation inserted
probability: 0.8
- type: div
x: 255
Print the shape before and after each step's __call__ to trace the data transformation process:
for step in pipeline.steps:
print(f"{step.__class__.__name__}: {img.shape}", end=" → ")
img = step(img)
print(img.shape)