Exemple #1
0
def blobs2annList(blobs, categoryDict, batch):
    if "maskVoid" not in batch:
        maskVoid = None
    else:
        maskVoid = batch["maskVoid"]
    h,w = blobs.shape
    annList = []
    blobs = ms.t2n(blobs)

    for u in np.unique(blobs):
        if u == 0:
            continue
        binmask = (blobs == u)
        if maskVoid is not None:
            binmask = binmask * (ms.t2n(maskVoid).squeeze())

        seg = maskUtils.encode(np.asfortranarray(ms.t2n(binmask)).astype("uint8")) 
        seg["counts"] = seg["counts"].decode("utf-8")
        score = 1

        annList += [{"segmentation":seg,
              "iscrowd":0,
              "area":int(maskUtils.area(seg)),
             "image_id":batch["name"][0],
             "category_id":int(categoryDict[u]),
             "height":h,
             "width":w,
             "score":score}]

    return annList
Exemple #2
0
def blobs2BestDice(blobs, categoryDict, propDict, batch):
    h, w = blobs.shape
    annList = []
    blobs_copy = np.zeros(blobs.shape, int)

    if "maskVoid" in batch:
        maskVoid = batch["maskVoid"]
    else:
        maskVoid = None

    for u in np.unique(blobs):
        if u == 0:
            continue
        binmask = (blobs == u)
        best_dice = 0.
        best_mask = None
        for ann in propDict['propDict'][u-1]["annList"]:

            score = dice(ann["mask"], binmask)
            if score > best_dice:
                best_dice = score
                best_mask = ann["mask"]
                prop_score = ann["score"]

        if best_mask is None:
            best_mask = (blobs==u).astype(int)


        if maskVoid is not None:
            binmask = best_mask * (ms.t2n(maskVoid).squeeze())
        else:
            binmask = best_mask

        if best_mask is None:
            blobs_copy[blobs==u] = u 
        else:
            blobs_copy[best_mask==1] = u


        seg = maskUtils.encode(np.asfortranarray(ms.t2n(binmask)).astype("uint8")) 
        seg["counts"] = seg["counts"].decode("utf-8")
        score = best_dice

        # if batch["dataset"] == "coco2014":
        #     image_id = int(batch["name"][0])
        # else:
        image_id = batch["name"][0]

        annList += [{"segmentation":seg,
              "iscrowd":0,
              "area":int(maskUtils.area(seg)),
             "image_id":image_id,
             "category_id":int(categoryDict[u]),
             "height":h,
             "width":w,
             "score":score}]
        
    return {"blobs":blobs_copy, "annList":annList}
Exemple #3
0
def pointList2BestObjectness(pointList, batch, proposal_type="sharp"):
    if "single_point" in batch:
        single_point = True
    else:
        single_point = False

    propDict = pointList2propDict(pointList, batch, thresh=0.5, 
                                  single_point=single_point,
                                  proposal_type=proposal_type)
    
    
    h,w = propDict["background"].squeeze().shape
    blobs = np.zeros((h,w), int)
    categoryDict = {}
    if "maskVoid" in batch and batch["maskVoid"] is not None:
        maskVoid = ms.t2n(batch["maskVoid"].squeeze())
    else:
        maskVoid = None

    annList = []
    for i, prop in enumerate(propDict['propDict']):
        if len(prop["annList"]) == 0:
            continue
        blobs[prop["annList"][0]["mask"] !=0] = i+1

        
        categoryDict[i+1] = prop["category_id"]

        if maskVoid is not None:
            binmask = prop["annList"][0]["mask"] * (ms.t2n(maskVoid).squeeze())
        else:
            binmask = prop["annList"][0]["mask"]

        seg = maskUtils.encode(np.asfortranarray(ms.t2n(binmask)).astype("uint8")) 
        seg["counts"] = seg["counts"].decode("utf-8")
        score = prop["annList"][0]["score"]

        annList += [{"segmentation":seg,
              "iscrowd":0,
              "area":int(maskUtils.area(seg)),
             "image_id":batch["name"][0],
             "category_id":int(prop['category_id']),
             "height":h,
             "width":w,
             "score":score}]


    return {"annList":annList, "blobs": blobs, "categoryDict":categoryDict}
Exemple #4
0
def annList2BestDice(annList, batch):
    sharp_proposals = base_dataset.SharpProposals(batch)

  
    new_annList = []
    

    if "maskVoid" in batch and batch["maskVoid"] is not None:
        maskVoid = batch["maskVoid"]
    else:
        maskVoid = None

    for ann in annList:

        binmask = ann2mask(ann)["mask"]
        best_dice = 0.
        best_mask = None
        for sharp_ann in sharp_proposals:
            if ann["score"] < 0.5:
                continue
            score = dice(sharp_ann["mask"], binmask)
            if score > best_dice:
                best_dice = score
                best_mask = sharp_ann["mask"]
                # prop_score = ann["score"]

        if best_mask is None:
            best_mask = binmask

        if maskVoid is not None:
            binmask = best_mask * (ms.t2n(maskVoid).squeeze())
        else:
            binmask = best_mask

        seg = maskUtils.encode(np.asfortranarray(ms.t2n(binmask)).astype("uint8")) 
        seg["counts"] = seg["counts"].decode("utf-8")
        ann["score"] = best_dice
        ann["segmentation"] = seg

        new_annList += [ann]
    
    return {"annList":new_annList}
Exemple #5
0
def maskList2annList(maskList, categoryList, image_id):
    annList = []
    _, h, w = maskList.shape
    
    for i in range(maskList.shape[0]):
        binmask = maskList[i]

        seg = maskUtils.encode(np.asfortranarray(ms.t2n(binmask)).astype("uint8")) 
        seg["counts"] = seg["counts"].decode("utf-8")
        score = 1

        annList += [{"segmentation":seg,
              "iscrowd":0,
              "area":int(maskUtils.area(seg)),
             "image_id":image_id,
             "category_id":int(categoryList[i]),
             "height":h,
             "width":w,
             "score":score}]

    return annList
Exemple #6
0
def mask2ann(binmask, category_id, image_id, 
             height, width, maskVoid=None, score=None, point=None):
    binmask = binmask.squeeze().astype("uint8")

    if maskVoid is not None:
        binmask = binmask * maskVoid

    segmentation = maskUtils.encode(np.asfortranarray(ms.t2n(binmask)).astype("uint8")) 
    segmentation["counts"] = segmentation["counts"].decode("utf-8")
    # print(segmentation)
    ann = {"segmentation":segmentation,
                  "iscrowd":0,
                  "area":int(maskUtils.area(segmentation)),
                 "image_id":image_id,
                 "category_id":int(category_id),
                 "height":height,
                 "width":width,
                 "score":score,
                 "point":point}

    return ann
Exemple #7
0
def load_gtAnnDict(main_dict, reset=None):
    reset = None
    _, val_set = load_trainval(main_dict)
    annList_path = val_set.annList_path

    if os.path.exists(annList_path) and reset != "reset":
        return ms.load_json(annList_path)

    else:        
        ann_json = {}
        ann_json["categories"] = val_set.categories
        ann_json["type"] = "instances"


        # Images
        imageList = []
        annList = []
        id = 1

        for i in range(len(val_set)):
            print("{}/{}".format(i, len(val_set)))
            batch = val_set[i]

            image_id = batch["name"]

            height, width = batch["images"].shape[-2:]
            imageList += [{"file_name":batch["name"],
                          "height":height,
                          "width":width,
                          "id":batch["name"]}]

            maskObjects = batch["maskObjects"]
            maskClasses = batch["maskClasses"]
            n_objects = maskObjects[maskObjects!=255].max().item()
            
            for obj_id in range(1, n_objects+1):
                if obj_id == 0:
                    continue

                binmask = (maskObjects == obj_id)
                segmentation = maskUtils.encode(np.asfortranarray(ms.t2n(binmask))) 
                segmentation["counts"] = segmentation["counts"].decode("utf-8")
                uniques = (binmask.long()*maskClasses).unique()
                uniques = uniques[uniques!=0]
                assert len(uniques) == 1

                category_id = uniques[0].item()
                
                annList += [{"segmentation":segmentation,
                              "iscrowd":0,
                              # "bbox":maskUtils.toBbox(segmentation).tolist(),
                              "area":int(maskUtils.area(segmentation)),
                              "id":id,
                             "image_id":image_id,
                             "category_id":category_id}]
                id += 1

        ann_json["annotations"] = annList
        ann_json["images"] = imageList

        ms.save_json(annList_path, ann_json)

        # Save dummy results
        anns = ms.load_json(annList_path)
        fname_dummy = annList_path.replace(".json","_best.json")
        annList = anns["annotations"]
        for a in annList:
            a["score"] = 1

        ms.save_json(fname_dummy, annList)