license: apache-2.0
tags:
- 视觉
widget:
- src: >-
https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/bee.jpg
candidate_labels: 天空中的蜜蜂, 花朵上的蜜蜂
example_title: 蜜蜂
library_name: transformers
pipeline_tag: 零样本图像分类
SigLIP(形状优化模型)
SigLIP模型在WebLi数据集上以224x224分辨率预训练完成。该模型由Zhai等人在论文Sigmoid Loss for Language Image Pre-Training中提出,并首次发布于此代码库。
该模型采用SoViT-400m架构,这是Alabdulmohsin等人在Getting ViT in Shape: Scaling Laws for Compute-Optimal Model Design中提出的形状优化版本。
免责声明:发布SigLIP的团队未为此模型编写模型卡,本模型卡由Hugging Face团队撰写。
模型描述
SigLIP是CLIP的改进版本,一种多模态模型,采用了更优的损失函数。Sigmoid损失仅作用于图像-文本对,无需对成对相似性进行全局归一化。这使得在扩大批处理规模的同时,也能在小批量情况下表现更佳。
作者之一关于SigLIP的简要说明可参见此处。
预期用途与限制
您可将原始模型用于零样本图像分类和图像-文本检索等任务。访问模型中心查找其他任务相关版本。
使用方法
以下是如何使用该模型进行零样本图像分类:
from PIL import Image
import requests
from transformers import AutoProcessor, AutoModel
import torch
model = AutoModel.from_pretrained("google/siglip-so400m-patch14-224")
processor = AutoProcessor.from_pretrained("google/siglip-so400m-patch14-224")
url = "http://images.cocodataset.org/val2017/000000039769.jpg"
image = Image.open(requests.get(url, stream=True).raw)
texts = ["两只猫的照片", "两只狗的照片"]
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"图像0是'{texts[0]}'的概率为{probs[0][0]:.1%}")
也可使用简化复杂度的pipeline API:
from transformers import pipeline
from PIL import Image
import requests
image_classifier = pipeline(task="zero-shot-image-classification", model="google/siglip-so400m-patch14-224")
url = 'http://images.cocodataset.org/val2017/000000039769.jpg'
image = Image.open(requests.get(url, stream=True).raw)
outputs = image_classifier(image, candidate_labels=["两只猫", "一架飞机", "一个遥控器"])
outputs = [{"score": round(output["score"], 4), "label": output["label"] } for output in outputs]
print(outputs)
更多代码示例请参阅文档。
训练流程
训练数据
SigLIP在WebLI数据集(Chen et al., 2023)上预训练。
预处理
图像调整至统一分辨率(384x384),并通过RGB通道标准化(均值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}
}