模型:
sentence-transformers/clip-ViT-B-32-multilingual-v1
这是OpenAI CLIP-ViT-B32模型的多语言版本。您可以将文本(支持50多种语言)和图像映射到一个共同的稠密向量空间,使得图像和匹配的文本接近。该模型可用于图像搜索(用户搜索大量图像集合)和多语言零样本图像分类(图像标签定义为文本)。
在安装了 sentence-transformers 后,使用本模型变得非常简单:
pip install -U sentence-transformers
然后您可以像这样使用该模型:
from sentence_transformers import SentenceTransformer, util from PIL import Image, ImageFile import requests import torch # We use the original clip-ViT-B-32 for encoding images img_model = SentenceTransformer('clip-ViT-B-32') # Our text embedding model is aligned to the img_model and maps 50+ # languages to the same vector space text_model = SentenceTransformer('sentence-transformers/clip-ViT-B-32-multilingual-v1') # Now we load and encode the images def load_image(url_or_path): if url_or_path.startswith("http://") or url_or_path.startswith("https://"): return Image.open(requests.get(url_or_path, stream=True).raw) else: return Image.open(url_or_path) # We load 3 images. You can either pass URLs or # a path on your disc img_paths = [ # Dog image "https://unsplash.com/photos/QtxgNsmJQSs/download?ixid=MnwxMjA3fDB8MXxhbGx8fHx8fHx8fHwxNjM1ODQ0MjY3&w=640", # Cat image "https://unsplash.com/photos/9UUoGaaHtNE/download?ixid=MnwxMjA3fDB8MXxzZWFyY2h8Mnx8Y2F0fHwwfHx8fDE2MzU4NDI1ODQ&w=640", # Beach image "https://unsplash.com/photos/Siuwr3uCir0/download?ixid=MnwxMjA3fDB8MXxzZWFyY2h8NHx8YmVhY2h8fDB8fHx8MTYzNTg0MjYzMg&w=640" ] images = [load_image(img) for img in img_paths] # Map images to the vector space img_embeddings = img_model.encode(images) # Now we encode our text: texts = [ "A dog in the snow", "Eine Katze", # German: A cat "Una playa con palmeras." # Spanish: a beach with palm trees ] text_embeddings = text_model.encode(texts) # Compute cosine similarities: cos_sim = util.cos_sim(text_embeddings, img_embeddings) for text, scores in zip(texts, cos_sim): max_img_idx = torch.argmax(scores) print("Text:", text) print("Score:", scores[max_img_idx] ) print("Path:", img_paths[max_img_idx], "\n")
欲了解多语言图像搜索的演示,请参阅: Image_Search-multilingual.ipynb ( Colab version )
欲了解有关图像搜索和零样本图像分类的更多详细信息,请查看 SBERT.net 上的文档。
本模型是使用 Multilingual Knowledge Distillation 创建的。作为教师模型,我们使用了原始的clip-ViT-B-32,然后训练了一个 multilingual DistilBERT 模型作为学生模型。通过使用平行数据,多语言学生模型学习了如何在许多语言之间对齐教师的向量空间。结果是,您可以获得适用于50多种语言的文本嵌入模型。
CLIP中的图像编码器未更改,即您仍然可以使用原始CLIP图像编码器对图像进行编码。
请查看 SBERT.net - Multilingual-Models documentation 上的文档以了解更多详细信息和训练代码。
我们使用了以下50多种语言来对齐向量空间: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-br,ro,ru,sk,sl,sq,sr,sv,th,tr,uk,ur,vi,zh-cn,zh-tw。
原始的多语言DistilBERT支持100多种语言。该模型也适用于这些语言,但可能无法获得最佳结果。
SentenceTransformer( (0): Transformer({'max_seq_length': 128, 'do_lower_case': False}) with Transformer model: DistilBertModel (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}) (2): Dense({'in_features': 768, 'out_features': 512, 'bias': False, 'activation_function': 'torch.nn.modules.linear.Identity'}) )
本模型由 sentence-transformers 进行训练。
如果您发现此模型有帮助,请随意引用我们的出版物 Sentence-BERT: Sentence Embeddings using Siamese BERT-Networks :
@inproceedings{reimers-2019-sentence-bert, title = "Sentence-BERT: Sentence Embeddings using Siamese BERT-Networks", author = "Reimers, Nils and Gurevych, Iryna", booktitle = "Proceedings of the 2019 Conference on Empirical Methods in Natural Language Processing", month = "11", year = "2019", publisher = "Association for Computational Linguistics", url = "http://arxiv.org/abs/1908.10084", }