关于 llava
LLaVA 是用于对话式图像理解的开源视觉语言模型,面向视觉问答、图像描述和多轮图像对话等任务。它提供多种参数规模,并支持 4-bit 量化以降低显存需求。
主要功能
- 01图像理解与视觉问答
- 02图像描述和对象识别
- 03多轮图像对话
- 04多种模型规模
- 054-bit 量化推理
- 06Gradio 网页界面
适用场景
- 01构建视觉语言聊天机器人
- 02针对图像回答问题
- 03描述图像或识别场景对象
- 04从图像文档提取信息
安装
复制下面的命令,在终端中运行。
$
npx skills add https:/ / github.com/ kalarislabs/ research-agent-skills --skill llava安装后在 Claude Code 中输入/llava确认已加载。
附带文件references/training.md
详细介绍
LLaVA - 大语言与视觉助手
用于对话式图像理解的开源视觉语言模型。
何时使用 LLaVA
适用于:
- 构建视觉语言聊天机器人
- 视觉问答(VQA)
- 图像描述与配文
- 多轮图像对话
- 视觉指令遵循
- 基于图像的文档理解
指标:
- 目标能力达到 GPT-4V 水平
- Apache 2.0 许可证
- 多种模型规模(7B-34B 参数)
也可选择以下替代方案:
- GPT-4V:质量最高,基于 API
- CLIP:简单的零样本分类
- BLIP-2:更适合仅用于配文
- Flamingo:用于研究,并非开源
快速入门
安装
# Clone repository
git clone https://github.com/haotian-liu/LLaVA
cd LLaVA
# Install
pip install -e .
基本用法
from llava.model.builder import load_pretrained_model
from llava.mm_utils import get_model_name_from_path, process_images, tokenizer_image_token
from llava.constants import IMAGE_TOKEN_INDEX, DEFAULT_IMAGE_TOKEN
from llava.conversation import conv_templates
from PIL import Image
import torch
# Load model
model_path = "liuhaotian/llava-v1.5-7b"
tokenizer, model, image_processor, context_len = load_pretrained_model(
model_path=model_path,
model_base=None,
model_name=get_model_name_from_path(model_path)
)
# Load image
image = Image.open("image.jpg")
image_tensor = process_images([image], image_processor, model.config)
image_tensor = image_tensor.to(model.device, dtype=torch.float16)
# Create conversation
conv = conv_templates["llava_v1"].copy()
conv.append_message(conv.roles[0], DEFAULT_IMAGE_TOKEN + "\nWhat is in this image?")
conv.append_message(conv.roles[1], None)
prompt = conv.get_prompt()
# Generate response
input_ids = tokenizer_image_token(prompt, tokenizer, IMAGE_TOKEN_INDEX, return_tensors='pt').unsqueeze(0).to(model.device)
with torch.inference_mode():
output_ids = model.generate(
input_ids,
images=image_tensor,
do_sample=True,
temperature=0.2,
max_new_tokens=512
)
response = tokenizer.decode(output_ids[0], skip_special_tokens=True).strip()
print(response)
可用模型
| 模型 | 参数量 | VRAM | 质量 |
|---|---|---|---|
| LLaVA-v1.5-7B | 7B | ~14 GB | 良好 |
| LLaVA-v1.5-13B | 13B | ~28 GB | 更好 |
| LLaVA-v1.6-34B | 34B | ~70 GB | 最佳 |
# Load different models
model_7b = "liuhaotian/llava-v1.5-7b"
model_13b = "liuhaotian/llava-v1.5-13b"
model_34b = "liuhaotian/llava-v1.6-34b"
# 4-bit quantization for lower VRAM
load_4bit = True # Reduces VRAM by ~4×
CLI 用法
# Single image query
python -m llava.serve.cli \
--model-path liuhaotian/llava-v1.5-7b \
--image-file image.jpg \
--query "What is in this image?"
# Multi-turn conversation
python -m llava.serve.cli \
--model-path liuhaotian/llava-v1.5-7b \
--image-file image.jpg
# Then type questions interactively
Web UI(Gradio)
# Launch Gradio interface
python -m llava.serve.gradio_web_server \
--model-path liuhaotian/llava-v1.5-7b \
--load-4bit # Optional: reduce VRAM
# Access at http://localhost:7860
多轮对话
# Initialize conversation
conv = conv_templates["llava_v1"].copy()
# Turn 1
conv.append_message(conv.roles[0], DEFAULT_IMAGE_TOKEN + "\nWhat is in this image?")
conv.append_message(conv.roles[1], None)
response1 = generate(conv, model, image) # "A dog playing in a park"
# Turn 2
conv.messages[-1][1] = response1 # Add previous response
conv.append_message(conv.roles[0], "What breed is the dog?")
conv.append_message(conv.roles[1], None)
response2 = generate(conv, model, image) # "Golden Retriever"
# Turn 3
conv.messages[-1][1] = response2
conv.append_message(conv.roles[0], "What time of day is it?")
conv.append_message(conv.roles[1], None)
response3 = generate(conv, model, image)
常见任务
图像配文
question = "Describe this image in detail."
response = ask(model, image, question)
视觉问答
question = "How many people are in the image?"
response = ask(model, image, question)
目标检测(文本形式)
question = "List all the objects you can see in this image."
response = ask(model, image, question)
场景理解
question = "What is happening in this scene?"
response = ask(model, image, question)
文档理解
question = "What is the main topic of this document?"
response = ask(model, document_image, question)
训练自定义模型
# Stage 1: Feature alignment (558K image-caption pairs)
bash scripts/v1_5/pretrain.sh
# Stage 2: Visual instruction tuning (150K instruction data)
bash scripts/v1_5/finetune.sh
量化(降低 VRAM 占用)
# 4-bit quantization
tokenizer, model, image_processor, context_len = load_pretrained_model(
model_path="liuhaotian/llava-v1.5-13b",
model_base=None,
model_name=get_model_name_from_path("liuhaotian/llava-v1.5-13b"),
load_4bit=True # Reduces VRAM ~4×
)
# 8-bit quantization
load_8bit=True # Reduces VRAM ~2×
最佳实践
- 从 7B 模型开始 - 质量良好,VRAM 占用适中
- 使用 4-bit 量化 - 显著降低 VRAM 占用
- 需要 GPU - CPU 推理速度极慢
- 提示词要清晰 - 具体的问题能获得更好的回答
- 进行多轮对话 - 保持对话上下文
- Temperature 设为 0.2-0.7 - 平衡创造性与一致性
- max_new_tokens 设为 512-1024 - 适用于详细回答
- 批量处理 - 依次处理多张图像
性能
| 模型 | VRAM(FP16) | VRAM(4-bit) | 速度(tokens/s) |
|---|---|---|---|
| 7B | ~14 GB | ~4 GB | ~20 |
| 13B | ~28 GB | ~8 GB | ~12 |
| 34B | ~70 GB | ~18 GB | ~5 |
使用 A100 GPU 时
基准测试
LLaVA 在以下测试中取得了有竞争力的分数:
- VQAv2:78.5%
- GQA:62.0%
- MM-Vet:35.4%
- MMBench:64.3%
局限性
- 幻觉 - 可能描述图像中不存在的内容
- 空间推理 - 难以准确判断位置
- 小字 - 难以读取细小文字
- 物体计数 - 物体数量较多时不够准确
- VRAM 要求 - 需要性能强劲的 GPU
- 推理速度 - 比 CLIP 慢
与框架集成
LangChain
from langchain.llms.base import LLM
class LLaVALLM(LLM):
def _call(self, prompt, stop=None):
# Custom LLaVA inference
return response
llm = LLaVALLM()
Gradio 应用
import gradio as gr
def chat(image, text, history):
response = ask_llava(model, image, text)
return response
demo = gr.ChatInterface(
chat,
additional_inputs=[gr.Image(type="pil")],
title="LLaVA Chat"
)
demo.launch()
资源
- GitHub: https://github.com/haotian-liu/LLaVA ⭐ 23,000+
- 论文: https://arxiv.org/abs/2304.08485
- 演示: https://llava.hliu.cc
- 模型: https://huggingface.co/liuhaotian
- 许可证: Apache 2.0
Agent 操作流程
- 检查环境。 确认 GPU 内存、模型检查点和许可证、框架版本及输入格式。
- 明确输入。 根据数据或用户确认格式、标识符和参数。凡是会影响结果的值,不要猜测,应主动询问。
- 先运行小规模版本。 先以低分辨率或较短长度对单个样本运行推理。
- 执行完整任务,遵循上述说明和参考资料。
- 验证结果。 通过定性检查和任务指标检查输出;核查生成内容的许可证限制。
- 汇报。 说明运行了什么(版本、命令、参数)、检查了什么,以及还有哪些不确定之处。
| 如果出现这种情况 | 请这样做 |
|---|---|
| 大型输入导致内存不足 | 降低分辨率或序列长度,使用半精度,或对输入进行分块处理。 |
| 这些说明中提到的函数、标志或端点在已安装版本中不存在 | 查阅已安装版本自带的文档(help()、--help、官方文档),进行适配并告知用户。切勿编造 API。 |
| 必需的输入、标识符或参数含义不明确 | 询问用户,或在运行前明确说明所采用的假设。 |
诚信规则
- 切勿捏造结果、参数、标识符、引用或统计数据。如果无法运行或验证某项内容,请如实说明。
- 商业用途或发布前,请检查模型和数据集的许可证。
- 此处与版本相关的细节可能已过时:请对照已安装版本的官方文档进行确认。
- 对于涉及费用、占用共享 GPU 或云配额、接触个人或患者数据,或无法撤销的操作,请先征求同意。
相关技能
clip:OpenAI 的视觉与语言连接模型。blip-2-vision-language:介绍如何通过 HuggingFace Trans… 使用 Salesforce BLIP-2(Q-Former 将冻结的图像编码器与 OPT 或 FlanT5 等 LLM 连接起来)fine-tuning-with-trl:使用 TRL 通过强化学习微调 LLM——用于指令微调的 SFT、用于偏好对齐的 DPO、用于奖励优化的 PPO/GRPO…
评论
支持 Markdown