Buckets:
CLIPSeg
Overview
The CLIPSeg model was proposed in Image Segmentation Using Text and Image Prompts by Timo Lüddecke and Alexander Ecker. CLIPSeg adds a minimal decoder on top of a frozen CLIP model for zero-shot and one-shot image segmentation.
The abstract from the paper is the following:
Image segmentation is usually addressed by training a model for a fixed set of object classes. Incorporating additional classes or more complex queries later is expensive as it requires re-training the model on a dataset that encompasses these expressions. Here we propose a system that can generate image segmentations based on arbitrary prompts at test time. A prompt can be either a text or an image. This approach enables us to create a unified model (trained once) for three common segmentation tasks, which come with distinct challenges: referring expression segmentation, zero-shot segmentation and one-shot segmentation. We build upon the CLIP model as a backbone which we extend with a transformer-based decoder that enables dense prediction. After training on an extended version of the PhraseCut dataset, our system generates a binary segmentation map for an image based on a free-text prompt or on an additional image expressing the query. We analyze different variants of the latter image-based prompts in detail. This novel hybrid input allows for dynamic adaptation not only to the three segmentation tasks mentioned above, but to any binary segmentation task where a text or image query can be formulated. Finally, we find our system to adapt well to generalized queries involving affordances or properties

CLIPSeg overview. Taken from the original paper.
This model was contributed by nielsr. The original code can be found here.
Usage tips
- CLIPSegForImageSegmentation adds a decoder on top of CLIPSegModel. The latter is identical to CLIPModel.
- CLIPSegForImageSegmentation can generate image segmentations based on arbitrary prompts at test time. A prompt can be either a text
(provided to the model as
input_ids) or an image (provided to the model asconditional_pixel_values). One can also provide custom conditional embeddings (provided to the model asconditional_embeddings).
Resources
A list of official Hugging Face and community (indicated by 🌎) resources to help you get started with CLIPSeg. 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.
- A notebook that illustrates zero-shot image segmentation with CLIPSeg.
CLIPSegConfig[[transformers.CLIPSegConfig]]
transformers.CLIPSegConfig[[transformers.CLIPSegConfig]]
This is the configuration class to store the configuration of a CLIPSegModel. It is used to instantiate a Clipseg 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 CIDAS/clipseg-rd64
Configuration objects inherit from PreTrainedConfig and can be used to control the model outputs. Read the documentation from PreTrainedConfig for more information.
Example:
>>> from transformers import CLIPSegConfig, CLIPSegModel
>>> # Initializing a CLIPSegConfig with CIDAS/clipseg-rd64 style configuration
>>> configuration = CLIPSegConfig()
>>> # Initializing a CLIPSegModel (with random weights) from the CIDAS/clipseg-rd64 style configuration
>>> model = CLIPSegModel(configuration)
>>> # Accessing the model configuration
>>> configuration = model.config
>>> # We can also initialize a CLIPSegConfig from a CLIPSegTextConfig and a CLIPSegVisionConfig
>>> # Initializing a CLIPSegText and CLIPSegVision configuration
>>> config_text = CLIPSegTextConfig()
>>> config_vision = CLIPSegVisionConfig()
>>> config = CLIPSegConfig(text_config=config_text, vision_config=config_vision)
Parameters:
text_config (Union[dict, ~models.clipseg.configuration_clipseg.CLIPSegTextConfig], optional) : The config object or dictionary of the text backbone.
vision_config (Union[dict, ~models.clipseg.configuration_clipseg.CLIPSegVisionConfig], optional) : The config object or dictionary of the vision backbone.
projection_dim (int, optional, defaults to 512) : Dimensionality of text and vision projection layers.
logit_scale_init_value (Union[float, int], optional, defaults to 2.6592) : The initial value of the logit_scale parameter.
initializer_factor (float, optional, defaults to 1.0) : A factor for initializing all weight matrices (should be kept to 1, used internally for initialization testing).
extract_layers (list[int], optional, defaults to [3, 6, 9]) : Layers to extract when forwarding the query image through the frozen visual backbone of CLIP.
reduce_dim (int, optional, defaults to 64) : Dimensionality to reduce the CLIP vision embedding.
decoder_num_attention_heads (int, optional, defaults to 4) : Number of attention heads for each attention layer in the Transformer decoder.
decoder_attention_dropout (Union[float, int], optional, defaults to 0.0) : The dropout ratio for the attention probabilities.
decoder_hidden_act (str, optional, defaults to quick_gelu) : The non-linear activation function (function or string) in the decoder. For example, "gelu", "relu", "silu", etc.
decoder_intermediate_size (int, optional, defaults to 2048) : Dimension of the MLP representations.
conditional_layer (int, optional, defaults to 0) : The layer to use of the Transformer encoder whose activations will be combined with the condition embeddings using FiLM (Feature-wise Linear Modulation). If 0, the last layer is used.
use_complex_transposed_convolution (bool, optional, defaults to False) : Whether to use a more complex transposed convolution in the decoder, enabling more fine-grained segmentation..
CLIPSegTextConfig[[transformers.CLIPSegTextConfig]]
transformers.CLIPSegTextConfig[[transformers.CLIPSegTextConfig]]
This is the configuration class to store the configuration of a CLIPSegModel. It is used to instantiate a Clipseg 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 CIDAS/clipseg-rd64
Configuration objects inherit from PreTrainedConfig and can be used to control the model outputs. Read the documentation from PreTrainedConfig for more information.
Example:
>>> from transformers import CLIPSegTextConfig, CLIPSegTextModel
>>> # Initializing a CLIPSegTextConfig with CIDAS/clipseg-rd64 style configuration
>>> configuration = CLIPSegTextConfig()
>>> # Initializing a CLIPSegTextModel (with random weights) from the CIDAS/clipseg-rd64 style configuration
>>> model = CLIPSegTextModel(configuration)
>>> # Accessing the model configuration
>>> configuration = model.config
Parameters:
vocab_size (int, optional, defaults to 49408) : Vocabulary size of the model. Defines the number of different tokens that can be represented by the input_ids.
hidden_size (int, optional, defaults to 512) : Dimension of the hidden representations.
intermediate_size (int, optional, defaults to 2048) : Dimension of the MLP representations.
num_hidden_layers (int, optional, defaults to 12) : Number of hidden layers in the Transformer decoder.
num_attention_heads (int, optional, defaults to 8) : Number of attention heads for each attention layer in the Transformer decoder.
max_position_embeddings (int, optional, defaults to 77) : The maximum sequence length that this model might ever be used with.
hidden_act (str, optional, defaults to quick_gelu) : The non-linear activation function (function or string) in the decoder. For example, "gelu", "relu", "silu", etc.
layer_norm_eps (float, optional, defaults to 1e-05) : The epsilon used by the layer normalization layers.
attention_dropout (Union[int, float], optional, defaults to 0.0) : The dropout ratio for the attention probabilities.
initializer_range (float, optional, defaults to 0.02) : The standard deviation of the truncated_normal_initializer for initializing all weight matrices.
initializer_factor (float, optional, defaults to 1.0) : A factor for initializing all weight matrices (should be kept to 1, used internally for initialization testing).
pad_token_id (int, optional, defaults to 1) : Token id used for padding in the vocabulary.
bos_token_id (int, optional, defaults to 49406) : Token id used for beginning-of-stream in the vocabulary.
eos_token_id (Union[int, list[int]], optional, defaults to 49407) : Token id used for end-of-stream in the vocabulary.
CLIPSegVisionConfig[[transformers.CLIPSegVisionConfig]]
transformers.CLIPSegVisionConfig[[transformers.CLIPSegVisionConfig]]
This is the configuration class to store the configuration of a CLIPSegModel. It is used to instantiate a Clipseg 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 CIDAS/clipseg-rd64
Configuration objects inherit from PreTrainedConfig and can be used to control the model outputs. Read the documentation from PreTrainedConfig for more information.
Example:
>>> from transformers import CLIPSegVisionConfig, CLIPSegVisionModel
>>> # Initializing a CLIPSegVisionConfig with CIDAS/clipseg-rd64 style configuration
>>> configuration = CLIPSegVisionConfig()
>>> # Initializing a CLIPSegVisionModel (with random weights) from the CIDAS/clipseg-rd64 style configuration
>>> model = CLIPSegVisionModel(configuration)
>>> # Accessing the model configuration
>>> configuration = model.config
Parameters:
hidden_size (int, optional, defaults to 768) : Dimension of the hidden representations.
intermediate_size (int, optional, defaults to 3072) : Dimension of the MLP representations.
num_hidden_layers (int, optional, defaults to 12) : Number of hidden layers in the Transformer decoder.
num_attention_heads (int, optional, defaults to 12) : Number of attention heads for each attention layer in the Transformer decoder.
num_channels (int, optional, defaults to 3) : The number of input channels.
image_size (Union[int, list[int], tuple[int, int]], optional, defaults to 224) : The size (resolution) of each image.
patch_size (Union[int, list[int], tuple[int, int]], optional, defaults to 32) : The size (resolution) of each patch.
hidden_act (str, optional, defaults to quick_gelu) : The non-linear activation function (function or string) in the decoder. For example, "gelu", "relu", "silu", etc.
layer_norm_eps (float, optional, defaults to 1e-05) : The epsilon used by the layer normalization layers.
attention_dropout (Union[int, float], optional, defaults to 0.0) : The dropout ratio for the attention probabilities.
initializer_range (float, optional, defaults to 0.02) : The standard deviation of the truncated_normal_initializer for initializing all weight matrices.
initializer_factor (float, optional, defaults to 1.0) : A factor for initializing all weight matrices (should be kept to 1, used internally for initialization testing).
CLIPSegProcessor[[transformers.CLIPSegProcessor]]
transformers.CLIPSegProcessor[[transformers.CLIPSegProcessor]]
Constructs a CLIPSegProcessor which wraps a image processor and a tokenizer into a single processor.
CLIPSegProcessor offers all the functionalities of ViTImageProcessor and CLIPTokenizer. See the ~ViTImageProcessor and ~CLIPTokenizer for more information.
__call__transformers.CLIPSegProcessor.__call__https://github.com/huggingface/transformers/blob/main/src/transformers/models/clipseg/processing_clipseg.py#L28[{"name": "text", "val": " = None"}, {"name": "images", "val": " = None"}, {"name": "visual_prompt", "val": " = None"}, {"name": "return_tensors", "val": " = None"}, {"name": "**kwargs", "val": ""}]- text (``) --
The sequence or batch of sequences to be encoded. Each sequence can be a string or a list of strings
(pretokenized string). If you pass a pretokenized input, set is_split_into_words=True to avoid ambiguity with batched inputs.
images (``) -- Image to preprocess. Expects a single or batch of images with pixel values ranging from 0 to 255. If passing in images with pixel values between 0 and 1, set
do_rescale=False.visual_prompt (
PIL.Image.Image,np.ndarray,torch.Tensor,list[PIL.Image.Image],list[np.ndarray],list[torch.Tensor]) -- The visual prompt image or batch of images to be prepared. Each visual prompt image can be a PIL image, NumPy array or PyTorch tensor. In case of a NumPy array/PyTorch tensor, each image should be of shape (C, H, W), where C is a number of channels, H and W are image height and width.return_tensors (``) -- If set, will return tensors of a particular framework. Acceptable values are:
'pt': Return PyTorchtorch.Tensorobjects.'np': Return NumPynp.ndarrayobjects.0BatchEncodingA BatchEncoding with the following fields:
input_ids -- List of token ids to be fed to a model. Returned when
textis notNone.attention_mask -- List of indices specifying which tokens should be attended to by the model (when
return_attention_mask=Trueor if "attention_mask" is inself.model_input_namesand iftextis notNone).pixel_values -- Pixel values to be fed to a model. Returned when
imagesis notNone.
Parameters:
image_processor (ViTImageProcessor) : The image processor is a required input.
tokenizer (CLIPTokenizer) : The tokenizer is a required input.
Returns:
[BatchEncoding](/docs/transformers/main/en/main_classes/tokenizer#transformers.BatchEncoding)
A BatchEncoding with the following fields:
- input_ids -- List of token ids to be fed to a model. Returned when
textis notNone. - attention_mask -- List of indices specifying which tokens should be attended to by the model (when
return_attention_mask=Trueor if "attention_mask" is inself.model_input_namesand iftextis notNone). - pixel_values -- Pixel values to be fed to a model. Returned when
imagesis notNone.
CLIPSegModel[[transformers.CLIPSegModel]]
transformers.CLIPSegModel[[transformers.CLIPSegModel]]
The bare Clipseg Model outputting raw hidden-states without any specific head on top.
This model inherits from PreTrainedModel. Check the superclass documentation for the generic methods the library implements for all its model (such as downloading or saving, resizing the input embeddings, pruning heads etc.)
This model is also a PyTorch torch.nn.Module subclass. Use it as a regular PyTorch Module and refer to the PyTorch documentation for all matter related to general usage and behavior.
forwardtransformers.CLIPSegModel.forwardhttps://github.com/huggingface/transformers/blob/main/src/transformers/models/clipseg/modeling_clipseg.py#L883[{"name": "input_ids", "val": ": torch.LongTensor | None = None"}, {"name": "pixel_values", "val": ": torch.FloatTensor | None = None"}, {"name": "attention_mask", "val": ": torch.Tensor | None = None"}, {"name": "position_ids", "val": ": torch.LongTensor | None = None"}, {"name": "return_loss", "val": ": bool | None = None"}, {"name": "interpolate_pos_encoding", "val": ": bool = True"}, {"name": "**kwargs", "val": ": typing_extensions.Unpack[transformers.utils.generic.TransformersKwargs]"}]- input_ids (torch.LongTensor of shape (batch_size, sequence_length), optional) --
Indices of input sequence tokens in the vocabulary. Padding will be ignored by default.
Indices can be obtained using AutoTokenizer. See PreTrainedTokenizer.encode() and PreTrainedTokenizer.call() for details.
pixel_values (
torch.FloatTensorof shape(batch_size, num_channels, image_size, image_size), optional) -- The tensors corresponding to the input images. Pixel values can be obtained using ViTImageProcessor. SeeViTImageProcessor.__call__()for details (CLIPSegProcessor uses ViTImageProcessor for processing images).attention_mask (
torch.Tensorof shape(batch_size, sequence_length), optional) -- Mask to avoid performing attention on padding token indices. Mask values selected in[0, 1]:- 1 for tokens that are not masked,
- 0 for tokens that are masked.
position_ids (
torch.LongTensorof shape(batch_size, sequence_length), optional) -- Indices of positions of each input sequence tokens in the position embeddings. Selected in the range[0, config.n_positions - 1].return_loss (
bool, optional) -- Whether or not to return the contrastive loss.interpolate_pos_encoding (
bool, optional, defaults toTrue) -- Whether to interpolate the pre-trained position encodings.0CLIPSegOutputortuple(torch.FloatTensor)ACLIPSegOutputor a tuple oftorch.FloatTensor(ifreturn_dict=Falseis passed or whenconfig.return_dict=False) comprising various elements depending on the configuration (CLIPSegConfig) and inputs. The CLIPSegModel 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.
- loss (
torch.FloatTensorof shape(1,), optional, returned whenreturn_lossisTrue) -- Contrastive loss for image-text similarity. - logits_per_image (
torch.FloatTensorof shape(image_batch_size, text_batch_size)) -- The scaled dot product scores betweenimage_embedsandtext_embeds. This represents the image-text similarity scores. - logits_per_text (
torch.FloatTensorof shape(text_batch_size, image_batch_size)) -- The scaled dot product scores betweentext_embedsandimage_embeds. This represents the text-image similarity scores. - text_embeds (
torch.FloatTensorof shape(batch_size, output_dim) -- The text embeddings obtained by applying the projection layer to the pooled output of CLIPSegTextModel. - image_embeds (
torch.FloatTensorof shape(batch_size, output_dim) -- The image embeddings obtained by applying the projection layer to the pooled output of CLIPSegVisionModel. - text_model_output (
~modeling_outputs.BaseModelOutputWithPooling, defaults toNone) -- The output of the CLIPSegTextModel. - vision_model_output (
~modeling_outputs.BaseModelOutputWithPooling, defaults toNone) -- The output of the CLIPSegVisionModel.
Examples:
>>> import torch
>>> from transformers import AutoProcessor, CLIPSegModel
>>> from transformers.image_utils import load_image
>>> processor = AutoProcessor.from_pretrained("CIDAS/clipseg-rd64-refined")
>>> model = CLIPSegModel.from_pretrained("CIDAS/clipseg-rd64-refined")
>>> url = "http://images.cocodataset.org/val2017/000000039769.jpg"
>>> image = load_image(url)
>>> inputs = processor(
... text=["a photo of a cat", "a photo of a dog"], images=image, return_tensors="pt", padding=True
... )
>>> with torch.inference_mode():
... outputs = model(**inputs)
>>> logits_per_image = outputs.logits_per_image # this is the image-text similarity score
>>> probs = logits_per_image.softmax(dim=1) # we can take the softmax to get the label probabilities
Parameters:
config (CLIPSegConfig) : 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.
Returns:
CLIPSegOutput` or `tuple(torch.FloatTensor)
A CLIPSegOutput 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 (CLIPSegConfig) and inputs.
get_text_features[[transformers.CLIPSegModel.get_text_features]]
last_hidden_state (
torch.FloatTensorof shape(batch_size, sequence_length, hidden_size)) -- Sequence of hidden-states at the output of the last layer of the model.pooler_output (
torch.FloatTensorof shape(batch_size, hidden_size)) -- Last layer hidden-state of the first token of the sequence (classification token) after further processing through the layers used for the auxiliary pretraining task. E.g. for BERT-family of models, this returns the classification token after processing through a linear layer and a tanh activation function. The linear layer weights are trained from the next sentence prediction (classification) objective during pretraining.hidden_states (
tuple(torch.FloatTensor), optional, returned whenoutput_hidden_states=Trueis passed or whenconfig.output_hidden_states=True) -- Tuple oftorch.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, sequence_length, 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 whenoutput_attentions=Trueis passed or whenconfig.output_attentions=True) -- Tuple oftorch.FloatTensor(one for each layer) of shape(batch_size, num_heads, sequence_length, sequence_length).Attentions weights after the attention softmax, used to compute the weighted average in the self-attention heads.
Examples:
>>> import torch
>>> from transformers import AutoTokenizer, CLIPSegModel
>>> tokenizer = AutoTokenizer.from_pretrained("CIDAS/clipseg-rd64-refined")
>>> model = CLIPSegModel.from_pretrained("CIDAS/clipseg-rd64-refined")
>>> inputs = tokenizer(["a photo of a cat", "a photo of a dog"], padding=True, return_tensors="pt")
>>> with torch.inference_mode():
... text_features = model.get_text_features(**inputs)
Parameters:
input_ids (torch.Tensor of shape (batch_size, sequence_length)) : Indices of input sequence tokens in the vocabulary. Padding will be ignored by default. Indices can be obtained using AutoTokenizer. See PreTrainedTokenizer.encode() and PreTrainedTokenizer.call() for details. What are input IDs?
attention_mask (torch.Tensor of shape (batch_size, sequence_length), optional) : Mask to avoid performing attention on padding token indices. Mask values selected in [0, 1]: - 1 for tokens that are not masked, - 0 for tokens that are masked. What are attention masks?
position_ids (torch.Tensor of shape (batch_size, sequence_length), optional) : Indices of positions of each input sequence tokens in the position embeddings. Selected in the range [0, config.n_positions - 1]. What are position IDs?
Returns:
[BaseModelOutputWithPooling](/docs/transformers/main/en/main_classes/output#transformers.modeling_outputs.BaseModelOutputWithPooling) or tuple(torch.FloatTensor)``
A BaseModelOutputWithPooling 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 (CLIPSegConfig) and inputs.
get_image_features[[transformers.CLIPSegModel.get_image_features]]
last_hidden_state (
torch.FloatTensorof shape(batch_size, sequence_length, hidden_size)) -- Sequence of hidden-states at the output of the last layer of the model.pooler_output (
torch.FloatTensorof shape(batch_size, hidden_size)) -- Last layer hidden-state of the first token of the sequence (classification token) after further processing through the layers used for the auxiliary pretraining task. E.g. for BERT-family of models, this returns the classification token after processing through a linear layer and a tanh activation function. The linear layer weights are trained from the next sentence prediction (classification) objective during pretraining.hidden_states (
tuple(torch.FloatTensor), optional, returned whenoutput_hidden_states=Trueis passed or whenconfig.output_hidden_states=True) -- Tuple oftorch.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, sequence_length, 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 whenoutput_attentions=Trueis passed or whenconfig.output_attentions=True) -- Tuple oftorch.FloatTensor(one for each layer) of shape(batch_size, num_heads, sequence_length, sequence_length).Attentions weights after the attention softmax, used to compute the weighted average in the self-attention heads.
Examples:
>>> import torch
>>> from transformers import AutoProcessor, CLIPSegModel
>>> from transformers.image_utils import load_image
>>> processor = AutoProcessor.from_pretrained("CIDAS/clipseg-rd64-refined")
>>> model = CLIPSegModel.from_pretrained("CIDAS/clipseg-rd64-refined")
>>> url = "http://images.cocodataset.org/val2017/000000039769.jpg"
>>> image = load_image(url)
>>> inputs = processor(images=image, return_tensors="pt")
>>> with torch.inference_mode():
... image_features = model.get_image_features(**inputs)
Parameters:
pixel_values (torch.FloatTensor of shape (batch_size, num_channels, image_size, image_size)) : The tensors corresponding to the input images. Pixel values can be obtained using ViTImageProcessor. See ViTImageProcessor.__call__() for details (CLIPSegProcessor uses ViTImageProcessor for processing images).
interpolate_pos_encoding (bool, optional, defaults to True) : Whether to interpolate the pre-trained position encodings.
Returns:
[BaseModelOutputWithPooling](/docs/transformers/main/en/main_classes/output#transformers.modeling_outputs.BaseModelOutputWithPooling) or tuple(torch.FloatTensor)``
A BaseModelOutputWithPooling 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 (CLIPSegConfig) and inputs.
CLIPSegTextModel[[transformers.CLIPSegTextModel]]
transformers.CLIPSegTextModel[[transformers.CLIPSegTextModel]]
The text model from CLIPSEG without any head or projection on top.
This model inherits from PreTrainedModel. Check the superclass documentation for the generic methods the library implements for all its model (such as downloading or saving, resizing the input embeddings, pruning heads etc.)
This model is also a PyTorch torch.nn.Module subclass. Use it as a regular PyTorch Module and refer to the PyTorch documentation for all matter related to general usage and behavior.
forwardtransformers.CLIPSegTextModel.forwardhttps://github.com/huggingface/transformers/blob/main/src/transformers/models/clipseg/modeling_clipseg.py#L616[{"name": "input_ids", "val": ": torch.Tensor | None = None"}, {"name": "attention_mask", "val": ": torch.Tensor | None = None"}, {"name": "position_ids", "val": ": torch.Tensor | None = None"}, {"name": "**kwargs", "val": ": typing_extensions.Unpack[transformers.utils.generic.TransformersKwargs]"}]- input_ids (torch.Tensor of shape (batch_size, sequence_length), optional) --
Indices of input sequence tokens in the vocabulary. Padding will be ignored by default.
Indices can be obtained using AutoTokenizer. See PreTrainedTokenizer.encode() and PreTrainedTokenizer.call() for details.
attention_mask (
torch.Tensorof shape(batch_size, sequence_length), optional) -- Mask to avoid performing attention on padding token indices. Mask values selected in[0, 1]:- 1 for tokens that are not masked,
- 0 for tokens that are masked.
position_ids (
torch.Tensorof shape(batch_size, sequence_length), optional) -- Indices of positions of each input sequence tokens in the position embeddings. Selected in the range[0, config.n_positions - 1].What are position IDs?0BaseModelOutputWithPooling or
tuple(torch.FloatTensor)A BaseModelOutputWithPooling 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 (CLIPSegConfig) and inputs.
The CLIPSegTextModel 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.
last_hidden_state (
torch.FloatTensorof shape(batch_size, sequence_length, hidden_size)) -- Sequence of hidden-states at the output of the last layer of the model.pooler_output (
torch.FloatTensorof shape(batch_size, hidden_size)) -- Last layer hidden-state of the first token of the sequence (classification token) after further processing through the layers used for the auxiliary pretraining task. E.g. for BERT-family of models, this returns the classification token after processing through a linear layer and a tanh activation function. The linear layer weights are trained from the next sentence prediction (classification) objective during pretraining.hidden_states (
tuple(torch.FloatTensor), optional, returned whenoutput_hidden_states=Trueis passed or whenconfig.output_hidden_states=True) -- Tuple oftorch.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, sequence_length, 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 whenoutput_attentions=Trueis passed or whenconfig.output_attentions=True) -- Tuple oftorch.FloatTensor(one for each layer) of shape(batch_size, num_heads, sequence_length, sequence_length).Attentions weights after the attention softmax, used to compute the weighted average in the self-attention heads.
Examples:
>>> from transformers import AutoTokenizer, CLIPSegTextModel
>>> tokenizer = AutoTokenizer.from_pretrained("CIDAS/clipseg-rd64-refined")
>>> model = CLIPSegTextModel.from_pretrained("CIDAS/clipseg-rd64-refined")
>>> inputs = tokenizer(["a photo of a cat", "a photo of a dog"], padding=True, return_tensors="pt")
>>> outputs = model(**inputs)
>>> last_hidden_state = outputs.last_hidden_state
>>> pooled_output = outputs.pooler_output # pooled (EOS token) states
Parameters:
config (CLIPSegTextConfig) : 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.
Returns:
[BaseModelOutputWithPooling](/docs/transformers/main/en/main_classes/output#transformers.modeling_outputs.BaseModelOutputWithPooling) or tuple(torch.FloatTensor)``
A BaseModelOutputWithPooling 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 (CLIPSegConfig) and inputs.
CLIPSegVisionModel[[transformers.CLIPSegVisionModel]]
transformers.CLIPSegVisionModel[[transformers.CLIPSegVisionModel]]
The vision model from CLIPSEG without any head or projection on top.
This model inherits from PreTrainedModel. Check the superclass documentation for the generic methods the library implements for all its model (such as downloading or saving, resizing the input embeddings, pruning heads etc.)
This model is also a PyTorch torch.nn.Module subclass. Use it as a regular PyTorch Module and refer to the PyTorch documentation for all matter related to general usage and behavior.
forwardtransformers.CLIPSegVisionModel.forwardhttps://github.com/huggingface/transformers/blob/main/src/transformers/models/clipseg/modeling_clipseg.py#L716[{"name": "pixel_values", "val": ": torch.FloatTensor | None"}, {"name": "interpolate_pos_encoding", "val": ": bool | None = True"}, {"name": "**kwargs", "val": ": typing_extensions.Unpack[transformers.utils.generic.TransformersKwargs]"}]- pixel_values (torch.FloatTensor of shape (batch_size, num_channels, image_size, image_size), optional) --
The tensors corresponding to the input images. Pixel values can be obtained using
ViTImageProcessor. See ViTImageProcessor.__call__() for details (CLIPSegProcessor uses
ViTImageProcessor for processing images).
- interpolate_pos_encoding (
bool, optional, defaults toTrue) -- Whether to interpolate the pre-trained position encodings.0BaseModelOutputWithPooling ortuple(torch.FloatTensor)A BaseModelOutputWithPooling or a tuple oftorch.FloatTensor(ifreturn_dict=Falseis passed or whenconfig.return_dict=False) comprising various elements depending on the configuration (CLIPSegConfig) and inputs. The CLIPSegVisionModel 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.
last_hidden_state (
torch.FloatTensorof shape(batch_size, sequence_length, hidden_size)) -- Sequence of hidden-states at the output of the last layer of the model.pooler_output (
torch.FloatTensorof shape(batch_size, hidden_size)) -- Last layer hidden-state of the first token of the sequence (classification token) after further processing through the layers used for the auxiliary pretraining task. E.g. for BERT-family of models, this returns the classification token after processing through a linear layer and a tanh activation function. The linear layer weights are trained from the next sentence prediction (classification) objective during pretraining.hidden_states (
tuple(torch.FloatTensor), optional, returned whenoutput_hidden_states=Trueis passed or whenconfig.output_hidden_states=True) -- Tuple oftorch.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, sequence_length, 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 whenoutput_attentions=Trueis passed or whenconfig.output_attentions=True) -- Tuple oftorch.FloatTensor(one for each layer) of shape(batch_size, num_heads, sequence_length, sequence_length).Attentions weights after the attention softmax, used to compute the weighted average in the self-attention heads.
Examples:
>>> import httpx
>>> from io import BytesIO
>>> from PIL import Image
>>> from transformers import AutoProcessor, CLIPSegVisionModel
>>> processor = AutoProcessor.from_pretrained("CIDAS/clipseg-rd64-refined")
>>> model = CLIPSegVisionModel.from_pretrained("CIDAS/clipseg-rd64-refined")
>>> url = "http://images.cocodataset.org/val2017/000000039769.jpg"
>>> with httpx.stream("GET", url) as response:
... image = Image.open(BytesIO(response.read()))
>>> inputs = processor(images=image, return_tensors="pt")
>>> outputs = model(**inputs)
>>> last_hidden_state = outputs.last_hidden_state
>>> pooled_output = outputs.pooler_output # pooled CLS states
Parameters:
config (CLIPSegVisionConfig) : 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.
Returns:
[BaseModelOutputWithPooling](/docs/transformers/main/en/main_classes/output#transformers.modeling_outputs.BaseModelOutputWithPooling) or tuple(torch.FloatTensor)``
A BaseModelOutputWithPooling 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 (CLIPSegConfig) and inputs.
CLIPSegForImageSegmentation[[transformers.CLIPSegForImageSegmentation]]
transformers.CLIPSegForImageSegmentation[[transformers.CLIPSegForImageSegmentation]]
CLIPSeg model with a Transformer-based decoder on top for zero-shot and one-shot image segmentation.
This model inherits from PreTrainedModel. Check the superclass documentation for the generic methods the library implements for all its model (such as downloading or saving, resizing the input embeddings, pruning heads etc.)
This model is also a PyTorch torch.nn.Module subclass. Use it as a regular PyTorch Module and refer to the PyTorch documentation for all matter related to general usage and behavior.
forwardtransformers.CLIPSegForImageSegmentation.forwardhttps://github.com/huggingface/transformers/blob/main/src/transformers/models/clipseg/modeling_clipseg.py#L1007[{"name": "input_ids", "val": ": torch.FloatTensor | None = None"}, {"name": "pixel_values", "val": ": torch.FloatTensor | None = None"}, {"name": "conditional_pixel_values", "val": ": torch.FloatTensor | None = None"}, {"name": "conditional_embeddings", "val": ": torch.FloatTensor | None = None"}, {"name": "attention_mask", "val": ": torch.Tensor | None = None"}, {"name": "position_ids", "val": ": torch.LongTensor | None = None"}, {"name": "labels", "val": ": torch.LongTensor | None = None"}, {"name": "interpolate_pos_encoding", "val": ": bool = True"}, {"name": "**kwargs", "val": ": typing_extensions.Unpack[transformers.utils.generic.TransformersKwargs]"}]- input_ids (torch.FloatTensor of shape (batch_size, sequence_length), optional) --
Indices of input sequence tokens in the vocabulary. Padding will be ignored by default.
Indices can be obtained using AutoTokenizer. See PreTrainedTokenizer.encode() and PreTrainedTokenizer.call() for details.
pixel_values (
torch.FloatTensorof shape(batch_size, num_channels, image_size, image_size), optional) -- The tensors corresponding to the input images. Pixel values can be obtained using ViTImageProcessor. SeeViTImageProcessor.__call__()for details (CLIPSegProcessor uses ViTImageProcessor for processing images).conditional_pixel_values (
torch.FloatTensor, optional) -- The pixel values of the conditional images.conditional_embeddings (
torch.FloatTensorof shape(batch_size, config.projection_dim), optional) -- The conditional embeddings for the query images. If provided, the model will use this instead of computing the embeddings from the conditional_pixel_values.attention_mask (
torch.Tensorof shape(batch_size, sequence_length), optional) -- Mask to avoid performing attention on padding token indices. Mask values selected in[0, 1]:- 1 for tokens that are not masked,
- 0 for tokens that are masked.
position_ids (
torch.LongTensorof shape(batch_size, sequence_length), optional) -- Indices of positions of each input sequence tokens in the position embeddings. Selected in the range[0, config.n_positions - 1].labels (
torch.LongTensorof shape(batch_size,), optional) -- Labels for computing the sequence classification/regression loss. Indices should be in[0, ..., config.num_labels - 1]. Ifconfig.num_labels == 1a regression loss is computed (Mean-Square loss), Ifconfig.num_labels > 1a classification loss is computed (Cross-Entropy).interpolate_pos_encoding (
bool, optional, defaults toTrue) -- Whether to interpolate the pre-trained position encodings.0CLIPSegOutputortuple(torch.FloatTensor)ACLIPSegOutputor a tuple oftorch.FloatTensor(ifreturn_dict=Falseis passed or whenconfig.return_dict=False) comprising various elements depending on the configuration (CLIPSegConfig) and inputs. The CLIPSegForImageSegmentation 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.
- loss (
torch.FloatTensorof shape(1,), optional, returned whenreturn_lossisTrue) -- Contrastive loss for image-text similarity. - logits_per_image (
torch.FloatTensorof shape(image_batch_size, text_batch_size)) -- The scaled dot product scores betweenimage_embedsandtext_embeds. This represents the image-text similarity scores. - logits_per_text (
torch.FloatTensorof shape(text_batch_size, image_batch_size)) -- The scaled dot product scores betweentext_embedsandimage_embeds. This represents the text-image similarity scores. - text_embeds (
torch.FloatTensorof shape(batch_size, output_dim) -- The text embeddings obtained by applying the projection layer to the pooled output of CLIPSegTextModel. - image_embeds (
torch.FloatTensorof shape(batch_size, output_dim) -- The image embeddings obtained by applying the projection layer to the pooled output of CLIPSegVisionModel. - text_model_output (
~modeling_outputs.BaseModelOutputWithPooling, defaults toNone) -- The output of the CLIPSegTextModel. - vision_model_output (
~modeling_outputs.BaseModelOutputWithPooling, defaults toNone) -- The output of the CLIPSegVisionModel.
Examples:
>>> import torch
>>> from transformers import AutoProcessor, CLIPSegForImageSegmentation
>>> from transformers.image_utils import load_image
>>> processor = AutoProcessor.from_pretrained("CIDAS/clipseg-rd64-refined")
>>> model = CLIPSegForImageSegmentation.from_pretrained("CIDAS/clipseg-rd64-refined")
>>> url = "http://images.cocodataset.org/val2017/000000039769.jpg"
>>> image = load_image(url)
>>> texts = ["a cat", "a remote", "a blanket"]
>>> inputs = processor(text=texts, images=[image] * len(texts), padding=True, return_tensors="pt")
>>> with torch.inference_mode():
... outputs = model(**inputs)
>>> logits = outputs.logits
>>> print(logits.shape)
torch.Size([3, 352, 352])
Parameters:
config (CLIPSegConfig) : 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.
Returns:
CLIPSegOutput` or `tuple(torch.FloatTensor)
A CLIPSegOutput 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 (CLIPSegConfig) and inputs.
Xet Storage Details
- Size:
- 53.1 kB
- Xet hash:
- bbad2388715580315eebefa9ddfac35ef66c43c700397ab9419e6071984ed812
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.