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 License
- 複数のモデルサイズ(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 ビット量子化を使用する - VRAM を大幅に削減できます
- GPU が必要です - CPU での推論は非常に遅くなります
- 明確なプロンプトを使う - 具体的な質問ほど、より良い回答が得られます
- 複数ターンの対話を使う - 対話のコンテキストを維持できます
- Temperature は 0.2~0.7 - 創造性と一貫性のバランスを取ります
- max_new_tokens は 512~1024 - 詳細な回答に適しています
- バッチ処理 - 複数の画像を順番に処理します
パフォーマンス
| モデル | 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%
制限事項
- 幻覚 - 画像にないものを説明する場合があります
- 空間推論 - 正確な位置の特定は苦手です
- 小さな文字 - 細かい文字を読むのは困難です
- 物体の数え上げ - 物体が多い場合、正確さに欠けます
- 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
エージェントの運用手順
- 環境を確認します。 GPU メモリ、モデルのチェックポイントとライセンス、フレームワークのバージョン、入力形式を確認します。
- 入力を明確にします。 データまたはユーザーから、形式、識別子、パラメーターを確認します。結果に影響する値を推測せず、質問してください。
- まず小規模なバージョンを実行します。 最初に、単一のサンプルを低解像度または短い長さで推論します。
- 上記の手順と参考情報を使用して、タスク全体を実行します。
- 結果を検証します。 出力を定性的に確認し、タスクの指標で評価します。生成コンテンツのライセンス制限も確認します。
- 報告します。 実行した内容(バージョン、コマンド、パラメーター)、確認した内容、未確定の点を伝えます。
| このような場合 | 対応方法 |
|---|---|
| 大きな入力でメモリ不足が発生する | 解像度またはシーケンス長を下げる、半精度を使用する、または入力をタイルに分割します。 |
| インストール済みのバージョンに、この手順に記載された関数、フラグ、エンドポイントがない | インストール済みバージョンのドキュメント(help()、--help、公式ドキュメント)を確認して対応し、ユーザーに伝えます。API を捏造してはいけません。 |
| 必須の入力、識別子、パラメーターが曖昧である | ユーザーに質問するか、実行前に仮定を明示します。 |
誠実性に関するルール
- 結果、パラメーター、識別子、引用、統計情報を捏造してはいけません。実行または検証できない場合は、そのことを明確に伝えてください。
- 商用利用または公開利用の前に、モデルとデータセットのライセンスを確認します。
- ここに記載されているバージョン固有の情報は古くなっている可能性があるため、インストール済みバージョンの公式ドキュメントで確認します。
- 費用が発生する操作、共有 GPU やクラウドのクォータを消費する操作、個人データや患者データを扱う操作、取り消せない操作の前には、ユーザーに確認します。
関連スキル
clip:視覚と言語をつなぐ OpenAI のモデルです。blip-2-vision-language:HuggingFace Trans… を通じて、Salesforce BLIP-2(凍結された画像エンコーダーと OPT や FlanT5 などの LLM をつなぐ Q-Former)を使用する方法を説明します。fine-tuning-with-trl:TRL を使用して LLM を強化学習でファインチューニングします。指示チューニングには SFT、選好アライメントには DPO、報酬最適化には PPO/GRPO を使用します。
コメント
Markdown 対応