🚀 Pix2Structのモデルカード - AI2D(科学図表の視覚的質問応答)でファインチューニング済み - ラージバージョン
Pix2Structは、画像キャプショニングや視覚的質問応答などの様々なタスクに対応した画像エンコーダーとテキストデコーダーのモデルです。このモデルは、画像とテキストのペアで学習されており、視覚的な言語理解に特化しています。

📚 目次
- 概要
- モデルの使用方法
- 貢献者
- 引用
概要
Pix2Structは、画像エンコーダーとテキストデコーダーから構成されるモデルで、画像キャプショニングや視覚的質問応答などの様々なタスクに対応しています。利用可能なモデルの完全なリストは、論文の表1に記載されています。

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