模型概述
模型特點
模型能力
使用案例
🚀 PaliGemma模型卡片
PaliGemma是一款多功能輕量級視覺語言模型(VLM),它結合圖像和文本輸入,生成文本輸出,支持多語言。該模型專為視覺語言任務的微調性能而設計,可用於圖像和短視頻字幕、視覺問答、文本閱讀、目標檢測和目標分割等多種場景。
🚀 快速開始
PaliGemma是單輪視覺語言模型,不適用於對話場景,在針對特定用例進行微調時效果最佳。你可以通過任務前綴(如“detect”或“segment”)來配置模型要解決的任務。預訓練模型以這種方式訓練,具備豐富的能力(問答、字幕生成、分割等),但並非直接使用,而是通過微調遷移到特定任務。對於交互式測試,可使用“mix”系列模型,它們已在多種任務的混合數據集上進行了微調。
若需瞭解預期用例,請參考使用和限制部分;若需更多詳細信息和示例,請訪問博客文章。
✨ 主要特性
- 多功能性:結合圖像和文本輸入,生成文本輸出,支持多語言。
- 輕量級:基於開放組件構建,易於使用和部署。
- 高性能:在多種視覺語言任務上表現出色,如問答、字幕生成、分割等。
📦 安裝指南
若要使用8位或4位精度自動運行推理,需要安裝bitsandbytes
:
pip install bitsandbytes accelerate
💻 使用示例
基礎用法
以下代碼展示瞭如何在CPU上以默認精度(float32
)運行PaliGemma模型:
from transformers import AutoProcessor, PaliGemmaForConditionalGeneration
from PIL import Image
import requests
import torch
model_id = "google/paligemma-3b-mix-224"
url = "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/tasks/car.jpg?download=true"
image = Image.open(requests.get(url, stream=True).raw)
model = PaliGemmaForConditionalGeneration.from_pretrained(model_id).eval()
processor = AutoProcessor.from_pretrained(model_id)
# Instruct the model to create a caption in Spanish
prompt = "caption es"
model_inputs = processor(text=prompt, images=image, return_tensors="pt")
input_len = model_inputs["input_ids"].shape[-1]
with torch.inference_mode():
generation = model.generate(**model_inputs, max_new_tokens=100, do_sample=False)
generation = generation[0][input_len:]
decoded = processor.decode(generation, skip_special_tokens=True)
print(decoded)
輸出:Un auto azul estacionado frente a un edificio.
高級用法
在CUDA上運行其他精度
以下代碼展示瞭如何在NVIDIA CUDA卡上以bfloat16
精度運行模型:
from transformers import AutoProcessor, PaliGemmaForConditionalGeneration
from PIL import Image
import requests
import torch
model_id = "google/paligemma-3b-mix-224"
device = "cuda:0"
dtype = torch.bfloat16
url = "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/tasks/car.jpg?download=true"
image = Image.open(requests.get(url, stream=True).raw)
model = PaliGemmaForConditionalGeneration.from_pretrained(
model_id,
torch_dtype=dtype,
device_map=device,
revision="bfloat16",
).eval()
processor = AutoProcessor.from_pretrained(model_id)
# Instruct the model to create a caption in Spanish
prompt = "caption es"
model_inputs = processor(text=prompt, images=image, return_tensors="pt").to(model.device)
input_len = model_inputs["input_ids"].shape[-1]
with torch.inference_mode():
generation = model.generate(**model_inputs, max_new_tokens=100, do_sample=False)
generation = generation[0][input_len:]
decoded = processor.decode(generation, skip_special_tokens=True)
print(decoded)
以4位/8位精度加載模型
from transformers import AutoProcessor, PaliGemmaForConditionalGeneration
from PIL import Image
import requests
import torch
from bitsandbytes.nn import BitsAndBytesConfig
model_id = "google/paligemma-3b-mix-224"
device = "cuda:0"
dtype = torch.bfloat16
url = "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/tasks/car.jpg?download=true"
image = Image.open(requests.get(url, stream=True).raw)
quantization_config = BitsAndBytesConfig(load_in_8bit=True)
model = PaliGemmaForConditionalGeneration.from_pretrained(
model_id, quantization_config=quantization_config
).eval()
processor = AutoProcessor.from_pretrained(model_id)
# Instruct the model to create a caption in Spanish
prompt = "caption es"
model_inputs = processor(text=prompt, images=image, return_tensors="pt").to(model.device)
input_len = model_inputs["input_ids"].shape[-1]
with torch.inference_mode():
generation = model.generate(**model_inputs, max_new_tokens=100, do_sample=False)
generation = generation[0][input_len:]
decoded = processor.decode(generation, skip_special_tokens=True)
print(decoded)
📚 詳細文檔
模型信息
模型概述
PaliGemma受PaLI - 3啟發,基於開放組件(如SigLIP視覺模型和Gemma語言模型)構建。它以圖像和文本為輸入,生成文本輸出,支持多語言。
模型架構
PaliGemma由Transformer解碼器和視覺Transformer圖像編碼器組成,共有30億個參數。文本解碼器從Gemma - 2B初始化,圖像編碼器從SigLIP - So400m/14初始化。模型按照PaLI - 3的方法進行訓練。
輸入和輸出
- 輸入:圖像和文本字符串,如為圖像生成字幕的提示或問題。
- 輸出:根據輸入生成的文本,如圖像字幕、問題答案、目標邊界框座標列表或分割碼字。
模型數據
-
預訓練數據集:PaliGemma在以下混合數據集上進行預訓練:
- WebLI:WebLI (Web Language Image)是一個基於公共網絡構建的網絡規模多語言圖像 - 文本數據集。使用多種WebLI分割來獲得通用的模型能力,如視覺語義理解、目標定位、視覺情境文本理解、多語言能力等。
- CC3M - 35L:從網頁中精選的英語圖像 - 替代文本對(Sharma等人,2018)。使用Google Cloud Translation API翻譯成另外34種語言。
- VQ²A - CC3M - 35L/VQG - CC3M - 35L:VQ2A - CC3M的一個子集(Changpinyo等人,2022a),使用Google Cloud Translation API翻譯成與CC3M - 35L相同的另外34種語言。
- OpenImages:在OpenImages數據集上通過手工規則生成的檢測和目標感知問答(Piergiovanni等人,2022)。
- WIT:從維基百科收集的圖像和文本(Srinivasan等人,2021)。
-
數據責任過濾:為了在乾淨的數據上訓練PaliGemma,對WebLI應用了以下過濾:
- 色情圖像過濾:移除被認為具有色情性質的圖像。
- 文本安全過濾:識別並過濾與不安全文本配對的圖像。不安全文本指包含或涉及兒童性虐待材料、色情內容、粗俗語言或其他冒犯性內容的文本。
- 文本毒性過濾:使用Perspective API識別並過濾與被認為具有侮辱性、淫穢、仇恨或其他毒性的文本配對的圖像。
- 文本個人信息過濾:使用Cloud Data Loss Prevention (DLP) API過濾某些個人信息和其他敏感數據,以保護個人隱私。移除了如社會安全號碼等標識符和其他敏感信息類型。
- 其他方法:根據內容質量和安全性進行過濾,符合我們的政策和實踐。
實現信息
硬件
PaliGemma使用最新一代的張量處理單元(TPU)硬件(TPUv5e)進行訓練。
軟件
訓練使用了JAX、Flax、TFDS和big_vision
。JAX使研究人員能夠利用最新一代的硬件(包括TPU)進行大型模型的更快、更高效的訓練。TFDS用於訪問數據集,Flax用於模型架構。PaliGemma的微調代碼和推理代碼在big_vision
的GitHub倉庫中發佈。
評估信息
基準測試結果
為了驗證PaliGemma在各種學術任務上的可遷移性,我們在每個任務上對預訓練模型進行微調。此外,我們還使用遷移任務的混合數據集訓練了混合模型。我們報告了不同分辨率下的結果,以展示哪些任務從更高的分辨率中受益。重要的是,這些任務和數據集都不是預訓練數據混合的一部分,並且它們的圖像已從網絡規模的預訓練數據中明確移除。
混合模型(在遷移任務的混合數據集上微調)
基準測試 | 指標(分割) | mix - 224 | mix - 448 |
---|---|---|---|
MMVP | 配對準確率 | 46.00 | 45.33 |
POPE | 準確率(隨機/流行/對抗) | 88.00 86.63 85.67 |
89.37 88.40 87.47 |
GQA | 準確率(測試) | 65.20 | 65.47 |
單任務(在單個任務上微調)
基準測試(訓練分割) | 指標(分割) | pt - 224 | pt - 448 | pt - 896 |
---|---|---|---|---|
字幕生成 | ||||
COCO captions(train + restval) | CIDEr(驗證) | 141.92 | 144.60 | - |
NoCaps(COCO字幕遷移評估) | CIDEr(驗證) | 121.72 | 123.58 | - |
COCO - 35L(訓練) | CIDEr開發(英語/平均34種語言/平均) | 139.2 115.8 116.4 |
141.2 118.0 118.6 |
- |
XM3600(COCO - 35L遷移評估) | CIDEr開發(英語/平均34種語言/平均) | 78.1 41.3 42.4 |
80.0 41.9 42.9 |
- |
TextCaps(訓練) | CIDEr(驗證) | 127.48 | 153.94 | - |
SciCap(第一句,無子圖)(train + val) | CIDEr/BLEU - 4(測試) | 162.25 0.192 |
181.49 0.211 |
- |
Screen2words(train + dev) | CIDEr(測試) | 117.57 | 119.59 | - |
Widget Captioning(train + dev) | CIDEr(測試) | 136.07 | 148.36 | - |
問答 | ||||
VQAv2(train + validation) | 準確率(測試服務器 - 標準) | 83.19 | 85.64 | - |
MMVP(VQAv2遷移評估) | 配對準確率 | 47.33 | 45.33 | - |
POPE(VQAv2遷移評估) | 準確率(隨機/流行/對抗) | 87.80 85.87 84.27 |
88.23 86.77 85.90 |
- |
OKVQA(訓練) | 準確率(驗證) | 63.54 | 63.15 | - |
A - OKVQA (MC)(train + val) | 準確率(測試服務器) | 76.37 | 76.90 | - |
A - OKVQA (DA)(train + val) | 準確率(測試服務器) | 61.85 | 63.22 | - |
GQA(train_balanced + val_balanced) | 準確率(testdev平衡) | 65.61 | 67.03 | - |
xGQA(GQA遷移評估) | 平均準確率(bn, de, en, id, ko, pt, ru, zh) | 58.37 | 59.07 | - |
NLVR2(train + dev) | 準確率(測試) | 90.02 | 88.93 | - |
MaRVL(NLVR2遷移評估) | 平均準確率(測試)(id, sw, ta, tr, zh) | 80.57 | 76.78 | - |
AI2D(訓練) | 準確率(測試) | 72.12 | 73.28 | - |
ScienceQA(圖像子集,無CoT)(train + val) | 準確率(測試) | 95.39 | 95.93 | - |
RSVQA - LR(非數字)(train + val) | 平均準確率(測試) | 92.65 | 93.11 | - |
RSVQA - HR(非數字)(train + val) | 平均準確率(測試/test2) | 92.61 90.58 |
92.79 90.54 |
- |
ChartQA(human + aug)x(train + val) | 平均寬鬆準確率(test_human, test_aug) | 57.08 | 71.36 | - |
VizWiz VQA(train + val) | 準確率(測試服務器 - 標準) | 73.7 | 75.52 | - |
TallyQA(訓練) | 準確率(test_simple/test_complex) | 81.72 69.56 |
84.86 72.27 |
- |
OCR - VQA(train + val) | 準確率(測試) | 72.32 | 74.61 | 74.93 |
TextVQA(train + val) | 準確率(測試服務器 - 標準) | 55.47 | 73.15 | 76.48 |
DocVQA(train + val) | ANLS(測試服務器) | 43.74 | 78.02 | 84.77 |
Infographic VQA(train + val) | ANLS(測試服務器) | 28.46 | 40.47 | 47.75 |
SceneText VQA(train + val) | ANLS(測試服務器) | 63.29 | 81.82 | 84.40 |
分割 | ||||
RefCOCO(組合refcoco, refcoco +, refcocog,不包括驗證和測試圖像) | MIoU(驗證)refcoco/refcoco +/refcocog | 73.40 68.32 67.65 |
75.57 69.76 70.17 |
76.94 72.18 72.22 |
視頻任務(字幕/問答) | ||||
MSR - VTT(字幕) |
📄 許可證
本模型遵循gemma許可證。
⚠️ 重要提示
要在Hugging Face上訪問PaliGemma,你需要審查並同意Google的使用許可。請確保你已登錄Hugging Face並點擊下方按鈕。請求將立即處理。
💡 使用建議
點擊以下按鈕確認許可:Acknowledge license









