当前位置:首页 > 大杂烩 > 正文内容

modelscope Qwen/Qwen3-VL-8B-Instruct ValueError: Image features and image tokens do not match: tok

高老师6个月前 (10-20)大杂烩133

使用modelscope推理Qwen/Qwen3-VL-8B-Instruct报错如下信息,官方例子会有这个报错:

ValueError: Image features and image tokens do not match: tokens: 2752, features 2752

调整后的代码:

from modelscope import Qwen3VLForConditionalGeneration, AutoProcessor
from PIL import Image
import torch

# Load the model on GPU for better performance
model = Qwen3VLForConditionalGeneration.from_pretrained(
    "Qwen/Qwen3-VL-8B-Instruct",
    dtype=torch.bfloat16,
    device_map="cuda",
)

processor = AutoProcessor.from_pretrained("Qwen/Qwen3-VL-8B-Instruct")

# 使用官方推荐的消息格式
messages = [
    {
        "role": "user",
        "content": [
            {
                "type": "image",
                "image": "https://qianwen-res.oss-cn-beijing.aliyuncs.com/Qwen-VL/assets/demo.jpeg",
            },
            {"type": "text", "text": "请介绍下这张图片"},
        ],
    }
]

# 使用apply_chat_template处理输入
inputs = processor.apply_chat_template(
    messages,
    tokenize=True,
    add_generation_prompt=True,
    return_tensors="pt"
)

# 检查inputs的类型并正确处理
if isinstance(inputs, torch.Tensor):
    # 如果inputs是张量,直接移到设备上
    inputs = inputs.to(model.device)
    # 创建正确的输入字典
    inputs_dict = {"input_ids": inputs}
else:
    # 如果inputs是字典,正常处理
    inputs_dict = {k: v.to(model.device) for k, v in inputs.items()}

# Inference: Generation of the output
generated_ids = model.generate(**inputs_dict, max_new_tokens=128)
generated_ids_trimmed = [
    out_ids[len(in_ids) :] for in_ids, out_ids in zip(inputs_dict["input_ids"], generated_ids)
]
output_text = processor.batch_decode(
    generated_ids_trimmed, skip_special_tokens=True, clean_up_tokenization_spaces=False
)
print(output_text)


扫描二维码推送至手机访问。

版权声明:本文由高久峰个人博客发布,如需转载请注明出处。

本文链接:https://blog.20230611.cn/post/911.html

分享给朋友:

“modelscope Qwen/Qwen3-VL-8B-Instruct ValueError: Image features and image tokens do not match: tok” 的相关文章

Git本地仓库学习

Git本地仓库学习

1.全局用户信息设置 git  config  --global  user.name  gaojiufeng git  config  --global  user.email  392223903...

c#关闭计算机的代码

c#关闭计算机的代码

    1.关机Process.Start("shutdown", "-s -t 0");    2. 注销  Proc...

C# md5加密,C# md5加密代码

C# md5加密,C# md5加密代码

public static string GetMD5(string str) {     //创建MD5对象     MD5 md5 = MD5.C...

c#中文简体转换繁体

c#中文简体转换繁体

private const string fantizi = "高久峰是個程序員"; private const string jiantizi = "高久峰是个程序员...

IIS7.0无法加载CSS的处理办法

IIS7.0无法加载CSS的处理办法

首先网页全部是纯静态的文件,本地测试正常访问,服务器端无法加载CSS,并且无法查看CSS文件的内容。 解决方案:关闭网站的压缩->>静态压缩和动态压缩...

nginx配置https,nginx ssl配置

nginx配置https,nginx ssl配置

首先在阿里云申请免费的证书,选择自动生成证书。然后就是nginx虚拟主机配置文件的修改。以下是我的配置文件(因为公司开发小程序,没有办法只能使用https)。您只需要关注带有ssl的配置选项,我增加了一个监听80和443的端口,同时增加了http跳转https的配置server  &nbs...