Deploying PyTorch models in production often requires installing a large framework and relying on a Python environment. The ONNX (Open Neural Network Exchange) format converts the computation graph into an intermediate binary format suitable for running on any device and programming language. We will export the PyTorch neural network and run its inference using the lightweight ONNX Runtime. 🛠
To export and run the neural network, we will install the PyTorch framework, the ONNX library, and the cross-platform ONNX Runtime engine.
pip install torch onnx onnxruntime
The packages for converting and high-performance execution of graphs have been successfully installed. ✅
We will write a Python script that creates a test PyTorch model, exports it to a
.onnx file, and immediately performs a verification of the output.import torch, torch.nn as nn, onnxruntime as ort, numpy as np
model = nn.Sequential(nn.Linear(10, 5), nn.ReLU())
x = torch.randn(1, 10)
torch.onnx.export(model, x, "model.onnx", input_names=["input"], output_names=["output"])
session = ort.InferenceSession("model.onnx")
res = session.run(None, {"input": x.numpy()})
print("ONNX Output shape:", res[0].shape)
The model graph has been successfully serialized into a binary file, and the runtime performed the prediction without involving PyTorch. 📦
# verification (checks the correctness and structure of the saved ONNX model)
python3 -c "import onnx; model = onnx.load('model.onnx'); onnx.checker.check_model(model); print('ONNX Model Status: Valid')"Expected output: ONNX Model Status: Valid
# cleanup (deletes the generated model file and cleans up binaries)
rm -f model.onnx
Converting neural networks to ONNX allows you to decouple inference from Python and run models in C++, Rust, Go, or directly in a web browser. Be sure to specify the names of the input and output tensors when exporting to simplify integration with the service. 💻
#PyTorch #ONNX #MachineLearning #DeepLearning #AI #DevOps
✨ Join Best TG Channels https://t.me/addlist/0f6vfFbEMdAwODBk
⭐️ Join Our WhatsApp Channel https://whatsapp.com/channel/0029VaC7Weq29753hpcggW2A