🚀 sentence-transformers/clip-ViT-B-32-multilingual-v1-onnx
这是OpenAI CLIP - ViT - B32模型的多语言版本,已转换为ONNX格式。你可以将文本(支持50多种语言)和图像映射到一个共同的密集向量空间中,使图像和匹配的文本在空间中距离相近。该模型可用于图像搜索(用户在大量图像集合中进行搜索)和多语言零样本图像分类(图像标签以文本形式定义)。
🚀 快速开始
使用此模型时,若安装了 sentence - transformers,操作将变得十分简单:
pip install -U sentence-transformers
📦 安装指南
安装 sentence - transformers:
pip install -U sentence-transformers
💻 使用示例
基础用法
from sentence_transformers import SentenceTransformer, util
from PIL import Image, ImageFile
import requests
import torch
img_model = SentenceTransformer('clip-ViT-B-32')
text_model = SentenceTransformer('sentence-transformers/clip-ViT-B-32-multilingual-v1')
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)
img_paths = [
"https://unsplash.com/photos/QtxgNsmJQSs/download?ixid=MnwxMjA3fDB8MXxhbGx8fHx8fHx8fHwxNjM1ODQ0MjY3&w=640",
"https://unsplash.com/photos/9UUoGaaHtNE/download?ixid=MnwxMjA3fDB8MXxzZWFyY2h8Mnx8Y2F0fHwwfHx8fDE2MzU4NDI1ODQ&w=640",
"https://unsplash.com/photos/Siuwr3uCir0/download?ixid=MnwxMjA3fDB8MXxzZWFyY2h8NHx8YmVhY2h8fDB8fHx8MTYzNTg0MjYzMg&w=640"
]
images = [load_image(img) for img in img_paths]
img_embeddings = img_model.encode(images)
texts = [
"A dog in the snow",
"Eine Katze",
"Una playa con palmeras."
]
text_embeddings = text_model.encode(texts)
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 - B32
,然后训练了一个 多语言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",
}
📄 许可证
本项目采用 apache - 2.0
许可证。