对于自定义数据集需要在mmseg/datasets下建立自己的数据集文件,如
import os.path as osp
from .builder import DATASETS
from .custom import CustomDataset
@DATASETS.register_module() # 注册 不要忘记在__init__.py作显示导入
class RoadDataset(CustomDataset):
CLASSES = ('background', 'foreground') # 类别名称设置
PALETTE = [[120, 120, 120], [6, 230, 230]] # 调色板设置
def __init__(self,**kwargs):
super(RoadDataset, self).__init__(
img_suffix='_sat.jpg', # img文件‘后缀’
seg_map_suffix='_mask.png', # gt文件‘后缀’
"""
对于二分类设成False,对于多分类,视数据集而定,对于ade20k为True
因为0代表背景,但是不包含在150个类别中
"""
reduce_zero_label=False,
**kwargs)
assert osp.exists(self.img_dir)
同时需要在configs/dataset下建立自己的数据处理配置文件,如
# dataset settings
dataset_type = 'RoadDataset'
data_root = 'data/DeepGlobe'
img_norm_cfg = dict(
mean=[90.473, 91.277, 83.520], std=[50.5127, 48.89, 48.681], to_rgb=True)
img_scale = (1024, 1024) # 图像的原始尺寸
crop_size = (256, 256)
train_pipeline = [
dict(type='LoadImageFromFile'),
dict(type='LoadAnnotations'),
dict(type='Resize', img_scale=img_scale, ratio_range=(0.5, 2.0)),
dict(type='RandomCrop', crop_size=crop_size, cat_max_ratio=0.75),
dict(type='RandomFlip', prob=0.5),
dict(type='PhotoMetricDistortion'),
dict(type='Normalize', **img_norm_cfg),
dict(type='Pad', size=crop_size, pad_val=0, seg_pad_val=255),
dict(type='DefaultFormatBundle'),
dict(type='Collect', keys=['img', 'gt_semantic_seg'])
]
test_pipeline = [
dict(type='LoadImageFromFile'),
dict(
type='MultiScaleFlipAug',
img_scale=img_scale,
flip=False,
transforms=[
dict(type='Resize', keep_ratio=True),
dict(type='RandomFlip'),
dict(type='Normalize', **img_norm_cfg),
dict(type='ImageToTensor', keys=['img']),
dict(type='Collect', keys=['img']),
])
]
data = dict(
samples_per_gpu=8,
workers_per_gpu=4,
train=dict(
type=dataset_type,
data_root=data_root,
img_dir='train/img',
ann_dir='train/label',
pipeline=train_pipeline),
val=dict(
type=dataset_type,
data_root=data_root,
img_dir='val/img',
ann_dir='val/label',
pipeline=test_pipeline),
test=dict(
type=dataset_type,
data_root=data_root,
img_dir='val/img',
ann_dir='val/label',
pipeline=test_pipeline))
mmsegmentation中要求,gt的像素值应该在[0,N-1],其中N为类别数
这个很重要,像素的值得从0开始逐渐递增。
我之前就在处理potsdam数据集的时候,重新二值化把像素的值设置成了1,2,3,4,5,6、结果就有一类的精度异常,怎么都是0。
比如在2分类的时候,像素的值就得是0,1. (0,255)的设置用于训练跑的起来,但是结果不对。
参考链接:
https://zhuanlan.zhihu.com/p/380189172
2. mmsegmentation调色板palette的使用
对分割结果进行可视化时,往往可以通过调色板技术将灰度图显示为彩色图
在mmsegmentation中的核心代码如下(以ade20K为例):
调色板的定义:
PALETTE = [[120, 120, 120], [180, 120, 120], [6, 230, 230], [80, 50, 50],
[4, 200, 3], [120, 120, 80], [140, 140, 140], [204, 5, 255],
[230, 230, 230], [4, 250, 7], [224, 5, 255], [235, 255, 7],
[150, 5, 61], [120, 120, 70], [8, 255, 51], [255, 6, 82],
[143, 255, 140], [204, 255, 4], [255, 51, 7], [204, 70, 3],
[0, 102, 200], [61, 230, 250], [255, 6, 51], [11, 102, 255],
[255, 7, 71], [255, 9, 224], [9, 7, 230], [220, 220, 220],
[255, 9, 92], [112, 9, 255], [8, 255, 214], [7, 255, 224],
[255, 184, 6], [10, 255, 71], [255, 41, 10], [7, 255, 255],
[224, 255, 8], [102, 8, 255], [255, 61, 6], [255, 194, 7],
[255, 122, 8], [0, 255, 20], [255, 8, 41], [255, 5, 153],
[6, 51, 255], [235, 12, 255], [160, 150, 20], [0, 163, 255],
[140, 140, 140], [250, 10, 15], [20, 255, 0], [31, 255, 0],
[255, 31, 0], [255, 224, 0], [153, 255, 0], [0, 0, 255],
[255, 71, 0], [0, 235, 255], [0, 173, 255], [31, 0, 255],
[11, 200, 200], [255, 82, 0], [0, 255, 245], [0, 61, 255],
[0, 255, 112], [0, 255, 133], [255, 0, 0], [255, 163, 0],
[255, 102, 0], [194, 255, 0], [0, 143, 255], [51, 255, 0],
[0, 82, 255], [0, 255, 41], [0, 255, 173], [10, 0, 255],
[173, 255, 0], [0, 255, 153], [255, 92, 0], [255, 0, 255],
[255, 0, 245], [255, 0, 102], [255, 173, 0], [255, 0, 20],
[255, 184, 184], [0, 31, 255], [0, 255, 61], [0, 71, 255],
[255, 0, 204], [0, 255, 194], [0, 255, 82], [0, 10, 255],
[0, 112, 255], [51, 0, 255], [0, 194, 255], [0, 122, 255],
[0, 255, 163], [255, 153, 0], [0, 255, 10], [255, 112, 0],
[143, 255, 0], [82, 0, 255], [163, 255, 0], [255, 235, 0],
[8, 184, 170], [133, 0, 255], [0, 255, 92], [184, 0, 255],
[255, 0, 31], [0, 184, 255], [0, 214, 255], [255, 0, 112],
[92, 255, 0], [0, 224, 255], [112, 224, 255], [70, 184, 160],
[163, 0, 255], [153, 0, 255], [71, 255, 0], [255, 0, 163],
[255, 204, 0], [255, 0, 143], [0, 255, 235], [133, 255, 0],
[255, 0, 235], [245, 0, 255], [255, 0, 122], [255, 245, 0],
[10, 190, 212], [214, 255, 0], [0, 204, 255], [20, 0, 255],
[255, 255, 0], [0, 153, 255], [0, 41, 255], [0, 255, 204],
[41, 0, 255], [41, 255, 0], [173, 0, 255], [0, 245, 255],
[71, 0, 255], [122, 0, 255], [0, 255, 184], [0, 92, 255],
[184, 255, 0], [0, 133, 255], [255, 214, 0], [25, 194, 194],
[102, 255, 0], [92, 0, 255]]
调色板的使用:
seg = np.array(seg_map)
color_seg = np.zeros((seg.shape[0], seg.shape[1], 3), dtype=np.uint8)
for label, color in enumerate(PALETTE):
color_seg[seg == label, :] = color # numpy 数组的“新奇”使用,就是把预测结果的灰度像素值改成RGB
color_seg = color_seg[..., ::-1] # convert to BGR (cv2的存储顺序是GBR,所以逆序读取RGB就行了)
cv2.imwrite(out_file,color_seg)
ps:PIL中的调色板模式为P,每个像素值对应一个RGB值