Link to this section实例分割#
实例分割比目标检测更进一步,它涉及识别图像中的各个对象,并将它们从图像的其余部分中分割出来。
实例分割模型的输出是一组勾勒出图像中每个对象轮廓的掩码或轮廓,以及每个对象的类别标签和置信度分数。当你不仅需要知道对象在图像中的位置,还需要知道它们的精确形状时,实例分割非常有用。
Watch: Run Segmentation with Pretrained Ultralytics YOLO Model in Python.
YOLO26 Segment 模型使用 -seg 后缀,例如 yolo26n-seg.pt,并预训练于 COCO。
Link to this section模型#
此处展示了 YOLO26 预训练 Segment 模型。检测、分割和姿态模型在 COCO 数据集上预训练,语义模型在 Cityscapes 上预训练,分类模型在 ImageNet 数据集上预训练。
模型在首次使用时会自动从最新的 Ultralytics 发布中下载。
| 模型 | 尺寸 (像素) | mAPbox 50-95(e2e) | mAPmask 50-95(e2e) | 速度 CPU ONNX (ms) | 速度 T4 TensorRT10 (ms) | 参数 (M) | FLOPs (B) |
|---|---|---|---|---|---|---|---|
| YOLO26n-seg | 640 | 39.6 | 33.9 | 53.3 ± 0.5 | 2.1 ± 0.0 | 2.7 | 9.1 |
| YOLO26s-seg | 640 | 47.3 | 40.0 | 118.4 ± 0.9 | 3.3 ± 0.0 | 10.4 | 34.2 |
| YOLO26m-seg | 640 | 52.5 | 44.1 | 328.2 ± 2.4 | 6.7 ± 0.1 | 23.6 | 121.5 |
| YOLO26l-seg | 640 | 54.4 | 45.5 | 387.0 ± 3.7 | 8.0 ± 0.1 | 28.0 | 139.8 |
| YOLO26x-seg | 640 | 56.5 | 47.0 | 787.0 ± 6.8 | 16.4 ± 0.1 | 62.8 | 313.5 |
- mAPval 值是单模型单尺度在 COCO val2017 数据集上的结果。
通过yolo val segment data=coco.yaml device=0进行复现 - 速度 是使用 Amazon EC2 P4d 实例在 COCO val 图像上平均得出的。
通过yolo val segment data=coco.yaml batch=1 device=0|cpu进行复现 - 参数 和 FLOPs 值是
model.fuse()之后融合模型的数值,它合并了 Conv 和 BatchNorm 层,并且对于端到端模型,移除了辅助的一对多检测头。预训练检查点保留了完整的训练架构,可能会显示更高的计数。
Link to this section训练#
在 COCO8-seg 数据集上以 640 的图像尺寸训练 YOLO26n-seg 100 个 epoch。有关可用参数的完整列表,请参阅 配置 页面。
from ultralytics import YOLO
# Load a model
model = YOLO("yolo26n-seg.yaml") # build a new model from YAML
model = YOLO("yolo26n-seg.pt") # load a pretrained model (recommended for training)
model = YOLO("yolo26n-seg.yaml").load("yolo26n-seg.pt") # build from YAML and transfer weights
# Train the model
results = model.train(data="coco8-seg.yaml", epochs=100, imgsz=640)See full train mode details in the Train page. Segmentation models can also be trained on cloud GPUs through Ultralytics Platform.
Link to this section数据集格式#
YOLO 分割数据集格式的详细信息可以在 数据集指南 中找到。要将现有数据集从其他格式(如 COCO 等)转换为 YOLO 格式,请使用 Ultralytics 提供的 JSON2YOLO 工具。你也可以在 Ultralytics Platform 上使用多边形工具和 SAM 驱动的智能标注来创建分割掩码。
Link to this section验证#
Validate trained YOLO26n-seg model accuracy on the COCO8-seg 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-seg.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(B)
metrics.box.map50 # map50(B)
metrics.box.map75 # map75(B)
metrics.box.maps # a list containing mAP50-95(B) for each category
metrics.box.image_metrics # per-image metrics dictionary for det with precision, recall, F1, TP, FP, and FN
metrics.seg.map # map50-95(M)
metrics.seg.map50 # map50(M)
metrics.seg.map75 # map75(M)
metrics.seg.maps # a list containing mAP50-95(M) for each category
metrics.seg.image_metrics # per-image metrics dictionary for seg with precision, recall, F1, TP, FP, and FNLink to this section预测#
使用已训练的 YOLO26n-seg 模型对图像运行预测。
from ultralytics import YOLO
# Load a model
model = YOLO("yolo26n-seg.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:
xy = result.masks.xy # mask polygons in pixel coordinates
xyn = result.masks.xyn # normalized mask polygons
masks = result.masks.data # binary masks, shape (N,H,W), dtype torch.uint8See full predict mode details in the Predict page.
Link to this section结果输出#
YOLO 实例分割每张图像返回一个 Results 对象。每个结果存储对象级别的预测,其中每个检测到的实例都有其自己的二进制掩码、类别、置信度和框。
| 属性 | 类型 | 形状 | 描述 |
|---|---|---|---|
result.masks | Masks | (N) | 实例掩码。 |
result.masks.data | torch.uint8 | (N,H,W) | 二进制掩码,值为 0 或 1。 |
result.masks.xy | np.float32 | list[(P,2)] | 像素多边形。 |
result.masks.xyn | np.float32 | list[(P,2)] | 归一化多边形。 |
result.boxes | Boxes | (N) | 实例框/类别/置信度。 |
result.boxes.cls | torch.float32 | (N,) | 类别 ID;转换为 int 以获取名称。 |
有关各项任务特定的 Results 字段,请参阅 按任务划分的预测结果 部分。
Link to this section这与语义分割有何不同#
实例分割是对象级分割:两辆车会产生两个掩码、两个框和两个置信度分数。语义分割是像素级分类:相同的两辆车在图像大小的类别图中变成具有相同类别 ID 的像素,没有每个对象的框、置信度或默认多边形列表。
Link to this section导出#
将 YOLO26n-seg 模型导出为 ONNX、CoreML 等不同格式。
from ultralytics import YOLO
# Load a model
model = YOLO("yolo26n-seg.pt") # load an official model
model = YOLO("path/to/best.pt") # load a custom-trained model
# Export the model
model.export(format="onnx")可用的 YOLO26-seg 导出格式如下表所示。你可以使用 format 参数导出为任何格式,例如 format='onnx' 或 format='engine'。你可以直接对导出的模型进行预测或验证,例如 yolo predict model=yolo26n-seg.onnx。导出完成后,会显示模型的用法示例。
| 格式 | format 参数 | 模型 | 元数据 | 参数 |
|---|---|---|---|---|
| PyTorch | - | yolo26n-seg.pt | ✅ | - |
| TorchScript | torchscript | yolo26n-seg.torchscript | ✅ | imgsz, half, dynamic, optimize, nms, batch, device |
| ONNX | onnx | yolo26n-seg.onnx | ✅ | imgsz, half, int8, dynamic, simplify, opset, nms, batch, data, fraction, device |
| OpenVINO | openvino | yolo26n-seg_openvino_model/ | ✅ | imgsz, half, dynamic, int8, nms, batch, data, fraction, device |
| TensorRT | engine | yolo26n-seg.engine | ✅ | imgsz, half, dynamic, simplify, workspace, int8, nms, batch, data, fraction, device |
| CoreML | coreml | yolo26n-seg.mlpackage | ✅ | imgsz, dynamic, half, int8, nms, batch, device |
| TF SavedModel | saved_model | yolo26n-seg_saved_model/ | ✅ | imgsz, keras, int8, nms, batch, data, fraction, device |
| TF GraphDef | pb | yolo26n-seg.pb | ❌ | imgsz, batch, device |
| TF Lite | tflite | yolo26n-seg.tflite | ✅ | imgsz, half, int8, nms, batch, data, fraction, device |
| TF Edge TPU | edgetpu | yolo26n-seg_edgetpu.tflite | ✅ | imgsz, int8, data, fraction, device |
| TF.js | tfjs | yolo26n-seg_web_model/ | ✅ | imgsz, half, int8, nms, batch, data, fraction, device |
| PaddlePaddle | paddle | yolo26n-seg_paddle_model/ | ✅ | imgsz, batch, device |
| MNN | mnn | yolo26n-seg.mnn | ✅ | imgsz, batch, int8, half, device |
| NCNN | ncnn | yolo26n-seg_ncnn_model/ | ✅ | imgsz, half, batch, device |
| IMX500 | imx | yolo26n-seg_imx_model/ | ✅ | imgsz, int8, data, fraction, nms, device |
| RKNN | rknn | yolo26n-seg_rknn_model/ | ✅ | imgsz, batch, name, int8, data, fraction, device |
| ExecuTorch | executorch | yolo26n-seg_executorch_model/ | ✅ | imgsz, batch, device |
| Axelera | axelera | yolo26n-seg_axelera_model/ | ✅ | imgsz, batch, int8, data, fraction, device |
| DEEPX | deepx | yolo26n-seg_deepx_model/ | ✅ | imgsz, int8, data, optimize, device |
| Qualcomm QNN | qnn | yolo26n-seg_qnn_model/ | ✅ | imgsz, batch, name, int8, data, fraction, device |
See full export details in the Export page.
Link to this section常见问题#
Link to this section如何训练自定义数据集上的 YOLO26 分割模型?#
要训练自定义数据集上的 YOLO26 分割模型,你首先需要按照 YOLO 分割格式准备数据集。你可以使用 JSON2YOLO 等工具将其他格式的数据集进行转换。数据集准备就绪后,你可以使用 Python 或 CLI 命令来训练模型:
from ultralytics import YOLO
# Load a pretrained YOLO26 segment model
model = YOLO("yolo26n-seg.pt")
# Train the model
results = model.train(data="path/to/your_dataset.yaml", epochs=100, imgsz=640)查看配置页面了解更多可用参数。
Link to this sectionYOLO26 中的目标检测与实例分割有什么区别?#
目标检测通过在图像中的对象周围绘制边界框来识别并定位对象,而实例分割不仅能识别边界框,还能描绘出每个对象的精确形状。YOLO26 实例分割模型提供勾勒出每个检测对象轮廓的掩码或轮廓,这对于医学影像或自动驾驶等需要了解对象精确形状的任务特别有用。
Link to this section为什么要使用 YOLO26 进行实例分割?#
Ultralytics YOLO26 是一款以高精度和实时性能著称的尖端模型,非常适合实例分割任务。YOLO26 Segment 模型在 COCO 数据集上经过预训练,确保了在各种对象上的稳健性能。此外,YOLO 支持训练、验证、预测和导出功能,集成流畅,使其在研究和工业应用中都具有极高的通用性。
Link to this section如何加载并验证预训练的 YOLO 分割模型?#
加载并验证预训练的 YOLO 分割模型非常简单。以下是你通过 Python 和 CLI 实现的方法:
from ultralytics import YOLO
# Load a pretrained model
model = YOLO("yolo26n-seg.pt")
# Validate the model
metrics = model.val()
print("Mean Average Precision for boxes:", metrics.box.map)
print("Mean Average Precision for masks:", metrics.seg.map)这些步骤将为你提供诸如平均精度均值 (mAP) 等验证指标,这对评估模型性能至关重要。
Link to this section如何将 YOLO 分割模型导出为 ONNX 格式?#
将 YOLO 分割模型导出为 ONNX 格式很简单,可以使用 Python 或 CLI 命令完成:
from ultralytics import YOLO
# Load a pretrained model
model = YOLO("yolo26n-seg.pt")
# Export the model to ONNX format
model.export(format="onnx")有关导出为各种格式的更多详情,请参阅导出页面。