模型概述
模型特點
模型能力
使用案例
🚀 PaliGemma模型卡片
PaliGemma是一款多功能輕量級視覺語言模型(VLM),它以圖像和文本作為輸入,生成文本輸出,支持多種語言。該模型可用於圖像和短視頻字幕、視覺問答、文本閱讀、目標檢測和目標分割等多種視覺語言任務。
🚀 快速開始
PaliGemma是單輪視覺語言模型,不適用於對話場景,在針對特定用例進行微調時效果最佳。你可以通過任務前綴(如 “detect” 或 “segment”)來配置模型要解決的任務。預訓練模型以這種方式進行訓練,以賦予它們豐富的能力(問答、字幕、分割等)。不過,它們並非設計用於直接使用,而是通過微調轉移到使用類似提示結構的特定任務。對於交互式測試,你可以使用 “mix” 系列模型,這些模型已針對多種任務進行了微調。要查看模型 google/paligemma-3b-mix-448 的實際運行情況,請查看 使用Transformers代碼庫的Space。
請參考使用和限制部分瞭解預期用例,或訪問博客文章獲取更多詳細信息和示例。
✨ 主要特性
- 多功能性:支持圖像和文本輸入,能處理圖像和短視頻字幕、視覺問答、文本閱讀、目標檢測和目標分割等多種視覺語言任務。
- 多語言支持:可以處理多種語言的輸入和輸出。
- 輕量級:模型參數相對較少,便於在不同設備上進行部署和微調。
📦 安裝指南
你需要安裝 bitsandbytes
以自動使用8位或4位精度運行推理:
pip install bitsandbytes accelerate
💻 使用示例
基礎用法
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上運行其他精度
為了方便起見,倉庫中包含已轉換為 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)
# 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, BitsAndBytesConfig
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)
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語言模型等開放組件構建的多功能輕量級視覺語言模型(VLM)。它以圖像和文本作為輸入,生成文本輸出,支持多種語言。該模型專為在圖像和短視頻字幕、視覺問答、文本閱讀、目標檢測和目標分割等廣泛的視覺語言任務中實現一流的微調性能而設計。
模型架構
PaliGemma由Transformer解碼器和視覺Transformer圖像編碼器組成,總參數為30億。文本解碼器從Gemma - 2B初始化,圖像編碼器從SigLIP - So400m/14初始化。PaliGemma按照PaLI - 3的方法進行訓練。
輸入和輸出
- 輸入:圖像和文本字符串,如圖像字幕提示或問題。
- 輸出:針對輸入生成的文本,如圖像字幕、問題答案、目標邊界框座標列表或分割代碼字。
模型數據
預訓練數據集
PaliGemma在以下數據集的混合上進行預訓練:
- WebLI:WebLI(Web語言圖像)是一個基於公共網絡構建的網絡規模多語言圖像 - 文本數據集。使用了多種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對各種學術任務的可遷移性,我們在每個任務上對預訓練模型進行微調。此外,我們還使用遷移任務的混合訓練了混合模型。我們報告了不同分辨率下的結果,以瞭解哪些任務從更高的分辨率中受益。重要的是,這些任務或數據集都不是預訓練數據混合的一部分,並且它們的圖像已從網絡規模的預訓練數據中明確移除。
單任務(在單任務上微調)
基準測試(訓練分割) | 指標(分割) | pt - 224 | pt - 448 | pt - 896 |
---|---|---|---|---|
字幕生成 | ||||
COCO captions(train + restval) | CIDEr(val) | 141.92 | 144.60 | |
NoCaps(Eval of COCO captions transfer) | CIDEr(val) | 121.72 | 123.58 | |
COCO - 35L(train) | 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(train) | CIDEr(val) | 127.48 | 153.94 | |
SciCap(first sentence, no subfigure)(train + val) | CIDEr/BLEU - 4(test) | 162.25 0.192 |
181.49 0.211 |
|
Screen2words(train + dev) | CIDEr(test) | 117.57 | 119.59 | |
Widget Captioning(train + dev) | CIDEr(test) | 136.07 | 148.36 | |
問答 | ||||
VQAv2(train + validation) | 準確率(Test server - std) | 83.19 | 85.64 | |
MMVP(Eval of VQAv2 transfer) | 配對準確率 | 47.33 | 45.33 | |
POPE(Eval of VQAv2 transfer) | 準確率(random/popular/ adversarial) |
87.80 85.87 84.27 |
88.23 86.77 85.90 |
|
OKVQA(train) | 準確率(val) | 63.54 | 63.15 | |
[A - OKVQA](https://allenai.org/project/a - okvqa/home) (MC)(train + val) | 準確率(Test server) | 76.37 | 76.90 | |
[A - OKVQA](https://allenai.org/project/a - okvqa/home) (DA)(train + val) | 準確率(Test server) | 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) | 準確率(test) | 90.02 | 88.93 | |
[MaRVL](https://marvl - challenge.github.io/)(Eval of NLVR2 transfer) | 平均準確率(test)(id, sw, ta, tr, zh) | 80.57 | 76.78 | |
AI2D(train) | 準確率(test) | 72.12 | 73.28 | |
ScienceQA(Img subset, no CoT)(train + val) | 準確率(test) | 95.39 | 95.93 | |
RSVQA - LR(Non numeric)(train + val) | 平均準確率(test) | 92.65 | 93.11 | |
RSVQA - HR(Non numeric)(train + val) | 平均準確率(test/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) | 準確率(Test server - std) | 73.7 | 75.52 | |
TallyQA(train) | 準確率(test_simple/ test_complex) |
81.72 69.56 |
84.86 72.27 |
|
[OCR - VQA](https://ocr - vqa.github.io/)(train + val) | 準確率(test) | 72.32 | 74.61 | 74.93 |
TextVQA(train + val) | 準確率(Test server - std) | 55.47 | 73.15 | 76.48 |
DocVQA(train + val) | ANLS(Test server) | 43.74 | 78.02 | 84.77 |
Infographic VQA(train + val) | ANLS(Test server) | 28.46 | 40.47 | 47.75 |
SceneText VQA(train + val) | ANLS(Test server) | 63.29 | 81.82 | 84.40 |
分割 | ||||
RefCOCO(combined refcoco, refcoco+, refcocog excluding val and test images) |
MIoU(validation) refcoco/refcoco+/ refcocog |
73.40 68.32 67.65 |
75.57 69.76 70.17 |
76.94 72.18 72.22 |
視頻任務(字幕/問答) | ||||
MSR - VTT(Captioning) | CIDEr(test) | 70.54 | ||
MSR - VTT(QA) | 準確率(test) | 50.09 | ||
ActivityNet(Captioning) | CIDEr(test) | 34.62 | ||
ActivityNet(QA) | 準確率(test) | 50.78 | ||
VATEX(Captioning) | CIDEr(test) | 79.73 | ||
MSVD(QA) | 準確率(test) | 60.22 |
混合模型(在遷移任務的混合上微調)
基準測試 | 指標(分割) | mix - 224 | mix - 448 |
---|---|---|---|
MMVP | 配對準確率 | 46.00 | 45.33 |
POPE | 準確率(random/popular/adversarial) | 88.00 86.63 85.67 |
89.37 88.40 87.47 |
倫理與安全
評估方法
我們的評估方法包括結構化評估和對相關內容政策的內部紅隊測試。紅隊測試由多個不同的團隊進行,每個團隊有不同的目標和人工評估指標。這些模型針對與倫理和安全相關的多個不同類別進行了評估,包括:
- 對涵蓋兒童安全、內容安全和代表性危害的提示進行人工評估。有關評估方法的更多詳細信息,請參閱Gemma模型卡片,但採用圖像字幕和視覺問答設置。
- 圖像到文本基準評估:針對相關學術數據集(如FairFace數據集(Karkkainen等人,2021))進行基準測試。
評估結果
- 倫理和安全評估的人工評估結果在符合[內部政策](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。
- 透明度和問責制:本模型卡片總結了模型的架構、能力、限制和評估過程的詳細信息。一個負責任開發的開放模型為通過使VLM技術在整個AI生態系統中可供開發者和研究人員使用來分享創新提供了機會。
已識別的風險和緩解措施
- 偏差的延續:鼓勵在模型訓練、微調及其他用例中使用評估指標和人工審查進行持續監測,並探索去偏技術。
- 有害內容的生成:內容安全機制和指南至關重要。鼓勵開發者謹慎行事,並根據其特定的產品政策和應用用例實施適當的內容安全保障措施。
- 用於惡意目的的濫用:技術限制以及對開發者和最終用戶的教育有助於減輕大語言模型的惡意應用。我們提供了教育資源和用戶舉報濫用行為的機制。Gemma模型的禁止使用情況在Gemma Prohibited Use Policy中進行了概述。
- 隱私侵犯:模型在經過過濾以去除某些個人信息和敏感數據的數據上進行訓練。鼓勵開發者遵守隱私法規並使用保護隱私的技術。
限制
- 大多數繼承自基礎Gemma模型的限制仍然適用:
- VLM在可以用明確提示和說明構建的任務中表現更好。開放式或高度複雜的任務可能具有挑戰性。
- 自然語言本質上是複雜的。VLM可能難以理解微妙的細微差別、諷刺或比喻語言。
- VLM根據從訓練數據集中學到的信息生成響應,但它們不是知識庫。它們可能生成不正確或過時的事實陳述。
- VLM依賴於語言和圖像中的統計模式。它們可能在某些情況下缺乏應用常識推理的能力。
- PaliGemma首先是作為用於轉移到專門任務的通用預訓練模型而設計的。因此,其“開箱即用”或“零樣本”性能可能落後於專門為此設計的模型。
- PaliGemma不是多輪聊天機器人。它設計用於單輪圖像和文本輸入。
🔧 技術細節
模型類型
PaliGemma是由Transformer解碼器和視覺Transformer圖像編碼器組成的視覺語言模型。
訓練數據
預訓練數據包括WebLI、CC3M - 35L、VQ²A - CC3M - 35L/VQG - CC3M - 35L、OpenImages和WIT等數據集。同時,對WebLI數據進行了色情圖像過濾、文本安全過濾、文本毒性過濾、文本個人信息過濾等處理,以確保訓練數據的質量和安全性。
訓練環境
硬件方面使用了TPUv5e;軟件方面使用了JAX、Flax、TFDS和big_vision
。
📄 許可證
許可證為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}
}
查看論文請點擊此處。








