模型简介
模型特点
模型能力
使用案例
🚀 PaliGemma模型卡片
PaliGemma是一款多功能轻量级视觉语言模型(VLM),它以图像和文本作为输入并生成文本输出,支持多语言。该模型在多种视觉语言任务(如图像和短视频字幕、视觉问答、文本阅读、目标检测和目标分割)的微调性能上表现出色。
🚀 快速开始
若要在Hugging Face上访问PaliGemma,你需要查看并同意Google的使用许可。请确保你已登录Hugging Face并点击下方按钮,请求将立即处理。 [确认许可](javascript:void(0);)
模型页面:PaliGemma
Transformers PaliGemma 3B权重,在 NLVR2 数据集上使用224*224输入图像进行微调。这些模型仅以float32、bfloat16和float16格式提供,用于研究目的。微调配置可在 big_vision 中找到。
资源和技术文档:
使用条款:条款
作者:Google
✨ 主要特性
- 多功能性:支持多种视觉语言任务,如图像和短视频字幕、视觉问答、目标检测和目标分割。
- 多语言支持:能够处理多种语言的输入和输出。
- 轻量级设计:基于开放组件构建,具有高效的性能。
📦 安装指南
若要使用8位或4位精度自动运行推理,你需要安装 bitsandbytes
:
pip install bitsandbytes accelerate
💻 使用示例
基础用法
PaliGemma是单轮视觉语言模型,不适用于对话场景,在针对特定用例进行微调时效果最佳。你可以通过任务前缀(如“detect”或“segment”)来配置模型要解决的任务。以下是在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 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受 PaLI-3 启发,基于开放组件(如 SigLIP视觉模型 和 Gemma语言模型)构建。它以图像和文本作为输入,生成文本作为输出,支持多语言。
模型架构
PaliGemma由 Transformer解码器 和 视觉Transformer图像编码器 组成,总共有30亿个参数。文本解码器从 Gemma-2B 初始化,图像编码器从 SigLIP-So400m/14 初始化。PaliGemma按照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 |
单任务(在单个任务上微调)
由于表格内容较多,此处省略具体表格,你可参考原文中的表格内容。
🔧 技术细节
PaliGemma的设计和实现涉及多个技术方面,包括模型架构、预训练数据集、数据过滤方法、训练硬件和软件等。通过对这些技术细节的优化,PaliGemma在多种视觉语言任务中表现出色。
📄 许可证
本模型遵循gemma许可。









