模型简介
模型特点
模型能力
使用案例
🚀 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









