模型概述
模型特點
模型能力
使用案例
🚀 LLaVA-NeXT-Video模型卡片
LLaVA-NeXT-Video是一個開源的聊天機器人,通過在多模態指令跟隨數據上微調大語言模型(LLM)進行訓練。該模型基於LLaVa-NeXT,在視頻和圖像數據的混合集上進行微調,以實現更好的視頻理解能力。
點擊下面的鏈接,在免費的Google Colab實例上運行Llava:
免責聲明:發佈LLaVa-NeXT-Video的團隊並未為該模型撰寫模型卡片,此模型卡片由Hugging Face團隊編寫。
🚀 快速開始
首先,確保安裝 transformers >= 4.42.0
。該模型支持多視覺和多提示生成,這意味著你可以在提示中傳遞多個圖像或視頻。同時,請確保遵循正確的提示模板 (USER: xxx\nASSISTANT:
),並在需要查詢圖像或視頻的位置添加 <image>
或 <video>
標記。
以下是在GPU設備上以 float16
精度運行生成的示例腳本:
import av
import torch
from transformers import LlavaNextVideoProcessor, LlavaNextVideoForConditionalGeneration
model_id = "llava-hf/LLaVA-NeXT-Video-34B-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))
以圖像為輸入進行推理
在如上述加載模型後,使用以下代碼從圖像生成結果:
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_video, 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)
s = 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)
從 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)
✨ 主要特性
- 多模態支持:支持圖像和視頻輸入,能夠處理多視覺和多提示生成。
- 高性能:在VideoMME bench上達到了當前開源模型中的最優性能。
📦 安裝指南
確保安裝 transformers >= 4.42.0
,若要進行模型優化,還需安裝相應的庫:
- 4位量化:
pip install bitsandbytes
- 使用Flash-Attention 2:參考 Flash Attention原倉庫 進行安裝。
💻 使用示例
基礎用法
import av
import torch
from transformers import LlavaNextVideoProcessor, LlavaNextVideoForConditionalGeneration
model_id = "llava-hf/LLaVA-NeXT-Video-34B-hf"
model = LlavaNextVideoForConditionalGeneration.from_pretrained(
model_id,
torch_dtype=torch.float16,
low_cpu_mem_usage=True,
).to(0)
processor = LlavaNextVideoProcessor.from_pretrained(model_id)
# 後續代碼保持不變
高級用法
4位量化通過 bitsandbytes
庫
model = LlavaNextVideoForConditionalGeneration.from_pretrained(
model_id,
torch_dtype=torch.float16,
low_cpu_mem_usage=True,
+ load_in_4bit=True
)
使用Flash-Attention 2進一步加速生成
model = LlavaNextVideoForConditionalGeneration.from_pretrained(
model_id,
torch_dtype=torch.float16,
low_cpu_mem_usage=True,
+ use_flash_attention_2=True
).to(0)
📚 詳細文檔
📄 模型詳情
屬性 | 詳情 |
---|---|
模型類型 | LLaVA-Next-Video是一個開源的聊天機器人,通過在多模態指令跟隨數據上微調大語言模型(LLM)進行訓練。該模型基於LLaVa-NeXT,在視頻和圖像數據的混合集上進行微調,以實現更好的視頻理解能力。視頻以每片段32幀的方式均勻採樣。該模型在VideoMME bench上是當前開源模型中的最優模型。基礎大語言模型為 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。
📊 評估數據集
包含4個基準測試的集合,其中包括3個學術VQA基準測試和1個字幕生成基準測試。
🔧 技術細節
LLaVA-Next-Video基於基礎大語言模型 lmsys/vicuna-7b-v1.5,在多模態指令跟隨數據上進行微調。通過在視頻和圖像數據的混合集上進行訓練,模型能夠學習到視頻和圖像的特徵,從而實現更好的視頻理解能力。
📄 許可證
Llama 2遵循LLAMA 2社區許可證,版權所有 (c) Meta Platforms, Inc. 保留所有權利。
✏️ 引用
如果你在研究中發現我們的論文和代碼有用,請使用以下引用:
@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}
}



