Reference for ultralytics/models/sam/build.py
Note
This file is available at https://github.com/ultralytics/ultralytics/blob/main/ultralytics/models/sam/build.py. If you spot a problem please help fix it by contributing a Pull Request 🛠️. Thank you 🙏!
ultralytics.models.sam.build.build_sam_vit_h
Builds and returns a Segment Anything Model (SAM) h-size model with specified encoder parameters.
Source code in ultralytics/models/sam/build.py
ultralytics.models.sam.build.build_sam_vit_l
Builds and returns a Segment Anything Model (SAM) l-size model with specified encoder parameters.
Source code in ultralytics/models/sam/build.py
ultralytics.models.sam.build.build_sam_vit_b
Constructs and returns a Segment Anything Model (SAM) with b-size architecture and optional checkpoint.
Source code in ultralytics/models/sam/build.py
ultralytics.models.sam.build.build_mobile_sam
Builds and returns a Mobile Segment Anything Model (Mobile-SAM) for efficient image segmentation.
Source code in ultralytics/models/sam/build.py
ultralytics.models.sam.build.build_sam2_t
Builds and returns a Segment Anything Model 2 (SAM2) tiny-size model with specified architecture parameters.
Source code in ultralytics/models/sam/build.py
ultralytics.models.sam.build.build_sam2_s
Builds and returns a small-size Segment Anything Model (SAM2) with specified architecture parameters.
Source code in ultralytics/models/sam/build.py
ultralytics.models.sam.build.build_sam2_b
Builds and returns a SAM2 base-size model with specified architecture parameters.
Source code in ultralytics/models/sam/build.py
ultralytics.models.sam.build.build_sam2_l
Builds and returns a large-size Segment Anything Model (SAM2) with specified architecture parameters.
Source code in ultralytics/models/sam/build.py
ultralytics.models.sam.build._build_sam
_build_sam(
encoder_embed_dim,
encoder_depth,
encoder_num_heads,
encoder_global_attn_indexes,
checkpoint=None,
mobile_sam=False,
)
Builds a Segment Anything Model (SAM) with specified encoder parameters.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
encoder_embed_dim
|
int | List[int]
|
Embedding dimension for the encoder. |
required |
encoder_depth
|
int | List[int]
|
Depth of the encoder. |
required |
encoder_num_heads
|
int | List[int]
|
Number of attention heads in the encoder. |
required |
encoder_global_attn_indexes
|
List[int] | None
|
Indexes for global attention in the encoder. |
required |
checkpoint
|
str | None
|
Path to the model checkpoint file. |
None
|
mobile_sam
|
bool
|
Whether to build a Mobile-SAM model. |
False
|
Returns:
Type | Description |
---|---|
SAMModel
|
A Segment Anything Model instance with the specified architecture. |
Examples:
>>> sam = _build_sam(768, 12, 12, [2, 5, 8, 11])
>>> sam = _build_sam([64, 128, 160, 320], [2, 2, 6, 2], [2, 4, 5, 10], None, mobile_sam=True)
Source code in ultralytics/models/sam/build.py
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 203 204 205 206 207 208 209 210 211 212 213 |
|
ultralytics.models.sam.build._build_sam2
_build_sam2(
encoder_embed_dim=1280,
encoder_stages=[2, 6, 36, 4],
encoder_num_heads=2,
encoder_global_att_blocks=[7, 15, 23, 31],
encoder_backbone_channel_list=[1152, 576, 288, 144],
encoder_window_spatial_size=[7, 7],
encoder_window_spec=[8, 4, 16, 8],
checkpoint=None,
)
Builds and returns a Segment Anything Model 2 (SAM2) with specified architecture parameters.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
encoder_embed_dim
|
int
|
Embedding dimension for the encoder. |
1280
|
encoder_stages
|
List[int]
|
Number of blocks in each stage of the encoder. |
[2, 6, 36, 4]
|
encoder_num_heads
|
int
|
Number of attention heads in the encoder. |
2
|
encoder_global_att_blocks
|
List[int]
|
Indices of global attention blocks in the encoder. |
[7, 15, 23, 31]
|
encoder_backbone_channel_list
|
List[int]
|
Channel dimensions for each level of the encoder backbone. |
[1152, 576, 288, 144]
|
encoder_window_spatial_size
|
List[int]
|
Spatial size of the window for position embeddings. |
[7, 7]
|
encoder_window_spec
|
List[int]
|
Window specifications for each stage of the encoder. |
[8, 4, 16, 8]
|
checkpoint
|
str | None
|
Path to the checkpoint file for loading pre-trained weights. |
None
|
Returns:
Type | Description |
---|---|
SAM2Model
|
A configured and initialized SAM2 model. |
Examples:
>>> sam2_model = _build_sam2(encoder_embed_dim=96, encoder_stages=[1, 2, 7, 2])
>>> sam2_model.eval()
Source code in ultralytics/models/sam/build.py
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 |
|
ultralytics.models.sam.build.build_sam
Builds and returns a Segment Anything Model (SAM) based on the provided checkpoint.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
ckpt
|
str | Path
|
Path to the checkpoint file or name of a pre-defined SAM model. |
'sam_b.pt'
|
Returns:
Type | Description |
---|---|
SAMModel | SAM2Model
|
A configured and initialized SAM or SAM2 model instance. |
Raises:
Type | Description |
---|---|
FileNotFoundError
|
If the provided checkpoint is not a supported SAM model. |
Examples:
Notes
Supported pre-defined models include: - SAM: 'sam_h.pt', 'sam_l.pt', 'sam_b.pt', 'mobile_sam.pt' - SAM2: 'sam2_t.pt', 'sam2_s.pt', 'sam2_b.pt', 'sam2_l.pt'