Link to this sectionCityscapes 数据集#
Cityscapes 数据集是一个大型的 语义分割 基准测试,专注于在 50 个欧洲城市中拍摄的城市街景。它提供了高质量的像素级标注,是用于自动驾驶研究和城市场景理解的最广泛使用的数据集之一,非常适合与 Ultralytics YOLO 模型配合使用。
Link to this section主要特性#
- Cityscapes 精细标注包括 2,975 张训练图像、500 张验证图像和 1,525 张测试图像。
- 该数据集涵盖 19 个评估类别,横跨道路、车辆、人类、建筑、物体、自然和天空等类别。
- Cityscapes 提供标准化的评估指标,如用于语义分割的平均交并比 (mIoU),从而实现对模型性能的有效比较。
Link to this section数据集结构#
Ultralytics 配置要求在准备后采用以下布局:
cityscapes/
├── images/
│ ├── train/
│ ├── val/
│ └── test/
└── masks/
├── train/
├── val/
└── test/语义掩码是单通道 PNG 文件。原始的 Cityscapes 标签 ID 通过 label_mapping 部分映射到标准的 19 个训练 ID,而忽略或无效标签被映射为 255,以便在训练和评估中将其排除。从 Cityscapes 网站下载官方的 leftImg8bit 和 gtFine 存档并将其解压到数据集根目录;然后 cityscapes.yaml 中的准备模块会将图像和掩码组织成这种布局。
Link to this section应用#
Cityscapes 被广泛用于训练和评估语义分割中的深度学习模型,特别是在自动驾驶、高级驾驶辅助系统 (ADAS) 和城市机器人领域。
其高分辨率图像和详细的标注也使其对于实时场景解析、车道和障碍物理解以及任何需要对复杂城市环境进行密集像素级理解的任务的研究非常有价值。
Link to this section数据集 YAML#
数据集 YAML 文件定义了 Cityscapes 的路径、类、掩码目录和标签映射。cityscapes.yaml 文件维护在 https://github.com/ultralytics/ultralytics/blob/main/ultralytics/cfg/datasets/cityscapes.yaml。
# Ultralytics 🚀 AGPL-3.0 License - https://ultralytics.com/license
# Cityscapes semantic segmentation dataset (19 classes)
# Documentation: https://docs.ultralytics.com/datasets/semantic/cityscapes
# Example usage: yolo semantic train data=cityscapes.yaml model=yolo26n-sem.pt
# parent
# ├── ultralytics
# └── datasets
# └── cityscapes ← downloads here (11 GB)
# └── images
# └── masks
# Dataset root directory
path: cityscapes # dataset root dir
train: images/train # train images (relative to 'path') 2975 images
val: images/val # val images (relative to 'path') 500 images
test: images/test # test images (relative to 'path') 1525 images
masks_dir: masks # semantic mask directory
# Cityscapes 19-class labels
names:
0: road
1: sidewalk
2: building
3: wall
4: fence
5: pole
6: traffic light
7: traffic sign
8: vegetation
9: terrain
10: sky
11: person
12: rider
13: car
14: truck
15: bus
16: train
17: motorcycle
18: bicycle
# Map source label IDs to train IDs; ignore_label is converted to 255.
label_mapping:
-1: ignore_label
0: ignore_label
1: ignore_label
2: ignore_label
3: ignore_label
4: ignore_label
5: ignore_label
6: ignore_label
7: 0
8: 1
9: ignore_label
10: ignore_label
11: 2
12: 3
13: 4
14: ignore_label
15: ignore_label
16: ignore_label
17: 5
18: ignore_label
19: 6
20: 7
21: 8
22: 9
23: 10
24: 11
25: 12
26: 13
27: 14
28: 15
29: ignore_label
30: ignore_label
31: 16
32: 17
33: 18
# Preparation script (requires manual Cityscapes download)
download: |
from pathlib import Path
from shutil import copy2
cityscapes_dir = Path(yaml["path"]) # dataset root dir
# Download and extract the official Cityscapes leftImg8bit and gtFine archives into cityscapes_dir first.
leftimg8bit_dir = cityscapes_dir / "leftImg8bit"
gtfine_dir = cityscapes_dir / "gtFine"
for split in ("train", "val", "test"):
print(f"Processing {split} set")
src_image_dir = leftimg8bit_dir / split
dst_image_dir = cityscapes_dir / "images" / split
dst_mask_dir = cityscapes_dir / "masks" / split
dst_image_dir.mkdir(parents=True, exist_ok=True)
dst_mask_dir.mkdir(parents=True, exist_ok=True)
image_paths = sorted(src_image_dir.rglob("*_leftImg8bit.png"))
for image_path in image_paths:
relative_path = image_path.relative_to(src_image_dir)
mask_path = gtfine_dir / split / relative_path.parent / image_path.name.replace(
"_leftImg8bit.png", "_gtFine_labelIds.png"
)
if not mask_path.exists():
raise FileNotFoundError(f"Mask not found for {image_path}: {mask_path}")
image_name = image_path.name.replace("_leftImg8bit", "")
mask_name = mask_path.name.replace("_gtFine_labelIds", "")
copy2(image_path, dst_image_dir / image_name)
copy2(mask_path, dst_mask_dir / mask_name)Link to this section用法#
要使用 1024 的图像大小在 Cityscapes 数据集上训练 YOLO26n-sem 模型 100 个周期,你可以使用以下代码片段。有关可用参数的完整列表,请参阅模型训练页面。
from ultralytics import YOLO
# Load a model
model = YOLO("yolo26n-sem.pt") # load a pretrained model (recommended for training)
# Train the model
results = model.train(data="cityscapes.yaml", epochs=100, imgsz=1024)Link to this section引用与致谢#
如果你在研究或开发工作中使用了 Cityscapes 数据集,请引用以下论文:
@inproceedings{Cordts2016Cityscapes,
title={The Cityscapes Dataset for Semantic Urban Scene Understanding},
author={Cordts, Marius and Omran, Mohamed and Ramos, Sebastian and Rehfeld, Timo and Enzweiler, Markus and Benenson, Rodrigo and Franke, Uwe and Roth, Stefan and Schiele, Bernt},
booktitle={Proc. of the IEEE Conference on Computer Vision and Pattern Recognition (CVPR)},
year={2016}
}我们要感谢 Cityscapes 团队为自动驾驶和计算机视觉社区创建并维护了这一宝贵的资源。有关 Cityscapes 数据集及其创建者的更多信息,请访问 Cityscapes 数据集网站。
Link to this section常见问题解答#
Link to this section什么是 Cityscapes 数据集,它对于计算机视觉为什么很重要?#
Cityscapes 数据集是一个大规模的语义分割基准,专注于在 50 个欧洲城市拍摄的城市街道场景。它包含 5,000 张经过精细标注的图像,涵盖 19 个评估类别,使其成为自动驾驶和城市场景理解研究的基础资源。其高分辨率图像、密集标注和标准化的平均交并比 (mIoU) 指标使其成为基准测试密集预测模型的理想选择。
Link to this section我该如何使用 Cityscapes 数据集训练 YOLO 模型?#
要使用 1024 的图像大小在 Cityscapes 数据集上训练 YOLO26n-sem 模型 100 个周期,你可以使用以下代码片段。有关可用参数的详细列表,请参阅模型训练页面。
from ultralytics import YOLO
# Load a model
model = YOLO("yolo26n-sem.pt") # load a pretrained model (recommended for training)
# Train the model
results = model.train(data="cityscapes.yaml", epochs=100, imgsz=1024)Link to this sectionCityscapes 数据集是如何构建的?#
准备好后,数据集被组织成 images/{train,val,test}/ 和 masks/{train,val,test}/ 目录,每张图像都与一个单通道 PNG 掩码配对。Ultralytics YAML 文件通过 masks_dir: masks 字段将每张图像与其掩码配对,并使用 label_mapping 将原始 Cityscapes 标签 ID 转换为标准的 19 个连续训练 ID,并将忽略和无效标签映射为 255。
Link to this section我需要手动下载 Cityscapes 吗?#
Yes. Cityscapes requires accepting the dataset terms on the official website. Download and extract leftImg8bit and gtFine into the cityscapes dataset root before using the preparation block in cityscapes.yaml to create the expected images/ and masks/ layout.
Link to this section为什么 Cityscapes 使用 label_mapping?#
Cityscapes 源掩码存储的原始标签 ID 与用于评估的 19 个训练 ID 不同。label_mapping 部分将有效标签转换为连续的类 ID 0–18,并将忽略和无效标签分配为 255,以便在训练和验证期间将其从损失函数和指标计算中排除。