yolo_export_fp16() converts a YOLO model (.pt PyTorch weights or .onnx model)
to 16-bit half precision (FLOAT16) ONNX format.
The exported model is saved by default in pliman_model_dir(), so it can be used
right away by name in any pliman inference function (e.g.
image_detect_dl(img, model = "yolo26x_fp16.onnx")).
Usage
yolo_export_fp16(
model,
output = NULL,
imgsz = 640,
dynamic = FALSE,
simplify = TRUE,
opset = 17L,
verbose = TRUE
)Arguments
- model
Character string. File path to a
.ptPyTorch weights file or.onnxmodel, or the name of an installed model inpliman_model_dir().- output
Optional destination file path for the exported
.onnxmodel. IfNULL(default), saves the model inpliman_model_dir()appending"_fp16.onnx"to the model name.- imgsz
Integer or vector of two integers. Inference image resolution
c(height, width)or single integer for square inputs. Defaults to640.- dynamic
Logical. Enable dynamic input axes. Defaults to
FALSE.- simplify
Logical. Simplify ONNX graph using
onnxslim. Defaults toTRUE.- opset
Integer. ONNX opset version. Defaults to
17L.- verbose
Logical. Display conversion metrics and file size reduction. Defaults to
TRUE.
What FP16 always gives you
~50% smaller files and ~50% less VRAM for the weights (e.g. YOLO26x: 212.9 MB to 106.5 MB; YOLO11n: 10.2 MB to 5.2 MB). This lets larger models, or several models at once, fit in GPUs with limited memory (e.g. 4 GB cards).
Practically identical predictions. Boxes, classes and scores match the FP32 model up to small rounding differences (e.g. a score of 0.9666 in FP32 vs 0.9658 in FP16).
Portability. The
.onnxfile runs on any machine supported by pliman and can be shared (e.g. on Hugging Face), unlike TensorRT.enginefiles, which are tied to one GPU/driver/CUDA combination (seeyolo_export_engine()).
Speed depends on the hardware
FP16 is not guaranteed to be faster. The speed-up comes from dedicated half-precision units, so the result depends on the GPU:
GPUs with Tensor Cores (NVIDIA RTX 20xx/30xx/40xx/50xx, Quadro/RTX A-series, data-center GPUs such as T4/A100): FP16 is expected to be faster than FP32, mainly for large models (
m,l,xvariants) and high resolutions.GPUs without Tensor Cores (e.g. NVIDIA GTX 10xx/16xx, many integrated GPUs): FP16 is usually equal or slower than FP32. Example measured with YOLO26x on a GTX 1650 via DirectML (
bench::mark(), 30 iterations): FP32 median 102 ms vs FP16 median 127 ms (~25% slower).CPU (
use_gpu = FALSE): most CPUs have no fast FP16 arithmetic, so FP16 models are typically slower than FP32 on CPU. For CPU speed-ups use INT8 quantization instead (onnx_quantize(),model_to_int8()).
Small models (n, s) are often limited by pre/post-processing rather than by the
network itself, so gains from FP16 are small even on Tensor Core GPUs.
Recommendation: keep both versions and benchmark them on your own machine before switching (see Examples). Use FP16 when it is faster on your GPU or when you need to save VRAM/disk; otherwise keep FP32.
Conversion details
.ptcheckpoints are exported with Ultralytics (format = "onnx", half = TRUE)..onnxmodels are converted with onnxconverter_common. Models with opset < 17 are upgraded to opset 17, internalCastnodes are aligned to FLOAT16, stale shape information is removed, and the original metadata (classes, stride, image size, task) is preserved soyolo_model_details()keeps working.Input and output tensors of the FP16 model are FLOAT16. pliman converts the image to half precision and the predictions back to single precision automatically, so no change is needed in your code.
Required Python packages:
pip install onnx onnxconverter_common(plusultralyticsfor.ptfiles).
See also
model_to_fp16() for batch conversion, onnx_quantize() / model_to_int8()
for INT8 (CPU speed-up), yolo_export_engine() for TensorRT engines.
Examples
if (FALSE) { # interactive()
library(pliman)
# 1. Convert a YOLO ONNX model (or .pt checkpoint) to FP16.
# The file is saved in pliman_model_dir() as "yolo26x_fp16.onnx".
fp16_model <- yolo_export_fp16("yolo26x.onnx")
# 2. Use it by name, exactly like the FP32 model
img <- image_pliman("apartment.jpeg")
det <- image_detect_dl(img, model = "yolo26x_fp16.onnx")
# 3. Benchmark FP16 vs FP32 on YOUR hardware before switching
# (FP16 is only expected to be faster on GPUs with Tensor Cores)
bench::mark(
fp16 = image_detect_dl(img, model = "yolo26x_fp16.onnx", plot = FALSE),
fp32 = image_detect_dl(img, model = "yolo26x.onnx", plot = FALSE),
iterations = 30,
check = FALSE
)
# 4. Compare model metadata and tensor types
yolo_model_details("yolo26x_fp16.onnx")
}
