0Pricing
Deep Learning Academy · Lección

Exporte a ONNX

Ejecute su modelo en distintos runtimes

Exporte a ONNX es una lección gratuita de Deep Learning Academy en CoddyKit. Esta es la lección 2 de 4. Puedes leer la lección completa abajo gratuitamente — luego la practicas en el navegador con un editor de código integrado y un tutor de IA 24/7. Forma parte de la ruta de aprendizaje de Deep Learning Academy, y tu progreso se sincroniza en la web y la app de CoddyKit. El curso de Deep Learning Academy incluye 4 lecciones en total.

Partes de esta lección aún no han sido traducidas y se muestran en inglés.

One Model, Many Runtimes

Sometimes your model must run somewhere PyTorch is not installed. ONNX is a shared format that lets one model run across many engines. 🌐

What ONNX Stands For

ONNX means Open Neural Network Exchange. It is a vendor-neutral file format that describes your model as a graph of standard operations.

Why Teams Love ONNX

With ONNX you train in PyTorch but deploy on ONNX Runtime, mobile, or the browser, without rewriting the model for each platform.

Export in One Call

You export by giving PyTorch the model and a sample input. The exporter traces the run and writes a portable graph to disk.

import torch
torch.onnx.export(model, sample_input, 'model.onnx')

The Dummy Input Shapes the Graph

That sample tensor must match your real input shape and dtype, because the exporter records the operations the data triggers.

sample_input = torch.randn(1, 3, 224, 224)

Name Your Inputs and Outputs

Give clear input and output names so the serving code can bind data by name instead of guessing positions.

torch.onnx.export(model, x, 'm.onnx',
  input_names=['image'], output_names=['logits'])

Allow Flexible Batch Sizes

By default the batch size is fixed. Mark it as a dynamic axis so the exported model accepts any number of inputs at once.

dynamic_axes={'image': {0: 'batch'}}

Always Check Your Export

Load the file with the onnx library and run the built-in checker to confirm the graph is valid before you ship it. ✅

import onnx
onnx.checker.check_model(onnx.load('model.onnx'))

Run It with ONNX Runtime

ONNX Runtime is a fast engine that executes the exported model on CPU or GPU, often quicker than plain Python inference.

import onnxruntime as ort
session = ort.InferenceSession('model.onnx')

Confirm the Numbers Match

Run the same input through PyTorch and ONNX Runtime and compare outputs. They should match closely, proving the export is faithful.

Mind the Opset Version

Each export targets an opset version, the set of supported operations. Pick a version your target runtime understands to avoid errors.

torch.onnx.export(model, x, 'm.onnx', opset_version=17)

Quick Check

You want a fixed batch to instead accept any size. What do you set?

Recap: Portable Across Runtimes

You exported a PyTorch model to ONNX with a sample input, named axes, validated it, and ran it on ONNX Runtime anywhere. 🎉

Preguntas frecuentes

¿La lección «Exporte a ONNX» es gratis?

Sí — el texto completo de «Exporte a ONNX» es gratis para leer aquí en la web. Para practicarla de forma interactiva (editor de código integrado y tutor de IA 24/7) y desbloquear el resto del curso de Deep Learning Academy, actualiza a CoddyKit PRO. El curso de Deep Learning Academy incluye 4 lecciones en total.

¿Qué aprenderé en «Exporte a ONNX»?

Ejecute su modelo en distintos runtimes Practicas Deep Learning Academy con código real que ejecutas directamente en el navegador, y un tutor de IA 24/7 responde tus preguntas mientras trabajas en la lección.

¿Necesito experiencia previa para empezar Deep Learning Academy?

No se requiere experiencia previa. Deep Learning Academy en CoddyKit está estructurado para principiantes hasta estudiantes avanzados, así que puedes empezar aquí o desde el inicio y avanzar a tu ritmo. Esta es la lección 2 de 4.

¿Cuánto tiempo toma la lección «Exporte a ONNX»?

La mayoría de las lecciones de CoddyKit toman alrededor de 5–10 minutos. Cada una es compacta e interactiva, así que avanzas constantemente y retomas exactamente por donde dejaste en la web y la app.

¿Puedo escribir y ejecutar código en esta lección de Deep Learning Academy?

Sí. Cada lección de Deep Learning Academy incluye un editor de código integrado, así que escribes y ejecutas código real directamente en tu navegador y obtienes retroalimentación instantánea de IA — sin configuración local necesaria.

Todas las lecciones de este curso

  1. TorchScript y torch.compile
  2. Exporte a ONNX
  3. Cuantización para modelos más pequeños y rápidos
  4. Sirva con FastAPI
← Volver a Deep Learning Academy