Mask2Former model trained on ADE20k semantic segmentation (base-sized version, Swin backbone). It was introduced in the paper
Masked-attention Mask Transformer for Universal Image Segmentation
and first released in
this repository.
Disclaimer: The team releasing Mask2Former did not write a model card for this model so this model card has been written by the Hugging Face team.
Mask2Former addresses instance, semantic and panoptic segmentation with the same paradigm: by predicting a set of masks and corresponding labels. Hence, all 3 tasks are treated as if they were instance segmentation. Mask2Former outperforms the previous SOTA,
MaskFormer both in terms of performance an efficiency by (i) replacing the pixel decoder with a more advanced multi-scale deformable attention Transformer, (ii) adopting a Transformer decoder with masked attention to boost performance without
without introducing additional computation and (iii) improving training efficiency by calculating the loss on subsampled points instead of whole masks.
You can use this particular checkpoint for panoptic segmentation. See the
model hub to look for other
fine-tuned versions on a task that interests you.
1import requests
2import torch
3from PIL import Image
4from transformers import AutoImageProcessor, Mask2FormerForUniversalSegmentation
5
6
7# load Mask2Former fine-tuned on ADE20k semantic segmentation
8processor = AutoImageProcessor.from_pretrained("facebook/mask2former-swin-base-ade-semantic")
9model = Mask2FormerForUniversalSegmentation.from_pretrained("facebook/mask2former-swin-base-ade-semantic")
10
11url = "http://images.cocodataset.org/val2017/000000039769.jpg"
12image = Image.open(requests.get(url, stream=True).raw)
13inputs = processor(images=image, return_tensors="pt")
14
15with torch.no_grad():
16 outputs = model(**inputs)
17
18# model predicts class_queries_logits of shape `(batch_size, num_queries)`
19# and masks_queries_logits of shape `(batch_size, num_queries, height, width)`
20class_queries_logits = outputs.class_queries_logits
21masks_queries_logits = outputs.masks_queries_logits
22
23# you can pass them to processor for postprocessing
24predicted_semantic_map = processor.post_process_semantic_segmentation(outputs, target_sizes=[image.size[::-1]])[0]
25# we refer to the demo notebooks for visualization (see "Resources" section in the Mask2Former docs)
For more code examples, we refer to the
documentation.