[docs]@Transform(in_keys=(K.boxes2d_classes,K.instance_masks),out_keys=K.seg_masks,)classConvertInstanceMaskToSegMask:"""Merge all instance masks into a single segmentation map."""
[docs]def__call__(self,classes_list:list[NDArrayI64],masks_list:list[NDArrayUI8])->list[NDArrayUI8]:"""Execute conversion. Args: classes_list (list[NDArrayI64]): List of Array of class ids, shape [N,]. masks_list (NDArrayU8): List of array of instance masks, shape [N, H, W]. Returns: list[NDArrayU8]: List of Segmentation mask, shape [H, W]. """seg_masks=[]forclasses,masksinzip(classes_list,masks_list):classes=np.asarray(classes,dtype=masks.dtype)target=np.max(masks*classes[:,None,None],axis=0)# discard overlapping instancestarget[np.sum(masks,axis=0)>1]=255seg_masks.append(target)returnseg_masks
[docs]@Transform(in_keys=K.boxes2d_classes,out_keys=K.boxes2d_classes,)classRemappingCategories:"""Remap classes using a mapping list."""def__init__(self,mapping:list[int]):"""Initialize remapping. Args: mapping (List[int]): List of class ids, such that classes will be mapped to its location in the list. """self.mapping=mapping
[docs]def__call__(self,classes_list:list[NDArrayI64])->list[NDArrayI64]:"""Execute remapping. Args: classes_list (list[NDArrayI64]): List of array of class ids, shape [N,]. Returns: list[NDArrayI64]: List of array of remapped class ids, shape [N,]. """fori,classesinenumerate(classes_list):forj,class_inenumerate(classes):classes_list[i][j]=self.mapping.index(class_)returnclasses_list