llava について
LLaVAは、対話形式で画像を理解するオープンソースの視覚言語モデルです。視覚的な質問応答や画像チャットを開発する人向けに、複数サイズのモデルと4ビット量子化を提供します。
主な機能
- 01画像キャプションと視覚的な質問応答
- 02複数ターンの画像会話
- 03視覚指示への追従
- 047Bから34Bまでのモデル
- 054ビット量子化
- 06コマンドラインとGradioのインターフェース
活用シーン
- 01視覚言語チャットボットの構築
- 02画像について質問して回答を得る
- 03画像の説明やキャプションの生成
- 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)
利用可能なモデル
| Model | Parameters | VRAM | Quality |
|---|---|---|---|
| LLaVA-v1.5-7B | 7B | ~14 GB | Good |
| LLaVA-v1.5-13B | 13B | ~28 GB | Better |
| LLaVA-v1.6-34B | 34B | ~70 GB | Best |
# 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 - 詳細な回答を得るため
- バッチ処理 - 複数の画像を順番に処理する
パフォーマンス
| Model | VRAM (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%
制限事項
- ハルシネーション - 画像に存在しないものを説明することがある
- 空間推論 - 正確な位置の把握が苦手
- 小さな文字 - 細かい文字の読み取りが難しい
- 物体の数え上げ - 多数の物体に対しては不正確
- 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
コメント
Markdown 対応