模型:
google/pix2struct-ai2d-base
Pix2Struct 是一个图像编码器-文本解码器模型,该模型在包括图像字幕和视觉问题回答在内的各种任务的图像 - 文本对上进行训练。可在论文的表1中找到可用模型的完整列表:
该模型的摘要说明如下:
可视化语言在我们身边无处不在 - 资料来源包括带有图表的教科书、带有图像和表格的网页以及带有按钮和表单的移动应用程序。也许由于这种多样性,先前的研究通常依赖于具有有限共享基础数据、模型架构和目标的领域专用技术。我们提出了Pix2Struct,这是一种用于纯视觉语言理解的预训练图像到文本模型,可以对包含可视化语言的任务进行微调。Pix2Struct的预训练目标是将屏幕截图的掩码解析成简化的HTML。Web具有丰富的可视化元素,这些元素在HTML结构中清晰地反映出来,为多样的下游任务提供了大量的预训练数据。直观地说,这个目标包含了常见的预训练信号,例如OCR、语言建模、图像字幕。除了新颖的预训练策略,我们还引入了可变分辨率的输入表示和更灵活的语言和视觉输入集成,其中诸如问题的语言提示直接呈现在输入图像之上。我们首次展示出单个预训练模型可以在四个领域的九个任务中的六个任务中取得最先进的结果:文档、插图、用户界面和自然图像。
该模型已在VQA上进行了微调,您需要以特定格式提供问题,最好以Choices问题回答的格式提供问题。
您可以在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-base") processor = Pix2StructProcessor.from_pretrained("google/pix2struct-ai2d-base") 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上以全精度运行模型:
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-base").to("cuda") processor = Pix2StructProcessor.from_pretrained("google/pix2struct-ai2d-base") 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上以半精度运行模型:
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-base", torch_dtype=torch.bfloat16).to("cuda") processor = Pix2StructProcessor.from_pretrained("google/pix2struct-ai2d-base") 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
您可以按照以下方式使用 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
保存后,您可以使用以下代码片段推送转换后的模型:
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")
该模型最初由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} }