多模态大模型的核心思想是将不同媒体数据(如文本、图像、音频和视频等)进行融合,通过学习不同模态之间的关联,实现更加智能化的信息处理。简单来说,多模态大模型可以可以理解多种不同模态的输入数据,并输出相应反馈结果,例如图像理解,语音识别,视觉问题等。

多模态大模型任务流程

多模态大模型都会将文本生成模型作为底座模型,以支持对话能力,其中千问团队近期发布的 Qwen2-Audio 和 Qwen2-VL 便是以 Qwen2 为底座的多模态大模型,分别支持语音/文本以及图像/文本作为多模态输入,相比上一代的 Qwen-VL 和 Qwen-Audio ,基于 Qwen2 的多模态模型具备更强大的视觉理解以语音理解能力,并实现了多语种的支持。本文将分享如何利用 OpenVINO™ 工具套件在轻薄本上部署 Qwen2-Audio 以及 Qwen2-VL 多模态模型。

Qwen2-Audio 示例地址:

https://github.com/openvinotoolkit/openvino_notebooks/blob/latest/notebooks/qwen2-audio/qwen2-audio.ipynb

Qwen2-VL 示例地址:

https://github.com/openvinotoolkit/openvino_notebooks/blob/latest/notebooks/qwen2-vl/qwen2-vl.ipynb

Qwen2 workshop:

https://github.com/openvino-dev-samples/qwen2-openvino-workshop

 

Qwen2-VL

1. 模型转换及量化

目前 Qwen2-VL 的推理任务还没有被完全集成进 Optimum 工具中,因此我们需要手动完成模型的转换和量化,其中包含语言模型 lang_model,图像编码模型 image_embed,文本 token 编码模型 embed_token 模型以及图像特征映射模型 image_embed_merger。

为了简化转化步骤,我们提前对这些转化任务行进行了封装,开发者只需要调用 Qwen2-VL 示例地址中提供的函数便可完成这些模型的转换,并对其中负载最大的语言模型进行量化。这里以 Qwen2-VL-2B-Instruct 为例。

from ov_qwen2_vl import convert_qwen2vl_model
import nncf

compression_configuration = {
    "mode": nncf.CompressWeightsMode.INT4_ASYM,
    "group_size": 128,
    "ratio": 1.0,
}

convert_qwen2vl_model("Qwen/Qwen2-VL-2B-Instruct", model_dir, compression_configuration)

2. 图片内容理解

此外在该示例中,我们也对模型的推理任务进行封装,通过以下代码便可快速部署图像理解任务,并实现文字的流式输出。由于 Qwen2-VL 对于输入数据有格式上的要求,因此我们需要提前将图片和文本包装为指定的字典格式,并调用模型自带的 processor 脚本将其转换为 prompt 输入。

question = "Describe this image."

messages = [
    {
        "role": "user",
        "content": [
            {
                "type": "image",
                "image": f"file://{example_image_path}",
            },
            {"type": "text", "text": question},
        ],
    }
]

你可以将以下推理代码中的 device 设置为“GPU“,以激活系统中 Intel 集显或是独显的能力。

from ov_qwen2_vl import OVQwen2VLModel
model = OVQwen2VLModel(model_dir, device)

processor = AutoProcessor.from_pretrained(model_dir, min_pixels=min_pixels, max_pixels=max_pixels)

text = processor.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
image_inputs, video_inputs = process_vision_info(messages)
inputs = processor(
    text=[text],
    images=image_inputs,
    videos=video_inputs,
    padding=True,
    return_tensors="pt",
)

generated_ids = model.generate(**inputs, max_new_tokens=100, streamer=TextStreamer(processor.tokenizer, skip_prompt=True, skip_special_tokens=True))

示例输出效果如下:

Question:

Describe this image.

Answer:

The image depicts a woman sitting on a sandy beach with a large dog. The dog is standing on its hind legs, reaching up to give the woman a high-five. The woman is smiling and appears to be enjoying the moment. The background shows the ocean with gentle waves, and the sky is clear with a soft light, suggesting it might be either sunrise or sunset. The scene is serene and joyful, capturing a heartwarming interaction between the woman and her dog.

 

3. 视频内容理解

由于 Qwen2-VL 可以同时支持对多个图像输入,因此可以基于这一特性实现视频内容理解,实现方法也特别简单,仅需对视频文件抽帧后保存为图片,并将这些图片基于 Qwen2-VL 提供的预处理脚本合并后,转化为 Prompt 模板,送入模型流水线进行推理。值得注意的是,当你将"type"设置为 "video"后,processor 会自动将两张图片拼接为一张,进行处理,以优化推理性能,并降低多图任务的内存占用。

question = "描述一下这段视频"
messages = [
    {
        "role": "user",
        "content": [
            {
                "type": "video",
                "video": [
                    "file://./examples/keyframe_1.jpg",
                    "file://./examples/keyframe_2.jpg",
                    "file://./examples/keyframe_3.jpg",
                    "file://./examples/keyframe_4.jpg",
                ],
                "fps": 1.0,
            },
            {"type": "text", "text": question},
        ],
    }
]

 

Qwen2-Audio

1. 模型转换及量化

针对 Qwen2-Audio,我们同样在 Qwen2-VL 示例地址中对模型的转换和量化步骤进行了接口封装,其中包含语言模型 lang_model,音频编码模型 audio_embed,文本 token 编码模型 embed_token 模型以及音频特征映射模型 projection。使用方法如下:

from ov_qwen2_audio_helper import convert_qwen2audio_model
import nncf

compression_configuration = {
    "mode": nncf.CompressWeightsMode.INT4_ASYM,
    "group_size": 128,
    "ratio": 1.0,
}

convert_qwen2audio_model("Qwen/Qwen2-Audio-7B-Instruct", model_dir, compression_configuration)

2. 语音对话

Qwen2-Audio 提供语音对话和音频分析两种任务模式。在语音对话模式中,用户只需输入语音而无需输入文字,指令则通过语音直接传达给模型。下面则是一个音频分析的例子。

conversation = [
    {"role": "system", "content": "You are a helpful assistant."},
    {
        "role": "user",
        "content": [
            {"type": "audio", "audio_url": audio_chat_url},
        ],
    },
]

text = processor.apply_chat_template(conversation, add_generation_prompt=True, tokenize=False)
audios = [librosa.load(audio_chat_file, sr=processor.feature_extractor.sampling_rate)[0]]

inputs = processor(text=text, audios=audios, return_tensors="pt", padding=True)

generate_ids = ov_model.generate(**inputs, max_new_tokens=50, streamer=TextStreamer(processor.tokenizer, skip_prompt=True, skip_special_tokens=True))

和 Qwen2-VL 一样,我们需要在构建输入 Prompt 前,提前准备好字典格式的数据,可以看到在语音对话模式下,我们仅需提供音频文件的地址或路径。该示例的输出如下:

Answer:

Yes, I can guess that you are a female in your twenties.

从输出结果可以看到 Qwen2-Audio 不光可以理解音频内容,并且可以识别对话者的音色和语调。

 

3. 音频分析

在音频分析模式下,Qwen2-Audio则支持多模态输入,此时我们可以将文本和音频拼接在一起,作为prompt送入模型中进行推理。

question = "What does the person say?"

conversation = [
    {"role": "system", "content": "You are a helpful assistant."},
    {
        "role": "user",
        "content": [
            {"type": "audio", "audio_url": audio_url},
            {"type": "text", "text": question},
        ],
    },
]

示例输入结果:

Answer:

The person says: 'Mister Quilter is the apostle of the middle classes, and we are glad to welcome his gospel.'

 

总结与展望

通过 OpenVINO™ 封装后的 API 函数,开发者可以非常便捷地对预训练模型进行转化压缩,并实现本地化的推理任务部署。同时基于 Qwen2 系列多模态模型强大的音频与图像理解能力,我们仅在轻薄本上便可以构建起一个完整的语言模型应用,在保护用户数据隐私的同时,降低硬件门槛。后期我们也计划将 Qwen2 多模态系列模型的流水线集成进 Optimum 组件中,方便开发者更灵活地进行调用,敬请期待。

参考资料

Qwen2-VL: 

https://github.com/QwenLM/Qwen2-VL

Qwen2-Audio:

https://github.com/QwenLM/Qwen2-Audio

Logo

ModelScope旨在打造下一代开源的模型即服务共享平台,为泛AI开发者提供灵活、易用、低成本的一站式模型服务产品,让模型应用更简单!

更多推荐