🚀 Pix2Struct - 在TextCaps上微調的大版本模型卡
Pix2Struct是一個圖像編碼器 - 文本解碼器模型,可用於圖像字幕生成和視覺問答等多種圖像轉文本任務,具有強大的視覺語言理解能力。
🚀 快速開始
從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
如果你要轉換大模型,請運行:
python convert_pix2struct_checkpoint_to_pytorch.py --t5x_checkpoint_path PATH_TO_T5X_CHECKPOINTS --pytorch_dump_path PATH_TO_SAVE --use-large
保存後,你可以使用以下代碼片段將轉換後的模型推送到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上運行
import requests
from PIL import Image
from transformers import Pix2StructForConditionalGeneration, Pix2StructProcessor
url = "https://www.ilankelman.org/stopsigns/australia.jpg"
image = Image.open(requests.get(url, stream=True).raw)
model = Pix2StructForConditionalGeneration.from_pretrained("google/pix2struct-textcaps-base")
processor = Pix2StructProcessor.from_pretrained("google/pix2struct-textcaps-base")
inputs = processor(images=image, return_tensors="pt")
predictions = model.generate(**inputs)
print(processor.decode(predictions[0], skip_special_tokens=True))
>>> A street scene with a sign that says "STOP".
全精度,在GPU上運行
import requests
from PIL import Image
from transformers import Pix2StructForConditionalGeneration, Pix2StructProcessor
url = "https://www.ilankelman.org/stopsigns/australia.jpg"
image = Image.open(requests.get(url, stream=True).raw)
model = Pix2StructForConditionalGeneration.from_pretrained("google/pix2struct-textcaps-large").to("cuda")
processor = Pix2StructProcessor.from_pretrained("google/pix2struct-textcaps-large")
inputs = processor(images=image, return_tensors="pt").to("cuda")
predictions = model.generate(**inputs)
print(processor.decode(predictions[0], skip_special_tokens=True))
>>> A street scene with a sign that says "STOP".
半精度,在GPU上運行
import requests
import torch
from PIL import Image
from transformers import Pix2StructForConditionalGeneration, Pix2StructProcessor
url = "https://www.ilankelman.org/stopsigns/australia.jpg"
image = Image.open(requests.get(url, stream=True).raw)
model = Pix2StructForConditionalGeneration.from_pretrained("google/pix2struct-textcaps-large", torch_dtype=torch.bfloat16).to("cuda")
processor = Pix2StructProcessor.from_pretrained("google/pix2struct-textcaps-large")
inputs = processor(images=image, return_tensors="pt").to("cuda", torch.bfloat16)
predictions = model.generate(**inputs)
print(processor.decode(predictions[0], skip_special_tokens=True))
>>> A street scene with a sign that says "STOP".
使用不同的序列長度
該模型在序列長度為4096
的情況下進行訓練。你可以嘗試減少序列長度以提高推理的內存效率,但對於較小的序列長度(<1024),可能會觀察到一些性能下降。在調用處理器時傳遞max_patches
參數即可:
inputs = processor(images=image, return_tensors="pt", max_patches=1024)
條件生成
你還可以預先添加一些輸入文本以執行條件生成:
import requests
from PIL import Image
from transformers import Pix2StructForConditionalGeneration, Pix2StructProcessor
url = "https://www.ilankelman.org/stopsigns/australia.jpg"
image = Image.open(requests.get(url, stream=True).raw)
text = "A picture of"
model = Pix2StructForConditionalGeneration.from_pretrained("google/pix2struct-textcaps-large")
processor = Pix2StructProcessor.from_pretrained("google/pix2struct-textcaps-large")
inputs = processor(images=image, text=text, return_tensors="pt")
predictions = model.generate(**inputs)
print(processor.decode(predictions[0], skip_special_tokens=True))
✨ 主要特性
- 多語言支持:支持英語、法語、羅馬尼亞語、德語等多種語言以及多語言模式。
- 圖像轉文本能力:可用於圖像字幕生成、視覺問答等多種圖像轉文本任務。
- 創新預訓練策略:通過學習將網頁的掩碼截圖解析為簡化的HTML進行預訓練,適用於多種下游任務。
- 靈活的輸入表示:引入可變分辨率輸入表示和更靈活的語言與視覺輸入集成方式。
📚 詳細文檔
模型概述
視覺情境語言無處不在,其來源廣泛,從帶有圖表的教科書到帶有圖像和表格的網頁,再到帶有按鈕和表單的移動應用程序。由於這種多樣性,以往的工作通常依賴於特定領域的方法,底層數據、模型架構和目標的共享有限。
Pix2Struct是一種用於純視覺語言理解的預訓練圖像到文本模型,可在包含視覺情境語言的任務上進行微調。它通過學習將網頁的掩碼截圖解析為簡化的HTML進行預訓練。網絡中豐富的視覺元素清晰地反映在HTML結構中,為預訓練數據提供了大量來源,非常適合下游任務的多樣性。直觀地說,這個目標包含了常見的預訓練信號,如OCR、語言建模、圖像字幕生成。
除了新穎的預訓練策略,還引入了可變分辨率輸入表示和更靈活的語言與視覺輸入集成方式,其中諸如問題之類的語言提示直接渲染在輸入圖像之上。首次表明,單個預訓練模型可以在四個領域的九個任務中的六個任務中取得最先進的結果:文檔、插圖、用戶界面和自然圖像。
模型相關表格

🔧 技術細節
預訓練策略
Pix2Struct通過學習將網頁的掩碼截圖解析為簡化的HTML進行預訓練。網絡中豐富的視覺元素清晰地反映在HTML結構中,為預訓練數據提供了大量來源,非常適合下游任務的多樣性。這種預訓練策略包含了常見的預訓練信號,如OCR、語言建模、圖像字幕生成。
輸入表示
引入了可變分辨率輸入表示和更靈活的語言與視覺輸入集成方式,其中語言提示(如問題)直接渲染在輸入圖像之上。
📄 許可證
本模型使用Apache-2.0許可證。
👥 貢獻者
此模型最初由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}
}