















Pillow==10.1.0
torch==2.1.2
torchvision==0.16.2
transformers==4.40.0
sentencepiece==0.1.99
decord1# test.py
2import torch
3from PIL import Image
4from transformers import AutoModel, AutoTokenizer
5
6model = AutoModel.from_pretrained('openbmb/MiniCPM-V-2_6', trust_remote_code=True,
7 attn_implementation='sdpa', torch_dtype=torch.bfloat16) # sdpa or flash_attention_2, no eager
8model = model.eval().cuda()
9tokenizer = AutoTokenizer.from_pretrained('openbmb/MiniCPM-V-2_6', trust_remote_code=True)
10
11image = Image.open('xx.jpg').convert('RGB')
12question = 'What is in the image?'
13msgs = [{'role': 'user', 'content': [image, question]}]
14
15res = model.chat(
16 image=None,
17 msgs=msgs,
18 tokenizer=tokenizer
19)
20print(res)
21
22## if you want to use streaming, please make sure sampling=True and stream=True
23## the model.chat will return a generator
24res = model.chat(
25 image=None,
26 msgs=msgs,
27 tokenizer=tokenizer,
28 sampling=True,
29 stream=True
30)
31
32generated_text = ""
33for new_text in res:
34 generated_text += new_text
35 print(new_text, flush=True, end='')1import torch
2from PIL import Image
3from transformers import AutoModel, AutoTokenizer
4
5model = AutoModel.from_pretrained('openbmb/MiniCPM-V-2_6', trust_remote_code=True,
6 attn_implementation='sdpa', torch_dtype=torch.bfloat16) # sdpa or flash_attention_2, no eager
7model = model.eval().cuda()
8tokenizer = AutoTokenizer.from_pretrained('openbmb/MiniCPM-V-2_6', trust_remote_code=True)
9
10image1 = Image.open('image1.jpg').convert('RGB')
11image2 = Image.open('image2.jpg').convert('RGB')
12question = 'Compare image 1 and image 2, tell me about the differences between image 1 and image 2.'
13
14msgs = [{'role': 'user', 'content': [image1, image2, question]}]
15
16answer = model.chat(
17 image=None,
18 msgs=msgs,
19 tokenizer=tokenizer
20)
21print(answer)1import torch
2from PIL import Image
3from transformers import AutoModel, AutoTokenizer
4
5model = AutoModel.from_pretrained('openbmb/MiniCPM-V-2_6', trust_remote_code=True,
6 attn_implementation='sdpa', torch_dtype=torch.bfloat16) # sdpa or flash_attention_2, no eager
7model = model.eval().cuda()
8tokenizer = AutoTokenizer.from_pretrained('openbmb/MiniCPM-V-2_6', trust_remote_code=True)
9
10question = "production date"
11image1 = Image.open('example1.jpg').convert('RGB')
12answer1 = "2023.08.04"
13image2 = Image.open('example2.jpg').convert('RGB')
14answer2 = "2007.04.24"
15image_test = Image.open('test.jpg').convert('RGB')
16
17msgs = [
18 {'role': 'user', 'content': [image1, question]}, {'role': 'assistant', 'content': [answer1]},
19 {'role': 'user', 'content': [image2, question]}, {'role': 'assistant', 'content': [answer2]},
20 {'role': 'user', 'content': [image_test, question]}
21]
22
23answer = model.chat(
24 image=None,
25 msgs=msgs,
26 tokenizer=tokenizer
27)
28print(answer)1import torch
2from PIL import Image
3from transformers import AutoModel, AutoTokenizer
4from decord import VideoReader, cpu # pip install decord
5
6model = AutoModel.from_pretrained('openbmb/MiniCPM-V-2_6', trust_remote_code=True,
7 attn_implementation='sdpa', torch_dtype=torch.bfloat16) # sdpa or flash_attention_2, no eager
8model = model.eval().cuda()
9tokenizer = AutoTokenizer.from_pretrained('openbmb/MiniCPM-V-2_6', trust_remote_code=True)
10
11MAX_NUM_FRAMES=64 # if cuda OOM set a smaller number
12
13def encode_video(video_path):
14 def uniform_sample(l, n):
15 gap = len(l) / n
16 idxs = [int(i * gap + gap / 2) for i in range(n)]
17 return [l[i] for i in idxs]
18
19 vr = VideoReader(video_path, ctx=cpu(0))
20 sample_fps = round(vr.get_avg_fps() / 1) # FPS
21 frame_idx = [i for i in range(0, len(vr), sample_fps)]
22 if len(frame_idx) > MAX_NUM_FRAMES:
23 frame_idx = uniform_sample(frame_idx, MAX_NUM_FRAMES)
24 frames = vr.get_batch(frame_idx).asnumpy()
25 frames = [Image.fromarray(v.astype('uint8')) for v in frames]
26 print('num frames:', len(frames))
27 return frames
28
29video_path ="video_test.mp4"
30frames = encode_video(video_path)
31question = "Describe the video"
32msgs = [
33 {'role': 'user', 'content': frames + [question]},
34]
35
36# Set decode params for video
37params={}
38params["use_image_id"] = False
39params["max_slice_nums"] = 2 # use 1 if cuda OOM and video resolution > 448*448
40
41answer = model.chat(
42 image=None,
43 msgs=msgs,
44 tokenizer=tokenizer,
45 **params
46)
47print(answer)1@article{yao2024minicpm,
2 title={MiniCPM-V: A GPT-4V Level MLLM on Your Phone},
3 author={Yao, Yuan and Yu, Tianyu and Zhang, Ao and Wang, Chongyi and Cui, Junbo and Zhu, Hongji and Cai, Tianchi and Li, Haoyu and Zhao, Weilin and He, Zhihui and others},
4 journal={arXiv preprint arXiv:2408.01800},
5 year={2024}
6}