def __init__(self, semantic_version_dataroot: str,
                 instance_version_dataroot: str):
        """
			Args:
			-	semantic_version_dataroot: from ADEChallengeData2016
			-	instance_verson_dataroot: from ADE20K_2016_07_26
		"""
        self.img_dir = f'{semantic_version_dataroot}/images'
        self.ade20k_instance_dataroot = instance_version_dataroot

        # following two maps are only used for relabeling original data
        self.id_to_classname_map = get_dataloader_id_to_classname_map(
            dataset_name='ade20k-151')
        self.classname_to_id_map = get_classname_to_dataloaderid_map(
            dataset_name='ade20k-151')

        self.ade20k_split_nickname_dict = {
            'train': 'training',
            'val': 'validation'
        }

        # make it easy to look up an RGB file path, given just the filename stem
        self.fname_to_rgbfpath_dict = {}
        for split in ['train', 'val']:
            ade20k_split_nickname = self.ade20k_split_nickname_dict[split]
            rgb_fpaths = glob.glob(
                f'{self.img_dir}/{ade20k_split_nickname}/*.jpg')
            for rgb_fpath in rgb_fpaths:
                fname_stem = Path(rgb_fpath).stem
                assert fname_stem not in self.fname_to_rgbfpath_dict
                self.fname_to_rgbfpath_dict[fname_stem] = rgb_fpath

        # make it easy to look up a SEG RGB file path, given just the filename stem
        self.fname_to_segrgbfpath_dict = {}
        seg_rgb_fpaths = glob.glob(
            f'{self.ade20k_instance_dataroot}/images/**/*seg.png',
            recursive=True)
        for seg_rgb_fpath in seg_rgb_fpaths:
            fname_stem = Path(seg_rgb_fpath).stem
            assert '_seg' in fname_stem[-4:]
            # remove suffix now
            fname_stem = fname_stem.replace('_seg', '')
            self.fname_to_segrgbfpath_dict[fname_stem] = seg_rgb_fpath
示例#2
0
def test_save_pred_vs_label_7tuple_uniform_random_preds():
    """
	"""
    data_dir = f'{TEST_DATA_ROOT}/Camvid_test_data'

    img_fpath = f'{data_dir}/images/0016E5_08159.png'
    label_fpath = f'{data_dir}/preds/0016E5_08159.png'

    img_rgb = imageio.imread(img_fpath)
    label_img = imageio.imread(label_fpath)
    img_h, img_w = label_img.shape

    pred_img = np.random.randint(0, 459, (img_h, img_w)).astype(np.uint16)
    id_to_class_name_map = get_dataloader_id_to_classname_map(
        'pascal-context-460')

    save_fpath = f'{TEST_DATA_ROOT}/rand_549_temp.png'
    save_pred_vs_label_7tuple(img_rgb, pred_img, label_img,
                              id_to_class_name_map, save_fpath)
    os.remove(f'{TEST_DATA_ROOT}/rand_549_temp_pred_labels_palette.png')
示例#3
0
def test_save_pred_vs_label_7tuple_shifted():
    """ """
    data_dir = f'{TEST_DATA_ROOT}/Camvid_test_data'

    img_fpath = f'{data_dir}/images/0016E5_08159.png'
    label_fpath = f'{data_dir}/preds/0016E5_08159.png'

    img_rgb = imageio.imread(img_fpath)
    label_img = imageio.imread(label_fpath)

    pred_img = np.zeros_like(label_img)
    pred_img[:, 150:] = label_img[:, :-150]

    id_to_class_name_map = get_dataloader_id_to_classname_map(
        'pascal-context-460')

    save_fpath = f'{TEST_DATA_ROOT}/shifted_temp.png'
    save_pred_vs_label_7tuple(img_rgb, pred_img, label_img,
                              id_to_class_name_map, save_fpath)
    os.remove(f'{TEST_DATA_ROOT}/shifted_temp_pred_labels_palette.png')
def visual_sanitychecks():
    """
	Save every 1000th image of each dataset, with classnames embedded.
	"""
    save_dir = 'temp_files/MSeg_verify'

    for dname, d_info in infos.items():
        print(f'Writing visual sanity checks for {dname}...')

        if dname in [
                'coco-panoptic-inst-201', 'mapillary-public66',
                'ade20k-151-inst'
        ]:
            continue  # is in RGB format and not comparable

        id_to_classname_map = get_dataloader_id_to_classname_map(dname)
        splits = ['train', 'val']
        split_lists = [d_info.trainlist, d_info.vallist]

        for split, split_list in zip(splits, split_lists):
            pairs = generate_all_img_label_pair_fpaths(d_info.dataroot,
                                                       split_list)

            for i, (rgb_fpath, label_fpath) in enumerate(pairs[::1000]):
                print(f'On {i*1000}/{len(pairs)} of {dname}')

                rgb_img = cv2_imread_rgb(rgb_fpath)
                label_img = imageio.imread(label_fpath)

                fname_stem = Path(rgb_fpath).stem
                save_fpath = f'{save_dir}/{dname}_{fname_stem}.jpg'

                if rgb_img.ndim == 2:
                    # this image was grayscale
                    rgb_img = grayscale_to_color(rgb_img)

                form_mask_triple_embedded_classnames(rgb_img,
                                                     label_img,
                                                     id_to_classname_map,
                                                     save_fpath,
                                                     save_to_disk=True)
示例#5
0
def dump_pascalcontext_mat_files(pcontext_dst_dir: str) -> None:
    """
	Convert PASCAL Context annotations from .mat files to .png

		Args:
		-	pcontext_dst_dir: string represent absolute path to 
				PASCAL Context destination directory

		Returns:
		-	None
	"""
    dataset_name = 'pascal-context-460'
    id_to_class_name_map = get_dataloader_id_to_classname_map(
        dataset_name, include_ignore_idx_cls=False)

    save_dirname = 'Segmentation_GT_460cls'
    png_save_dir = f'{pcontext_dst_dir}/{save_dirname}'
    check_mkdir(png_save_dir)

    # annotation files, stored as .mat files
    mat_files_dir = f'{pcontext_dst_dir}/trainval'
    mat_fpaths = glob.glob(f'{mat_files_dir}/*.mat')

    for i, mat_fpath in enumerate(mat_fpaths):
        if i % 500 == 0:
            print(f'On {i}/{len(mat_fpaths)}')
        fname_stem = Path(mat_fpath).stem

        label_data = scipy.io.loadmat(mat_fpath)
        label_img = label_data['LabelMap']

        label_save_fpath = f'{png_save_dir}/{fname_stem}.png'

        # Need uint16 to be able to exceed 256 value range
        # there are up to 460 classes present.
        label_img_uint16 = label_img.astype(np.uint16)

        imageio.imwrite(label_save_fpath, label_img_uint16)
        loaded_label_img = imageio.imread(label_save_fpath)
        assert np.allclose(loaded_label_img, label_img)
示例#6
0
def test_save_pred_vs_label_7tuple_100_strided_preds():
    """
	"""
    data_dir = f'{TEST_DATA_ROOT}/Camvid_test_data'

    img_fpath = f'{data_dir}/images/0016E5_08159.png'
    label_fpath = f'{data_dir}/preds/0016E5_08159.png'

    img_rgb = imageio.imread(img_fpath)
    label_img = imageio.imread(label_fpath)
    img_h, img_w = label_img.shape

    class_ids_to_sample = np.array([0, 100, 200, 300])
    pred_img = np.random.choice(a=class_ids_to_sample,
                                size=(img_h, img_w)).astype(np.uint16)
    id_to_class_name_map = get_dataloader_id_to_classname_map(
        'pascal-context-460')

    save_fpath = f'{TEST_DATA_ROOT}/100_strided_temp.png'
    save_pred_vs_label_7tuple(img_rgb, pred_img, label_img,
                              id_to_class_name_map, save_fpath)
    os.remove(f'{TEST_DATA_ROOT}/100_strided_temp_pred_labels_palette.png')
    def __init__(self, dataroot: str, dname: str) -> None:
        """
		"""
        self.dataroot = dataroot
        self.dname = dname
        self.id_to_classname_map = get_dataloader_id_to_classname_map(dname)
示例#8
0
def main():
	"""
	"""
	save_dir = 'temp_files'

	# beforehand
	# orig_dname = 'cityscapes-19'
	# orig_dataroot = '/export/share/Datasets/cityscapes'

	# rel_dname = 'cityscapes-19'
	# rel_dataroot = '/export/share/Datasets/MSeg/mseg_dataset/Cityscapes'

	orig_dname = 'coco-panoptic-133'
	orig_dname_lists = 'coco-panoptic-133-temp'
	orig_dataroot = '/export/share/Datasets/COCO2017'

	rel_dname = 'coco-panoptic-133'
	rel_dataroot = '/export/share/Datasets/MSeg/mseg_dataset/COCOPanoptic'

	orig_id_to_classname_map = get_dataloader_id_to_classname_map(orig_dname)
	rel_id_to_classname_map = get_dataloader_id_to_classname_map(rel_dname)

	for split in ['train', 'val']:
		orig_split_txt_fpath = f'../mseg/dataset_lists/{orig_dname_lists}/list/{split}.txt'
		orig_pairs = generate_all_img_label_pair_fpaths(orig_dataroot, orig_split_txt_fpath)

		rel_split_txt_fpath = f'../mseg/dataset_lists/{rel_dname}/list/{split}.txt'
		rel_pairs = generate_all_img_label_pair_fpaths(rel_dataroot, rel_split_txt_fpath)

		for i in range(len(orig_pairs))[::100]:
			orig_pair = orig_pairs[i]
			orig_rgb_fpath, orig_label_fpath = orig_pair
			orig_rgb_img = imageio.imread(orig_rgb_fpath)
			orig_label_img = imageio.imread(orig_label_fpath)

			rel_pair = rel_pairs[i]
			rel_rgb_fpath, rel_label_fpath = rel_pair
			rel_rgb_img = imageio.imread(rel_rgb_fpath)
			rel_label_img = imageio.imread(rel_label_fpath)

			if not np.allclose(orig_label_img,rel_label_img):
				pdb.set_trace()

			fname_stem = Path(orig_rgb_fpath).stem
			orig_save_fpath = f'{save_dir}/{split}_{i}_noguide_orig_{fname_stem}.png'
			orig_guide_save_fpath = f'{save_dir}/{split}_{i}_guide_orig_{fname_stem}.png'

			rel_save_fpath = f'{save_dir}/{split}_{i}_noguide_rel_{fname_stem}.png'
			rel_guide_save_fpath = f'{save_dir}/{split}_{i}_guide_rel_{fname_stem}.png'

			if orig_rgb_img.ndim == 2:
				# this image was grayscale
				orig_rgb_img = grayscale_to_color(orig_rgb_img)
			
			if rel_rgb_img.ndim == 2:
				# this image was grayscale
				rel_rgb_img = grayscale_to_color(rel_rgb_img)

			#save_classnames_in_image(img_rgb, label_img, id_to_class_name_map, save_to_disk=True, save_fpath=save_fpath)
			form_mask_triple_embedded_classnames(orig_rgb_img, orig_label_img, orig_id_to_classname_map, orig_save_fpath, save_to_disk=True)
			save_mask_triple_with_color_guide(orig_rgb_img, orig_label_img, orig_id_to_classname_map,fname_stem,save_dir, orig_guide_save_fpath)

			form_mask_triple_embedded_classnames(rel_rgb_img, rel_label_img, rel_id_to_classname_map, rel_save_fpath, save_to_disk=True)
			save_mask_triple_with_color_guide(rel_rgb_img, rel_label_img, rel_id_to_classname_map,fname_stem,save_dir, rel_guide_save_fpath)
from tqdm import tqdm

from mseg.utils.names_utils import (get_classname_to_dataloaderid_map,
                                    get_dataloader_id_to_classname_map)
"""
AutoNUE labels and classes to store, read, and write annotations.
In their hierarchy, we use 'id-type'='id' (the most fine-grained).
Uses PIllow to raster json polygons.

Ref: 
https://github.com/AutoNUE/public-code/blob/master/preperation/createLabels.py
"""

name2labelid = get_classname_to_dataloaderid_map('idd-40',
                                                 include_ignore_idx_cls=False)
id2label = get_dataloader_id_to_classname_map('idd-40',
                                              include_ignore_idx_cls=False)

# A point in a polygon
Point = namedtuple('Point', ['x', 'y'])


# Class that contains the information of a single annotated object
class CsObject:
    # Constructor
    def __init__(self):
        # the label
        self.label = ""
        # the polygon as list of points
        self.polygon = []
        # the object ID
        self.id = -1