Zero-Shot Image Classification
Transformers
Safetensors
siglip
vision
Inference Endpoints
merve HF staff commited on
Commit
5d67b24
1 Parent(s): 022480e

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +114 -3
README.md CHANGED
@@ -1,3 +1,114 @@
1
- ---
2
- license: apache-2.0
3
- ---
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ license: apache-2.0
3
+ tags:
4
+ - vision
5
+ widget:
6
+ - src: https://huggingface.co/datasets/mishig/sample_images/resolve/main/cat-dog-music.png
7
+ candidate_labels: playing music, playing sports
8
+ example_title: Cat & Dog
9
+ ---
10
+
11
+ **Note**: This is not yet supported in transformers, install transformers from [here](https://github.com/huggingface/transformers/pull/32938) if you want to try it.
12
+
13
+ # SigLIP (shape-optimized model)
14
+
15
+ SigLIP model pre-trained on WebLi at resolution 384x384. It was introduced in the paper [Sigmoid Loss for Language Image Pre-Training](https://arxiv.org/abs/2303.15343) by Zhai et al. and first released in [this repository](https://github.com/google-research/big_vision).
16
+
17
+ This model has the SoViT-400m architecture, which is the shape-optimized version as presented in [Getting ViT in Shape: Scaling Laws for Compute-Optimal Model Design](https://arxiv.org/abs/2305.13035) by Alabdulmohsin et al.
18
+
19
+ Disclaimer: The team releasing SigLIP did not write a model card for this model so this model card has been written by the Hugging Face team.
20
+
21
+ ## Model description
22
+
23
+ SigLIP is [CLIP](https://huggingface.co/docs/transformers/model_doc/clip), a multimodal model, with a better loss function. The sigmoid loss operates solely on image-text pairs and does not require a global view of the pairwise similarities for normalization. This allows further scaling up the batch size, while also performing better at smaller batch sizes.
24
+
25
+ A TLDR of SigLIP by one of the authors can be found [here](https://twitter.com/giffmana/status/1692641733459267713).
26
+
27
+ ## Intended uses & limitations
28
+
29
+ You can use the raw model for tasks like zero-shot image classification and image-text retrieval. See the [model hub](https://huggingface.co/models?search=google/siglip) to look for
30
+ other versions on a task that interests you.
31
+
32
+ ### How to use
33
+
34
+ Here is how to use this model to perform zero-shot image classification:
35
+
36
+ ```python
37
+ from PIL import Image
38
+ import requests
39
+ from transformers import AutoProcessor, AutoModel
40
+ import torch
41
+
42
+ model = AutoModel.from_pretrained("merve/siglip-so400m-patch16-256-i18n")
43
+ processor = AutoProcessor.from_pretrained("merve/siglip-so400m-patch16-256-i18n")
44
+
45
+ url = "http://images.cocodataset.org/val2017/000000039769.jpg"
46
+ image = Image.open(requests.get(url, stream=True).raw)
47
+
48
+ texts = ["a photo of 2 cats", "a photo of 2 dogs"]
49
+ inputs = processor(text=texts, images=image, padding="max_length", return_tensors="pt")
50
+
51
+ with torch.no_grad():
52
+ outputs = model(**inputs)
53
+
54
+ logits_per_image = outputs.logits_per_image
55
+ probs = torch.sigmoid(logits_per_image) # these are the probabilities
56
+ print(f"{probs[0][0]:.1%} that image 0 is '{texts[0]}'")
57
+ ```
58
+
59
+ Alternatively, one can leverage the pipeline API which abstracts away the complexity for the user:
60
+
61
+ ```python
62
+ from transformers import pipeline
63
+ from PIL import Image
64
+ import requests
65
+
66
+ # load pipe
67
+ image_classifier = pipeline(task="zero-shot-image-classification", model="merve/siglip-so400m-patch16-256-i18n")
68
+
69
+ # load image
70
+ url = 'http://images.cocodataset.org/val2017/000000039769.jpg'
71
+ image = Image.open(requests.get(url, stream=True).raw)
72
+
73
+ # inference
74
+ outputs = image_classifier(image, candidate_labels=["2 cats", "a plane", "a remote"])
75
+ outputs = [{"score": round(output["score"], 4), "label": output["label"] } for output in outputs]
76
+ print(outputs)
77
+ ```
78
+ For more code examples, we refer to the [documentation](https://huggingface.co/transformers/main/model_doc/siglip.html#).
79
+
80
+ ## Training procedure
81
+
82
+ ### Training data
83
+
84
+ SigLIP is pre-trained on the WebLI dataset [(Chen et al., 2023)](https://arxiv.org/abs/2209.06794).
85
+
86
+ ### Preprocessing
87
+
88
+ Images are resized/rescaled to the same resolution (384x384) and normalized across the RGB channels with mean (0.5, 0.5, 0.5) and standard deviation (0.5, 0.5, 0.5).
89
+
90
+ Texts are tokenized and padded to the same length (64 tokens).
91
+
92
+ ### Compute
93
+
94
+ The model was trained on 16 TPU-v4 chips for three days.
95
+
96
+ ## Evaluation results
97
+
98
+ Evaluation of SigLIP compared to CLIP is shown below (taken from the paper).
99
+
100
+ <img src="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/model_doc/siglip_table.jpeg"
101
+ alt="drawing" width="600"/>
102
+
103
+ ### BibTeX entry and citation info
104
+
105
+ ```bibtex
106
+ @misc{zhai2023sigmoid,
107
+ title={Sigmoid Loss for Language Image Pre-Training},
108
+ author={Xiaohua Zhai and Basil Mustafa and Alexander Kolesnikov and Lucas Beyer},
109
+ year={2023},
110
+ eprint={2303.15343},
111
+ archivePrefix={arXiv},
112
+ primaryClass={cs.CV}
113
+ }
114
+ ```