Views
No views yet



[!Warning] 🚨 Note: the prefix'summarize:'andtokenizer.pad_token_id = 0are necessary. Their absence will lead to abnormal results.
1import torch
2from PIL import Image
3from transformers import AutoModel, CLIPImageProcessor
4from transformers import AutoTokenizer
5
6
7model = AutoModel.from_pretrained(
8 'OpenGVLab/InternVL-14B-224px',
9 torch_dtype=torch.bfloat16,
10 low_cpu_mem_usage=True,
11 trust_remote_code=True).cuda().eval()
12
13image_processor = CLIPImageProcessor.from_pretrained('OpenGVLab/InternVL-14B-224px')
14
15tokenizer = AutoTokenizer.from_pretrained(
16 'OpenGVLab/InternVL-14B-224px', use_fast=False, add_eos_token=True)
17tokenizer.pad_token_id = 0 # set pad_token_id to 0
18
19images = [
20 Image.open('./examples/image1.jpg').convert('RGB'),
21 Image.open('./examples/image2.jpg').convert('RGB'),
22 Image.open('./examples/image3.jpg').convert('RGB')
23]
24prefix = 'summarize:'
25texts = [
26 prefix + 'a photo of a red panda', # English
27 prefix + '一张熊猫的照片', # Chinese
28 prefix + '二匹の猫の写真' # Japanese
29]
30
31pixel_values = image_processor(images=images, return_tensors='pt').pixel_values
32pixel_values = pixel_values.to(torch.bfloat16).cuda()
33input_ids = tokenizer(texts, return_tensors='pt', max_length=80,
34 truncation=True, padding='max_length').input_ids.cuda()
35
36# InternVL-C
37logits_per_image, logits_per_text = model(
38 image=pixel_values, text=input_ids, mode='InternVL-C')
39probs = logits_per_image.softmax(dim=-1)
40# tensor([[9.9609e-01, 5.2185e-03, 6.0070e-08],
41# [2.2949e-02, 9.7656e-01, 5.9903e-06],
42# [3.2932e-06, 7.4863e-05, 1.0000e+00]], device='cuda:0',
43# dtype=torch.bfloat16, grad_fn=<SoftmaxBackward0>)
44
45# InternVL-G
46logits_per_image, logits_per_text = model(
47 image=pixel_values, text=input_ids, mode='InternVL-G')
48probs = logits_per_image.softmax(dim=-1)
49# tensor([[9.9609e-01, 3.1738e-03, 3.6322e-08],
50# [8.6060e-03, 9.9219e-01, 2.8759e-06],
51# [1.7583e-06, 3.1233e-05, 1.0000e+00]], device='cuda:0',
52# dtype=torch.bfloat16, grad_fn=<SoftmaxBackward0>)
53
54# please set add_eos_token to False for generation
55tokenizer.add_eos_token = False
56image = Image.open('./examples/image1.jpg').convert('RGB')
57pixel_values = image_processor(images=image, return_tensors='pt').pixel_values
58pixel_values = pixel_values.to(torch.bfloat16).cuda()
59
60tokenized = tokenizer("English caption:", return_tensors='pt')
61pred = model.generate(
62 pixel_values=pixel_values,
63 input_ids=tokenized.input_ids.cuda(),
64 attention_mask=tokenized.attention_mask.cuda(),
65 num_beams=5,
66 min_new_tokens=8,
67)
68caption = tokenizer.decode(pred[0].cpu(), skip_special_tokens=True).strip()
69# English caption: a red panda sitting on top of a wooden platform1@article{chen2024expanding,
2 title={Expanding Performance Boundaries of Open-Source Multimodal Models with Model, Data, and Test-Time Scaling},
3 author={Chen, Zhe and Wang, Weiyun and Cao, Yue and Liu, Yangzhou and Gao, Zhangwei and Cui, Erfei and Zhu, Jinguo and Ye, Shenglong and Tian, Hao and Liu, Zhaoyang and others},
4 journal={arXiv preprint arXiv:2412.05271},
5 year={2024}
6}
7@article{gao2024mini,
8 title={Mini-internvl: A flexible-transfer pocket multimodal model with 5\% parameters and 90\% performance},
9 author={Gao, Zhangwei and Chen, Zhe and Cui, Erfei and Ren, Yiming and Wang, Weiyun and Zhu, Jinguo and Tian, Hao and Ye, Shenglong and He, Junjun and Zhu, Xizhou and others},
10 journal={arXiv preprint arXiv:2410.16261},
11 year={2024}
12}
13@article{chen2024far,
14 title={How Far Are We to GPT-4V? Closing the Gap to Commercial Multimodal Models with Open-Source Suites},
15 author={Chen, Zhe and Wang, Weiyun and Tian, Hao and Ye, Shenglong and Gao, Zhangwei and Cui, Erfei and Tong, Wenwen and Hu, Kongzhi and Luo, Jiapeng and Ma, Zheng and others},
16 journal={arXiv preprint arXiv:2404.16821},
17 year={2024}
18}
19@inproceedings{chen2024internvl,
20 title={Internvl: Scaling up vision foundation models and aligning for generic visual-linguistic tasks},
21 author={Chen, Zhe and Wu, Jiannan and Wang, Wenhai and Su, Weijie and Chen, Guo and Xing, Sen and Zhong, Muyan and Zhang, Qinglong and Zhu, Xizhou and Lu, Lewei and others},
22 booktitle={Proceedings of the IEEE/CVF Conference on Computer Vision and Pattern Recognition},
23 pages={24185--24198},
24 year={2024}
25}