def dump_class_masks(self, required_class_names: List[str],
                         highlight_classname: str, condition,
                         folder_prefix: str):
        """ Get for all splits, at once.

			Args:
			-	required_class_names
			-	highlight_classname: class to highlight in pink
			-	condition
			-	folder_prefix

			Returns:
			-	None
		"""
        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 i, rgb_fpath in enumerate(rgb_fpaths):
                print(f'On {i}/{len(rgb_fpaths)-1}')
                fname_stem = Path(rgb_fpath).stem
                assert rgb_fpath == self.fname_to_rgbfpath_dict[fname_stem]

                rgb_img, label_img = self.get_img_pair(fname_stem)

                present_class_idxs = np.unique(label_img)
                present_classnames = [
                    self.id_to_classname_map[idx] for idx in present_class_idxs
                ]

                if not all([
                        req_name in present_classnames
                        for req_name in required_class_names
                ]):
                    continue

                seg_rgb_fpath = self.fname_to_segrgbfpath_dict[fname_stem]
                instance_masks, instance_ids = get_ade20k_instance_label_masks(
                    seg_rgb_fpath, rgb_img)
                for (instance_mask,
                     instance_id) in zip(instance_masks, instance_ids):

                    # test the instance's class
                    label_votes, majority_vote = get_instance_mask_class_votes(
                        instance_mask, label_img)
                    if label_votes.size < MIN_REQ_PX:
                        continue

                    instance_classname = self.id_to_classname_map[
                        majority_vote]
                    if instance_classname != highlight_classname:  # not in required_class_names:
                        continue

                    save_fname = f'{fname_stem}_{instance_id}.png'
                    save_fpath = f'temp_files/{folder_prefix}_{split}_2019_12_16/{save_fname}'
                    save_binary_mask_double(rgb_img,
                                            instance_mask,
                                            save_fpath,
                                            save_to_disk=True)
Ejemplo n.º 2
0
	def get_class_masks(
		self, 
		required_class_names: List[str], 
		highlight_classname: str, 
		condition, 
		folder_prefix: str):
		""" """
		for split in ['train', 'val']:
			rgb_fpaths = glob.glob(f'{self.img_dir}/{split}/*.jpg')

			num_split_imgs = len(rgb_fpaths)
			for i, rgb_fpath in enumerate(rgb_fpaths):
				print(f'On image {i}/{num_split_imgs-1}')
				fname_stem = Path(rgb_fpath).stem
				rgb_img, label_img = self.get_img_pair(fname_stem, split)

				present_classnames = get_present_classes_in_img(label_img, self.id_to_classname_map)
				if not all([req_name in present_classnames for req_name in required_class_names]):
					continue

				fname_stem = Path(rgb_fpath).stem
				# save_fpath = f'temp_files/bdd_vis_min400px/{fname_stem}.png'
				# form_mask_triple_embedded_classnames(rgb_img, label_img, self.id_to_classname_map, save_fpath, save_to_disk=True)
				# filename = Path(label_img_fpath).name

				for class_idx in np.unique(label_img):
					instance_classname = self.id_to_classname_map[class_idx]
					if instance_classname != highlight_classname: # not in required_class_names:
						continue

					label_mask = (label_img == class_idx).astype(np.uint8)
					save_fpath = str(ROOT / f'temp_files/{folder_prefix}_{split}/{fname_stem}_{class_idx}.jpg')
					save_binary_mask_double(rgb_img, label_mask, save_fpath, save_to_disk=True)
    def dump_class_masks(self, required_class_names: List[str],
                         highlight_classname: str, condition: str,
                         folder_prefix: str):
        """
			Write out all requested COCO masks to disk. Get for all splits, at once.
			If person-car combinations are desired, this tuple may be specified
			as a requirement.

			Args:
			-	required_class_names:
			-	highlight_classname:
			-	condition:
			-	folder_prefix:

			Returns:
			-	None
		"""
        for split in ['train', 'val']:
            instance_img_fpaths = self.instance_api.get_instance_img_fpaths(
                split)
            num_imgs = len(instance_img_fpaths)

            for i, instance_img_fpath in enumerate(instance_img_fpaths):
                print(f'On image {i} of {num_imgs-1}')

                fname_stem = Path(instance_img_fpath).stem
                rgb_img = self.get_rgb_img(split, fname_stem)
                instance_id_img = self.instance_api.get_instance_id_img(
                    split, fname_stem)
                segmentid_to_class_name_map = {0: 'unlabeled'}

                present_classnames = self.semantic_api.get_present_classes_in_img(
                    split, fname_stem)
                print(present_classnames)
                if not all([
                        req_name in present_classnames
                        for req_name in required_class_names
                ]):
                    continue

                img_annot = self.semantic_api.get_img_annotation(
                    split, fname_stem)
                for segment in img_annot['segments_info']:
                    segmentid = segment['id']
                    categoryid = segment['category_id']
                    instance_classname = self.categoryid_to_classname_map[
                        categoryid]
                    segmentid_to_class_name_map[segmentid] = instance_classname

                    if instance_classname != highlight_classname:
                        continue

                    label_mask = (instance_id_img == segmentid).astype(
                        np.uint8)
                    save_fpath = f'temp_files/{folder_prefix}_{split}/{fname_stem}_{segmentid}.png'
                    save_binary_mask_double(rgb_img,
                                            label_mask,
                                            save_fpath,
                                            save_to_disk=True)
Ejemplo n.º 4
0
    def write_class_masks(self, required_class_names: List[str],
                          highlight_classname: str, condition: str,
                          folder_prefix: str):
        """
		Each path resembles:
		gt_fpath = '/export/work/johnlamb/MTURK_IDD_COPY/IDD_Segmentation/gtFine/val/119/638495_gtFine_polygons.json'

		"""
        for split in ['train', 'val']:
            gt_fpaths = glob.glob(f'{self.dataroot}/gtFine/{split}/**/*.json',
                                  recursive=True)
            num_split_imgs = len(gt_fpaths)
            for i, gt_fpath in enumerate(gt_fpaths):
                print(f'On image {i} / {num_split_imgs-1}')

                img_gt_dict = read_json_file(gt_fpath)
                img_gt_objs = img_gt_dict['objects']
                img_h = img_gt_dict['imgHeight']
                img_w = img_gt_dict['imgWidth']

                present_classnames = [
                    gt_obj['label'] for gt_obj in img_gt_objs
                ]
                if highlight_classname not in present_classnames:
                    print(f'\tSkip {i}')
                    continue

                seq_id = Path(gt_fpath).parts[-2]
                fname_stem = Path(gt_fpath).stem
                rgb_img = self.get_rgb_img(seq_id, fname_stem, split)

                segment_ids = set()
                for obj_idx, gt_obj in enumerate(img_gt_objs):

                    if 'id' in gt_obj.keys():
                        segment_id = gt_obj['id']
                    else:
                        segment_id = obj_idx

                    # each segment ID must be unique for this image.
                    assert segment_id not in segment_ids
                    segment_ids.add(segment_id)
                    obj_label = gt_obj['label']
                    if obj_label == highlight_classname:
                        obj_poly = gt_obj['polygon']
                        # first arg is tuple_verts: Iteratable with elements of size 2 (list of 2-tuples, or Nx2 numpy array)
                        obj_mask = get_mask_from_polygon(
                            obj_poly, img_h, img_w)
                        obj_mask = obj_mask.astype(np.uint8)
                        # not adding {obj_idx} to filename path now.
                        save_fpath = f'temp_files/{folder_prefix}_{split}_2020_03_10/seq{seq_id}_{fname_stem}_{segment_id}.jpg'
                        save_binary_mask_double(rgb_img,
                                                obj_mask,
                                                save_fpath,
                                                save_to_disk=True)
Ejemplo n.º 5
0
def dump_img_masks(highlight_classname: str, split: str, rgb_img_fpath: str,
                   folder_prefix: str, api: MapillaryMaskDataset):
    """
	"""
    # print(f'On image {i}/{len(rgb_img_fpaths)}')
    rgb_img = cv2_imread_rgb(rgb_img_fpath)
    fname_stem = Path(rgb_img_fpath).stem
    label_rgb_fpath = f'{api.dataroot}/{split}/labels/{fname_stem}.png'
    label_img_rgb = cv2_imread_rgb(label_rgb_fpath)
    label_img = api.labelrgb_to_label(label_img_rgb)

    present_classnames = [
        api.id_to_classname_map[id] for id in np.unique(label_img)
    ]
    if highlight_classname not in present_classnames:
        return

    instance_img_fpath = f'{api.dataroot}/{split}/instances/{fname_stem}.png'
    instance_img = imageio.imread(instance_img_fpath)
    instance_ids = np.unique(instance_img)

    for instance_id in instance_ids:
        instance_mask = (instance_img == instance_id).astype(np.uint8)

        is_single_class, sem_class_ids = mask_belongs_single_semantic_class(
            instance_mask, label_img)
        assert is_single_class
        instance_classid = int(sem_class_ids)

        instance_classname = api.id_to_classname_map[int(sem_class_ids)]
        if instance_classname != highlight_classname:  # not in required_class_names:
            continue

        save_fpath = f'temp_files/{folder_prefix}_{split}_2020_04_18/mapillary_{fname_stem}_{instance_id}.jpg'
        save_binary_mask_double(rgb_img,
                                instance_mask,
                                save_fpath,
                                save_to_disk=True)