模型:

M-CLIP/XLM-Roberta-Large-Vit-L-14

英文

Multilingual-clip:XLM-Roberta-Large-Vit-L-14

Multilingual-CLIP将OpenAI的英文文本编码器扩展到其他多种语言。该模型仅包含多语言文本编码器。对应的图像模型ViT-L-14可以通过在OpenAI的 CLIP repository on Github 中找到的说明来获取。以下是使用示例。

需求

要同时使用多语言文本编码器和对应的图像编码器,我们需要安装 multilingual-clip clip 包。

pip install multilingual-clip
pip install git+https://github.com/openai/CLIP.git

用法

以下是从文本编码器中提取嵌入的方法:

from multilingual_clip import pt_multilingual_clip
import transformers

texts = [
    'Three blind horses listening to Mozart.',
    'Älgen är skogens konung!',
    'Wie leben Eisbären in der Antarktis?',
    'Вы знали, что все белые медведи левши?'
]
model_name = 'M-CLIP/XLM-Roberta-Large-Vit-L-14'

# Load Model & Tokenizer
model = pt_multilingual_clip.MultilingualCLIP.from_pretrained(model_name)
tokenizer = transformers.AutoTokenizer.from_pretrained(model_name)

embeddings = model.forward(texts, tokenizer)
print("Text features shape:", embeddings.shape)

从相应的图像编码器中提取嵌入的方法:

import torch
import clip
import requests
from PIL import Image

device = "cuda" if torch.cuda.is_available() else "cpu"
model, preprocess = clip.load("ViT-L/14", device=device)

url = "http://images.cocodataset.org/val2017/000000039769.jpg"
image = Image.open(requests.get(url, stream=True).raw)
image = preprocess(image).unsqueeze(0).to(device)

with torch.no_grad():
    image_features = model.encode_image(image)

print("Image features shape:", image_features.shape) 

评估结果

尽管M-CLIP模型尚未经过全面评估,但在人工翻译的MS-COCO数据集上进行了Txt2Img检索测试,我们得到了以下R@10结果:

Name En De Es Fr Zh It Pl Ko Ru Tr Jp
1236321 90.3 - - - - - - - - - -
1237321 91.8 - - - - - - - - - -
1238321 94.3 - - - - - - - - - -
1239321 91.6 89.6 89.5 89.9 88.9 90.1 89.8 80.8 85.5 89.8 73.9
12310321 91.8 88.7 89.1 89.4 89.3 89.8 91.4 82.1 86.1 88.8 81.0
12311321 92.4 90.6 91.0 90.0 89.7 91.1 91.3 85.2 85.8 90.3 81.9
12312321 95.0 93.0 93.6 93.1 94.0 93.1 94.4 89.0 90.0 93.0 84.2

培训/模型详细信息

关于模型培训和数据的更多详细信息可以在 model card 中找到。