LongVU是由Meta AI团队推出的一种专注于长视频语言理解的多模态模型。
LongVU的架构设计包括使用DINOv2技术去除冗余帧,融合剩余帧的特征,通过跨模态查询选择性地减少视觉标记,根据时间依赖关系进行空间标记压缩,以进一步适应大型语言模型的有限上下文长度。
LongVU利用基于文本引导的跨模态查询来选择性地减少视频帧的特征,能保留与文本查询最相关的帧的详细信息,将其他帧减少到低分辨率的标记表示。
LongVU能有效处理1fps采样的视频输入,且能适应性地将每小时长视频的平均每个帧的标记数量减少到2个,适应8k上下文长度的多模态大型语言模型。
LongVU模型的出现为长视频的语言理解提供了一种有效的解决方案,通过减少视频标记的数量并保留视觉细节,LongVU不仅提高了视频处理的效率,还保持了视频内容的完整性。
github项目地址:https://github.com/Vision-CAIR/LongVU。
一、环境安装
1、python环境
建议安装python版本在3.10以上。
2、pip库安装
pip install -r requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple
3、LongVU_Qwen2_7B_img 模型下载:
git lfs install
git clone https://huggingface.co/Vision-CAIR/LongVU_Qwen2_7B_img
4、LongVU_Qwen2_7B 模型下载:
git lfs install
git clone https://huggingface.co/Vision-CAIR/LongVU_Qwen2_7B
二**、功能测试**
1、运行测试:
(1)python代码调用测试
import numpy as np
import torch
from longvu.builder import load_pretrained_model
from longvu.constants import DEFAULT_IMAGE_TOKEN, IMAGE_TOKEN_INDEX
from longvu.conversation import conv_templates, SeparatorStyle
from longvu.mm_datautils import KeywordsStoppingCriteria, process_images, tokenizer_image_token
from decord import cpu, VideoReader
def describe_video(video_path, model_path="./checkpoints/longvu_qwen", model_name="cambrian_qwen", query="Describe this video in detail"):
# Load pretrained model and tokenizer
tokenizer, model, image_processor, context_len = load_pretrained_model(model_path, None, model_name)
model.eval()
# Read and process the video
vr = VideoReader(video_path, ctx=cpu(0), num_threads=1)
fps = float(vr.get_avg_fps())
frame_indices = np.array([i for i in range(0, len(vr), round(fps))])
video = np.stack([vr[frame_index].asnumpy() for frame_index in frame_indices])
image_sizes = [video[0].shape[:2]]
video = process_images(video, image_processor, model.config)
video = [item.unsqueeze(0) for item in video]
# Prepare the query
qs = f"{DEFAULT_IMAGE_TOKEN}\n{query}"
conv = conv_templates["qwen"].copy()
conv.append_message(conv.roles[0], qs)
conv.append_message(conv.roles[1], None)
prompt = conv.get_prompt()
# Tokenize input
input_ids = tokenizer_image_token(prompt, tokenizer, IMAGE_TOKEN_INDEX, return_tensors="pt").unsqueeze(0).to(model.device)
stop_str = conv.sep if conv.sep_style != SeparatorStyle.TWO else conv.sep2
stopping_criteria = KeywordsStoppingCriteria([stop_str], tokenizer, input_ids)
# Generate description
with torch.inference_mode():
output_ids = model.generate(
input_ids,
images=video,
image_sizes=image_sizes,
do_sample=False,
temperature=0.2,
max_new_tokens=128,
use_cache=True,
stopping_criteria=[stopping_criteria],
)
# Decode the output
description = tokenizer.batch_decode(output_ids, skip_special_tokens=True)[0].strip()
return description
# Example usage
video_description = describe_video("./examples/video1.mp4")
print(video_description)
未完......
更多详细的欢迎关注:杰哥新技术