開始使用免費開始

使用 Vision Language Transformer(ViLT)的 VQA

現在來實作多模態生成,先從視覺問答(VQA)開始。你將使用 dandelin/vilt-b32-finetuned-vqa 模型,判斷下列影像中號誌的顏色:

Picture of a traffic light showing red

前處理器(processor)、模型(model)以及影像(image)都已為你載入。

本練習屬於課程

使用 Hugging Face 的多模態模型

檢視課程

練習說明

  • 先對 text 提示與 image 進行前處理。
  • 從模型產生答案的權杖,並指定給 outputs
  • 使用輸出 logits,找出置信度最高的答案 ID。

動手互動練習

試著完成這個範例程式碼,體驗一下這個練習。

text = "What color is the traffic light?"

# Preprocess the text prompt and image
encoding = ____(____, ____, return_tensors="pt")

# Generate the answer tokens
outputs = ____

# Find the ID of the answer with the highest confidence
idx = outputs.logits.____
print("Predicted answer:", model.config.id2label[idx])
編輯並執行程式碼