此归档版本保持可链接,以便旧版安装、搜索结果和智能体能够指向正确的文档。
复制文档LibreYOLO 是一个基于 MIT 许可的目标检测库,专注于两个旗舰模型系列:作为 CNN 旗舰的 YOLO9 与作为 Transformer 旗舰的 RF-DETR 。其 API 在预测、训练、验证和导出之间保持一致。
1 from libreyolo import LibreYOLO 2 3 model = LibreYOLO ( "LibreYOLO9c.pt" ) 4 results = model ( "image.jpg" , conf = 0.25 , save = True ) 5 print ( results . boxes . xyxy )
核心特性 对 YOLO9 和 RF-DETR 的旗舰支持 从权重自动检测模型架构、尺寸和类别数 针对大尺寸/高分辨率图像的分块推理 支持 ONNX、TorchScript、TensorRT、OpenVINO 和 NCNN 导出,并嵌入元数据 ONNX Runtime、TensorRT、OpenVINO 和 NCNN 推理后端 兼容 COCO 的验证,提供 mAP 指标 接受任意图像格式:文件路径、URL、PIL、NumPy、PyTorch 张量、原始字节
环境要求 从 PyPI 安装 从源码安装 1 git clone https://github.com/LibreYOLO/libreyolo.git 2 cd libreyolo 3 pip install -e .
可选依赖 1 2 pip install libreyolo [ onnx ] 3 4 5 6 pip install libreyolo [ rtdetr ] 7 8 9 10 pip install libreyolo [ rfdetr ] 11 12 13 14 pip install libreyolo [ tensorrt ] 15 16 17 18 pip install libreyolo [ openvino ] 19 20 21 22 pip install libreyolo [ ncnn ] 23
如果使用 uv,最可靠的方式是为每个 extra 使用独立的虚拟环境:
1 2 uv venv .venv-onnx 3 uv pip install --python .venv-onnx/bin/python -e '.[onnx]' 4 5 6 uv venv .venv-rtdetr 7 uv pip install --python .venv-rtdetr/bin/python -e '.[rtdetr]' 8 9
这样可以避免改动项目环境,并使可选依赖保持隔离。TensorRT、OpenVINO 和 NCNN 等特定厂商的 extra 仍可能需要平台特定的原生包。
加载模型并运行推理 1 from libreyolo import LibreYOLO 2 3 4 model = LibreYOLO ( "LibreYOLO9c.pt" ) 5 6 7 result = model ( "photo.jpg" ) 8 9 print ( f"Found {len(result)} objects" ) 10 print ( result . boxes . xyxy ) 11 print ( result . boxes . conf ) 12 print ( result . boxes . cls )
保存标注后的输出 1 result = model ( "photo.jpg" , save = True ) 2
处理整个目录 1 results = model ( "images/" , save = True , batch = 4 ) 2 for r in results : 3 print ( f"{r.path}: {len(r)} detections" )
v1.2.0 文档以两个旗舰模型系列为中心。YOLO9 是 CNN 旗舰,RF-DETR 是 Transformer 旗舰。其他受支持的系列以简要参考形式列出。
YOLO9 旗舰 尺寸 代码 输入尺寸 用途 Tiny "t"640 快速推理 Small "s"640 均衡 Medium "m"640 更高精度 Compact "c"640 最佳精度
1 from libreyolo import LibreYOLO 2 3 model = LibreYOLO ( "LibreYOLO9t.pt" ) 4 5 6
RF-DETR 旗舰 尺寸 代码 输入尺寸 用途 Nano "n"384 边缘端 Small "s"512 均衡 Medium "m"576 更高精度 Large "l"704 最高精度
1 from libreyolo import LibreRFDETR 2 3 model = LibreRFDETR ( size = "s" )
其他受支持的模型 系列 模型类 模型名称 YOLOX LibreYOLOXLibreYOLOXn.pt, LibreYOLOXt.pt, LibreYOLOXs.pt, LibreYOLOXm.pt, LibreYOLOXl.pt, LibreYOLOXx.pt YOLO9-E2E LibreYOLO9E2ELibreYOLO9E2Et.pt, LibreYOLO9E2Es.pt, LibreYOLO9E2Em.pt, LibreYOLO9E2Ec.pt YOLO-NAS LibreYOLONASLibreYOLONASs.pt, LibreYOLONASm.pt, LibreYOLONASl.pt, LibreYOLONASn-pose.pt, LibreYOLONASs-pose.pt, LibreYOLONASm-pose.pt, LibreYOLONASl-pose.pt D-FINE LibreDFINELibreDFINEn.pt, LibreDFINEs.pt, LibreDFINEm.pt, LibreDFINEl.pt, LibreDFINEx.pt DEIM LibreDEIMLibreDEIMn.pt, LibreDEIMs.pt, LibreDEIMm.pt, LibreDEIMl.pt, LibreDEIMx.pt DEIMv2 LibreDEIMv2LibreDEIMv2atto.pt, LibreDEIMv2femto.pt, LibreDEIMv2pico.pt, LibreDEIMv2n.pt, LibreDEIMv2s.pt, LibreDEIMv2m.pt, LibreDEIMv2l.pt, LibreDEIMv2x.pt RT-DETR LibreRTDETRLibreRTDETRr18.pt, LibreRTDETRr34.pt, LibreRTDETRr50.pt, LibreRTDETRr50m.pt, LibreRTDETRr101.pt, LibreRTDETRl.pt, LibreRTDETRx.pt RT-DETRv2 LibreRTDETRv2LibreRTDETRv2r18.pt, LibreRTDETRv2r34.pt, LibreRTDETRv2r50.pt, LibreRTDETRv2r50m.pt, LibreRTDETRv2r101.pt RT-DETRv4 LibreRTDETRv4LibreRTDETRv4s.pt, LibreRTDETRv4m.pt, LibreRTDETRv4l.pt, LibreRTDETRv4x.pt PicoDet LibrePICODETLibrePICODETs.pt, LibrePICODETm.pt, LibrePICODETl.pt EdgeCrafter LibreECLibreECs.pt, LibreECm.pt, LibreECl.pt, LibreECx.pt, LibreECs-pose.pt, LibreECm-pose.pt, LibreECl-pose.pt, LibreECx-pose.pt, LibreECs-seg.pt, LibreECm-seg.pt, LibreECl-seg.pt, LibreECx-seg.pt DAMO-YOLO LibreDAMOYOLOLibreDAMOYOLOns.pt, LibreDAMOYOLOnm.pt, LibreDAMOYOLOnl.pt, LibreDAMOYOLOt.pt, LibreDAMOYOLOs.pt, LibreDAMOYOLOm.pt, LibreDAMOYOLOl.pt RTMDet LibreRTMDetLibreRTMDett.pt, LibreRTMDets.pt, LibreRTMDetm.pt, LibreRTMDetl.pt, LibreRTMDetx.pt
工厂函数(推荐) LibreYOLO() 工厂函数会从权重文件自动检测一切:
1 from libreyolo import LibreYOLO 2 3 4 model = LibreYOLO ( "LibreYOLOXs.pt" ) 5 6 7 model = LibreYOLO ( "LibreYOLO9c.pt" ) 8 9 10 model = LibreYOLO ( "LibreRTDETRr50.pt" ) 11 12 13 model = LibreYOLO ( "/path/to/checkpoint_best_regular.pth" ) 14 15 16 model = LibreYOLO ( "model.onnx" ) 17 18 19 model = LibreYOLO ( "model.engine" ) 20 21 22 model = LibreYOLO ( "model_openvino/" ) 23 24 25 model = LibreYOLO ( "model_ncnn/" )
对于可识别的官方检查点文件名,LibreYOLO 可以自动下载缺失的权重。对于自定义文件名和 RF-DETR 检查点,建议使用明确的本地路径或特定系列的构造函数。
基本预测 1 result = model ( "image.jpg" )
全部预测参数 1 result = model ( 2 "image.jpg" , 3 conf = 0.25 , 4 iou = 0.45 , 5 imgsz = 640 , 6 classes = [ 0 , 2 , 5 ] , 7 max_det = 300 , 8 save = True , 9 output_path = "out/" , 10 color_format = "auto" , 11 output_file_format = "png" , 12 )
model.predict(...) 是 model(...) 的别名。
支持的输入格式 LibreYOLO 接受以下任意格式的图像:
1 2 result = model ( "photo.jpg" ) 3 result = model ( Path ( "photo.jpg" ) ) 4 5 6 result = model ( "https://example.com/image.jpg" ) 7 8 9 from PIL import Image 10 img = Image . open ( "photo.jpg" ) 11 result = model ( img ) 12 13 14 import numpy as np 15 arr = np . random . randint ( 0 , 255 , ( 480 , 640 , 3 ) , dtype = np . uint8 ) 16 result = model ( arr ) 17 18 19 import cv2 20 frame = cv2 . imread ( "photo.jpg" ) 21 result = model ( frame , color_format = "bgr" ) 22 23 24 import torch 25 tensor = torch . randn ( 3 , 640 , 640 ) 26 result = model ( tensor ) 27 28 29 with open ( "photo.jpg" , "rb" ) as f : 30 result = model ( f . read ( ) ) 31 32 33 results = model ( "images/" , batch = 4 )
处理结果 每次预测都会返回一个 Results 对象(处理目录时则返回它们的列表):
1 result = model ( "image.jpg" ) 2 3 4 len ( result ) 5 6 7 result . boxes . xyxy 8 9 10 result . boxes . xywh 11 12 13 result . boxes . conf 14 15 16 result . boxes . cls 17 18 19 result . boxes . data 20 21 22 result . orig_shape 23 result . path 24 result . names 25 26 27 result_cpu = result . cpu ( ) 28 boxes_np = result . boxes . numpy ( )
类别过滤 将检测结果过滤到特定的类别 ID:
1 2 result = model ( "image.jpg" , classes = [ 0 , 2 ] )
对于远大于模型输入尺寸的图像(例如卫星影像、无人机航拍),分块推理会将图像切分为相互重叠的图块,对每个图块运行检测,然后合并结果。
1 result = model ( 2 "large_aerial_image.jpg" , 3 tiling = True , 4 overlap_ratio = 0.2 , 5 save = True , 6 ) 7 8 9 result . tiled 10 result . num_tiles 11 result . saved_path 12 result . tiles_path 13 result . grid_path
在分块时设置 save=True,LibreYOLO 会保存:
final_image.jpg - 绘制了所有合并检测结果的完整图像grid_visualization.jpg - 显示图块网格叠加的图像tiles/ - 单个图块裁剪metadata.json - 分块参数和检测计数如果图像已经小于模型的输入尺寸,则会自动跳过分块。
v1.2.0 的旗舰训练路径是 YOLO9 和 RF-DETR。为保持 API 兼容性,文档仍保留了其他训练示例。
YOLOX 训练 1 from libreyolo import LibreYOLOX 2 3 model = LibreYOLOX ( size = "s" ) 4 5 results = model . train ( 6 data = "coco128.yaml" , 7 8 9 epochs = 100 , 10 batch = 16 , 11 imgsz = 640 , 12 13 14 lr0 = 0.01 , 15 optimizer = "SGD" , 16 17 18 device = "0" , 19 workers = 8 , 20 seed = 0 , 21 22 23 project = "runs/train" , 24 name = "exp" , 25 exist_ok = False , 26 27 28 amp = True , 29 patience = 50 , 30 resume = False , 31 ) 32 33 print ( f"Best mAP50-95: {results['best_mAP50_95']:.3f}" ) 34 print ( f"Best checkpoint: {results['best_checkpoint']}" )
训练完成后,模型实例会自动更新为最佳权重。
训练结果字典 1 { 2 "final_loss" : 2.31 , 3 "best_mAP50" : 0.682 , 4 "best_mAP50_95" : 0.451 , 5 "best_epoch" : 87 , 6 "save_dir" : "runs/train/exp" , 7 "best_checkpoint" : "runs/train/exp/weights/best.pt" , 8 "last_checkpoint" : "runs/train/exp/weights/last.pt" , 9 }
恢复训练 1 model = LibreYOLOX ( "runs/train/exp/weights/last.pt" , size = "s" ) 2 results = model . train ( data = "coco128.yaml" , resume = True )
自定义数据集 YAML 格式 1 path : /path/to/dataset 2 train : images/train 3 val : images/val 4 test : images/test 5 6 nc : 3 7 names : [ "cat" , "dog" , "bird" ]
YOLO9 训练 1 from libreyolo import LibreYOLO9 2 3 model = LibreYOLO9 ( "LibreYOLO9c.pt" , size = "c" ) 4 5 results = model . train ( 6 data = "coco128.yaml" , 7 epochs = 300 , 8 batch = 16 , 9 imgsz = 640 , 10 lr0 = 0.01 , 11 optimizer = "SGD" , 12 device = "0" , 13 workers = 8 , 14 seed = 0 , 15 project = "runs/train" , 16 name = "yolo9_exp" , 17 exist_ok = False , 18 resume = False , 19 amp = True , 20 patience = 50 , 21 ) 22 23 print ( f"Best mAP50-95: {results['best_mAP50_95']:.3f}" )
YOLO9 训练使用与 YOLOX 相同的参数 API,但默认 epochs=300 和 name="yolo9_exp"。它没有 pretrained 参数。
RT-DETR 训练 1 from libreyolo import LibreRTDETR 2 3 model = LibreRTDETR ( size = "r50" ) 4 5 results = model . train ( 6 data = "coco128.yaml" , 7 epochs = 72 , 8 batch = 4 , 9 imgsz = 640 , 10 lr0 = 1 e - 4 , 11 lr_backbone = 1 e - 5 , 12 optimizer = "AdamW" , 13 scheduler = "linear" , 14 device = "0" , 15 workers = 4 , 16 seed = 0 , 17 project = "runs/train" , 18 name = "rtdetr_exp" , 19 exist_ok = False , 20 pretrained = True , 21 resume = False , 22 amp = True , 23 patience = 50 , 24 )
RT-DETR 训练使用 YOLO 风格的 data.yaml 流程,但有自己的默认值,并额外增加了 lr_backbone 和 scheduler。
RF-DETR 训练 RF-DETR 使用不同的训练 API,它封装了原始的 rfdetr 实现:
1 from libreyolo import LibreRFDETR 2 3 model = LibreRFDETR ( size = "s" ) 4 5 results = model . train ( 6 data = "path/to/dataset" , 7 epochs = 100 , 8 batch_size = 4 , 9 lr = 1 e - 4 , 10 output_dir = "runs/train" , 11 )
RF-DETR 数据集使用 COCO 标注格式:
1 dataset/ 2 train/ 3 _annotations.coco.json 4 image1.jpg 5 image2.jpg 6 valid/ 7 _annotations.coco.json 8 image1.jpg
在验证集上运行 COCO 标准评估:
1 results = model . val ( 2 data = "coco128.yaml" , 3 batch = 16 , 4 imgsz = 640 , 5 conf = 0.001 , 6 iou = 0.6 , 7 split = "val" , 8 save_json = False , 9 verbose = True , 10 ) 11 12 print ( f"mAP50: {results['metrics/mAP50']:.3f}" ) 13 print ( f"mAP50-95: {results['metrics/mAP50-95']:.3f}" )
验证结果字典 默认情况下,LibreYOLO 使用 COCO 评估并返回 12 个标准指标:
1 { 2 "metrics/mAP50-95" : 0.489 , 3 "metrics/mAP50" : 0.721 , 4 "metrics/mAP75" : 0.534 , 5 "metrics/mAP_small" : 0.291 , 6 "metrics/mAP_medium" : 0.532 , 7 "metrics/mAP_large" : 0.648 , 8 "metrics/AR1" : 0.362 , 9 "metrics/AR10" : 0.571 , 10 "metrics/AR100" : 0.601 , 11 "metrics/AR_small" : 0.387 , 12 "metrics/AR_medium" : 0.641 , 13 "metrics/AR_large" : 0.739 , 14 }
在 ValidationConfig 中设置 use_coco_eval=False 可获得旧版的 precision/recall 指标。
将 PyTorch 模型导出为 ONNX、TorchScript、TensorRT、OpenVINO 或 NCNN 以便部署。
快速导出 1 2 model . export ( ) 3 4 5 model . export ( format = "torchscript" ) 6 7 8 model . export ( format = "tensorrt" ) 9 10 11 model . export ( format = "openvino" ) 12 13 14 model . export ( format = "ncnn" )
全部导出参数 1 path = model . export ( 2 format = "onnx" , 3 output_path = "model.onnx" , 4 imgsz = 640 , 5 opset = 13 , 6 simplify = True , 7 dynamic = True , 8 half = False , 9 batch = 1 , 10 device = None , 11 int8 = False , 12 data = None , 13 fraction = 1.0 , 14 workspace = 4.0 , 15 hardware_compatibility = "none" , 16 gpu_device = 0 , 17 trt_config = None , 18 verbose = False , 19 )
OpenVINO INT8 导出还需要 nncf。NCNN 导出会写入一个目录,其中包含 model.ncnn.param、model.ncnn.bin 和 metadata.yaml。
ONNX 元数据 导出的 ONNX 文件包含嵌入的元数据:
键 示例值 libreyolo_version"1.0.0"model_family"yolox"model_size"s"nb_classes"80"names'{"0": "person", "1": "bicycle", ...}'imgsz"640"dynamic"True"half"False"
使用 OnnxBackend 加载模型时,会自动读回这些元数据。
直接使用导出器工厂 1 from libreyolo . export import BaseExporter 2 3 exporter = BaseExporter . create ( "onnx" , model ) 4 path = exporter ( dynamic = True , simplify = True )
使用 ONNX Runtime 而非 PyTorch 运行推理。适用于没有 PyTorch 的部署环境。
1 from libreyolo import OnnxBackend 2 3 model = OnnxBackend ( "model.onnx" ) 4 5 result = model ( "image.jpg" , conf = 0.25 , iou = 0.45 , save = True ) 6 print ( result . boxes . xyxy )
自动元数据 如果 ONNX 文件由 LibreYOLO 导出,则会自动从嵌入的元数据中读取类别名称和类别数:
1 2 model . export ( format = "onnx" , output_path = "model.onnx" ) 3 4 5 onnx_model = OnnxBackend ( "model.onnx" ) 6 print ( onnx_model . names ) 7 print ( onnx_model . nb_classes )
对于没有元数据的 ONNX 文件(例如由其他工具导出),请手动指定 nb_classes:
1 model = OnnxBackend ( "external_model.onnx" , nb_classes = 20 )
设备选择 1 2 model = OnnxBackend ( "model.onnx" , device = "auto" ) 3 4 5 model = OnnxBackend ( "model.onnx" , device = "cpu" ) 6 7 8 model = OnnxBackend ( "model.onnx" , device = "cuda" )
预测参数 OnnxBackend 支持各运行时后端共享的核心预测 API:
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" , 10 color_format = "auto" , 11 )
运行时后端不暴露仅限 PyTorch 的选项,例如 tiling、overlap_ratio 或 output_file_format。
运行时后端处理保存的方式也与 PyTorch 包装器略有不同:如果设置 output_path,请传入最终文件路径,而非目录。如果省略它,当前后端默认保存位置在 runs/detections/ 下。
使用 TensorRT 在 NVIDIA GPU 上以最大吞吐量运行推理。需要 CUDA 以及 TensorRT 的 Python 绑定。
1 from libreyolo import TensorRTBackend 2 3 model = TensorRTBackend ( "model.engine" ) 4 5 result = model ( "image.jpg" , conf = 0.25 , iou = 0.45 , save = True ) 6 print ( result . boxes . xyxy )
通过工厂自动检测 LibreYOLO() 工厂函数会自动检测 .engine 文件:
1 from libreyolo import LibreYOLO 2 3 4 model = LibreYOLO ( "model.engine" )
TensorRTBackend 支持与 ONNX 和 OpenVINO 相同的核心运行时后端预测 API,包括 save=True 时同样的仅文件路径 output_path 行为。
使用 OpenVINO 运行推理,针对 Intel CPU、GPU 和 VPU 进行了优化。
1 from libreyolo import OpenVINOBackend 2 3 model = OpenVINOBackend ( "model_openvino/" ) 4 5 result = model ( "image.jpg" , conf = 0.25 , iou = 0.45 , save = True ) 6 print ( result . boxes . xyxy )
通过工厂自动检测 LibreYOLO() 工厂函数会自动检测 OpenVINO 模型目录:
1 from libreyolo import LibreYOLO 2 3 4 model = LibreYOLO ( "model_openvino/" )
OpenVINOBackend 在存在 metadata.yaml 时会读取它,并支持相同的核心运行时后端预测 API。
使用 NCNN 在 CPU 或支持 Vulkan 的 GPU 目标上进行轻量级部署推理。
1 from libreyolo import NcnnBackend 2 3 model = NcnnBackend ( "model_ncnn/" ) 4 5 result = model ( "image.jpg" , conf = 0.25 , iou = 0.45 , save = True ) 6 print ( result . boxes . xyxy )
通过工厂自动检测 LibreYOLO() 工厂函数会自动检测 NCNN 模型目录:
1 from libreyolo import LibreYOLO 2 3 4 model = LibreYOLO ( "model_ncnn/" )
NCNN 导出目录包含 model.ncnn.param、model.ncnn.bin,通常还有 metadata.yaml。
LibreYOLO(工厂) 1 LibreYOLO ( 2 model_path : str , 3 size : str = None , 4 reg_max : int = 16 , 5 nb_classes : int = None , 6 device : str = "auto" , 7 ) -> LibreYOLOX | LibreYOLO9 | LibreRTDETR | LibreRFDETR | OnnxBackend | TensorRTBackend | OpenVINOBackend | NcnnBackend
从权重文件自动检测模型架构、尺寸和类别数。它还可处理 .onnx、.engine、包含 model.xml 的 OpenVINO 目录,以及包含 model.ncnn.param 和 model.ncnn.bin 的 NCNN 目录。
预测(PyTorch 模型包装器) 1 model ( 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 , 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 ]
预测(运行时后端) 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 , 12 color_format : str = "auto" , 13 ) -> Results | list [ Results ]
如果运行时后端省略 output_path,当前默认保存位置为 runs/detections/。
Results 1 result = Results ( 2 boxes : Boxes , 3 orig_shape : tuple [ int , int ] , 4 path : str | None , 5 names : dict [ int , str ] , 6 ) 7 8 len ( result ) 9 result . cpu ( )
Boxes 1 boxes = Boxes ( boxes , conf , cls ) 2 3 boxes . xyxy 4 boxes . xywh 5 boxes . conf 6 boxes . cls 7 boxes . data 8 9 len ( boxes ) 10 boxes . cpu ( ) 11 boxes . numpy ( )
model.export() 1 model . export ( 2 format : str = "onnx" , 3 * , 4 output_path : str = None , 5 imgsz : int = None , 6 opset : int = 13 , 7 simplify : bool = True , 8 dynamic : bool = True , 9 half : bool = False , 10 batch : int = 1 , 11 device : str = None , 12 int8 : bool = False , 13 data : str = None , 14 fraction : float = 1.0 , 15 workspace : float = 4.0 , 16 hardware_compatibility : str = "none" , 17 gpu_device : int = 0 , 18 trt_config = None , 19 verbose : bool = False , 20 ) -> str
BaseExporter 1 from libreyolo . export import BaseExporter 2 3 exporter = BaseExporter . create ( "onnx" , model ) 4 path = exporter ( dynamic = True , simplify = True ) 5 6 BaseExporter . create ( "ncnn" , model ) ( output_path = "model_ncnn" )
model.val() 1 model . val ( 2 data : str = None , 3 batch : int = 16 , 4 imgsz : int = None , 5 conf : float = 0.001 , 6 iou : float = 0.6 , 7 device : str = None , 8 split : str = "val" , 9 save_json : bool = False , 10 verbose : bool = True , 11 ) -> dict
返回值(COCO 评估,默认):
1 { 2 "metrics/mAP50-95" : float , 3 "metrics/mAP50" : float , 4 "metrics/mAP75" : float , 5 "metrics/mAP_small" : float , 6 "metrics/mAP_medium" : float , 7 "metrics/mAP_large" : float , 8 "metrics/AR1" : float , 9 "metrics/AR10" : float , 10 "metrics/AR100" : float , 11 "metrics/AR_small" : float , 12 "metrics/AR_medium" : float , 13 "metrics/AR_large" : float , 14 }
model.train()(YOLOX) 1 model . train ( 2 data : str , 3 * , 4 epochs : int = 100 , 5 batch : int = 16 , 6 imgsz : int = 640 , 7 lr0 : float = 0.01 , 8 optimizer : str = "SGD" , 9 device : str = "" , 10 workers : int = 8 , 11 seed : int = 0 , 12 project : str = "runs/train" , 13 name : str = "exp" , 14 exist_ok : bool = False , 15 pretrained : bool = True , 16 resume : bool = False , 17 amp : bool = True , 18 patience : int = 50 , 19 ) -> dict
返回值:
1 { 2 "final_loss" : float , 3 "best_mAP50" : float , 4 "best_mAP50_95" : float , 5 "best_epoch" : int , 6 "save_dir" : str , 7 "best_checkpoint" : str , 8 "last_checkpoint" : str , 9 }
model.train()(YOLO9) 1 model . train ( 2 data : str , 3 * , 4 epochs : int = 300 , 5 batch : int = 16 , 6 imgsz : int = 640 , 7 lr0 : float = 0.01 , 8 optimizer : str = "SGD" , 9 device : str = "" , 10 workers : int = 8 , 11 seed : int = 0 , 12 project : str = "runs/train" , 13 name : str = "yolo9_exp" , 14 exist_ok : bool = False , 15 resume : bool = False , 16 amp : bool = True , 17 patience : int = 50 , 18 ) -> dict
返回与 YOLOX 训练相同的字典。
model.train()(RT-DETR) 1 model . train ( 2 data : str , 3 * , 4 epochs : int = 72 , 5 batch : int = 4 , 6 imgsz : int = 640 , 7 lr0 : float = 1 e - 4 , 8 lr_backbone : float = 1 e - 5 , 9 optimizer : str = "AdamW" , 10 scheduler : str = "linear" , 11 device : str = "" , 12 workers : int = 4 , 13 seed : int = 0 , 14 project : str = "runs/train" , 15 name : str = "rtdetr_exp" , 16 exist_ok : bool = False , 17 pretrained : bool = True , 18 resume : bool = False , 19 amp : bool = True , 20 patience : int = 50 , 21 ) -> dict
model.train()(RF-DETR) 1 model . train ( 2 data : str , 3 epochs : int = 100 , 4 batch_size : int = 4 , 5 lr : float = 1 e - 4 , 6 output_dir : str = "runs/train" , 7 resume : str = None , 8 ** kwargs , 9 ) -> dict
OnnxBackend 1 OnnxBackend ( 2 onnx_path : str , 3 nb_classes : int = 80 , 4 device : str = "auto" , 5 )
使用 ONNX Runtime 对 ONNX 模型运行推理。支持上面所示的运行时后端预测 API。
TensorRTBackend 1 TensorRTBackend ( 2 engine_path : str , 3 nb_classes : int | None = None , 4 device : str = "auto" , 5 )
对 TensorRT .engine 文件运行推理,并可从相邻的 .json 附属文件读取元数据。
OpenVINOBackend 1 OpenVINOBackend ( 2 model_dir : str , 3 nb_classes : int | None = None , 4 device : str = "auto" , 5 )
对包含 model.xml 以及可选 metadata.yaml 的 OpenVINO 模型目录运行推理。
NcnnBackend 1 NcnnBackend ( 2 model_dir : str , 3 nb_classes : int | None = None , 4 device : str = "auto" , 5 )
对包含 model.ncnn.param、model.ncnn.bin 以及可选 metadata.yaml 的 NCNN 模型目录运行推理。
ValidationConfig 1 from libreyolo import ValidationConfig 2 3 config = ValidationConfig ( 4 data = "coco128.yaml" , 5 data_dir = None , 6 batch_size = 16 , 7 imgsz = 640 , 8 conf_thres = 0.001 , 9 iou_thres = 0.6 , 10 max_det = 300 , 11 split = "val" , 12 device = "auto" , 13 save_json = False , 14 verbose = True , 15 half = False , 16 use_coco_eval = True , 17 num_workers = 4 , 18 ) 19 20 21 config = ValidationConfig . from_yaml ( "config.yaml" ) 22 config . to_yaml ( "config.yaml" )
本节面向希望了解代码库内部实现的贡献者。
基类设计 PyTorch 模型系列继承自 libreyolo/models/base/model.py 中的 BaseModel。子类需实现以下抽象方法:
方法 作用 _init_model()构建并返回 nn.Module _get_available_layers()返回层名到模块的映射 _get_preprocess_numpy()返回用于导出/校准的 NumPy 预处理器 _preprocess()图像到张量的转换 _forward()模型前向传播 _postprocess()原始输出到检测字典
BaseModel 提供共享的包装行为:预测、导出、验证、尺寸/名称元数据以及训练辅助函数。实际的单图、批量和分块推理流程位于 libreyolo/models/base/inference.py,而部署运行时位于 libreyolo/backends/ 下。
包结构 1 libreyolo/ 2 __init__.py # Public API exports 3 models/ 4 __init__.py # LibreYOLO() factory + model registry bootstrap 5 base/ 6 model.py # BaseModel 7 inference.py # Shared prediction pipeline 8 yolox/ 9 model.py 10 nn.py 11 utils.py 12 yolo9/ 13 model.py 14 nn.py 15 utils.py 16 rtdetr/ 17 model.py 18 nn.py 19 trainer.py 20 utils.py 21 rfdetr/ 22 model.py 23 utils.py 24 train.py 25 backends/ 26 base.py # BaseBackend runtime wrapper 27 onnx.py # OnnxBackend 28 tensorrt.py # TensorRTBackend 29 openvino.py # OpenVINOBackend 30 ncnn.py # NcnnBackend 31 utils/ 32 results.py # Results and Boxes classes 33 image_loader.py # Unified image loading 34 general.py # Path helpers, NMS, tiling utilities 35 export/ 36 exporter.py # BaseExporter and format registry 37 onnx.py # ONNX export logic 38 torchscript.py # TorchScript export logic 39 tensorrt.py # TensorRT export logic 40 openvino.py # OpenVINO export logic 41 ncnn.py # NCNN export logic 42 training/ 43 config.py # YOLOXTrainConfig / YOLOv9TrainConfig 44 trainer.py # YOLOXTrainer 45 v9_trainer.py # YOLOv9Trainer 46 dataset.py # Training dataset 47 augment.py # Mosaic, mixup, etc. 48 loss.py # YOLOX loss functions 49 scheduler.py # LR schedulers 50 ema.py # Exponential moving average 51 validation/ 52 config.py # ValidationConfig 53 detection_validator.py # DetectionValidator 54 metrics.py # DetMetrics, mAP computation 55 base.py # BaseValidator 56 preprocessors.py # Per-model val preprocessing 57 data/ 58 utils.py # Dataset loading, YAML parsing 59 yolo_coco_api.py # YOLO-to-COCO annotation bridge 60 config/ 61 datasets/ # Built-in dataset YAML configs (coco8, coco128, coco5000, coco, etc.)
添加新的模型系列 1 在 libreyolo/models/newmodel/model.py 中创建一个继承 BaseModel 的类 2 实现所有抽象方法 3 在 libreyolo/models/newmodel/ 下创建配套的网络和工具 4 将导入添加到 libreyolo/models/__init__.py,以便注册表能识别它 5 从 libreyolo/__init__.py 导出该类 6 (可选)如果验证预处理与标准流程不同,可重写 val_preprocessor_class 导出架构 libreyolo/export/exporter.py 中的 BaseExporter 是导出入口。具体的导出器通过子类注册机制自行注册,调用方使用 BaseExporter.create(format, model) 获取正确的实现:
1 from libreyolo . export import BaseExporter 2 3 onnx_exporter = BaseExporter . create ( "onnx" , model ) 4 ncnn_exporter = BaseExporter . create ( "ncnn" , model )
要添加新的导出格式,请实现一个具有唯一 format_name 的新 BaseExporter 子类,并从 libreyolo/export/exporter.py 导入它,以填充注册表。
YOLO 风格的模型使用通过 data.yaml 配置的数据集。RF-DETR 使用 COCO 格式的标注,将在下文单独说明。
data.yaml 结构 1 path : /absolute/path/to/dataset 2 train : images/train 3 val : images/val 4 test : images/test 5 6 nc : 80 7 names : [ 8 "person" , "bicycle" , "car" , "motorcycle" , "airplane" , 9 "bus" , "train" , "truck" , "boat" , "traffic light" , 10 11 ]
文件列表变体 相同的 YAML 格式也可以让 train、val 或 test 指向每行包含一个图像路径的 .txt 文件:
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" , "..." ]
目录布局 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
标签格式 每张图像对应一个文本文件。每一行表示一个目标:
1 <class_id> <center_x> <center_y> <width> <height>
所有坐标都相对于图像尺寸归一化到 [0, 1]。
示例(img001.txt):
1 0 0.5 0.4 0.3 0.6 2 2 0.1 0.2 0.05 0.1
内置数据集 LibreYOLO 在 libreyolo/config/datasets/ 下附带内置数据集配置,并可在首次使用时自动下载受支持的数据集:
1 2 results = model . val ( data = "coco8.yaml" ) 3 results = model . train ( data = "coco128.yaml" , epochs = 10 )
RF-DETR 数据集格式 RF-DETR 使用 COCO 格式的标注(JSON),而非 YOLO 文本标签:
1 dataset/ 2 train/ 3 _annotations.coco.json 4 image1.jpg 5 valid/ 6 _annotations.coco.json 7 image1.jpg