def bitmask2coco_ins_seg(mask_base: str, config: Config, nproc: int = NPROC) -> GtType: """Converting BDD100K Instance Segmentation Set to COCO format.""" files = list_files(mask_base, suffix=".png") images: List[ImgType] = [] logger.info("Collecting bitmasks...") image_id = 0 for file_ in tqdm(files): image_id += 1 image = ImgType( id=image_id, file_name=file_.replace(".png", ".jpg"), ) images.append(image) annotations = bitmask2coco_wo_ids_parallel(mask_base, images, nproc) return GtType( type="instances", categories=get_coco_categories(config), images=images, annotations=annotations, )
def bitmask2coco_pan_seg( mask_base: str, pan_mask_base: str, nproc: int = NPROC, ) -> PanopticGtType: """Converting BDD100K Instance Segmentation Set to COCO format.""" files = list_files(mask_base, suffix=".png") images: List[ImgType] = [] logger.info("Collecting bitmasks...") image_id = 0 for file_ in tqdm(files): image_id += 1 image = ImgType(id=image_id, file_name=file_) images.append(image) annotations = bitmask2panseg_parallel(mask_base, pan_mask_base, images, nproc) categories: List[PanopticCatType] = [ PanopticCatType( id=label.id, name=label.name, supercategory=label.category, isthing=label.hasInstances, color=label.color, ) for label in labels ] return PanopticGtType( categories=categories, images=images, annotations=annotations, )
def test_bitmask2pan_json(self) -> None: """Check bitmask to panoptic json file.""" mask_name = f"{self.cur_dir}/testcases/panseg_bdd100k/{MASK_NAME}" gt_json_name = f"{self.cur_dir}/testcases/panseg_coco/panseg_coco.json" image = ImgType(id=255, file_name="panseg_mask.jpg") pan_ann = bitmask2pan_json(image, mask_name) with open(gt_json_name, encoding="utf-8") as fp: gt_pan_ann = json.load(fp) self.assertDictEqual(dict(pan_ann), gt_pan_ann)
def bitmask2coco_seg_track( mask_base: str, shape: Tuple[int, int], all_files: List[str], categories: List[CatType], mask_mode: str = "rle", nproc: int = 4, ) -> GtType: """Converting BDD100K Instance Segmentation Set to COCO format.""" files_list = group_and_sort_files(all_files) videos: List[VidType] = [] images: List[ImgType] = [] image_ids: List[int] = [] mask_names: List[str] = [] logger.info("Collecting bitmasks...") video_id, image_id = 0, 0 for files in files_list: video_name = os.path.split(files[0])[0] video_id += 1 video = VidType(id=video_id, name=video_name) videos.append(video) for frame_id, file_ in tqdm(enumerate(files)): image_id += 1 image = ImgType( video_id=video_id, frame_id=frame_id, id=image_id, file_name=file_.replace(".png", ".jpg"), height=shape[0], width=shape[1], ) images.append(image) image_ids.append(image_id) mask_name = os.path.join(mask_base, file_.replace(".jpg", ".png")) mask_names.append(mask_name) annotations = bitmask2coco_wo_ids_parallel(image_ids, mask_names, mask_mode, nproc) return GtType( type="instances", categories=categories, videos=videos, images=images, annotations=annotations, )
def bitmask2coco_ins_seg( mask_base: str, shape: Tuple[int, int], files: List[str], categories: List[CatType], mask_mode: str = "rle", nproc: int = 4, ) -> GtType: """Converting BDD100K Instance Segmentation Set to COCO format.""" images: List[ImgType] = [] image_ids: List[int] = [] mask_names: List[str] = [] logger.info("Collecting bitmasks...") image_id = 0 for file_ in tqdm(files): image_id += 1 image = ImgType( id=image_id, file_name=file_, height=shape[0], width=shape[1], ) images.append(image) image_ids.append(image_id) mask_name = os.path.join(mask_base, file_) mask_names.append(mask_name) annotations = bitmask2coco_wo_ids_parallel(image_ids, mask_names, mask_mode, nproc) return GtType( type="instances", categories=categories, images=images, annotations=annotations, )
def bitmask2coco_seg_track(mask_base: str, config: Config, nproc: int = NPROC) -> GtType: """Converting BDD100K Instance Segmentation Set to COCO format.""" videos: List[VidType] = [] images: List[ImgType] = [] all_files = list_files(mask_base, suffix=".png") files_list = group_and_sort_files(all_files) logger.info("Collecting bitmasks...") video_id, image_id = 0, 0 for files in files_list: video_name = os.path.split(files[0])[0] video_id += 1 video = VidType(id=video_id, name=video_name) videos.append(video) for frame_id, file_ in tqdm(enumerate(files)): image_id += 1 image = ImgType( video_id=video_id, frame_id=frame_id, id=image_id, file_name=file_.replace(".png", ".jpg"), ) images.append(image) annotations = bitmask2coco_wo_ids_parallel(mask_base, images, nproc) return GtType( type="instances", categories=get_coco_categories(config), videos=videos, images=images, annotations=annotations, )
def bdd100k2coco_seg_track( mask_base: str, shape: Tuple[int, int], frames: List[Frame], categories: List[CatType], name_mapping: Optional[Dict[str, str]] = None, ignore_mapping: Optional[Dict[str, str]] = None, ignore_as_class: bool = False, remove_ignore: bool = False, mask_mode: str = "rle", nproc: int = 4, ) -> GtType: """Converting BDD100K Segmentation Tracking Set to COCO format.""" frames_list = group_and_sort(frames) videos: List[VidType] = [] images: List[ImgType] = [] video_id, image_id, ann_id = 0, 0, 0 mask_names: List[str] = [] category_ids_list: List[List[int]] = [] instance_ids_list: List[List[int]] = [] annotations_list: List[List[AnnType]] = [] for video_anns in tqdm(frames_list): global_instance_id: int = 1 instance_id_maps: Dict[str, int] = dict() video_name = video_anns[0].video_name video_id += 1 video = VidType(id=video_id, name=video_name) videos.append(video) for image_anns in video_anns: image_id += 1 image = ImgType( video_id=video_id, frame_id=image_anns.frame_index, id=image_id, file_name=os.path.join(video_name, image_anns.name), height=shape[0], width=shape[1], ) images.append(image) mask_name = os.path.join( mask_base, video_name, image_anns.name.replace(".jpg", ".png"), ) mask_names.append(mask_name) category_ids: List[int] = [] instance_ids: List[int] = [] annotations: List[AnnType] = [] for label in image_anns.labels: if label.poly_2d is None: continue category_ignored, category_id = process_category( label.category, categories, name_mapping, ignore_mapping, ignore_as_class=ignore_as_class, ) if remove_ignore and category_ignored: continue scalabel_id = str(label.id) instance_id, global_instance_id = get_bdd100k_instance_id( instance_id_maps, global_instance_id, scalabel_id) ann_id += 1 iscrowd, ignore = get_bdd100k_object_attributes( label, category_ignored) annotation = AnnType( id=ann_id, image_id=image_id, category_id=category_id, instance_id=instance_id, scalabel_id=scalabel_id, iscrowd=iscrowd, ignore=ignore, ) category_ids.append(category_id) instance_ids.append(instance_id) annotations.append(annotation) category_ids_list.append(category_ids) instance_ids_list.append(instance_ids) annotations_list.append(annotations) annotations = bitmask2coco_with_ids_parallel( annotations_list, mask_names, category_ids_list, instance_ids_list, mask_mode, nproc, ) return GtType( type="instances", categories=categories, videos=videos, images=images, annotations=annotations, )
def bdd100k2coco_ins_seg( mask_base: str, shape: Tuple[int, int], frames: List[Frame], categories: List[CatType], name_mapping: Optional[Dict[str, str]] = None, ignore_mapping: Optional[Dict[str, str]] = None, ignore_as_class: bool = False, remove_ignore: bool = False, mask_mode: str = "rle", nproc: int = 4, ) -> GtType: """Converting BDD100K Instance Segmentation Set to COCO format.""" image_id, ann_id = 0, 0 images: List[ImgType] = [] mask_names: List[str] = [] category_ids_list: List[List[int]] = [] instance_ids_list: List[List[int]] = [] annotations_list: List[List[AnnType]] = [] logger.info("Collecting annotations...") for image_anns in tqdm(frames): instance_id = 0 image_id += 1 image = ImgType( id=image_id, file_name=image_anns.name, height=shape[0], width=shape[1], ) images.append(image) mask_name = os.path.join( mask_base, image_anns.name.replace(".jpg", ".png"), ) mask_names.append(mask_name) category_ids: List[int] = [] instance_ids: List[int] = [] annotations: List[AnnType] = [] for label in image_anns.labels: if label.poly_2d is None: continue category_ignored, category_id = process_category( label.category, categories, name_mapping, ignore_mapping, ignore_as_class=ignore_as_class, ) if category_ignored and remove_ignore: continue ann_id += 1 instance_id += 1 iscrowd, ignore = get_bdd100k_object_attributes( label, category_ignored) annotation = AnnType( id=ann_id, image_id=image_id, category_id=category_id, scalabel_id=str(label.id), iscrowd=iscrowd, ignore=ignore, ) category_ids.append(category_id) instance_ids.append(instance_id) annotations.append(annotation) category_ids_list.append(category_ids) instance_ids_list.append(instance_ids) annotations_list.append(annotations) annotations = bitmask2coco_with_ids_parallel( annotations_list, mask_names, category_ids_list, instance_ids_list, mask_mode, nproc, ) return GtType( type="instances", categories=categories, images=images, annotations=annotations, )
def bdd100k2coco_seg_track(mask_base: str, frames: List[Frame], config: Config, nproc: int = NPROC) -> GtType: """Converting BDD100K Segmentation Tracking Set to COCO format.""" video_id, image_id, ann_id = 0, 0, 0 img_shape = config.imageSize frames_list = group_and_sort(frames) videos: List[VidType] = [] images: List[ImgType] = [] mask_names: List[str] = [] category_ids_list: List[List[int]] = [] instance_ids_list: List[List[int]] = [] annotations_list: List[List[AnnType]] = [] categories = get_leaf_categories(config.categories) cat_name2id = {cat.name: i + 1 for i, cat in enumerate(categories)} logger.info("Collecting annotations...") for video_anns in tqdm(frames_list): global_instance_id: int = 1 instance_id_maps: Dict[str, int] = {} video_name = video_anns[0].videoName video_id += 1 video = VidType(id=video_id, name=video_name) videos.append(video) for image_anns in video_anns: image_id += 1 if img_shape is None: if image_anns.size is not None: img_shape = image_anns.size else: raise ValueError("Image shape not defined!") image = ImgType( video_id=video_id, frame_id=image_anns.frameIndex, id=image_id, file_name=os.path.join(video_name, image_anns.name), height=img_shape.height, width=img_shape.width, ) if image_anns.url is not None: image["coco_url"] = image_anns.url images.append(image) mask_name = os.path.join( mask_base, video_name, # Bitmask in .png format, image in .jpg format image_anns.name.replace(".jpg", ".png"), ) mask_names.append(mask_name) category_ids: List[int] = [] instance_ids: List[int] = [] annotations: List[AnnType] = [] for label in image_anns.labels: if label.poly2d is None: continue if label.category not in cat_name2id: continue ann_id += 1 instance_id, global_instance_id = get_bdd100k_instance_id( instance_id_maps, global_instance_id, label.id) category_id = cat_name2id[label.category] annotation = AnnType( id=ann_id, image_id=image_id, instance_id=instance_id, category_id=category_id, scalabel_id=label.id, iscrowd=int(check_crowd(label) or check_ignored(label)), ignore=0, ) category_ids.append(category_id) instance_ids.append(instance_id) annotations.append(annotation) category_ids_list.append(category_ids) instance_ids_list.append(instance_ids) annotations_list.append(annotations) annotations = bitmask2coco_with_ids_parallel( annotations_list, mask_names, category_ids_list, instance_ids_list, nproc, ) return GtType( type="instances", categories=get_coco_categories(config), videos=videos, images=images, annotations=annotations, )