Converting PyTorch Models to ONNX
What you'll learn: How to export PyTorch models to the ONNX format using torch.onnx.export(), handle dynamic input shapes, and verify the conversion is correct.
Why Convert to ONNX?
ONNX provides a standardized format that unlocks cross-framework inference, enabling you to train in PyTorch but deploy with optimized runtimes like ONNX Runtime. Converting is like translating your model into a universal language that specialized inference engines can execute more efficiently.
Using torch.onnx.export()
The core function takes your PyTorch model and traces it with example inputs:
import torch
import torch.onnx
model = MyModel().eval()
dummy_input = torch.randn(1, 3, 224, 224)
torch.onnx.export(
model,
dummy_input,
"model.onnx",
input_names=['input'],
output_names=['output'],
dynamic_axes={'input': {0: 'batch_size'},
'output': {0: 'batch_size'}}
)
Handling Dynamic Shapes
The dynamic_axes parameter lets you specify which dimensions can vary at inference time. Without it, your ONNX model accepts only the exact input shape used during export—problematic for variable batch sizes or sequence lengths.
Validating Correctness
Always verify your conversion produces identical outputs:
import onnxruntime as ort
ort_session = ort.InferenceSession("model.onnx")
ort_inputs = {ort_session.get_inputs()[0].name: dummy_input.numpy()}
ort_outputs = ort_session.run(None, ort_inputs)
pytorch_outputs = model(dummy_input).detach().numpy()
assert torch.allclose(torch.tensor(ort_outputs[0]), torch.tensor(pytorch_outputs), atol=1e-5)
Key Takeaway: Use torch.onnx.export() with dynamic axes for flexible shapes, and always validate numerical equivalence between PyTorch and ONNX outputs to ensure faithful conversion.