Mask2Former模型是基于Cityscapes全景分割训练的(基于IN21k版本,使用Swin骨干网络)。该模型在论文[ Masked-attention Mask Transformer for Universal Image Segmentation ](链接)中提出,并于[ this repository ](链接)首次发布。
免责声明:发布Mask2Former模型的团队未为该模型撰写模型卡片,因此本模型卡是由Hugging Face团队编写的。
Mask2Former模型采用了相同的思路来处理实例分割、语义分割和全景分割,通过预测一组掩码和对应的标签来解决这三个任务。因此,所有三个任务都被视为实例分割。Mask2Former通过以下方式在性能和效率上优于之前的最优模型[ MaskFormer ](链接):(i) 用更先进的多尺度可变形注意力Transformer替换了像素解码器,(ii) 采用Transformer解码器并使用掩码注意力来提升性能而不引入额外计算,(iii) 通过在子采样点上计算损失而不是整个掩码来提高训练效率。
您可以将该模型用于全景分割任务。如果您有其他感兴趣的任务,请查看[ model hub ](链接)以寻找其他经过微调的版本。
以下是使用该模型的方法:
import requests import torch from PIL import Image from transformers import AutoImageProcessor, Mask2FormerForUniversalSegmentation # load Mask2Former fine-tuned on Cityscapes panoptic segmentation processor = AutoImageProcessor.from_pretrained("facebook/mask2former-swin-base-IN21k-cityscapes-panoptic/") model = Mask2FormerForUniversalSegmentation.from_pretrained("facebook/mask2former-swin-base-IN21k-cityscapes-panoptic/") url = "http://images.cocodataset.org/val2017/000000039769.jpg" image = Image.open(requests.get(url, stream=True).raw) inputs = processor(images=image, return_tensors="pt") with torch.no_grad(): outputs = model(**inputs) # model predicts class_queries_logits of shape `(batch_size, num_queries)` # and masks_queries_logits of shape `(batch_size, num_queries, height, width)` class_queries_logits = outputs.class_queries_logits masks_queries_logits = outputs.masks_queries_logits # you can pass them to processor for postprocessing result = processor.post_process_panoptic_segmentation(outputs, target_sizes=[image.size[::-1]])[0] # we refer to the demo notebooks for visualization (see "Resources" section in the Mask2Former docs) predicted_panoptic_map = result["segmentation"]
对于更多代码示例,请参考[ documentation ](链接)。