The UPerNet model was proposed in Unified Perceptual Parsing for Scene Understanding by Tete Xiao, Yingcheng Liu, Bolei Zhou, Yuning Jiang, Jian Sun. UPerNet is a general framework to effectively segment a wide range of concepts from images, leveraging any vision backbone like ConvNeXt or Swin.
The abstract from the paper is the following:
Humans recognize the visual world at multiple levels: we effortlessly categorize scenes and detect objects inside, while also identifying the textures and surfaces of the objects along with their different compositional parts. In this paper, we study a new task called Unified Perceptual Parsing, which requires the machine vision systems to recognize as many visual concepts as possible from a given image. A multi-task framework called UPerNet and a training strategy are developed to learn from heterogeneous image annotations. We benchmark our framework on Unified Perceptual Parsing and show that it is able to effectively segment a wide range of concepts from images. The trained networks are further applied to discover visual knowledge in natural scenes.UPerNet framework. Taken from the original paper.
A list of official Hugging Face and community (indicated by 🌎) resources to help you get started with UPerNet.
- Demo notebooks for UPerNet can be found here.
- UperNetForSemanticSegmentation is supported by this example script and notebook.
- See also: Semantic segmentation task guide
If you’re interested in submitting a resource to be included here, please feel free to open a Pull Request and we’ll review it! The resource should ideally demonstrate something new instead of duplicating an existing resource.
UPerNet is a general framework for semantic segmentation. It can be used with any vision backbone, like so:
from transformers import SwinConfig, UperNetConfig, UperNetForSemanticSegmentation backbone_config = SwinConfig(out_features=["stage1", "stage2", "stage3", "stage4"]) config = UperNetConfig(backbone_config=backbone_config) model = UperNetForSemanticSegmentation(config)
To use another vision backbone, like ConvNeXt, simply instantiate the model with the appropriate backbone:
from transformers import ConvNextConfig, UperNetConfig, UperNetForSemanticSegmentation backbone_config = ConvNextConfig(out_features=["stage1", "stage2", "stage3", "stage4"]) config = UperNetConfig(backbone_config=backbone_config) model = UperNetForSemanticSegmentation(config)
Note that this will randomly initialize all the weights of the model.
class transformers.UperNetConfig< source >
( backbone_config = None hidden_size = 512 initializer_range = 0.02 pool_scales = [1, 2, 3, 6] use_auxiliary_head = True auxiliary_loss_weight = 0.4 auxiliary_in_channels = 384 auxiliary_channels = 256 auxiliary_num_convs = 1 auxiliary_concat_input = False loss_ignore_index = 255 **kwargs )
dict, optional, defaults to
ResNetConfig()) — The configuration of the backbone model.
- hidden_size (
int, optional, defaults to 512) — The number of hidden units in the convolutional layers.
float, optional, defaults to 0.02) — The standard deviation of the truncated_normal_initializer for initializing all weight matrices.
Tuple[int], optional, defaults to
[1, 2, 3, 6]) — Pooling scales used in Pooling Pyramid Module applied on the last feature map.
bool, optional, defaults to
True) — Whether to use an auxiliary head during training.
float, optional, defaults to 0.4) — Weight of the cross-entropy loss of the auxiliary head.
int, optional, defaults to 256) — Number of channels to use in the auxiliary head.
int, optional, defaults to 1) — Number of convolutional layers to use in the auxiliary head.
bool, optional, defaults to
False) — Whether to concatenate the output of the auxiliary head with the input before the classification layer.
int, optional, defaults to 255) — The index that is ignored by the loss function.
This is the configuration class to store the configuration of an UperNetForSemanticSegmentation. It is used to instantiate an UperNet model according to the specified arguments, defining the model architecture. Instantiating a configuration with the defaults will yield a similar configuration to that of the UperNet openmmlab/upernet-convnext-tiny architecture.
from transformers import UperNetConfig, UperNetForSemanticSegmentation # Initializing a configuration configuration = UperNetConfig() # Initializing a model (with random weights) from the configuration model = UperNetForSemanticSegmentation(configuration) # Accessing the model configuration configuration = model.config
class transformers.UperNetForSemanticSegmentation< source >
( config )
- This model is a PyTorch [torch.nn.Module](https —//pytorch.org/docs/stable/nn.html#torch.nn.Module) sub-class. Use
- it as a regular PyTorch Module and refer to the PyTorch documentation for all matter related to general usage and — behavior. — config (UperNetConfig): Model configuration class with all the parameters of the model. Initializing with a config file does not load the weights associated with the model, only the configuration. Check out the from_pretrained() method to load the model weights.
UperNet framework leveraging any vision backbone e.g. for ADE20k, CityScapes.
forward< source >
pixel_values: typing.Optional[torch.Tensor] = None
output_attentions: typing.Optional[bool] = None
output_hidden_states: typing.Optional[bool] = None
labels: typing.Optional[torch.Tensor] = None
return_dict: typing.Optional[bool] = None
(batch_size, num_channels, height, width)) — Pixel values. Padding will be ignored by default should you provide it. Pixel values can be obtained using AutoImageProcessor. See SegformerImageProcessor.call() for details.
bool, optional) — Whether or not to return the attentions tensors of all attention layers in case the backbone has them. See
attentionsunder returned tensors for more detail.
- output_hidden_states (
bool, optional) — Whether or not to return the hidden states of all layers of the backbone. See
hidden_statesunder returned tensors for more detail.
bool, optional) — Whether or not to return a ModelOutput instead of a plain tuple.
(batch_size, height, width), optional) — Ground truth semantic segmentation maps for computing the loss. Indices should be in
[0, ..., config.num_labels - 1]. If
config.num_labels > 1, a classification loss is computed (Cross-Entropy).
A transformers.modeling_outputs.SemanticSegmenterOutput or a tuple of
return_dict=False is passed or when
config.return_dict=False) comprising various
elements depending on the configuration (UperNetConfig) and inputs.
(1,), optional, returned when
labelsis provided) — Classification (or regression if config.num_labels==1) loss.
(batch_size, config.num_labels, logits_height, logits_width)) — Classification scores for each pixel.
The logits returned do not necessarily have the same size as the
pixel_valuespassed as inputs. This is to avoid doing two interpolations and lose some quality when a user needs to resize the logits to the original image size as post-processing. You should always check your logits shape and resize as needed.
tuple(torch.FloatTensor), optional, returned when
output_hidden_states=Trueis passed or when
config.output_hidden_states=True) — Tuple of
torch.FloatTensor(one for the output of the embeddings, if the model has an embedding layer, + one for the output of each layer) of shape
(batch_size, patch_size, hidden_size).
Hidden-states of the model at the output of each layer plus the optional initial embedding outputs.
tuple(torch.FloatTensor), optional, returned when
output_attentions=Trueis passed or when
config.output_attentions=True) — Tuple of
torch.FloatTensor(one for each layer) of shape
(batch_size, num_heads, patch_size, sequence_length).
Attentions weights after the attention softmax, used to compute the weighted average in the self-attention heads.
The UperNetForSemanticSegmentation forward method, overrides the
__call__ special method.
Although the recipe for forward pass needs to be defined within this function, one should call the
instance afterwards instead of this since the former takes care of running the pre and post processing steps while
the latter silently ignores them.
from transformers import AutoImageProcessor, UperNetForSemanticSegmentation from PIL import Image from huggingface_hub import hf_hub_download image_processor = AutoImageProcessor.from_pretrained("openmmlab/upernet-convnext-tiny") model = UperNetForSemanticSegmentation.from_pretrained("openmmlab/upernet-convnext-tiny") filepath = hf_hub_download( repo_id="hf-internal-testing/fixtures_ade20k", filename="ADE_val_00000001.jpg", repo_type="dataset" ) image = Image.open(filepath).convert("RGB") inputs = image_processor(images=image, return_tensors="pt") outputs = model(**inputs) logits = outputs.logits # shape (batch_size, num_labels, height, width) list(logits.shape) [1, 150, 512, 512]