マルチモーダル感情分析

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

James Chapman

Curriculum Manager, DataCamp

視覚と言語の統合モデル(VLM)

一般的な視覚と言語モデルのアーキテクチャ図

  • 画像とテキストを別々にエンコード
  • 特徴を融合
  • 共有表現を生成
    • 1つのモデルで複数タスク
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 で学ぶマルチモーダルモデル

Let's practice!

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

Preparing Video For Download...