LLaVASkill llava

LLaVA(大型语言和视觉助手)是一个开源的人工智能模型,用于图像理解和视觉语言对话。它结合了CLIP视觉编码器和语言模型,支持视觉问答、图像描述、多轮对话等任务,适用于构建视觉语言聊天机器人、文档理解等应用。关键词:视觉语言模型、图像理解、VQA视觉问答、开源AI、对话式AI、多模态学习、深度学习、计算机视觉、自然语言处理。

AI应用 0 次安装 0 次浏览 更新于 3/21/2026

name: llava description: 大型语言和视觉助手。支持视觉指令调整和基于图像的对话。结合CLIP视觉编码器与Vicuna/LLaMA语言模型。支持多轮图像聊天、视觉问答和指令跟随。用于视觉语言聊天机器人或图像理解任务。最适合对话式图像分析。 version: 1.0.0 author: Orchestra Research license: MIT tags: [LLaVA, 视觉语言, 多模态, 视觉问答, 图像聊天, CLIP, Vicuna, 对话式AI, 指令调整, VQA] dependencies: [transformers, torch, pillow]

LLaVA - 大型语言和视觉助手

开源视觉语言模型,用于对话式图像理解。

何时使用LLaVA

使用场景:

  • 构建视觉语言聊天机器人
  • 视觉问答(VQA)
  • 图像描述和字幕生成
  • 多轮图像对话
  • 视觉指令跟随
  • 带图像的文档理解

指标:

  • 23,000+ GitHub星标
  • 目标达到GPT-4V水平的能力
  • Apache 2.0许可证
  • 多种模型大小(7B-34B参数)

使用替代方案代替:

  • GPT-4V:最高质量,基于API
  • CLIP:简单的零样本分类
  • BLIP-2:仅适用于字幕生成
  • Flamingo:研究用途,非开源

快速开始

安装

# 克隆仓库
git clone https://github.com/haotian-liu/LLaVA
cd LLaVA

# 安装
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

# 加载模型
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)
)

# 加载图像
image = Image.open("image.jpg")
image_tensor = process_images([image], image_processor, model.config)
image_tensor = image_tensor.to(model.device, dtype=torch.float16)

# 创建对话
conv = conv_templates["llava_v1"].copy()
conv.append_message(conv.roles[0], DEFAULT_IMAGE_TOKEN + "
What is in this image?")
conv.append_message(conv.roles[1], None)
prompt = conv.get_prompt()

# 生成响应
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 最佳
# 加载不同模型
model_7b = "liuhaotian/llava-v1.5-7b"
model_13b = "liuhaotian/llava-v1.5-13b"
model_34b = "liuhaotian/llava-v1.6-34b"

# 4位量化以降低VRAM
load_4bit = True  # 减少VRAM约4倍

CLI用法

# 单图像查询
python -m llava.serve.cli \
    --model-path liuhaotian/llava-v1.5-7b \
    --image-file image.jpg \
    --query "What is in this image?"

# 多轮对话
python -m llava.serve.cli \
    --model-path liuhaotian/llava-v1.5-7b \
    --image-file image.jpg
# 然后交互式输入问题

Web UI(Gradio)

# 启动Gradio界面
python -m llava.serve.gradio_web_server \
    --model-path liuhaotian/llava-v1.5-7b \
    --load-4bit  # 可选:减少VRAM

# 访问 http://localhost:7860

多轮对话

# 初始化对话
conv = conv_templates["llava_v1"].copy()

# 第一轮
conv.append_message(conv.roles[0], DEFAULT_IMAGE_TOKEN + "
What is in this image?")
conv.append_message(conv.roles[1], None)
response1 = generate(conv, model, image)  # "一只狗在公园玩耍"

# 第二轮
conv.messages[-1][1] = response1  # 添加先前响应
conv.append_message(conv.roles[0], "狗的品种是什么?")
conv.append_message(conv.roles[1], None)
response2 = generate(conv, model, image)  # "金毛寻回犬"

# 第三轮
conv.messages[-1][1] = response2
conv.append_message(conv.roles[0], "是什么时间?")
conv.append_message(conv.roles[1], None)
response3 = generate(conv, model, image)

常见任务

图像字幕生成

question = "详细描述这张图像。"
response = ask(model, image, question)

视觉问答

question = "图像中有多少人?"
response = ask(model, image, question)

对象检测(文本形式)

question = "列出你在这张图像中能看到的所有对象。"
response = ask(model, image, question)

场景理解

question = "这个场景中正在发生什么?"
response = ask(model, image, question)

文档理解

question = "这份文档的主要主题是什么?"
response = ask(model, document_image, question)

训练自定义模型

# 阶段1:特征对齐(558K图像-字幕对)
bash scripts/v1_5/pretrain.sh

# 阶段2:视觉指令调整(150K指令数据)
bash scripts/v1_5/finetune.sh

量化(减少VRAM)

# 4位量化
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  # 减少VRAM约4倍
)

# 8位量化
load_8bit=True  # 减少VRAM约2倍

最佳实践

  1. 从7B模型开始 - 质量良好,VRAM可管理
  2. 使用4位量化 - 显著减少VRAM
  3. 需要GPU - CPU推理极慢
  4. 清晰的提示 - 具体问题获得更好答案
  5. 多轮对话 - 保持对话上下文
  6. 温度0.2-0.7 - 平衡创意/一致性
  7. max_new_tokens 512-1024 - 用于详细响应
  8. 批量处理 - 顺序处理多个图像

性能

模型 VRAM(FP16) VRAM(4位) 速度(令牌/秒)
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%

局限性

  1. 幻觉 - 可能描述图像中不存在的内容
  2. 空间推理 - 难以精确定位
  3. 小文本 - 难以阅读细小的文字
  4. 对象计数 - 对多个对象不精确
  5. VRAM要求 - 需要强大GPU
  6. 推理速度 - 比CLIP慢

与框架集成

LangChain

from langchain.llms.base import LLM

class LLaVALLM(LLM):
    def _call(self, prompt, stop=None):
        # 自定义LLaVA推理
        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聊天"
)
demo.launch()

资源