YOLO to ONNX conversion: Difference between revisions

From LogicalDOC Community Wiki
Jump to navigationJump to search
Giuseppe (talk | contribs)
No edit summary
Giuseppe (talk | contribs)
No edit summary
Line 21: Line 21:
== Example of ONNX Conversion Script ==
== Example of ONNX Conversion Script ==


<highlightsyntax lang="Python">
<syntaxhighlight lang="Python">
from ultralytics import YOLO
from ultralytics import YOLO


Line 31: Line 31:
     nms=True
     nms=True
)
)
</highlightsyntax>
</syntaxhighlight>

Revision as of 07:54, 25 June 2026

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.

Example of ONNX Conversion Script

from ultralytics import YOLO

model = YOLO("runs/detect/target/weights/best.pt")

model.export(
    format="onnx",
    simplify=True,
    nms=True
)