Mask2Former模型是在ADE20k语义分割数据集上训练的(小尺寸版本,使用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 ADE20k semantic segmentation processor = AutoImageProcessor.from_pretrained("facebook/mask2former-swin-small-ade-semantic") model = Mask2FormerForUniversalSegmentation.from_pretrained("facebook/mask2former-swin-small-ade-semantic") 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 predicted_semantic_map = processor.post_process_semantic_segmentation(outputs, target_sizes=[image.size[::-1]])[0] # we refer to the demo notebooks for visualization (see "Resources" section in the Mask2Former docs)
有关更多代码示例,请参阅 documentation 。