ส่งออกเป็น ONNX
เรียกใช้โมเดลของคุณบนสภาพแวดล้อมการทำงานต่าง ๆ
ส่งออกเป็น ONNX เป็นบทเรียน Deep Learning Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Deep Learning Academy และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Deep Learning Academy มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
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. 🎉
คำถามที่พบบ่อย
บทเรียน “ส่งออกเป็น ONNX” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “ส่งออกเป็น ONNX” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Deep Learning Academy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Deep Learning Academy มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “ส่งออกเป็น ONNX”
เรียกใช้โมเดลของคุณบนสภาพแวดล้อมการทำงานต่าง ๆ คุณปฏิบัติ Deep Learning Academy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Deep Learning Academy หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Deep Learning Academy บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน
บทเรียน “ส่งออกเป็น ONNX” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Deep Learning Academy นี้ได้ไหม
ได้ บทเรียน Deep Learning Academy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- TorchScript และ torch.compile
- ส่งออกเป็น ONNX
- การทำควอนไทซ์เพื่อโมเดลที่เล็กและเร็วขึ้น
- ให้บริการด้วย FastAPI