Skip to content

Commit 27a25be

Browse files
chore: update model card for SigLIP (#37585)
* edit siglip model card * fix syntax * Update docs/source/en/model_doc/siglip.md Co-authored-by: Steven Liu <59462357+stevhliu@users.noreply.github.com> * Update docs/source/en/model_doc/siglip.md Co-authored-by: Steven Liu <59462357+stevhliu@users.noreply.github.com> * Update docs/source/en/model_doc/siglip.md Co-authored-by: Steven Liu <59462357+stevhliu@users.noreply.github.com> * Update docs/source/en/model_doc/siglip.md Co-authored-by: Steven Liu <59462357+stevhliu@users.noreply.github.com> * Update docs/source/en/model_doc/siglip.md Co-authored-by: Steven Liu <59462357+stevhliu@users.noreply.github.com> * Update docs/source/en/model_doc/siglip.md Co-authored-by: Steven Liu <59462357+stevhliu@users.noreply.github.com> * address comments --------- Co-authored-by: Steven Liu <59462357+stevhliu@users.noreply.github.com>
1 parent e1f379b commit 27a25be

File tree

1 file changed

+81
-149
lines changed

1 file changed

+81
-149
lines changed

docs/source/en/model_doc/siglip.md

Lines changed: 81 additions & 149 deletions
Original file line numberDiff line numberDiff line change
@@ -14,184 +14,116 @@ rendered properly in your Markdown viewer.
1414
1515
-->
1616

17-
# SigLIP
18-
19-
<div class="flex flex-wrap space-x-1">
20-
<img alt="PyTorch" src="https://img.shields.io/badge/PyTorch-DE3412?style=flat&logo=pytorch&logoColor=white">
21-
<img alt="FlashAttention" src="https://img.shields.io/badge/%E2%9A%A1%EF%B8%8E%20FlashAttention-eae0c8?style=flat">
22-
<img alt="SDPA" src="https://img.shields.io/badge/SDPA-DE3412?style=flat&logo=pytorch&logoColor=white">
17+
<div style="float: right;">
18+
<div class="flex flex-wrap space-x-1">
19+
<img alt="PyTorch" src="https://img.shields.io/badge/PyTorch-DE3412?style=flat&logo=pytorch&logoColor=white">
20+
<img alt="FlashAttention" src="https://img.shields.io/badge/%E2%9A%A1%EF%B8%8E%20FlashAttention-eae0c8?style=flat">
21+
<img alt="SDPA" src="https://img.shields.io/badge/SDPA-DE3412?style=flat&logo=pytorch&logoColor=white">
22+
</div>
2323
</div>
2424

25-
## Overview
26-
27-
The SigLIP model was proposed in [Sigmoid Loss for Language Image Pre-Training](https://arxiv.org/abs/2303.15343) by Xiaohua Zhai, Basil Mustafa, Alexander Kolesnikov, Lucas Beyer. SigLIP proposes to replace the loss function used in [CLIP](clip) by a simple pairwise sigmoid loss. This results in better performance in terms of zero-shot classification accuracy on ImageNet.
28-
29-
The abstract from the paper is the following:
30-
31-
*We propose a simple pairwise Sigmoid loss for Language-Image Pre-training (SigLIP). Unlike standard contrastive learning with softmax normalization, the sigmoid loss operates solely on image-text pairs and does not require a global view of the pairwise similarities for normalization. The sigmoid loss simultaneously allows further scaling up the batch size, while also performing better at smaller batch sizes. Combined with Locked-image Tuning, with only four TPUv4 chips, we train a SigLiT model that achieves 84.5% ImageNet zero-shot accuracy in two days. The disentanglement of the batch size from the loss further allows us to study the impact of examples vs pairs and negative to positive ratio. Finally, we push the batch size to the extreme, up to one million, and find that the benefits of growing batch size quickly diminish, with a more reasonable batch size of 32k being sufficient.*
32-
33-
## Usage tips
34-
35-
- Usage of SigLIP is similar to [CLIP](clip). The main difference is the training loss, which does not require a global view of all the pairwise similarities of images and texts within a batch. One needs to apply the sigmoid activation function to the logits, rather than the softmax.
36-
- Training is supported but does not use `torch.distributed` utilities which may limit the scalability of batch size. However, DDP and FDSP works on single-node multi-gpu setup.
37-
- When using the standalone [`SiglipTokenizer`] or [`SiglipProcessor`], make sure to pass `padding="max_length"` as that's how the model was trained.
38-
- To get the same results as the pipeline, a prompt template of "This is a photo of {label}." should be used.
39-
40-
<img src="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/model_doc/siglip_table.jpeg"
41-
alt="drawing" width="600"/>
25+
# SigLIP
4226

43-
<small> SigLIP evaluation results compared to CLIP. Taken from the <a href="https://arxiv.org/abs/2303.15343">original paper</a>.</small>
27+
[SigLIP](https://huggingface.co/papers/2303.15343) is a multimodal image-text model similar to [CLIP](clip). It uses separate image and text encoders to generate representations for both modalities.
4428

45-
This model was contributed by [nielsr](https://huggingface.co/nielsr).
46-
The original code can be found [here](https://github.com/google-research/big_vision/tree/main).
29+
Unlike CLIP, SigLIP employs a pairwise sigmoid loss on image-text pairs during training. This training loss eliminates the need for a global view of all pairwise similarities between images and texts within a batch. Consequently, it enables more efficient scaling to larger batch sizes while also delivering superior performance with smaller batch sizes.
4730

48-
## Usage example
31+
You can find all the original SigLIP checkpoints under the [SigLIP](https://huggingface.co/collections/google/siglip-659d5e62f0ae1a57ae0e83ba) collection.
4932

50-
There are 2 main ways to use SigLIP: either using the pipeline API, which abstracts away all the complexity for you, or by using the `SiglipModel` class yourself.
5133

52-
### Pipeline API
34+
> [!TIP]
35+
> Click on the SigLIP models in the right sidebar for more examples of how to apply SigLIP to different image and text tasks.
5336
54-
The pipeline allows to use the model in a few lines of code:
37+
The example below demonstrates how to generate similarity scores between texts and image(s) with [`Pipeline`] or the [`AutoModel`] class.
5538

56-
```python
57-
>>> from transformers import pipeline
58-
>>> from PIL import Image
59-
>>> import requests
39+
<hfoptions id="usage">
40+
<hfoption id="Pipeline">
6041

61-
>>> # load pipe
62-
>>> image_classifier = pipeline(task="zero-shot-image-classification", model="google/siglip-base-patch16-224")
42+
```py
43+
import torch
44+
from transformers import pipeline
6345

64-
>>> # load image
65-
>>> url = 'http://images.cocodataset.org/val2017/000000039769.jpg'
66-
>>> image = Image.open(requests.get(url, stream=True).raw)
46+
image = "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/pipeline-cat-chonk.jpeg"
47+
candidate_labels = ["a Pallas cat", "a lion", "a Siberian tiger"]
6748

68-
>>> # inference
69-
>>> candidate_labels = ["2 cats", "a plane", "a remote"]
70-
>>> outputs = image_classifier(image, candidate_labels=candidate_labels)
71-
>>> outputs = [{"score": round(output["score"], 4), "label": output["label"] } for output in outputs]
72-
>>> print(outputs)
73-
[{'score': 0.1979, 'label': '2 cats'}, {'score': 0.0, 'label': 'a remote'}, {'score': 0.0, 'label': 'a plane'}]
49+
pipeline = pipeline(task="zero-shot-image-classification", model="google/siglip-base-patch16-224", device=0, torch_dtype=torch.bfloat16)
50+
pipeline(image, candidate_labels=candidate_labels)
7451
```
7552

76-
### Using the model yourself
77-
78-
If you want to do the pre- and postprocessing yourself, here's how to do that:
53+
</hfoption>
54+
<hfoption id="AutoModel">
7955

80-
```python
81-
>>> from PIL import Image
82-
>>> import requests
83-
>>> from transformers import AutoProcessor, AutoModel
84-
>>> import torch
56+
```py
57+
import torch
58+
import requests
59+
from PIL import Image
60+
from transformers import AutoProcessor, AutoModel
8561

86-
>>> model = AutoModel.from_pretrained("google/siglip-base-patch16-224")
87-
>>> processor = AutoProcessor.from_pretrained("google/siglip-base-patch16-224")
62+
model = AutoModel.from_pretrained("google/siglip-base-patch16-224", torch_dtype=torch.float16, device_map="auto", attn_implementation="sdpa")
63+
processor = AutoProcessor.from_pretrained("google/siglip-base-patch16-224")
8864

89-
>>> url = "http://images.cocodataset.org/val2017/000000039769.jpg"
90-
>>> image = Image.open(requests.get(url, stream=True).raw)
65+
url = "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/pipeline-cat-chonk.jpeg"
66+
image = Image.open(requests.get(url, stream=True).raw)
67+
candidate_labels = ["a Pallas cat", "a lion", "a Siberian tiger"]
68+
texts = [f'This is a photo of {label}.' for label in candidate_labels]
69+
inputs = processor(text=texts, images=image, padding="max_length", return_tensors="pt").to("cuda")
9170

92-
>>> candidate_labels = ["2 cats", "2 dogs"]
93-
# follows the pipeline prompt template to get same results
94-
>>> texts = [f'This is a photo of {label}.' for label in candidate_labels]
95-
# important: we pass `padding=max_length` since the model was trained with this
96-
>>> inputs = processor(text=texts, images=image, padding="max_length", return_tensors="pt")
71+
with torch.no_grad():
72+
outputs = model(**inputs)
9773

98-
>>> with torch.no_grad():
99-
... outputs = model(**inputs)
100-
101-
>>> logits_per_image = outputs.logits_per_image
102-
>>> probs = torch.sigmoid(logits_per_image) # these are the probabilities
103-
>>> print(f"{probs[0][0]:.1%} that image 0 is '{candidate_labels[0]}'")
104-
19.8% that image 0 is '2 cats'
74+
logits_per_image = outputs.logits_per_image
75+
probs = torch.sigmoid(logits_per_image)
76+
print(f"{probs[0][0]:.1%} that image 0 is '{candidate_labels[0]}'")
10577
```
10678

107-
## Resources
108-
109-
A list of official Hugging Face and community (indicated by 🌎) resources to help you get started with SigLIP.
110-
111-
- [Zero-shot image classification task guide](../tasks/zero_shot_image_classification)
112-
- Demo notebooks for SigLIP can be found [here](https://github.com/NielsRogge/Transformers-Tutorials/tree/master/SigLIP). 🌎
79+
</hfoption>
80+
</hfoptions>
11381

114-
If you're interested in submitting a resource to be included here, please feel free to open a Pull Request and we'll review it! The resource should ideally demonstrate something new instead of duplicating an existing resource.
82+
Quantization reduces the memory burden of large models by representing the weights in a lower precision. Refer to the [Quantization](../quantization/overview) overview for more available quantization backends.
11583

84+
The example below uses [bitsandbytes](../quantization/bitsandbytes) to only quantize the weights to int4.
11685

117-
## Combining SigLIP and Flash Attention 2
86+
```py
87+
import torch
88+
import requests
89+
from PIL import Image
90+
from transformers import AutoProcessor, AutoModel, BitsAndBytesConfig
11891

119-
First, make sure to install the latest version of Flash Attention 2.
92+
bnb_config = BitsAndBytesConfig(load_in_4bit=True)
93+
model = AutoModel.from_pretrained("google/siglip-base-patch16-224", quantization_config=bnb_config, device_map="auto", attn_implementation="sdpa")
94+
processor = AutoProcessor.from_pretrained("google/siglip-base-patch16-224")
12095

121-
```bash
122-
pip install -U flash-attn --no-build-isolation
123-
```
124-
125-
Make also sure that you have a hardware that is compatible with Flash-Attention 2. Read more about it in the official documentation of flash-attn repository. Make also sure to load your model in half-precision (e.g. `torch.float16``)
126-
127-
To load and run a model using Flash Attention 2, refer to the snippet below:
128-
129-
```python
130-
>>> import torch
131-
>>> import requests
132-
>>> from PIL import Image
133-
>>> from transformers import SiglipProcessor, SiglipModel
134-
>>> device = "cuda" # the device to load the model onto
135-
136-
>>> model = SiglipModel.from_pretrained(
137-
... "google/siglip-so400m-patch14-384",
138-
... attn_implementation="flash_attention_2",
139-
... torch_dtype=torch.float16,
140-
... device_map=device,
141-
... )
142-
>>> processor = SiglipProcessor.from_pretrained("google/siglip-so400m-patch14-384")
143-
144-
>>> url = "http://images.cocodataset.org/val2017/000000039769.jpg"
145-
>>> image = Image.open(requests.get(url, stream=True).raw)
146-
147-
>>> candidate_labels = ["2 cats", "2 dogs"]
148-
# follows the pipeline prompt template to get same results
149-
>>> texts = [f'This is a photo of {label}.' for label in candidate_labels]
150-
# important: we pass `padding=max_length` since the model was trained with this
151-
>>> inputs = processor(text=texts, images=image, padding="max_length", return_tensors="pt").to(device)
152-
153-
>>> with torch.no_grad():
154-
... with torch.autocast(device):
155-
... outputs = model(**inputs)
156-
157-
>>> logits_per_image = outputs.logits_per_image
158-
>>> probs = torch.sigmoid(logits_per_image) # these are the probabilities
159-
>>> print(f"{probs[0][0]:.1%} that image 0 is '{candidate_labels[0]}'")
160-
19.8% that image 0 is '2 cats'
161-
```
162-
163-
164-
## Using Scaled Dot Product Attention (SDPA)
165-
166-
PyTorch includes a native scaled dot-product attention (SDPA) operator as part of `torch.nn.functional`. This function
167-
encompasses several implementations that can be applied depending on the inputs and the hardware in use. See the
168-
[official documentation](https://pytorch.org/docs/stable/generated/torch.nn.functional.scaled_dot_product_attention.html)
169-
or the [GPU Inference](https://huggingface.co/docs/transformers/main/en/perf_infer_gpu_one#pytorch-scaled-dot-product-attention)
170-
page for more information.
171-
172-
You may set `attn_implementation="sdpa"` in `from_pretrained()` to explicitly request SDPA to be used. Make sure you have `torch>=2.1.1`.
96+
url = "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/pipeline-cat-chonk.jpeg"
97+
image = Image.open(requests.get(url, stream=True).raw)
98+
candidate_labels = ["a Pallas cat", "a lion", "a Siberian tiger"]
99+
texts = [f'This is a photo of {label}.' for label in candidate_labels]
100+
inputs = processor(text=texts, images=image, padding="max_length", return_tensors="pt").to("cuda")
173101

174-
```python
175-
>>> from transformers import SiglipModel
102+
with torch.no_grad():
103+
outputs = model(**inputs)
176104

177-
>>> model = SiglipModel.from_pretrained(
178-
... "google/siglip-so400m-patch14-384",
179-
... attn_implementation="sdpa",
180-
... torch_dtype=torch.float16,
181-
... device_map=device,
182-
... )
105+
logits_per_image = outputs.logits_per_image
106+
probs = torch.sigmoid(logits_per_image)
107+
print(f"{probs[0][0]:.1%} that image 0 is '{candidate_labels[0]}'")
183108
```
184-
185-
For the best speedups, we recommend loading the model in half-precision (e.g. `torch.float16` or `torch.bfloat16`).
186-
187-
188-
## Expected speedups
189-
190-
Below is an expected speedup diagram that compares inference time between the native implementation in transformers using `google/siglip-so400m-patch14-384` checkpoint in `float16` precision and the Flash Attention 2 / SDPA version of the model using different batch sizes.
191-
192-
<div style="text-align: center">
193-
<img src="https://i.imgur.com/cWm4rsn.png">
194-
</div>
109+
## Notes
110+
111+
- Training is supported for DDP and FSDP on single-node multi-GPU setups. However, it does not use [torch.distributed](https://pytorch.org/tutorials/beginner/dist_overview.html) utilities which may limit the scalability of batch size.
112+
- When using the standalone [`SiglipTokenizer`] or [`SiglipProcessor`], make sure to pass `padding="max_length"` because that is how the model was trained.
113+
- To get the same results as the [`Pipeline`], a prompt template of `"This is a photo of {label}."` should be passed to the processor.
114+
- Toggle the `attn_implementation` parameter to either `"sdpa"` or `"flash_attention_2"` to use a more memory-efficient attention.
115+
```py
116+
# pip install -U flash-attn --no-build-isolation
117+
118+
from transformers import SiglipModel
119+
120+
model = SiglipModel.from_pretrained(
121+
"google/siglip-so400m-patch14-384",
122+
attn_implementation="flash_attention_2",
123+
torch_dtype=torch.float16,
124+
device_map=device,
125+
)
126+
```
195127

196128

197129
## SiglipConfig

0 commit comments

Comments
 (0)