本人项目地址大全:Victor94-king/NLP__ManVictor: 优快云 of ManVictor
写在前面: 笔者更新不易,希望走过路过点个关注和赞,笔芯!!!
写在前面: 笔者更新不易,希望走过路过点个关注和赞,笔芯!!!
写在前面: 笔者更新不易,希望走过路过点个关注和赞,笔芯!!!
前言
多模态大模型越来越火,效果也越来越好,比如Qwen2-VL效果就不错,其支持不同尺寸图片理解、视频理解,其目前已经开源,且最大开源到了72B。
关于其技术报告,官方已经发了paper,大家可以自行阅读,而笔者主要想通过实操来看一下其实际效果以及提前踩一些坑,给大家提供一些具体的demo代码,方便大家迅速上手,甚至以此为开端上手所有多模态模型。
需要说明的是,这里的多模态是指多模态理解模型,如果大家对图片等生成模型比较感兴趣,可以看笔者之前另外一个系列:
《快速上手文生图sd3.5模型》:<span leaf="">https://zhuanlan.zhihu.com/p/8561420928</span>
言归正传,本篇一共分为两个part,第一个part是简单快速试一下其对视频理解的效果。第二个part是阿里天池上面正在举行一个多模态AI比赛(截至笔者写这篇博客的时候,其还没结束,感兴趣的小伙伴,可以一试),我们正好可以进行一下实践,看看能得多少分,当然官方也给了对应的baseline,大家也可以参考,也是使用Qwen2-VL。
全文较长,感兴趣的小伙伴建议收藏。
Qwen2-VL paper:<span leaf="">https://arxiv.org/pdf/2409.12191</span>
Qwen2-VL官方github链接:<span leaf="">https://github.com/QwenLM/Qwen2-VL</span>
WWW2025 多模态对话系统意图识别挑战赛:<span leaf="">https://tianchi.aliyun.com/competition/entrance/532277?spm=a2c22.12281949.0.0.7e923b74AbsruI</span>
小试牛刀
推理的demo大家直接用其git中提供的demo即可,作者这里试一下其对视频的理解能力
下面这个视频是笔者之前去江西婺源的景点拍摄的。
,时长00:12
[ ]
我们先试一下其7b的模型:<span leaf="">https://huggingface.co/Qwen/Qwen2-VL-7B-Instruct</span>
from transformers import Qwen2VLForConditionalGeneration, AutoTokenizer, AutoProcessor
from qwen_vl_utils import process_vision_info, smart_resize
model_path = "Qwen2-VL-7B-Instruct"
model = Qwen2VLForConditionalGeneration.from_pretrained(model_path, torch_dtype="auto", device_map="auto", attn_implementation='flash_attention_2')
processor = AutoProcessor.from_pretrained(model_path)
messages = [
{
"role": "user",
"content": [
{
"type": "video",
"video": "data/demo.mp4"
},
{"type": "text", "text": "描述一下这个视频,并猜一下这个视频可能的拍摄地点"},
],
}
]
# Preparation for inference
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",
)
inputs = inputs.to("cuda")
# Inference
generated_ids = model.generate(**inputs, max_new_tokens=128)
generated_ids_trimmed = [
out_ids[len(in_ids) :] for in_ids, out_ids in zip(inputs.input_ids, generated_ids)
]
output_text = processor.batch_decode(
generated_ids_trimmed, skip_special_tokens=True, clean_up_tokenization_spaces=False
)
print(output_text)
它的输出如下:
['这个视频展示了一个传统的中国村庄,坐落在山间。村庄的房屋大多是白墙黑瓦,屋顶上覆盖着黑色的瓦片。村庄的建筑风格具有浓厚的中国传统文化特色,房屋紧密相连,形成了一个紧凑的社区。村庄周围环绕着郁郁葱葱的树木和山脉,给人一种宁静和自然的感觉。天空中有一些云雾,增加了画面的层次感和神秘感。\n\n根据这些特征,这个视频可能的拍摄地点是中国的某个山区村庄,可能是江南地区的一个古镇,如乌镇、西塘等。这些地方以其传统的建筑风格和美丽的自然风光而闻名。']
可以看到基本上描述是正确的,而且比较详细,不过没有直接说出地点到底是哪里?其还提供了一个Qwen2-VL-Max版本的在线demo,我们直接试一下
demo:<span leaf="">https://huggingface.co/spaces/Qwen/Qwen2-VL</span>
可以看到其准确说出了地点,而且追问拍摄手法时也回答的准确,效果还是不错的。
初步体验还是可以的,接下来我们就要开始上手训练一番了。
实践
- 比赛简介
此比赛全部为文本结合图像的分类任务,其中包含2大类:图片场景分类以及多轮对话意图分类,比赛限制模型大小参数量小于10B
- baseline
我们先不做任何trick,直接用官方提供的1K训练集进行训练,然后提交一个baseline看看能有多少分?
Qwen2-VL的训练代码用的是LLaMA-Factory:<span leaf="">https://github.com/hiyouga/LLaMA-Factory</span>
该框架非常的简单,容易上手,按照步骤一步步照着做就行。
首先是把数据转化成其要求的shareg pt格式:
import os
import json
def tans2sharegpt_train(input_file, output_file):
train_data_list = []
with open(input_file) as f:
lines = json.load(f)
for line in lines:
cur_train_data = dict()
messages = [{"role":"user", "content":line["instruction"]}, {"role":"assistant", "content":line["output"]}]
images = []
for cur_image_name in line["image"]:
images.append(os.path.join("data/train/images",cur_image_name))
cur_train_data["messages"] = messages
cur_train_data["images"] = images
train_data_list.append(cur_train_data)
# save
with open(output_file, "w") as file:
json.dump(train_data_list, file, ensure_ascii=False)
print("total data num : ", len(train_data_list))
input_file = "data/train/train.json"
output_file = "data/www2025.json"
tans2sharegpt_train(input_file, output_file)
然后在data/dataset_info.json里面配置一下:
"www2025": {
"file_name": "www2025.json",
"formatting": "sharegpt",
"columns": {
"messages": "messages",
"images": "images"
},
"tags": {
"role_tag": "role",
"content_tag": "content",
"user_tag": "user",
"assistant_tag": "assistant"
}
},
接下来就可以开始训练就行了,LLaMA-Factory推荐的训练脚本是其 llamafactory-cli工具,推理也是用它,该工具其实是其作者进行了进一步的封装,笔者这里这里再给一个更原生一点的启动脚本供大家参考,其中@就是实际大家需要使用的机器数。
pip install pyav
pip install transformers==4.46.1
pip install qwen-vl-utils==0.0.2
pip install accelerate==1.0.1
pip install deepspeed==0.13.5
cd LLaMA-Factory
OUTPUT_PATH=/output/www2025_1126