YOLO to ONNX Conversion: Difference between revisions

From LogicalDOC Community Wiki
Jump to navigationJump to search
Giuseppe (talk | contribs)
Created page with "= 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 effici..."
 
Giuseppe (talk | contribs)
Line 1: Line 1:
= Converting a YOLO Model to ONNX =
= YOLO to ONNX Conversion =


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.
== Converting a YOLO Model to ONNX ==


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.
LogicalDOC performs object detection using the ONNX Runtime Java API. Therefore, a trained YOLO model must be converted from the native PyTorch (`.pt`) format to the Open Neural Network Exchange (ONNX) format before it can be imported into LogicalDOC.


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.
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 programming languages and runtime environments.
 
The conversion process does not retrain or modify the model. Instead, it exports the trained network, including its learned weights and computational graph, into a portable format that can be executed by ONNX Runtime.


== Why ONNX? ==
== Why ONNX? ==
Line 15: Line 17:
* No dependency on the Python runtime during inference.
* No dependency on the Python runtime during inference.
* Native Java support through the ONNX Runtime API.
* Native Java support through the ONNX Runtime API.
* Faster startup and lower memory consumption compared to embedding a Python interpreter.
* Faster startup and lower memory consumption than embedding a Python interpreter.
* Cross-platform model portability.
* Cross-platform model portability.
* Hardware acceleration when supported by the execution environment.
* Support for hardware acceleration when available.


== Export the Model ==


The following script converts the trained `best.pt` model into the ONNX format:


This is an example of python script that performs the YOLO-to-ONNX conversion:
<syntaxhighlight lang="python">
 
<syntaxhighlight lang="Python">
from ultralytics import YOLO
from ultralytics import YOLO


Line 29: Line 31:


model.export(
model.export(
    format="onnx",
format="onnx",
    simplify=True,
simplify=True,
    nms=True
nms=True
)
) </syntaxhighlight>
</syntaxhighlight>
 
Where:
 
* `format="onnx"` exports the model in the ONNX format.
* `simplify=True` simplifies the exported computation graph to improve compatibility and potentially reduce inference overhead.
* `nms=True` embeds the Non-Maximum Suppression (NMS) step into the exported model, allowing the ONNX Runtime to return the final detections directly.
 
After the conversion completes, the generated model (`best.onnx`) can be imported into LogicalDOC for inference.

Revision as of 09:20, 25 June 2026

YOLO to ONNX Conversion

Converting a YOLO Model to ONNX

LogicalDOC performs object detection using the ONNX Runtime Java API. Therefore, a trained YOLO model must be converted from the native PyTorch (`.pt`) format to the Open Neural Network Exchange (ONNX) format before it 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 programming languages and runtime environments.

The conversion process does not retrain or modify the model. Instead, it exports the trained network, including its learned weights and computational graph, into a portable format that can be executed by ONNX Runtime.

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 than embedding a Python interpreter.
  • Cross-platform model portability.
  • Support for hardware acceleration when available.

Export the Model

The following script converts the trained `best.pt` model into the ONNX format:

from ultralytics import YOLO

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

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

Where:

  • `format="onnx"` exports the model in the ONNX format.
  • `simplify=True` simplifies the exported computation graph to improve compatibility and potentially reduce inference overhead.
  • `nms=True` embeds the Non-Maximum Suppression (NMS) step into the exported model, allowing the ONNX Runtime to return the final detections directly.

After the conversion completes, the generated model (`best.onnx`) can be imported into LogicalDOC for inference.