🚀 SigLIP (基础规模模型)
SigLIP是一种多模态模型,它在分辨率为512x512的WebLi数据集上进行了预训练。该模型引入了更优的损失函数,能在图像 - 文本对任务上表现出色,可用于零样本图像分类和图像 - 文本检索等任务。
🚀 快速开始
SigLIP是一种改进版的CLIP多模态模型,它采用了更优的损失函数。Sigmoid损失仅作用于图像 - 文本对,无需对成对相似度进行全局归一化,这使得它在扩大批量大小的同时,在小批量下也能有出色表现。
✨ 主要特性
- 更优损失函数:Sigmoid损失无需全局归一化,可扩大批量大小,小批量表现也佳。
- 多用途:可用于零样本图像分类和图像 - 文本检索等任务。
📦 安装指南
文档未提及安装步骤,可参考Hugging Face的通用安装方法安装相关依赖库。
💻 使用示例
基础用法
以下是使用该模型进行零样本图像分类的示例代码:
from PIL import Image
import requests
from transformers import AutoProcessor, AutoModel
import torch
model = AutoModel.from_pretrained("google/siglip-base-patch16-512")
processor = AutoProcessor.from_pretrained("google/siglip-base-patch16-512")
url = "http://images.cocodataset.org/val2017/000000039769.jpg"
image = Image.open(requests.get(url, stream=True).raw)
texts = ["a photo of 2 cats", "a photo of 2 dogs"]
inputs = processor(text=texts, images=image, padding="max_length", return_tensors="pt")
with torch.no_grad():
outputs = model(**inputs)
logits_per_image = outputs.logits_per_image
probs = torch.sigmoid(logits_per_image)
print(f"{probs[0][0]:.1%} that image 0 is '{texts[0]}'")
高级用法
也可以使用pipeline API简化操作:
from transformers import pipeline
from PIL import Image
import requests
image_classifier = pipeline(task="zero-shot-image-classification", model="google/siglip-base-patch16-512")
url = 'http://images.cocodataset.org/val2017/000000039769.jpg'
image = Image.open(requests.get(url, stream=True).raw)
outputs = image_classifier(image, candidate_labels=["2 cats", "a plane", "a remote"])
outputs = [{"score": round(output["score"], 4), "label": output["label"] } for output in outputs]
print(outputs)
更多代码示例可参考文档。
📚 详细文档
预期用途与限制
可以使用原始模型进行零样本图像分类和图像 - 文本检索等任务。可在模型中心查找其他版本以满足需求。
训练过程
训练数据
SigLIP在WebLI数据集的英文图像 - 文本对上进行预训练,相关论文为(Chen et al., 2023)。
预处理
- 图像:将图像调整/缩放至相同分辨率(512x512),并在RGB通道上进行归一化,均值为(0.5, 0.5, 0.5),标准差为(0.5, 0.5, 0.5)。
- 文本:对文本进行分词并填充至相同长度(64个标记)。
计算资源
该模型在16个TPU - v4芯片上训练了三天。
评估结果
SigLIP与CLIP的评估对比结果如下(取自论文):

BibTeX引用
@misc{zhai2023sigmoid,
title={Sigmoid Loss for Language Image Pre-Training},
author={Xiaohua Zhai and Basil Mustafa and Alexander Kolesnikov and Lucas Beyer},
year={2023},
eprint={2303.15343},
archivePrefix={arXiv},
primaryClass={cs.CV}
}
📄 许可证
本模型采用Apache 2.0许可证。