How to verify ONNX model export
Quick answer
To verify an ONNX model export, load the model using
onnx.load() and check its structure with onnx.checker.check_model(). Then run inference using onnxruntime.InferenceSession to confirm the model produces expected outputs.PREREQUISITES
Python 3.8+pip install onnx onnxruntime numpy
Setup
Install the required packages onnx, onnxruntime, and numpy using pip to load, check, and run inference on ONNX models.
pip install onnx onnxruntime numpy Step by step verification
This example loads an ONNX model file, verifies its structure, and runs a sample inference to validate the export.
import onnx
import onnxruntime as ort
import numpy as np
import os
# Path to your ONNX model file
model_path = "model.onnx"
# Load the ONNX model
model = onnx.load(model_path)
# Check the model's structure and validity
onnx.checker.check_model(model)
print("ONNX model structure is valid.")
# Create an inference session
session = ort.InferenceSession(model_path)
# Prepare dummy input data matching the model's input shape
input_name = session.get_inputs()[0].name
input_shape = session.get_inputs()[0].shape
input_type = session.get_inputs()[0].type
# Replace dynamic dimensions (None or 'None') with 1 for dummy input
input_shape = [dim if isinstance(dim, int) else 1 for dim in input_shape]
# Create a dummy input array of the correct shape and type
dummy_input = np.random.rand(*input_shape).astype(np.float32)
# Run inference
outputs = session.run(None, {input_name: dummy_input})
print(f"Inference output shapes: {[output.shape for output in outputs]}") output
ONNX model structure is valid. Inference output shapes: [(1, 1000)]
Common variations
- Use
onnxruntime.InferenceSessionwith different execution providers like CUDA for GPU acceleration. - Validate multiple inputs by preparing a dictionary with all input names and dummy data.
- Use
onnxruntime.SessionOptionsto customize session behavior.
import onnxruntime as ort
# Use CUDA execution provider if available
session = ort.InferenceSession(model_path, providers=["CUDAExecutionProvider", "CPUExecutionProvider"])
# Prepare inputs for multiple inputs example
inputs = {}
for input_meta in session.get_inputs():
shape = [dim if isinstance(dim, int) else 1 for dim in input_meta.shape]
inputs[input_meta.name] = np.random.rand(*shape).astype(np.float32)
outputs = session.run(None, inputs)
print(f"Output shapes with multiple inputs: {[o.shape for o in outputs]}") output
Output shapes with multiple inputs: [(1, 1000)]
Troubleshooting
- If
onnx.checker.check_model()raises errors, the model export is invalid or corrupted. - If inference fails, verify input shapes and types match the model's expected inputs.
- Use
onnx.helper.printable_graph(model.graph)to inspect the model graph for debugging.
import onnx
# Print the model graph for inspection
print(onnx.helper.printable_graph(model.graph)) output
graph ( ... ) { ... } # Detailed graph structure printed Key Takeaways
- Always use
onnx.checker.check_model()to validate ONNX model structure after export. - Run inference with
onnxruntime.InferenceSessionand dummy inputs to verify runtime correctness. - Inspect model inputs and outputs carefully to prepare matching dummy data for testing.
- Use execution providers like CUDA in
onnxruntimefor accelerated inference if available. - Print the ONNX graph to debug structural issues when verification or inference fails.