Eksport do ONNX
Uruchamiać model w różnych środowiskach wykonawczych
Eksport do ONNX to bezpłatna lekcja Deep Learning Academy na CoddyKit. To lekcja 2 z 4. Możesz przeczytać całą lekcję poniżej za darmo — a potem ćwiczyć ją interaktywnie w przeglądarce z wbudowanym edytorem kodu i tutorem AI dostępnym 24/7. To część ścieżki edukacyjnej Deep Learning Academy, a Twój postęp synchronizuje się między webem a aplikacją CoddyKit. Kurs Deep Learning Academy zawiera 4 lekcji w sumie.
Części tej lekcji nie zostały jeszcze przetłumaczone i są wyświetlane po angielsku.
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. 🎉
Często zadawane pytania
Czy lekcja „Eksport do ONNX” jest bezpłatna?
Tak — pełny tekst „Eksport do ONNX” jest dostępny za darmo tutaj w sieci. Aby ćwiczyć ją interaktywnie (wbudowany edytor kodu i tutor AI dostępny 24/7) i odblokować resztę kursu Deep Learning Academy, przejdź na CoddyKit PRO. Kurs Deep Learning Academy zawiera 4 lekcji w sumie.
Co nauczysz się w „Eksport do ONNX”?
Uruchamiać model w różnych środowiskach wykonawczych Ćwiczysz Deep Learning Academy z praktycznym kodem, który uruchamiasz bezpośrednio w przeglądarce, a tutor AI dostępny 24/7 odpowiada na Twoje pytania podczas pracy nad lekcją.
Czy potrzebuję doświadczenia, aby zacząć Deep Learning Academy?
Nie wymagamy żadnego doświadczenia. Deep Learning Academy w CoddyKit jest strukturyzowany dla początkujących i zaawansowanych użytkowników, więc możesz zacząć tutaj lub od początku i uczyć się w swoim tempie. To lekcja 2 z 4.
Ile czasu zajmuje lekcja „Eksport do ONNX”?
Większość lekcji CoddyKit trwa około 5–10 minut. Każda lekcja to mały, interaktywny krok, dzięki czemu robisz systematyczne postępy i zawsze wracasz dokładnie do tego samego miejsca — na webie i w aplikacji.
Czy mogę pisać i uruchamiać kod w tej lekcji Deep Learning Academy?
Tak. Każda lekcja Deep Learning Academy zawiera wbudowany edytor kodu, więc piszesz i uruchamiasz prawdziwy kod bezpośrednio w przeglądarce i od razu otrzymujesz sprzężenie zwrotne od AI — bez konfiguracji na komputerze.
Wszystkie lekcje w tym kursie
- TorchScript i torch.compile
- Eksport do ONNX
- Kwantyzacja dla mniejszych i szybszych modeli
- Udostępniać model za pomocą FastAPI