A53072 | Qwen2.5-VL 是多模态⼤语⾔模型(Vision-Language Model, VLM),能够同时处理图像与⽂本输 ⼊,实现图⽂问答与视觉描述等任务。以下代码演⽰了使⽤ Qwen2.5-VL-7B-Instruct 模型,对输⼊图 像进⾏分析并⽣成⽂本回答。请阅读以下代码,并根据描述完成空缺部分。import torch
来源2025年
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
Qwen2.5-VL 是多模态⼤语⾔模型(Vision-Language Model, VLM),能够同时处理图像与⽂本输 ⼊,实现图⽂问答与视觉描述等任务。以下代码演⽰了使⽤ Qwen2.5-VL-7B-Instruct 模型,对输⼊图 像进⾏分析并⽣成⽂本回答。请阅读以下代码,并根据描述完成空缺部分。
import torch
from transformers import Qwen2_5_VLForConditionalGeneration, AutoProcessor
from PIL import Image
model_name = "Qwen/Qwen2.5-VL-7B-Instruct"
# 1) 加载处理器与模型
processor = ____[1]____
model = Qwen2_5_VLForConditionalGeneration.from_pretrained(
model_name,
device_map="auto",
torch_dtype=torch.bfloat16,
trust_remote_code=True
)
# 2) 读取输⼊图像
image = ____[2]____
# 3) 构建多模态输⼊提⽰
messages = [
{
"role": "user",
"content": [
{"type": "image", "image": image},
{"type": "text", "text": "请描述图⽚中的主要内容。"}
]
}
]
text = ____[3]____
inputs = processor(text=[text], images=[image],
return_tensors="pt").to(model.device)
# 4) 模型推理⽣成回答
model = model.eval()
with ____[4]____:
output_ids = model.generate(
**inputs,
max_new_tokens=128,
temperature=0.7,
)
generated_ids = ____[5]____
answer = processor.tokenizer.batch_decode(generated_ids,
skip_special_tokens=True)[0]
print("模型回答:", answer)暂无题解
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?