visheratin
commited on
Commit
•
6ce6524
1
Parent(s):
a59a2af
Update README.md
Browse files
README.md
CHANGED
@@ -3,6 +3,99 @@ tags:
|
|
3 |
- clip
|
4 |
library_name: open_clip
|
5 |
pipeline_tag: zero-shot-image-classification
|
6 |
-
license:
|
|
|
|
|
7 |
---
|
8 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
3 |
- clip
|
4 |
library_name: open_clip
|
5 |
pipeline_tag: zero-shot-image-classification
|
6 |
+
license: cc-by-nc-4.0
|
7 |
+
datasets:
|
8 |
+
- visheratin/laion-coco-nllb
|
9 |
---
|
10 |
+
|
11 |
+
## Model Summary
|
12 |
+
|
13 |
+
NLLB-SigLIP-MRL is a model that combines a text encoder from the [NLLB model](https://huggingface.co/facebook/nllb-200-distilled-600M) and an image encoder from the
|
14 |
+
[SigLIP](https://huggingface.co/timm/ViT-B-16-SigLIP-384) model. This allows us to extend the model capabilities
|
15 |
+
to 201 languages of the Flores-200. This version of the model was trained using a variation of [Matryoshka Representation learning](https://arxiv.org/abs/2205.13147)
|
16 |
+
to enable the generation of embeddings of sizes [32, 64, 128, 256, 512] in addition to the original 768. Based on the benchmarks below, embeddings of sizes 256 and 512
|
17 |
+
preserve 90%+ of the full embedding quality.
|
18 |
+
|
19 |
+
![image/png](https://cdn-uploads.huggingface.co/production/uploads/609ede05121df5de54007033/PP5GJOgM2YVQM4RSWHKtq.png)
|
20 |
+
|
21 |
+
The full embedding model sets new state-of-the-art for multilingual image and text retrieval on both XTD10 and Crossmodal-3600.
|
22 |
+
|
23 |
+
## How to use
|
24 |
+
|
25 |
+
### Variable resolutions
|
26 |
+
|
27 |
+
<a target="_blank" href="https://colab.research.google.com/drive/1gYKUm3urhhHapaFbJ6GD1Fl3pI5g-fjM">
|
28 |
+
<img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open In Colab"/>
|
29 |
+
</a>
|
30 |
+
|
31 |
+
If you want to use the model that supports variable embedding sizes, you can do it as follows:
|
32 |
+
|
33 |
+
```
|
34 |
+
!pip install -U transformers open_clip_torch
|
35 |
+
```
|
36 |
+
|
37 |
+
```
|
38 |
+
from transformers import AutoModel
|
39 |
+
from PIL import Image
|
40 |
+
import requests
|
41 |
+
import torch
|
42 |
+
|
43 |
+
model = AutoModel.from_pretrained("visheratin/nllb-siglip-mrl-base", device="cpu", trust_remote_code=True)
|
44 |
+
|
45 |
+
image_path = "https://huggingface.co/spaces/jjourney1125/swin2sr/resolve/main/samples/butterfly.jpg"
|
46 |
+
image = Image.open(requests.get(image_path, stream=True).raw)
|
47 |
+
|
48 |
+
class_options = ["бабочка", "butterfly", "kat"]
|
49 |
+
class_langs = ["rus_Cyrl", "eng_Latn", "afr_Latn"]
|
50 |
+
|
51 |
+
image_logits, text_logits = model.get_logits(
|
52 |
+
images=[image],
|
53 |
+
texts=class_options,
|
54 |
+
langs=class_langs,
|
55 |
+
resolution=512 # set resolution here or set `None` to use the original resolution
|
56 |
+
)
|
57 |
+
|
58 |
+
print(torch.softmax(image_logits, dim=1))
|
59 |
+
```
|
60 |
+
|
61 |
+
### OpenCLIP
|
62 |
+
|
63 |
+
This model is also integrated into OpenCLIP so that you can use it as any other model:
|
64 |
+
|
65 |
+
```
|
66 |
+
!pip install -U open_clip_torch
|
67 |
+
```
|
68 |
+
|
69 |
+
```
|
70 |
+
from open_clip import create_model_from_pretrained, get_tokenizer
|
71 |
+
from PIL import Image
|
72 |
+
import requests
|
73 |
+
import torch
|
74 |
+
|
75 |
+
model, transform = create_model_from_pretrained("nllb-clip-base-siglip", "mrl", device="cuda")
|
76 |
+
|
77 |
+
tokenizer = get_tokenizer("nllb-clip-base-siglip")
|
78 |
+
|
79 |
+
class_options = ["бабочка", "butterfly", "kat"]
|
80 |
+
class_langs = ["rus_Cyrl", "eng_Latn", "afr_Latn"]
|
81 |
+
|
82 |
+
text_inputs = []
|
83 |
+
for i in range(len(class_options)):
|
84 |
+
tokenizer.set_language(class_langs[i])
|
85 |
+
text_inputs.append(tokenizer(class_options[i]))
|
86 |
+
text_inputs = torch.stack(text_inputs).squeeze(1).to("cuda")
|
87 |
+
|
88 |
+
image_path = "https://huggingface.co/spaces/jjourney1125/swin2sr/resolve/main/samples/butterfly.jpg"
|
89 |
+
image = Image.open(requests.get(image_path, stream=True).raw)
|
90 |
+
|
91 |
+
image_inputs = transform(image).unsqueeze(0).to("cuda")
|
92 |
+
|
93 |
+
with torch.inference_mode():
|
94 |
+
logits_per_image, logits_per_text = model.get_logits(image_inputs, text_inputs)
|
95 |
+
|
96 |
+
print(logits_per_image.softmax(dim=-1))
|
97 |
+
```
|
98 |
+
|
99 |
+
## Acknowledgements
|
100 |
+
|
101 |
+
I thank [ML Collective](https://mlcollective.org/) for providing Google Cloud compute resources.
|