The OneFormer model was proposed in OneFormer: One Transformer to Rule Universal Image Segmentation by Jitesh Jain, Jiachen Li, MangTik Chiu, Ali Hassani, Nikita Orlov, Humphrey Shi. OneFormer is a universal image segmentation framework that can be trained on a single panoptic dataset to perform semantic, instance, and panoptic segmentation tasks. OneFormer uses a task token to condition the model on the task in focus, making the architecture task-guided for training, and task-dynamic for inference.
The abstract from the paper is the following:
Universal Image Segmentation is not a new concept. Past attempts to unify image segmentation in the last decades include scene parsing, panoptic segmentation, and, more recently, new panoptic architectures. However, such panoptic architectures do not truly unify image segmentation because they need to be trained individually on the semantic, instance, or panoptic segmentation to achieve the best performance. Ideally, a truly universal framework should be trained only once and achieve SOTA performance across all three image segmentation tasks. To that end, we propose OneFormer, a universal image segmentation framework that unifies segmentation with a multi-task train-once design. We first propose a task-conditioned joint training strategy that enables training on ground truths of each domain (semantic, instance, and panoptic segmentation) within a single multi-task training process. Secondly, we introduce a task token to condition our model on the task at hand, making our model task-dynamic to support multi-task training and inference. Thirdly, we propose using a query-text contrastive loss during training to establish better inter-task and inter-class distinctions. Notably, our single OneFormer model outperforms specialized Mask2Former models across all three segmentation tasks on ADE20k, CityScapes, and COCO, despite the latter being trained on each of the three tasks individually with three times the resources. With new ConvNeXt and DiNAT backbones, we observe even more performance improvement. We believe OneFormer is a significant step towards making image segmentation more universal and accessible.
Tips:
get_num_masks
function inside in the OneFormerLoss
class of modeling_oneformer.py
. When training on multiple nodes, this should be
set to the average number of target masks across all nodes, as can be seen in the original implementation here.OneformerProcessor
wraps OneFormerImageProcessor and CLIPTokenizer into a single instance to both prepare the images and encode the task inputs.label_ids_to_fuse
argument to fuse instances of the target object/s (e.g. sky) together.The figure below illustrates the architecture of OneFormer. Taken from the original paper.
This model was contributed by Jitesh Jain. The original code can be found here.
A list of official Hugging Face and community (indicated by π) resources to help you get started with OneFormer.
If youβre interested in submitting a resource to be included here, please feel free to open a Pull Request and we will review it. The resource should ideally demonstrate something new instead of duplicating an existing resource.
( encoder_hidden_states: typing.Optional[typing.Tuple[torch.FloatTensor]] = None pixel_decoder_hidden_states: typing.Optional[typing.Tuple[torch.FloatTensor]] = None transformer_decoder_hidden_states: typing.Optional[torch.FloatTensor] = None transformer_decoder_object_queries: FloatTensor = None transformer_decoder_contrastive_queries: typing.Optional[torch.FloatTensor] = None transformer_decoder_mask_predictions: FloatTensor = None transformer_decoder_class_predictions: FloatTensor = None transformer_decoder_auxiliary_predictions: typing.Union[typing.Tuple[typing.Dict[str, torch.FloatTensor]], NoneType] = None text_queries: typing.Optional[torch.FloatTensor] = None task_token: FloatTensor = None attentions: typing.Optional[typing.Tuple[torch.FloatTensor]] = None )
Parameters
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 + one for the output of each stage) of
shape (batch_size, num_channels, height, width)
. Hidden-states (also called feature maps) of the encoder
model at the output of each stage.
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 + one for the output of each stage) of
shape (batch_size, num_channels, height, width)
. Hidden-states (also called feature maps) of the pixel
decoder model at the output of each stage.
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 + one for the output of each stage) of
shape (batch_size, sequence_length, hidden_size)
. Hidden-states (also called feature maps) of the
transformer decoder at the output of each stage.
torch.FloatTensor
of shape (batch_size, num_queries, hidden_dim)
) —
Output object queries from the last layer in the transformer decoder.
torch.FloatTensor
of shape (batch_size, num_queries, hidden_dim)
) —
Contrastive queries from the transformer decoder.
torch.FloatTensor
of shape (batch_size, num_queries, height, width)
) —
Mask Predictions from the last layer in the transformer decoder.
torch.FloatTensor
of shape (batch_size, num_queries, num_classes+1)
) —
Class Predictions from the last layer in the transformer decoder.
str, torch.FloatTensor
, optional) —
Tuple of class and mask predictions from each layer of the transformer decoder.
torch.FloatTensor
, optional of shape (batch_size, num_queries, hidden_dim)
) —
Text queries derived from the input text list used for calculating contrastive loss during training.
torch.FloatTensor
of shape (batch_size, hidden_dim)
) —
1D task token to condition the queries.
tuple(tuple(torch.FloatTensor))
, optional, returned when output_attentions=True
is passed or when config.output_attentions=True
) —
Tuple of tuple(torch.FloatTensor)
(one for each layer) of shape (batch_size, num_heads, sequence_length, sequence_length)
. Self and Cross Attentions weights from transformer decoder.
Class for outputs of OneFormerModel. This class returns all the needed hidden states to compute the logits.
( loss: typing.Optional[torch.FloatTensor] = None class_queries_logits: FloatTensor = None masks_queries_logits: FloatTensor = None auxiliary_predictions: typing.List[typing.Dict[str, torch.FloatTensor]] = None encoder_hidden_states: typing.Optional[typing.Tuple[torch.FloatTensor]] = None pixel_decoder_hidden_states: typing.Optional[typing.List[torch.FloatTensor]] = None transformer_decoder_hidden_states: typing.Optional[torch.FloatTensor] = None transformer_decoder_object_queries: FloatTensor = None transformer_decoder_contrastive_queries: typing.Optional[torch.FloatTensor] = None transformer_decoder_mask_predictions: FloatTensor = None transformer_decoder_class_predictions: FloatTensor = None transformer_decoder_auxiliary_predictions: typing.Union[typing.List[typing.Dict[str, torch.FloatTensor]], NoneType] = None text_queries: typing.Optional[torch.FloatTensor] = None task_token: FloatTensor = None attentions: typing.Optional[typing.Tuple[typing.Tuple[torch.FloatTensor]]] = None )
Parameters
torch.Tensor
, optional) —
The computed loss, returned when labels are present.
torch.FloatTensor
) —
A tensor of shape (batch_size, num_queries, num_labels + 1)
representing the proposed classes for each
query. Note the + 1
is needed because we incorporate the null class.
torch.FloatTensor
) —
A tensor of shape (batch_size, num_queries, height, width)
representing the proposed masks for each
query.
str, torch.FloatTensor
, optional) —
List of class and mask predictions from each layer of the transformer decoder.
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 + one for the output of each stage) of
shape (batch_size, num_channels, height, width)
. Hidden-states (also called feature maps) of the encoder
model at the output of each stage.
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 + one for the output of each stage) of
shape (batch_size, num_channels, height, width)
. Hidden-states (also called feature maps) of the pixel
decoder model at the output of each stage.
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 + one for the output of each stage) of
shape (batch_size, sequence_length, hidden_size)
. Hidden-states (also called feature maps) of the
transformer decoder at the output of each stage.
torch.FloatTensor
of shape (batch_size, num_queries, hidden_dim)
) —
Output object queries from the last layer in the transformer decoder.
torch.FloatTensor
of shape (batch_size, num_queries, hidden_dim)
) —
Contrastive queries from the transformer decoder.
torch.FloatTensor
of shape (batch_size, num_queries, height, width)
) —
Mask Predictions from the last layer in the transformer decoder.
torch.FloatTensor
of shape (batch_size, num_queries, num_classes+1)
) —
Class Predictions from the last layer in the transformer decoder.
str, torch.FloatTensor
, optional) —
List of class and mask predictions from each layer of the transformer decoder.
torch.FloatTensor
, optional of shape (batch_size, num_queries, hidden_dim)
) —
Text queries derived from the input text list used for calculating contrastive loss during training.
torch.FloatTensor
of shape (batch_size, hidden_dim)
) —
1D task token to condition the queries.
tuple(tuple(torch.FloatTensor))
, optional, returned when output_attentions=True
is passed or when config.output_attentions=True
) —
Tuple of tuple(torch.FloatTensor)
(one for each layer) of shape (batch_size, num_heads, sequence_length, sequence_length)
. Self and Cross Attentions weights from transformer decoder.
Class for outputs of OneFormerForUniversalSegmentationOutput
.
This output can be directly passed to post_process_semantic_segmentation() or post_process_instance_segmentation() or post_process_panoptic_segmentation() depending on the task. Please, see [`~OneFormerImageProcessor] for details regarding usage.
( backbone_config: typing.Optional[typing.Dict] = None ignore_value: int = 255 num_queries: int = 150 no_object_weight: int = 0.1 class_weight: float = 2.0 mask_weight: float = 5.0 dice_weight: float = 5.0 contrastive_weight: float = 0.5 contrastive_temperature: float = 0.07 train_num_points: int = 12544 oversample_ratio: float = 3.0 importance_sample_ratio: float = 0.75 init_std: float = 0.02 init_xavier_std: float = 1.0 layer_norm_eps: float = 1e-05 is_training: bool = False use_auxiliary_loss: bool = True output_auxiliary_logits: bool = True strides: typing.Optional[list] = [4, 8, 16, 32] task_seq_len: int = 77 text_encoder_width: int = 256 text_encoder_context_length: int = 77 text_encoder_num_layers: int = 6 text_encoder_vocab_size: int = 49408 text_encoder_proj_layers: int = 2 text_encoder_n_ctx: int = 16 conv_dim: int = 256 mask_dim: int = 256 hidden_dim: int = 256 encoder_feedforward_dim: int = 1024 norm: str = 'GN' encoder_layers: int = 6 decoder_layers: int = 10 use_task_norm: bool = True num_attention_heads: int = 8 dropout: float = 0.1 dim_feedforward: int = 2048 pre_norm: bool = False enforce_input_proj: bool = False query_dec_layers: int = 2 common_stride: int = 4 **kwargs )
Parameters
PretrainedConfig
, optional, defaults to SwinConfig
) —
The configuration of the backbone model.
int
, optional, defaults to 255) —
Values to be ignored in GT label while calculating loss.
int
, optional, defaults to 150) —
Number of object queries.
float
, optional, defaults to 0.1) —
Weight for no-object class predictions.
float
, optional, defaults to 2.0) —
Weight for Classification CE loss.
float
, optional, defaults to 5.0) —
Weight for binary CE loss.
float
, optional, defaults to 5.0) —
Weight for dice loss.
float
, optional, defaults to 0.5) —
Weight for contrastive loss.
float
, optional, defaults to 0.07) —
Initial value for scaling the contrastive logits.
int
, optional, defaults to 12544) —
Number of points to sample while calculating losses on mask predictions.
float
, optional, defaults to 3.0) —
Ratio to decide how many points to oversample.
float
, optional, defaults to 0.75) —
Ratio of points that are sampled via importance sampling.
float
, optional, defaults to 0.02) —
Standard deviation for normal intialization.
float
, optional, defaults to 0.02) —
Standard deviation for xavier uniform initialization.
float
, optional, defaults to 1e-05) —
Epsilon for layer normalization.
bool
, optional, defaults to False) —
Whether to run in training or inference mode.
bool
, optional, defaults to True) —
Whether to calculate loss using intermediate predictions from transformer decoder.
bool
, optional, defaults to True) —
Whether to return intermediate predictions from transformer decoder.
list
, optional, defaults to [4, 8, 16, 32]) —
List containing the strides for feature maps in the encoder.
int
, optional, defaults to 77) —
Sequence length for tokenizing text list input.
int
, optional, defaults to 256) —
Hidden size for text encoder.
int
, optional, defaults to 77) —
Input sequence length for text encoder.
int
, optional, defaults to 6) —
Number of layers for transformer in text encoder.
int
, optional, defaults to 49408) —
Vocabulary size for tokenizer.
int
, optional, defaults to 2) —
Number of layers in MLP for project text queries.
int
, optional, defaults to 16) —
Number of learnable text context queries.
int
, optional, defaults to 256) —
Feature map dimension to map outputs from the backbone.
int
, optional, defaults to 256) —
Dimension for feature maps in pixel decoder.
int
, optional, defaults to 256) —
Dimension for hidden states in transformer decoder.
int
, optional, defaults to 1024) —
Dimension for FFN layer in pixel decoder.
str
, optional, defaults to GN
) —
Type of normalization.
int
, optional, defaults to 6) —
Number of layers in pixel decoder.
int
, optional, defaults to 10) —
Number of layers in transformer decoder.
bool
, optional, defaults to True
) —
Whether to normalize the task token.
int
, optional, defaults to 8) —
Number of attention heads in transformer layers in the pixel and transformer decoders.
float
, optional, defaults to 0.1) —
Dropout probability for pixel and transformer decoders.
int
, optional, defaults to 2048) —
Dimension for FFN layer in transformer decoder.
bool
, optional, defaults to False
) —
Whether to normalize hidden states before attention layers in transformer decoder.
bool
, optional, defaults to False
) —
Whether to project hidden states in transformer decoder.
int
, optional, defaults to 2) —
Number of layers in query transformer.
int
, optional, defaults to 4) —
Common stride used for features in pixel decoder.
This is the configuration class to store the configuration of a OneFormerModel. It is used to instantiate a OneFormer 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 OneFormer shi-labs/oneformer_ade20k_swin_tiny architecture trained on ADE20k-150.
Configuration objects inherit from PretrainedConfig and can be used to control the model outputs. Read the documentation from PretrainedConfig for more information.
Examples:
>>> from transformers import OneFormerConfig, OneFormerModel
>>> # Initializing a OneFormer shi-labs/oneformer_ade20k_swin_tiny configuration
>>> configuration = OneFormerConfig()
>>> # Initializing a model (with random weights) from the shi-labs/oneformer_ade20k_swin_tiny style configuration
>>> model = OneFormerModel(configuration)
>>> # Accessing the model configuration
>>> configuration = model.config
Serializes this instance to a Python dictionary. Override the default to_dict(). Returns:
Dict[str, any]
: Dictionary of all the attributes that make up this configuration instance,
( do_resize: bool = True size: typing.Dict[str, int] = None resample: Resampling = <Resampling.BILINEAR: 2> do_rescale: bool = True rescale_factor: float = 0.00392156862745098 do_normalize: bool = True image_mean: typing.Union[float, typing.List[float]] = None image_std: typing.Union[float, typing.List[float]] = None ignore_index: typing.Optional[int] = None do_reduce_labels: bool = False repo_path: str = 'shi-labs/oneformer_demo' class_info_file: str = None num_text: typing.Optional[int] = None **kwargs )
Parameters
bool
, optional, defaults to True
) —
Whether to resize the input to a certain size
.
int
, optional, defaults to 800) —
Resize the input to the given size. Only has an effect if do_resize
is set to True
. If size is a
sequence like (width, height)
, output size will be matched to this. If size is an int, smaller edge of
the image will be matched to this number. i.e, if height > width
, then image will be rescaled to (size * height / width, size)
.
int
, optional, defaults to 1333) —
The largest size an image dimension can have (otherwise it’s capped). Only has an effect if do_resize
is
set to True
.
int
, optional, defaults to PIL.Image.Resampling.BILINEAR
) —
An optional resampling filter. This can be one of PIL.Image.Resampling.NEAREST
,
PIL.Image.Resampling.BOX
, PIL.Image.Resampling.BILINEAR
, PIL.Image.Resampling.HAMMING
,
PIL.Image.Resampling.BICUBIC
or PIL.Image.Resampling.LANCZOS
. Only has an effect if do_resize
is set
to True
.
bool
, optional, defaults to True
) —
Whether to rescale the input to a certain scale
.
float
, optional, defaults to 1/ 255) —
Rescale the input by the given factor. Only has an effect if do_rescale
is set to True
.
bool
, optional, defaults to True
) —
Whether or not to normalize the input with mean and standard deviation.
int
, optional, defaults to [0.485, 0.456, 0.406]
) —
The sequence of means for each channel, to be used when normalizing images. Defaults to the ImageNet mean.
int
, optional, defaults to [0.229, 0.224, 0.225]
) —
The sequence of standard deviations for each channel, to be used when normalizing images. Defaults to the
ImageNet std.
int
, optional) —
Label to be assigned to background pixels in segmentation maps. If provided, segmentation map pixels
denoted with 0 (background) will be replaced with ignore_index
.
bool
, optional, defaults to False
) —
Whether or not to decrement all label values of segmentation maps by 1. Usually used for datasets where 0
is used for background, and background itself is not included in all classes of a dataset (e.g. ADE20k).
The background label will be replaced by ignore_index
.
str
, defaults to shi-labs/oneformer_demo
) —
Dataset repository on huggingface hub containing the JSON file with class information for the dataset.
str
) —
JSON file containing class information for the dataset. It is stored inside on the repo_path
dataset
repository.
int
, optional) —
Number of text entries in the text input list.
Constructs a OneFormer image processor. The image processor can be used to prepare image(s), task input(s) and optional text inputs and targets for the model.
This image processor inherits from BaseImageProcessor
which contains most of the main methods. Users should
refer to this superclass for more information regarding those methods.
( images: typing.Union[ForwardRef('PIL.Image.Image'), numpy.ndarray, ForwardRef('torch.Tensor'), typing.List[ForwardRef('PIL.Image.Image')], typing.List[numpy.ndarray], typing.List[ForwardRef('torch.Tensor')]] task_inputs: typing.Optional[typing.List[str]] = None segmentation_maps: typing.Union[ForwardRef('PIL.Image.Image'), numpy.ndarray, ForwardRef('torch.Tensor'), typing.List[ForwardRef('PIL.Image.Image')], typing.List[numpy.ndarray], typing.List[ForwardRef('torch.Tensor')], NoneType] = None instance_id_to_semantic_id: typing.Union[typing.Dict[int, int], NoneType] = None do_resize: typing.Optional[bool] = None size: typing.Union[typing.Dict[str, int], NoneType] = None resample: Resampling = None do_rescale: typing.Optional[bool] = None rescale_factor: typing.Optional[float] = None do_normalize: typing.Optional[bool] = None image_mean: typing.Union[float, typing.List[float], NoneType] = None image_std: typing.Union[float, typing.List[float], NoneType] = None ignore_index: typing.Optional[int] = None do_reduce_labels: typing.Optional[bool] = None return_tensors: typing.Union[str, transformers.utils.generic.TensorType, NoneType] = None data_format: typing.Union[str, transformers.image_utils.ChannelDimension] = <ChannelDimension.FIRST: 'channels_first'> **kwargs )
( pixel_values_list: typing.List[typing.Union[ForwardRef('PIL.Image.Image'), numpy.ndarray, ForwardRef('torch.Tensor'), typing.List[ForwardRef('PIL.Image.Image')], typing.List[numpy.ndarray], typing.List[ForwardRef('torch.Tensor')]]] task_inputs: typing.List[str] segmentation_maps: typing.Union[ForwardRef('PIL.Image.Image'), numpy.ndarray, ForwardRef('torch.Tensor'), typing.List[ForwardRef('PIL.Image.Image')], typing.List[numpy.ndarray], typing.List[ForwardRef('torch.Tensor')]] = None instance_id_to_semantic_id: typing.Union[typing.List[typing.Dict[int, int]], typing.Dict[int, int], NoneType] = None ignore_index: typing.Optional[int] = None reduce_labels: bool = False return_tensors: typing.Union[str, transformers.utils.generic.TensorType, NoneType] = None **kwargs ) β BatchFeature
Parameters
List[ImageInput]
) —
List of images (pixel values) to be padded. Each image should be a tensor of shape (channels, height, width)
.
List[str]
) —
List of task values.
ImageInput
, optional) —
The corresponding semantic segmentation maps with the pixel-wise annotations.
(bool
, optional, defaults to True
):
Whether or not to pad images up to the largest image in a batch and create a pixel mask.
If left to the default, will return a pixel mask that is:
List[Dict[int, int]]
or Dict[int, int]
, optional) —
A mapping between object instance ids and class ids. If passed, segmentation_maps
is treated as an
instance segmentation map where each pixel represents an instance id. Can be provided as a single
dictionary with a global/dataset-level mapping or as a list of dictionaries (one per image), to map
instance ids in each image separately.
str
or TensorType, optional) —
If set, will return tensors instead of NumPy arrays. If set to 'pt'
, return PyTorch torch.Tensor
objects.
Returns
A BatchFeature with the following fields:
=True
or if pixel_mask
is in
self.model_input_names
).(labels, height, width)
to be fed to a model
(when annotations
are provided).(labels)
to be fed to a model (when
annotations
are provided). They identify the labels of mask_labels
, e.g. the label of
mask_labels[i][j]
if class_labels[i][j]
.annotations
are
provided). They identify the binary masks present in the image.Pad images up to the largest image in a batch and create a corresponding pixel_mask
.
OneFormer addresses semantic segmentation with a mask classification paradigm, thus input segmentation maps
will be converted to lists of binary masks and their respective labels. Letβs see an example, assuming
segmentation_maps = [[2,6,7,9]]
, the output will contain mask_labels = [[1,0,0,0],[0,1,0,0],[0,0,1,0],[0,0,0,1]]
(four binary masks) and class_labels = [2,6,7,9]
, the labels for
each mask.
(
outputs
target_sizes: typing.Union[typing.List[typing.Tuple[int, int]], NoneType] = None
)
β
List[torch.Tensor]
Parameters
List[Tuple[int, int]]
, optional) —
List of length (batch_size), where each list item (Tuple[int, int]]
) corresponds to the requested
final size (height, width) of each prediction. If left to None, predictions will not be resized.
Returns
List[torch.Tensor]
A list of length batch_size
, where each item is a semantic segmentation map of shape (height, width)
corresponding to the target_sizes entry (if target_sizes
is specified). Each entry of each
torch.Tensor
correspond to a semantic class id.
Converts the output of MaskFormerForInstanceSegmentation into semantic segmentation maps. Only supports PyTorch.
(
outputs
task_type: str = 'instance'
is_demo: bool = True
threshold: float = 0.5
mask_threshold: float = 0.5
overlap_mask_area_threshold: float = 0.8
target_sizes: typing.Union[typing.List[typing.Tuple[int, int]], NoneType] = None
return_coco_annotation: typing.Optional[bool] = False
)
β
List[Dict]
Parameters
OneFormerForUniversalSegmentationOutput
) —
The outputs from OneFormerForUniversalSegmentationOutput
.
str
, optional), defaults to “instance”) —
The post processing depends on the task token input. If the task_type
is “panoptic”, we need to
ignore the stuff predictions.
bool
, optional), defaults to True
) —
Whether the model is in demo mode. If true, use threshold to predict final masks.
float
, optional, defaults to 0.5) —
The probability score threshold to keep predicted instance masks.
float
, optional, defaults to 0.5) —
Threshold to use when turning the predicted masks into binary values.
float
, optional, defaults to 0.8) —
The overlap mask area threshold to merge or discard small disconnected parts within each binary
instance mask.
List[Tuple]
, optional) —
List of length (batch_size), where each list item (Tuple[int, int]]
) corresponds to the requested
final size (height, width) of each prediction in batch. If left to None, predictions will not be
resized.
bool
, optional), defaults to False
) —
Whether to return predictions in COCO format.
Returns
List[Dict]
A list of dictionaries, one per image, each dictionary containing two keys:
(height, width)
where each pixel represents a segment_id
, set
to None
if no mask if found above threshold
. If target_sizes
is specified, segmentation is resized
to the corresponding target_sizes
entry.segment_id
.segment_id
.True
if label_id
was in label_ids_to_fuse
, False
otherwise.
Multiple instances of the same class / label were fused and assigned a single segment_id
.segment_id
.Converts the output of OneFormerForUniversalSegmentationOutput
into image instance segmentation
predictions. Only supports PyTorch.
(
outputs
threshold: float = 0.5
mask_threshold: float = 0.5
overlap_mask_area_threshold: float = 0.8
label_ids_to_fuse: typing.Optional[typing.Set[int]] = None
target_sizes: typing.Union[typing.List[typing.Tuple[int, int]], NoneType] = None
)
β
List[Dict]
Parameters
MaskFormerForInstanceSegmentationOutput
) —
The outputs from MaskFormerForInstanceSegmentation.
float
, optional, defaults to 0.5) —
The probability score threshold to keep predicted instance masks.
float
, optional, defaults to 0.5) —
Threshold to use when turning the predicted masks into binary values.
float
, optional, defaults to 0.8) —
The overlap mask area threshold to merge or discard small disconnected parts within each binary
instance mask.
Set[int]
, optional) —
The labels in this state will have all their instances be fused together. For instance we could say
there can only be one sky in an image, but several persons, so the label ID for sky would be in that
set, but not the one for person.
List[Tuple]
, optional) —
List of length (batch_size), where each list item (Tuple[int, int]]
) corresponds to the requested
final size (height, width) of each prediction in batch. If left to None, predictions will not be
resized.
Returns
List[Dict]
A list of dictionaries, one per image, each dictionary containing two keys:
(height, width)
where each pixel represents a segment_id
, set
to None
if no mask if found above threshold
. If target_sizes
is specified, segmentation is resized
to the corresponding target_sizes
entry.segment_id
.segment_id
.True
if label_id
was in label_ids_to_fuse
, False
otherwise.
Multiple instances of the same class / label were fused and assigned a single segment_id
.segment_id
.Converts the output of MaskFormerForInstanceSegmentationOutput
into image panoptic segmentation
predictions. Only supports PyTorch.
( image_processor = None tokenizer = None max_seq_length: int = 77 task_seq_length: int = 77 **kwargs )
Parameters
CLIPTokenizer
, CLIPTokenizerFast
]) —
The tokenizer is a required input.
int
, optional, defaults to 77)) —
Sequence length for input text list.
int
, optional, defaults to 77) —
Sequence length for input task token.
Constructs an OneFormer processor which wraps OneFormerImageProcessor and CLIPTokenizer/CLIPTokenizerFast into a single processor that inherits both the image processor and tokenizer functionalities.
This method forwards all its arguments to OneFormerImageProcessor.encode_inputs() and then tokenizes the task_inputs. Please refer to the docstring of this method for more information.
This method forwards all its arguments to OneFormerImageProcessor.post_process_instance_segmentation(). Please refer to the docstring of this method for more information.
This method forwards all its arguments to OneFormerImageProcessor.post_process_panoptic_segmentation(). Please refer to the docstring of this method for more information.
This method forwards all its arguments to OneFormerImageProcessor.post_process_semantic_segmentation(). Please refer to the docstring of this method for more information.
( config: OneFormerConfig )
Parameters
The bare OneFormer Model outputting raw hidden-states without any specific head on top. This model is a PyTorch 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.
(
pixel_values: Tensor
task_inputs: Tensor
text_inputs: typing.Optional[torch.Tensor] = None
pixel_mask: typing.Optional[torch.Tensor] = None
output_hidden_states: typing.Optional[bool] = None
output_attentions: typing.Optional[bool] = None
return_dict: typing.Optional[bool] = None
)
β
transformers.models.oneformer.modeling_oneformer.OneFormerModelOutput or tuple(torch.FloatTensor)
Parameters
torch.FloatTensor
of shape (batch_size, num_channels, height, width)
) —
Pixel values. Pixel values can be obtained using OneFormerProcessor. See
OneFormerProcessor.__call__()
for details.
torch.FloatTensor
of shape (batch_size, sequence_length)
) —
Task inputs. Task inputs can be obtained using AutoImageProcessor. See OneFormerProcessor.__call__()
for details.
torch.LongTensor
of shape (batch_size, height, width)
, optional) —
Mask to avoid performing attention on padding pixel values. Mask values selected in [0, 1]
:
bool
, optional) —
Whether or not to return the hidden states of all layers. See hidden_states
under returned tensors for
more detail.
bool
, optional) —
Whether or not to return the attentions tensors of Detr’s decoder attention layers.
bool
, optional) —
Whether or not to return a ~OneFormerModelOutput
instead of a plain tuple.
Returns
transformers.models.oneformer.modeling_oneformer.OneFormerModelOutput or tuple(torch.FloatTensor)
A transformers.models.oneformer.modeling_oneformer.OneFormerModelOutput 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 (OneFormerConfig) and inputs.
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 + one for the output of each stage) of
shape (batch_size, num_channels, height, width)
. Hidden-states (also called feature maps) of the encoder
model at the output of each stage.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 + one for the output of each stage) of
shape (batch_size, num_channels, height, width)
. Hidden-states (also called feature maps) of the pixel
decoder model at the output of each stage.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 + one for the output of each stage) of
shape (batch_size, sequence_length, hidden_size)
. Hidden-states (also called feature maps) of the
transformer decoder at the output of each stage.torch.FloatTensor
of shape (batch_size, num_queries, hidden_dim)
)
Output object queries from the last layer in the transformer decoder.torch.FloatTensor
of shape (batch_size, num_queries, hidden_dim)
)
Contrastive queries from the transformer decoder.torch.FloatTensor
of shape (batch_size, num_queries, height, width)
)
Mask Predictions from the last layer in the transformer decoder.torch.FloatTensor
of shape (batch_size, num_queries, num_classes+1)
) β Class Predictions from the last layer in the transformer decoder.str, torch.FloatTensor
, optional) β Tuple of class and mask predictions from each layer of the transformer decoder.torch.FloatTensor
, optional of shape (batch_size, num_queries, hidden_dim)
)
Text queries derived from the input text list used for calculating contrastive loss during training.torch.FloatTensor
of shape (batch_size, hidden_dim)
)
1D task token to condition the queries.tuple(tuple(torch.FloatTensor))
, optional, returned when output_attentions=True
is passed or when config.output_attentions=True
) β Tuple of tuple(torch.FloatTensor)
(one for each layer) of shape (batch_size, num_heads, sequence_length, sequence_length)
. Self and Cross Attentions weights from transformer decoder.OneFormerModelOutput
The OneFormerModel 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.
Example:
>>> import torch
>>> from PIL import Image
>>> import requests
>>> from transformers import OneFormerProcessor, OneFormerModel
>>> # download texting image
>>> url = "http://images.cocodataset.org/val2017/000000039769.jpg"
>>> image = Image.open(requests.get(url, stream=True).raw)
>>> # load processor for preprocessing the inputs
>>> processor = OneFormerProcessor.from_pretrained("shi-labs/oneformer_ade20k_swin_tiny")
>>> model = OneFormerModel.from_pretrained("shi-labs/oneformer_ade20k_swin_tiny")
>>> inputs = processor(image, ["semantic"], return_tensors="pt")
>>> with torch.no_grad():
... outputs = model(**inputs)
>>> mask_predictions = outputs.transformer_decoder_mask_predictions
>>> class_predictions = outputs.transformer_decoder_class_predictions
>>> f"π Mask Predictions Shape: {list(mask_predictions.shape)}, Class Predictions Shape: {list(class_predictions.shape)}"
'π Mask Predictions Shape: [1, 150, 128, 171], Class Predictions Shape: [1, 150, 151]'
( config: OneFormerConfig )
Parameters
OneFormer Model for instance, semantic and panoptic image segmentation. This model is a PyTorch 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.
(
pixel_values: Tensor
task_inputs: Tensor
text_inputs: typing.Optional[torch.Tensor] = None
mask_labels: typing.Optional[typing.List[torch.Tensor]] = None
class_labels: typing.Optional[typing.List[torch.Tensor]] = None
pixel_mask: typing.Optional[torch.Tensor] = None
output_auxiliary_logits: typing.Optional[bool] = None
output_hidden_states: typing.Optional[bool] = None
output_attentions: typing.Optional[bool] = None
return_dict: typing.Optional[bool] = None
)
β
transformers.models.oneformer.modeling_oneformer.OneFormerForUniversalSegmentationOutput or tuple(torch.FloatTensor)
Parameters
torch.FloatTensor
of shape (batch_size, num_channels, height, width)
) —
Pixel values. Pixel values can be obtained using OneFormerProcessor. See
OneFormerProcessor.__call__()
for details.
torch.FloatTensor
of shape (batch_size, sequence_length)
) —
Task inputs. Task inputs can be obtained using AutoImageProcessor. See OneFormerProcessor.__call__()
for details.
torch.LongTensor
of shape (batch_size, height, width)
, optional) —
Mask to avoid performing attention on padding pixel values. Mask values selected in [0, 1]
:
bool
, optional) —
Whether or not to return the hidden states of all layers. See hidden_states
under returned tensors for
more detail.
bool
, optional) —
Whether or not to return the attentions tensors of Detr’s decoder attention layers.
bool
, optional) —
Whether or not to return a ~OneFormerModelOutput
instead of a plain tuple.
List[torch.Tensor]
, optional) —
Tensor fof shape (num_queries, sequence_length)
to be fed to a model
List[torch.Tensor]
, optional) —
List of mask labels of shape (num_labels, height, width)
to be fed to a model
List[torch.LongTensor]
, optional) —
list of target class labels of shape (num_labels, height, width)
to be fed to a model. They identify the
labels of mask_labels
, e.g. the label of mask_labels[i][j]
if class_labels[i][j]
.
Returns
transformers.models.oneformer.modeling_oneformer.OneFormerForUniversalSegmentationOutput or tuple(torch.FloatTensor)
A transformers.models.oneformer.modeling_oneformer.OneFormerForUniversalSegmentationOutput 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 (OneFormerConfig) and inputs.
torch.Tensor
, optional) β The computed loss, returned when labels are present.torch.FloatTensor
) β A tensor of shape (batch_size, num_queries, num_labels + 1)
representing the proposed classes for each
query. Note the + 1
is needed because we incorporate the null class.torch.FloatTensor
) β A tensor of shape (batch_size, num_queries, height, width)
representing the proposed masks for each
query.str, torch.FloatTensor
, optional) β List of class and mask predictions from each layer of the transformer decoder.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 + one for the output of each stage) of
shape (batch_size, num_channels, height, width)
. Hidden-states (also called feature maps) of the encoder
model at the output of each stage.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 + one for the output of each stage) of
shape (batch_size, num_channels, height, width)
. Hidden-states (also called feature maps) of the pixel
decoder model at the output of each stage.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 + one for the output of each stage) of
shape (batch_size, sequence_length, hidden_size)
. Hidden-states (also called feature maps) of the
transformer decoder at the output of each stage.torch.FloatTensor
of shape (batch_size, num_queries, hidden_dim)
)
Output object queries from the last layer in the transformer decoder.torch.FloatTensor
of shape (batch_size, num_queries, hidden_dim)
)
Contrastive queries from the transformer decoder.torch.FloatTensor
of shape (batch_size, num_queries, height, width)
)
Mask Predictions from the last layer in the transformer decoder.torch.FloatTensor
of shape (batch_size, num_queries, num_classes+1)
) β Class Predictions from the last layer in the transformer decoder.str, torch.FloatTensor
, optional) β List of class and mask predictions from each layer of the transformer decoder.torch.FloatTensor
, optional of shape (batch_size, num_queries, hidden_dim)
)
Text queries derived from the input text list used for calculating contrastive loss during training.torch.FloatTensor
of shape (batch_size, hidden_dim)
)
1D task token to condition the queries.tuple(tuple(torch.FloatTensor))
, optional, returned when output_attentions=True
is passed or when config.output_attentions=True
) β Tuple of tuple(torch.FloatTensor)
(one for each layer) of shape (batch_size, num_heads, sequence_length, sequence_length)
. Self and Cross Attentions weights from transformer decoder.OneFormerUniversalSegmentationOutput
The OneFormerForUniversalSegmentation 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.
Example:
Universal segmentation example:
>>> from transformers import OneFormerProcessor, OneFormerForUniversalSegmentation
>>> from PIL import Image
>>> import requests
>>> import torch
>>> # load OneFormer fine-tuned on ADE20k for universal segmentation
>>> processor = OneFormerProcessor.from_pretrained("shi-labs/oneformer_ade20k_swin_tiny")
>>> model = OneFormerForUniversalSegmentation.from_pretrained("shi-labs/oneformer_ade20k_swin_tiny")
>>> url = (
... "https://huggingface.co/datasets/hf-internal-testing/fixtures_ade20k/resolve/main/ADE_val_00000001.jpg"
... )
>>> image = Image.open(requests.get(url, stream=True).raw)
>>> # Semantic Segmentation
>>> inputs = processor(image, ["semantic"], 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 semantic postprocessing
>>> predicted_semantic_map = processor.post_process_semantic_segmentation(
... outputs, target_sizes=[image.size[::-1]]
... )[0]
>>> f"π Semantic Predictions Shape: {list(predicted_semantic_map.shape)}"
'π Semantic Predictions Shape: [512, 683]'
>>> # Instance Segmentation
>>> inputs = processor(image, ["instance"], 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 instance postprocessing
>>> predicted_instance_map = processor.post_process_instance_segmentation(
... outputs, target_sizes=[image.size[::-1]]
... )[0]["segmentation"]
>>> f"π Instance Predictions Shape: {list(predicted_instance_map.shape)}"
'π Instance Predictions Shape: [512, 683]'
>>> # Panoptic Segmentation
>>> inputs = processor(image, ["panoptic"], 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 panoptic postprocessing
>>> predicted_panoptic_map = processor.post_process_panoptic_segmentation(
... outputs, target_sizes=[image.size[::-1]]
... )[0]["segmentation"]
>>> f"π Panoptic Predictions Shape: {list(predicted_panoptic_map.shape)}"
'π Panoptic Predictions Shape: [512, 683]'