Views
No views yet
1import torch
2import os
3import numpy as np
4from decord import VideoReader, cpu
5
6from llava.constants import IMAGE_TOKEN_INDEX, DEFAULT_IMAGE_TOKEN, DEFAULT_IM_START_TOKEN, DEFAULT_IM_END_TOKEN
7from llava.conversation import conv_templates, SeparatorStyle
8from llava.model.builder import load_pretrained_model
9from llava.mm_utils import tokenizer_image_token, get_model_name_from_path
10from llava.utils import disable_torch_init
11
12def load_video(video_path, max_frames_num):
13 vr = VideoReader(video_path, num_threads=4)
14 fps = round(vr.get_avg_fps())
15 frame_idx = [i for i in range(0, len(vr), fps)]
16
17 uniform_sampled_frames = np.linspace(0, len(vr) - 1, max_frames_num, dtype=int)
18 frame_idx = uniform_sampled_frames.tolist()
19 spare_frames = vr.get_batch(frame_idx).asnumpy()
20
21 return spare_frames
22
23# Model
24# Ensure you have cloned the code repository: git clone https://github.com/SHI-Labs/Slow-Fast-Video-Multimodal-LLM.git
25model_path = "shi-labs/slowfast-video-mllm-qwen2-7b-convnext-576-frame64-s1t4" # Or other checkpoint
26video_path = "Slow-Fast-Video-Multimodal-LLM/assets/catinterrupt.mp4" # Example video path from cloned repo
27question = "Please describe this video in detail."
28max_frames=64 # Set according to the specific checkpoint
29
30disable_torch_init()
31model_path = os.path.expanduser(model_path)
32model_name = get_model_name_from_path(model_path)
33# Make sure to pass trust_remote_code=True
34tokenizer, model, image_processor, context_len = load_pretrained_model(model_path, None, model_name, use_flash_attn=True, trust_remote_code=True)
35
36if model.config.mm_use_im_start_end:
37 prompt = DEFAULT_IM_START_TOKEN + DEFAULT_IMAGE_TOKEN + DEFAULT_IM_END_TOKEN + "
38" + question
39else:
40 prompt = DEFAULT_IMAGE_TOKEN + "
41" + question
42
43conv = conv_templates["qwen_1_5"].copy()
44conv.append_message(conv.roles[0], prompt)
45conv.append_message(conv.roles[1], None)
46prompt = conv.get_prompt()
47
48# read and process video
49video = load_video(video_path, max_frames_num=max_frames)
50video_tensor = image_processor.preprocess(video, return_tensors="pt")["pixel_values"].half().cuda()
51videos = [video_tensor]
52
53input_ids = tokenizer_image_token(prompt, tokenizer, IMAGE_TOKEN_INDEX, return_tensors='pt')
54input_ids = input_ids.to(device='cuda', non_blocking=True).unsqueeze(dim=0)
55
56with torch.inference_mode():
57 output_ids = model.generate(
58 input_ids,
59 images=videos,
60 do_sample=True,
61 max_new_tokens=1024,
62 num_beams=1,
63 temperature=0.2,
64 top_p=1.0,
65 use_cache=True)
66
67outputs = tokenizer.batch_decode(output_ids, skip_special_tokens=True)[0].strip()
68print(f"User input: {question}
69")
70print(outputs)1@misc{wang2025slowfast,
2 title={Slow-Fast Architecture for Video Multi-Modal Large Language Models},
3 author={Haotian Wang and Zhengyuan Yang and Yue Zhao and Bin Lin and Zhe Chen and Yue Cao and Hongxia Yang},
4 year={2025},
5 eprint={2504.01328},\
6 archivePrefix={arXiv},
7 primaryClass={cs.CV},
8 url={https://arxiv.org/abs/2504.01328v1},
9}