模型概述
模型特點
模型能力
使用案例
🚀 Pix2Struct - 在UI RefExp數據集上微調的模型
Pix2Struct是一個圖像編碼器 - 文本解碼器模型,在圖像 - 文本對上進行訓練,可用於圖像描述和視覺問答等任務。本項目將Google在gcloud中的檢查點轉換為HF格式,為圖像到文本的處理提供支持。
🚀 快速開始
從T5x轉換為Hugging Face格式
你可以使用以下腳本將模型從T5x轉換為Hugging Face格式:
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 stop sign is on a street corner.
全精度,在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-base").to("cuda")
processor = Pix2StructProcessor.from_pretrained("google/pix2struct-textcaps-base")
# 僅處理圖像
inputs = processor(images=image, return_tensors="pt").to("cuda")
predictions = model.generate(**inputs)
print(processor.decode(predictions[0], skip_special_tokens=True))
>>> A stop sign is on a street corner.
半精度,在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-base", torch_dtype=torch.bfloat16).to("cuda")
processor = Pix2StructProcessor.from_pretrained("google/pix2struct-textcaps-base")
# 僅處理圖像
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 stop sign is on a street corner.
使用不同的序列長度
該模型在序列長度為 2048
的情況下進行訓練。你可以嘗試減少序列長度以提高推理時的內存效率,但對於較小的序列長度(<512),可能會觀察到性能下降。在調用處理器時傳遞 max_patches
參數即可:
inputs = processor(images=image, return_tensors="pt", max_patches=512)
條件生成
你還可以預先添加一些輸入文本以執行條件生成:
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-base")
processor = Pix2StructProcessor.from_pretrained("google/pix2struct-textcaps-base")
# 僅處理圖像
inputs = processor(images=image, text=text, return_tensors="pt")
predictions = model.generate(**inputs)
print(processor.decode(predictions[0], skip_special_tokens=True))
>>> A picture of a stop sign that says yes.
✨ 主要特性
Pix2Struct - RefExp 詳細信息
(基於其預處理)
- 輸入:一張帶有圍繞候選對象繪製的邊界框的圖像,以及包含指代表達式的標題(存儲在圖像特徵中)。
- 輸出:一個布爾標誌(解析特徵),指示候選對象是否是指代表達式的正確指稱。
模型概述
Pix2Struct 是一個圖像編碼器 - 文本解碼器模型,針對各種任務在圖像 - 文本對上進行訓練,包括圖像描述和視覺問答。可用模型的完整列表可在論文的表1中找到:
模型摘要指出:
視覺情境語言無處不在 —— 其來源範圍廣泛,從帶有圖表的教科書到帶有圖像和表格的網頁,再到帶有按鈕和表單的移動應用程序。也許由於這種多樣性,以前的工作通常依賴於特定領域的方法,底層數據、模型架構和目標的共享有限。我們提出了 Pix2Struct,這是一個用於純視覺語言理解的預訓練圖像到文本模型,可在包含視覺情境語言的任務上進行微調。Pix2Struct 通過學習將網頁的掩碼截圖解析為簡化的 HTML 進行預訓練。網絡中豐富的視覺元素清晰地反映在 HTML 結構中,為預訓練數據提供了大量來源,非常適合下游任務的多樣性。直觀地說,這個目標包含了常見的預訓練信號,如 OCR、語言建模、圖像描述。除了新穎的預訓練策略,我們還引入了可變分辨率的輸入表示,以及更靈活的語言和視覺輸入集成,其中諸如問題之類的語言提示直接渲染在輸入圖像之上。我們首次表明,單個預訓練模型可以在四個領域的九個任務中的六個任務中取得最先進的結果:文檔、插圖、用戶界面和自然圖像。
📄 許可證
本項目採用 Apache-2.0 許可證。
👥 貢獻者
該模型最初由 Kenton Lee、Mandar Joshi 等人貢獻,並由 Murali Manohar 添加到 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}
}








