企业级安全保障: 符合 ISO 27001 + SOC 2 Type I 标准。

Link to this section语义分割#

Semantic segmentation examples

语义分割 为图像中的每个像素分配一个类标签,从而生成覆盖整个场景的密集类映射。与区分单个对象的 实例分割 不同,语义分割将同一类的所有像素组合在一起,而不管存在多少个不同的对象。



Watch: Semantic Segmentation with Ultralytics YOLO26 | Quickstart Tutorial

语义分割模型的输出是一个单一的宽高类映射,其中每个像素值对应一个预测的类 ID。这使得语义分割非常适合场景解析任务,如自动驾驶、医学成像和土地覆盖映射。

提示

使用 task=semanticyolo semantic CLI 任务进行语义分割。YOLO26 语义分割模型文件使用 -sem 后缀,例如 yolo26n-sem.pt

Link to this section模型#

Cityscapes 数据集上预训练的 YOLO26 语义分割模型如下所示。

模型在首次使用时会自动从最新的 Ultralytics 发布版本下载。

模型尺寸
(像素)
mIoUval速度
RTX3090 PyTorch
(ms)
参数量
(M)
FLOPs
(B)
YOLO26n-sem1024 × 204878.34.4 ± 0.01.622.7
YOLO26s-sem1024 × 204880.88.4 ± 0.06.588.8
YOLO26m-sem1024 × 204882.019.9 ± 0.114.3304.5
YOLO26l-sem1024 × 204882.926.5 ± 0.117.9384.7
YOLO26x-sem1024 × 204883.648.9 ± 0.240.2861.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-ade20k64038.83.9 ± 0.21.64.4
YOLO26s-sem-ade20k64045.64.2 ± 0.36.517.4
YOLO26m-sem-ade20k64047.44.7 ± 0.314.359.5
YOLO26l-sem-ade20k64049.78.3 ± 0.217.975.0
YOLO26x-sem-ade20k64051.59.9 ± 0.340.2168.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. Semantic segmentation models can also be trained with Ultralytics Platform cloud training.

Link to this section数据集格式#

语义分割数据集使用单通道掩码图像(通常为 PNG),其中每个像素值代表一个类 ID。值为 255 的像素被视为“忽略”并排除在损失计算之外。数据集 YAML 应指定图像路径及其对应的掩码目录。有关格式的详细信息,请参阅 Semantic Segmentation Dataset Guide。支持的数据集包括 CityscapesADE20K。你可以使用 Ultralytics Platform annotation 管理和标注语义数据集。

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 accuracy

Link 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 count

See full predict mode details in the Predict page.

Link to this section结果输出#

YOLO 语义分割为每张图像返回一个 Results 对象。每个结果存储整个图像的一个密集类映射,而不是对象掩码列表。即使属于不同的对象,具有相同预测类的像素也共享相同的类 ID。

属性类型形状描述
result.semantic_maskSemanticMask(H,W)密集类地图。
result.semantic_mask.datatorch.uint8
torch.int16
torch.int32
(H,W)类 ID;dtype 由类别数量决定。
result.masks--无实例掩膜。
result.boxes--无实例框/置信度。
result.masks.xy--无默认多边形。

关于各项任务中特定于任务的 Results 字段,请参阅按任务划分的预测结果 (Predict Results by Task) 部分。

掩码边界质量

Semantic segmentation predicts a dense class map, then resizes that map back to the image shape for visualization and downstream use. Very thin structures, such as lane markings, court lines, poles, or wires, can therefore look stair-stepped when inference runs at a much lower imgsz than the original image resolution. If boundaries appear jagged, first retest the native PyTorch .pt model with a larger imgsz, such as 1024, 1280, or the closest practical value to the source image size. Use exported models only after confirming the .pt output is acceptable, since lower-resolution inputs cannot recover fine detail that was not present in the predicted class map.

Link to this section实例分割与语义分割#

方面实例分割 (task="segment")语义分割 (task="semantic")
预测目标单独分割每个检测到的对象为每个像素分配一个类 ID
输出字段result.masksresult.semantic_mask
主要数据result.masks.dataresult.semantic_mask.data
形状(N,H,W)(H,W)
像素值二进制掩码值:01类 ID:012,...
数据类型 (Dtype)torch.uint8torch.uint8
torch.int16
torch.int32
同类对象作为单独的实例保留合并为同一类区域
多边形是,通过 result.masks.xyresult.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-
TorchScripttorchscriptyolo26n-sem.torchscriptimgsz, quantize, dynamic, nms, batch, device
ONNXonnxyolo26n-sem.onnximgsz, quantize, dynamic, simplify, opset, nms, batch, data, fraction, device
OpenVINOopenvinoyolo26n-sem_openvino_model/imgsz, quantize, dynamic, nms, batch, data, fraction, device
TensorRTengineyolo26n-sem.engineimgsz, quantize, dynamic, simplify, workspace, nms, batch, data, fraction, device
CoreMLcoremlyolo26n-sem.mlpackageimgsz, dynamic, quantize, nms, batch, device
TF SavedModelsaved_modelyolo26n-sem_saved_model/imgsz, keras, quantize, nms, batch, data, fraction, device
TF GraphDefpbyolo26n-sem.pbimgsz, batch, device
TF Edge TPUedgetpuyolo26n-sem_edgetpu.tfliteimgsz, quantize, data, fraction, device
PaddlePaddlepaddleyolo26n-sem_paddle_model/imgsz, batch, device
MNNmnnyolo26n-sem.mnnimgsz, batch, dynamic, quantize, nms, device
NCNNncnnyolo26n-sem_ncnn_model/imgsz, quantize, batch, device
IMX500imxyolo26n-sem_imx_model/imgsz, quantize, data, fraction, nms, device
RKNNrknnyolo26n-sem_rknn_model/imgsz, batch, name, quantize, data, fraction, device
ExecuTorchexecutorchyolo26n-sem_executorch_model/imgsz, batch, device
Axeleraaxelerayolo26n-sem_axelera_model/imgsz, batch, quantize, data, fraction, device
DEEPXdeepxyolo26n-sem_deepx_model/imgsz, quantize, data, optimize, device
Qualcomm QNNqnnyolo26n-sem_qnn.onnximgsz, batch, name, quantize, data, fraction, device
LiteRTlitertyolo26n-sem.tfliteimgsz, quantize, batch, data, fraction, device
Hailohailoyolo26n-sem_hailo_model/imgsz, name, quantize, data, fraction, opset, simplify, conf, iou

查看关于 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 make sure no masks/ folder exists next to your images at the dataset root (its presence alone triggers PNG-mask mode even without masks_dir set). The loader then converts 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")

有关导出为各种格式的更多详细信息,请参阅导出页面。

评论