Sub topic 14
Exporting a Trained YOLO Model for Deployment
This chapter assumes you can run Python, understand image preprocessing, and have a trained Ultralytics YOLO checkpoint such as best.pt. Deployment means using the trained model in an application, service, browser, or device instead of continuing to train it.
Why export a model?
A training checkpoint is usually tied to its training framework. It may contain optimizer state, training metadata, and framework-specific objects. A deployment format contains the parts needed for inference: the model structure and learned weights.
Exporting can provide:
- Compatibility with runtimes such as ONNX Runtime or TensorRT.
- Smaller files through optional quantization.
- Faster inference on supported hardware.
- A separation between training code and production code.
- Easier integration with applications written in other languages.
The best format depends on the target environment.
| Format | Common runtime | Typical use |
|---|---|---|
| PyTorch checkpoint | PyTorch | Python experiments and continued training |
| ONNX | ONNX Runtime | Cross-platform CPU or GPU inference |
| TensorRT engine | NVIDIA TensorRT | Optimized NVIDIA GPU deployment |
| Core ML | Apple platforms | iPhone, iPad, and macOS applications |
Exporting to ONNX
The following code loads the best checkpoint from a training run and exports it to ONNX:
from ultralytics import YOLO
model = YOLO("runs/detect/train/weights/best.pt")
exported_path = model.export(format="onnx")
print(f"Exported model: {exported_path}")The export normally creates a file beside the checkpoint, such as best.onnx. The exported model does not automatically remove the need for preprocessing or postprocessing. Your application still needs to:
- Resize and normalize input images as the model expects.
- Run the model with the correct input shape and data type.
- Decode the model output.
- Apply confidence filtering and non-maximum suppression when required.
- Convert boxes back to the original image coordinates.
- Map class indices to class names.
Check the exported model
An export can succeed while producing an output that your application interprets incorrectly. Compare the original and exported models on the same images.
from ultralytics import YOLO
image_path = "bus.jpg"
pytorch_model = YOLO("runs/detect/train/weights/best.pt")
onnx_model = YOLO("runs/detect/train/weights/best.onnx")
pytorch_results = pytorch_model.predict(image_path, conf=0.25, iou=0.7)
onnx_results = onnx_model.predict(image_path, conf=0.25, iou=0.7)
for result in pytorch_results:
print("PyTorch:", len(result.boxes), "detections")
for result in onnx_results:
print("ONNX:", len(result.boxes), "detections")The detections may not have exactly identical floating-point scores. They should be close enough that class predictions and box locations remain acceptable for your application. Test several images, including small objects, crowded scenes, and images with no objects.
Deployment choices and trade-offs
Fixed input dimensions can improve optimization, while dynamic dimensions make one model easier to use with different image sizes. Dynamic shapes may reduce optimization opportunities. Quantization can reduce memory use and increase speed, but it can also lower accuracy, especially for small or low-contrast objects.
The deployment path usually looks like this:
A practical deployment checklist
- Record the model version, class names, image size, and confidence threshold.
- Keep preprocessing identical between validation and production.
- Confirm the coordinate conversion uses the original image dimensions.
- Measure end-to-end latency, including image loading and postprocessing.
- Monitor false positives and missed objects after release.
- Retain the original checkpoint so you can reproduce or retrain the export.
Exercise
Why should you compare an exported ONNX model with the original checkpoint on the same test images?
Show answer
Export can change numerical behavior or expose preprocessing and output-decoding mistakes. Comparing the same images checks that predictions remain accurate enough for the deployment target.