YOLO to ONNX Conversion
Converting a YOLO Model to ONNX
LogicalDOC performs object detection using the ONNX Runtime Java API. For this reason, trained YOLO models must be converted from the native PyTorch (.pt) format to the Open Neural Network Exchange (ONNX) format before they can be imported into LogicalDOC.
ONNX (Open Neural Network Exchange) is an open standard for representing machine learning models. It enables models trained in one framework, such as PyTorch, to be executed efficiently in different environments and programming languages.
The conversion process does not retrain or modify the model. Instead, it exports the learned weights and computational graph into a portable format that can be executed by the ONNX Runtime inference engine.
Why ONNX?
Ultralytics trains YOLO models using PyTorch. However, LogicalDOC is implemented in Java and performs inference using the ONNX Runtime library.
Using ONNX provides several advantages:
- No dependency on the Python runtime during inference.
- Native Java support through the ONNX Runtime API.
- Faster startup and lower memory consumption compared to embedding a Python interpreter.
- Cross-platform model portability.
- Hardware acceleration when supported by the execution environment.
This is an example of python script that performs the YOLO-to-ONNX conversion:
from ultralytics import YOLO
model = YOLO("runs/detect/target/weights/best.pt")
model.export(
format="onnx",
simplify=True,
nms=True
)