模型概述
模型特點
模型能力
使用案例
🚀 PaliGemma模型卡片
PaliGemma是一款多功能輕量級視覺語言模型(VLM),它結合圖像和文本輸入,生成文本輸出,支持多語言。該模型基於開放組件構建,適用於圖像和短視頻字幕、視覺問答、文本閱讀、目標檢測和目標分割等多種視覺語言任務。
模型信息
屬性 | 詳情 |
---|---|
模型類型 | 視覺語言模型 |
訓練數據 | WebLI、CC3M - 35L、VQ²A - CC3M - 35L/VQG - CC3M - 35L、OpenImages、WIT |
模型頁面
資源和技術文檔
使用條款
作者
✨ 主要特性
- 多功能性:支持圖像和文本輸入,可完成多種視覺語言任務,如問答、字幕生成、分割等。
- 多語言支持:能夠處理多種語言的輸入和輸出。
- 輕量級設計:適合在不同場景下進行微調使用。
📦 安裝指南
若要使用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)
# 指示模型創建西班牙語字幕
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)
# 指示模型創建西班牙語字幕
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 transformers 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)
# 指示模型創建西班牙語字幕
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解碼器和Vision Transformer圖像編碼器組成,共有30億個參數。文本解碼器從Gemma - 2B初始化,圖像編碼器從SigLIP - So400m/14初始化。PaliGemma按照PaLI - 3的配方進行訓練。
輸入和輸出
- 輸入:圖像和文本字符串,如為圖像添加字幕的提示或問題。
- 輸出:針對輸入生成的文本,如圖像字幕、問題答案、目標邊界框座標列表或分割碼字。
模型數據
預訓練數據集
PaliGemma在以下數據集的混合上進行預訓練:
- WebLI:WebLI (Web Language Image)是一個基於公共網絡構建的網絡規模多語言圖像 - 文本數據集。使用多種WebLI分割來獲得模型的多功能能力,如視覺語義理解、目標定位、視覺情境文本理解、多語言能力等。
- CC3M - 35L:從網頁中精心挑選的英語圖像 - 替代文本對([Sharma et al., 2018](https://aclanthology.org/P18 - 1238/))。使用Google Cloud Translation API將其翻譯成另外34種語言。
- VQ²A - CC3M - 35L/VQG - CC3M - 35L:VQ2A - CC3M的一個子集([Changpinyo et al., 2022a](https://aclanthology.org/2022.naacl - main.142/)),使用Google Cloud Translation API翻譯成與CC3M - 35L相同的另外34種語言。
- OpenImages:基於OpenImages數據集通過手工規則生成的檢測和目標感知問答數據(Piergiovanni et al. 2022)。
- WIT:從維基百科收集的圖像和文本(Srinivasan et al., 2021)。
數據責任過濾
為了在乾淨的數據上訓練PaliGemma,對WebLI應用了以下過濾:
- 色情圖像過濾:移除被認為具有色情性質的圖像。
- 文本安全過濾:識別並過濾與不安全文本配對的圖像。不安全文本是指包含或關於兒童性虐待材料、色情內容、粗俗語言或其他冒犯性內容的文本。
- 文本毒性過濾:使用Perspective API識別並過濾與被認為具有侮辱性、淫穢、仇恨或其他毒性的文本配對的圖像。
- 文本個人信息過濾:使用Cloud Data Loss Prevention (DLP) API過濾某些個人信息和其他敏感數據,以保護個人隱私。移除了如社會保險號碼等標識符和[其他敏感信息類型](https://cloud.google.com/sensitive - data - protection/docs/high - sensitivity - infotypes - reference?_gl=1jg604m_gaODk5MzA3ODQyLjE3MTAzMzQ3NTk._ga_WH2QY8WWF5*MTcxMDUxNTkxMS4yLjEuMTcxMDUxNjA2NC4wLjAuMA..&_ga=2.172110058.-899307842.1710334759)。
- 其他方法:根據內容質量和安全性,按照相關政策和實踐進行過濾。
如何使用
PaliGemma是單輪視覺語言模型,不適合對話式使用,在針對特定用例進行微調時效果最佳。
可以通過使用任務前綴(如“detect”或“segment”)來配置模型要解決的任務。預訓練模型以這種方式進行訓練,以賦予它們豐富的能力(問答、字幕生成、分割等)。然而,它們並非設計用於直接使用,而是通過微調轉移到使用類似提示結構的特定任務。對於交互式測試,可以使用“mix”系列模型,這些模型已在多種任務的混合上進行了微調。
如需瞭解預期用例,請參考[使用和限制部分](#usage - and - limitations),或訪問[博客文章](https://huggingface.co/blog/paligemma - google - vlm)獲取更多詳細信息和示例。
實現信息
硬件
PaliGemma使用最新一代的張量處理單元(TPU)硬件(TPUv5e)進行訓練。
軟件
訓練使用了JAX、Flax、TFDS和[big_vision
](https://github.com/google - research/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](https://allenai.org/project/a - okvqa/home) (MC)(train + val) | 準確率(測試服務器) | 76.37 | 76.90 | |
[A - OKVQA](https://allenai.org/project/a - okvqa/home) (DA)(train + val) | 準確率(測試服務器) | 61.85 | 63.22 | |
GQA(train_balanced + val_balanced) | 準確率(testdev平衡) | 65.61 | 67.03 | |
[xGQA](https://aclanthology.org/2022.findings - acl.196/)(GQA轉移評估) | 平均準確率(bn, de, en, id, ko, pt, ru, zh) | 58.37 | 59.07 | |
NLVR2(train + dev) | 準確率(測試) | 90.02 | 88.93 | |
[MaRVL](https://marvl - challenge.github.io/)(NLVR2轉移評估) | 平均準確率(測試)(id, sw, ta, tr, zh) | 80.57 | 76.78 | |
AI2D(訓練) | 準確率(測試) | 72.12 | 73.28 | |
ScienceQA(圖像子集,無思維鏈)(train + val) | 準確率(測試) | 95.39 | 95.93 | |
RSVQA - LR (Non numeric)(train + val) | 平均準確率(測試) | 92.65 | 93.11 | |
RSVQA - HR (Non numeric)(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](https://vizwiz.org/tasks - and - datasets/vqa/)(train + val) | 準確率(測試服務器 - 標準) | 73.7 | 75.52 | |
TallyQA(訓練) | 準確率(test_simple/test_complex) | 81.72 69.56 |
84.86 72.27 |
|
[OCR - VQA](https://ocr - vqa.github.io/)(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 (Cap |
📄 許可證
Gemma
訪問提示
⚠️ 重要提示
要在Hugging Face上訪問PaliGemma,您需要審查並同意Google的使用許可。為此,請確保您已登錄Hugging Face並點擊下方按鈕。請求將立即處理。
💡 使用建議
PaliGemma是單輪視覺語言模型,不適合對話式使用,在針對特定用例進行微調時效果最佳。可以使用任務前綴(如“detect”或“segment”)配置模型要解決的任務。









