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

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

高老师1年前 (2025-10-20)大杂烩279

使用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” 的相关文章

Application的错误使用

Application的错误使用

Application 对象用于存储和访问来自任意页面的变量,类似 Session 对象。不同之处在于所有的用户分享一个 Application 对象,而 session 对象和用户的关系是一一对应的。很多的书籍中介绍的Application对象都喜欢以统计在线人数来介绍Application 对象...

Git推送文件到远程仓库

Git推送文件到远程仓库

1.远程仓库的协作模式开发者把自己最新的版本推到线上仓库,同时把线上仓库的最新代码,拉到自己本地即可2.注册git帐号国外: http://www.github.com国内: http://git.oschina.net2.在码云创建项目,不要初始化readmegit push https://gi...

nginx配置https,nginx ssl配置

nginx配置https,nginx ssl配置

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

navicat for mysql 11.1.13 企业破解版

navicat for mysql 11.1.13 企业破解版

navicat for mysql 11.1.13 企业破解版.绿色软件免安装,请直接打开安装说明.一定要看说明.下载地址:          http://pan.baidu.com/s/1dFINxNN下载链接失效,直接点击上方QQ呼叫...

VMware CentOS网络连接设置

VMware CentOS网络连接设置

1.设置桥接模式,复制物理网络状态2.编辑/etc/sysconfig/network-scripts/ifcfg-eth0以下配置:(如果没有自己创建对应选项)ONBOOT=yes NM_CONTROLLED=noBOOTPROTO=staticIPADDR=192.168.2.240...

redis set集合,set集合链表命令大全

redis set集合,set集合链表命令大全

集合的性质: 唯一性,无序性,确定性注: (1).在string和link的命令中,可以通过range 来访问string中的某几个字符或某几个元素但,因为集合的无序性,无法通过下标或范围来访问部分元素.(2).集合中不会出现重复的值因此想看元素,要么随机选一个,要么全选1.sadd ke...