HuggingFace镜像/clip-ViT-B-32-multilingual-v1
模型介绍文件和版本分析
下载使用量0

sentence-transformers/clip-ViT-B-32-multilingual-v1

这是 OpenAI CLIP-ViT-B32 模型的多语言版本。您可以将文本(支持 50 多种语言)和图像映射到一个共同的密集向量空间,从而使图像和匹配的文本在空间中距离接近。该模型可用于图像搜索(用户在大量图像集合中进行搜索)和多语言零样本图像分类(图像标签通过文本定义)。

使用方法(Sentence-Transformers)

当您安装了 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 版本)

如需了解更多关于图像搜索和零样本图像分类的详细信息,请查看 SBERT.net 上的文档。

训练

该模型是使用 多语言知识蒸馏 技术创建的。我们使用原始的 clip-ViT-B-32 作为教师模型,然后训练了一个 多语言 DistilBERT 模型作为学生模型。通过平行数据,多语言学生模型学习在多种语言上对齐教师模型的向量空间。因此,您获得了一个适用于 50 多种语言的文本嵌入模型。

CLIP 的图像编码器未做改动,即您可以使用原始的 CLIP 图像编码器对图像进行编码。

有关更多详细信息和训练代码,请查看 SBERT.net - 多语言模型文档。

我们使用以下 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, 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",
}