Transformers 文档
视频到文本到文本
并获得增强的文档体验
开始使用
视频到文本到文本
视频到文本到文本模型,也称为视频语言模型或具有视频输入的视觉语言模型,是接收视频输入的语言模型。这些模型可以处理各种任务,从视频问答到视频字幕。
这些模型与图像到文本到文本模型具有几乎相同的架构,除了为了接受视频数据而进行了一些更改,因为视频数据本质上是具有时间依赖性的图像帧。一些图像到文本到文本模型接受多张图像,但这本身不足以让模型接受视频。此外,视频到文本到文本模型通常使用所有视觉模态进行训练。每个示例可能包含视频、多个视频、图像和多张图像。其中一些模型还可以接受交错输入。例如,您可以通过在文本中添加视频标记来引用文本字符串中的特定视频,例如“这个视频中发生了什么?<video>
”。
在本指南中,我们将简要概述视频 LM,并展示如何使用 Transformers 进行推理。
首先,视频 LM 有多种类型
- 用于微调的基础模型
- 用于对话的聊天微调模型
- 指令微调模型
本指南重点介绍使用指令微调模型 llava-hf/llava-interleave-qwen-7b-hf 进行推理,该模型可以接收交错数据。或者,如果您的硬件不允许运行 7B 模型,您可以尝试 llava-interleave-qwen-0.5b-hf。
让我们开始安装依赖项。
pip install -q transformers accelerate flash_attn
让我们初始化模型和处理器。
from transformers import LlavaProcessor, LlavaForConditionalGeneration
import torch
model_id = "llava-hf/llava-interleave-qwen-0.5b-hf"
processor = LlavaProcessor.from_pretrained(model_id)
model = LlavaForConditionalGeneration.from_pretrained(model_id, torch_dtype=torch.float16)
model.to("cuda") # can also be xpu, mps, npu etc. depending on your hardware accelerator
有些模型直接使用 <video>
标记,而另一些模型则接受与采样帧数相等的 <image>
标记。该模型以后者的方式处理视频。我们将编写一个简单的实用程序来处理图像标记,另一个实用程序从 URL 获取视频并从中采样帧。
import uuid
import requests
import cv2
from PIL import Image
def replace_video_with_images(text, frames):
return text.replace("<video>", "<image>" * frames)
def sample_frames(url, num_frames):
response = requests.get(url)
path_id = str(uuid.uuid4())
path = f"./{path_id}.mp4"
with open(path, "wb") as f:
f.write(response.content)
video = cv2.VideoCapture(path)
total_frames = int(video.get(cv2.CAP_PROP_FRAME_COUNT))
interval = total_frames // num_frames
frames = []
for i in range(total_frames):
ret, frame = video.read()
pil_img = Image.fromarray(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB))
if not ret:
continue
if i % interval == 0:
frames.append(pil_img)
video.release()
return frames[:num_frames]
让我们获取输入。我们将采样帧并将其连接起来。
video_1 = "https://huggingface.co/spaces/merve/llava-interleave/resolve/main/cats_1.mp4"
video_2 = "https://huggingface.co/spaces/merve/llava-interleave/resolve/main/cats_2.mp4"
video_1 = sample_frames(video_1, 6)
video_2 = sample_frames(video_2, 6)
videos = video_1 + video_2
videos
# [<PIL.Image.Image image mode=RGB size=1920x1080>,
# <PIL.Image.Image image mode=RGB size=1920x1080>,
# <PIL.Image.Image image mode=RGB size=1920x1080>, ...]
两个视频中都有猫。
现在我们可以预处理输入了。
该模型具有如下所示的提示模板。首先,我们将所有采样帧放入一个列表中。由于每个视频有八帧,我们将在提示中插入 12 个 <image>
标记。在提示末尾添加 assistant
以触发模型给出答案。然后我们可以进行预处理。
user_prompt = "Are these two cats in these two videos doing the same thing?"
toks = "<image>" * 12
prompt = "<|im_start|>user"+ toks + f"\n{user_prompt}<|im_end|><|im_start|>assistant"
inputs = processor(text=prompt, images=videos, return_tensors="pt").to(model.device, model.dtype)
我们现在可以调用 generate() 进行推理。模型会输出我们输入中的问题和答案,因此我们只从模型输出中获取提示和 assistant
部分之后的文本。
output = model.generate(**inputs, max_new_tokens=100, do_sample=False)
print(processor.decode(output[0][2:], skip_special_tokens=True)[len(user_prompt)+10:])
# The first cat is shown in a relaxed state, with its eyes closed and a content expression, while the second cat is shown in a more active state, with its mouth open wide, possibly in a yawn or a vocalization.
瞧!
要了解有关视频到文本到文本模型的聊天模板和标记流的更多信息,请参阅图像到文本到文本任务指南,因为这些模型的工作方式类似。
< > 在 GitHub 上更新