시각 질의응답(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)까지 감지하는 VQA 확장
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 실행에 추가 종속성 필요
  • pip으로 pytesseract 설치
  • Tesseract OCR은 패키지 인스톨러로 설치(예: apt-get, exe, homebrew/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로 배우는 멀티모달 모델

연습해 봅시다!

Hugging Face로 배우는 멀티모달 모델

Preparing Video For Download...