Views
No views yet
pip install torch transformers accelerate pillow1import torch
2import transformers
3from transformers import AutoModelForCausalLM, AutoTokenizer
4from PIL import Image
5import warnings
6
7# disable some warnings
8transformers.logging.set_verbosity_error()
9transformers.logging.disable_progress_bar()
10warnings.filterwarnings('ignore')
11
12# set device
13torch.set_default_device('cpu') # or 'cuda'
14
15# create model
16model = AutoModelForCausalLM.from_pretrained(
17 'AI4VR/Bunny-MMR-3B',
18 torch_dtype=torch.float16,
19 device_map='auto',
20 trust_remote_code=True)
21tokenizer = AutoTokenizer.from_pretrained(
22 'AI4VR/Bunny-MMR-3B',
23 trust_remote_code=True)
24
25# text prompt
26prompt = 'text prompt'
27text = f"A chat between a curious user and an artificial intelligence assistant. The assistant gives helpful, detailed, and polite answers to the user's questions. USER: <image>\n{prompt} ASSISTANT:"
28text_chunks = [tokenizer(chunk).input_ids for chunk in text.split('<image>')]
29input_ids = torch.tensor(text_chunks[0] + [-200] + text_chunks[1][0:], dtype=torch.long).unsqueeze(0)
30
31# image, sample images can be found in images folder
32image = Image.open('path/to/image')
33image_tensor = model.process_images([image], model.config).to(dtype=model.dtype)
34
35# generate
36output_ids = model.generate(
37 input_ids,
38 images=image_tensor,
39 max_new_tokens=100,
40 use_cache=True)[0]
41
42print(tokenizer.decode(output_ids[input_ids.shape[1]:], skip_special_tokens=True).strip())1@misc{liu2024seeing,
2 title={Seeing Clearly, Answering Incorrectly: A Multimodal Robustness Benchmark for Evaluating MLLMs on Leading Questions},
3 author={Yexin Liu and Zhengyang Liang and Yueze Wang and Muyang He and Jian Li and Bo Zhao},
4 year={2024},
5 eprint={2406.10638},
6 archivePrefix={arXiv},
7}