模型概述
模型特點
模型能力
使用案例
🚀 PaliGemma模型卡片
PaliGemma是一款多功能輕量級視覺語言模型(VLM),它以圖像和文本作為輸入,生成文本輸出,支持多種語言。該模型適用於圖像和短視頻字幕、視覺問答、文本閱讀、目標檢測和目標分割等多種視覺語言任務。
🚀 快速開始
訪問權限
要在Hugging Face上訪問PaliGemma,你需要查看並同意Google的使用許可。請確保你已登錄Hugging Face,然後點擊下方按鈕。請求將立即處理。 [確認許可](Acknowledge license)
模型頁面
資源與技術文檔
使用條款
作者
✨ 主要特性
模型概述
描述
PaliGemma是受PaLI - 3啟發,基於SigLIP視覺模型和Gemma語言模型等開放組件構建的多功能輕量級視覺語言模型(VLM)。它接受圖像和文本作為輸入,生成文本輸出,支持多種語言,適用於多種視覺語言任務。
模型架構
PaliGemma由Transformer解碼器和視覺Transformer圖像編碼器組成,總參數為30億。文本解碼器從Gemma - 2B初始化,圖像編碼器從SigLIP - So400m/14初始化,訓練遵循PaLI - 3的方法。
輸入與輸出
- 輸入:圖像和文本字符串,如為圖像添加字幕的提示或問題。
- 輸出:對輸入的響應生成的文本,如圖像字幕、問題答案、目標邊界框座標列表或分割碼字。
模型數據
預訓練數據集
PaliGemma在以下數據集的混合上進行預訓練:
- WebLI:WebLI(網絡語言圖像)是一個基於公共網絡構建的網絡規模多語言圖像 - 文本數據集,用於獲取模型的多種能力。
- CC3M - 35L:從網頁中精心挑選的英語圖像 - 替代文本對,並使用Google Cloud Translation API翻譯成34種其他語言。
- VQ²A - CC3M - 35L/VQG - CC3M - 35L:VQ2A - CC3M的一個子集,同樣翻譯成34種其他語言。
- OpenImages:通過手工規則在OpenImages數據集上生成的檢測和目標感知問答。
- WIT:從維基百科收集的圖像和文本。
數據責任過濾
為了在乾淨的數據上訓練PaliGemma,對WebLI應用了以下過濾:
- 色情圖像過濾:移除被認為具有色情性質的圖像。
- 文本安全過濾:識別並過濾與不安全文本配對的圖像,不安全文本包括包含兒童性虐待、色情、粗俗或冒犯性內容的文本。
- 文本毒性過濾:使用Perspective API識別並過濾與被認為具有侮辱性、淫穢、仇恨或其他毒性的文本配對的圖像。
- 文本個人信息過濾:使用Cloud Data Loss Prevention (DLP) API過濾某些個人信息和其他敏感數據,保護個人隱私。
- 其他方法:根據內容質量和安全性進行過濾,符合相關政策和實踐。
📦 安裝指南
若要使用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上運行其他精度
為了方便起見,倉庫中包含已轉換為bfloat16
和float16
的權重版本,你可以使用它們來減小下載大小並避免在本地計算機上進行類型轉換。以下是在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 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)
# 指示模型用西班牙語創建字幕
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使用最新一代的張量處理單元(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 dev(en/avg - 34/avg) | 139.2 115.8 116.4 |
141.2 118.0 118.6 |
|
XM3600(COCO - 35L轉移評估) | 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(第一句,無子圖)(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(圖像子集,無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](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(字幕生成) | 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% |
使用與限制
預期用途
開放視覺語言模型(VLM)在各個行業和領域有廣泛的應用。以下是可能的用途列表,但不全面,旨在提供模型創建者在模型訓練和開發過程中考慮的可能用例的上下文信息。
- 特定視覺語言任務微調:預訓練模型可在多種視覺語言任務(如圖像字幕、短視頻字幕、視覺問答、文本閱讀、目標檢測和目標分割)上進行微調,也可針對特定領域(如遙感問答、盲人視覺問題、科學問答、描述UI元素功能)進行微調,還可用於具有非文本輸出(如邊界框或分割掩碼)的任務。
- 視覺語言研究:預訓練模型和微調模型可作為研究人員試驗VLM技術、開發算法和推動該領域發展的基礎。
倫理考慮與風險
視覺語言模型(VLM)的開發引發了一些倫理問題,在創建開放模型時,仔細考慮了以下方面:
- 偏差與公平性:在大規模真實世界圖像 - 文本數據上訓練的VLM可能反映訓練材料中嵌入的社會文化偏差,對這些模型進行了仔細審查、輸入數據預處理和後續評估。
- 錯誤信息與濫用:VLM可能被濫用來生成虛假、誤導或有害的文本,提供了負責任使用模型的指南,見Responsible Generative AI Toolkit。
- 透明度與問責制:本模型卡片總結了模型的架構、能力、限制和評估過程的詳細信息,一個負責任開發的開放模型為AI生態系統中的開發者和研究人員提供了共享創新的機會。
風險識別與緩解
- 偏差的延續:鼓勵在模型訓練、微調及其他用例中進行持續監測(使用評估指標、人工審查)並探索去偏技術。
- 有害內容的生成:內容安全機制和指南至關重要,鼓勵開發者根據具體產品政策和應用用例謹慎行事並實施適當的內容安全保障措施。
- 惡意用途的濫用:技術限制以及對開發者和最終用戶的教育有助於減輕大語言模型的惡意應用,提供了教育資源和用戶舉報濫用的機制,Gemma模型的禁止使用情況在Gemma Prohibited Use Policy中列出。
- 隱私侵犯:模型在經過過濾以去除某些個人信息和敏感數據的數據上進行訓練,鼓勵開發者遵守隱私法規並採用隱私保護技術。
限制
- 大多數繼承自基礎Gemma模型的限制仍然適用:VLM更擅長可以用明確提示和指令構建的任務,開放式或高度複雜的任務可能具有挑戰性;自然語言本質上很複雜,VLM可能難以理解微妙的細微差別、諷刺或比喻語言;VLM根據從訓練數據中學到的信息生成響應,但它們不是知識庫,可能會生成不正確或過時的事實陳述;VLM依賴於語言和圖像中的統計模式,在某些情況下可能缺乏應用常識推理的能力。
- PaliGemma首先是作為一個通用預訓練模型設計的,用於轉移到專門任務,因此其“開箱即用”或“零樣本”性能可能落後於專門為此設計的模型。
- PaliGemma不是多輪聊天機器人,它設計用於單輪圖像和文本輸入。
📄 許可證
本項目採用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








