Beispiel #1
0
def compute_BD(A, B):
    bd = 0.
    for i in range(len(A)):
        gt_mask = maskUtils.decode(A[i]["segmentation"])
        max_dice = 0.
        for j in range(len(B)):
            pred_mask = maskUtils.decode(B[j]["segmentation"])
            dice = compute_dice(pred_mask, gt_mask)
            max_dice = max(max_dice, dice)

        bd += max_dice

    return bd / len(A)
Beispiel #2
0
def annList2mask(annList):
    n_anns = len(annList)
    if n_anns == 0:
        return {"mask": None}

    ann = annList[0]
    try:
        h, w = ann["mask"].shape
    except:
        h, w = ann["height"], ann["width"]
    mask = np.zeros((h, w), int)

    for i in range(n_anns):
        ann = annList[i]

        if "mask" in ann:
            ann_mask = ann["mask"]
        else:
            ann_mask = maskUtils.decode(ann["segmentation"])

        assert ann_mask.max() <= 1
        mask += ann_mask

    # mask[mask==1] = ann["category_id"]
    return {"mask": mask}
Beispiel #3
0
def annList2mask(annList, box=False, color=False):
    n_anns = len(annList)
    if n_anns == 0:
        return {"mask":None}

    ann = annList[0]
    try:
        h, w = ann["mask"].shape
    except:
        h, w = ann["height"], ann["width"]
    mask = np.zeros((h, w), int)

    for i in range(n_anns):
        ann = annList[i]

        if "mask" in ann:
            ann_mask = ann["mask"]
        else:
            ann_mask = maskUtils.decode(ann["segmentation"])

        assert ann_mask.max() <= 1

        if color:
            mask[ann_mask!=0] = i + 1
        else:
            mask += ann_mask

    for i in range(n_anns):
        if box:
            box_mask = ann2bbox(annList[i])["mask"]
            mask[box_mask!=0] = 255

    # mask[mask==1] = ann["category_id"]
    return {"mask":mask}
Beispiel #4
0
def annList2maskList(annList, box=False, color=False):
    n_anns = len(annList)
    if n_anns == 0:
        return {"mask":None}

    ann = annList[0]
    try:
        h, w = ann["mask"].shape
    except:
        h, w = ann["height"], ann["width"]
    maskList = np.zeros((h, w, n_anns), int)
    categoryList = np.zeros(n_anns, int)
    for i in range(n_anns):
        ann = annList[i]

        if "mask" in ann:
            ann_mask = ann["mask"]
        else:
            ann_mask = maskUtils.decode(ann["segmentation"])

        assert ann_mask.max() <= 1
        maskList[:,:,i] = ann_mask

        categoryList[i] = ann["category_id"]
    # mask[mask==1] = ann["category_id"]
    return {"maskList":maskList, "categoryList":categoryList}
Beispiel #5
0
def compute_coverage(A, B):
    mucov = 0.
    mwcov = 0.
    gt_size = 0.
    for i in range( len(A)):
        gt_mask = maskUtils.decode(A[i]["segmentation"])
        best_iou = 0.
        for j in range(len(B)):
            pred_mask = maskUtils.decode(B[j]["segmentation"])
            iou=compute_iou(pred_mask, gt_mask)
            best_iou = max(best_iou, iou)

        mucov += best_iou  
        mwcov += best_iou * gt_mask.sum()
        gt_size += gt_mask.sum()

    return (mucov /  len(A)), (mwcov  / gt_size), gt_size
Beispiel #6
0
    def __getitem__(self, i):
        encoded = self.proposals[i]["segmentation"]
        proposal_mask = maskUtils.decode(encoded)

        return {"mask": proposal_mask, "score": self.proposals[i]["score"]}
Beispiel #7
0
def ann2mask(ann):
    mask =  maskUtils.decode(ann["segmentation"])
    # mask[mask==1] = ann["category_id"]
    return {"mask":mask}
Beispiel #8
0
    def __getitem__(self, index):
        name_id = self.ids[index]
        name = self.image_names[index]

        image = np.array(
            Image.open(self.path +
                       "/{}/{}".format(self.split +
                                       self.year, name)).convert('RGB'))
        points = np.zeros(image.shape[:2], "uint8")[:, :, None]
        counts = np.zeros(80)
        maskVoid = np.zeros(points.shape[:2])
        annList = ms.load_pkl(
            self.path + "/groundtruth/{}_{}.pkl".format(self.split, name))

        h, w, _ = image.shape
        maskClasses = np.zeros((h, w), int)
        maskObjects = np.zeros((h, w), int)
        for obj_id, ann in enumerate(annList):
            mask = maskUtils.decode(COCO.annToRLE_issam(h, w, ann))
            if ann["iscrowd"]:
                maskVoid += mask
            else:

                dist = distance_transform_edt(mask)
                yx = np.unravel_index(dist.argmax(), dist.shape)

                label = self.category2label[int(ann["category_id"])]
                points[yx] = label
                counts[label - 1] += 1

                assert mask.max() <= 1
                mask_ind = mask == 1

                maskObjects[mask_ind] = obj_id + 1
                maskClasses[mask_ind] = label

        maskVoid = maskVoid.clip(0, 1)
        assert maskVoid.max() <= 1
        counts = torch.LongTensor(counts)

        image, points, maskObjects, maskClasses = self.transform_function(
            [image, points, maskObjects, maskClasses])

        # Sharp Proposals
        image_id = int(name[:-4].split("_")[-1])
        SharpProposals_name = self.proposals_path + "{}".format(image_id)
        lcfcn_pointList = self.get_lcfcn_pointList(str(image_id))
        assert image_id == name_id
        if self.split == "train":
            return {
                "images": image,
                "points": points.squeeze(),
                "SharpProposals_name": str(name_id),
                "counts": counts,
                "index": index,
                "name": str(name_id),
                "image_id": str(image_id),
                "maskObjects": maskObjects * 0,
                "maskClasses": maskClasses * 0,
                "proposals_path": self.proposals_path,
                "dataset": "coco2014",
                "lcfcn_pointList": lcfcn_pointList,
                "split": self.split
            }
        else:
            return {
                "images": image,
                "points": points.squeeze(),
                "SharpProposals_name": str(name_id),
                "counts": counts,
                "index": index,
                "name": str(name_id),
                "image_id": str(image_id),
                "maskObjects": maskObjects,
                "maskClasses": maskClasses,
                "proposals_path": self.proposals_path,
                "dataset": "coco2014",
                "lcfcn_pointList": lcfcn_pointList,
                "split": self.split
            }