Implementation:PeterL1n BackgroundMattingV2 OnnxRuntime InferenceSession
| Knowledge Sources | |
|---|---|
| Domains | Model_Deployment, Testing |
| Last Updated | 2026-02-09 00:00 GMT |
Overview
Concrete external tool usage of onnxruntime.InferenceSession for validating exported ONNX matting models against PyTorch reference outputs.
Description
The ONNX validation in export_onnx.py uses onnxruntime.InferenceSession to load and run the exported ONNX model, then compares each output tensor element-wise against the original PyTorch model's output. Test inputs use different dimensions (1×3×720×1280) than the export dummy inputs (2×3×1080×1920) to verify dynamic axis support. Validation passes if the maximum absolute difference across all outputs is below 0.005.
Usage
Use after ONNX export to verify correctness. Enable via --validate flag. If validation fails, try different patch crop/replace methods or refine modes.
Code Reference
Source Location
- Repository: BackgroundMattingV2
- File: export_onnx.py
- Lines: 126-155
Signature
import onnxruntime
# Load ONNX model
sess = onnxruntime.InferenceSession(model_path: str)
# Run inference
outputs = sess.run(
output_names=None, # Return all outputs
input_feed={
'src': numpy.ndarray, # (B, 3, H, W) float32
'bgr': numpy.ndarray # (B, 3, H, W) float32
}
) -> List[numpy.ndarray]
Import
import onnxruntime
import numpy as np
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| model_path | str | Yes | Path to exported .onnx file |
| src | numpy.ndarray | Yes | Source image array (B, 3, H, W) float32 |
| bgr | numpy.ndarray | Yes | Background image array (B, 3, H, W) float32 |
Outputs
| Name | Type | Description |
|---|---|---|
| outputs | List[ndarray] | Model outputs as numpy arrays |
| validation result | str | "Validation passed." or raises exception |
Usage Examples
Validate ONNX Export
import torch
import onnxruntime
import numpy as np
from model import MattingRefine
# PyTorch reference
model = MattingRefine(backbone='resnet50')
model.load_state_dict(torch.load('checkpoint.pth', map_location='cpu'), strict=False)
model.eval()
src = torch.randn(1, 3, 720, 1280)
bgr = torch.randn(1, 3, 720, 1280)
with torch.no_grad():
out_torch = model(src, bgr)
# ONNX inference
sess = onnxruntime.InferenceSession('matting.onnx')
out_onnx = sess.run(None, {
'src': src.numpy(),
'bgr': bgr.numpy()
})
# Compare outputs
for a, b, name in zip(out_torch, out_onnx, ['pha', 'fgr', 'pha_sm', 'fgr_sm', 'err_sm', 'ref_sm']):
e = torch.abs(a - torch.as_tensor(b)).max()
print(f'"{name}" max diff: {e}')
assert e < 0.005, f'Validation failed for {name}'
print('Validation passed.')