def __init__(self, coco_dataroot: str) -> None:
     """ """
     self.categoryid_to_classname_map = get_dataloader_id_to_classname_map(
         dataset_name='coco-panoptic-201')
     self.coco_dataroot = coco_dataroot
     self.semantic_api = COCOSemanticAPI(coco_dataroot)
     self.instance_api = COCOInstanceAPI(coco_dataroot)
def test_get_instance_img_fpaths():
    """ """
    coco_dataroot = f'{_TEST_DIR}/test_data/COCOPanoptic_test_data'
    c_api = COCOInstanceAPI(coco_dataroot)
    split = 'val'
    fpaths = c_api.get_instance_img_fpaths(split)
    assert len(fpaths) == 1
    assert Path(fpaths[0]).name == '000000000139.png'
def test_get_instance_id_img():
    """ """
    coco_dataroot = f'{_TEST_DIR}/test_data/COCOPanoptic_test_data'
    c_api = COCOInstanceAPI(coco_dataroot)
    split = 'train'
    fname_stem = '000000000009'
    instance_id_img = c_api.get_instance_id_img(split, fname_stem)

    assert np.amax(instance_id_img) == 8922372
    assert np.amin(instance_id_img) == 0
    assert np.sum(instance_id_img) == 1451563332418
Beispiel #4
0
	def __init__(self): #, coco_dataroot):
		"""
			We will remap the instance images to semantic images, using 
			semantic JSON data. Data will be dumped in 201-class taxonomy.
		"""
		self.orig_dname = 'coco-panoptic-inst-201'
		self.orig_dataroot = infos[self.orig_dname].dataroot
		self.instance_api = COCOInstanceAPI(self.orig_dataroot)
		self.semantic_api = COCOSemanticAPI(self.orig_dataroot)
def test_constructor():
    """ """
    coco_dataroot = f'{_TEST_DIR}/test_data/COCOPanoptic_test_data'
    c_api = COCOInstanceAPI(coco_dataroot)

    assert len(c_api.instance_img_fpaths_splitdict['train']) == 1
    assert len(c_api.instance_img_fpaths_splitdict['val']) == 1
    assert Path(c_api.instance_img_fpaths_splitdict['train']
                [0]).name == '000000000009.png'
    assert Path(c_api.instance_img_fpaths_splitdict['val']
                [0]).name == '000000000139.png'

    assert len(c_api.fname_to_instanceimgfpath_dict.keys()) == 2
    assert Path(c_api.fname_to_instanceimgfpath_dict['000000000009']
                ).name == '000000000009.png'
    assert Path(c_api.fname_to_instanceimgfpath_dict['000000000139']
                ).name == '000000000139.png'
class COCOPanopticJsonMaskDataset:
    """
	Simple API to interact with instance masks of COCO Panoptic dataset.

	We take the instance ID images from the Panoptic .png files, and we take
	the semantic class images from the Panoptic .json files
	"""
    def __init__(self, coco_dataroot: str) -> None:
        """ """
        self.categoryid_to_classname_map = get_dataloader_id_to_classname_map(
            dataset_name='coco-panoptic-201')
        self.coco_dataroot = coco_dataroot
        self.semantic_api = COCOSemanticAPI(coco_dataroot)
        self.instance_api = COCOInstanceAPI(coco_dataroot)

    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)

    def get_rgb_img(self, split: str, fname_stem: str) -> np.ndarray:
        """
			Args:
			-	split: string representing training, validation, or testing split of the data
			-	fname_stem: 

			Returns:
			-	rgb_img: color image.
		"""
        rgb_img_fpath = f'{self.coco_dataroot}/{split}2017/{fname_stem}.jpg'
        rgb_img = imageio.imread(rgb_img_fpath)
        if rgb_img.ndim == 2:
            # this image was grayscale
            rgb_img = grayscale_to_color(rgb_img)
        return rgb_img

    def get_segment_mask(self, seq_id: None, segmentid: int, fname_stem: str,
                         split: str):
        """
		Use semantic and instance APIs to identify 

			Args:
			-	segmentid:
			-	fname_stem:
			-	split:

			Returns:
			-	segment_mask:
		"""
        ids = self.instance_api.get_instance_id_img(split, fname_stem)
        img_annot = self.semantic_api.get_img_annotation(split, fname_stem)

        for segment in img_annot['segments_info']:
            if segmentid == segment['id']:
                categoryid = segment['category_id']
                segment_mask = (ids == segmentid).astype(np.uint8)

        return segment_mask