chenjoya commited on
Commit
36848d7
·
verified ·
1 Parent(s): 8b382f5

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +423 -3
README.md CHANGED
@@ -1,3 +1,423 @@
1
- ---
2
- license: mit
3
- ---
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ license: apache-2.0
3
+ datasets:
4
+ - chenjoya/Live-CC-5M
5
+ language:
6
+ - en
7
+ base_model:
8
+ - Qwen/Qwen2-VL-7B
9
+ tags:
10
+ - multimodal
11
+ - streaming
12
+ ---
13
+
14
+ # Live-CC-7B-Base
15
+
16
+ ## Introduction
17
+
18
+ We introduce LiveCC, the first multimodal LLM with real-time video commentary capability, and also strong at general image/video tasks.
19
+
20
+ - Project Page: https://showlab.github.io/livecc
21
+ - Paper: https://arxiv.org/abs/xxxx.xxxxx
22
+ - Demo: https://huggingface.co/spaces/chenjoya/livecc-7b-base
23
+ - Training Code: https://www.github.com/showlab/videollm
24
+
25
+ > [!Important]
26
+ > This is the base model, pre-trained on [Live-CC-5M](https://huggingface.co/datasets/chenjoya/Live-CC-5M) dataset only with our proposed streaming frame-words paradigm. The instruction tuned model is [LiveCC-7B-Instruct](https://huggingface.co/chenjoya/LiveCC-7B-Instruct).
27
+
28
+
29
+ ## Quickstart
30
+ Like qwen-vl-utils, we offer a toolkit to help you handle various types of visual input more conveniently, **especially on video streaming inputs**. You can install it using the following command:
31
+
32
+ ```bash
33
+ pip install qwen-vl-utils livecc-utils
34
+ ```
35
+
36
+ Here we show a code snippet to show you how to use the chat model with `transformers` and the above utils:
37
+
38
+ ```python
39
+ from transformers import Qwen2VLForConditionalGeneration, AutoTokenizer, AutoProcessor
40
+ from qwen_vl_utils import process_vision_info
41
+
42
+ # default: Load the model on the available device(s)
43
+ model = Qwen2VLForConditionalGeneration.from_pretrained(
44
+ "Qwen/Qwen2-VL-7B-Instruct", torch_dtype="auto", device_map="auto"
45
+ )
46
+
47
+ # We recommend enabling flash_attention_2 for better acceleration and memory saving, especially in multi-image and video scenarios.
48
+ # model = Qwen2VLForConditionalGeneration.from_pretrained(
49
+ # "Qwen/Qwen2-VL-7B-Instruct",
50
+ # torch_dtype=torch.bfloat16,
51
+ # attn_implementation="flash_attention_2",
52
+ # device_map="auto",
53
+ # )
54
+
55
+ # default processer
56
+ processor = AutoProcessor.from_pretrained("Qwen/Qwen2-VL-7B-Instruct")
57
+
58
+ # The default range for the number of visual tokens per image in the model is 4-16384. You can set min_pixels and max_pixels according to your needs, such as a token count range of 256-1280, to balance speed and memory usage.
59
+ # min_pixels = 256*28*28
60
+ # max_pixels = 1280*28*28
61
+ # processor = AutoProcessor.from_pretrained("Qwen/Qwen2-VL-7B-Instruct", min_pixels=min_pixels, max_pixels=max_pixels)
62
+
63
+ messages = [
64
+ {
65
+ "role": "user",
66
+ "content": [
67
+ {
68
+ "type": "image",
69
+ "image": "https://qianwen-res.oss-cn-beijing.aliyuncs.com/Qwen-VL/assets/demo.jpeg",
70
+ },
71
+ {"type": "text", "text": "Describe this image."},
72
+ ],
73
+ }
74
+ ]
75
+
76
+ # Preparation for inference
77
+ text = processor.apply_chat_template(
78
+ messages, tokenize=False, add_generation_prompt=True
79
+ )
80
+ image_inputs, video_inputs = process_vision_info(messages)
81
+ inputs = processor(
82
+ text=[text],
83
+ images=image_inputs,
84
+ videos=video_inputs,
85
+ padding=True,
86
+ return_tensors="pt",
87
+ )
88
+ inputs = inputs.to("cuda")
89
+
90
+ # Inference: Generation of the output
91
+ generated_ids = model.generate(**inputs, max_new_tokens=128)
92
+ generated_ids_trimmed = [
93
+ out_ids[len(in_ids) :] for in_ids, out_ids in zip(inputs.input_ids, generated_ids)
94
+ ]
95
+ output_text = processor.batch_decode(
96
+ generated_ids_trimmed, skip_special_tokens=True, clean_up_tokenization_spaces=False
97
+ )
98
+ print(output_text)
99
+ ```
100
+ <details>
101
+ <summary>Without qwen_vl_utils</summary>
102
+
103
+ ```python
104
+ from PIL import Image
105
+ import requests
106
+ import torch
107
+ from torchvision import io
108
+ from typing import Dict
109
+ from transformers import Qwen2VLForConditionalGeneration, AutoTokenizer, AutoProcessor
110
+
111
+ # Load the model in half-precision on the available device(s)
112
+ model = Qwen2VLForConditionalGeneration.from_pretrained(
113
+ "Qwen/Qwen2-VL-7B-Instruct", torch_dtype="auto", device_map="auto"
114
+ )
115
+ processor = AutoProcessor.from_pretrained("Qwen/Qwen2-VL-7B-Instruct")
116
+
117
+ # Image
118
+ url = "https://qianwen-res.oss-cn-beijing.aliyuncs.com/Qwen-VL/assets/demo.jpeg"
119
+ image = Image.open(requests.get(url, stream=True).raw)
120
+
121
+ conversation = [
122
+ {
123
+ "role": "user",
124
+ "content": [
125
+ {
126
+ "type": "image",
127
+ },
128
+ {"type": "text", "text": "Describe this image."},
129
+ ],
130
+ }
131
+ ]
132
+
133
+
134
+ # Preprocess the inputs
135
+ text_prompt = processor.apply_chat_template(conversation, add_generation_prompt=True)
136
+ # Excepted output: '<|im_start|>system\nYou are a helpful assistant.<|im_end|>\n<|im_start|>user\n<|vision_start|><|image_pad|><|vision_end|>Describe this image.<|im_end|>\n<|im_start|>assistant\n'
137
+
138
+ inputs = processor(
139
+ text=[text_prompt], images=[image], padding=True, return_tensors="pt"
140
+ )
141
+ inputs = inputs.to("cuda")
142
+
143
+ # Inference: Generation of the output
144
+ output_ids = model.generate(**inputs, max_new_tokens=128)
145
+ generated_ids = [
146
+ output_ids[len(input_ids) :]
147
+ for input_ids, output_ids in zip(inputs.input_ids, output_ids)
148
+ ]
149
+ output_text = processor.batch_decode(
150
+ generated_ids, skip_special_tokens=True, clean_up_tokenization_spaces=True
151
+ )
152
+ print(output_text)
153
+ ```
154
+ </details>
155
+ <details>
156
+ <summary>Multi image inference</summary>
157
+
158
+ ```python
159
+ # Messages containing multiple images and a text query
160
+ messages = [
161
+ {
162
+ "role": "user",
163
+ "content": [
164
+ {"type": "image", "image": "file:///path/to/image1.jpg"},
165
+ {"type": "image", "image": "file:///path/to/image2.jpg"},
166
+ {"type": "text", "text": "Identify the similarities between these images."},
167
+ ],
168
+ }
169
+ ]
170
+
171
+ # Preparation for inference
172
+ text = processor.apply_chat_template(
173
+ messages, tokenize=False, add_generation_prompt=True
174
+ )
175
+ image_inputs, video_inputs = process_vision_info(messages)
176
+ inputs = processor(
177
+ text=[text],
178
+ images=image_inputs,
179
+ videos=video_inputs,
180
+ padding=True,
181
+ return_tensors="pt",
182
+ )
183
+ inputs = inputs.to("cuda")
184
+
185
+ # Inference
186
+ generated_ids = model.generate(**inputs, max_new_tokens=128)
187
+ generated_ids_trimmed = [
188
+ out_ids[len(in_ids) :] for in_ids, out_ids in zip(inputs.input_ids, generated_ids)
189
+ ]
190
+ output_text = processor.batch_decode(
191
+ generated_ids_trimmed, skip_special_tokens=True, clean_up_tokenization_spaces=False
192
+ )
193
+ print(output_text)
194
+ ```
195
+ </details>
196
+
197
+ <details>
198
+ <summary>Video inference</summary>
199
+
200
+ ```python
201
+ # Messages containing a images list as a video and a text query
202
+ messages = [
203
+ {
204
+ "role": "user",
205
+ "content": [
206
+ {
207
+ "type": "video",
208
+ "video": [
209
+ "file:///path/to/frame1.jpg",
210
+ "file:///path/to/frame2.jpg",
211
+ "file:///path/to/frame3.jpg",
212
+ "file:///path/to/frame4.jpg",
213
+ ],
214
+ "fps": 1.0,
215
+ },
216
+ {"type": "text", "text": "Describe this video."},
217
+ ],
218
+ }
219
+ ]
220
+ # Messages containing a video and a text query
221
+ messages = [
222
+ {
223
+ "role": "user",
224
+ "content": [
225
+ {
226
+ "type": "video",
227
+ "video": "file:///path/to/video1.mp4",
228
+ "max_pixels": 360 * 420,
229
+ "fps": 1.0,
230
+ },
231
+ {"type": "text", "text": "Describe this video."},
232
+ ],
233
+ }
234
+ ]
235
+
236
+ # Preparation for inference
237
+ text = processor.apply_chat_template(
238
+ messages, tokenize=False, add_generation_prompt=True
239
+ )
240
+ image_inputs, video_inputs = process_vision_info(messages)
241
+ inputs = processor(
242
+ text=[text],
243
+ images=image_inputs,
244
+ videos=video_inputs,
245
+ padding=True,
246
+ return_tensors="pt",
247
+ )
248
+ inputs = inputs.to("cuda")
249
+
250
+ # Inference
251
+ generated_ids = model.generate(**inputs, max_new_tokens=128)
252
+ generated_ids_trimmed = [
253
+ out_ids[len(in_ids) :] for in_ids, out_ids in zip(inputs.input_ids, generated_ids)
254
+ ]
255
+ output_text = processor.batch_decode(
256
+ generated_ids_trimmed, skip_special_tokens=True, clean_up_tokenization_spaces=False
257
+ )
258
+ print(output_text)
259
+ ```
260
+ </details>
261
+
262
+ <details>
263
+ <summary>Batch inference</summary>
264
+
265
+ ```python
266
+ # Sample messages for batch inference
267
+ messages1 = [
268
+ {
269
+ "role": "user",
270
+ "content": [
271
+ {"type": "image", "image": "file:///path/to/image1.jpg"},
272
+ {"type": "image", "image": "file:///path/to/image2.jpg"},
273
+ {"type": "text", "text": "What are the common elements in these pictures?"},
274
+ ],
275
+ }
276
+ ]
277
+ messages2 = [
278
+ {"role": "system", "content": "You are a helpful assistant."},
279
+ {"role": "user", "content": "Who are you?"},
280
+ ]
281
+ # Combine messages for batch processing
282
+ messages = [messages1, messages1]
283
+
284
+ # Preparation for batch inference
285
+ texts = [
286
+ processor.apply_chat_template(msg, tokenize=False, add_generation_prompt=True)
287
+ for msg in messages
288
+ ]
289
+ image_inputs, video_inputs = process_vision_info(messages)
290
+ inputs = processor(
291
+ text=texts,
292
+ images=image_inputs,
293
+ videos=video_inputs,
294
+ padding=True,
295
+ return_tensors="pt",
296
+ )
297
+ inputs = inputs.to("cuda")
298
+
299
+ # Batch Inference
300
+ generated_ids = model.generate(**inputs, max_new_tokens=128)
301
+ generated_ids_trimmed = [
302
+ out_ids[len(in_ids) :] for in_ids, out_ids in zip(inputs.input_ids, generated_ids)
303
+ ]
304
+ output_texts = processor.batch_decode(
305
+ generated_ids_trimmed, skip_special_tokens=True, clean_up_tokenization_spaces=False
306
+ )
307
+ print(output_texts)
308
+ ```
309
+ </details>
310
+
311
+ ### More Usage Tips
312
+
313
+ For input images, we support local files, base64, and URLs. For videos, we currently only support local files.
314
+
315
+ ```python
316
+ # You can directly insert a local file path, a URL, or a base64-encoded image into the position where you want in the text.
317
+ ## Local file path
318
+ messages = [
319
+ {
320
+ "role": "user",
321
+ "content": [
322
+ {"type": "image", "image": "file:///path/to/your/image.jpg"},
323
+ {"type": "text", "text": "Describe this image."},
324
+ ],
325
+ }
326
+ ]
327
+ ## Image URL
328
+ messages = [
329
+ {
330
+ "role": "user",
331
+ "content": [
332
+ {"type": "image", "image": "http://path/to/your/image.jpg"},
333
+ {"type": "text", "text": "Describe this image."},
334
+ ],
335
+ }
336
+ ]
337
+ ## Base64 encoded image
338
+ messages = [
339
+ {
340
+ "role": "user",
341
+ "content": [
342
+ {"type": "image", "image": "data:image;base64,/9j/..."},
343
+ {"type": "text", "text": "Describe this image."},
344
+ ],
345
+ }
346
+ ]
347
+ ```
348
+ #### Image Resolution for performance boost
349
+
350
+ The model supports a wide range of resolution inputs. By default, it uses the native resolution for input, but higher resolutions can enhance performance at the cost of more computation. Users can set the minimum and maximum number of pixels to achieve an optimal configuration for their needs, such as a token count range of 256-1280, to balance speed and memory usage.
351
+
352
+ ```python
353
+ min_pixels = 256 * 28 * 28
354
+ max_pixels = 1280 * 28 * 28
355
+ processor = AutoProcessor.from_pretrained(
356
+ "Qwen/Qwen2-VL-7B-Instruct", min_pixels=min_pixels, max_pixels=max_pixels
357
+ )
358
+ ```
359
+
360
+ Besides, We provide two methods for fine-grained control over the image size input to the model:
361
+
362
+ 1. Define min_pixels and max_pixels: Images will be resized to maintain their aspect ratio within the range of min_pixels and max_pixels.
363
+
364
+ 2. Specify exact dimensions: Directly set `resized_height` and `resized_width`. These values will be rounded to the nearest multiple of 28.
365
+
366
+ ```python
367
+ # min_pixels and max_pixels
368
+ messages = [
369
+ {
370
+ "role": "user",
371
+ "content": [
372
+ {
373
+ "type": "image",
374
+ "image": "file:///path/to/your/image.jpg",
375
+ "resized_height": 280,
376
+ "resized_width": 420,
377
+ },
378
+ {"type": "text", "text": "Describe this image."},
379
+ ],
380
+ }
381
+ ]
382
+ # resized_height and resized_width
383
+ messages = [
384
+ {
385
+ "role": "user",
386
+ "content": [
387
+ {
388
+ "type": "image",
389
+ "image": "file:///path/to/your/image.jpg",
390
+ "min_pixels": 50176,
391
+ "max_pixels": 50176,
392
+ },
393
+ {"type": "text", "text": "Describe this image."},
394
+ ],
395
+ }
396
+ ]
397
+ ```
398
+
399
+ ## Limitations
400
+
401
+ While Qwen2-VL are applicable to a wide range of visual tasks, it is equally important to understand its limitations. Here are some known restrictions:
402
+
403
+ 1. Lack of Audio Support: The current model does **not comprehend audio information** within videos.
404
+ 2. Data timeliness: Our image dataset is **updated until June 2023**, and information subsequent to this date may not be covered.
405
+ 3. Constraints in Individuals and Intellectual Property (IP): The model's capacity to recognize specific individuals or IPs is limited, potentially failing to comprehensively cover all well-known personalities or brands.
406
+ 4. Limited Capacity for Complex Instruction: When faced with intricate multi-step instructions, the model's understanding and execution capabilities require enhancement.
407
+ 5. Insufficient Counting Accuracy: Particularly in complex scenes, the accuracy of object counting is not high, necessitating further improvements.
408
+ 6. Weak Spatial Reasoning Skills: Especially in 3D spaces, the model's inference of object positional relationships is inadequate, making it difficult to precisely judge the relative positions of objects.
409
+
410
+ These limitations serve as ongoing directions for model optimization and improvement, and we are committed to continually enhancing the model's performance and scope of application.
411
+
412
+ ## Citation
413
+
414
+ If you find our work helpful, feel free to give us a cite.
415
+
416
+ ```
417
+ @inproceedings{livecc,
418
+ author = {Joya Chen and Ziyun Zeng and Yiqi Lin and Wei Li and Zejun Ma and Mike Zheng Shou},
419
+ title = {LiveCC: Learning Video LLM with Streaming Speech Transcription at Scale},
420
+ booktitle = {CVPR},
421
+ year = {2025},
422
+ }
423
+ ```