mcpField
L
llava

llava

github.com/orchestra-research開発14.1k948 回インストール19 週間前に更新
リポジトリ

llava について

LLaVAは、対話形式で画像を理解するオープンソースの視覚言語モデルです。視覚的な質問応答や画像チャットを開発する人向けに、複数サイズのモデルと4ビット量子化を提供します。

主な機能

  1. 01画像キャプションと視覚的な質問応答
  2. 02複数ターンの画像会話
  3. 03視覚指示への追従
  4. 047Bから34Bまでのモデル
  5. 054ビット量子化
  6. 06コマンドラインとGradioのインターフェース

活用シーン

  1. 01視覚言語チャットボットの構築
  2. 02画像について質問して回答を得る
  3. 03画像の説明やキャプションの生成
  4. 04画像形式の文書の理解

インストール

コマンドをコピーしてターミナルで実行します。

$npx skills add https://github.com/orchestra-research/ai-research-skills --skill llava

インストール後、Claude Code で/llavaを入力して読み込まれたか確認します。

同梱ファイルreferences/training.md

詳細

LLaVA - Large Language and Vision Assistant

会話形式の画像理解に対応した、オープンソースのビジョン言語モデルです。

LLaVA を使用するタイミング

使用する場合:

  • ビジョン言語チャットボットを構築するとき
  • 視覚的質問応答(VQA)
  • 画像の説明とキャプション生成
  • 複数ターンにわたる画像についての会話
  • 視覚的な指示への追従
  • 画像を含むドキュメントの理解

指標:

  • 23,000+ GitHub スター
  • 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)

利用可能なモデル

ModelParametersVRAMQuality
LLaVA-v1.5-7B7B~14 GBGood
LLaVA-v1.5-13B13B~28 GBBetter
LLaVA-v1.6-34B34B~70 GBBest
# 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×

ベストプラクティス

  1. 7B モデルから始める - 品質が良く、VRAM も扱いやすい
  2. 4-bit 量子化を使用する - VRAM を大幅に削減できる
  3. GPU が必要 - CPU での推論は極端に遅い
  4. 明確なプロンプト - 具体的な質問ほど良い回答が得られる
  5. 複数ターンの会話 - 会話のコンテキストを維持する
  6. Temperature 0.2〜0.7 - 創造性と一貫性のバランスを取る
  7. max_new_tokens 512〜1024 - 詳細な回答を得るため
  8. バッチ処理 - 複数の画像を順番に処理する

パフォーマンス

ModelVRAM (FP16)VRAM (4-bit)Speed (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%

制限事項

  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):
        # 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()

リソース

コメント