🚀 SigLIP (形狀優化模型)
SigLIP 是一個多模態模型,它在 CLIP 的基礎上改進了損失函數,能更好地處理圖像 - 文本對,可用於零樣本圖像分類和圖像 - 文本檢索等任務。
🚀 快速開始
SigLIP 是在分辨率為 384x384 的 WebLi 上預訓練的模型。它由 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 的簡要總結可在 此處 找到。
📦 安裝指南
文檔未提及安裝步驟,可參考 Hugging Face 相關庫的安裝方式來使用此模型。
💻 使用示例
基礎用法
以下是如何使用此模型進行零樣本圖像分類的示例:
from PIL import Image
import requests
from transformers import AutoProcessor, AutoModel
import torch
model = AutoModel.from_pretrained("google/siglip-so400m-patch14-384")
processor = AutoProcessor.from_pretrained("google/siglip-so400m-patch14-384")
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-so400m-patch14-384")
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 等人, 2023) 上進行預訓練。
預處理
- 圖像:圖像被調整大小/重新縮放至相同分辨率(384x384),並在 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 許可證。