[文档]@UNFORKABLE.register_module(OP_NAME)@TAGGING_OPS.register_module(OP_NAME)@OPERATORS.register_module(OP_NAME)@LOADED_IMAGES.register_module(OP_NAME)classImageSegmentMapper(Mapper):"""Perform segment-anything on images and return the bounding boxes."""_accelerator='cuda'
[文档]def__init__(self,imgsz=1024,conf=0.05,iou=0.5,model_path='FastSAM-x.pt',*args,**kwargs):""" Initialization method. :param imgsz: resolution for image resizing :param conf: confidence score threshold :param iou: IoU (Intersection over Union) score threshold :param model_path: the path to the FastSAM model. Model name should be one of ['FastSAM-x.pt', 'FastSAM-s.pt']. """kwargs.setdefault('mem_required','800MB')super().__init__(*args,**kwargs)self.imgsz=imgszself.conf=confself.iou=iouself.model_key=prepare_model(model_type='fastsam',model_path=model_path)
[文档]defprocess_single(self,sample,rank=None,context=False):# there is no image in this sampleifself.image_keynotinsampleornotsample[self.image_key]:# N x M x 4 for N images, M boxes, 4 coordssample[Fields.meta][MetaKeys.bbox_tag]=np.empty((0,0,4),dtype=np.float32)returnsampleifMetaKeys.bbox_taginsample[Fields.meta]:returnsampleloaded_image_keys=sample[self.image_key]sample,images=load_data_with_context(sample,context,loaded_image_keys,load_image)model=get_model(self.model_key,rank=rank,use_cuda=self.use_cuda())sample[Fields.meta][MetaKeys.bbox_tag]=[]forimageinimages:masks=model(image,retina_masks=True,imgsz=self.imgsz,conf=self.conf,iou=self.iou,verbose=False)[0]sample[Fields.meta][MetaKeys.bbox_tag].append(masks.boxes.xywh.cpu().numpy())# match schemaiflen(sample[Fields.meta][MetaKeys.bbox_tag])==0:sample[Fields.meta][MetaKeys.bbox_tag]=np.empty((0,0,4),dtype=np.float32)returnsample