Reference for ultralytics/nn/text_model.py
Note
This file is available at https://github.com/ultralytics/ultralytics/blob/main/ultralytics/nn/text_model.py. If you spot a problem please help fix it by contributing a Pull Request 🛠️. Thank you 🙏!
ultralytics.nn.text_model.TextModel
Bases: Module
Abstract base class for text encoding models.
This class defines the interface for text encoding models used in vision-language tasks. Subclasses must implement the tokenize and encode_text methods.
Methods:
Name | Description |
---|---|
tokenize |
Convert input texts to tokens. |
encode_text |
Encode tokenized texts into feature vectors. |
Source code in ultralytics/nn/text_model.py
encode_text
abstractmethod
ultralytics.nn.text_model.CLIP
Bases: TextModel
Implements OpenAI's CLIP (Contrastive Language-Image Pre-training) text encoder.
This class provides a text encoder based on OpenAI's CLIP model, which can convert text into feature vectors that are aligned with corresponding image features in a shared embedding space.
Attributes:
Name | Type | Description |
---|---|---|
model |
CLIP
|
The loaded CLIP model. |
device |
device
|
Device where the model is loaded. |
Methods:
Name | Description |
---|---|
tokenize |
Convert input texts to CLIP tokens. |
encode_text |
Encode tokenized texts into normalized feature vectors. |
Examples:
>>> from ultralytics.models.sam import CLIP
>>> import torch
>>> device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
>>> clip_model = CLIP(size="ViT-B/32", device=device)
>>> tokens = clip_model.tokenize(["a photo of a cat", "a photo of a dog"])
>>> text_features = clip_model.encode_text(tokens)
>>> print(text_features.shape)
This class implements the TextModel interface using OpenAI's CLIP model for text encoding. It loads a pre-trained CLIP model of the specified size and prepares it for text encoding tasks.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
size
|
str
|
Model size identifier (e.g., 'ViT-B/32'). |
required |
device
|
device
|
Device to load the model on. |
required |
Examples:
>>> import torch
>>> from ultralytics.models.sam.modules.clip import CLIP
>>> clip_model = CLIP("ViT-B/32", device=torch.device("cuda:0"))
>>> text_features = clip_model.encode_text(["a photo of a cat", "a photo of a dog"])
Source code in ultralytics/nn/text_model.py
encode_text
Encode tokenized texts into normalized feature vectors.
This method processes tokenized text inputs through the CLIP model to generate feature vectors, which are then normalized to unit length. These normalized vectors can be used for text-image similarity comparisons.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
texts
|
Tensor
|
Tokenized text inputs, typically created using the tokenize() method. |
required |
dtype
|
dtype
|
Data type for output features. Default is torch.float32. |
float32
|
Returns:
Type | Description |
---|---|
Tensor
|
Normalized text feature vectors with unit length (L2 norm = 1). |
Examples:
>>> clip_model = CLIP("ViT-B/32", device="cuda")
>>> tokens = clip_model.tokenize(["a photo of a cat", "a photo of a dog"])
>>> features = clip_model.encode_text(tokens)
>>> features.shape
torch.Size([2, 512])
Source code in ultralytics/nn/text_model.py
tokenize
Convert input texts to CLIP tokens.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
texts
|
str | List[str]
|
Input text or list of texts to tokenize. |
required |
Returns:
Type | Description |
---|---|
Tensor
|
Tokenized text tensor with shape (batch_size, context_length) ready for model processing. |
Examples:
>>> model = CLIP("ViT-B/32", device="cpu")
>>> tokens = model.tokenize("a photo of a cat")
>>> print(tokens.shape) # torch.Size([1, 77])
Source code in ultralytics/nn/text_model.py
ultralytics.nn.text_model.MobileCLIP
Bases: TextModel
Implement Apple's MobileCLIP text encoder for efficient text encoding.
This class implements the TextModel interface using Apple's MobileCLIP model, providing efficient text encoding capabilities for vision-language tasks.
Attributes:
Name | Type | Description |
---|---|---|
model |
MobileCLIP
|
The loaded MobileCLIP model. |
tokenizer |
callable
|
Tokenizer function for processing text inputs. |
device |
device
|
Device where the model is loaded. |
config_size_map |
dict
|
Mapping from size identifiers to model configuration names. |
Methods:
Name | Description |
---|---|
tokenize |
Convert input texts to MobileCLIP tokens. |
encode_text |
Encode tokenized texts into normalized feature vectors. |
Examples:
>>> device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
>>> text_encoder = MobileCLIP(size="s0", device=device)
>>> tokens = text_encoder.tokenize(["a photo of a cat", "a photo of a dog"])
>>> features = text_encoder.encode_text(tokens)
This class implements the TextModel interface using Apple's MobileCLIP model for efficient text encoding.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
size
|
str
|
Model size identifier (e.g., 's0', 's1', 's2', 'b', 'blt'). |
required |
device
|
device
|
Device to load the model on. |
required |
Examples:
>>> from ultralytics.nn.modules import MobileCLIP
>>> import torch
>>> model = MobileCLIP("s0", device=torch.device("cpu"))
>>> tokens = model.tokenize(["a photo of a cat", "a photo of a dog"])
>>> features = model.encode_text(tokens)
Source code in ultralytics/nn/text_model.py
encode_text
Encode tokenized texts into normalized feature vectors.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
texts
|
Tensor
|
Tokenized text inputs. |
required |
dtype
|
dtype
|
Data type for output features. |
float32
|
Returns:
Type | Description |
---|---|
Tensor
|
Normalized text feature vectors with L2 normalization applied. |
Examples:
>>> model = MobileCLIP("s0", device="cpu")
>>> tokens = model.tokenize(["a photo of a cat", "a photo of a dog"])
>>> features = model.encode_text(tokens)
>>> features.shape
torch.Size([2, 512]) # Actual dimension depends on model size
Source code in ultralytics/nn/text_model.py
tokenize
Convert input texts to MobileCLIP tokens.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
texts
|
list[str]
|
List of text strings to tokenize. |
required |
Returns:
Type | Description |
---|---|
Tensor
|
Tokenized text inputs with shape (batch_size, sequence_length). |
Examples:
>>> model = MobileCLIP("s0", "cpu")
>>> tokens = model.tokenize(["a photo of a cat", "a photo of a dog"])
Source code in ultralytics/nn/text_model.py
ultralytics.nn.text_model.build_text_model
Build a text encoding model based on the specified variant.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
variant
|
str
|
Model variant in format "base:size" (e.g., "clip:ViT-B/32" or "mobileclip:s0"). |
required |
device
|
device
|
Device to load the model on. |
None
|
Returns:
Type | Description |
---|---|
TextModel
|
Instantiated text encoding model. |
Examples:
>>> model = build_text_model("clip:ViT-B/32", device=torch.device("cuda"))
>>> model = build_text_model("mobileclip:s0", device=torch.device("cpu"))