YOLO to ONNX Conversion: Difference between revisions

From LogicalDOC Community Wiki
Jump to navigationJump to search
Giuseppe (talk | contribs)
No edit summary
Tag: Manual revert
Giuseppe (talk | contribs)
No edit summary
 
(3 intermediate revisions by the same user not shown)
Line 2: Line 2:


== Converting a YOLO Model to ONNX ==
== Converting a YOLO Model to ONNX ==
{{Advice| This guide describes an example workflow for training a custom YOLO model and preparing it for use with LogicalDOC.
<b><u>Please be aware that this procedure is not coverded by the standard support contract</u></b>. LogicalDOC cannot provide assistance with issues related to dataset preparation, training failures, model quality, GPU configuration, or third-party tools such as Label Studio, Ultralytics YOLO, or ONNX Runtime.
If you require professional assistance, please contact  <b>sales@logicaldoc.com</b> to request a quotation for consulting services.}}
{{Warning|
<b>Licensing Notice:</b> The examples in this guide use the Ultralytics implementation of YOLO. Ultralytics YOLO is distributed under its own licensing terms. <b><u>Before using YOLO in a commercial environment, ensure that your intended use complies with the applicable Ultralytics license.</u></b> For the latest licensing information, refer to the official Ultralytics website: https://www.ultralytics.com/license
}}


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.
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.
Line 23: Line 31:
== Export the Model ==
== Export the Model ==


The following script converts the trained `best.pt` model into the ONNX format:
The following script converts the trained <code>best.pt</code> model into the ONNX format:


<syntaxhighlight lang="python">
<syntaxhighlight lang="python">
Line 38: Line 46:
Where:
Where:


* `format="onnx"` exports the model in the ONNX format.
* <code>format="onnx"</code> exports the model in the ONNX format.
* `simplify=True` simplifies the exported computation graph to improve compatibility and potentially reduce inference overhead.
* <code>simplify=True</code> 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.
* <code>nms=True</code> 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.
After the conversion completes, the generated model (<code>best.onnx</code> ) can be imported into LogicalDOC for inference.

Latest revision as of 07:44, 26 June 2026

YOLO to ONNX Conversion

Converting a YOLO Model to ONNX


This guide describes an example workflow for training a custom YOLO model and preparing it for use with LogicalDOC.

Please be aware that this procedure is not coverded by the standard support contract. LogicalDOC cannot provide assistance with issues related to dataset preparation, training failures, model quality, GPU configuration, or third-party tools such as Label Studio, Ultralytics YOLO, or ONNX Runtime.

If you require professional assistance, please contact sales@logicaldoc.com to request a quotation for consulting services.



Licensing Notice: The examples in this guide use the Ultralytics implementation of YOLO. Ultralytics YOLO is distributed under its own licensing terms. Before using YOLO in a commercial environment, ensure that your intended use complies with the applicable Ultralytics license. For the latest licensing information, refer to the official Ultralytics website: https://www.ultralytics.com/license


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.