许可证:其他
语言:
- 英文
基础模型:
- meta-llama/Meta-Llama-3.1-8B-Instruct
任务标签:视频文本转文本
推理:不支持
库名称:transformers
中文阅读
CogVLM2-Llama3-视频描述模型
代码 | 🤗 Hugging Face | 🤖 ModelScope
通常情况下,大多数视频数据并不附带相应的描述性文本,因此需要将视频数据转换为文本描述,为文本到视频模型提供必要的训练数据。CogVLM2-Caption是一个视频描述生成模型,用于为CogVideoX模型生成训练数据。
使用方法
import io
import argparse
import numpy as np
import torch
from decord import cpu, VideoReader, bridge
from transformers import AutoModelForCausalLM, AutoTokenizer
模型路径 = "THUDM/cogvlm2-llama3-caption"
设备 = 'cuda' if torch.cuda.is_available() else 'cpu'
TORCH类型 = torch.bfloat16 if torch.cuda.is_available() and torch.cuda.get_device_capability()[0] >= 8 else torch.float16
解析器 = argparse.ArgumentParser(description="CogVLM2-视频 命令行演示")
解析器.add_argument('--quant', type=int, choices=[4, 8], help='启用4位或8位精度加载', default=0)
参数 = 解析器.parse_args([])
def 加载视频(视频数据, 策略='chat'):
bridge.set_bridge('torch')
mp4流 = 视频数据
帧数 = 24
decord_vr = VideoReader(io.BytesIO(mp4流), ctx=cpu(0))
帧ID列表 = None
总帧数 = len(decord_vr)
if 策略 == 'base':
剪辑结束秒 = 60
剪辑开始秒 = 0
起始帧 = int(剪辑开始秒 * decord_vr.get_avg_fps())
结束帧 = min(总帧数,
int(剪辑结束秒 * decord_vr.get_avg_fps())) if 剪辑结束秒 is not None else 总帧数
帧ID列表 = np.linspace(起始帧, 结束帧 - 1, 帧数, dtype=int)
elif 策略 == 'chat':
时间戳 = decord_vr.get_frame_timestamp(np.arange(总帧数))
时间戳 = [i[0] for i in 时间戳]
最大秒数 = round(max(时间戳)) + 1
帧ID列表 = []
for 秒 in range(最大秒数):
最接近数 = min(时间戳, key=lambda x: abs(x - 秒))
索引 = 时间戳.index(最接近数)
帧ID列表.append(索引)
if len(帧ID列表) >= 帧数:
break
视频数据 = decord_vr.get_batch(帧ID列表)
视频数据 = 视频数据.permute(3, 0, 1, 2)
return 视频数据
分词器 = AutoTokenizer.from_pretrained(
模型路径,
trust_remote_code=True,
)
模型 = AutoModelForCausalLM.from_pretrained(
模型路径,
torch_dtype=TORCH类型,
trust_remote_code=True
).eval().to(设备)
def 预测(提示, 视频数据, 温度):
策略 = 'chat'
视频 = 加载视频(视频数据, 策略=策略)
历史 = []
查询 = 提示
输入 = 模型.build_conversation_input_ids(
tokenizer=分词器,
query=查询,
images=[视频],
history=历史,
template_version=策略
)
输入 = {
'input_ids': 输入['input_ids'].unsqueeze(0).to('cuda'),
'token_type_ids': 输入['token_type_ids'].unsqueeze(0).to('cuda'),
'attention_mask': 输入['attention_mask'].unsqueeze(0).to('cuda'),
'images': [[输入['images'][0].to('cuda').to(TORCH类型)]],
}
生成参数 = {
"max_new_tokens": 2048,
"pad_token_id": 128002,
"top_k": 1,
"do_sample": False,
"top_p": 0.1,
"temperature": 温度,
}
with torch.no_grad():
输出 = 模型.generate(**输入, **生成参数)
输出 = 输出[:, 输入['input_ids'].shape[1]:]
响应 = 分词器.decode(输出[0], skip_special_tokens=True)
return 响应
def 测试():
提示 = "请详细描述这个视频内容。"
温度 = 0.1
视频数据 = open('test.mp4', 'rb').read()
响应 = 预测(提示, 视频数据, 温度)
print(响应)
if __name__ == '__main__':
测试()
许可证
本模型根据CogVLM2 许可证发布。对于基于Meta Llama 3构建的模型,请同时遵守LLAMA3许可证。
引用
🌟 如果您觉得我们的工作有帮助,请为我们点赞并引用我们的论文。
@article{yang2024cogvideox,
title={CogVideoX: 基于专家Transformer的文本到视频扩散模型},
author={杨卓毅、滕佳言、郑文迪、丁铭、黄诗雨、徐嘉政、杨元明、洪文怡、张笑涵、冯冠宇等},
journal={arXiv预印本 arXiv:2408.06072},
year={2024}
}