You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
* 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>
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.
<small> SigLIP evaluation results compared to CLIP. Taken from the <ahref="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.
44
28
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.
47
30
48
-
## Usage example
31
+
You can find all the original SigLIP checkpoints under the [SigLIP](https://huggingface.co/collections/google/siglip-659d5e62f0ae1a57ae0e83ba) collection.
49
32
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.
51
33
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.
53
36
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.
- Demo notebooks for SigLIP can be found [here](https://github.com/NielsRogge/Transformers-Tutorials/tree/master/SigLIP). 🌎
79
+
</hfoption>
80
+
</hfoptions>
113
81
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.
115
83
84
+
The example below uses [bitsandbytes](../quantization/bitsandbytes) to only quantize the weights to int4.
116
85
117
-
## Combining SigLIP and Flash Attention 2
86
+
```py
87
+
import torch
88
+
import requests
89
+
fromPILimport Image
90
+
from transformers import AutoProcessor, AutoModel, BitsAndBytesConfig
118
91
119
-
First, make sure to install the latest version of Flash Attention 2.
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:
print(f"{probs[0][0]:.1%} that image 0 is '{candidate_labels[0]}'")
183
108
```
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
-
<divstyle="text-align: center">
193
-
<imgsrc="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.
0 commit comments