From 327a65d6f581da800f713e9815cc8362912e03be Mon Sep 17 00:00:00 2001 From: LutaoChu <30695251+LutaoChu@users.noreply.github.com> Date: Wed, 11 Dec 2019 10:50:38 +0800 Subject: [PATCH] improve gray2psedo_color and data_prepare (#113) * modify data_prepare * improve gray2pseudo_color --- docs/data_prepare.md | 18 ++++++------ pdseg/tools/gray2pseudo_color.py | 49 ++++++++++++++++++++------------ 2 files changed, 40 insertions(+), 27 deletions(-) diff --git a/docs/data_prepare.md b/docs/data_prepare.md index 6d93992e..734a13c6 100644 --- a/docs/data_prepare.md +++ b/docs/data_prepare.md @@ -94,7 +94,7 @@ PaddleSeg采用通用的文件列表方式组织训练集、验证集和测试 **注意事项** -* 务必保证分隔符在文件列表中每行只存在一次, 如文件名中存在空格,请使用'|'等文件名不可用字符进行切分 +* 务必保证分隔符在文件列表中每行只存在一次, 如文件名中存在空格,请使用"|"等文件名不可用字符进行切分 * 文件列表请使用**UTF-8**格式保存, PaddleSeg默认使用UTF-8编码读取file_list文件 @@ -128,11 +128,11 @@ python pdseg/tools/create_dataset_list.py ${FLAGS} |FLAG|用途|默认值|参数数目| |-|-|-|-| |--type|指定数据集类型,`cityscapes`或`自定义`|`自定义`|1| -|--separator|文件列表分隔符|'|'|1| -|--folder|图片和标签集的文件夹名|'images' 'annotations'|2| -|--second_folder|训练/验证/测试集的文件夹名|'train' 'val' 'test'|若干| -|--format|图片和标签集的数据格式|'jpg' 'png'|2| -|--postfix|按文件主名(无扩展名)是否包含指定后缀对图片和标签集进行筛选|'' ''(2个空字符)|2| +|--separator|文件列表分隔符|"|"|1| +|--folder|图片和标签集的文件夹名|"images" "annotations"|2| +|--second_folder|训练/验证/测试集的文件夹名|"train" "val" "test"|若干| +|--format|图片和标签集的数据格式|"jpg" "png"|2| +|--postfix|按文件主名(无扩展名)是否包含指定后缀对图片和标签集进行筛选|"" ""(2个空字符)|2| #### 使用示例 - **对于自定义数据集** @@ -184,9 +184,9 @@ python pdseg/tools/create_dataset_list.py \ |FLAG|固定值| |-|-| -|--folder|'leftImg8bit' 'gtFine'| -|--format|'png' 'png'| -|--postfix|'_leftImg8bit' '_gtFine_labelTrainIds'| +|--folder|"leftImg8bit" "gtFine"| +|--format|"png" "png"| +|--postfix|"_leftImg8bit" "_gtFine_labelTrainIds"| 其余FLAG可以按需要设定。 ``` diff --git a/pdseg/tools/gray2pseudo_color.py b/pdseg/tools/gray2pseudo_color.py index 59555783..6128f377 100644 --- a/pdseg/tools/gray2pseudo_color.py +++ b/pdseg/tools/gray2pseudo_color.py @@ -2,7 +2,6 @@ from __future__ import print_function import argparse -import glob import os import os.path as osp import sys @@ -57,18 +56,29 @@ def gray2pseudo_color(args): color_map = get_color_map_list(256) if os.path.isdir(input): - for grt_path in glob.glob(osp.join(input, '*.png')): - print('Converting original label:', grt_path) - basename = osp.basename(grt_path) - - im = Image.open(grt_path) - lbl = np.asarray(im) - - lbl_pil = Image.fromarray(lbl.astype(np.uint8), mode='P') - lbl_pil.putpalette(color_map) - - new_file = osp.join(output_dir, basename) - lbl_pil.save(new_file) + for fpath, dirs, fs in os.walk(input): + for f in fs: + try: + grt_path = osp.join(fpath, f) + _output_dir = fpath.replace(input, '') + if _output_dir[0] == os.path.sep: + _output_dir = _output_dir.lstrip(os.path.sep) + + im = Image.open(grt_path) + lbl = np.asarray(im) + + lbl_pil = Image.fromarray(lbl.astype(np.uint8), mode='P') + lbl_pil.putpalette(color_map) + + real_dir = osp.join(output_dir, _output_dir) + if not osp.exists(real_dir): + os.makedirs(real_dir) + new_grt_path = osp.join(real_dir, f) + + lbl_pil.save(new_grt_path) + print('New label path:', new_grt_path) + except: + continue elif os.path.isfile(input): if args.dataset_dir is None or args.file_separator is None: print('No dataset_dir or file_separator input!') @@ -79,17 +89,20 @@ def gray2pseudo_color(args): grt_name = parts[1] grt_path = os.path.join(args.dataset_dir, grt_name) - print('Converting original label:', grt_path) - basename = osp.basename(grt_path) - im = Image.open(grt_path) lbl = np.asarray(im) lbl_pil = Image.fromarray(lbl.astype(np.uint8), mode='P') lbl_pil.putpalette(color_map) - new_file = osp.join(output_dir, basename) - lbl_pil.save(new_file) + grt_dir, _ = osp.split(grt_name) + new_dir = osp.join(output_dir, grt_dir) + if not osp.exists(new_dir): + os.makedirs(new_dir) + new_grt_path = osp.join(output_dir, grt_name) + + lbl_pil.save(new_grt_path) + print('New label path:', new_grt_path) else: print('It\'s neither a dir nor a file') -- GitLab