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
def get_points(self, batch): from skimage.segmentation import mark_boundaries if 1: img = ms.f2l(ms.t2n((ms.denormalize(batch["images"])))).squeeze() pred_dict = self(batch["images"].cuda()) h, w = batch["images"].shape[-2:] mask = ms.t2n( F.interpolate(pred_dict["cam"][None][None], size=(h, w))).squeeze() segments_slic = slic(img, n_segments=1000, compactness=5, sigma=0.5) labels = np.unique(segments_slic) scoreList = np.zeros(len(labels)) for i, l in enumerate(labels): scoreList[i] = (mask * (segments_slic == l)).mean() selected = np.argsort(scoreList)[-batch["counts"].item():] points = np.zeros(segments_slic.shape) for s in selected: ind = segments_slic == s max_val = mask[ind].max() points[mask == max_val] = 1 return points
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}
def probs2blobs(probs): annList = [] probs = ms.t2n(probs) n, n_classes, h, w = probs.shape counts = np.zeros((n, n_classes-1)) # Binary case pred_mask = ms.t2n(probs.argmax(1)) blobs = np.zeros(pred_mask.shape) points = np.zeros(pred_mask.shape) max_id = 0 for i in range(n): for category_id in np.unique(pred_mask[i]): if category_id == 0: continue ind = pred_mask==category_id connected_components = morph.label(ind) uniques = np.unique(connected_components) blobs[ind] = connected_components[ind] + max_id max_id = uniques.max() + max_id n_blobs = (uniques != 0).sum() counts[i, category_id-1] = n_blobs for j in range(1, n_blobs+1): binmask = connected_components == j blob_probs = probs[i, category_id] * binmask y, x = np.unravel_index(blob_probs.squeeze().argmax(), blob_probs.squeeze().shape) points[i, y, x] = category_id annList += [mask2ann(binmask, category_id, image_id=-1, height=binmask.shape[1], width=binmask.shape[2], maskVoid=None, score=None, point={"y":y,"x":x, "prob":float(blob_probs[blob_probs!=0].max()), "category_id":int(category_id)})] blobs = blobs.astype(int) # Get points return {"blobs":blobs, "annList":annList, "probs":probs, "counts":counts, "points":points, "pointList":mask2pointList(points)["pointList"], "pred_mask":pred_mask, "n_blobs":len(annList)}
def scoreBatch(self, model, batch): model.eval() preds = ms.t2n(model.predict(batch, metric="maskClasses")) maskClasses = ms.t2n(batch["maskClasses"]) cf = confusion_matrix(y_true=preds.ravel(), y_pred=maskClasses.ravel(), labels=np.arange(max(model.n_classes, 2))) return {"score": self._average(cf), "cf": cf}
def validation_phase_mIoU(history, main_dict, model, val_set, predict_name, epoch): category2name = lambda x: "person" if x == 1 else "car" iou_mean = {"person":0, "car":0} iou_sum = {"person":0, "car":0} n_objects = {"person":0, "car":0} n_images = len(val_set) for i in range(n_images): batch = ms.get_batch(val_set, [i]) maskObjects = batch["maskObjects"].squeeze() pred_dict = model.predict(batch, predict_method="BestDice") for ann in pred_dict["annList"]: point = ann["point"] category_id = point["category_id"] label = maskObjects[point["y"], point["x"]].item() assert label != 0 A = maskObjects == label B = au.ann2mask(ann)["mask"] iou_sum[category2name(category_id)] += au.compute_iou(ms.t2n(A), ms.t2n(B)) n_objects[category2name(category_id)] += 1 # ms.images(batch["images"], A, denorm=1, win="GT") # ms.images(batch["images"], B, denorm=1, win="Pred") for name in ["person", "car"]: iou_mean[name] = iou_sum[name] / max(1, n_objects[name]) print("{}/{} - {:.3f} {:.3f}".format(i, n_images, iou_mean["person"], iou_mean["car"])) # iou_mean = iou_sum / n_objects val_dict = {} val_dict["iou_mean"] = iou_mean val_dict["predict_name"] = predict_name val_dict["epoch"] = epoch val_dict["time"] = datetime.datetime.now().strftime("%b %d, 20%y") # Update history history["val"] += [val_dict] # Higher is better if (history["best_model"] == {} or history["best_model"]["iou_mean"]["person"] <= val_dict["iou_mean"]["person"]): history["best_model"] = val_dict ms.save_best_model(main_dict, model) return history
def scoreBatch(self, model, batch): model.eval() n = batch["images"].shape[0] # Predict pixel labels and get true labels pred = ms.t2n(model.predict( batch, predict_method="counts")["counts"]).ravel() true = ms.t2n(batch["counts"]).ravel() # COMPUTE SCORE se = MAE.compute_squared_error(true, pred) return {"score": self._average(se, n), "se": se, "n": n}
def scoreBatch(self, model, batch): model.eval() # Pred maskObjects, maskProbs = ms.t2n(model.predict(batch, "blobs_probs")) maskObjects = maskObjects.squeeze() maskProbs = maskProbs.squeeze() pred_masks, pred_labels, pred_scores = pred2masks( maskObjects, maskProbs) # Probs maskClasses = ms.t2n(batch["maskClasses"].squeeze()) maskObjects = ms.t2n(batch["maskObjects"].squeeze()) gt_masks, gt_labels = gt2mask(maskClasses, maskObjects) import ipdb ipdb.set_trace() # breakpoint 0fd24f32 // bestOverlapDict = np.zeros(self.n_classes) n_objectDict = np.zeros(self.n_classes) bestOverlap = 0 gt_labels = gt_labels[0] pred_labels = pred_labels[0] gt_masks = gt_masks[0] pred_masks = pred_masks[0] for i in range(len(gt_labels)): c = gt_labels[i] for j in range(len(pred_labels)): if pred_labels[j] != c: continue overlap = sf.dice(pred_masks[j], gt_masks[i], smooth=0.) if overlap > bestOverlap: bestOverlap = overlap bestOverlapDict[c - 1] += bestOverlap n_objectDict[c - 1] += 1 # COMPUTE SCORE return { "score": self._average(bestOverlapDict, n_objectDict), "bestOverlapDict": bestOverlapDict, "n_objectDict": n_objectDict }
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}
def predict(self, batch, predict_method="counts", **options): self.eval() n,c,h,w = batch["images"].shape x_input = batch["images"].cuda() O = self(x_input) if predict_method == "counts": return {"counts":ms.t2n(torch.sigmoid(O)>0.5)} if predict_method == "points": pred_dict = self.forward_test(x_input, class_threshold=0, peak_threshold = 30) return pred_dict["points"] elif predict_method == "BestDice": points = self.get_points(batch) pointList = au.mask2pointList(points[None])["pointList"] if len(pointList) == 0: return {"annList":[]} pred_dict = au.pointList2BestObjectness(pointList, batch) return {"annList":pred_dict["annList"]} else: return self.forward_test(x_input, class_threshold=0, peak_threshold = 30)
def compute_fixed_recursive_loss(model, batch, S_log): blob_dict = get_blob_dict(model.base_model, batch, training=True) blobs = blob_dict["blobs"] probs = ms.t2n(blob_dict["probs"]) point_loss = 0. for b in blob_dict["blobList"]: if b["n_points"] != 1: continue T = np.zeros(blobs.shape[-2:]) W = np.zeros(blobs.shape[-2:]) ind = blobs[b["class"]] == b["blob_id"] T[ind] = (b["class"] + 1) W[ind] = probs[b["class"] + 1][ind] # ms.images(probs[b["class"]+1]>0.5) b_loss = F.nll_loss( S_log, ms.n2l(T[None]), ignore_index=0, reduce=False) * torch.FloatTensor(W).cuda() point_loss += (b_loss.sum() / float(ind.sum())) return point_loss
def get_embedding_blobs(self, O, fg_bg_seeds): n, c, h, w = O.shape # seeds = torch.cat([fg_seeds, bg_seeds], 2)[:,:,None] fA = O.view(1, c, -1) fS = O[:, :, fg_bg_seeds["yList"], fg_bg_seeds["xList"]] n_pixels = h * w blobs = torch.zeros(h * w) n_seeds = fS.shape[-1] maximum = 5000000 n_loops = int(np.ceil((n_pixels * n_seeds) / maximum)) for (s, e) in get_batches(n_pixels, size=n_pixels // n_loops): # s,e = map(int, (s,e)) diff = log_pairwise(fS[:, :, None], fA[:, :, s:e, None]) blobs[s:e] = diff.max(2)[1] + 1 bg_min_index = np.where( np.array(fg_bg_seeds["categoryList"]) == 0)[0].min() assert len(fg_bg_seeds["yList"]) // 2 == bg_min_index blobs[blobs > int(bg_min_index)] = 0 blobs = blobs.squeeze().reshape(h, w).long() categoryDict = {} for i, category_id in enumerate(fg_bg_seeds["categoryList"]): if category_id == 0: continue categoryDict[i + 1] = category_id return {"blobs": ms.t2n(blobs), "categoryDict": categoryDict}
def compute_sp_loss(batch, S_log): points = batch["points"] point_loss = 0. segments = get_sp(batch) for c in np.unique(points): if c == 0: continue indices = ((ms.t2n(points)[0] != 0) * (segments)) sp_ind = np.unique(indices[indices != 0]) for sp_i in sp_ind: T = torch.from_numpy((segments == sp_i).astype(int))[None] T[T != 0] = int(c) point_loss += F.nll_loss(S_log, T.long().cuda(), ignore_index=0, reduction="elementwise_mean") # sp_ind_byte = torch.ByteTensor(np.isin(segments, sp_ind).astype(int))[None] # sp_points[sp_ind_byte] = float(c) return point_loss
def compute_split_loss(S_log, S, points, blob_dict, split_mode="line", return_mask=False): blobs = blob_dict["blobs"] S_numpy = ms.t2n(S[0]) points_npy = ms.t2n(points).squeeze() loss = 0. if return_mask: mask = np.ones(points_npy.shape) for b in blob_dict["blobList"]: if b["n_points"] < 2: continue c = b["class"] + 1 probs = S_numpy[b["class"] + 1] points_class = (points_npy == c).astype("int") blob_ind = blobs[b["class"]] == b["blob_id"] if split_mode == "line": T = sp.line_splits(probs * blob_ind, points_class * blob_ind) elif split_mode == "water": T = sp.watersplit(probs, points_class * blob_ind) * blob_ind T = 1 - T else: raise ValueError("%s LOL" % split_mode) if return_mask: mask[T == 0] = 0 # ms.images(T) scale = b["n_points"] + 1 loss += float(scale) * F.nll_loss(S_log, ms.n2l(T)[None], ignore_index=1, reduction="elementwise_mean") if return_mask: return (mask == 0) return loss
def pointList2UpperBoundMask(pointList, batch, proposal_type="sharp"): # propDict = pointList2propDict(pointList, batch, thresh=0.5) n, c = batch["counts"].shape n, _, h, w = batch["images"].shape n_objects = batch["maskObjects"].max() if "maskVoid" in batch: maskVoid = ms.t2n(batch["maskVoid"].squeeze()) else: maskVoid = None ########### annList = [] visited = set() for p_index, p in enumerate(pointList): category_id = p["category_id"] best_score = 0 best_mask = None gt_object_found = False cls_mask = (batch["maskClasses"] == category_id).long().squeeze() for k in range(n_objects): gt_mask = (batch["maskObjects"] == (k+1)).long().squeeze() if k in visited: continue if (gt_mask[p["y"],p["x"]].item() != 0 and cls_mask[p["y"],p["x"]].item()==1): gt_object_found = True visited.add(k) break if gt_object_found == False: continue gt_mask = ms.t2n(gt_mask) ann = mask2ann(gt_mask, p["category_id"], image_id=batch["name"][0], height=batch["images"].shape[2], width=batch["images"].shape[3], maskVoid=maskVoid, score=best_score) annList += [ann] return {"annList":annList}
def visualize(self, batch, **options): pred_dict = self(batch["images"].cuda()) h, w = batch["images"].shape[-2:] mask = ms.t2n(F.interpolate(pred_dict["cam"][None][None], size=(h, w))).squeeze() ms.images(ms.gray2cmap(mask), win="mask") ms.images(batch["images"], mask > 0.5, denorm=1)
def predict(self, batch, **options): self.eval() n, c, h, w = batch["images"].shape pred_dict = self(batch["images"].cuda()) return {"counts": ms.t2n(pred_dict["count"]).ravel()}
def visualize(self, batch, cam_index=None): cam = ms.resizeTo(self.forward_cam(batch["images"].cuda()),batch["images"]) preds = self.predict(batch, "counts") print(preds) if cam_index is None: cam_index = preds["indices"][0] image_points = ms.get_image(batch["images"], (batch["points"]==(cam_index+1)).long(), denorm=1, enlarge=1) ms.images(image_points[0], ms.gray2cmap(ms.t2n(cam[:,cam_index])))
def scoreBatch(self, model, batch, n_pos=None, map_scores=None, match=None): model.eval() # Pred maskObjects, maskProbs = ms.t2n( model.predict(batch, "blobs_probs", return_annList=False)) maskObjects = maskObjects.squeeze() maskProbs = maskProbs.squeeze() pred_masks, pred_labels, pred_scores = pred2masks( maskObjects, maskProbs) # Probs maskClasses = ms.t2n(batch["maskClasses"].squeeze()) maskObjects = ms.t2n(batch["maskObjects"].squeeze()) gt_masks, gt_labels = gt2mask(maskClasses, maskObjects) score_dict = mAP_eval.eval_instance_segmentation_voc( pred_masks, pred_labels, pred_scores, gt_masks, gt_labels, iou_thresh=self.thresh, use_07_metric=False, n_pos=n_pos, score=map_scores, match=match) # COMPUTE SCORE return { "score": score_dict["map"], "n_pos": score_dict['n_pos'], "map_scores": score_dict["score"], "match": score_dict["match"] }
def old_visSplit(model, batch, split_mode="water", win="split"): image = ms.denormalize(batch["images"]) image = ms.f2l(ms.t2n(image)).squeeze() probs = ms.t2n(model.predict(batch, "probs").squeeze()[1]) points =ms.t2n(batch["points"].squeeze()) # if "blobs" in split_mode: # # probs[probs < 0.5] = 0 # points[probs < 0.5] = 0 if split_mode == "water" or split_mode == "water_blobs": split_matrix = splits.watersplit(probs, points) elif split_mode == "line": split_matrix = 1 - splits.line_splits(probs, points) elif split_mode == "line_blobs": # eqwe blob_dict = l_helpers.get_blob_dict(model, batch) split_matrix = np.zeros(points.shape, int) for b in blob_dict["blobList"]: if b["n_points"] < 2: continue points_class = (points==(b["class"] + 1)).astype("int") blob_ind = blob_dict["blobs"][b["class"] ] == b["label"] # print((blob_ind).sum()) splitss = 1 - splits.line_splits(probs*blob_ind, points_class*blob_ind) split_matrix += splitss*blob_ind # if "blobs" in split_mode: # split_matrix[probs < 0.5] = 0 # print(split_matrix.shape) print(split_matrix.max()) split_img = ms.get_image(ms.l2f(image)[None], split_matrix.squeeze()[None].astype(int)) #print(image.shape) ms.images(split_img, points, enlarge=True, win=win)
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}
def get_backprop_guided_pixels(model, images, points, which=None): points_single, point_class = helpers.points2single(points, which) points_single = ms.t2n(points_single) mask = torch.from_numpy(ms.mask2hot(points_single, model.n_classes)).float() excited_mask = guided_backprop(model, images, gradient=mask.cuda()) return excited_mask, point_class
def compute_boundary_loss(S_log, S, points, counts, add_bg=False, return_mask=False): S_npy = ms.t2n(S[0]) points_npy = ms.t2n(points).squeeze() loss = 0. if return_mask: mask = np.ones(points_npy.shape) for c in range(S.shape[1]): if c == 0: continue points_class = (points_npy == c).astype(int) if add_bg: points_class[S_npy[c] == S_npy[c].min()] = 1 if points_class.sum() <= 1: continue T = sp.watersplit(S_npy[c], points_class) T = 1 - T if return_mask: mask[T == 0] = 0 scale = float(counts.sum()) loss += float(scale) * F.nll_loss(S_log, ms.n2l(T)[None], ignore_index=1, reduction="elementwise_mean") if return_mask: return (mask == 0) return loss
def scoreBatch(self, model, batch): model.eval() p_blobs = ms.t2n(model.predict(batch, metric="blobs")) labels = ms.t2n(batch["labels"]).astype(int) bd1_dict = calc_bd(p_blobs, labels) bd2_dict = calc_bd(labels, p_blobs) bd1 = bd1_dict["bd"] M1 = bd1_dict["M"] bd2 = bd2_dict["bd"] M2 = bd2_dict["M"] return { "score": self._average(bd1, M1, bd2, M2), "bd1": bd1, "bd2": bd2, "M1": M1, "M2": M2 }
def extract_proposalMasks(self, batch): self.eval() import ipdb; ipdb.set_trace() # breakpoint aa7e62ed // proposals = base_dataset.SharpProposals(batch) import ipdb; ipdb.set_trace() # breakpoint cd8bb791 // n,c,h,w = batch["images"].shape x_input = batch["images"].cuda() O = self(x_input) if predict_method == "counts": return {"counts":ms.t2n(torch.sigmoid(O)>0.5)} else: return self.forward_test(x_input, class_threshold=0, peak_threshold = 30)
def getitem(self, index): img_meta = self.meta[index] img_path = self.path + img_meta["img"] _img = Image.open(img_path).convert('RGB') # GET POINTS w, h = _img.size _img = _img.resize((int(w * self.ratio), int(h * self.ratio)), Image.BILINEAR) #_img = _img.resize((224, 224), Image.BILINEAR) w, h = _img.size points = np.zeros((h, w), np.uint8) for p in img_meta["pointList"]: points[int(p["y"] * h), int(p["x"] * w)] = 1 _points = transforms.functional.to_pil_image(points[:, :, np.newaxis]) """ iii, ppp = self.transform_function([_img, _points]); vis.images(np.array(iii), mask=np.array(ppp), enlarge=1) """ if self.transform_function is not None: _img, _points = self.transform_function([_img, _points]) # COMPUTE COUNTS counts = torch.from_numpy(np.array([_points.sum()])).long() # density = gaussian_filter(ms.t2n(_points).astype(float), sigma=8.0) # density = torch.from_numpy(density).float() # #### ADD DENSITY MAP # if self.density: #density = gaussian_filter(ms.t2n(_points).astype(float), sigma=8.0) density = gaussian_filter(ms.t2n(_points).astype(float), sigma=1.0) density = torch.from_numpy(density).float() return { "images": _img, "name": img_meta["img"], "points": _points, "counts": counts, "density": density, "index": index, "split": self.split }
def mask2pointList(mask): pointList = [] mask = ms.t2n(mask) pointInd = np.where(mask.squeeze()) n_points = pointInd[0].size for p in range(n_points): p_y, p_x = pointInd[0][p], pointInd[1][p] point_category = mask[0, p_y,p_x] pointList += [{"y":p_y,"x":p_x, "category_id":int(point_category), "shape":mask.shape}] return {"pointList":pointList}
def get_sp_points(batch): points = batch["points"] segments = get_sp(batch) sp_points = torch.zeros(points.shape) for c in np.unique(points): if c == 0: continue indices = ((ms.t2n(points)[0] != 0) * (segments)) sp_ind = np.unique(indices[indices != 0]) sp_ind_byte = torch.ByteTensor(np.isin(segments, sp_ind).astype(int))[None] sp_points[sp_ind_byte] = float(c) return sp_points
def get_mask_image(model, gm): image_path = "/mnt/datasets/public/issam/VOCdevkit/VOC2007/JPEGImages/" seg = gm["segmentation"] img_id = gm["image_id"] mask = maskUtils.decode(seg) img = ms.imread(image_path + "{:0>6}.jpg".format(img_id)) ms.images(img, mask) # model = get_style_model(name="udnie") # style1 = ms.t2n(style_content(model, img))[:,:, :mask.shape[0], :mask.shape[1]].squeeze() model = get_style_model(name="rain_princess") style2 = ms.t2n(style_content( model, img))[:, :, :mask.shape[0], :mask.shape[1]].squeeze() ms.images(style2 * mask[None] + ms.l2f(img) * (1 - mask[None]), win="324324") return img, mask
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