Mask2Former模型基于Mapillary Vistas语义分割(大型版本,采用Swin骨干网络)进行训练。该模型在 Masked-attention Mask Transformer for Universal Image Segmentation 论文中首次提出,并在 this repository 时首次发布。
免责声明:发布Mask2Former的团队并没有为该模型撰写模型卡,因此此模型卡是由Hugging Face团队撰写的。
Mask2Former同时处理实例分割、语义分割和全景分割,采用相同的范式:预测一组矩形掩码及其对应的标签。因此,这三个任务都被视为实例分割。Mask2Former通过(i)使用更先进的多尺度可变形注意力Transformer替换像素解码器,(ii)采用具有掩码注意力的Transformer解码器以提高性能而不引入额外计算量,(iii)通过计算部分掩码上的损失而不是整个掩码来提高训练效率,从而优于之前的SOTA模型 MaskFormer ,无论是在性能还是效率上都有所提升。
您可以使用这个特定的检查点进行全景分割。查看 model hub 以寻找您感兴趣的其他任务的微调版本。
下面是如何使用该模型的步骤:
import requests
import torch
from PIL import Image
from transformers import AutoImageProcessor, Mask2FormerForUniversalSegmentation
# load Mask2Former fine-tuned on Mapillary Vistas semantic segmentation
processor = AutoImageProcessor.from_pretrained("facebook/mask2former-swin-large-mapillary-vistas-semantic")
model = Mask2FormerForUniversalSegmentation.from_pretrained("facebook/mask2former-swin-large-mapillary-vistas-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 。