模型简介
模型特点
模型能力
使用案例
🚀 Pix2Struct - 在AI2D(科学图表视觉问答)上微调的大版本模型卡片
Pix2Struct是一个图像编码器 - 文本解码器模型,它基于图像 - 文本对进行训练,可用于多种任务,如图像描述和视觉问答。该模型能有效处理包含视觉语言的任务,为视觉语言理解提供了强大的支持。
目录
简要说明
Pix2Struct是一种图像编码器 - 文本解码器模型,它针对包括图像描述和视觉问答在内的各种任务,在图像 - 文本对上进行训练。可用模型的完整列表可在论文的表1中找到:
该模型的摘要指出:
视觉情境化语言无处不在,其来源范围广泛,从带有图表的教科书到带有图像和表格的网页,再到带有按钮和表单的移动应用程序。也许由于这种多样性,先前的工作通常依赖于特定领域的方法,底层数据、模型架构和目标的共享有限。我们提出了Pix2Struct,这是一种用于纯视觉语言理解的预训练图像到文本模型,可在包含视觉情境化语言的任务上进行微调。Pix2Struct通过学习将网页的掩码截图解析为简化的HTML进行预训练。网络中丰富的视觉元素清晰地反映在HTML结构中,为预训练数据提供了大量来源,非常适合下游任务的多样性。直观地说,这个目标包含了常见的预训练信号,如OCR、语言建模、图像描述。除了新颖的预训练策略,我们还引入了可变分辨率的输入表示以及更灵活的语言和视觉输入集成,其中诸如问题之类的语言提示直接渲染在输入图像之上。我们首次表明,单个预训练模型可以在四个领域(文档、插图、用户界面和自然图像)的九个任务中的六个任务上取得最先进的结果。
使用模型
此模型已在视觉问答(VQA)上进行了微调,你需要以特定格式提供问题,理想情况下是选择题的格式。
从T5x转换到Hugging Face
你可以使用convert_pix2struct_checkpoint_to_pytorch.py
脚本,如下所示:
python convert_pix2struct_checkpoint_to_pytorch.py --t5x_checkpoint_path PATH_TO_T5X_CHECKPOINTS --pytorch_dump_path PATH_TO_SAVE --is_vqa
如果你要转换大模型,请运行:
python convert_pix2struct_checkpoint_to_pytorch.py --t5x_checkpoint_path PATH_TO_T5X_CHECKPOINTS --pytorch_dump_path PATH_TO_SAVE --use-large --is_vqa
保存后,你可以使用以下代码片段将转换后的模型推送到Hugging Face Hub:
from transformers import Pix2StructForConditionalGeneration, Pix2StructProcessor
model = Pix2StructForConditionalGeneration.from_pretrained(PATH_TO_SAVE)
processor = Pix2StructProcessor.from_pretrained(PATH_TO_SAVE)
model.push_to_hub("USERNAME/MODEL_NAME")
processor.push_to_hub("USERNAME/MODEL_NAME")
运行模型
全精度,在CPU上运行:
你可以在CPU上以全精度运行模型:
import requests
from PIL import Image
from transformers import Pix2StructForConditionalGeneration, Pix2StructProcessor
image_url = "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/tasks/ai2d-demo.jpg"
image = Image.open(requests.get(image_url, stream=True).raw)
model = Pix2StructForConditionalGeneration.from_pretrained("google/pix2struct-ai2d-large")
processor = Pix2StructProcessor.from_pretrained("google/pix2struct-ai2d-large")
question = "What does the label 15 represent? (1) lava (2) core (3) tunnel (4) ash cloud"
inputs = processor(images=image, text=question, return_tensors="pt")
predictions = model.generate(**inputs)
print(processor.decode(predictions[0], skip_special_tokens=True))
>>> ash cloud
全精度,在GPU上运行:
你可以在GPU上以全精度运行模型:
import requests
from PIL import Image
from transformers import Pix2StructForConditionalGeneration, Pix2StructProcessor
image_url = "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/tasks/ai2d-demo.jpg"
image = Image.open(requests.get(image_url, stream=True).raw)
model = Pix2StructForConditionalGeneration.from_pretrained("google/pix2struct-ai2d-large").to("cuda")
processor = Pix2StructProcessor.from_pretrained("google/pix2struct-ai2d-large")
question = "What does the label 15 represent? (1) lava (2) core (3) tunnel (4) ash cloud"
inputs = processor(images=image, text=question, return_tensors="pt").to("cuda")
predictions = model.generate(**inputs)
print(processor.decode(predictions[0], skip_special_tokens=True))
>>> ash cloud
半精度,在GPU上运行:
你可以在GPU上以半精度运行模型:
import requests
from PIL import Image
import torch
from transformers import Pix2StructForConditionalGeneration, Pix2StructProcessor
image_url = "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/tasks/ai2d-demo.jpg"
image = Image.open(requests.get(image_url, stream=True).raw)
model = Pix2StructForConditionalGeneration.from_pretrained("google/pix2struct-ai2d-large", torch_dtype=torch.bfloat16).to("cuda")
processor = Pix2StructProcessor.from_pretrained("google/pix2struct-ai2d-large")
question = "What does the label 15 represent? (1) lava (2) core (3) tunnel (4) ash cloud"
inputs = processor(images=image, text=question, return_tensors="pt").to("cuda", torch.bfloat16)
predictions = model.generate(**inputs)
print(processor.decode(predictions[0], skip_special_tokens=True))
>>> ash cloud
贡献者
该模型最初由Kenton Lee、Mandar Joshi等人贡献,并由Younes Belkada添加到Hugging Face生态系统中。
引用信息
如果你想引用这项工作,请考虑引用原始论文:
@misc{https://doi.org/10.48550/arxiv.2210.03347,
doi = {10.48550/ARXIV.2210.03347},
url = {https://arxiv.org/abs/2210.03347},
author = {Lee, Kenton and Joshi, Mandar and Turc, Iulia and Hu, Hexiang and Liu, Fangyu and Eisenschlos, Julian and Khandelwal, Urvashi and Shaw, Peter and Chang, Ming-Wei and Toutanova, Kristina},
keywords = {Computation and Language (cs.CL), Computer Vision and Pattern Recognition (cs.CV), FOS: Computer and information sciences, FOS: Computer and information sciences},
title = {Pix2Struct: Screenshot Parsing as Pretraining for Visual Language Understanding},
publisher = {arXiv},
year = {2022},
copyright = {Creative Commons Attribution 4.0 International}
}
📄 许可证
本模型采用Apache-2.0许可证。









