Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:PeterL1n BackgroundMattingV2 OnnxRuntime InferenceSession

From Leeroopedia
Revision as of 16:18, 16 February 2026 by Admin (talk | contribs) (Auto-imported from implementations/PeterL1n_BackgroundMattingV2_OnnxRuntime_InferenceSession.md)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)


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

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.')

Related Pages

Implements Principle

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment