Cityscapes数据集官网下载地址:
https://www.cityscapes-dataset.com/
相关介绍:从官网下载这三个压缩包文件leftImg8bit_trainvaltest.zip、gtCoarse.zip、gtFine_trainvaltest.zip
1)leftImg8bit_trainvaltest.zip分为train、val以及test三个文件夹,共包含了5000张图像;
2)gtFine_trainvaltest.zip是精细化的注释信息,在其精细标注数据集文件夹(gtFine)中,也有train、val以及test三个文件夹,每张图片对应四个标注文件:用于可视化的彩色标注图
(_color.png)、用于实例分割的实例I图
(_instanceIds.png)、用于语义分割的标签图
(_labelsIds.png)以及包含原始人工标注信息的JSON文件
(_polygons.json)等;
Cityscapes数据集简单介绍:
城市街景数据集 Cityscapes © 收集了来自 50 个不同城市、良好天气条件下的街景,共包含 5000 张高分辨率(1024x2048)图像。其中 3475 张带标注的图像用于训练和验证(2975 张用于训练,500 张用于验证
),1525 张未标注图像用于测试。此外,数据集中还提供了 20000 张弱标注图像供研究使用。该数据集涵盖 30 多种类别
,主要包括城市街景中的车辆、行人、建筑物、道路等常见物体和场景。
实例分割数据集改造为目标检测数据集
将分割数据中的像素级多边形标注转换为目标检测的边界框(bbox)标注
,其实并不复杂。每个分割目标的多边形标注由多个像素点的 [x, y] 坐标组成,只需找到这些坐标的 Xmin, Ymin, Xmax, 和 Ymax 值,就能确定该目标的边界框
。
结果:
preprocess.py代码如下
import os
import glob
from shutil import copy2
from PIL import Image
import json
import numpy as np
def copy_file(src, src_ext, dst):
# find all files ends up with ext
flist = sorted(glob.glob(os.path.join(src, '*', src_ext)))
for fname in flist:
src_path = os.path.join(src, fname)
copy2(src_path, dst)
print('copied %s to %s' % (src_path, dst))
def construct_box(inst_root, inst_name, cls_name, dst):
inst_list = sorted(glob.glob(os.path.join(inst_root, '*', inst_name)))
cls_list = sorted(glob.glob(os.path.join(inst_root, '*', cls_name)))
for inst, cls in zip(*(inst_list, cls_list)):
inst_map = Image.open(os.path.join(inst_root, inst))
inst_map = np.array(inst_map, dtype=np.int32)
cls_map = Image.open(os.path.join(inst_root, cls))
cls_map = np.array(cls_map, dtype=np.int32)
H, W = inst_map.shape
# get a list of unique instances
inst_info = {'imgHeight':H, 'imgWidth':W, 'objects':{}}
inst_ids = np.unique(inst_map)
for iid in inst_ids:
if int(iid) < 1000: # filter out non-instance masks
continue
ys,xs = np.where(inst_map==iid)
ymin, ymax, xmin, xmax = \
ys.min(), ys.max(), xs.min(), xs.max()
cls_label = np.median(cls_map[inst_map==iid])
inst_info['objects'][str(iid)] = {'bbox': [int(xmin), int(ymin), int(xmax), int(ymax)],
'cls': int(cls_label)}
# write a file to path
filename = os.path.splitext(os.path.basename(inst))[0]
savename = os.path.join(dst, filename + '.json')
with open(savename, 'w') as f:
json.dump(inst_info, f)
print('wrote a bbox summary of %s to %s' % (inst, savename))
# organize image
if __name__ == '__main__':
folder_name = 'datasets/cityscape/'
train_img_dst = os.path.join(folder_name, 'train_img')
train_label_dst = os.path.join(folder_name, 'train_label')
train_inst_dst = os.path.join(folder_name, 'train_inst')
train_bbox_dst = os.path.join(folder_name, 'train_bbox')
val_img_dst = os.path.join(folder_name, 'val_img')
val_label_dst = os.path.join(folder_name, 'val_label')
val_inst_dst = os.path.join(folder_name, 'val_inst')
val_bbox_dst = os.path.join(folder_name, 'val_bbox')
if not os.path.exists(train_img_dst):
os.makedirs(train_img_dst)
if not os.path.exists(train_label_dst):
os.makedirs(train_label_dst)
if not os.path.exists(train_inst_dst):
os.makedirs(train_inst_dst)
if not os.path.exists(val_img_dst):
os.makedirs(val_img_dst)
if not os.path.exists(val_label_dst):
os.makedirs(val_label_dst)
if not os.path.exists(val_inst_dst):
os.makedirs(val_inst_dst)
root = "G:\Dataset\cityscapes"
# # train_image
# copy_file('G:/Dataset/cityscapes/leftImg8bit/train',\
# '*_leftImg8bit.png', train_img_dst)
# # train_label
# copy_file('G:/Dataset/cityscapes/gtFine/train',\
# '*_labelIds.png', train_label_dst)
# # train_inst
# copy_file('G:/Dataset/cityscapes/gtFine/train',\
# '*_instanceIds.png', train_inst_dst)
# # val_image
# copy_file('G:/Dataset/cityscapes/leftImg8bit/val',\
# '*_leftImg8bit.png', val_img_dst)
# # val_label
# copy_file('G:/Dataset/cityscapes/gtFine/val',\
# '*_labelIds.png', val_label_dst)
# # val_inst
# copy_file('G:/Dataset/cityscapes/gtFine/val',\
# '*_instanceIds.png', val_inst_dst)
if not os.path.exists(train_bbox_dst):
os.makedirs(train_bbox_dst)
if not os.path.exists(val_bbox_dst):
os.makedirs(val_bbox_dst)
# wrote a bounding box summary
construct_box('G:/Dataset/cityscapes/gtFine/train',\
'*_instanceIds.png', '*_labelIds.png', train_bbox_dst)
construct_box('G:/Dataset/cityscapes/gtFine/val',\
'*_instanceIds.png', '*_labelIds.png', val_bbox_dst)