库名称: transformers
许可证: gemma
流水线标签: 图像文本到文本
额外授权标题: 在Hugging Face上访问PaliGemma
额外授权提示: 要访问Hugging Face上的PaliGemma,您需要审阅并同意Google的使用许可。请确保已登录Hugging Face账号并点击下方按钮。请求将立即处理。
额外授权按钮内容: 确认许可
PaliGemma模型卡
模型页面: PaliGemma
Transformers PaliGemma 3B权重,基于448*448输入图像和512个token的输入/输出文本序列,在多个下游学术数据集上微调。该模型提供float32、bfloat16和float16格式,仅供研究用途。
资源与技术文档:
使用条款: 条款
作者: Google
模型信息
模型概述
描述
PaliGemma是一款多功能轻量级视觉语言模型(VLM),灵感来自PaLI-3,基于SigLIP视觉模型和Gemma语言模型等开放组件。它接受图像和文本输入并生成文本输出,支持多语言。专为在图像/短视频描述、视觉问答、文本阅读、目标检测和分割等广泛视觉语言任务上实现领先的微调性能而设计。
模型架构
PaliGemma由Transformer解码器和Vision Transformer图像编码器组成,总计30亿参数。文本解码器初始化自Gemma-2B,图像编码器初始化自SigLIP-So400m/14,训练遵循PaLI-3方案。
输入输出
- 输入: 图像和文本字符串(如图像描述提示或问题)
- 输出: 生成的响应文本(如图像描述、问题答案、目标框坐标或分割代码)
模型数据
预训练数据集
PaliGemma在以下混合数据集上预训练:
- WebLI: 从公开网络构建的网页级多语言图文数据集,用于获取视觉语义理解、目标定位、多语言等能力
- CC3M-35L: 网页英文图像-替代文本对,翻译为34种语言
- VQ²A-CC3M-35L/VQG-CC3M-35L: VQ2A-CC3M子集,翻译为相同34种语言
- OpenImages: 基于OpenImages数据集规则生成的目标检测问答
- WIT: 维基百科收集的图文数据
数据责任过滤
对WebLI应用以下过滤:
- 色情图像过滤
- 文本安全过滤(移除涉及CSAI、色情、粗俗等内容)
- 文本毒性过滤(使用Perspective API识别侮辱/仇恨内容)
- 个人信息过滤(使用Cloud DLP API移除社保号等敏感信息)
- 其他基于内容质量与安全的过滤方法
使用方式
PaliGemma是单轮视觉语言模型,不适合对话场景,在特定用例微调后表现最佳。可通过任务前缀(如"detect"或"segment")配置模型任务。预训练模型通过此类提示结构获得丰富能力,但设计初衷是通过类似提示结构微调到具体任务。交互测试可使用"mix"系列模型(在多任务上微调),示例见基于Transformers的演示Space。
详细用例请参考使用与限制章节,或访问博客文章获取更多示例。
Transformers中使用
以下代码示例使用google/paligemma-3b-mix-224
模型,当前仓库中的模型可能针对其他任务训练,请确保输入与任务匹配。
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)
print(processor.decode(generation[0][input_len:], skip_special_tokens=True))
输出: Un auto azul estacionado frente a un edificio.
CUDA上运行其他精度
使用bfloat16
的示例:
model = PaliGemmaForConditionalGeneration.from_pretrained(
model_id,
torch_dtype=torch.bfloat16,
device_map="cuda:0",
revision="bfloat16"
).eval()
4-bit/8-bit加载
需安装bitsandbytes
:
pip install bitsandbytes accelerate
quantization_config = BitsAndBytesConfig(load_in_8bit=True)
model = PaliGemmaForConditionalGeneration.from_pretrained(
model_id,
quantization_config=quantization_config
).eval()
实现信息
硬件
使用最新TPUv5e硬件训练。
软件
使用JAX、Flax、TFDS和big_vision
训练。微调代码与推理代码发布于big_vision
仓库。
评估信息
基准结果
通过在各项任务上微调预训练模型验证迁移能力,同时训练混合任务的"mix"模型。评估不同分辨率下任务表现(注:这些任务数据集均未出现在预训练数据中)。
单任务微调结果
(表格内容:COCO描述CIDEr 141.92@224/144.60@448,TextVQA准确率55.47@224/73.15@448/76.48@896等,此处省略具体数值)
混合模型结果
(表格内容:MMVP配对准确率46.00@224/45.33@448,POPE准确率88.00-87.47@448等)
伦理与安全
评估方法
通过结构化评估和内部红队测试评估内容政策相关性,涵盖儿童安全、内容安全和表征危害等类别,使用FairFace等数据集进行图像到文本基准评估。
评估结果
- 伦理安全评估结果符合内部政策阈值
- FairFace图像描述生成中,使用Perspective API(阈值0.8)测量毒性/侮辱等指标,各人口属性组最大/中位值如下:
指标 |
性别(最大/中位) |
种族(最大/中位) |
年龄(最大/中位) |
毒性 |
0.04%/0.03% |
0.08%/0.00% |
0.09%/0.00% |
身份攻击 |
0.00%/0.00% |
0.00%/0.00% |
0.00%/0.00% |
使用与限制
预期用途
- 微调特定视觉语言任务(图像描述、视觉问答、文本阅读等)
- 适配专业领域(遥感问答、盲人视觉问题、科学问答等)
- 非文本输出任务(边界框或分割掩码)
- 视觉语言研究基础
伦理考量与风险
- 偏见与公平性: 需持续监控和去偏技术
- 有害内容生成: 需实施内容安全措施
- 恶意滥用: 遵循Gemma禁止使用政策
- 隐私侵犯: 训练数据已过滤个人信息,开发者需遵守隐私法规
局限性
- 继承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}
}
论文链接:arXiv