Safetensors
llava
k-m-irfan commited on
Commit
cd071c7
·
verified ·
1 Parent(s): d48ba4d

Upload folder using huggingface_hub

Browse files
Files changed (1) hide show
  1. README.md +108 -3
README.md CHANGED
@@ -1,3 +1,108 @@
1
- ---
2
- license: cc-by-sa-4.0
3
- ---
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ license: cc-by-sa-4.0
3
+ ---
4
+
5
+ # Model Card: VIMUL
6
+
7
+ ## Requires
8
+
9
+ ```bash
10
+ git clone https://github.com/LLaVA-VL/LLaVA-NeXT.git
11
+ pip install LLaVA-NeXT
12
+ ```
13
+
14
+ ## Inference
15
+
16
+ Example video inference:
17
+
18
+ ```python
19
+ import torch
20
+ import numpy as np
21
+ from llava.model.builder import load_pretrained_model
22
+ from llava.mm_utils import process_anyres_image, tokenizer_image_token, get_model_name_from_path, KeywordsStoppingCriteria
23
+ from llava.constants import IMAGE_TOKEN_INDEX, DEFAULT_IMAGE_TOKEN, DEFAULT_IM_START_TOKEN, DEFAULT_IM_END_TOKEN
24
+ from llava.conversation import conv_templates, SeparatorStyle
25
+ from transformers import AutoConfig
26
+ from decord import VideoReader, cpu
27
+
28
+ def load_video(video_path, num_frames=32, force_sample=False):
29
+ vr = VideoReader(video_path, ctx=cpu(0), num_threads=1)
30
+ total_frame_num = len(vr)
31
+ fps = round(vr.get_avg_fps())
32
+ frame_idx = [i for i in range(0, len(vr), fps)]
33
+ if len(frame_idx) > num_frames or force_sample:
34
+ uniform_sampled_frames = np.linspace(0, total_frame_num - 1, num_frames, dtype=int)
35
+ frame_idx = uniform_sampled_frames.tolist()
36
+ frames = vr.get_batch(frame_idx).asnumpy()
37
+ return frames
38
+
39
+ def infer(
40
+ model_path,
41
+ video_path,
42
+ prompt,
43
+ model_base=None,
44
+ conv_mode=None,
45
+ num_frames=32,
46
+ force_sample=False,
47
+ load_8bit=False,
48
+ device="cuda"
49
+ ):
50
+ model_name = get_model_name_from_path(model_path)+"llava_qwen"
51
+ tokenizer, model, image_processor, context_len = load_pretrained_model(
52
+ model_path, model_base, model_name, load_8bit=load_8bit
53
+ )
54
+ frames = load_video(video_path, num_frames=num_frames, force_sample=force_sample)
55
+ video = image_processor.preprocess(frames, return_tensors="pt")["pixel_values"].half().to(device)
56
+ video = [video]
57
+
58
+ qs = DEFAULT_IMAGE_TOKEN + "\n" + prompt
59
+ conv = conv_templates[conv_mode].copy() if conv_mode else conv_templates["default"].copy()
60
+ conv.append_message(conv.roles[0], qs)
61
+ conv.append_message(conv.roles[1], None)
62
+ prompt_str = conv.get_prompt()
63
+
64
+ input_ids = tokenizer_image_token(prompt_str, tokenizer, IMAGE_TOKEN_INDEX, return_tensors="pt").unsqueeze(0).to(device)
65
+ if tokenizer.pad_token_id is None:
66
+ tokenizer.pad_token_id = tokenizer.eos_token_id
67
+
68
+ attention_masks = input_ids.ne(tokenizer.pad_token_id).long().to(device)
69
+ stop_str = conv.sep if conv.sep_style != SeparatorStyle.TWO else conv.sep2
70
+ keywords = [stop_str]
71
+ stopping_criteria = KeywordsStoppingCriteria(keywords, tokenizer, input_ids)
72
+
73
+ with torch.inference_mode():
74
+ output_ids = model.generate(
75
+ inputs=input_ids,
76
+ images=video,
77
+ attention_mask=attention_masks,
78
+ modalities="video",
79
+ do_sample=False,
80
+ temperature=0.0,
81
+ max_new_tokens=1024,
82
+ top_p=0.1,
83
+ num_beams=1,
84
+ use_cache=True,
85
+ stopping_criteria=[stopping_criteria]
86
+ )
87
+ outputs = tokenizer.batch_decode(output_ids, skip_special_tokens=True)[0].strip()
88
+ if outputs.endswith(stop_str):
89
+ outputs = outputs[:-len(stop_str)]
90
+ return outputs.strip()
91
+
92
+ if __name__ == "__main__":
93
+ model_path = "MBZUAI/ViMUL"
94
+ video_path = "LLaVA-NeXT/playground/demo/xU25MMA2N4aVtYay.mp4"
95
+ prompt = "Describe what happens in the video."
96
+ conv_mode = "qwen_1_5"
97
+ output = infer(model_path, video_path, prompt, conv_mode=conv_mode)
98
+ print("\n")
99
+ print("="*40)
100
+ print("Output:", output)
101
+ print("="*40)
102
+ ```
103
+
104
+ ## Citation
105
+
106
+ ```
107
+
108
+ ```