Link to this section语义分割#
语义分割为图像中的每个像素分配一个类标签,生成覆盖整个场景的密集类图。与将单个对象分离开来的实例分割不同,语义分割将同一类的所有像素归为一组,无论是否存在多个独立对象。
语义分割模型的输出是一个高度乘以宽度的单通道类图,其中每个像素值对应一个预测出的类别 ID。这使得语义分割非常适合场景解析任务,例如自动驾驶、医学成像和土地覆盖测绘。
使用 task=semantic 或 yolo semantic CLI 任务进行语义分割。YOLO26 语义分割模型文件使用 -sem 后缀,例如 yolo26n-sem.pt。
Link to this section模型#
在 Cityscapes 数据集上预训练的 YOLO26 语义分割模型如下所示。
模型在首次使用时会自动从最新的 Ultralytics 发布版本中下载。
| 模型 | 尺寸 (像素) | mIoUval | 速度 RTX3090 PyTorch (ms) | 参数 (M) | FLOPs (B) |
|---|---|---|---|---|---|
| YOLO26n-sem | 1024 × 2048 | 78.3 | 4.4 ± 0.0 | 1.6 | 22.7 |
| YOLO26s-sem | 1024 × 2048 | 80.8 | 8.4 ± 0.0 | 6.5 | 88.8 |
| YOLO26m-sem | 1024 × 2048 | 82.0 | 19.9 ± 0.1 | 14.3 | 304.5 |
| YOLO26l-sem | 1024 × 2048 | 82.9 | 26.5 ± 0.1 | 17.9 | 384.7 |
| YOLO26x-sem | 1024 × 2048 | 83.6 | 48.9 ± 0.2 | 40.2 | 861.7 |
- mIoUval 值是在 Cityscapes 验证集上进行的单模型单尺度测试。
使用yolo semantic val data=cityscapes.yaml device=0 imgsz=2048进行复现。 - 速度指标是在 RTX3090 实例上对 Cityscapes 验证图像进行平均的结果。
使用yolo semantic val data=cityscapes.yaml batch=1 device=0|cpu imgsz=2048进行复现。 - 参数和 FLOPs 值针对
model.fuse()融合后的模型,该操作合并了 Conv 和 BatchNorm 层。预训练检查点保留了完整的训练架构,因此计数值可能会更高。
Link to this section训练#
在 Cityscapes8 数据集上以 1024 图像尺寸训练 YOLO26n-sem 100 个 epoch。有关可用参数的完整列表,请查看配置页面。
from ultralytics import YOLO
# Load a model
model = YOLO("yolo26n-sem.yaml") # build a new model from YAML
model = YOLO("yolo26n-sem.pt") # load a pretrained model (recommended for training)
model = YOLO("yolo26n-sem.yaml").load("yolo26n-sem.pt") # build from YAML and transfer weights
# Train the model
results = model.train(data="cityscapes8.yaml", epochs=100, imgsz=1024)See full train mode details in the Train page.
Link to this section数据集格式#
语义分割数据集使用单通道掩码图像(通常为 PNG),其中每个像素值代表一个类 ID。值为 255 的像素被视为“忽略”并从损失计算中排除。数据集 YAML 文件应指定图像及其对应掩码目录的路径。有关格式详细信息,请参阅语义分割数据集指南。支持的数据集包括 Cityscapes 和 ADE20K。
Link to this section验证#
在语义分割数据集上验证已训练的 YOLO26n-sem 模型准确率。请显式传递 data,以便验证过程使用预期的数据集 YAML。
from ultralytics import YOLO
# Load a model
model = YOLO("yolo26n-sem.pt") # load an official model
model = YOLO("path/to/best.pt") # load a custom model
# Validate the model
metrics = model.val(data="cityscapes.yaml")
metrics.miou # mean Intersection over Union
metrics.pixel_accuracy # overall pixel accuracyLink to this section预测#
使用已训练的 YOLO26n-sem 模型对图像进行预测。
from ultralytics import YOLO
# Load a model
model = YOLO("yolo26n-sem.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:
semantic_mask = result.semantic_mask.data # class map, shape (H,W), integer dtype selected by class countSee full predict mode details in the Predict page.
Link to this section结果输出#
YOLO 语义分割为每张图像返回一个 Results 对象。每个结果存储一张覆盖整幅图像的密集类图,而不是一组对象掩码。具有相同预测类别的像素共享相同的类 ID,即使它们属于不同的对象。
| 属性 | 类型 | 形状 | 描述 |
|---|---|---|---|
result.semantic_mask | SemanticMask | (H,W) | 密集类图。 |
result.semantic_mask.data | torch.uint8torch.int16torch.int32 | (H,W) | 类 ID;数据类型由类别数量决定。 |
result.masks | - | - | 无实例掩码。 |
result.boxes | - | - | 无实例边界框/置信度。 |
result.masks.xy | - | - | 无默认多边形。 |
有关各项任务中特定的 Results 字段,请参阅按任务划分的预测结果部分。
Link to this section实例分割与语义分割对比#
| 方面 | 实例分割 (task="segment") | 语义分割 (task="semantic") |
|---|---|---|
| 预测目标 | 分别分割每个检测到的对象 | 为每个像素分配一个类 ID |
| 输出字段 | result.masks | result.semantic_mask |
| 主要数据 | result.masks.data | result.semantic_mask.data |
| 形状 | (N,H,W) | (H,W) |
| 形状 | Binary mask values: 0 or 1 | Class IDs: 0, 1, 2, ... |
| 像素值 | torch.uint8 | torch.uint8torch.int16torch.int32 |
| Same-class objects | 数据类型 | Merged into the same class region |
| 同类对象 | Yes, through result.masks.xy and result.masks.xyn | 合并到同一类区域中 |
| 多边形 | Yes, through result.boxes | 默认无多边形输出 |
| 边界框和置信度 | Counting, tracking, cropping, object-level measurement | 无逐个实例的边界框或置信度分数 |
Link to this section导出#
典型用途
from ultralytics import YOLO
# Load a model
model = YOLO("yolo26n-sem.pt") # load an official model
model = YOLO("path/to/best.pt") # load a custom model
# Export the model
model.export(format="onnx")下表列出了可用的 YOLO26 语义分割导出格式。你可以使用 format 参数导出到任何格式,例如 format='onnx' 或 format='engine'。你可以直接在导出的模型上进行预测或验证,例如 yolo predict model=yolo26n-sem.onnx。导出完成后,将显示针对你模型的用法示例。
| 格式 | format 参数 | 模型 | 元数据 | 参数 |
|---|---|---|---|---|
| PyTorch | - | yolo26n-sem.pt | ✅ | - |
| TorchScript | torchscript | yolo26n-sem.torchscript | ✅ | imgsz, half, dynamic, optimize, nms, batch, device |
| ONNX | onnx | yolo26n-sem.onnx | ✅ | imgsz, half, int8, dynamic, simplify, opset, nms, batch, data, fraction, device |
| OpenVINO | openvino | yolo26n-sem_openvino_model/ | ✅ | imgsz, half, dynamic, int8, nms, batch, data, fraction, device |
| TensorRT | engine | yolo26n-sem.engine | ✅ | imgsz, half, dynamic, simplify, workspace, int8, nms, batch, data, fraction, device |
| CoreML | coreml | yolo26n-sem.mlpackage | ✅ | imgsz, dynamic, half, int8, nms, batch, device |
| TF SavedModel | saved_model | yolo26n-sem_saved_model/ | ✅ | imgsz, keras, int8, nms, batch, data, fraction, device |
| TF GraphDef | pb | yolo26n-sem.pb | ❌ | imgsz, batch, device |
| TF Lite | tflite | yolo26n-sem.tflite | ✅ | imgsz, half, int8, nms, batch, data, fraction, device |
| TF Edge TPU | edgetpu | yolo26n-sem_edgetpu.tflite | ✅ | imgsz, int8, data, fraction, device |
| TF.js | tfjs | yolo26n-sem_web_model/ | ✅ | imgsz, half, int8, nms, batch, data, fraction, device |
| PaddlePaddle | paddle | yolo26n-sem_paddle_model/ | ✅ | imgsz, batch, device |
| MNN | mnn | yolo26n-sem.mnn | ✅ | imgsz, batch, int8, half, device |
| NCNN | ncnn | yolo26n-sem_ncnn_model/ | ✅ | imgsz, half, batch, device |
| IMX500 | imx | yolo26n-sem_imx_model/ | ✅ | imgsz, int8, data, fraction, nms, device |
| RKNN | rknn | yolo26n-sem_rknn_model/ | ✅ | imgsz, batch, name, int8, data, fraction, device |
| ExecuTorch | executorch | yolo26n-sem_executorch_model/ | ✅ | imgsz, batch, device |
| Axelera | axelera | yolo26n-sem_axelera_model/ | ✅ | imgsz, batch, int8, data, fraction, device |
| DEEPX | deepx | yolo26n-sem_deepx_model/ | ✅ | imgsz, int8, data, optimize, device |
| Qualcomm QNN | qnn | yolo26n-sem_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 语义分割模型,你需要准备 PNG 格式的掩码图像,其中每个像素值代表一个类 ID(0, 1, 2, ...),值为 255 的像素在训练过程中会被忽略。创建一个指向图像和掩码目录的数据集 YAML 文件,然后训练模型:
from ultralytics import YOLO
# Load a pretrained YOLO26 semantic segmentation model
model = YOLO("yolo26n-sem.pt")
# Train the model
results = model.train(data="path/to/your_dataset.yaml", epochs=100, imgsz=512)查看 Configuration 页面以获取更多可用参数。
Link to this section实例分割和语义分割有什么区别?#
实例分割和语义分割都是像素级任务,但有一个关键区别:
语义分割最适合自动驾驶和土地覆盖映射等场景理解任务,而当需要计数或跟踪个体对象时,通常首选实例分割。
Link to this section我可以使用实例分割数据来训练语义分割吗?#
Yes. If your dataset uses Ultralytics YOLO polygon labels (one .txt per image), omit masks_dir from the dataset YAML and the loader will convert polygons to per-image semantic masks on the fly. For multi-class datasets (N > 1) an extra background class is appended to names automatically. For single-class datasets (N == 1) training stays at 1 class — your declared class becomes 1 in the mask and uncovered pixels become 0. See the Semantic Segmentation Dataset Guide for details.
Link to this section语义分割支持哪些数据集?#
Ultralytics YOLO26 为多个语义分割数据集提供了内置配置:
- Cityscapes: 具有 19 个类别的城市街道场景,广泛用于自动驾驶研究。
- ADE20K: 一个拥有 150 个类别的大规模场景解析数据集。
你也可以使用任何提供 PNG 掩码标注的自定义数据集,其中像素值对应类 ID。
Link to this section如何验证预训练的 YOLO26 语义分割模型?#
使用用于评估的数据集 YAML 验证预训练的 YOLO26 语义分割模型:
from ultralytics import YOLO
# Load a pretrained model
model = YOLO("yolo26n-sem.pt")
# Validate the model
metrics = model.val(data="cityscapes.yaml")
print("Mean IoU:", metrics.miou)
print("Pixel Accuracy:", metrics.pixel_accuracy)这些步骤将为你提供诸如平均交并比 (mIoU) 和像素准确率等验证指标,这些是评估语义分割性能的标准度量。
Link to this section如何将 YOLO26 语义分割模型导出为 ONNX 格式?#
使用 Python 或 CLI 命令将 YOLO26 语义分割模型导出为 ONNX 格式:
from ultralytics import YOLO
# Load a pretrained model
model = YOLO("yolo26n-sem.pt")
# Export the model to ONNX format
model.export(format="onnx")有关导出到各种格式的更多详细信息,请参考 Export 页面。