๐Ÿ”ฅLimited Offer: Get 50% OFFon AI & Full Stack Courses๐Ÿ”ฅ
Back to Deep Learning Notes
Topic #408

ONNX

ONNX (Open Neural Network Exchange) is a framework-agnostic model format โ€” a way to export a model trained in PyTorch (or TensorFlow, or other frameworks) into a common representation that many different runtimes and hardware backends can execute.

Why a Framework-Agnostic Format Matters

Without ONNX, a model trained in PyTorch is generally tied to PyTorch-compatible deployment environments. ONNX defines a standard computational graph representation that many specialized inference runtimes (ONNX Runtime, TensorRT, and others) can consume directly, often with significant inference speed optimizations that a general-purpose training framework doesn't apply by default โ€” genuinely valuable when raw inference speed matters in production.

Code โ€” Exporting a PyTorch Model to ONNX

import torch

model.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"}}
    # dynamic_axes lets the exported model accept variable batch sizes at inference,
    # rather than being locked to the exact batch size used during export
)

Code โ€” Running Inference with ONNX Runtime

import onnxruntime as ort
import numpy as np

session = ort.InferenceSession("model.onnx")
input_data = np.random.randn(1, 3, 224, 224).astype(np.float32)

outputs = session.run(None, {"input": input_data})
print(outputs[0].shape)

Notice that this inference code has no dependency on PyTorch at all โ€” the ONNX Runtime session loads and executes the exported graph independently, which is exactly the portability benefit ONNX provides.

ONNX vs TorchScript โ€” When to Use Which

TorchScriptONNX
EcosystemPyTorch-specific (LibTorch for C++ deployment)Framework-agnostic โ€” usable across many runtimes and hardware backends
Control flow supportFull support via scriptingMore limited โ€” complex dynamic control flow can be harder to export correctly
Typical use caseDeploying specifically within a PyTorch/LibTorch-based systemCross-framework deployment, or targeting specialized inference hardware/runtimes

Common Mistakes

  • Exporting to ONNX without setting dynamic_axes for the batch dimension, then discovering the exported model only accepts the exact batch size used during export.
  • Assuming an ONNX export will automatically support every possible PyTorch operation โ€” some custom or exotic operations may not have direct ONNX equivalents and require special handling or workarounds.

Interview Relevance

Q: "Why might a team choose to export a PyTorch model to ONNX before deploying it, rather than deploying the native PyTorch model directly?" ONNX provides framework-agnostic portability โ€” the exported model can run on specialized, often significantly faster inference runtimes (ONNX Runtime, TensorRT) and various hardware backends without requiring a PyTorch dependency in the serving environment. This can deliver meaningful inference speed and deployment flexibility benefits, particularly valuable when raw latency or cross-platform compatibility matters in production.

Practice Question

Why is setting dynamic_axes during ONNX export important for a model that will serve requests with varying batch sizes in production?

Want to go beyond the notes?

Join CodingNow 2.0's Deep Learning course โ€” live mentorship, real projects, and 100% placement support.

Enroll Now โ€” Free Demo Available

ONNX โ€“ FAQs

Quick answers about learning ONNX in Deep Learning.

This free note from CodingNow 2.0 explains ONNX in Deep Learning โ€” concept, syntax and worked code examples you can copy, run and revise before interviews.
Yes. Every Deep Learning topic on CodingNow 2.0, including ONNX, is 100% free with no signup required.
With focused practice, most students grasp ONNX in 1โ€“3 days from these notes; pairing it with CodingNow 2.0's mentor-led course takes you to job-ready depth faster.
Use the code examples in this note, then ask doubts for free on the CodingNow 2.0 Community (/community) โ€” expert instructors answer within 24 hours.
WhatsApp
Call NowEnroll Now