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

🚀 クイックスタート
目次
- 要約
- モデルの使用方法
- 貢献者
- 引用
要約
Pix2Structは、画像 - テキストのペアを用いて学習された画像からテキストへのモデルです。このモデルは、視覚的な言語理解のために事前学習されており、視覚的な言語を含むタスクに対してファインチューニングすることができます。
論文の表1には、利用可能なモデルの完全なリストが記載されています。

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