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








