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 上更新