🚀 Pix2Struct - TextCapsでファインチューニングされたモデル
Pix2Structは、画像キャプショニングや視覚的質問応答などの様々なタスクのために、画像とテキストのペアで学習された画像エンコーダ - テキストデコーダモデルです。このモデルは、視覚的な言語理解に特化しており、多様なダウンストリームタスクに適用できます。

🚀 クイックスタート
Pix2Structは、ウェブページのマスクされたスクリーンショットを簡略化されたHTMLに解析することで事前学習されます。この事前学習戦略により、単一の事前学習モデルが4つのドメイン(ドキュメント、イラスト、ユーザーインターフェイス、自然画像)の9つのタスクのうち6つで最先端の結果を達成できることが示されています。
✨ 主な機能
- 画像からテキストへの変換が可能で、画像キャプショニングや視覚的質問応答などのタスクに対応。
- ウェブページのスクリーンショットを利用した事前学習により、多様なダウンストリームタスクに適用可能。
- 可変解像度の入力表現と、言語とビジョンの入力を柔軟に統合。
📦 インストール
T5xからHugging Faceへの変換
以下のスクリプトを使用して、T5xのチェックポイントをPyTorch形式に変換できます。
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.
📚 ドキュメント
モデルの概要
論文の表1には、利用可能なモデルの完全なリストが記載されています。

論文の要約
視覚的に配置された言語はありふれています。教科書の図から、画像や表を含むウェブページ、ボタンやフォームを持つモバイルアプリまで、様々なソースがあります。おそらくこの多様性のため、以前の研究では、基礎となるデータ、モデルアーキテクチャ、目的の共有が限られたドメイン固有の手法に依存することが多かったです。我々は、純粋な視覚的言語理解のための事前学習された画像からテキストへのモデルであるPix2Structを提案します。このモデルは、視覚的に配置された言語を含むタスクでファインチューニングすることができます。Pix2Structは、ウェブページのマスクされたスクリーンショットを簡略化されたHTMLに解析することで事前学習されます。ウェブは、その豊富な視覚要素がHTML構造にきれいに反映されており、ダウンストリームタスクの多様性に適した大量の事前学習データを提供します。直感的には、この目的は、OCR、言語モデリング、画像キャプショニングなどの一般的な事前学習信号を包含しています。新しい事前学習戦略に加えて、我々は可変解像度の入力表現と、言語とビジョンの入力をより柔軟に統合する手法を導入します。ここでは、質問などの言語プロンプトが入力画像の上に直接レンダリングされます。初めて、単一の事前学習モデルが4つのドメイン(ドキュメント、イラスト、ユーザーインターフェイス、自然画像)の9つのタスクのうち6つで最先端の結果を達成できることを示します。
🔧 技術詳細
Pix2Structは、画像エンコーダとテキストデコーダから構成されています。事前学習では、ウェブページのマスクされたスクリーンショットを簡略化されたHTMLに解析することで学習されます。このアプローチにより、多様なダウンストリームタスクに適用可能なモデルを構築できます。
📄 ライセンス
このモデルはApache 2.0ライセンスの下で提供されています。
🔗 参考文献
@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}
}
👥 コントリビューション
このモデルは、Kenton Lee、Mandar Joshiらによって最初に貢献され、Younes BelkadaによってHugging Faceのエコシステムに追加されました。