模型概述
模型特點
模型能力
使用案例
🚀 PaliGemma模型卡片
PaliGemma是一款多功能輕量級視覺語言模型(VLM),靈感源自PaLI - 3,基於開放組件構建,可接收圖像和文本輸入並生成文本輸出,支持多語言,適用於多種視覺語言任務。
🚀 快速開始
PaliGemma是單輪視覺語言模型,不適用於對話場景,在針對特定用例進行微調時效果最佳。你可以通過任務前綴(如“detect”或“segment”)來配置模型要解決的任務。預訓練模型以這種方式訓練,具備豐富的能力,但不建議直接使用,而是通過微調遷移到特定任務。對於交互式測試,可使用“mix”系列模型。
✨ 主要特性
- 多功能性:可用於圖像和短視頻字幕生成、視覺問答、文本閱讀、目標檢測和分割等多種視覺語言任務。
- 多語言支持:支持多種語言的輸入和輸出。
- 可微調性:預訓練模型可針對特定領域和任務進行微調。
📦 安裝指南
若要使用8位或4位精度自動運行推理,需要安裝bitsandbytes
:
pip install bitsandbytes accelerate
💻 使用示例
基礎用法
在CPU上以默認精度(float32
)運行:
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上以其他精度運行:
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是一款多功能輕量級視覺語言模型(VLM),受PaLI - 3啟發,基於開放組件(如SigLIP視覺模型和Gemma語言模型)構建。它接收圖像和文本輸入,生成文本輸出,支持多語言,適用於多種視覺語言任務。
模型架構
PaliGemma由Transformer解碼器和視覺Transformer圖像編碼器組成,共有30億參數。文本解碼器從Gemma - 2B初始化,圖像編碼器從SigLIP - So400m/14初始化。
輸入和輸出
- 輸入:圖像和文本字符串,如為圖像添加字幕的提示或問題。
- 輸出:針對輸入生成的文本,如圖像字幕、問題答案、目標邊界框座標列表或分割碼字。
模型數據
預訓練數據集
PaliGemma在以下數據集的混合上進行預訓練:
- WebLI:WebLI(Web Language Image)是一個基於公共網絡構建的網絡規模多語言圖像 - 文本數據集。使用多種WebLI分割來獲取多功能模型能力。
- CC3M - 35L:從網頁中精選的英語圖像 - 替代文本對,使用Google Cloud Translation API翻譯成34種其他語言。
- VQ²A - CC3M - 35L/VQG - CC3M - 35L:VQ2A - CC3M的一個子集,使用Google Cloud Translation API翻譯成與CC3M - 35L相同的34種其他語言。
- OpenImages:通過手工規則在OpenImages數據集上生成的檢測和目標感知問題與答案。
- WIT:從維基百科收集的圖像和文本。
數據責任過濾
為了在乾淨的數據上訓練PaliGemma,對WebLI應用了以下過濾器:
- 色情圖像過濾:移除被認為具有色情性質的圖像。
- 文本安全過濾:識別並過濾與不安全文本配對的圖像。
- 文本毒性過濾:使用Perspective API識別並過濾與被認為具有侮辱性、淫穢、仇恨或其他毒性的文本配對的圖像。
- 文本個人信息過濾:使用Cloud Data Loss Prevention (DLP) API過濾某些個人信息和其他敏感數據。
- 其他方法:根據內容質量和安全性進行過濾。
實現信息
硬件
PaliGemma使用最新一代的張量處理單元(TPU)硬件(TPUv5e)進行訓練。
軟件
訓練使用了JAX、Flax、TFDS和big_vision
。JAX允許研究人員利用最新一代的硬件進行更快、更高效的大模型訓練。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(Eval of COCO captions transfer) | CIDEr(驗證) | 121.72 | 123.58 | |
COCO - 35L(訓練) | CIDEr dev(en/avg - 34/avg) | 139.2 115.8 116.4 |
141.2 118.0 118.6 |
|
XM3600(Eval of COCO - 35L transfer) | CIDEr dev(en/avg - 34/avg) | 78.1 41.3 42.4 |
80.0 41.9 42.9 |
|
TextCaps(訓練) | CIDEr(驗證) | 127.48 | 153.94 | |
SciCap(first sentence, no subfigure)(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(Eval of VQAv2 transfer) | 配對準確率 | 47.33 | 45.33 | |
POPE(Eval of VQAv2 transfer) | 準確率(隨機/流行/對抗) | 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 balanced) | 65.61 | 67.03 | |
[xGQA](https://aclanthology.org/2022.findings - acl.196/)(Eval of GQA transfer) | 平均準確率(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/)(Eval of NLVR2 transfer) | 平均準確率(測試)(id, sw, ta, tr, zh) | 80.57 | 76.78 | |
AI2D(訓練) | 準確率(測試) | 72.12 | 73.28 | |
ScienceQA(Img subset, no CoT)(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(combined refcoco, refcoco+, refcocog excluding val and test images) | MIoU(驗證)refcoco/refcoco+/refcocog | 73.40 68.32 67.65 |
75.57 69.76 70.17 |
76.94 72.18 72.22 |
視頻任務(字幕/問答) | ||||
MSR - VTT(字幕生成) | CIDEr(測試) | 70.54 | ||
MSR - VTT(問答) | 準確率(測試) | 50.09 | ||
ActivityNet(字幕生成) | CIDEr(測試) | 34.62 | ||
ActivityNet(問答) | 準確率(測試) | 50.78 | ||
VATEX(字幕生成) | CIDEr(測試) | 79.73 | ||
MSVD(問答) | 準確率(測試) | 60.22 |
倫理與安全
評估方法
評估方法包括結構化評估和相關內容政策的內部紅隊測試。紅隊測試由多個不同團隊進行,每個團隊有不同的目標和人工評估指標。模型針對與倫理和安全相關的多個類別進行評估,包括:
- 對涵蓋兒童安全、內容安全和代表性危害的提示進行人工評估。更多評估方法細節見Gemma模型卡片,但採用圖像字幕和視覺問答設置。
- 圖像到文本基準測試評估:針對相關學術數據集(如FairFace數據集)進行基準測試。
評估結果
- 倫理和安全評估的人工評估結果在可接受的閾值內,符合[內部政策](https://storage.googleapis.com/gweb - uniblog - publish - prod/documents/2023_Google_AI_Principles_Progress_Update.pdf#page=11)。
- 使用Perspective API(閾值為0.8)測量從FairFace數據集獲取的圖像生成字幕中的毒性、褻瀆和其他潛在問題。報告了每個感知性別、種族和年齡屬性子組中觀察到的最大值和中位數。
指標 | 感知性別 | 種族 | 年齡組 | |||
---|---|---|---|---|---|---|
最大值 | 中位數 | 最大值 | 中位數 | 最大值 | 中位數 | |
毒性 | 0.04% | 0.03% | 0.08% | 0.00% | 0.09% | 0.00% |
身份攻擊 | 0.00% | 0.00% | 0.00% | 0.00% | 0.00% | 0.00% |
侮辱 | 0.06% | 0.04% | 0.09% | 0.07% | 0.16% | 0.00% |
威脅 | 0.06% | 0.05% | 0.14% | 0.05% | 0.17% | 0.00% |
褻瀆 | 0.00% | 0.00% | 0.00% | 0.00% | 0.00% | 0.00% |
使用和限制
預期用途
開放視覺語言模型(VLMs)在各個行業和領域有廣泛應用。以下潛在用途列表並非詳盡無遺,旨在提供模型創建者在模型訓練和開發過程中考慮的可能用例的上下文信息。
特定視覺語言任務微調
- 預訓練模型可在多種視覺語言任務(如圖像字幕、短視頻字幕、視覺問答、文本閱讀、目標檢測和分割)上進行微調。
- 可針對特定領域(如遙感問答、盲人視覺問題、科學問答、描述UI元素功能)進行微調。
- 可針對具有非文本輸出(如邊界框或分割掩碼)的任務進行微調。
視覺語言研究
預訓練模型和微調模型可作為研究人員實驗VLM技術、開發算法和推動該領域發展的基礎。
倫理考慮和風險
視覺語言模型(VLMs)的開發引發了一些倫理問題。在創建開放模型時,我們仔細考慮了以下方面:
- 偏差和公平性:在大規模真實世界圖像 - 文本數據上訓練的VLMs可能反映訓練材料中嵌入的社會文化偏差。這些模型經過仔細審查,本卡片中描述了輸入數據預處理和後續評估。
- 錯誤信息和濫用:VLMs可能被濫用於生成虛假、誤導或有害的文本。提供了負責任使用模型的指南,見Responsible Generative AI Toolkit。
- 透明度和問責制:本模型卡片總結了模型的架構、能力、限制和評估過程的詳細信息。一個負責任開發的開放模型為AI生態系統中的開發者和研究人員提供了分享創新和獲取VLM技術的機會。
已識別的風險和緩解措施
- 偏差的延續:鼓勵在模型訓練、微調等用例中進行持續監測(使用評估指標、人工審查)並探索去偏技術。
- 有害內容的生成:內容安全機制和指南至關重要。開發者應謹慎行事,並根據其特定產品政策和應用用例實施適當的內容安全保障措施。
- 惡意用途:技術限制和開發者及最終用戶教育有助於減輕LLMs的惡意應用。提供了教育資源和用戶舉報濫用的機制。Gemma模型的禁止使用規定在Gemma Prohibited Use Policy中列出。
- 隱私侵犯:模型在經過過濾以去除某些個人信息和敏感數據的數據上進行訓練。鼓勵開發者遵守隱私法規並採用隱私保護技術。
限制
- 大多數繼承自基礎Gemma模型的限制仍然適用:
- VLMs更擅長可以用清晰提示和說明構建的任務。開放式或高度複雜的任務可能具有挑戰性。
- 自然語言本質上很複雜。VLMs可能難以理解微妙的細微差別、諷刺或比喻語言。
- VLMs根據從訓練數據集中學到的信息生成響應,但它們不是知識庫。它們可能生成不正確或過時的事實陳述。
- VLMs依賴於語言和圖像中的統計模式。它們可能在某些情況下缺乏應用常識推理的能力。
- PaliGemma首先是作為通用預訓練模型設計的,用於遷移到專門任務。因此,其“開箱即用”或“零樣本”性能可能落後於專門為此設計的模型。
- PaliGemma不是多輪聊天機器人。它設計用於單輪圖像和文本輸入。
🔧 技術細節
PaliGemma由Transformer解碼器和視覺Transformer圖像編碼器組成,使用TPUv5e硬件和JAX、Flax、TFDS等軟件進行訓練。在訓練過程中,對數據進行了責任過濾,以確保模型在乾淨的數據上學習。
📄 許可證
本項目採用gemma許可證。
📚 引用
@article{beyer2024paligemma,
title={{PaliGemma: A versatile 3B VLM for transfer}},
author={Lucas Beyer* and Andreas Steiner* and André Susano Pinto* and Alexander Kolesnikov* and Xiao Wang* and Daniel Salz and Maxim Neumann and Ibrahim Alabdulmohsin and Michael Tschannen and Emanuele Bugliarello and Thomas Unterthiner and Daniel Keysers and Skanda Koppula and Fangyu Liu and Adam Grycner and Alexey Gritsenko and Neil Houlsby and Manoj Kumar and Keran Rong and Julian Eisenschlos and Rishabh Kabra and Matthias Bauer and Matko Bošnjak and Xi Chen and Matthias Minderer and Paul Voigtlaender and Ioana Bica and Ivana Balazevic and Joan Puigcerver and Pinelopi Papalampidi and Olivier Henaff and Xi Xiong and Radu Soricut and Jeremiah Harmsen and Xiaohua Zhai*},
year={2024},
journal={arXiv preprint arXiv:2407.07726}
}
論文鏈接:here








