Link to this section目标检测#
目标检测是一项涉及识别图像或视频流中对象位置和类别的任务。
目标检测器的输出是一组包围图像中对象的边界框,以及每个框的类别标签和置信度分数。当你需要识别场景中的目标对象,但不需要确切知道该对象的位置或其精确形状时,目标检测是一个不错的选择。
Watch: Object Detection with Pretrained Ultralytics YOLO Model.
YOLO26 Detect 模型是默认的 YOLO26 模型,即yolo26n.pt,它们是在COCO数据集上预训练的。
Link to this section模型#
这里展示了 YOLO26 预训练的 Detect 模型。Detect、Segment 和 Pose 模型是在COCO数据集上预训练的,Semantic模型是在Cityscapes上预训练的,而 Classify 模型是在ImageNet数据集上预训练的。
模型在首次使用时会自动从最新的 Ultralytics 发布版本下载。
| 模型 | 尺寸 (像素) | mAPval 50-95 | mAPval 50-95(e2e) | 速度 CPU ONNX (ms) | 速度 T4 TensorRT10 (ms) | 参数 (M) | FLOPs (B) |
|---|---|---|---|---|---|---|---|
| YOLO26n | 640 | 40.9 | 40.1 | 38.9 ± 0.7 | 1.7 ± 0.0 | 2.4 | 5.4 |
| YOLO26s | 640 | 48.6 | 47.8 | 87.2 ± 0.9 | 2.5 ± 0.0 | 9.5 | 20.7 |
| YOLO26m | 640 | 53.1 | 52.5 | 220.0 ± 1.4 | 4.7 ± 0.1 | 20.4 | 68.2 |
| YOLO26l | 640 | 55.0 | 54.4 | 286.2 ± 2.0 | 6.2 ± 0.2 | 24.8 | 86.4 |
| YOLO26x | 640 | 57.5 | 56.9 | 525.8 ± 4.0 | 11.8 ± 0.2 | 55.7 | 193.9 |
- mAPval 数值是针对COCO val2017数据集上的单模型单尺度结果。
通过运行yolo val detect data=coco.yaml device=0进行复现。 - 速度是通过使用Amazon EC2 P4d实例对 COCO val 图像进行平均得出的。
通过运行yolo val detect data=coco.yaml batch=1 device=0|cpu进行复现。 - 参数和FLOPs数值是针对执行
model.fuse()后的融合模型,该操作合并了 Conv 和 BatchNorm 层,对于端到端模型,它移除了辅助的一对多检测头。预训练检查点保留了完整的训练架构,数值可能会更高。
Link to this section训练#
在 COCO8 数据集上,以 640 的图像尺寸训练 YOLO26n 模型 100 个epoch。如需查看可用参数的完整列表,请参阅配置页面。
from ultralytics import YOLO
# Load a model
model = YOLO("yolo26n.yaml") # build a new model from YAML
model = YOLO("yolo26n.pt") # load a pretrained model (recommended for training)
model = YOLO("yolo26n.yaml").load("yolo26n.pt") # build from YAML and transfer weights
# Train the model
results = model.train(data="coco8.yaml", epochs=100, imgsz=640)See full train mode details in the Train page. Detection models can also be trained on cloud GPUs through Ultralytics Platform.
Link to this section数据集格式#
YOLO 检测数据集格式的详细信息可以在数据集指南中找到。要将现有数据集从其他格式(如 COCO 等)转换为 YOLO 格式,请使用 Ultralytics 提供的JSON2YOLO工具。你还可以通过Ultralytics 平台使用 AI 辅助标注工具直接标注和管理检测数据集。
Link to this section验证#
Validate trained YOLO26n model accuracy on the COCO8 dataset. No arguments are needed as the model retains its training data and arguments as model attributes.
from ultralytics import YOLO
# Load a model
model = YOLO("yolo26n.pt") # load an official model
model = YOLO("path/to/best.pt") # load a custom model
# Validate the model
metrics = model.val() # no arguments needed, dataset and settings remembered
metrics.box.map # map50-95
metrics.box.map50 # map50
metrics.box.map75 # map75
metrics.box.maps # a list containing mAP50-95 for each category
metrics.box.image_metrics # per-image metrics dictionary with precision, recall, F1, TP, FP, and FNLink to this section预测#
使用已训练的 YOLO26n 模型对图像运行预测。
from ultralytics import YOLO
# Load a model
model = YOLO("yolo26n.pt") # load an official model
model = YOLO("path/to/best.pt") # load a custom model
# Predict with the model
results = model("https://ultralytics.com/images/bus.jpg") # predict on an image
# Access the results
for result in results:
xywh = result.boxes.xywh # center-x, center-y, width, height
xywhn = result.boxes.xywhn # normalized
xyxy = result.boxes.xyxy # top-left-x, top-left-y, bottom-right-x, bottom-right-y
xyxyn = result.boxes.xyxyn # normalized
names = [result.names[cls.item()] for cls in result.boxes.cls.int()] # class name of each box
confs = result.boxes.conf # confidence score of each boxSee full predict mode details in the Predict page.
Link to this section结果输出#
目标检测为每张图像返回一个 Results 对象。主要的预测字段是 result.boxes,其中包含每个检测到对象的目标框坐标、类别 ID 和置信度分数。
| 属性 | 类型 | 形状 | 描述 |
|---|---|---|---|
result.boxes | Boxes | (N) | 检测框。 |
result.boxes.data | torch.float32 | (N,6/7) | 原始 [x1,y1,x2,y2,conf,cls],外加可选的跟踪 ID。 |
result.boxes.xyxy | torch.float32 | (N,4) | xyxy 像素框。 |
result.boxes.conf | torch.float32 | (N,) | 置信度分数。 |
result.boxes.cls | torch.float32 | (N,) | 类别 ID;转换为 int 以获取名称。 |
有关各任务特定的 Results 字段,请参阅按任务预测结果部分。
Link to this section导出#
将 YOLO26n 模型导出为其他格式,如 ONNX、CoreML 等。
from ultralytics import YOLO
# Load a model
model = YOLO("yolo26n.pt") # load an official model
model = YOLO("path/to/best.pt") # load a custom-trained model
# Export the model
model.export(format="onnx")可用的 YOLO26 导出格式如下表所示。你可以使用 format 参数导出为任何格式,例如 format='onnx' 或 format='engine'。你可以直接在导出的模型上进行预测或验证,例如 yolo predict model=yolo26n.onnx。导出完成后,会显示针对你模型的用法示例。
| 格式 | format 参数 | 模型 | 元数据 | 参数 |
|---|---|---|---|---|
| PyTorch | - | yolo26n.pt | ✅ | - |
| TorchScript | torchscript | yolo26n.torchscript | ✅ | imgsz, half, dynamic, optimize, nms, batch, device |
| ONNX | onnx | yolo26n.onnx | ✅ | imgsz, half, int8, dynamic, simplify, opset, nms, batch, data, fraction, device |
| OpenVINO | openvino | yolo26n_openvino_model/ | ✅ | imgsz, half, dynamic, int8, nms, batch, data, fraction, device |
| TensorRT | engine | yolo26n.engine | ✅ | imgsz, half, dynamic, simplify, workspace, int8, nms, batch, data, fraction, device |
| CoreML | coreml | yolo26n.mlpackage | ✅ | imgsz, dynamic, half, int8, nms, batch, device |
| TF SavedModel | saved_model | yolo26n_saved_model/ | ✅ | imgsz, keras, int8, nms, batch, data, fraction, device |
| TF GraphDef | pb | yolo26n.pb | ❌ | imgsz, batch, device |
| TF Lite | tflite | yolo26n.tflite | ✅ | imgsz, half, int8, nms, batch, data, fraction, device |
| TF Edge TPU | edgetpu | yolo26n_edgetpu.tflite | ✅ | imgsz, int8, data, fraction, device |
| TF.js | tfjs | yolo26n_web_model/ | ✅ | imgsz, half, int8, nms, batch, data, fraction, device |
| PaddlePaddle | paddle | yolo26n_paddle_model/ | ✅ | imgsz, batch, device |
| MNN | mnn | yolo26n.mnn | ✅ | imgsz, batch, int8, half, device |
| NCNN | ncnn | yolo26n_ncnn_model/ | ✅ | imgsz, half, batch, device |
| IMX500 | imx | yolo26n_imx_model/ | ✅ | imgsz, int8, data, fraction, nms, device |
| RKNN | rknn | yolo26n_rknn_model/ | ✅ | imgsz, batch, name, int8, data, fraction, device |
| ExecuTorch | executorch | yolo26n_executorch_model/ | ✅ | imgsz, batch, device |
| Axelera | axelera | yolo26n_axelera_model/ | ✅ | imgsz, batch, int8, data, fraction, device |
| DEEPX | deepx | yolo26n_deepx_model/ | ✅ | imgsz, int8, data, optimize, device |
| Qualcomm QNN | qnn | yolo26n_qnn_model/ | ✅ | imgsz, batch, name, int8, data, fraction, device |
See full export details in the Export page.
Link to this section常见问题解答 (FAQ)#
Link to this section我可以在不编写代码的情况下训练和部署检测模型吗?#
可以。Ultralytics Platform 提供了基于浏览器的工作流程,用于标注数据集、在云端 GPU 上训练检测模型并将其部署到推理端点。请参阅 Platform quickstart 开始使用。
Link to this section如何在我自己的数据集上训练 YOLO26 模型?#
在自定义数据集上训练 YOLO26 模型包含以下几个步骤:
- 准备数据集:确保你的数据集采用 YOLO 格式。有关指南,请参考我们的 Dataset Guide。
- 加载模型:使用 Ultralytics YOLO 库加载预训练模型,或从 YAML 文件创建新模型。
- 训练模型:在 Python 中执行
train方法,或在 CLI 中执行yolo detect train命令。
from ultralytics import YOLO
# Load a pretrained model
model = YOLO("yolo26n.pt")
# Train the model on your custom dataset
model.train(data="my_custom_dataset.yaml", epochs=100, imgsz=640)有关详细配置选项,请访问 Configuration 页面。
Link to this sectionYOLO26 中有哪些可用的预训练模型?#
Ultralytics YOLO26 提供了各种预训练模型,适用于 object detection、instance segmentation、semantic segmentation 和 pose estimation。这些模型是在 COCO 数据集、用于语义分割的 Cityscapes 数据集,或用于分类任务的 ImageNet 数据集上预训练的。以下是一些可用模型:
有关详细列表和性能指标,请参阅 Models 部分。
Link to this section我该如何验证已训练的 YOLO 模型的准确性?#
要验证已训练的 YOLO26 模型的准确性,你可以在 Python 中使用 .val() 方法,或在 CLI 中使用 yolo detect val 命令。这将提供 mAP50-95、mAP50 等指标。
from ultralytics import YOLO
# Load the model
model = YOLO("path/to/best.pt")
# Validate the model
metrics = model.val()
print(metrics.box.map) # mAP50-95有关更多验证详细信息,请访问 Val 页面。
Link to this sectionYOLO26 模型可以导出为哪些格式?#
Ultralytics YOLO26 允许将模型导出为 ONNX、TensorRT、CoreML 等多种格式,以确保在不同平台和设备上的兼容性。
from ultralytics import YOLO
# Load the model
model = YOLO("yolo26n.pt")
# Export the model to ONNX format
model.export(format="onnx")在 Export 页面查看支持的格式完整列表和说明。
Link to this section为什么我应该使用 Ultralytics YOLO26 进行目标检测?#
Ultralytics YOLO26 旨在为 object detection、instance segmentation、semantic segmentation 和 pose estimation 提供最先进的性能。以下是一些主要优势:
- 预训练模型:利用在 COCO 和 ImageNet 等热门数据集上预训练的模型,实现更快的开发。
- 高准确率:实现出色的 mAP 分数,确保可靠的目标检测。
- 速度:针对 real-time inference 进行了优化,使其成为需要快速处理的应用的理想选择。
- 灵活性:将模型导出为 ONNX 和 TensorRT 等各种格式,以便在多个平台上部署。
探索我们的 Blog,了解 YOLO26 的实际应用案例和成功故事。