Example #1
0
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,
    )
Example #2
0
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,
    )
Example #3
0
def evaluate_ins_seg(
    gt_paths: List[str],
    pred_paths: List[str],
    pred_score_file: str,
    config: Config,
    nproc: int = NPROC,
    with_logs: bool = True,
) -> DetResult:
    """Load the ground truth and prediction results.

    Args:
        gt_paths: paths to the ground truth bitmasks.
        pred_paths: paths to the prediciton bitmasks.
        pred_score_file: path tothe prediction scores.
        config: Config instance.
        nproc: number of processes.
        with_logs: whether to print logs

    Returns:
        dict: detection metric scores
    """
    categories = get_coco_categories(config)
    cat_ids = [category["id"] for category in categories]
    cat_names = [category["name"] for category in categories]
    pred_paths = reorder_preds(gt_paths, pred_paths)
    bdd_eval = BDD100KInsSegEval(gt_paths, pred_paths, pred_score_file,
                                 cat_names, nproc)
    bdd_eval.params.catIds = cat_ids
    if with_logs:
        logger.info("evaluating...")
    bdd_eval.evaluate()
    if with_logs:
        logger.info("accumulating...")
    bdd_eval.accumulate()
    result = bdd_eval.summarize()  # pylint: disable=redefined-outer-name
    return result
Example #4
0
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,
    )