Views
No views yet
1from oryx.model.builder import load_pretrained_model
2from oryx.mm_utils import get_model_name_from_path, process_images, tokenizer_image_token
3from oryx.constants import IMAGE_TOKEN_INDEX, DEFAULT_IMAGE_TOKEN, DEFAULT_IM_START_TOKEN, DEFAULT_IM_END_TOKEN, IGNORE_INDEX
4from oryx.conversation import conv_templates, SeparatorStyle
5from PIL import Image
6import requests
7import copy
8import torch
9import sys
10import warnings
11from decord import VideoReader, cpu
12import numpy as np
13
14def load_video(self, video_path, max_frames_num,fps=1,force_sample=False):
15 if max_frames_num == 0:
16 return np.zeros((1, 336, 336, 3))
17 vr = VideoReader(video_path, ctx=cpu(0),num_threads=1)
18 total_frame_num = len(vr)
19 video_time = total_frame_num / vr.get_avg_fps()
20 fps = round(vr.get_avg_fps()/fps)
21 frame_idx = [i for i in range(0, len(vr), fps)]
22 frame_time = [i/fps for i in frame_idx]
23 if len(frame_idx) > max_frames_num or force_sample:
24 sample_fps = max_frames_num
25 uniform_sampled_frames = np.linspace(0, total_frame_num - 1, sample_fps, dtype=int)
26 frame_idx = uniform_sampled_frames.tolist()
27 frame_time = [i/vr.get_avg_fps() for i in frame_idx]
28 frame_time = ",".join([f"{i:.2f}s" for i in frame_time])
29 spare_frames = vr.get_batch(frame_idx).asnumpy()
30 # import pdb;pdb.set_trace()
31 return spare_frames,frame_time,video_time
32pretrained = "THUdyh/Oryx-7B"
33model_name = "oryx_qwen"
34device = "cuda"
35device_map = "auto"
36tokenizer, model, image_processor, max_length = load_pretrained_model(pretrained, None, model_name, device_map=device_map)
37model.eval()
38video_path = ""
39max_frames_num = "64"
40video,frame_time,video_time = load_video(video_path, max_frames_num, 1, force_sample=True)
41video = image_processor.preprocess(video, return_tensors="pt")["pixel_values"].cuda().bfloat16()
42video = [video]
43video_data = (video, video)
44input_data = (video_data, (384, 384), "video")
45conv_template = "qwen_1_5"
46question = DEFAULT_IMAGE_TOKEN + "\nPlease describe this video in detail."
47conv = copy.deepcopy(conv_templates[conv_template])
48conv.append_message(conv.roles[0], question)
49conv.append_message(conv.roles[1], None)
50prompt_question = conv.get_prompt()
51input_ids = tokenizer_image_token(prompt_question, tokenizer, IMAGE_TOKEN_INDEX, return_tensors="pt").unsqueeze(0).to(device)
52output_ids = model.generate(
53 inputs=input_ids,
54 images=input_data[0][0],
55 images_highres=input_data[0][1],
56 modalities=video_data[2],
57 do_sample=False,
58 temperature=0,
59 max_new_tokens=128,
60 use_cache=True,
61)
62
63text_outputs = tokenizer.batch_decode(cont, skip_special_tokens=True)
64print(text_outputs)



1@article{liu2024oryx,
2title={Oryx MLLM: On-Demand Spatial-Temporal Understanding at Arbitrary Resolution},
3author={Liu, Zuyan and Dong, Yuhao and Liu, Ziwei and Hu, Winston and Lu, Jiwen and Rao, Yongming},
4journal={arXiv preprint arXiv:2409.12961},
5year={2024}
6}