canavar commited on
Commit
b3e9450
1 Parent(s): 35cfec8

adds: initial commit with optimum exported clip-vit-b-32-multilingual-v1

Browse files
Files changed (7) hide show
  1. README.md +124 -0
  2. config.json +24 -0
  3. model.onnx +3 -0
  4. special_tokens_map.json +7 -0
  5. tokenizer.json +0 -0
  6. tokenizer_config.json +57 -0
  7. vocab.txt +0 -0
README.md CHANGED
@@ -1,3 +1,127 @@
1
  ---
 
 
 
 
 
 
 
2
  license: apache-2.0
3
  ---
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  ---
2
+ pipeline_tag: sentence-similarity
3
+ language: multilingual
4
+ tags:
5
+ - sentence-transformers
6
+ - feature-extraction
7
+ - sentence-similarity
8
+ - transformers
9
  license: apache-2.0
10
  ---
11
+
12
+ # sentence-transformers/clip-ViT-B-32-multilingual-v1-onnx
13
+
14
+ This is a multi-lingual version of the OpenAI CLIP-ViT-B32 model converted to ONNX. You can map text (in 50+ languages) and images to a common dense vector space such that images and the matching texts are close. This model can be used for **image search** (users search through a large collection of images) and for **multi-lingual zero-shot image classification** (image labels are defined as text).
15
+
16
+
17
+ ## Usage (Sentence-Transformers)
18
+
19
+ Using this model becomes easy when you have [sentence-transformers](https://www.SBERT.net) installed:
20
+
21
+ ```
22
+ pip install -U sentence-transformers
23
+ ```
24
+
25
+ Then you can use the model like this:
26
+
27
+ ```python
28
+ from sentence_transformers import SentenceTransformer, util
29
+ from PIL import Image, ImageFile
30
+ import requests
31
+ import torch
32
+
33
+ # We use the original clip-ViT-B-32 for encoding images
34
+ img_model = SentenceTransformer('clip-ViT-B-32')
35
+
36
+ # Our text embedding model is aligned to the img_model and maps 50+
37
+ # languages to the same vector space
38
+ text_model = SentenceTransformer('sentence-transformers/clip-ViT-B-32-multilingual-v1')
39
+
40
+
41
+ # Now we load and encode the images
42
+ def load_image(url_or_path):
43
+ if url_or_path.startswith("http://") or url_or_path.startswith("https://"):
44
+ return Image.open(requests.get(url_or_path, stream=True).raw)
45
+ else:
46
+ return Image.open(url_or_path)
47
+
48
+ # We load 3 images. You can either pass URLs or
49
+ # a path on your disc
50
+ img_paths = [
51
+ # Dog image
52
+ "https://unsplash.com/photos/QtxgNsmJQSs/download?ixid=MnwxMjA3fDB8MXxhbGx8fHx8fHx8fHwxNjM1ODQ0MjY3&w=640",
53
+
54
+ # Cat image
55
+ "https://unsplash.com/photos/9UUoGaaHtNE/download?ixid=MnwxMjA3fDB8MXxzZWFyY2h8Mnx8Y2F0fHwwfHx8fDE2MzU4NDI1ODQ&w=640",
56
+
57
+ # Beach image
58
+ "https://unsplash.com/photos/Siuwr3uCir0/download?ixid=MnwxMjA3fDB8MXxzZWFyY2h8NHx8YmVhY2h8fDB8fHx8MTYzNTg0MjYzMg&w=640"
59
+ ]
60
+
61
+ images = [load_image(img) for img in img_paths]
62
+
63
+ # Map images to the vector space
64
+ img_embeddings = img_model.encode(images)
65
+
66
+ # Now we encode our text:
67
+ texts = [
68
+ "A dog in the snow",
69
+ "Eine Katze", # German: A cat
70
+ "Una playa con palmeras." # Spanish: a beach with palm trees
71
+ ]
72
+
73
+ text_embeddings = text_model.encode(texts)
74
+
75
+ # Compute cosine similarities:
76
+ cos_sim = util.cos_sim(text_embeddings, img_embeddings)
77
+
78
+ for text, scores in zip(texts, cos_sim):
79
+ max_img_idx = torch.argmax(scores)
80
+ print("Text:", text)
81
+ print("Score:", scores[max_img_idx] )
82
+ print("Path:", img_paths[max_img_idx], "\n")
83
+
84
+ ```
85
+
86
+ ## Multilingual Image Search - Demo
87
+ For a demo of multilingual image search, have a look at: [Image_Search-multilingual.ipynb](https://github.com/UKPLab/sentence-transformers/tree/master/examples/applications/image-search/Image_Search-multilingual.ipynb) ( [Colab version](https://colab.research.google.com/drive/1N6woBKL4dzYsHboDNqtv-8gjZglKOZcn?usp=sharing) )
88
+
89
+ For more details on image search and zero-shot image classification, have a look at the documentation on [SBERT.net](https://www.sbert.net/examples/applications/image-search/README.html).
90
+
91
+
92
+ ## Training
93
+ This model has been created using [Multilingual Knowledge Distillation](https://arxiv.org/abs/2004.09813). As teacher model, we used the original `clip-ViT-B-32` and then trained a [multilingual DistilBERT](https://huggingface.co/distilbert-base-multilingual-cased) model as student model. Using parallel data, the multilingual student model learns to align the teachers vector space across many languages. As a result, you get an text embedding model that works for 50+ languages.
94
+
95
+ The image encoder from CLIP is unchanged, i.e. you can use the original CLIP image encoder to encode images.
96
+
97
+ Have a look at the [SBERT.net - Multilingual-Models documentation](https://www.sbert.net/examples/training/multilingual/README.html) on more details and for **training code**.
98
+
99
+ We used the following 50+ languages to align the vector spaces: ar, bg, ca, cs, da, de, el, es, et, fa, fi, fr, fr-ca, gl, gu, he, hi, hr, hu, hy, id, it, ja, ka, ko, ku, lt, lv, mk, mn, mr, ms, my, nb, nl, pl, pt, pt, pt-br, ro, ru, sk, sl, sq, sr, sv, th, tr, uk, ur, vi, zh-cn, zh-tw.
100
+
101
+ The original multilingual DistilBERT supports 100+ lanugages. The model also work for these languages, but might not yield the best results.
102
+
103
+ ## Full Model Architecture
104
+ ```
105
+ SentenceTransformer(
106
+ (0): Transformer({'max_seq_length': 128, 'do_lower_case': False}) with Transformer model: DistilBertModel
107
+ (1): Pooling({'word_embedding_dimension': 768, 'pooling_mode_cls_token': False, 'pooling_mode_mean_tokens': True, 'pooling_mode_max_tokens': False, 'pooling_mode_mean_sqrt_len_tokens': False})
108
+ (2): Dense({'in_features': 768, 'out_features': 512, 'bias': False, 'activation_function': 'torch.nn.modules.linear.Identity'})
109
+ )
110
+ ```
111
+
112
+ ## Citing & Authors
113
+
114
+ This model was trained by [sentence-transformers](https://www.sbert.net/).
115
+
116
+ If you find this model helpful, feel free to cite our publication [Sentence-BERT: Sentence Embeddings using Siamese BERT-Networks](https://arxiv.org/abs/1908.10084):
117
+ ```bibtex
118
+ @inproceedings{reimers-2019-sentence-bert,
119
+ title = "Sentence-BERT: Sentence Embeddings using Siamese BERT-Networks",
120
+ author = "Reimers, Nils and Gurevych, Iryna",
121
+ booktitle = "Proceedings of the 2019 Conference on Empirical Methods in Natural Language Processing",
122
+ month = "11",
123
+ year = "2019",
124
+ publisher = "Association for Computational Linguistics",
125
+ url = "http://arxiv.org/abs/1908.10084",
126
+ }
127
+ ```
config.json ADDED
@@ -0,0 +1,24 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "_name_or_path": "/home/canavar/.cache/torch/sentence_transformers/sentence-transformers_clip-ViT-B-32-multilingual-v1/",
3
+ "activation": "gelu",
4
+ "architectures": [
5
+ "DistilBertModel"
6
+ ],
7
+ "attention_dropout": 0.1,
8
+ "dim": 768,
9
+ "dropout": 0.1,
10
+ "hidden_dim": 3072,
11
+ "initializer_range": 0.02,
12
+ "max_position_embeddings": 512,
13
+ "model_type": "distilbert",
14
+ "n_heads": 12,
15
+ "n_layers": 6,
16
+ "output_past": true,
17
+ "pad_token_id": 0,
18
+ "qa_dropout": 0.1,
19
+ "seq_classif_dropout": 0.2,
20
+ "sinusoidal_pos_embds": false,
21
+ "tie_weights_": true,
22
+ "transformers_version": "4.36.0",
23
+ "vocab_size": 119547
24
+ }
model.onnx ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:7f3129e76a60a33aa42941a369d54266151f9ca2e7e4d52295300edb85c5bbeb
3
+ size 540650752
special_tokens_map.json ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ {
2
+ "cls_token": "[CLS]",
3
+ "mask_token": "[MASK]",
4
+ "pad_token": "[PAD]",
5
+ "sep_token": "[SEP]",
6
+ "unk_token": "[UNK]"
7
+ }
tokenizer.json ADDED
The diff for this file is too large to render. See raw diff
 
tokenizer_config.json ADDED
@@ -0,0 +1,57 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "added_tokens_decoder": {
3
+ "0": {
4
+ "content": "[PAD]",
5
+ "lstrip": false,
6
+ "normalized": false,
7
+ "rstrip": false,
8
+ "single_word": false,
9
+ "special": true
10
+ },
11
+ "100": {
12
+ "content": "[UNK]",
13
+ "lstrip": false,
14
+ "normalized": false,
15
+ "rstrip": false,
16
+ "single_word": false,
17
+ "special": true
18
+ },
19
+ "101": {
20
+ "content": "[CLS]",
21
+ "lstrip": false,
22
+ "normalized": false,
23
+ "rstrip": false,
24
+ "single_word": false,
25
+ "special": true
26
+ },
27
+ "102": {
28
+ "content": "[SEP]",
29
+ "lstrip": false,
30
+ "normalized": false,
31
+ "rstrip": false,
32
+ "single_word": false,
33
+ "special": true
34
+ },
35
+ "103": {
36
+ "content": "[MASK]",
37
+ "lstrip": false,
38
+ "normalized": false,
39
+ "rstrip": false,
40
+ "single_word": false,
41
+ "special": true
42
+ }
43
+ },
44
+ "clean_up_tokenization_spaces": true,
45
+ "cls_token": "[CLS]",
46
+ "do_basic_tokenize": true,
47
+ "do_lower_case": false,
48
+ "mask_token": "[MASK]",
49
+ "model_max_length": 512,
50
+ "never_split": null,
51
+ "pad_token": "[PAD]",
52
+ "sep_token": "[SEP]",
53
+ "strip_accents": null,
54
+ "tokenize_chinese_chars": true,
55
+ "tokenizer_class": "DistilBertTokenizer",
56
+ "unk_token": "[UNK]"
57
+ }
vocab.txt ADDED
The diff for this file is too large to render. See raw diff