Computer Vision · Advanced level

Advanced level · Module 9: Computer Vision Model Deployment · Lesson 1 of 3

Exporting Models to ONNX

Learners export a PyTorch classifier and a YOLO detector to ONNX and verify that outputs match the original models.

In this lesson you will

  • Export models to ONNX
  • Verify exported models numerically

Key concepts

ONNX formattorch.onnx.exportUltralytics export(format='onnx')dynamic axesnumerical verification

The lesson

ONNX is an open format for machine learning models, so one exported file can run in many runtimes and languages. Export a PyTorch model with torch.onnx.export using a sample input; Ultralytics models export with model.export(format='onnx').

Preprocessing (resizing, normalisation) is not always part of the exported model, so document it and reproduce it exactly at inference time.

Verify every export: run the same images through PyTorch and ONNX Runtime and check that outputs agree within a small tolerance (for example with numpy.allclose).

Step by step

  1. Export the classifier from Module 2 to ONNX.
  2. Export the YOLO detector to ONNX.
  3. Run both in ONNX Runtime on ten images.
  4. Compare outputs with the original models.

Practice activity

Export and verify two models and document their preprocessing.

Expected result: Verified ONNX files with matching outputs and documented preprocessing.

Check your understanding

  1. Why verify an exported model?

    Show answer

    To make sure it produces the same outputs as the original.

  2. What must be documented with an exported model?

    Show answer

    The exact preprocessing it expects.

Further learning

Taught live by an instructor — online or in person. Free course lessons are for self-study and do not lead to a certificate — see registration options.