Zero-Shot Image Classification
Transformers
Safetensors
siglip
vision
nielsr HF Staff commited on
Commit
415aa2d
·
1 Parent(s): c5a4d02

Create README.md

Browse files
Files changed (1) hide show
  1. README.md +95 -0
README.md ADDED
@@ -0,0 +1,95 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ license: apache-2.0
3
+ ---
4
+
5
+ # SigLIP (base-sized model)
6
+
7
+ SigLIP model pre-trained on ImageNet-21k (14 million images, 21,843 classes) at resolution 224x224, and fine-tuned on ImageNet 2012 (1 million images, 1,000 classes) at resolution 224x224. It was introduced in the paper [An Image is Worth 16x16 Words: Transformers for Image Recognition at Scale](https://arxiv.org/abs/2010.11929) by Dosovitskiy et al. and first released in [this repository](https://github.com/google-research/vision_transformer). However, the weights were converted from the [timm repository](https://github.com/rwightman/pytorch-image-models) by Ross Wightman, who already converted the weights from JAX to PyTorch. Credits go to him.
8
+
9
+ Disclaimer: The team releasing ViT did not write a model card for this model so this model card has been written by the Hugging Face team.
10
+
11
+ ## Model description
12
+
13
+ SigLIP is [CLIP](https://huggingface.co/docs/transformers/model_doc/clip) 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.
14
+
15
+ ## Intended uses & limitations
16
+
17
+ 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
18
+ other versions on a task that interests you.
19
+
20
+ ### How to use
21
+
22
+ Here is how to use this model to perform zero-shot image classification:
23
+
24
+ ```python
25
+ from PIL import Image
26
+ import requests
27
+ from transformers import AutoProcessor, AutoModel
28
+ import torch
29
+
30
+ model = AutoModel.from_pretrained("nielsr/siglip-base-patch16-224")
31
+ processor = AutoProcessor.from_pretrained("nielsr/siglip-base-patch16-224")
32
+
33
+ url = "http://images.cocodataset.org/val2017/000000039769.jpg"
34
+ image = Image.open(requests.get(url, stream=True).raw)
35
+
36
+ texts = ["a photo of 2 cats", "a photo of 2 dogs"]
37
+ inputs = processor(text=texts, images=image, return_tensors="pt")
38
+
39
+ with torch.no_grad():
40
+ outputs = model(**inputs)
41
+
42
+ logits_per_image = outputs.logits_per_image
43
+ probs = torch.sigmoid(logits_per_image) # these are the probabilities
44
+ print(f"{probs[0][0]:.1%} that image 0 is '{texts[0]}'")
45
+ ```
46
+
47
+ Alternatively, one can leverage the pipeline API which abstracts away the complexity for the user:
48
+
49
+ ```
50
+ from transformers import pipeline
51
+ from PIL import Image
52
+ import requests
53
+
54
+ # load pipe
55
+ image_classifier = pipeline(task="zero-shot-image-classification", model="nielsr/siglip-base-patch16-224")
56
+
57
+ # load image
58
+ url = 'http://images.cocodataset.org/val2017/000000039769.jpg'
59
+ image = Image.open(requests.get(url, stream=True).raw)
60
+
61
+ # inference
62
+ outputs = image_classifier(image, candidate_labels=["2 cats", "a plane", "a remote"])
63
+ outputs = [{"score": round(output["score"], 4), "label": output["label"] } for output in outputs]
64
+ print(outputs)
65
+ ```
66
+ For more code examples, we refer to the [documentation](https://huggingface.co/transformers/main/model_doc/siglip.html#).
67
+
68
+ ## Training procedure
69
+
70
+ ### Training data
71
+
72
+ SigLIP is pre-trained on the WebLI dataset [(Chen et al., 2023)](https://arxiv.org/abs/2209.06794).
73
+
74
+ ### Preprocessing
75
+
76
+ Images are resized/rescaled to the same resolution (224x224) and normalized across the RGB channels with mean (0.5, 0.5, 0.5) and standard deviation (0.5, 0.5, 0.5).
77
+
78
+ Texts are tokenized and padded to the same length (64 tokens).
79
+
80
+ ### Compute
81
+
82
+ The model was trained on 16 TPU-v4 chips for three days.
83
+
84
+ ### BibTeX entry and citation info
85
+
86
+ ```bibtex
87
+ @misc{zhai2023sigmoid,
88
+ title={Sigmoid Loss for Language Image Pre-Training},
89
+ author={Xiaohua Zhai and Basil Mustafa and Alexander Kolesnikov and Lucas Beyer},
90
+ year={2023},
91
+ eprint={2303.15343},
92
+ archivePrefix={arXiv},
93
+ primaryClass={cs.CV}
94
+ }
95
+ ```