التصدير إلى ONNX
شغّل نموذجك عبر بيئات تشغيل مختلفة
التصدير إلى ONNX درس مجاني في Deep Learning Academy على CoddyKit. هذا هو الدرس 2 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 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» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Deep Learning Academy، انتقل إلى CoddyKit PRO. تتضمن دورة Deep Learning Academy 4 دروس في المجموع.
ماذا ستتعلم في «التصدير إلى ONNX»؟
شغّل نموذجك عبر بيئات تشغيل مختلفة تتمرن على Deep Learning Academy مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ Deep Learning Academy؟
لا تُشترط خبرة سابقة. Deep Learning Academy على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 2 من أصل 4.
كم من الوقت يستغرق درس «التصدير إلى ONNX»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس Deep Learning Academy هذا؟
نعم. كل درس في Deep Learning Academy يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.