Link to this section语义分割#
语义分割 为图像中的每个像素分配一个类标签,从而生成覆盖整个场景的密集类映射。与区分单个对象的 实例分割 不同,语义分割将同一类的所有像素组合在一起,而不管存在多少个不同的对象。
Watch: How to Train Ultralytics YOLO26 Semantic Segmentation Model on Custom Dataset | Ultralytics Platform
语义分割模型的输出是一个单一的宽高类映射,其中每个像素值对应一个预测的类 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进行复现。 - 参数 (Params) 和 FLOPs 数值是模型经过
model.fuse()(合并卷积层和批量归一化层)之后的融合模型数据。预训练检查点保留了完整的训练架构,因此可能会显示更高的数值。
以下展示了在 ADE20K 数据集上预训练的 YOLO26 语义分割模型。
模型在首次使用时会自动从最新的 Ultralytics 发布版本下载。
| 模型 | 尺寸 (像素) | mIoUval | 速度 RTX3090 PyTorch (ms) | 参数量 (M) | FLOPs (B) |
|---|---|---|---|---|---|
| YOLO26n-sem-ade20k | 640 | 38.8 | 3.9 ± 0.2 | 1.6 | 4.4 |
| YOLO26s-sem-ade20k | 640 | 45.6 | 4.2 ± 0.3 | 6.5 | 17.4 |
| YOLO26m-sem-ade20k | 640 | 47.4 | 4.7 ± 0.3 | 14.3 | 59.5 |
| YOLO26l-sem-ade20k | 640 | 49.7 | 8.3 ± 0.2 | 17.9 | 75.0 |
| YOLO26x-sem-ade20k | 640 | 51.5 | 9.9 ± 0.3 | 40.2 | 168.1 |
- mIoUval 数值基于 ADE20K 验证集上的单模型单尺度测试。
你可以使用yolo semantic val model=yolo26n-sem-ade20k.pt data=ade20k.yaml device=0 imgsz=640进行复现,并将yolo26n-sem-ade20k.pt替换为你想要的yolo26*-sem-ade20k.pt权重文件。 - 速度指标是在 RTX3090 实例上使用 ADE20K 验证集图像平均所得。
你可以使用yolo semantic val model=yolo26n-sem-ade20k.pt data=ade20k.yaml batch=1 device=0|cpu imgsz=640进行复现,并将yolo26n-sem-ade20k.pt替换为你想要的yolo26*-sem-ade20k.pt权重文件。 - 参数 (Params) 和 FLOPs 数值是模型经过
model.fuse()(合并卷积层和批量归一化层)之后的融合模型数据。预训练检查点保留了完整的训练架构,因此可能会显示更高的数值。
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;dtype 由类别数量决定。 |
result.masks | - | - | 无实例掩膜。 |
result.boxes | - | - | 无实例框/置信度。 |
result.masks.xy | - | - | 无默认多边形。 |
关于各项任务中特定于任务的 Results 字段,请参阅按任务划分的预测结果 (Predict Results by Task) 部分。
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) |
| 像素值 | 二进制掩码值:0 或 1 | 类 ID:0,1,2,... |
| 数据类型 (Dtype) | torch.uint8 | torch.uint8torch.int16torch.int32 |
| 同类对象 | 作为单独的实例保留 | 合并为同一类区域 |
| 多边形 | 是,通过 result.masks.xy 和 result.masks.xyn | 默认无多边形输出 |
| 边界框和置信度 | 是,通过 result.boxes | 无每个实例的边界框或置信度分数 |
| 典型用途 | 计数、跟踪、裁剪、对象级测量 | 密集场景标注、可行驶区域、土地覆盖、医疗区域 |
Link to this section导出#
将 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, quantize, dynamic, optimize, nms, batch, device |
| ONNX | onnx | yolo26n-sem.onnx | ✅ | imgsz, quantize, dynamic, simplify, opset, nms, batch, data, fraction, device |
| OpenVINO | openvino | yolo26n-sem_openvino_model/ | ✅ | imgsz, quantize, dynamic, nms, batch, data, fraction, device |
| TensorRT | engine | yolo26n-sem.engine | ✅ | imgsz, quantize, dynamic, simplify, workspace, nms, batch, data, fraction, device |
| CoreML | coreml | yolo26n-sem.mlpackage | ✅ | imgsz, dynamic, quantize, nms, batch, device |
| TF SavedModel | saved_model | yolo26n-sem_saved_model/ | ✅ | imgsz, keras, quantize, nms, batch, data, fraction, device |
| TF GraphDef | pb | yolo26n-sem.pb | ❌ | imgsz, batch, device |
| TF Lite | tflite | yolo26n-sem.tflite | ✅ | imgsz, quantize, nms, batch, data, fraction, device |
| TF Edge TPU | edgetpu | yolo26n-sem_edgetpu.tflite | ✅ | imgsz, quantize, data, fraction, device |
| TF.js | tfjs | yolo26n-sem_web_model/ | ✅ | imgsz, quantize, nms, batch, data, fraction, device |
| PaddlePaddle | paddle | yolo26n-sem_paddle_model/ | ✅ | imgsz, batch, device |
| MNN | mnn | yolo26n-sem.mnn | ✅ | imgsz, batch, quantize, device |
| NCNN | ncnn | yolo26n-sem_ncnn_model/ | ✅ | imgsz, quantize, batch, device |
| IMX500 | imx | yolo26n-sem_imx_model/ | ✅ | imgsz, quantize, data, fraction, nms, device |
| RKNN | rknn | yolo26n-sem_rknn_model/ | ✅ | imgsz, batch, name, quantize, data, fraction, device |
| ExecuTorch | executorch | yolo26n-sem_executorch_model/ | ✅ | imgsz, batch, device |
| Axelera | axelera | yolo26n-sem_axelera_model/ | ✅ | imgsz, batch, quantize, data, fraction, device |
| DEEPX | deepx | yolo26n-sem_deepx_model/ | ✅ | imgsz, quantize, data, optimize, device |
| Qualcomm QNN | qnn | yolo26n-sem_qnn.onnx | ✅ | imgsz, batch, name, quantize, data, fraction, device |
查看关于 export 的完整详细信息,请访问 Export 页面。
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)查看配置页面以了解更多可用参数。
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")有关导出为各种格式的更多详细信息,请参阅导出页面。