标签:
- clip
库名称: open_clip
管道标签: 零样本图像分类
许可证: cc-by-nc-4.0
数据集:
- visheratin/laion-coco-nllb
新版本: visheratin/mexma-siglip2
模型概述
NLLB-SigLIP-MRL是一个结合了NLLB模型文本编码器和SigLIP图像编码器的模型。这使得我们能够将模型能力扩展到Flores-200的201种语言。该版本模型采用套娃表示学习的变体进行训练,除了原始的768维外,还能生成[32, 64, 128, 256, 512]维度的嵌入向量。根据下方基准测试,256和512维度的嵌入保留了90%以上的完整嵌入质量。

完整嵌入模型在XTD10和Crossmodal-3600数据集上为多语言图像文本检索设立了新的最先进水平。
数据集 |
图像检索R@1平均 |
文本检索R@1平均 |
图像检索R@5平均 |
文本检索R@5平均 |
图像检索R@10平均 |
文本检索R@10平均 |
Crossmodal-3600 |
0.5539 |
0.5232 |
0.7963 |
0.7792 |
0.8643 |
0.8558 |
XTD10 |
0.6559 |
0.6106 |
0.8846 |
0.8643 |
0.9458 |
0.9379 |
使用方法
可变分辨率
如需使用支持可变嵌入尺寸的模型,可按以下方式操作:
!pip install -U transformers open_clip_torch
from transformers import AutoModel
from PIL import Image
import requests
import torch
model = AutoModel.from_pretrained("visheratin/nllb-siglip-mrl-base", device="cpu", trust_remote_code=True)
image_path = "https://huggingface.co/spaces/jjourney1125/swin2sr/resolve/main/samples/butterfly.jpg"
image = Image.open(requests.get(image_path, stream=True).raw)
class_options = ["бабочка", "butterfly", "kat"]
class_langs = ["rus_Cyrl", "eng_Latn", "afr_Latn"]
image_logits, text_logits = model.get_logits(
images=[image],
texts=class_options,
langs=class_langs,
resolution=512 # 在此设置分辨率或设为`None`使用原始分辨率
)
print(torch.softmax(image_logits, dim=1))
OpenCLIP集成
本模型已集成至OpenCLIP,可像其他模型一样使用:
!pip install -U open_clip_torch
from open_clip import create_model_from_pretrained, get_tokenizer
from PIL import Image
import requests
import torch
model, transform = create_model_from_pretrained("nllb-clip-base-siglip", "mrl", device="cuda")
tokenizer = get_tokenizer("nllb-clip-base-siglip")
class_options = ["бабочка", "butterfly", "kat"]
class_langs = ["rus_Cyrl", "eng_Latn", "afr_Latn"]
text_inputs = []
for i in range(len(class_options)):
tokenizer.set_language(class_langs[i])
text_inputs.append(tokenizer(class_options[i]))
text_inputs = torch.stack(text_inputs).squeeze(1).to("cuda")
image_path = "https://huggingface.co/spaces/jjourney1125/swin2sr/resolve/main/samples/butterfly.jpg"
image = Image.open(requests.get(image_path, stream=True).raw)
image_inputs = transform(image).unsqueeze(0).to("cuda")
with torch.inference_mode():
logits_per_image, logits_per_text = model.get_logits(image_inputs, text_inputs)
print(logits_per_image.softmax(dim=-1))
致谢
感谢ML Collective提供的Google Cloud计算资源。