Views
No views yet
['sdpa', 'flash_attention_2'].1import torch
2model_path = 'mPLUG/mPLUG-Owl3-7B-240728'
3config = mPLUGOwl3Config.from_pretrained(model_path)
4print(config)
5# model = mPLUGOwl3Model(config).cuda().half()
6model = mPLUGOwl3Model.from_pretrained(model_path, attn_implementation='sdpa', torch_dtype=torch.half)
7model.eval().cuda()1from PIL import Image
2
3from transformers import AutoTokenizer, AutoProcessor
4from decord import VideoReader, cpu # pip install decord
5model_path = 'mPLUG/mPLUG-Owl3-7B-240728'
6tokenizer = AutoTokenizer.from_pretrained(model_path)
7processor = model.init_processor(tokenizer)
8
9image = Image.new('RGB', (500, 500), color='red')
10
11messages = [
12 {"role": "user", "content": """<|image|>
13Describe this image."""},
14 {"role": "assistant", "content": ""}
15]
16
17inputs = processor(messages, images=[image], videos=None)
18
19inputs.to('cuda')
20inputs.update({
21 'tokenizer': tokenizer,
22 'max_new_tokens':100,
23 'decode_text':True,
24})
25
26
27g = model.generate(**inputs)
28print(g)1from PIL import Image
2
3from transformers import AutoTokenizer, AutoProcessor
4from decord import VideoReader, cpu # pip install decord
5model_path = 'mPLUG/mPLUG-Owl3-7B-240728'
6tokenizer = AutoTokenizer.from_pretrained(model_path)
7processor = model.init_processor(tokenizer)
8
9
10messages = [
11 {"role": "user", "content": """<|video|>
12Describe this video."""},
13 {"role": "assistant", "content": ""}
14]
15
16videos = ['/nas-mmu-data/examples/car_room.mp4']
17
18MAX_NUM_FRAMES=16
19
20def encode_video(video_path):
21 def uniform_sample(l, n):
22 gap = len(l) / n
23 idxs = [int(i * gap + gap / 2) for i in range(n)]
24 return [l[i] for i in idxs]
25
26 vr = VideoReader(video_path, ctx=cpu(0))
27 sample_fps = round(vr.get_avg_fps() / 1) # FPS
28 frame_idx = [i for i in range(0, len(vr), sample_fps)]
29 if len(frame_idx) > MAX_NUM_FRAMES:
30 frame_idx = uniform_sample(frame_idx, MAX_NUM_FRAMES)
31 frames = vr.get_batch(frame_idx).asnumpy()
32 frames = [Image.fromarray(v.astype('uint8')) for v in frames]
33 print('num frames:', len(frames))
34 return frames
35video_frames = [encode_video(_) for _ in videos]
36inputs = processor(messages, images=None, videos=video_frames)
37
38inputs.to('cuda')
39inputs.update({
40 'tokenizer': tokenizer,
41 'max_new_tokens':100,
42 'decode_text':True,
43})
44
45
46g = model.generate(**inputs)
47print(g)@misc{ye2024mplugowl3longimagesequenceunderstanding,
title={mPLUG-Owl3: Towards Long Image-Sequence Understanding in Multi-Modal Large Language Models},
author={Jiabo Ye and Haiyang Xu and Haowei Liu and Anwen Hu and Ming Yan and Qi Qian and Ji Zhang and Fei Huang and Jingren Zhou},
year={2024},
eprint={2408.04840},
archivePrefix={arXiv},
primaryClass={cs.CV},
url={https://arxiv.org/abs/2408.04840},
}