HuggingFace镜像/cogvlm2-llama3-chinese-chat-19B-int4
模型介绍文件和版本分析
下载使用量0

CogVLM2

👋 微信交流 · 💡在线演示 · 🎈GitHub主页

📍在 智谱AI开放平台 体验更大规模的CogVLM模型。

模型介绍

我们推出新一代CogVLM2系列模型,并开源了基于Meta-Llama-3-8B-Instruct构建的两款模型。与上一代CogVLM开源模型相比,CogVLM2系列开源模型具有以下改进:

  1. 在TextVQA、DocVQA等多项基准测试中性能显著提升。
  2. 支持8K内容长度。
  3. 支持最高1344×1344的图像分辨率。
  4. 提供支持中英文双语的开源模型版本。

CogVLM2 Int4模型需要16G GPU内存,且必须在配备Nvidia GPU的Linux系统上运行。

模型名称cogvlm2-llama3-chinese-chat-19B-int4cogvlm2-llama3-chinese-chat-19B
所需GPU内存16G42G
系统要求Linux (带Nvidia GPU)Linux (带Nvidia GPU)

基准测试

我们的开源模型与上一代CogVLM开源模型相比,在多项榜单中均取得了优异成绩。其出色性能可与部分非开源模型相媲美,如下表所示:

模型是否开源LLM大小TextVQADocVQAChartQAOCRbenchMMMUMMVetMMBench
CogVLM1.1✅7B69.7-68.359037.352.065.8
LLaVA-1.5✅13B61.3--33737.035.467.7
Mini-Gemini✅34B74.1---48.059.380.6
LLaVA-NeXT-LLaMA3✅8B-78.269.5-41.7-72.1
LLaVA-NeXT-110B✅110B-85.779.7-49.1-80.5
InternVL-1.5✅20B80.690.983.872046.855.482.3
QwenVL-Plus❌-78.991.478.172651.455.767.0
Claude3-Opus❌--89.380.869459.451.763.3
Gemini Pro 1.5❌-73.586.581.3-58.5--
GPT-4V❌-78.088.478.565656.867.775.0
CogVLM2-LLaMA3 (我们的模型)✅8B84.292.381.075644.360.480.5
CogVLM2-LLaMA3-Chinese (我们的模型)✅8B85.088.474.778042.860.578.9

所有评测结果均未使用任何外部OCR工具("纯像素输入")。

快速开始

以下是使用该模型与 CogVLM2 模型进行对话的简单示例。更多使用场景,请在我们的 github 中查找。

import torch
from PIL import Image
from transformers import AutoModelForCausalLM, AutoTokenizer

MODEL_PATH = "THUDM/cogvlm2-llama3-chinese-chat-19B-int4"
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,
    low_cpu_mem_usage=True,
).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))

许可协议

本模型基于 CogVLM2 许可协议 发布。对于使用 Meta Llama 3 构建的模型,还请同时遵守 LLAMA3_许可协议。

引用

如果您发现我们的工作对您有所帮助,请考虑引用以下论文

@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}
}