CogVLM2: 智谱开源新一代多模态大模型!

简介: 智谱·AI推出了新一代 CogVLM2 系列模型,并开源了使用 Meta-Llama-3-8B-Instruct 构建的两个模型。 与上一代CogVLM开源模型相比,CogVLM2系列开源模型有了很多改进...

引言


智谱·AI推出了新一代 CogVLM2 系列模型,并开源了使用 Meta-Llama-3-8B-Instruct 构建的两个模型。 与上一代CogVLM开源模型相比,CogVLM2系列开源模型有以下改进:

  • 许多基准测试(例如 TextVQA、DocVQA)的显着改进。
  • 支持8K内容长度。
  • 支持图像分辨率高达1344*1344。
  • 提供开源模型版本,支持中英文。


模型架构

CogVLM2 继承并优化了上一代模型的经典架构,采用了一个拥有50亿参数的强大视觉编码器,并创新性地在大语言模型中整合了一个70亿参数的视觉专家模块。这一模块通过独特的参数设置,精细地建模了视觉与语言序列的交互,确保了在增强视觉理解能力的同时,不会削弱模型在语言处理上的原有优势。这种深度融合的策略,使得视觉模态与语言模态能够更加紧密地结合。


模型信息:

模型名称

cogvlm2-llama3-chat-19B

cogvlm2-llama3-chinese-chat-19B

基础模型

Meta-Llama-3-8B-Instruct

Meta-Llama-3-8B-Instruct

支持语言

English

Chinese, English

模型大小

19B

19B

任务

Image understanding, dialogue model

Image understanding, dialogue model

上下文窗口

8K

8K

图像分辨率

1344 * 1344

1344 * 1344

Benchmark

相比上一代CogVLM开源模型,CogVLM2模型在很多榜单中都取得了不错的成绩。


代码仓库:


模型体验

创空间体验链接:

https://www.modelscope.cn/studios/ZhipuAI/Cogvlm2-llama3-chinese-chat-Demo

数数:


文档理解:


大促信息解读:

添加图片注释,不超过 140 字(可选)

手写菜单理解:


图表理解:


agent效果:


模型链接及下载

cogvlm2-llama3-chinese-chat-19B

模型链接:

https://www.modelscope.cn/models/ZhipuAI/cogvlm2-llama3-chinese-chat-19B

cogvlm2-llama3-chat-19B

模型链接:

https://www.modelscope.cn/models/ZhipuAI/cogvlm2-llama3-chat-19B/summary

模型weights下载

from modelscope import snapshot_download model_dir = snapshot_download("ZhipuAI/cogvlm2-llama3-chinese-chat-19B")


模型推理

CogVLM2推理代码

import torch from PIL import Image from modelscope import AutoModelForCausalLM, AutoTokenizer MODEL_PATH = "ZhipuAI/cogvlm2-llama3-chinese-chat-19B" DEVICE = 'cuda' if torch.cuda.is_available() else 'cpu' TORCH_TYPE = torch.bfloat16 if torch.cuda.is_available() and torch.cuda.get_device_capability()[0] >= 8 else torch.float16 tokenizer = AutoTokenizer.from_pretrained(     MODEL_PATH,     trust_remote_code=True ) model = AutoModelForCausalLM.from_pretrained(     MODEL_PATH,     torch_dtype=TORCH_TYPE,     trust_remote_code=True, ).to(DEVICE).eval() text_only_template = "A chat between a curious user and an artificial intelligence assistant. The assistant gives helpful, detailed, and polite answers to the user's questions. USER: {} ASSISTANT:" while True:     image_path = input("image path >>>>> ")     if image_path == '':         print('You did not enter image path, the following will be a plain text conversation.')         image = None         text_only_first_query = True     else:         image = Image.open(image_path).convert('RGB')     history = []     while True:         query = input("Human:")         if query == "clear":             break         if image is None:             if text_only_first_query:                 query = text_only_template.format(query)                 text_only_first_query = False             else:                 old_prompt = ''                 for _, (old_query, response) in enumerate(history):                     old_prompt += old_query + " " + response + "\n"                 query = old_prompt + "USER: {} ASSISTANT:".format(query)         if image is None:             input_by_model = model.build_conversation_input_ids(                 tokenizer,                 query=query,                 history=history,                 template_version='chat'             )         else:             input_by_model = model.build_conversation_input_ids(                 tokenizer,                 query=query,                 history=history,                 images=[image],                 template_version='chat'             )         inputs = {             'input_ids': input_by_model['input_ids'].unsqueeze(0).to(DEVICE),             'token_type_ids': input_by_model['token_type_ids'].unsqueeze(0).to(DEVICE),             'attention_mask': input_by_model['attention_mask'].unsqueeze(0).to(DEVICE),             'images': [[input_by_model['images'][0].to(DEVICE).to(TORCH_TYPE)]] if image is not None else None,         }         gen_kwargs = {             "max_new_tokens": 2048,             "pad_token_id": 128002,           }         with torch.no_grad():             outputs = model.generate(**inputs, **gen_kwargs)             outputs = outputs[:, inputs['input_ids'].shape[1]:]             response = tokenizer.decode(outputs[0])             response = response.split("<|end_of_text|>")[0]             print("\nCogVLM2:", response)         history.append((query, response))


显存占用:

添加图片注释,不超过 140 字(可选)

模型微调

我们将使用swift来对CogVLM2进行微调。swift是魔搭社区官方提供的大模型与多模态大模型微调推理框架。swift开源地址:https://github.com/modelscope/swift

swift对CogVLM2推理与微调的最佳实践可以查看:https://github.com/modelscope/swift/blob/main/docs/source/Multi-Modal/cogvlm2%E6%9C%80%E4%BD%B3%E5%AE%9E%E8%B7%B5.md

通常,多模态大模型微调会使用自定义数据集进行微调。在这里,我们将展示可直接运行的demo。我们使用 coco-mini-en-2 数据集进行微调,该数据集的任务是对图片内容进行描述。

在开始微调之前,请确保您的环境已准备妥当。

git clone https://github.com/modelscope/swift.gitcd swift pip install -e .[llm]

LoRA微调脚本如下所示。该脚本将只对语言和视觉模型的qkv进行lora微调,如果你想对所有linear层都进行微调,可以指定--lora_target_modules ALL。

# 单卡 # Experimental environment: A100 # 70GB GPU memory CUDA_VISIBLE_DEVICES=0 swift sft \     --model_type cogvlm2-19b-chat \     --dataset coco-mini-en-2 \ # ZeRO2 # Experimental environment: 2 * A100 # 2 * 66GB GPU memory CUDA_VISIBLE_DEVICES=0,1 NPROC_PER_NODE=2 swift sft \     --model_type cogvlm2-19b-chat \     --dataset coco-mini-en-2 \     --deepspeed default-zero2

如果要使用自定义数据集,只需按以下方式进行指定:

--dataset train.jsonl \

自定义数据集支持json和jsonl样式。CogVLM2支持多轮对话,但总的对话轮次中需包含一张图片, 支持传入本地路径或URL。以下是自定义数据集的示例:

{"query": "55555", "response": "66666", "images": ["image_path"]} {"query": "eeeee", "response": "fffff", "history": [], "images": ["image_path"]} {"query": "EEEEE", "response": "FFFFF", "history": [["AAAAA", "BBBBB"], ["CCCCC", "DDDDD"]], "images": ["image_path"]}

微调后推理脚本如下,这里的ckpt_dir需要修改为训练生成的checkpoint文件夹:

CUDA_VISIBLE_DEVICES=0 swift infer \     --ckpt_dir output/cogvlm2-19b-chat/vx-xxx/checkpoint-xxx \     --load_dataset_config true \

你也可以选择merge-lora并进行推理:

CUDA_VISIBLE_DEVICES=0 swift export \     --ckpt_dir output/cogvlm2-19b-chat/vx-xxx/checkpoint-xxx \     --merge_lora true CUDA_VISIBLE_DEVICES=0 swift infer \     --ckpt_dir output/cogvlm2-19b-chat/vx-xxx/checkpoint-xxx-merged \     --load_dataset_config true

微调过程的loss可视化:(由于时间原因,这里我们只训练了250个steps)

添加图片注释,不超过 140 字(可选)

使用验证集对微调后模型进行推理的示例:

添加图片注释,不超过 140 字(可选)

[PROMPT]<|begin_of_text|>[128002 * 2306]Question: please describe the image. Answer:[OUTPUT]A group of people riding on top of elephants.<|end_of_text|> [LABELS]Men in blue with hats are riding elephants. [IMAGES]['https://xingchen-data.oss-cn-zhangjiakou.aliyuncs.com/coco/2014/val2014/COCO_val2014_000000320743.jpg']


添加图片注释,不超过 140 字(可选)

[PROMPT]<|begin_of_text|>[128002 * 2306]Question: please describe the image. Answer:[OUTPUT]A beach with a lot of umbrellas and people.<|end_of_text|> [LABELS]A group of lawn chairs sitting on top of a beach. [IMAGES]['https://xingchen-data.oss-cn-zhangjiakou.aliyuncs.com/coco/2014/val2014/COCO_val2014_000000017272.jpg']


目录
打赏
0
1
1
0
157
分享
相关文章
通义OmniAudio大模型,让 AI 看懂 360° 视频,并“听”出对应的空间音频
OmniAudio 是一项突破性的空间音频生成技术,能够直接从 360° 视频生成 FOA(First-order Ambisonics)空间音频,为虚拟现实和沉浸式娱乐带来全新可能。通过自监督 coarse-to-fine 预训练和双分支视频表示微调,OmniAudio 在非空间音频质量和空间定位准确性上显著优于现有方法。项目包含超过 103,000 个视频片段的 Sphere360 数据集,支持高质量的模型训练与评估。代码、数据及论文均已开源,助力沉浸式体验技术发展。
智能体(AI Agent)开发实战之【LangChain】(二)结合大模型基于RAG实现本地知识库问答
智能体(AI Agent)开发实战之【LangChain】(二)结合大模型基于RAG实现本地知识库问答
魔塔社区-微调Qwen3-1.7B大模型实战
这是一篇关于模型微调实战的教程,主要步骤如下:1. 使用魔塔社区提供的GPU环境;2. 处理 delicate_medical_r1_data 数据集生成训练和验证文件;3. 加载Modelscope上的Qwen3-1.7B模型;4. 注册并使用Swanlab工具配置API;5. 按顺序执行完整代码完成微调设置;6. 展示训练过程。完整代码与实验记录分别托管于魔塔社区和SwanLab平台,方便复现与学习。
263 1
AI Agent智能体:底层逻辑、原理与大模型关系深度解析·优雅草卓伊凡
AI Agent智能体:底层逻辑、原理与大模型关系深度解析·优雅草卓伊凡
157 2
AI Agent智能体:底层逻辑、原理与大模型关系深度解析·优雅草卓伊凡
小米又放大招!MiMo-VL 多模态大模型开源,魔搭推理微调全面解读来了!
今天,小米开源发布两款 7B 规模视觉-语言模型 MiMo-VL-7B-SFT 和 MiMo-VL-7B-RL。
293 9
通义千问推理模型QwQ-32B开源,更小尺寸、更强性能
阿里云发布并开源全新推理模型通义千问QwQ-32B,通过大规模强化学习,在数学、代码及通用能力上实现质的飞跃,性能比肩DeepSeek-R1。该模型大幅降低部署成本,支持消费级显卡本地部署,并集成智能体Agent相关能力。阿里云采用Apache2.0协议全球开源,用户可通过通义APP免费体验。此外,通义团队已开源200多款模型,覆盖全模态和全尺寸。
通义 CoGenAV 大模型音画同步感知,重新定义语音理解边界
CoGenAV 是一种创新的多模态语音理解模型,核心理念是实现“音画同步”的深度理解。通过学习 audio-visual-text 的时序对齐关系,构建更鲁棒、更通用的语音表征框架。它在视觉语音识别(VSR)、音视频语音识别(AVSR)、语音增强与分离(AVSE/AVSS)及主动说话人检测(ASD)等任务中表现出色,尤其在嘈杂环境下性能显著提升。仅需 223 小时数据训练即可媲美传统数千小时数据的效果,大幅降低训练成本。CoGenAV 支持主流平台如 GitHub、HuggingFace 和 ModelScope,助力多场景应用开发。

热门文章

最新文章

AI助理

你好,我是AI助理

可以解答问题、推荐解决方案等