> 技术文档 > 计算机视觉常用数据集Cityscapes的介绍、下载、转为YOLO格式进行训练_cityscapes数据集

计算机视觉常用数据集Cityscapes的介绍、下载、转为YOLO格式进行训练_cityscapes数据集

我在寻找Cityscapes数据集的时候花了一番功夫,因为官网下载需要用公司或学校邮箱邮箱注册账号,等待审核通过后才能进行下载数据集。并且一开始我也并不了解Cityscapes的格式和内容是什么样的,现在我弄明白后写下这篇文章,用于记录和分享。

计算机视觉常用数据集Foggy Cityscapes的介绍、下载、转为YOLO格式进行训练-CSDN博客文章浏览阅读1k次,点赞25次,收藏14次。大雾城市景观Foggy Cityscapes (F):Foggy Cityscapes是由 Cityscapes 生成的合成数据集,它旨在模拟和研究自动驾驶车辆在雾天条件下的性能,有三个级别的大雾天气(0.005,0.01,0.02),从轻微的雾到浓厚的雾,分别对应于600,300和150米的能见度范围。由于雾天条件下的能见度降低,图像中的许多目标会变得模糊不清,这对计算机视觉检测来说是一个挑战。Foggy Cityscapes 为研究人员提供了一个测试和改进算法的机会,以提高在恶劣条件下算法的鲁棒和准确性 https://blog.csdn.net/m0_63294504/article/details/143357968

1Cityscapes介绍

城市景观Cityscapes(C):Cityscapes 收集了50个不同城市良好天气条件下的城市街景,总共包含5000张真实城市场景图像;其中3475张带标注信息的图像用于训练验证(2,975张图像用于训练,500张用于验证),剩下1525张无标注信息图像用于测试。除此之外,还有20000张弱注释的图像供研究使用。数据集中的图像分辨率为1024x2048,涵盖了30多个类别,包括车辆、行人、建筑物、道路等城市街景中常见的物体和场景;但常用8个类别:

classes = [\'car\', \'person\', \'rider\', \'truck\', \'bus\', \'train\', \'motorcycle\', \'bicycle\']。

Cityscapes数据集是计算机视觉领域常用的数据集,它的高质量标注和丰富场景使其成为评估和训练算法的理想选择。

2、下载

官网下载地址:Cityscapes Dataset – Semantic Understanding of Urban Street Scenes

结尾有我的网盘数据下载方式,已经全部处理好了,可以直接用于YOLO格式的目标检测。

从官网下载这三个压缩包文件:leftImg8bit_trainvaltest.zip、gtCoarse.zip、gtFine_trainvaltest.zip

1leftImg8bit_trainvaltest.zip分为train、val以及test三个文件夹,共包含了5000张图像;

2gtFine_trainvaltest.zip是精细化的注释信息,在其精细标注数据集文件夹(gtFine)中,也有train、val以及test三个文件夹,每张图片对应四个标注文件:用于可视化的彩色标注图(_color.png)、用于实例分割的实例ID图(_instanceIds.png)、用于语义分割的标签ID图(_labelsIds.png)以及包含原始人工标注信息的JSON文件(_polygons.json);如下图:

计算机视觉常用数据集Cityscapes的介绍、下载、转为YOLO格式进行训练_cityscapes数据集

3gtCoarse.zip是粗略的注释信息,一般不使用。

3、转为YOLO格式

对于gtFine_trainvaltest.zip中的gtFine文件夹,我们找到json文件,然后使用下方代码可以将该图片的语义分割json标注转为YOLO格式的标注,将自己的数据集路径替换就行

import jsonimport osfrom sympy import print_glsl# 类别列表和类别字典all_classes = [\'car\', \'person\', \'rider\', \'truck\', \'bus\', \'train\', \'motorcycle\', \'bicycle\']class_dict = {\'car\': 0, \'person\': 1, \'rider\': 2, \'truck\': 3, \'bus\': 4, \'train\': 5, \'motorcycle\': 6, \'bicycle\': 7}# 根目录rootdir = \'D:/深度学习相关数据集/目标检测数据集/Cityscapes/gtFine_trainvaltest/gtFine/test\'# 输出目录output_rootdir = \'D:/深度学习相关数据集/目标检测数据集/Cityscapes/YOLOLabels/test\'def position(pos): x = [point[0] for point in pos] y = [point[1] for point in pos] x_min = min(x) x_max = max(x) y_min = min(y) y_max = max(y) return float(x_min), float(x_max), float(y_min), float(y_max)def convert(size, box): dw = 1. / size[0] dh = 1. / size[1] x = (box[0] + box[1]) / 2.0 y = (box[2] + box[3]) / 2.0 w = box[1] - box[0] h = box[3] - box[2] return x * dw, y * dh, w * dw, h * dhdef convert_annotation(json_id, city_name): json_file_path = os.path.join(rootdir, city_name, \'%s.json\' % json_id) out_file_path = os.path.join(output_rootdir, city_name, \'%s.txt\' % json_id) if not os.path.exists(os.path.dirname(out_file_path)): os.makedirs(os.path.dirname(out_file_path)) with open(json_file_path, \'r\') as load_f: load_dict = json.load(load_f) w = load_dict[\'imgWidth\'] h = load_dict[\'imgHeight\'] objects = load_dict[\'objects\'] with open(out_file_path, \'w\') as out_file: for obj in objects: labels = obj[\'label\'] if labels in class_dict: pos = obj[\'polygon\'] b = position(pos) bb = convert((w, h), b) cls_id = class_dict[labels] out_file.write(str(cls_id) + \" \" + \" \".join([str(a) for a in bb]) + \'\\n\')def jsons_id(rootdir): a = [] for parent, dirnames, filenames in os.walk(rootdir): for filename in filenames: if filename.endswith(\'.json\'): filename_without_ext = os.path.splitext(filename)[0] a.append(filename_without_ext) return a# 获取所有子目录subdirs = [d for d in os.listdir(rootdir) if os.path.isdir(os.path.join(rootdir, d))]# print(subdirs)# [\'aachen\', \'bochum\', \'bremen\', \'cologne\', \'darmstadt\', \'dusseldorf\', \'erfurt\', \'hamburg\', \'hanover\', \'jena\',# \'krefeld\', \'monchengladbach\', \'strasbourg\', \'stuttgart\', \'tubingen\', \'ulm\', \'weimar\', \'zurich\']# 为每个子目录生成YOLO格式的标注文件for subdir in subdirs: names = jsons_id(os.path.join(rootdir, subdir)) for json_id in names: convert_annotation(json_id, subdir)

4、json_to_YOLO结果可视化

当我们对所有图片的Json注释都转换完后,得到YOLO格式的文件夹,长这样:

计算机视觉常用数据集Cityscapes的介绍、下载、转为YOLO格式进行训练_cityscapes数据集

计算机视觉常用数据集Cityscapes的介绍、下载、转为YOLO格式进行训练_cityscapes数据集

这时我们可以运行以下代码,将原图与YOLO标注信息结合起来,得到带有边界框的图像,不要忘记修改你自己的数据集路径。

import cv2import os# 图片路径# image_path = \'../leftImg8bit_trainvaltest/leftImg8bit/train/aachen/aachen_000001_000019_leftImg8bit.png\'image_path = \'../leftImg8bit_trainvaltest/images/val/munster_000167_000019.png\'# YOLO注释文件路径# annotation_path = \'../YOLOLabels/train/aachen/aachen_000001_000019_gtFine_polygons.txt\'annotation_path = \'../leftImg8bit_trainvaltest/labels/val/munster_000167_000019.txt\'# 读取YOLO注释文件with open(annotation_path, \'r\') as file: lines = file.readlines()# 读取图片image = cv2.imread(image_path)# 禁用窗口缩放cv2.namedWindow(\'Image with Bounding Boxes\', cv2.WINDOW_NORMAL)# 类别名称列表# class_names = [\'car\', \'person\', \'rider\', \'truck\', \'bus\', \'train\', \'motorcycle\', \'bicycle\']# 类别名称与ID的映射字典class_dict = {0: \'car\', 1: \'person\', 2: \'rider\', 3: \'truck\', 4: \'bus\', 5: \'train\', 6: \'motorcycle\', 7: \'bicycle\'}# 绘制边界框和类别标签for line in lines: parts = line.strip().split() class_id = int(parts[0]) print(f\"class_id:{class_id}\") x_center = float(parts[1]) y_center = float(parts[2]) width = float(parts[3]) height = float(parts[4]) # 将归一化的坐标转换为像素坐标 x_min = int((x_center - width / 2) * image.shape[1]) y_min = int((y_center - height / 2) * image.shape[0]) x_max = int((x_center + width / 2) * image.shape[1]) y_max = int((y_center + height / 2) * image.shape[0]) # 获取类别名称 class_name = class_dict[class_id] # 绘制边界框 cv2.rectangle(image, (x_min, y_min), (x_max, y_max), (0, 255, 0), 2) # 绘制类别标签 cv2.putText(image, class_name, (x_min, y_min - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (0, 255, 0), 2)print(image.shape)# 显示图片cv2.imshow(\'Image with Bounding Boxes\', image)cv2.waitKey(0)cv2.destroyAllWindows()

得到的结果长这样:

计算机视觉常用数据集Cityscapes的介绍、下载、转为YOLO格式进行训练_cityscapes数据集

计算机视觉常用数据集Cityscapes的介绍、下载、转为YOLO格式进行训练_cityscapes数据集

5、对图像和标签命名统一化

相信有伙伴发现了,官网下载的文件中,对于json标注信息的命名规则是类似于这样的:berlin_000000_000019_gtFine_polygons.json,而图片的命名规则是类似于这样的:berlin_000000_000019_leftImg8bit.png。我们知道如果图像和标签的名称没有保持一致,那么在使用YOLO进行训练的时候,程序就会报错显示找不到标签。

我将图像和标签名称统一化后如下所示:

计算机视觉常用数据集Cityscapes的介绍、下载、转为YOLO格式进行训练_cityscapes数据集

计算机视觉常用数据集Cityscapes的介绍、下载、转为YOLO格式进行训练_cityscapes数据集

现在就可以直接进行训练了,还要注意的是,数据路径不能有中文,否则就会报以下错误。

Dataset not found , missing paths [\'D:\\\\\\\\\\\\\\\\YOLO\\\\images\\\\val\'] Traceback (most recent call last): File \"E:\\pythonCode\\ObjectDetection\\yolov9-main\\train_dual.py\", line 644, in main(opt) File \"E:\\pythonCode\\ObjectDetection\\yolov9-main\\train_dual.py\", line 538, in main train(opt.hyp, opt, device, callbacks) File \"E:\\pythonCode\\ObjectDetection\\yolov9-main\\train_dual.py\", line 97, in train data_dict = data_dict or check_dataset(data) # check if None File \"E:\\pythonCode\\ObjectDetection\\yolov9-main\\utils\\general.py\", line 537, in check_dataset raise Exception(\'Dataset not found ❌\') Exception: Dataset not found ❌

数据配置文件长这样:

计算机视觉常用数据集Cityscapes的介绍、下载、转为YOLO格式进行训练_cityscapes数据集

6、使用YOLOv9-m进行训练

我的GPU是4060Laptop,8GB显存,使用YOLOv9-m,batchsize=4,刚好可以训练,再多就爆显存了。2975训练集、500验证集。

计算机视觉常用数据集Cityscapes的介绍、下载、转为YOLO格式进行训练_cityscapes数据集

7、个人下载方式

通过百度网盘分享的文件:CityScape
链接:https://pan.baidu.com/s/1fDy_c1nXRCsAUHAr9L7Ocg?pwd=zjlp 
提取码:zjlp