Cityscapes 数据集

Cityscapes 数据集是一个大规模语义分割基准,专注于在 50 个欧洲城市拍摄的城市街道场景。它提供高质量的像素级标注,是自动驾驶研究和城市场景理解领域中配合 Ultralytics YOLO 模型使用最广泛的数据集之一。

主要特性

  • Cityscapes 的精细标注包含 2,975 张训练图像、500 张验证图像和 1,525 张测试图像。
  • 该数据集涵盖 19 个评估类别,横跨道路、车辆、行人、建筑、物体、自然和天空等类别。
  • Cityscapes 提供了如平均交并比 (mIoU) 等标准化评估指标,用于语义分割,从而能够有效地比较模型性能。

数据集结构

准备工作完成后,Ultralytics 配置预期将采用以下布局:

cityscapes/
├── images/
│   ├── train/
│   ├── val/
│   └── test/
└── masks/
    ├── train/
    ├── val/
    └── test/

语义掩码是单通道 PNG 文件。原始 Cityscapes 标签 ID 通过 label_mapping 部分映射到标准的 19 个训练 ID,而被忽略或无效的标签被映射为 255,以便在训练和评估中将其排除。请从 Cityscapes 官网下载官方的 leftImg8bitgtFine 归档文件并将其解压到数据集根目录;随后 cityscapes.yaml 中的准备模块会将图像和掩码组织成此布局。

应用

Cityscapes 被广泛用于训练和评估语义分割中的深度学习模型,特别是在自动驾驶、高级驾驶辅助系统 (ADAS) 和城市机器人领域。

其高分辨率图像和详细的标注也使其在实时场景解析、车道和障碍物理解,以及任何需要对复杂城市环境进行密集像素级理解的任务研究中极具价值。

数据集 YAML

数据集 YAML 文件定义了 Cityscapes 的路径、类别、掩码目录和标签映射。cityscapes.yaml 文件维护在 https://github.com/ultralytics/ultralytics/blob/main/ultralytics/cfg/datasets/cityscapes.yaml

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)

使用方法

若要使用图像大小为 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)

引用与致谢

如果你在研究或开发工作中使用 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 数据集网站

常见问题

什么是 Cityscapes 数据集,它对于计算机视觉为何如此重要?

Cityscapes 数据集是一个大规模语义分割基准,专注于在 50 个欧洲城市拍摄的城市街道场景。它包含 5,000 张经过精细标注的图像,涵盖 19 个评估类别,是自动驾驶和城市场景理解研究的基础资源。其高分辨率图像、密集标注和标准化的平均交并比 (mIoU) 指标使其成为基准测试密集预测模型的理想选择。

我该如何使用 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)

Cityscapes 数据集是如何组织的?

准备完成后,数据集被组织为 images/{train,val,test}/masks/{train,val,test}/ 目录,每张图像都配有一个单通道 PNG 掩码。Ultralytics YAML 文件通过 masks_dir: masks 字段将每张图像与其掩码配对,并使用 label_mapping 将原始 Cityscapes 标签 ID 转换为标准的 19 个连续训练 ID,并将被忽略和无效的标签映射为 255

我需要手动下载 Cityscapes 吗?

是的。Cityscapes 要求在官网接受数据集条款。请先下载 leftImg8bitgtFine 并将其解压到 cityscapes 数据集根目录,然后再使用 cityscapes.yaml 中的准备模块来创建预期的 images/masks/ 布局。

为什么 Cityscapes 使用 label_mapping

Cityscapes 源掩码存储的原始标签 ID 与评估时使用的 19 个训练 ID 不同。label_mapping 部分将有效标签转换为 018 的连续类别 ID,并为被忽略和无效的标签分配 255,以便在训练和验证过程中将其从损失计算和指标评估中排除。

评论