モデル概要
モデル特徴
モデル能力
使用事例
🚀 LLaVA-NeXT-Video モデルカード
Google Colabの無料枠でLlavaを実行するためのGoogle Colabデモもチェックしてください。
免責事項:LLaVA-NeXT-Videoをリリースしたチームはこのモデルのモデルカードを作成していないため、このモデルカードはHugging Faceチームによって作成されました。
📄 モデルの詳細
モデルの種類: LLaVA-Next-Videoは、マルチモーダルな命令従属性データでLLMをファインチューニングすることで学習されたオープンソースのチャットボットです。このモデルは、LLaVa-NeXTをベースに、ビデオと画像のデータの混合を使って調整され、より良いビデオ理解能力を実現しています。ビデオは、1クリップあたり32フレームに均一にサンプリングされています。 このモデルは、VideoMME benchのオープンソースモデルの中で現在SOTA(最先端)です。 ベースのLLM:lmsys/vicuna-7b-v1.5
モデルの作成日: LLaVA-Next-Video-7Bは2024年4月に学習されました。
詳細情報のための論文またはリソース:https://github.com/LLaVA-VL/LLaVA-NeXT
📚 学習データセット
画像
- LAION/CC/SBUからフィルタリングされた558Kの画像テキストペアで、BLIPによってキャプション付けされています。
- 158KのGPT生成マルチモーダル命令従属性データ。
- 500Kの学術タスク指向のVQAデータの混合。
- 50KのGPT-4Vデータの混合。
- 40KのShareGPTデータ。
ビデオ
- 100KのVideoChatGPT-Instruct。
📊 評価データセット
3つの学術VQAベンチマークと1つのキャプショニングベンチマークを含む4つのベンチマークのコレクション。
🚀 モデルの使い方
まず、transformers >= 4.42.0
がインストールされていることを確認してください。
このモデルは、マルチビジュアルおよびマルチプロンプト生成をサポートしています。つまり、プロンプトに複数の画像やビデオを渡すことができます。また、正しいプロンプトテンプレート (USER: xxx\nASSISTANT:
) を使用し、画像やビデオをクエリしたい場所にトークン <image>
または <video>
を追加するようにしてください。
以下は、GPUデバイスで float16
精度で生成を実行するためのサンプルスクリプトです。
import av
import torch
import numpy as np
from huggingface_hub import hf_hub_download
from transformers import LlavaNextVideoProcessor, LlavaNextVideoForConditionalGeneration
model_id = "llava-hf/LLaVA-NeXT-Video-7B-hf"
model = LlavaNextVideoForConditionalGeneration.from_pretrained(
model_id,
torch_dtype=torch.float16,
low_cpu_mem_usage=True,
).to(0)
processor = LlavaNextVideoProcessor.from_pretrained(model_id)
def read_video_pyav(container, indices):
'''
Decode the video with PyAV decoder.
Args:
container (`av.container.input.InputContainer`): PyAV container.
indices (`List[int]`): List of frame indices to decode.
Returns:
result (np.ndarray): np array of decoded frames of shape (num_frames, height, width, 3).
'''
frames = []
container.seek(0)
start_index = indices[0]
end_index = indices[-1]
for i, frame in enumerate(container.decode(video=0)):
if i > end_index:
break
if i >= start_index and i in indices:
frames.append(frame)
return np.stack([x.to_ndarray(format="rgb24") for x in frames])
# define a chat history and use `apply_chat_template` to get correctly formatted prompt
# Each value in "content" has to be a list of dicts with types ("text", "image", "video")
conversation = [
{
"role": "user",
"content": [
{"type": "text", "text": "Why is this video funny?"},
{"type": "video"},
],
},
]
prompt = processor.apply_chat_template(conversation, add_generation_prompt=True)
video_path = hf_hub_download(repo_id="raushan-testing-hf/videos-test", filename="sample_demo_1.mp4", repo_type="dataset")
container = av.open(video_path)
# sample uniformly 8 frames from the video, can sample more for longer videos
total_frames = container.streams.video[0].frames
indices = np.arange(0, total_frames, total_frames / 8).astype(int)
clip = read_video_pyav(container, indices)
inputs_video = processor(text=prompt, videos=clip, padding=True, return_tensors="pt").to(model.device)
output = model.generate(**inputs_video, max_new_tokens=100, do_sample=False)
print(processor.decode(output[0][2:], skip_special_tokens=True))
transformers>=v4.48からは、会話履歴に画像やビデオのURLまたはローカルパスを渡し、チャットテンプレートに残りの処理を任せることもできます。
ビデオの場合、ビデオからサンプリングする num_frames
を指定する必要もあります。指定しない場合、ビデオ全体がロードされます。
チャットテンプレートは、画像やビデオを自動的にロードし、torch.Tensor
形式の入力を返します。これを直接 model.generate()
に渡すことができます。
messages = [
{
"role": "user",
"content": [
{"type": "image", "url": "https://www.ilankelman.org/stopsigns/australia.jpg"},
{"type": "video", "path": "my_video.mp4"},
{"type": "text", "text": "What is shown in this image and video?"},
],
},
]
inputs = processor.apply_chat_template(messages, num_frames=8, add_generation_prompt=True, tokenize=True, return_dict=True, return_tensors="pt")
output = model.generate(**inputs, max_new_tokens=50)
画像を入力とした推論
上記のようにモデルをロードした後、以下のコードを使用して画像から生成を行うことができます。
import requests
from PIL import Image
conversation = [
{
"role": "user",
"content": [
{"type": "text", "text": "What are these?"},
{"type": "image"},
],
},
]
prompt = processor.apply_chat_template(conversation, add_generation_prompt=True)
image_file = "http://images.cocodataset.org/val2017/000000039769.jpg"
raw_image = Image.open(requests.get(image_file, stream=True).raw)
inputs_image = processor(text=prompt, images=raw_image, return_tensors='pt').to(0, torch.float16)
output = model.generate(**inputs_image, max_new_tokens=100, do_sample=False)
print(processor.decode(output[0][2:], skip_special_tokens=True))
画像とビデオを入力とした推論
上記のようにモデルをロードした後、以下のコードを使用して画像とビデオを同時に入力として生成を行うことができます。
conversation_1 = [
{
"role": "user",
"content": [
{"type": "text", "text": "What's the content of the image>"},
{"type": "image"},
],
}
]
conversation_2 = [
{
"role": "user",
"content": [
{"type": "text", "text": "Why is this video funny?"},
{"type": "video"},
],
},
]
prompt_1 = processor.apply_chat_template(conversation_1, add_generation_prompt=True)
prompt_2 = processor.apply_chat_template(conversation_2, add_generation_prompt=True)
inputs = processor(text=[prompt_1, prompt_2], images=image, videos=clip, padding=True, return_tensors="pt").to(model.device)
# Generate
generate_ids = model.generate(**inputs, max_new_tokens=100)
out = processor.batch_decode(generate_ids, skip_special_tokens=True, clean_up_tokenization_spaces=False)
print(out)
モデルの最適化
bitsandbytes
ライブラリを使用した4ビット量子化
まず、bitsandbytes
をインストールしてください (pip install bitsandbytes
)。また、CUDA互換のGPUデバイスにアクセスできることを確認してください。上記のコードスニペットを以下のように変更するだけです。
model = LlavaNextVideoForConditionalGeneration.from_pretrained(
model_id,
torch_dtype=torch.float16,
low_cpu_mem_usage=True,
+ load_in_4bit=True
)
Flash-Attention 2を使用して生成をさらに高速化する
まず、flash-attn
をインストールしてください。このパッケージのインストールについては、Flash Attentionのオリジナルリポジトリ を参照してください。上記のコードスニペットを以下のように変更するだけです。
model = LlavaNextVideoForConditionalGeneration.from_pretrained(
model_id,
torch_dtype=torch.float16,
low_cpu_mem_usage=True,
+ use_flash_attention_2=True
).to(0)
🔒 ライセンス
Llama 2は、LLAMA 2 Community Licenseの下でライセンスされています。 Copyright (c) Meta Platforms, Inc. All Rights Reserved.
✏️ 引用
もしあなたの研究で私たちの論文やコードが役立った場合、以下のように引用してください。
@misc{zhang2024llavanextvideo,
title={LLaVA-NeXT: A Strong Zero-shot Video Understanding Model},
url={https://llava-vl.github.io/blog/2024-04-30-llava-next-video/},
author={Zhang, Yuanhan and Li, Bo and Liu, haotian and Lee, Yong jae and Gui, Liangke and Fu, Di and Feng, Jiashi and Liu, Ziwei and Li, Chunyuan},
month={April},
year={2024}
}
@misc{liu2024llavanext,
title={LLaVA-NeXT: Improved reasoning, OCR, and world knowledge},
url={https://llava-vl.github.io/blog/2024-01-30-llava-next/},
author={Liu, Haotian and Li, Chunyuan and Li, Yuheng and Li, Bo and Zhang, Yuanhan and Shen, Sheng and Lee, Yong Jae},
month={January},
year={2024}
}



