license: apache-2.0
language:
CogAgent
CogAgent 是基于 CogVLM 改进的开源视觉语言模型。
📖 论文: https://arxiv.org/abs/2312.08914
🚀 GitHub: 如需了解更多信息,如演示、微调和查询提示,请参考 我们的GitHub
提醒
📍 这是 CogAgent 的 cogagent-vqa
版本检查点。
我们开源了2个版本的 CogAgent 检查点,您可以根据需求选择其一。
-
cogagent-chat
: 此模型 在 GUI 代理、视觉多轮对话、视觉定位 等方面具有强大能力。
如果您需要 GUI 代理和视觉定位功能,或需与给定图像进行多轮对话,我们推荐使用此版本模型。
-
cogagent-vqa
: 此模型 在 单轮视觉对话 方面表现更优。
如果您需要 处理 VQA 基准测试(如 MMVET, VQAv2),我们推荐使用此模型。
简介
CogAgent-18B 拥有110亿视觉参数和70亿语言参数。
CogAgent 在图像理解和 GUI 代理方面展现出 卓越性能:
-
CogAgent-18B 在9个跨模态基准测试中达到顶尖水平,包括:VQAv2, MM-Vet, POPE, ST-VQA, OK-VQA, TextVQA, ChartQA, InfoVQA, DocVQA。
-
CogAgent-18B 在 GUI 操作数据集(如 AITW 和 Mind2Web)上显著 超越现有模型。
除 CogVLM 已有功能(视觉多轮对话、视觉定位)外,CogAgent 还具备:
-
支持更高分辨率视觉输入与对话问答。可处理 1120x1120 超高清图像输入。
-
拥有视觉代理能力,能针对任意 GUI 截图任务返回计划、下一步行动及带坐标的具体操作。
-
增强的 GUI 相关问答能力,可处理网页、PC应用、移动应用等任意 GUI 截图的提问。
-
通过改进预训练和微调,提升了 OCR 相关任务能力。
本仓库模型权重 学术研究免费。商业用途用户须注册 此处。注册用户可免费商用,但须遵守许可全部条款。软件任何副本或实质性部分均须包含许可声明。
快速开始
使用以下 Python 代码通过 cli_demo.py
快速体验:
import torch
from PIL import Image
from transformers import AutoModelForCausalLM, LlamaTokenizer
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("--quant", choices=[4], type=int, default=None, help='量化位数')
parser.add_argument("--from_pretrained", type=str, default="THUDM/cogagent-chat-hf", help='预训练权重路径')
parser.add_argument("--local_tokenizer", type=str, default="lmsys/vicuna-7b-v1.5", help='分词器路径')
parser.add_argument("--fp16", action="store_true")
parser.add_argument("--bf16", action="store_true")
args = parser.parse_args()
MODEL_PATH = args.from_pretrained
TOKENIZER_PATH = args.local_tokenizer
DEVICE = 'cuda' if torch.cuda.is_available() else 'cpu'
tokenizer = LlamaTokenizer.from_pretrained(TOKENIZER_PATH)
if args.bf16:
torch_type = torch.bfloat16
else:
torch_type = torch.float16
print("========使用 torch 类型:{} 设备:{}========\n\n".format(torch_type, DEVICE))
if args.quant:
model = AutoModelForCausalLM.from_pretrained(
MODEL_PATH,
torch_dtype=torch_type,
low_cpu_mem_usage=True,
load_in_4bit=True,
trust_remote_code=True
).eval()
else:
model = AutoModelForCausalLM.from_pretrained(
MODEL_PATH,
torch_dtype=torch_type,
low_cpu_mem_usage=True,
load_in_4bit=args.quant is not None,
trust_remote_code=True
).to(DEVICE).eval()
while True:
image_path = input("图片路径 >>>>> ")
if image_path == "stop":
break
image = Image.open(image_path).convert('RGB')
history = []
while True:
query = input("用户:")
if query == "clear":
break
input_by_model = model.build_conversation_input_ids(tokenizer, query=query, history=history, images=[image])
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 'cross_images' in input_by_model and input_by_model['cross_images']:
inputs['cross_images'] = [[input_by_model['cross_images'][0].to(DEVICE).to(torch_type)]]
gen_kwargs = {"max_length": 2048,
"temperature": 0.9,
"do_sample": False}
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("</s>")[0]
print("\nCog:", response)
history.append((query, response))
运行命令:
python cli_demo_hf.py --bf16
许可
本仓库代码遵循 Apache-2.0 许可,CogAgent 和 CogVLM 模型权重使用须遵守 模型许可。
引用与致谢
如果我们的工作对您有帮助,请考虑引用以下论文
@misc{hong2023cogagent,
title={CogAgent: A Visual Language Model for GUI Agents},
author={Wenyi Hong and Weihan Wang and Qingsong Lv and Jiazheng Xu and Wenmeng Yu and Junhui Ji and Yan Wang and Zihan Wang and Yuxiao Dong and Ming Ding and Jie Tang},
year={2023},
eprint={2312.08914},
archivePrefix={arXiv},
primaryClass={cs.CV}
}
@misc{wang2023cogvlm,
title={CogVLM: Visual Expert for Pretrained Language Models},
author={Weihan Wang and Qingsong Lv and Wenmeng Yu and Wenyi Hong and Ji Qi and Yan Wang and Junhui Ji and Zhuoyi Yang and Lei Zhao and Xixuan Song and Jiazheng Xu and Bin Xu and Juanzi Li and Yuxiao Dong and Ming Ding and Jie Tang},
year={2023},
eprint={2311.03079},
archivePrefix={arXiv},
primaryClass={cs.CV}
}
在 CogVLM 的指令微调阶段,我们使用了来自 MiniGPT-4, LLAVA, LRV-Instruction, LLaVAR 和 Shikra 项目的部分英文图文数据,以及许多经典跨模态工作数据集。我们衷心感谢这些项目的贡献。