说明文档
LLaVA Interleave 模型卡片
模型详情
模型类型: LLaVA Interleave 是一个开源聊天机器人,通过在多模态指令遵循数据上微调 LLM 训练而成。它是一个基于 Transformer 架构的自回归语言模型。 基础 LLM:Qwen/Qwen1.5-7B-Chat
论文或更多资源: https://llava-vl.github.io/
主要预期用途: LLaVA-Next Interleave 的主要用途是大型多模态模型和聊天机器人的研究。这仅用于研究探索,禁止商业使用。
主要预期用户: 该模型的主要预期用户是计算机视觉、自然语言处理、机器学习和人工智能领域的研究人员和爱好者。
如何使用模型
首先,请确保安装了 transformers >= 4.35.3。
该模型支持多图像和多提示生成。这意味着您可以在提示中传递多个图像。请确保遵循正确的提示模板(USER: xxx\nASSISTANT:),并在您想查询图像的位置添加 <image> 标记:
使用 pipeline:
下面我们使用了 "llava-hf/llava-interleave-qwen-0.5b-hf" 检查点。
from transformers import pipeline
from PIL import Image
import requests
model_id = "llava-hf/llava-interleave-qwen-0.5b-dpo-hf"
pipe = pipeline("image-to-text", model=model_id)
url = "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/tasks/ai2d-demo.jpg"
image = Image.open(requests.get(url, stream=True).raw)
# 定义聊天历史并使用 `apply_chat_template` 获取正确格式的提示
# "content" 中的每个值必须是包含类型("text"、"image")的字典列表
conversation = [
{
"role": "user",
"content": [
{"type": "text", "text": "标签 15 代表什么?(1) 熔岩 (2) 核心 (3) 隧道 (4) 火山灰云"},
{"type": "image"},
],
},
]
prompt = processor.apply_chat_template(conversation, add_generation_prompt=True)
outputs = pipe(image, prompt=prompt, generate_kwargs={"max_new_tokens": 200})
print(outputs)
使用纯 transformers:
下面是一个在 GPU 设备上以 float16 精度运行生成的示例脚本:
import requests
from PIL import Image
import torch
from transformers import AutoProcessor, LlavaForConditionalGeneration
model_id = "llava-hf/llava-interleave-qwen-0.5b-dpo-hf"
model = LlavaForConditionalGeneration.from_pretrained(
model_id,
torch_dtype=torch.float16,
low_cpu_mem_usage=True,
).to(0)
processor = AutoProcessor.from_pretrained(model_id)
# 定义聊天历史并使用 `apply_chat_template` 获取正确格式的提示
# "content" 中的每个值必须是包含类型("text"、"image")的字典列表
conversation = [
{
"role": "user",
"content": [
{"type": "text", "text": "这些是什么?"},
{"type": "image"},
],
},
]
prompt = processor.apply_chat_template(conversation, add_generation_prompt=True)
image_file = "http://images.cocodataset.org/val2017/000000039769.jpg"
raw_image = Image.open(requests.get(image_file, stream=True).raw)
inputs = processor(prompt, raw_image, return_tensors='pt').to(0, torch.float16)
output = model.generate(**inputs, max_new_tokens=200, do_sample=False)
print(processor.decode(output[0][2:], skip_special_tokens=True))
当使用视频/3D/多视角输入进行提示时,请按以下方式提示:
# 如果您从输入中下采样了 n 帧
image_tokens = "<image>" * n
prompt = f"<|im_start|>user {image_tokens}\n这些是什么?|im_end|><|im_start|>assistant"
# 使用聊天模板,如果您采样了 5 帧,则在一个对话轮次中必须有 5 张图像
conversation = [
{
"role": "user",
"content": [
{"type": "text", "text": "这些是什么?"},
{"type": "image"},
{"type": "image"},
{"type": "image"},
{"type": "image"},
{"type": "image"},
],
},
]
prompt = processor.apply_chat_template(conversation, add_generation_prompt=True)
当使用交错图像和视频进行提示时,请按以下方式提示:
# 两张交错图像
prompt = "<|im_start|>user <image><image>\n这两张图像之间有什么区别?|im_end|><|im_start|>assistant"
# 两个交错视频,如果您从两个视频中共下采样了 n 帧
image_tokens = "<image>" * n
prompt = f"<|im_start|>user {image_tokens}\n这些是什么?|im_end|><|im_start|>assistant"
# 交错格式的聊天模板与采样视频的工作方式相同。只需传入您想要为提示使用的任意数量的图像
conversation = [
{
"role": "user",
"content": [
{"type": "text", "text": "这两张图像之间有什么区别?"},
{"type": "image"},
{"type": "image"},
],
},
]
模型优化
通过 bitsandbytes 库进行 4 位量化
首先确保安装 bitsandbytes,pip install bitsandbytes,并确保可以访问 CUDA 兼容的 GPU 设备。只需将上面的代码片段更改为:
model = LlavaForConditionalGeneration.from_pretrained(
model_id,
torch_dtype=torch.float16,
low_cpu_mem_usage=True,
+ load_in_4bit=True
)
使用 Flash-Attention 2 进一步加速生成
首先确保安装 flash-attn。关于该软件包的安装,请参考 Flash Attention 的原始仓库。只需将上面的代码片段更改为:
model = LlavaForConditionalGeneration.from_pretrained(
model_id,
torch_dtype=torch.float16,
low_cpu_mem_usage=True,
+ use_flash_attention_2=True
).to(0)
许可声明
本项目使用了某些受其各自原始许可证约束的数据集和检查点。用户必须遵守这些原始许可证的所有条款和条件,包括但不限于数据集的 OpenAI 使用条款,以及使用数据集训练的检查点的基础语言模型的特定许可证 通义千问许可协议 和 META LLAMA 3 社区许可协议)。本项目不会在原始许可证规定之外施加任何额外限制。此外,提醒用户确保其对数据集和检查点的使用符合所有适用的法律法规。
Bibtext 引用
@misc{li2024llavanextinterleavetacklingmultiimagevideo,
title={LLaVA-NeXT-Interleave: Tackling Multi-image, Video, and 3D in Large Multimodal Models},
author={Feng Li and Renrui Zhang and Hao Zhang and Yuanhan Zhang and Bo Li and Wei Li and Zejun Ma and Chunyuan Li},
year={2024},
eprint={2407.07895},
archivePrefix={arXiv},
primaryClass={cs.CV},
url={https://arxiv.org/abs/2407.07895},
}
luisresende13/llava-interleave-qwen-0.5b-hf
作者 luisresende13
创建时间: 2024-07-21 06:51:05+00:00
更新时间: 2024-07-21 07:52:46+00:00
在 Hugging Face 上查看