使用 ViLT 的 VQA(视觉问答)
现在来亲自实践多模态生成,从视觉问答(VQA)开始。您将使用 dandelin/vilt-b32-finetuned-vqa 模型来判断下图中交通信号灯的颜色:

预处理器(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])