模型概述
模型特點
模型能力
使用案例
🚀 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許可證。









