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








