視覚質問応答(VQA)

Hugging Face で学ぶマルチモーダルモデル

James Chapman

Curriculum Manager, DataCamp

マルチモーダルQAタスク

VQAモデルで画像とテキストを処理する図

  1. 質問文と他のモダリティを別々にエンコード
Hugging Face で学ぶマルチモーダルモデル

マルチモーダルQAタスク

VQAモデルで画像とテキストを処理する図

  1. 質問文と他のモダリティを別々にエンコード
  2. エンコード特徴を結合
Hugging Face で学ぶマルチモーダルモデル

マルチモーダルQAタスク

VQAモデルで画像とテキストを処理する図

  1. 質問文と他のモダリティを別々にエンコード
  2. エンコード特徴を結合
  3. 追加レイヤーで回答トークンを予測
Hugging Face で学ぶマルチモーダルモデル

VQA

import requests
from PIL import Image

url = "https://www.worldanimalprotection
.org/cdn-cgi/image/width=1920,format=
auto/globalassets/images/elephants/1
033551-elephant.jpg"


image = Image.open(requests.get(url, stream=True).raw)
text = "What animal is in this photo?"

野生のゾウの写真

Hugging Face で学ぶマルチモーダルモデル

VQA

 

  • 多くの物体の画像・テキスト特徴を学習済み
  • 追加の微調整なしで再利用可能

動物にフォーカスして識別する図

Hugging Face で学ぶマルチモーダルモデル

VQA

from transformers import ViltProcessor, ViltForQuestionAnswering


processor = ViltProcessor.from_pretrained("dandelin/vilt-b32-finetuned-vqa") model = ViltForQuestionAnswering.from_pretrained("dandelin/vilt-b32-finetuned-vqa")
encoding = processor(image, text, return_tensors="pt")
outputs = model(**encoding)
idx = outputs.logits.argmax(-1).item()
print("Predicted answer:", model.config.id2label[idx])
Predicted answer: elephant
Hugging Face で学ぶマルチモーダルモデル

文書VQA

  • 画像内のグラフ・表・テキスト(OCR)まで拡張
from datasets import load_dataset
from transformers import pipeline

dataset = load_dataset("lmms-lab/DocVQA")


import matplotlib.pyplot as plt plt.imshow(dataset["test"][2]["image"]) plt.show()

グラフと棒グラフを含む文書の画像

Hugging Face で学ぶマルチモーダルモデル

文書VQA

Google Tessearact ロゴ

  • OCRの実行には追加の依存関係が必要
  • pytesseractpip でインストール
  • Tesseract OCR はパッケージインストーラで導入(例:apt-getexehomebrew/macports

OCR付きのコーヒー看板の写真

Hugging Face で学ぶマルチモーダルモデル

文書VQA

from transformers import pipeline
pipe = pipeline("document-question-answering", "impira/layoutlm-document-qa")

result = pipe( dataset["test"][2]["image"], "What was the gross income in 2011-2012?" )
Hugging Face で学ぶマルチモーダルモデル

文書VQA

print(result)
[{'score': 0.05149758607149124,
  'answer': '3 36073 Crores', ...}]

グラフと棒グラフを含む文書の画像

Hugging Face で学ぶマルチモーダルモデル

Ayo berlatih!

Hugging Face で学ぶマルチモーダルモデル

Preparing Video For Download...