语义分割
语义分割为图像中的每个像素分配类别标签,从而生成覆盖整个场景的密集类图。与用于区分单个对象的实例分割不同,语义分割将相同类别的所有像素归为一类,无论场景中存在多少个不同的对象。
语义分割模型的输出是一张高度乘以宽度的单通道类图,其中每个像素值对应一个预测的类 ID。这使得语义分割非常适用于场景解析任务,例如自动驾驶、医学影像和土地覆盖绘图。
请使用 task=semantic 或 yolo semantic CLI 任务来进行语义分割。YOLO26 语义分割模型文件使用 -sem 后缀,例如 yolo26n-sem.pt。
模型
在 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进行复现。 - 速度指标基于 Amazon EC2 P4d 实例上的 Cityscapes 验证集图像平均计算得出。
通过yolo semantic val data=cityscapes.yaml batch=1 device=0|cpu imgsz=2048进行复现。 - 参数量 (Params) 和 FLOPs 数值适用于
model.fuse()融合后的模型,该操作合并了 Conv 和 BatchNorm 层。预训练检查点保留完整的训练架构,因此可能显示更高的统计数值。
训练
在 Cityscapes8 数据集上,以 1024 的图像大小训练 YOLO26n-sem 模型 100 个 轮次 (epochs)。有关可用参数的完整列表,请参阅配置 (Configuration)页面。
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.
数据集格式
语义分割数据集使用单通道掩码图像(通常为 PNG 格式),其中每个像素值代表一个类 ID。值为 255 的像素被视为“忽略”并从损失计算中排除。数据集的 YAML 文件应指定图像及其对应掩码目录的路径。有关格式详细信息,请参阅语义分割数据集指南。支持的数据集包括 Cityscapes 和 ADE20K。
验证
在语义分割数据集上验证经过训练的 YOLO26n-sem 模型的准确率 (accuracy)。请显式传递 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 accuracy预测
使用训练好的 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 # height x width class map (torch.Tensor)See full predict mode details in the Predict page.
导出
将 YOLO26n-sem 模型导出为其他格式,例如 ONNX、CoreML 等。
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, dynamic, simplify, opset, nms, batch, 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, 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 |
See full export details in the Export page.
常见问题
我该如何在自定义数据集上训练 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)页面以了解更多可用参数。
实例分割和语义分割有什么区别?
实例分割和语义分割都是像素级任务,但它们之间存在一个关键区别:
- **语义分割**为每个像素分配一个类别标签,但不区分同一类别的不同对象。例如,场景中的所有汽车共享相同的类别标签。
- **实例分割**单独识别每个对象,即使它们属于同一类别,也会为每个对象生成不同的掩码。
语义分割最适合自动驾驶和土地覆盖绘图等场景理解任务,而当需要计数或跟踪单个对象时,实例分割则是首选。
我可以使用实例分割数据来训练语义分割吗?
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.
语义分割支持哪些数据集?
Ultralytics YOLO26 为多种语义分割数据集提供了内置配置:
- Cityscapes: 拥有 19 个类别的城市街道场景,广泛用于自动驾驶研究。
- ADE20K: 一个包含 150 个类别的大规模场景解析数据集。
你也可以使用任何提供 PNG 掩码标注且像素值对应类 ID 的自定义数据集。
如何验证预训练的 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) 和像素准确率等验证指标,这些是衡量语义分割性能的标准指标。
如何将 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)页面。