Commit
•
f5d0f75
1
Parent(s):
f96eaab
Update README.md
Browse files
README.md
CHANGED
@@ -39,3 +39,102 @@ tags:
|
|
39 |
| [VideoLLaMA2-7B-16F](https://huggingface.co/DAMO-NLP-SG/VideoLLaMA2-7B-16F) | Chat | [clip-vit-large-patch14-336](https://huggingface.co/openai/clip-vit-large-patch14-336) | [Mistral-7B-Instruct-v0.2](https://huggingface.co/mistralai/Mistral-7B-Instruct-v0.2) | 16 |
|
40 |
|
41 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
39 |
| [VideoLLaMA2-7B-16F](https://huggingface.co/DAMO-NLP-SG/VideoLLaMA2-7B-16F) | Chat | [clip-vit-large-patch14-336](https://huggingface.co/openai/clip-vit-large-patch14-336) | [Mistral-7B-Instruct-v0.2](https://huggingface.co/mistralai/Mistral-7B-Instruct-v0.2) | 16 |
|
40 |
|
41 |
|
42 |
+
## 🤖 Inference with VideoLLaMA2
|
43 |
+
```python
|
44 |
+
import torch
|
45 |
+
import transformers
|
46 |
+
|
47 |
+
import sys
|
48 |
+
sys.path.append('./')
|
49 |
+
from videollama2.conversation import conv_templates, SeparatorStyle
|
50 |
+
from videollama2.constants import DEFAULT_MMODAL_TOKEN, MMODAL_TOKEN_INDEX
|
51 |
+
from videollama2.mm_utils import get_model_name_from_path, tokenizer_MMODAL_token, KeywordsStoppingCriteria, process_video, process_image
|
52 |
+
from videollama2.model.builder import load_pretrained_model
|
53 |
+
|
54 |
+
|
55 |
+
def inference():
|
56 |
+
# Video Inference
|
57 |
+
paths = ['assets/cat_and_chicken.mp4']
|
58 |
+
questions = ['What animals are in the video, what are they doing, and how does the video feel?']
|
59 |
+
# Reply:
|
60 |
+
# The video features a kitten and a baby chick playing together. The kitten is seen laying on the floor while the baby chick hops around. The two animals interact playfully with each other, and the video has a cute and heartwarming feel to it.
|
61 |
+
modal_list = ['video']
|
62 |
+
|
63 |
+
# Video Inference
|
64 |
+
paths = ['assets/sora.mp4']
|
65 |
+
questions = ['Please describe this video.']
|
66 |
+
# Reply:
|
67 |
+
# The video features a series of colorful kites flying in the sky. The kites are first seen flying over trees, and then they are shown flying in the sky. The kites come in various shapes and colors, including red, green, blue, and yellow. The video captures the kites soaring gracefully through the air, with some kites flying higher than others. The sky is clear and blue, and the trees below are lush and green. The kites are the main focus of the video, and their vibrant colors and intricate designs are highlighted against the backdrop of the sky and trees. Overall, the video showcases the beauty and artistry of kite-flying, and it is a delight to watch the kites dance and glide through the air.
|
68 |
+
modal_list = ['video']
|
69 |
+
|
70 |
+
# Image Inference
|
71 |
+
paths = ['assets/sora.png']
|
72 |
+
questions = ['What is the woman wearing, what is she doing, and how does the image feel?']
|
73 |
+
# Reply:
|
74 |
+
# The woman in the image is wearing a black coat and sunglasses, and she is walking down a rain-soaked city street. The image feels vibrant and lively, with the bright city lights reflecting off the wet pavement, creating a visually appealing atmosphere. The woman's presence adds a sense of style and confidence to the scene, as she navigates the bustling urban environment.
|
75 |
+
modal_list = ['image']
|
76 |
+
|
77 |
+
# 1. Initialize the model.
|
78 |
+
model_path = 'DAMO-NLP-SG/VideoLLaMA2-7B'
|
79 |
+
model_name = get_model_name_from_path(model_path)
|
80 |
+
tokenizer, model, processor, context_len = load_pretrained_model(model_path, None, model_name)
|
81 |
+
model = model.to('cuda:0')
|
82 |
+
conv_mode = 'llama_2'
|
83 |
+
|
84 |
+
# 2. Visual preprocess (load & transform image or video).
|
85 |
+
if modal_list[0] == 'video':
|
86 |
+
tensor = process_video(paths[0], processor, model.config.image_aspect_ratio).to(dtype=torch.float16, device='cuda', non_blocking=True)
|
87 |
+
default_mm_token = DEFAULT_MMODAL_TOKEN["VIDEO"]
|
88 |
+
modal_token_index = MMODAL_TOKEN_INDEX["VIDEO"]
|
89 |
+
else:
|
90 |
+
tensor = process_image(paths[0], processor, model.config.image_aspect_ratio)[0].to(dtype=torch.float16, device='cuda', non_blocking=True)
|
91 |
+
default_mm_token = DEFAULT_MMODAL_TOKEN["IMAGE"]
|
92 |
+
modal_token_index = MMODAL_TOKEN_INDEX["IMAGE"]
|
93 |
+
tensor = [tensor]
|
94 |
+
|
95 |
+
# 3. Text preprocess (tag process & generate prompt).
|
96 |
+
question = default_mm_token + "\n" + questions[0]
|
97 |
+
conv = conv_templates[conv_mode].copy()
|
98 |
+
conv.append_message(conv.roles[0], question)
|
99 |
+
conv.append_message(conv.roles[1], None)
|
100 |
+
prompt = conv.get_prompt()
|
101 |
+
input_ids = tokenizer_MMODAL_token(prompt, tokenizer, modal_token_index, return_tensors='pt').unsqueeze(0).to('cuda:0')
|
102 |
+
|
103 |
+
# 4. Generate a response according to visual signals and prompts.
|
104 |
+
stop_str = conv.sep if conv.sep_style in [SeparatorStyle.SINGLE] else conv.sep2
|
105 |
+
# keywords = ["<s>", "</s>"]
|
106 |
+
keywords = [stop_str]
|
107 |
+
stopping_criteria = KeywordsStoppingCriteria(keywords, tokenizer, input_ids)
|
108 |
+
|
109 |
+
with torch.inference_mode():
|
110 |
+
output_ids = model.generate(
|
111 |
+
input_ids,
|
112 |
+
images_or_videos=tensor,
|
113 |
+
modal_list=modal_list,
|
114 |
+
do_sample=True,
|
115 |
+
temperature=0.2,
|
116 |
+
max_new_tokens=1024,
|
117 |
+
use_cache=True,
|
118 |
+
stopping_criteria=[stopping_criteria],
|
119 |
+
)
|
120 |
+
|
121 |
+
outputs = tokenizer.batch_decode(output_ids, skip_special_tokens=True)
|
122 |
+
print(outputs[0])
|
123 |
+
|
124 |
+
|
125 |
+
if __name__ == "__main__":
|
126 |
+
inference()
|
127 |
+
```
|
128 |
+
|
129 |
+
|
130 |
+
## Citation
|
131 |
+
If you find our project useful, hope you can star our repo and cite our paper as follows:
|
132 |
+
```
|
133 |
+
@article{damonlpsg2024videollama2,
|
134 |
+
author = {Cheng, Zesen and Leng, Sicong and Zhang, Hang and Xin, Yifei and Li, Xin and Chen, Guanzheng and Zhu, Yongxin and Zhang, Wenqi and Luo, Ziyang and Bing, Lidong},
|
135 |
+
title = {VideoLLaMA 2: Advancing Spatial-Temporal Modeling and Audio Understanding in Video-LLMs},
|
136 |
+
year = 2024,
|
137 |
+
journal = {arXiv preprint arXiv:2406.07476},
|
138 |
+
url = {https://arxiv.org/abs/2406.07476}
|
139 |
+
}
|
140 |
+
```
|