GitHub上很多语义分割的项目都使用了cityscape数据集,在训练自己的数据时,不想改太多代码,需要将自己的标签数据转换成cityscape格式.
1. cityscape标签数据格式
- cityscape标签数据是单通道的灰度图像,第n类目标的像素灰度值为n;
- cityscape有自己的标注软件, cityscape使用笔记,我没有使用;
2. 生成自己的数据
- 我是使用labelme打的语义分割标签;
- labelme安装、使用、标签指定颜色
- 在制定颜色时,将其中一个通道®的灰度值设置为类值n,
lable.py

- json文件夹里label.png的R通道就是需要的gtFine标签图像;
标签图像、.json文件和生成的json文件夹都在2020文件夹里

'''
将color.png => gtFine_labelIds.png
单通道,灰度值0-n, n为类别
labelme json文件处理生成的label.png R通道的像素灰度值对应类别n
直接取R通道的图像作为gtFine
'''
import os
import cv2
json_path = './2020-6-11_11_5'
for root, dirs, files in os.walk(json_path):
for file in files:
if file.strip() == "label.png":
file_path = os.path.join(root, file)
src_image_path = os.path.join(root, 'img.png')
image = cv2.imread(file_path)
src = cv2.imread(src_image_path)
file_name = os.path.split(root)[-1]
file_name = file_name.split('_')[0] + '.png'
B, G, R = cv2.split(image)
path = os.path.join(os.path.abspath('.'), 'gtFine')
if not os.path.exists(path):
os.makedirs(path)
labelId_image = os.path.join(path, file_name.split('.')[0] + '_gtFine_labelIds.' + file_name.split('.')[-1])
print(labelId_image)
cv2.imwrite(labelId_image, R)
src_image_path = os.path.join(os.path.abspath('.'), 'leftImg8bit')
if not os.path.exists(src_image_path):
os.makedirs(src_image_path)
src_image = os.path.join(src_image_path, file_name)
cv2.imwrite(src_image, src)
R = R * 100
cv2.imshow('label', R)
cv2.waitKey(1)
print(file_name)
- 生成train、val、test文件夹
'''
将数据分为train val
'''
import os
import random
import shutil
total_list = []
train_list = []
val_list = []
test_list = []
image_path = './leftImg8bit'
label_path = './gtFine'
for dir in ['train', 'val', 'test']:
image_dir = os.path.join(image_path, dir)
label_dir = os.path.join(label_path, dir)
if os.path.exists(image_dir):
shutil.rmtree(image_dir)
os.makedirs(image_dir)
if os.path.exists(label_dir):
shutil.rmtree(label_dir)
os.makedirs(label_dir)
for root, dirs, files in os.walk(image_path):
for file in files:
if file.endswith('png'):
total_list.append(file)
total_size = len(total_list)
train_size = int(total_size * 0.7)
val_size = int(total_size * 0.2)
train_list = random.sample(total_list, train_size)
remain_list = list(set(total_list) - set(train_list))
val_list = random.sample(remain_list, val_size)
test_list = list(set(remain_list) - set(val_list))
print(len(total_list))
print(len(train_list))
print(len(val_list))
print(len(test_list))
image_path = './leftImg8bit'
label_path = './gtFine'
for dir in ['train', 'val', 'test']:
image_dir = os.path.join(image_path, dir)
label_dir = os.path.join(label_path, dir)
if os.path.exists(image_dir):
shutil.rmtree(image_dir)
os.makedirs(image_dir)
if os.path.exists(label_dir):
shutil.rmtree(label_dir)
os.makedirs(label_dir)
for file in total_list:
image_path_0 = os.path.join(image_path, file)
label_file = file.split('.')[0] + '_gtFine_labelIds' + '.png'
label_path_0 = os.path.join(label_path, label_file)
if file in train_list:
image_path_1 = os.path.join(image_path, 'train', file)
shutil.move(image_path_0, image_path_1)
label_path_1 = os.path.join(label_path, 'train', label_file)
shutil.move(label_path_0, label_path_1)
elif file in val_list:
image_path_1 = os.path.join(image_path, 'val', file)
shutil.move(image_path_0, image_path_1)
label_path_1 = os.path.join(label_path, 'val', label_file)
shutil.move(label_path_0, label_path_1)
elif file in test_list:
image_path_1 = os.path.join(image_path, 'test', file)
shutil.move(image_path_0, image_path_1)
label_path_1 = os.path.join(label_path, 'test', label_file)
shutil.move(label_path_0, label_path_1)
- 还可能需要crop
'''
将1280×720
=》1280×704
'''
import cv2
import os
import shutil
image_path = './leftImg8bit'
label_path = './gtFine'
print("processing")
for root, dirs, files in os.walk(image_path):
for file in files:
if file.endswith('.png'):
image_name = os.path.join(root, file)
image = cv2.imread(image_name, -1)
crop_image = image[:704, :, :]
os.remove(image_name)
cv2.imwrite(image_name, crop_image)
for root, dirs, files in os.walk(label_path):
for file in files:
if file.endswith('.png'):
image_name = os.path.join(root, file)
image = cv2.imread(image_name, -1)
crop_image = image[:704, :]
os.remove(image_name)
cv2.imwrite(image_name, crop_image)
print("processing")