Phân tích cảm xúc đa phương thức

Mô hình đa phương thức với Hugging Face

James Chapman

Curriculum Manager, DataCamp

Mô hình Thị giác–Ngôn ngữ (VLM)

Sơ đồ kiến trúc mô hình thị giác-ngôn ngữ tổng quát

  • Ảnh và văn bản mã hóa riêng
  • Trộn đặc trưng
  • Tạo biểu diễn chung
    • Nhiều tác vụ từ một mô hình
Mô hình đa phương thức với Hugging Face

Tác vụ suy luận trực quan

  • VQA: Ảnh + câu hỏi → Trả lời trực tiếp về nội dung ảnh

Hỏi đáp trực quan với một hotdog

1 https://arxiv.org/pdf/1505.00468
Mô hình đa phương thức với Hugging Face

Tác vụ suy luận trực quan

  • VQA: Ảnh + câu hỏi → Trả lời trực tiếp về nội dung ảnh
  • Matching: Ảnh + mệnh đề → Xác thực Đúng/Sai

Ví dụ Matching với hotdog và các mệnh đề liên quan

Mô hình đa phương thức với Hugging Face

Tác vụ suy luận trực quan

  • VQA: Ảnh + câu hỏi → Trả lời trực tiếp về nội dung ảnh
  • Matchings: Ảnh + mệnh đề → Xác thực Đúng/Sai
  • Entailment: Ảnh + văn bản đối chiếu → Kiểm tra quan hệ ngữ nghĩa

Ví dụ Entailment với hotdog cùng giả thuyết suy ra, trung lập và mâu thuẫn

Mô hình đa phương thức với Hugging Face

Tình huống dùng: tác động đến giá cổ phiếu

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 ...

hình ảnh từ bài báo về đầu tư sản xuất ô tô

Mô hình đa phương thức với 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" )
Mô hình đa phương thức với Hugging Face

Bộ tiền xử lý

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
)
Mô hình đa phương thức với Hugging Face

Prompt đa phương thức

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} ] } ]
Mô hình đa phương thức với Hugging Face

Phân loại bằng 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)]
Mô hình đa phương thức với Hugging Face

Phân loại bằng 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 ...
Mô hình đa phương thức với Hugging Face

Hãy thực hành!

Mô hình đa phương thức với Hugging Face

Preparing Video For Download...