멀티모달 감성 분석

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

James Chapman

Curriculum Manager, DataCamp

비전-언어 모델(VLM)

일반적인 비전-언어 모델 아키텍처 다이어그램

  • 이미지와 텍스트를 별도 인코딩
  • 특징 융합
  • 공유 표현 생성
    • 하나의 모델로 여러 태스크
Hugging Face로 배우는 멀티모달 모델

시각적 추론 태스크

  • VQA: 이미지 + 질문 → 이미지 내용에 대한 직접 답변

핫도그 이미지를 활용한 시각적 질의응답

1 https://arxiv.org/pdf/1505.00468
Hugging Face로 배우는 멀티모달 모델

시각적 추론 태스크

  • VQA: 이미지 + 질문 → 이미지 내용에 대한 직접 답변
  • 매칭: 이미지 + 진술문 → 참/거짓 판정

핫도그와 관련 진술문을 사용한 매칭 예시

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

시각적 추론 태스크

  • VQA: 이미지 + 질문 → 이미지 내용에 대한 직접 답변
  • 매칭: 이미지 + 진술문 → 참/거짓 판정
  • 함의: 이미지 + 대비되는 텍스트 → 의미 관계 확인

핫도그 이미지와 함의/중립/모순 가설을 사용하는 함의 예시

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

사용 사례: 주가 영향

from datasets import load_dataset
dset = "RealTimeData/bbc_news_alltime"
dataset = load_dataset(dset, '2017-01', 
                       split="train")


image = dataset[87]["top_image"] content = dataset[87]["content"] print(content)
'Ford\'s decision to cancel a $1.6bn 
investment in Mexico and 
invest an extra $700m in Michigan ...

자동차 생산 투자 관련 뉴스 기사 이미지

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

Qwen 2 VLMs

from transformers import Qwen2VLForConditionalGeneration
from qwen_vl_utils import process_vision_info


vl_model = Qwen2VLForConditionalGeneration.from_pretrained( "Qwen/Qwen2-VL-2B-Instruct", device_map="auto", torch_dtype="auto" )
Hugging Face로 배우는 멀티모달 모델

전처리기

from transformers import Qwen2VLProcessor

min_pixels = 224 * 224
max_pixels = 448 * 448
vl_model_processor = Qwen2VLProcessor.from_pretrained(
    "Qwen/Qwen2-VL-2B-Instruct", min_pixels=min_pixels, max_pixels=max_pixels
)
Hugging Face로 배우는 멀티모달 모델

멀티모달 프롬프트

text_query = f"Is the sentiment of the following content good or bad for the Ford share price:
{article_text}. Provide reasoning."


chat_template = [ { "role": "user", "content": [ {"type": "image", "image": article_image}, {"type": "text", "text": text_query} ] } ]
Hugging Face로 배우는 멀티모달 모델

VLM 분류

text = vl_model_processor.apply_chat_template(chat_template, tokenize=False,
                                              add_generation_prompt=True)

image_inputs, _ = process_vision_info(chat_template)
inputs = vl_model_processor(text=[text], images=image_inputs, padding=True, return_tensors="pt")
generated_ids = vl_model.generate(**inputs, max_new_tokens=500)
generated_ids_trimmed = [out_ids[len(in_ids) :] for in_ids, out_ids\ in zip(inputs.input_ids, generated_ids)]
Hugging Face로 배우는 멀티모달 모델

VLM 분류

output_text = vl_model_processor.batch_decode(
    generated_ids_trimmed, skip_special_tokens=True, clean_up_tokenization_spaces=False
)
print(output_text[0])
The sentiment of the provided text is negative. The author is expressing concern and
skepticism ...
Hugging Face로 배우는 멀티모달 모델

연습해 봅시다!

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

Preparing Video For Download...