Transformers documentation
Video-text-to-text
Video-text-to-text
Video-text-to-text、別名ビデオ言語モデルは、ビデオを処理してテキストを出力できるモデルです。これらのモデルは、ビデオ質問応答からビデオキャプション生成まで、さまざまなタスクに対応できます。
These models have nearly the same architecture as image-text-to-text models except for some changes to accept video data, since video data is essentially image frames with temporal dependencies. Some image-text-to-text models take in multiple images, but this alone is inadequate for a model to accept videos.
さらに、video-text-to-textモデルは、多くの場合、すべてのビジョンモダリティで学習されます。各サンプルには、動画、複数の動画、画像、複数の画像が含まれる可能性があります。これらのモデルの中には、交互に並んだ入力を受け付けるものもあります。例えば、テキスト文字列内に<video>のような動画トークンを追加することで、特定の動画を参照できます。例: 「この動画で何が起きているか?<video>」
これらのモデルは音声なしの動画を処理する点に注意してください。一方、Any-to-anyモデルは音声付きの動画を処理できます。
本ガイドでは、動画LMの概要を簡潔に紹介し、Transformers を用いた推論の使用方法を示します。
まず、動画LMには複数のタイプがあります。
- ファインチューニングに使用されるベースモデル
- 対話用のファインチューニング済みモデル
- 指示ファインチューニング済みモデル
このガイドでは、指示チューニング済みモデル、llava-hf/llava-onevision-qwen2-0.5b-ov-hf を用いた推論に焦点を当てます。このモデルは、インターリーブされたデータを処理できます。また、ハードウェアの制約により 7B モデルの実行が難しい場合は、llava-interleave-qwen-0.5b-hf を試すこともできます。
依存関係のインストールから始めましょう。
pip install -q transformers accelerate flash_attn torchcodec
モデルとプロセッサを初期化しましょう。
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)2つの動画で推論を実行します。どちらも猫が写っています。
動画は画像フレームの連続です。ハードウェアの制約に応じて、ダウンサンプリングが必要です。ダウンサンプリング後のフレーム数が少なすぎると、予測の品質が低下します。
Video-text-to-text モデルには、video processor が抽象化されたプロセッサが備わっています。apply_chat_template() 関数に、video 推論に関連する引数を渡すことができます。
こちらで、ビデオプロセッサについて詳しく学ぶことができます。
以下のように、URLを指定して動画を含むチャット履歴を定義できます。
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フレームをサンプリングします。
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、10フレーム分のpixel_values_videos、および対象となるトークンを示すattention_maskが含まれます。
これにより、前処理済みの入力を使って推論を実行し、デコードすることができます。
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"以下の例のように、チャットテンプレート内でテキストと複数の動画を交互に配置することもできます。
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."},
],
}
]推論は、前回の例と同じです。
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. ']