视频语言模型的推理流程与帧采样要点
视频语言模型在图像文本模型架构上扩展以接收视频帧序列,可处理视频问答与视频描述等任务。指南以指令微调的 LLaVA-OneVision 为例,说明处理器如何抽象视频处理并支持交错输入。
Copyright 2024 HuggingFace Team. 全权保留. 根据 Apache 许可, 版本 2.0 (“ License ”) 获得许可; 您不能使用此文件, 除非符合 您可在下列地点获得许可证副本: http://www.apache.org/licenses/LICENSE-2.0 除非适用法律要求或书面同意,否则根据许可证分发的软件在下列网址分发: a " :ASIS"BASIS,WITHOUTWARRANTIESORCONDITIONSOFANYKIND,或明示的或暗示的。 《许可证》规定的许可和限制的具体语言。 ⚠️ 注意此文件在 Markdown 中, 但包含我们doc- builder(类似于 MDX ) 的特定语法, 可能不是 在您的Markdown 查看器中正确渲染。 # 视频- text-to-text [[open-in-colab]] Video-text-to-text,又称Video语言模型,是能够处理视频和输出文本的模型,这些模型可以处理各种任务,从视频问题回答到视频字幕. 这些模型的建筑结构几乎与 [image-text-to-text](<https://github.com/huggingface/transformers/blob/HEAD/docs/source/en/tasks/image_text_to_text>) 模型除了接受视频数据的一些变化外,因为视频数据本质上是带有时间依赖性的图像帧. 一些image-text-to-text模型接受多个图像,但单靠这一点不足以让一个模型接受视频. 此外,video-text-to-text模型也经常接受所有视觉模式的培训,每个实例可能都有视频,多个视频,图像和多个图像,其中一些模型也可以进行互页输入,例如,您可以通过在文本中添加像"这段视频中发生了什么? `<video>`". 注意这些模型处理没有音频的视频. [任意对任意](<https://github.com/huggingface/transformers/blob/HEAD/docs/source/en/tasks/any_to_any>) 另一方面,模型可以处理带音频的视频。 在本指南中,我们简要地概述了视频LM,并展示了如何用Transformers进行推论. 首先,视频LM有多种类型: - 用于微调的基础模型 - 聊天的微调模式 - 指令精细调整模型 本指南侧重于以一个经指导调整的模型进行推论, [llava-hf/llava-onevision-qwen2-0.5b-ov-hf](https://huggingface.co/llava-hf/llava-interleave-qwen-7b-hf) 中可以接收互页数据。或者,您可以尝试 [llava-interleave-qwen-0 5b-hf (英语).](https://huggingface.co/llava-hf/llava-interleave-qwen-0.5b-hf) 如果你的硬件不允许运行一个 7B 模型。 让我们开始安装依赖。 ```bash pip install -q transformers accelerate flash_attn torchcodec ``` 让我们初始化模型和处理器。 ```python from transformers import AutoProcessor, LlavaOnevisionForConditionalGeneration import torch model_id = "llava-hf/llava-onevision-qwen2-0.5b-ov-hf" device = torch.accelerator.current_accelerator().type if torch.accelerator.is_available() else "cpu" processor = AutoProcessor.from_pretrained(model_id, device=device) model = LlavaForConditionalGeneration.from_pretrained(model_id, device_map="auto", dtype=torch.float16) ``` 我们可以推断出两段视频, 两者都有猫。 400 "控件" https://huggingface.co/spaces/merve/llava-interleave/resolve/main/cats_1.mp4" 类型='video/mp4'> </视频> 400 "控件" https://huggingface.co/spaces/merve/llava-interleave/resolve/main/cats_2.mp4" 类型='video/mp4'> </视频> 视频是一系列的图像帧,根据硬件限制,需要下取样,如果下取样帧的数量太少,预测的质量就会低. Video- text-to-text 模型有带视频处理器抽象化的处理器。 您可以将视频推断相关的参数传递给 [`~ProcessorMixin.apply_chat_template`] 函数。 > [!WARNING] > 你可以更多地了解视频处理器 [这儿](<https://github.com/huggingface/transformers/blob/HEAD/docs/source/en/main_classes/video_processor>). 我们可以定义我们的聊天历史,用下面的URL在视频中通过. ```python messages = [ { "role": "user", "content": [ {"type": "video", "video": "https://huggingface.co/spaces/merve/llava-interleave/resolve/main/cats_1.mp4"}, {"type": "text", "text": "Describe what is happening in this video."}, ], } ] ``` 您可以通过发送信件、 设置来预处理视频 `do_sample_frames` 到 True 然后经过这里 `num_frames`在这里,我们抽样10帧。 ```python inputs = processor.apply_chat_template( messages, tokenize=True, add_generation_prompt=True, return_dict=True, return_tensors="pt", num_frames=10, do_sample_frames=True ) inputs.to(model.device) ``` 投入包括: `input_ids` 用于标定文本, `pixel_values_videos` 用于 10 帧和 `attention_mask` 任何标志 我们现在可以用我们预先处理过的输入推断并解码它们。 ```python generated_ids = model.generate(**inputs, max_new_tokens=128) input_length = len(inputs["input_ids"][0]) output_text = processor.batch_decode( generated_ids[:, input_length:], skip_special_tokens=True, clean_up_tokenization_spaces=False ) output_text = processor.batch_decode( generated_ids, skip_special_tokens=True, clean_up_tokenization_spaces=False ) print(output_text[0]) #"The video features a fluffy, long-haired cat with a mix of brown and white fur, lying on a beige carpeted floor. The cat's eyes are wide open, and its whiskers are prominently visible. The cat appears to be in a relaxed state, with its head slightly" ``` 您也可以在下面的聊天模板中直接将多个视频与文本交互. ```python messages = [ { "role": "user", "content": [ {"type": "text", "text": "Here's a video."}, {"type": "video", "video": "https://huggingface.co/spaces/merve/llava-interleave/resolve/main/cats_1.mp4"}, {"type": "text", "text": "Here's another video."}, {"type": "video", "video": "https://huggingface.co/spaces/merve/llava-interleave/resolve/main/cats_2.mp4"}, {"type": "text", "text": "Describe similarities in these videos."}, ], } ] ``` 推论与前例无异. ```python inputs = processor.apply_chat_template( messages, tokenize=True, add_generation_prompt=True, return_dict=True, return_tensors="pt", num_frames=100, do_sample_frames=True ) inputs.to(model.device) generated_ids = model.generate(**inputs, max_new_tokens=50) input_length = len(inputs["input_ids"][0]) output_text = processor.batch_decode( generated_ids[:, input_length:], skip_special_tokens=True, clean_up_tokenization_spaces=False ) print(output_text) #['Both videos feature a cat with a similar appearance, characterized by a fluffy white coat with black markings, a pink nose, and a pink tongue. The cat\'s eyes are wide open, and it appears to be in a state of alertness or excitement. '] ```
查看原始来源