🚀 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
許可證。