SigLIP (Sigmoid Loss for Language Image Pre-Training)
SigLIP (Sigmoid Loss for Language Image Pre-Training) is a vision-language model that builds upon the principles of CLIP but introduces a key architectural change: it uses a sigmoid loss function instead of the softmax-based contrastive loss. Additionally, there are some slight implementation differences (no attention_mask for the text encoder, padding the text inputs, multihead attention pooling for the vision encoder rather than a linear projection layer).
This modification simplifies the training objective by treating the problem as a binary classification for each image-text pair (i.e., are they a positive or negative match?). This approach avoids the need for a global normalization over all pairs in a batch, which makes it more scalable and robust to noisy, web-scale data.
Key features of SigLIP: 1. Vision Encoder: A Vision Transformer (ViT) with a Multi-Head Attention Pooling (MAP) head. 2. Text Encoder: A standard Transformer model. 3. Sigmoid Loss: Enables training on larger batches and noisier datasets without requiring careful data curation or complex negative sampling strategies.
SigLIP was introduced in the paper "Sigmoid Loss for Language Image Pre-Training" and has demonstrated improved performance and training efficiency.
jimm.models.siglip.SigLIPVisionModel
Bases: Module
Source code in src/jimm/models/siglip/siglip_model.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 114 115 116 117 | |
__call__(image, do_projection=True)
Encode images into embeddings.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
image
|
Float[Array, 'batch height width channels']
|
Batch of input images. |
required |
do_projection
|
bool
|
Included for API compatibility with CLIP. SigLIP vision model doesn't have a projection layer. Defaults to True. |
True
|
Returns:
| Type | Description |
|---|---|
Float[Array, 'batch vision_width']
|
Float[Array, "batch vision_width"]: Image embeddings. |
Source code in src/jimm/models/siglip/siglip_model.py
68 69 70 71 72 73 74 75 76 77 78 | |
__init__(image_resolution, vision_layers, vision_width, vision_patch_size, use_gradient_checkpointing=False, rngs=nnx.Rngs(0), dtype=jnp.float32, param_dtype=jnp.float32, mesh=None, mesh_rules=DEFAULT_SHARDING)
Initialize the SigLIP Vision Encoder.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
image_resolution
|
int
|
The resolution of the input images. |
required |
vision_layers
|
int
|
The number of layers in the vision transformer. |
required |
vision_width
|
int
|
The width of the vision transformer. |
required |
vision_patch_size
|
int
|
The patch size of the vision transformer. |
required |
use_gradient_checkpointing
|
bool
|
Whether to use gradient checkpointing. Defaults to False. |
False
|
rngs
|
Rngs
|
The random number generator state. Defaults to nnx.Rngs(0). |
Rngs(0)
|
dtype
|
DTypeLike
|
The data type for computations. Defaults to jnp.float32. |
float32
|
param_dtype
|
DTypeLike
|
The data type for parameters. Defaults to jnp.float32. |
float32
|
mesh
|
Mesh | None
|
The device mesh for parameter sharding. Defaults to None. |
None
|
mesh_rules
|
MeshRules
|
Logical axis sharding rules. Defaults to DEFAULT_SHARDING. |
DEFAULT_SHARDING
|
Source code in src/jimm/models/siglip/siglip_model.py
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 | |
from_pretrained(model_name_or_path, use_pytorch=False, mesh=None, dtype=jnp.float32, param_dtype=jnp.float32, use_gradient_checkpointing=False, rngs=nnx.Rngs(0))
classmethod
Load a pretrained vision encoder from a SigLIP checkpoint.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model_name_or_path
|
str
|
Path to local weights or HuggingFace model ID. |
required |
use_pytorch
|
bool
|
Whether to load from PyTorch weights. Defaults to False. |
False
|
mesh
|
Mesh | None
|
Optional device mesh for parameter sharding. Defaults to None. |
None
|
dtype
|
DTypeLike
|
Data type for computations. Defaults to jnp.float32. |
float32
|
param_dtype
|
DTypeLike
|
Data type for parameters. Defaults to jnp.float32. |
float32
|
use_gradient_checkpointing
|
bool
|
Whether to use gradient checkpointing. Defaults to False. |
False
|
rngs
|
Rngs
|
Random number generator keys. Defaults to nnx.Rngs(0). |
Rngs(0)
|
Returns:
| Name | Type | Description |
|---|---|---|
SigLIPVisionModel |
SigLIPVisionModel
|
Pretrained SigLIP vision model |
Source code in src/jimm/models/siglip/siglip_model.py
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 | |
save_pretrained(save_directory)
Save model weights and config in HuggingFace format.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
save_directory
|
str
|
Directory path where the model will be saved. |
required |
Source code in src/jimm/models/siglip/siglip_model.py
109 110 111 112 113 114 115 116 117 | |
jimm.models.siglip.SigLIP
Bases: Module
Source code in src/jimm/models/siglip/siglip_model.py
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 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 | |
__call__(image, text)
Calculate similarity between image and text embeddings.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
image
|
Float[Array, 'batch height width channels']
|
Batch of input images. |
required |
text
|
Int[Array, 'batch context_length']
|
Batch of token sequences. |
required |
Returns:
| Type | Description |
|---|---|
Float[Array, 'batch batch']
|
Float[Array, "batch batch"]: Similarity scores between all pairs of images and texts. |
Source code in src/jimm/models/siglip/siglip_model.py
370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 | |
__init__(image_resolution, vision_layers, vision_width, vision_patch_size, context_length, vocab_size, transformer_width, transformer_heads, transformer_layers, use_gradient_checkpointing=False, rngs=nnx.Rngs(0), dtype=jnp.float32, param_dtype=jnp.float32, mesh=None, mesh_rules=DEFAULT_SHARDING)
Initialize the SigLIP model.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
image_resolution
|
int
|
The resolution of the input images. |
required |
vision_layers
|
int
|
The number of layers in the vision transformer. |
required |
vision_width
|
int
|
The width of the vision transformer. |
required |
vision_patch_size
|
int
|
The patch size of the vision transformer. |
required |
context_length
|
int
|
The length of the context. |
required |
vocab_size
|
int
|
The size of the vocabulary. |
required |
transformer_width
|
int
|
The width of the transformer. |
required |
transformer_heads
|
int
|
The number of attention heads in the transformer. |
required |
transformer_layers
|
int
|
The number of layers in the transformer. |
required |
use_gradient_checkpointing
|
bool
|
Whether to use gradient checkpointing. Defaults to False. |
False
|
rngs
|
Rngs
|
The random number generator state. Defaults to nnx.Rngs(0). |
Rngs(0)
|
dtype
|
DTypeLike
|
The data type for computations. Defaults to jnp.float32. |
float32
|
param_dtype
|
DTypeLike
|
The data type for parameters. Defaults to jnp.float32. |
float32
|
mesh
|
Mesh | None
|
Optional device mesh for parameter sharding. Defaults to None. |
None
|
mesh_rules
|
MeshRules
|
Logical axis sharding rules. Defaults to DEFAULT_SHARDING. |
DEFAULT_SHARDING
|
Source code in src/jimm/models/siglip/siglip_model.py
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 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 | |
encode_image(image)
Encode images into embeddings.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
image
|
Float[Array, 'batch height width channels']
|
Batch of input images. |
required |
Returns:
| Type | Description |
|---|---|
Float[Array, 'batch transformer_width']
|
Float[Array, "batch transformer_width"]: Image embeddings. |
Source code in src/jimm/models/siglip/siglip_model.py
348 349 350 351 352 353 354 355 356 357 | |
encode_text(text)
Encode text tokens into embeddings.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
Int[Array, 'batch context_length']
|
Batch of token sequences. |
required |
Returns:
| Type | Description |
|---|---|
Float[Array, 'batch transformer_width']
|
Float[Array, "batch transformer_width"]: Text embeddings. |
Source code in src/jimm/models/siglip/siglip_model.py
359 360 361 362 363 364 365 366 367 368 | |
from_pretrained(model_name_or_path, use_pytorch=False, mesh=None, dtype=jnp.float32, param_dtype=jnp.float32, use_gradient_checkpointing=False, rngs=nnx.Rngs(0))
classmethod
Load a pretrained SigLIP model from a local path or HuggingFace Hub.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model_name_or_path
|
str
|
Path to local weights or HuggingFace model ID. |
required |
use_pytorch
|
bool
|
Whether to load from PyTorch weights. Defaults to False. |
False
|
mesh
|
Mesh | None
|
Optional device mesh for parameter sharding. Defaults to None. |
None
|
dtype
|
DTypeLike
|
Data type for computations. Defaults to jnp.float32. |
float32
|
param_dtype
|
DTypeLike
|
Data type for parameters. Defaults to jnp.float32. |
float32
|
use_gradient_checkpointing
|
bool
|
Whether to use gradient checkpointing. Defaults to False. |
False
|
rngs
|
Rngs
|
Random number generator keys. Defaults to nnx.Rngs(0). |
Rngs(0)
|
Returns:
| Name | Type | Description |
|---|---|---|
SigLIP |
SigLIP
|
Pretrained SigLIP model |
Source code in src/jimm/models/siglip/siglip_model.py
390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 | |
save_pretrained(save_directory)
Save the model weights and config in HuggingFace format.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
save_directory
|
str
|
Directory path where the model will be saved. |
required |
Source code in src/jimm/models/siglip/siglip_model.py
419 420 421 422 423 424 425 426 427 | |