【语义分割】——VOC12AUG数据集解析,标签制作,可视化

简介

简介参考:PASCAL VOC 2012 and SBD (the augment dataset) 总结

地址: http://home.bharathh.info/pubs/codes/SBD/download.html

下载下来的的数据集有 cls img inst 三个文件夹。

  • cls:语义标签,但是是.mat格式
  • img:文件夹则是样本

1. mat转语义png

这里直接上code。 只需要修改main函数中的 input_path output_path就ok了

from __future__ import print_function
import os
import sys
import glob
import scipy.io
from PIL import Image as PILImage

# Mat to png conversion for http://www.cs.berkeley.edu/~bharath2/codes/SBD/download.html
# 'GTcls' key is for class segmentation
# 'GTinst' key is for instance segmentation
def mat2png_hariharan(mat_file, key='GTcls'):
    mat = scipy.io.loadmat(mat_file, mat_dtype=True, squeeze_me=True, struct_as_record=False)
    return mat[key].Segmentation

def main():
    # input_path, output_path = process_arguments("/home/data/CM/data/Segmentation/voc12_aug/benchmark_RELEASE/dataset/cls")
    input_path = "./voc12_aug/benchmark_RELEASE/dataset/cls"	# mat所在文件夹
    output_path = './voc12_aug/benchmark_RELEASE/dataset/seg'	# 输出的语义png存放文件夹

    if os.path.isdir(input_path) and os.path.isdir(output_path):
        mat_files = glob.glob(os.path.join(input_path, '*.mat'))
        convert_mat2png(mat_files, output_path)
    else:
        help('Input or output path does not exist!\n')

def process_arguments(argv):
    num_args = len(argv)

    input_path  = None
    output_path = None

    if num_args == 3:
        input_path  = argv[1]
        output_path = argv[2]
    else:
        help()

    return input_path, output_path

def convert_mat2png(mat_files, output_path):
    if not mat_files:
        help('Input directory does not contain any Matlab files!\n')

    for mat in mat_files:
        numpy_img = mat2png_hariharan(mat)
        pil_img = PILImage.fromarray(numpy_img)
        pil_img.save(os.path.join(output_path, modify_image_name(mat, 'png')))

# Extract name of image from given path, replace its extension with specified one
# and return new name only, not path.
def modify_image_name(path, ext):
    return os.path.basename(path).split('.')[0] + '.' + ext

def help(msg=''):
    print(msg +
        'Usage: python mat2png.py INPUT_PATH OUTPUT_PATH\n'
        'INPUT_PATH denotes path containing Matlab files for conversion.\n'
        'OUTPUT_PATH denotes path where converted Png files ar going to be saved.'
        , file=sys.stderr)
    exit()

if __name__ == '__main__':
    main()

2. 语义图可视化

语义图的差别是很小的。这里再给出一个可视化脚本,将类别进行颜色可视化。

code

from __future__ import print_function
import os
import sys
import numpy as np
from skimage.io import imread, imsave
import glob


def pascal_palette():
    palette = {(0, 0, 0): 0,
               (128, 0, 0): 1,
               (0, 128, 0): 2,
               (128, 128, 0): 3,
               (0, 0, 128): 4,
               (128, 0, 128): 5,
               (0, 128, 128): 6,
               (128, 128, 128): 7,
               (64, 0, 0): 8,
               (192, 0, 0): 9,
               (64, 128, 0): 10,
               (192, 128, 0): 11,
               (64, 0, 128): 12,
               (192, 0, 128): 13,
               (64, 128, 128): 14,
               (192, 128, 128): 15,
               (0, 64, 0): 16,
               (128, 64, 0): 17,
               (0, 192, 0): 18,
               (128, 192, 0): 19,
               (0, 64, 128): 20}

    return palette


def convert_from_color_segmentation(seg):
    color_seg = np.zeros((seg.shape[0], seg.shape[1], 3), dtype=np.uint8)
    palette = pascal_palette()

    for c, i in palette.items():
        color_seg[ seg == i] = c
        
    color_seg = color_seg[..., ::-1]

    return color_seg


def main():
    ##
    ext = '.png'
    ##
    # path, txt_file, path_converted = process_arguments(sys.argv)
    path = './voc12_aug/benchmark_RELEASE/dataset/seg/*.png'					# 前面转换后的语义图片
    path_converted = './voc12_aug/benchmark_RELEASE/dataset/seg_visualize'				# 着色后的图片的保存位置

    # Create dir for converted labels
    if not os.path.isdir(path_converted):
        os.makedirs(path_converted)

    f = glob.glob(path)
    for img_name in f:
        img_base_name = os.path.basename(img_name)
        img = imread(img_name)

        if (len(img.shape) == 2):
            img = convert_from_color_segmentation(img)
            imsave(os.path.join(path_converted, img_base_name), img)
        else:
            print(img_name + " is not composed of three dimensions, therefore "
                                "shouldn't be processed by this script.\n"
                                "Exiting.", file=sys.stderr)
            exit()


def process_arguments(argv):
    if len(argv) != 4:
        help()

    path = argv[1]
    list_file = argv[2]
    new_path = argv[3]

    return path, list_file, new_path


def help():
    print('Usage: python convert_labels.py PATH LIST_FILE NEW_PATH\n'
          'PATH points to directory with segmentation image labels.\n'
          'LIST_FILE denotes text file containing names of images in PATH.\n'
          'Names do not include extension of images.\n'
          'NEW_PATH points to directory where converted labels will be stored.'
          , file=sys.stderr)
    exit()


if __name__ == '__main__':
    main()

结果
【语义分割】——VOC12AUG数据集解析,标签制作,可视化_第1张图片

你可能感兴趣的:(语义分割,voc12,语义分割)