Skip to contents

Exports a trained YOLO PyTorch (.pt) model to standalone ONNX (.onnx) format, or imports/registers an existing .onnx model and its training results into the pliman model repository (pliman_model_dir()).

When export_results = TRUE (default), all diagnostic plots, loss curves, confusion matrices, and performance metrics from the training run are also copied to <output_name>_results in output_dir. This allows inspecting the exported model anytime with yolo_results() or yolo_model_details().

Usage

yolo_export(
  model,
  output_dir = pliman_model_dir(),
  output_name = NULL,
  export_results = TRUE,
  imgsz = 640,
  dynamic = TRUE,
  simplify = TRUE,
  opset = 17L,
  half = FALSE,
  auto_install = TRUE
)

Arguments

model

A model checkpoint path (.pt or .onnx), a training results directory, a results .zip archive, or a yolo_train object from yolo_results() or yolo_train().

output_dir

Target directory to save the exported model and results. Defaults to pliman_model_dir().

output_name

Desired filename or identifier for the exported model (e.g. "capsulas_det" or "capsulas_det.onnx"). If NULL (default), it is inferred automatically from the model name or results folder.

export_results

Logical. If TRUE (default), copies the full training results directory (curves, metrics, confusion matrices, weights) alongside the model so that yolo_results() can load them.

imgsz

Integer image resolution (width and height) for ONNX export. Default is 640.

dynamic

Logical. If TRUE (default), exports the ONNX model with dynamic input and output axes ([batch, 3, height, width]), enabling inference with variable batch sizes and flexible image resolutions (e.g., 640, 960, 1024, 1280) in downstream functions like image_detect_dl() and video_detect_dl(). If FALSE, inputs are fixed to imgsz x imgsz.

simplify

Logical. If TRUE (default), runs graph optimization via onnxslim / onnx-simplifier to prune redundant operators, fuse layers, and reduce model file size and runtime latency.

opset

Integer specifying the ONNX operator set version (default is 17L). Modern YOLO architectures typically recommend opset 17 or 18 for compatibility with advanced operators and DirectML/CUDA.

half

Logical. If TRUE, converts model weights and graph activations to FP16 half-precision, reducing file size by approximately 50\ Defaults to FALSE (standard FP32 single precision).

auto_install

Logical. If TRUE (default), automatically installs missing Python export libraries (ultralytics, onnx, onnxslim).

Value

The full path to the exported .onnx model (invisibly).

Examples

if (FALSE) { # interactive()
library(pliman)

# 1. Export from a PyTorch checkpoint with dynamic axes
yolo_export("best.pt", output_name = "custom_model.onnx", imgsz = 640, dynamic = TRUE)

# 2. Export with FP16 half-precision for GPU optimization
yolo_export("best.pt", output_name = "custom_fp16.onnx", half = TRUE)

# 3. Register an external training folder with all results
yolo_export("path/to/model_capsulas", output_name = "capsulas_det")

# 4. Directly export from a yolo_results object
res <- yolo_results("path/to/model_capsulas")
yolo_export(res, output_name = "capsulas_det")

# 5. Inspect exported model details anytime
yolo_model_details("capsulas_det.onnx")
}