LibreYOLO is an MIT-licensed object detection library that provides a unified Python API across four architectures: YOLOX, YOLOv9, RT-DETR, and RF-DETR. One interface for prediction, training, validation, and export, regardless of which model family you use.
python
1
fromlibreyoloimportLibreYOLO
2
3
model=LibreYOLO("LibreYOLOXs.pt")
4
results=model("image.jpg",conf=0.25,save=True)
5
print(results.boxes.xyxy)
Key features
Unified API across YOLOX, YOLOv9, RT-DETR, and RF-DETR
Auto-detection of model architecture, size, and class count from weights
Tiled inference for large/high-resolution images
ONNX, TorchScript, TensorRT, OpenVINO, and NCNN export with embedded metadata
ONNX Runtime, TensorRT, OpenVINO, and NCNN inference backends
COCO-compatible validation with mAP metrics
Accepts any image format: file paths, URLs, PIL, NumPy, PyTorch tensors, raw bytes
# Repeat with .[rfdetr], .[openvino], .[ncnn], or .[tensorrt] as needed
This avoids mutating the project environment and keeps optional dependencies isolated. Vendor-specific extras such as TensorRT, OpenVINO, and NCNN may still require platform-specific native packages.
Quickstart
Load a model and run inference
python
1
fromlibreyoloimportLibreYOLO
2
3
# Auto-detects architecture and size from the weights file
4
model=LibreYOLO("LibreYOLOXs.pt")
5
6
# Run on a single image
7
result=model("photo.jpg")
8
9
print(f"Found {len(result)} objects")
10
print(result.boxes.xyxy)# bounding boxes (N, 4)
11
print(result.boxes.conf)# confidence scores (N,)
12
print(result.boxes.cls)# class IDs (N,)
Save annotated output
python
1
result=model("photo.jpg",save=True)
2
# Saved under runs/detect/predict*/photo.jpg by default
Process a directory
python
1
results=model("images/",save=True,batch=4)
2
forrinresults:
3
print(f"{r.path}: {len(r)} detections")
Available Models
YOLOX
Size
Code
Input size
Use case
Nano
"n"
416
Edge devices, mobile
Tiny
"t"
416
Edge devices, faster
Small
"s"
640
Balanced speed/accuracy
Medium
"m"
640
Higher accuracy
Large
"l"
640
High accuracy
XLarge
"x"
640
Maximum accuracy
python
1
fromlibreyoloimportLibreYOLO
2
3
model=LibreYOLO("LibreYOLOXn.pt")
4
# model = LibreYOLO("LibreYOLOXt.pt")
5
# model = LibreYOLO("LibreYOLOXs.pt")
6
# model = LibreYOLO("LibreYOLOXm.pt")
7
# model = LibreYOLO("LibreYOLOXl.pt")
8
# model = LibreYOLO("LibreYOLOXx.pt")
YOLOv9
Size
Code
Input size
Use case
Tiny
"t"
640
Fast inference
Small
"s"
640
Balanced
Medium
"m"
640
Higher accuracy
Compact
"c"
640
Best accuracy
python
1
fromlibreyoloimportLibreYOLO
2
3
model=LibreYOLO("LibreYOLO9t.pt")
4
# model = LibreYOLO("LibreYOLO9s.pt")
5
# model = LibreYOLO("LibreYOLO9m.pt")
6
# model = LibreYOLO("LibreYOLO9c.pt")
RT-DETR
Size
Code
Input size
Use case
ResNet-18
"r18"
640
Fastest RT-DETR variant
ResNet-34
"r34"
640
Balanced speed/accuracy
ResNet-50
"r50"
640
General-purpose default
ResNet-50-m
"r50m"
640
Higher accuracy
ResNet-101
"r101"
640
Largest RT-DETR variant
python
1
fromlibreyoloimportLibreYOLORTDETR
2
3
model=LibreYOLORTDETR(size="r50")
RF-DETR
Size
Code
Input size
Use case
Nano
"n"
384
Edge
Small
"s"
512
Balanced
Medium
"m"
576
Higher accuracy
Large
"l"
704
Maximum accuracy
python
1
fromlibreyoloimportLibreYOLORFDETR
2
3
model=LibreYOLORFDETR(size="s")
Factory function (recommended)
The LibreYOLO() factory auto-detects everything from the weights file:
python
1
fromlibreyoloimportLibreYOLO
2
3
# Auto-detects: YOLOX, size=s, 80 classes
4
model=LibreYOLO("LibreYOLOXs.pt")
5
6
# Auto-detects: YOLOv9, size=c, 80 classes
7
model=LibreYOLO("LibreYOLO9c.pt")
8
9
# Auto-detects: RT-DETR
10
model=LibreYOLO("LibreRTDETRr50.pt")
11
12
# RF-DETR checkpoints also work when you point at an actual checkpoint file
# NCNN models (directory with model.ncnn.param + model.ncnn.bin)
25
model=LibreYOLO("model_ncnn/")
For recognized official checkpoint filenames, LibreYOLO can auto-download missing weights. For custom filenames and RF-DETR checkpoints, prefer explicit local paths or the family-specific constructors.
Every prediction returns a Results object (or a list of them for directories):
python
1
result=model("image.jpg")
2
3
# Number of detections
4
len(result)# e.g., 5
5
6
# Bounding boxes in xyxy format (x1, y1, x2, y2)
7
result.boxes.xyxy# tensor of shape (N, 4)
8
9
# Bounding boxes in xywh format (center_x, center_y, width, height)
10
result.boxes.xywh# tensor of shape (N, 4)
11
12
# Confidence scores
13
result.boxes.conf# tensor of shape (N,)
14
15
# Class IDs
16
result.boxes.cls# tensor of shape (N,)
17
18
# Combined data: [x1, y1, x2, y2, conf, cls]
19
result.boxes.data# tensor of shape (N, 6)
20
21
# Metadata
22
result.orig_shape# (height, width) of original image
23
result.path# source file path (or None)
24
result.names# {0: "person", 1: "bicycle", ...}
25
26
# Move to CPU / convert to numpy
27
result_cpu=result.cpu()
28
boxes_np=result.boxes.numpy()
Class filtering
Filter detections to specific class IDs:
python
1
# Only detect people (class 0) and cars (class 2)
2
result=model("image.jpg",classes=[0,2])
Tiled Inference
For images much larger than the model's input size (e.g., satellite imagery, drone footage), tiled inference splits the image into overlapping tiles, runs detection on each, and merges results.
python
1
result=model(
2
"large_aerial_image.jpg",
3
tiling=True,
4
overlap_ratio=0.2,# 20% overlap between tiles (default)
5
save=True,
6
)
7
8
# Extra metadata on tiled results
9
result.tiled# True
10
result.num_tiles# number of tiles used
11
result.saved_path# output directory when save=True
OnnxBackend supports the core prediction API shared by the runtime backends:
python
1
result=model(
2
"image.jpg",
3
conf=0.25,
4
iou=0.45,
5
imgsz=640,
6
classes=[0,2],
7
max_det=300,
8
save=True,
9
output_path="output/annotated.jpg",# final file path when save=True
10
color_format="auto",
11
)
Runtime backends do not expose PyTorch-only options such as tiling, overlap_ratio, or output_file_format.
Runtime backends also handle saving a little differently from the PyTorch wrappers: if you set output_path, pass a final file path, not a directory. If you omit it, the current backend default is under runs/detections/.
TensorRT Inference
Run inference using TensorRT for maximum throughput on NVIDIA GPUs. Requires CUDA plus the TensorRT Python bindings.
The LibreYOLO() factory automatically detects .engine files:
python
1
fromlibreyoloimportLibreYOLO
2
3
# Auto-detects TensorRT engine
4
model=LibreYOLO("model.engine")
TensorRTBackend supports the same core runtime-backend prediction API as ONNX and OpenVINO, including the same file-path-only output_path behavior for save=True.
OpenVINO Inference
Run inference using OpenVINO, optimized for Intel CPUs, GPUs, and VPUs.
Auto-detects model architecture, size, and class count from the weights file. It also handles .onnx, .engine, OpenVINO directories containing model.xml, and NCNN directories containing model.ncnn.param plus model.ncnn.bin.
Prediction (PyTorch model wrappers)
python
1
model(
2
source,# image input (see supported formats)
3
*,
4
conf:float=0.25,
5
iou:float=0.45,
6
imgsz:int=None,
7
classes:list[int]=None,
8
max_det:int=300,
9
save:bool=False,
10
batch:int=1,
11
output_path:str=None,
12
color_format:str="auto",
13
tiling:bool=False,
14
overlap_ratio:float=0.2,
15
output_file_format:str=None,
16
)->Results|list[Results]
Prediction (runtime backends)
python
1
backend(
2
source,
3
*,
4
conf:float=0.25,
5
iou:float=0.45,
6
imgsz:int=None,
7
classes:list[int]=None,
8
max_det:int=300,
9
save:bool=False,
10
batch:int=1,
11
output_path:str=None,# final file path when save=True
12
color_format:str="auto",
13
)->Results|list[Results]
If output_path is omitted for a runtime backend, the current default save location is runs/detections/.
Results
python
1
result=Results(
2
boxes:Boxes,
3
orig_shape:tuple[int,int],# (height, width)
4
path:str|None,
5
names:dict[int,str],
6
)
7
8
len(result)# number of detections
9
result.cpu()# copy with tensors on CPU
Boxes
python
1
boxes=Boxes(boxes,conf,cls)
2
3
boxes.xyxy# (N, 4) tensor — x1, y1, x2, y2
4
boxes.xywh# (N, 4) tensor — cx, cy, w, h
5
boxes.conf# (N,) tensor — confidence scores
6
boxes.cls# (N,) tensor — class IDs
7
boxes.data# (N, 6) tensor — [xyxy, conf, cls]
8
9
len(boxes)# number of boxes
10
boxes.cpu()# copy on CPU
11
boxes.numpy()# copy as numpy arrays
model.export()
python
1
model.export(
2
format:str="onnx",# "onnx", "torchscript", "tensorrt", "openvino", or "ncnn"
nb_classes:int=80,# auto-read from metadata if available
4
device:str="auto",
5
)
Runs inference on an ONNX model with ONNX Runtime. Supports the runtime-backend prediction API shown above.
TensorRTBackend
python
1
TensorRTBackend(
2
engine_path:str,
3
nb_classes:int|None=None,
4
device:str="auto",
5
)
Runs inference on a TensorRT .engine file and can read metadata from an adjacent .json sidecar.
OpenVINOBackend
python
1
OpenVINOBackend(
2
model_dir:str,
3
nb_classes:int|None=None,
4
device:str="auto",
5
)
Runs inference on an OpenVINO model directory containing model.xml and optionally metadata.yaml.
NcnnBackend
python
1
NcnnBackend(
2
model_dir:str,
3
nb_classes:int|None=None,
4
device:str="auto",
5
)
Runs inference on an NCNN model directory containing model.ncnn.param, model.ncnn.bin, and optionally metadata.yaml.
ValidationConfig
python
1
fromlibreyoloimportValidationConfig
2
3
config=ValidationConfig(
4
data="coco128.yaml",
5
data_dir=None,# override dataset root directory
6
batch_size=16,
7
imgsz=640,
8
conf_thres=0.001,
9
iou_thres=0.6,
10
max_det=300,
11
split="val",# "val", "test", or "train"
12
device="auto",
13
save_json=False,
14
verbose=True,
15
half=False,
16
use_coco_eval=True,# use COCO eval (12 keys); False for legacy
17
num_workers=4,
18
)
19
20
# Load/save YAML
21
config=ValidationConfig.from_yaml("config.yaml")
22
config.to_yaml("config.yaml")
Architecture Guide
This section is for contributors who want to understand the codebase internals.
Base class design
PyTorch model families inherit from BaseModel in libreyolo/models/base/model.py. Subclasses implement these abstract methods:
Method
Purpose
_init_model()
Build and return the nn.Module
_get_available_layers()
Return layer-name to module mapping
_get_preprocess_numpy()
Return the NumPy preprocessor used for export / calibration
_preprocess()
Image to tensor conversion
_forward()
Model forward pass
_postprocess()
Raw output to detection dicts
BaseModel provides the shared wrapper behavior: prediction, export, validation, size/name metadata, and training helpers. The actual single-image, batch, and tiled inference flow lives in libreyolo/models/base/inference.py, while deployment runtimes live under libreyolo/backends/.
Package structure
text
1
libreyolo/
2
__init__.py # Public API exports
3
models/
4
__init__.py # LibreYOLO() factory + model registry bootstrap
1Create libreyolo/models/newmodel/model.py with a class inheriting BaseModel
2Implement all abstract methods
3Create the supporting network and utilities under libreyolo/models/newmodel/
4Add the import to libreyolo/models/__init__.py so the registry sees it
5Export the class from libreyolo/__init__.py
6(Optional) Override val_preprocessor_class if validation preprocessing differs from the standard path
Export architecture
BaseExporter in libreyolo/export/exporter.py is the export entrypoint. Concrete exporters register themselves through subclass registration, and callers use BaseExporter.create(format, model) to get the right implementation:
python
1
fromlibreyolo.exportimportBaseExporter
2
3
onnx_exporter=BaseExporter.create("onnx",model)
4
ncnn_exporter=BaseExporter.create("ncnn",model)
To add a new export format, implement a new BaseExporter subclass with a unique format_name and import it from libreyolo/export/exporter.py so the registry is populated.
Dataset Format
YOLOX, YOLOv9, and RT-DETR use YOLO-style datasets configured via data.yaml. RF-DETR uses COCO-format annotations and is documented separately below.
data.yaml structure
data.yaml
1
path:/absolute/path/to/dataset# dataset root
2
train:images/train# directory path, relative to path
3
val:images/val# directory path, relative to path
4
test:images/test# optional
5
6
nc:80# number of classes
7
names:[# class names
8
"person","bicycle","car","motorcycle","airplane",
9
"bus","train","truck","boat","traffic light",
10
# ...
11
]
File-list variant
The same YAML format can also point train, val, or test at .txt files containing one image path per line:
coco.yaml
1
path:/absolute/path/to/coco
2
train:train2017.txt
3
val:val2017.txt
4
test:test-dev2017.txt
5
6
nc:80
7
names:["person","bicycle","car","..."]
Directory layout
text
1
dataset/
2
images/
3
train/
4
img001.jpg
5
img002.jpg
6
val/
7
img003.jpg
8
labels/
9
train/
10
img001.txt
11
img002.txt
12
val/
13
img003.txt
Label format
One text file per image. Each line is one object:
text
1
<class_id> <center_x> <center_y> <width> <height>
All coordinates are normalized to [0, 1] relative to image dimensions.
Example (img001.txt):
img001.txt
1
0 0.5 0.4 0.3 0.6
2
2 0.1 0.2 0.05 0.1
Built-in datasets
LibreYOLO ships built-in dataset configs under libreyolo/config/datasets/ and can auto-download supported datasets on first use: