UperNet
Last updated
Last updated
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.
This model was contributed by nielsr. The original code is based on OpenMMLab’s mmsegmentation here.
A list of official BOINC AI 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:
Copied
To use another vision backbone, like ConvNeXt, simply instantiate the model with the appropriate backbone:
Copied
Note that this will randomly initialize all the weights of the model.
( backbone_config = Nonehidden_size = 512initializer_range = 0.02pool_scales = [1, 2, 3, 6]use_auxiliary_head = Trueauxiliary_loss_weight = 0.4auxiliary_in_channels = 384auxiliary_channels = 256auxiliary_num_convs = 1auxiliary_concat_input = Falseloss_ignore_index = 255**kwargs )
Parameters
backbone_config (PretrainedConfig
or 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.
initializer_range (float
, optional, defaults to 0.02) — The standard deviation of the truncated_normal_initializer for initializing all weight matrices.
pool_scales (Tuple[int]
, optional, defaults to [1, 2, 3, 6]
) — Pooling scales used in Pooling Pyramid Module applied on the last feature map.
use_auxiliary_head (bool
, optional, defaults to True
) — Whether to use an auxiliary head during training.
auxiliary_loss_weight (float
, optional, defaults to 0.4) — Weight of the cross-entropy loss of the auxiliary head.
auxiliary_channels (int
, optional, defaults to 256) — Number of channels to use in the auxiliary head.
auxiliary_num_convs (int
, optional, defaults to 1) — Number of convolutional layers to use in the auxiliary head.
auxiliary_concat_input (bool
, optional, defaults to False
) — Whether to concatenate the output of the auxiliary head with the input before the classification layer.
loss_ignore_index (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.
Configuration objects inherit from PretrainedConfig and can be used to control the model outputs. Read the documentation from PretrainedConfig for more information.
Examples:
Copied
( config )
Parameters
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
( pixel_values: typing.Optional[torch.Tensor] = Noneoutput_attentions: typing.Optional[bool] = Noneoutput_hidden_states: typing.Optional[bool] = Nonelabels: typing.Optional[torch.Tensor] = Nonereturn_dict: typing.Optional[bool] = None ) → transformers.modeling_outputs.SemanticSegmenterOutput or tuple(torch.FloatTensor)
Parameters
pixel_values (torch.FloatTensor
of shape (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.
output_attentions (bool
, optional) — Whether or not to return the attentions tensors of all attention layers in case the backbone has them. See attentions
under 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_states
under returned tensors for more detail.
return_dict (bool
, optional) — Whether or not to return a ModelOutput instead of a plain tuple.
labels (torch.LongTensor
of shape (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).
Returns
transformers.modeling_outputs.SemanticSegmenterOutput or tuple(torch.FloatTensor)
A transformers.modeling_outputs.SemanticSegmenterOutput or a tuple of torch.FloatTensor
(if return_dict=False
is passed or when config.return_dict=False
) comprising various elements depending on the configuration (UperNetConfig) and inputs.
loss (torch.FloatTensor
of shape (1,)
, optional, returned when labels
is provided) — Classification (or regression if config.num_labels==1) loss.
logits (torch.FloatTensor
of shape (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_values
passed 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.
hidden_states (tuple(torch.FloatTensor)
, optional, returned when output_hidden_states=True
is 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.
attentions (tuple(torch.FloatTensor)
, optional, returned when output_attentions=True
is 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 Module
instance afterwards instead of this since the former takes care of running the pre and post processing steps while the latter silently ignores them.
Examples:
Copied