Reference for ultralytics/models/sam/modules/transformer.py
Note
This file is available at https://github.com/ultralytics/ultralytics/blob/main/ultralytics/models/sam/modules/transformer.py. If you spot a problem please help fix it by contributing a Pull Request 🛠️. Thank you 🙏!
ultralytics.models.sam.modules.transformer.TwoWayTransformer
Bases: Module
A Two-Way Transformer module that enables the simultaneous attention to both image and query points. This class serves as a specialized transformer decoder that attends to an input image using queries whose positional embedding is supplied. This is particularly useful for tasks like object detection, image segmentation, and point cloud processing.
Attributes:
Name | Type | Description |
---|---|---|
depth |
int
|
The number of layers in the transformer. |
embedding_dim |
int
|
The channel dimension for the input embeddings. |
num_heads |
int
|
The number of heads for multihead attention. |
mlp_dim |
int
|
The internal channel dimension for the MLP block. |
layers |
ModuleList
|
The list of TwoWayAttentionBlock layers that make up the transformer. |
final_attn_token_to_image |
Attention
|
The final attention layer applied from the queries to the image. |
norm_final_attn |
LayerNorm
|
The layer normalization applied to the final queries. |
Source code in ultralytics/models/sam/modules/transformer.py
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
|
__init__(depth, embedding_dim, num_heads, mlp_dim, activation=nn.ReLU, attention_downsample_rate=2)
A transformer decoder that attends to an input image using queries whose positional embedding is supplied.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
depth |
int
|
number of layers in the transformer |
required |
embedding_dim |
int
|
the channel dimension for the input embeddings |
required |
num_heads |
int
|
the number of heads for multihead attention. Must divide embedding_dim |
required |
mlp_dim |
int
|
the channel dimension internal to the MLP block |
required |
activation |
Module
|
the activation to use in the MLP block |
ReLU
|
Source code in ultralytics/models/sam/modules/transformer.py
forward(image_embedding, image_pe, point_embedding)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
image_embedding |
Tensor
|
image to attend to. Should be shape B x embedding_dim x h x w for any h and w. |
required |
image_pe |
Tensor
|
the positional encoding to add to the image. Must have same shape as image_embedding. |
required |
point_embedding |
Tensor
|
the embedding to add to the query points. Must have shape B x N_points x embedding_dim for any N_points. |
required |
Returns:
Type | Description |
---|---|
Tensor
|
the processed point_embedding |
Tensor
|
the processed image_embedding |
Source code in ultralytics/models/sam/modules/transformer.py
ultralytics.models.sam.modules.transformer.TwoWayAttentionBlock
Bases: Module
An attention block that performs both self-attention and cross-attention in two directions: queries to keys and keys to queries. This block consists of four main layers: (1) self-attention on sparse inputs, (2) cross-attention of sparse inputs to dense inputs, (3) an MLP block on sparse inputs, and (4) cross-attention of dense inputs to sparse inputs.
Attributes:
Name | Type | Description |
---|---|---|
self_attn |
Attention
|
The self-attention layer for the queries. |
norm1 |
LayerNorm
|
Layer normalization following the first attention block. |
cross_attn_token_to_image |
Attention
|
Cross-attention layer from queries to keys. |
norm2 |
LayerNorm
|
Layer normalization following the second attention block. |
mlp |
MLPBlock
|
MLP block that transforms the query embeddings. |
norm3 |
LayerNorm
|
Layer normalization following the MLP block. |
norm4 |
LayerNorm
|
Layer normalization following the third attention block. |
cross_attn_image_to_token |
Attention
|
Cross-attention layer from keys to queries. |
skip_first_layer_pe |
bool
|
Whether to skip the positional encoding in the first layer. |
Source code in ultralytics/models/sam/modules/transformer.py
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 |
|
__init__(embedding_dim, num_heads, mlp_dim=2048, activation=nn.ReLU, attention_downsample_rate=2, skip_first_layer_pe=False)
A transformer block with four layers: (1) self-attention of sparse inputs, (2) cross attention of sparse inputs to dense inputs, (3) mlp block on sparse inputs, and (4) cross attention of dense inputs to sparse inputs.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
embedding_dim |
int
|
the channel dimension of the embeddings |
required |
num_heads |
int
|
the number of heads in the attention layers |
required |
mlp_dim |
int
|
the hidden dimension of the mlp block |
2048
|
activation |
Module
|
the activation of the mlp block |
ReLU
|
skip_first_layer_pe |
bool
|
skip the PE on the first layer |
False
|
Source code in ultralytics/models/sam/modules/transformer.py
forward(queries, keys, query_pe, key_pe)
Apply self-attention and cross-attention to queries and keys and return the processed embeddings.
Source code in ultralytics/models/sam/modules/transformer.py
ultralytics.models.sam.modules.transformer.Attention
Bases: Module
An attention layer that allows for downscaling the size of the embedding after projection to queries, keys, and values.
Source code in ultralytics/models/sam/modules/transformer.py
__init__(embedding_dim, num_heads, downsample_rate=1)
Initializes the Attention model with the given dimensions and settings.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
embedding_dim |
int
|
The dimensionality of the input embeddings. |
required |
num_heads |
int
|
The number of attention heads. |
required |
downsample_rate |
int
|
The factor by which the internal dimensions are downsampled. Defaults to 1. |
1
|
Raises:
Type | Description |
---|---|
AssertionError
|
If 'num_heads' does not evenly divide the internal dim (embedding_dim / downsample_rate). |
Source code in ultralytics/models/sam/modules/transformer.py
forward(q, k, v)
Compute the attention output given the input query, key, and value tensors.