Views
No views yet



'summarize:' and tokenizer.pad_token_id = 0 are 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-Flickr30K-FT-364px',
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-Flickr30K-FT-364px')
14
15tokenizer = AutoTokenizer.from_pretrained(
16 'OpenGVLab/InternVL-14B-Flickr30K-FT-364px', 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
41# InternVL-G
42logits_per_image, logits_per_text = model(
43 image=pixel_values, text=input_ids, mode='InternVL-G')
44probs = logits_per_image.softmax(dim=-1)1@article{chen2023internvl,
2 title={InternVL: Scaling up Vision Foundation Models and Aligning for Generic Visual-Linguistic Tasks},
3 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 Li, Bin and Luo, Ping and Lu, Tong and Qiao, Yu and Dai, Jifeng},
4 journal={arXiv preprint arXiv:2312.14238},
5 year={2023}
6}