CLIP (Contrastive Language–Image Pre-training)
CLIP (Contrastive Language–Image Pre-training) is a neural network architecture that learns visual concepts from natural language supervision. It is trained on a large dataset of image-text pairs to create a unified vision-language model that can understand both images and text in a shared semantic space.
CLIP consists of two main components: 1. A vision encoder (Vision Transformer) that processes images into visual features 2. A text encoder (Transformer) that processes text into textual features
The model is trained using contrastive learning, where it learns to maximize the cosine similarity between the embeddings of matching image-text pairs while minimizing it for non-matching pairs. This allows CLIP to perform zero-shot classification by comparing image embeddings with text embeddings of potential labels.
CLIP was introduced in the paper "Learning Transferable Visual Models From Natural Language Supervision" and has shown remarkable zero-shot generalization capabilities across a wide range of visual classification tasks. The CLIP model combines a Vision Transformer and a Text Transformer to learn joint representations of images and text. It is trained to maximize the similarity between matching image-text pairs while minimizing similarity between non-matching pairs.
jimm.models.clip.CLIPVisionModel
Bases: Module
Source code in src/jimm/models/clip/clip_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 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 | |
__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
|
Whether to apply the visual projection layer. Defaults to True. |
True
|
Returns:
| Type | Description |
|---|---|
Float[Array, 'batch vision_width_or_transformer_width']
|
Float[Array, "batch vision_width_or_transformer_width"]: Image embeddings. |
Float[Array, 'batch vision_width_or_transformer_width']
|
Shape depends on do_projection: vision_width if False, transformer_width if True. |
Source code in src/jimm/models/clip/clip_model.py
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 | |
__init__(image_resolution, vision_layers, vision_width, vision_patch_size, transformer_width, use_gradient_checkpointing=False, rngs=nnx.Rngs(0), dtype=jnp.float32, param_dtype=jnp.float32, mesh=None, mesh_rules=DEFAULT_SHARDING)
Initialize the Vision Encoder with projection.
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 |
transformer_width
|
int
|
The output dimension after projection. |
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/clip/clip_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 67 68 69 70 71 72 73 74 75 76 77 78 | |
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 CLIP 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 |
|---|---|---|
CLIPVisionModel |
CLIPVisionModel
|
Pretrained CLIP vision model |
Source code in src/jimm/models/clip/clip_model.py
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 | |
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/clip/clip_model.py
125 126 127 128 129 130 131 132 133 | |
jimm.models.clip.CLIP
Bases: Module
Source code in src/jimm/models/clip/clip_model.py
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 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 | |
__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/clip/clip_model.py
390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 | |
__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 CLIP 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
|
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/clip/clip_model.py
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 | |
encode_image(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
|
Whether the image encoder should do the visual projection layer. Defaults to true. |
True
|
Returns:
| Type | Description |
|---|---|
Float[Array, 'batch transformer_width']
|
Float[Array, "batch transformer_width"]: Image embeddings. |
Source code in src/jimm/models/clip/clip_model.py
367 368 369 370 371 372 373 374 375 376 377 | |
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/clip/clip_model.py
379 380 381 382 383 384 385 386 387 388 | |
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 CLIP 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 |
|---|---|---|
CLIP |
CLIP
|
Pretrained CLIP model |
Source code in src/jimm/models/clip/clip_model.py
410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 | |
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/clip/clip_model.py
439 440 441 442 443 444 445 446 447 | |